function GPSCoordinate(coords, type)
{
	if (type == null)
	  type = "lat";

	this.type = type;
	this.dir = "";
	this.deg = "";
	this.min = "";
	this.sec = "";
	this.wholeSec = "";
	this.dec = 0;
	this.sign = "";
	this.cset = new Array();

	this.parse(coords, type);
}


GPSCoordinate.prototype.parse = GPSCoordinate_parse;

function GPSCoordinate_parse(coords, type) 
{
    this.cset['unparsed'] = coords;     
	this.type = type;
	this.sign = "";

//	alert("matching '" + coords + "'\n");

	var matches = new Array();

	// "sa(N38.8600667,W9.2310667"
	if (matches = coords.match(/([NWSE\-])?[^0-9NWSE\-]{0,3}0*([0-9]{1,3})[^0-9]{1,3}([0-9]{1,2})([^0-9]{1,3})([0-9]{1,3})([NWSE])?/i))
        {
	    // GPS Coordinates
//	    alert("GPS match in GPSCoordinate coords " + matches);

//	    alert(parseInt(matches[2]));

	    this.dir = matches[1];
	    this.deg = parseInt(matches[2], 10);
	    this.min = parseInt(matches[3], 10);

	    if (matches[1] == "" && matches[2] != "")
	    {
	      this.dir = matches[2];
	    }

	    if (matches[5].length < 3 && parseInt(matches[5]) < 60 && matches[4].indexOf('.') < 0)
	    {
//	       alert("using dms");
	       // we probably have DMS coords
	       this.sec = parseInt(matches[5], 10) / 60.0;
	    }
	    else
            {
  //	    # can be 600, 60 or 6
	      this.sec = parseFloat("." + matches[5]);
	    }

            this.dec = (this.min + this.sec)/60.0;

//	    alert("dir = " + this.dir + ", deg = " + this.deg + ", min = " + this.min + ", sec = " + this.sec);

	}
	else
	{
//	    alert("dec match in GPSCoordinate coords<BR>\n");

	    // Decimal coordinates

            matches = coords.match(/([NWSE\-\+])?[^0-9NWSE\-\+]*(([0-9]{1,3})\.([0-9]{2,}))/i);

            // 0 -71.46755
            // 1 -
 	    // 2 71.46755
	    // 3 71
	    // 4 46755

//	    alert("matches: \'" +  matches.join( "', '") + "\'");
	
	    // 1 = sign, 2 = deg, 3 = dec

	    this.dir = matches[1];
	    this.deg = parseInt(matches[3], 10);
//	    alert(this.deg);
            this.dec = parseFloat("0." + matches[4]);

	    this.min = (matches[2] - matches[3]) * 60.0;
	    this.sec = (this.min - Math.floor(this.min));
	    this.min = Math.floor(this.min);

        }

	// common parsing

        this.dir = String("" + this.dir).toUpperCase().trim();

	if (this.dir == "S" || this.dir == "W")
	{
	  this.sign = "-";
	}
	else if (this.dir == "-")
        {
           this.sign = "-";
	   if (type == "lat")
           {
	      this.dir = "S";
	   }
	   else
           {
	      this.dir = "W";
	   }
        }
	else
        {
	   this.dir = "";
	   if (type == "lat")
           {
	      this.dir = "N";
	   }
	   else
           {
	      this.dir = "E";
	   }
	}

	this.wholeSec = Math.floor((this.sec * 60.0));

   }	


   // TODO: this doesn't handle going from W to E or N to S

GPSCoordinate.prototype.addGPS = GPSCoordinate_addGPS;

   function GPSCoordinate_addGPS(deg, min)
   {
	deg = this.deg + deg;
	
	newmin = this.min + this.sec + min;
	if (newmin > 60)
	{
	  deg++;
	  newmin -= 60;
	}
	else if (newmin < 0)
	{
	  deg--;
	  newmin += 60;
	}
 	
	newstr = sprintf("%s %d %02.3f ", this.dir, deg, newmin);
//	alert("parsing newstr\n");
	this.parse(newstr, this.type);
   }

GPSCoordinate.prototype.toGPSString = GPSCoordinate_toGPSString;

function GPSCoordinate_toGPSString()
{
  return this.dir + " " + this.deg + " " + lz((this.min + this.sec).toFixed(3), 6);
}

function GPSCoordinate_toDec()
{
 return parseFloat(this.sign + (this.deg + this.dec));
}
GPSCoordinate.prototype.toDec = GPSCoordinate_toDec;

function GPSCoordinate_toDecString()
{
	return this.sign + (this.deg + this.dec);
}
GPSCoordinate.prototype.toDecString = GPSCoordinate_toDecString;

function GPSCoordinate_toDMSString()
{
	return sprintf("%s%02d %s %s", this.dir, this.deg, lz(this.min,2), lz(this.wholeSec,2));
}
GPSCoordinate.prototype.toDMSString = GPSCoordinate_toDMSString;



