
/*
    Microformats RFC2426 Geo Normalisation 0.1
    Author: Glenn Jones
    
	The MIT License
	
	Copyright (c) 2008 Glenn Jones
	
	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:
	
	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.
	
	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
		
*/

function RFC2426Geo() {
	
	this.latitude = 0;
	this.longitude = 0;
	
	if(arguments[0])
		this.Parse( arguments[0] );
}

RFC2426Geo.prototype.Parse = function( geo ) {
	
	this.Reset();
	if (geo.indexOf(";") > 0)
    {
    	parts = geo.split(';');
    	
    	// Make sure no more 6 decimal places
    	this.latitude = parseFloat(parts[0]).toFixed(6);
        this.longitude = parseFloat(parts[1]).toFixed(6);

    	// Remove trailing zero's
    	this.latitude = parseFloat(this.FormatNumber(String(this.latitude)));
        this.longitude = parseFloat(this.FormatNumber(String(this.longitude)));

        if (this.latitude > 90 || this.latitude < -90)
            this.latitude = 0;

        if (this.longitude > 180 || this.longitude < -180)
            this.longitude = 0;
            
    }
}

RFC2426Geo.prototype.Reset = function() {
	this.latitude = 0;
	this.longitude = 0;
}

// Returns location in RFC2426 geo format
RFC2426Geo.prototype.toString = function() {
	return this.latitude + ";" + this.longitude;
}

RFC2426Geo.prototype.FormatNumber = function( geo ) {

	var decPos = geo.indexOf(".")
	if (decPos > -1){
		first = geo.substring(0,decPos);
		second = geo.substring(decPos,geo.length);

		while (second.charAt(second.length-1) == "0")
			second = second.substring(0,second.length-1);

		if (second.length > 1)
			return first + second;
		else
			return first;

	}
	return geo;
}

function CompareRFC2426Geo( geo1, geo2 ) {
	var converted1 = new RFC2426Geo( geo1 ).toString();
	var converted2 = new RFC2426Geo( geo2 ).toString();
	if( ( converted1 == '') && (converted2 == '') ){
		return false;
	}else{
		if( converted1 == converted2 )
			return true;
		else
			return false;
     }
}











  	


