//****************************************//
function validateMarkerComment(which)
{
var bPass = true;
var sMessage
var oField

//begin field checking
	switch (bPass)
	{
	case (which.myName.value.length == 0) :
	sMessage = "Please enter your name.";
	bPass=false;
	oField = which.myName;
	break;

	case (which.myComment.value.length==0) :
	sMessage = "Please enter your comments.";
	bPass=false;
	oField = which.myComment;
	break;

	case ((which.myFile.value !="" && which.myFile.value.toLowerCase().indexOf('.jpg') < 0)  && which.myFile.value.toLowerCase().indexOf('.png') < 0) :
	sMessage = "Only .jpg and .png files are allowed.";
	bPass=false;
	oField = which.myFile;
	break;


	default :
	break;
	}
if (!bPass)
	{
	window.alert(sMessage);
	oField.focus();
	return false;
	}
	else
	{
	return true;
	}

//end function
}
//****************************************//
function checkCount(which)
{
if (!document.getElementById("count") =="")
	{
	document.getElementById("count").innerHTML = which.value.length + "/500";
	}
}
//end function
//**********************************************************************
function validateMe(which)
{
var bPass = true;
var sMessage
var oField

//begin field checking
	switch (bPass)
	{

	case (which.TITLE.value.length ==0) :
	sMessage = "Title is required.";
	bPass=false;
	oField = which.TITLE;
	break;

	case (which.URL.value.length ==0) :
	sMessage = "Web Address is required.";
	bPass=false;
	oField = which.URL;
	break;

	case (which.DESCRIPTION.value.length >500) :
	sMessage = "Descriptions must be no more than 500 characters.";
	bPass=false;
	oField = which.DESCRIPTION;
	break;

	case (which.DESCRIPTION.value.length ==0) :
	sMessage = "Description is required.";
	bPass=false;
	oField = which.DESCRIPTION;
	break;

	case (which.CATEGORY_ID.value==0) :
	sMessage = "Please select a Directory Category.";
	bPass=false;
	oField = which.CATEGORY_ID;
	break;

	case (which.OWNER_NAME.value.length ==0) :
	sMessage = "Owner name is required.";
	bPass=false;
	oField = which.OWNER_NAME;
	break;

	case (which.OWNER_EMAIL.value.length ==0) :
	sMessage = "Owner E-Mail is required.";
	bPass=false;
	oField = which.OWNER_EMAIL;
	break;

	case (which.ADDRESS.value.length!=0 && which.CITY.value.length==0) :
	sMessage = "City required when address entered.";
	bPass=false;
	oField = which.CITY;
	break;

	case (which.ADDRESS.value.length!=0 && which.STATE.value.length==0) :
	sMessage = "State required when address entered.";
	bPass=false;
	oField = which.STATE;
	break;

	case (which.ADDRESS.value.length!=0 && which.ZIP.value.length==0) :
	sMessage = "Zip Code required when address entered.";
	bPass=false;
	oField = which.ZIP;
	break;


	default :
	break;
	}
if (!bPass)
	{
	window.alert(sMessage);
	oField.focus();
	return false;
	}
	else
	{
	return true;
	}
}
//**********************************************************************
function startUpload(){
      document.getElementById('f1_upload_process').style.visibility = 'visible';
      document.getElementById('f1_upload_form').style.visibility = 'hidden';
      return true;
}
//**********************************************************************
function stopUpload(success){
      var result = '';
      if (success == 1){
         result = '<span class="msg">The file was uploaded successfully!<\/span><br/><br/>';
      }
      else {
         result = '<span class="emsg">There was an error during file upload!<\/span><br/><br/>';
      }
      document.getElementById('f1_upload_process').style.visibility = 'hidden';
      //document.getElementById('f1_upload_form').innerHTML = result + '<label>File: <input name="myfile" type="file" size="30" /><\/label><label><input type="submit" name="submitBtn" class="sbtn" value="Upload" /><\/label>';
      document.getElementById('f1_upload_form').style.visibility = 'visible';      
      return true;   
}
//**********************************************************************
function showCommentForm()
{
var myForm = document.getElementById('myForm');
if (myForm.className=='visibleComments'){
	myForm.className = 'hiddenComments';
	}
else{
	myForm.className = 'visibleComments';
	}
}
//**********************************************************************
function val_utm(zone,letter,northing,easting)
{
var rad2deg = 180.0 / Math.PI;
var mrm = sanitize_refMeridian();
var lat;
var lon;
//var zone = 14;
//var northing = 3254185;
//var easting = 549288;
var d = 22;
var xtm = 0;
var g2 = 0;
var gW = 0;
var zl = 11;
if (zone < 0 || zone > 60 || isNaN(zone))
    {
    alert("Zone number is out of range.\nMust be between 1 and 60.");
    ++bulkErrors;
    return;
    }
  if (isNaN(northing))
    {
    alert("Northing should be numbers only,\nno letters or spaces.");
    ++bulkErrors;
    return;
    }
  if (isNaN(easting))
    {
    alert("Easting should be numbers only,\nno letters or spaces.");
    ++bulkErrors;
    return;
    }
  if (northing < 0 || northing > 10000000)
    {
    alert("Northing value is out of range");
    ++bulkErrors;
    return;
    }
  if (mrm == 0 && (easting < 160000 || easting > 834000) )
    {
    alert("Easting value is out of range");
    ++bulkErrors;
    return;
    }
  ellipsoid = geo_constants(d, xtm);

  if (ellipsoid.axis == 0)
    {
    alert("Ellipsoid must be entered");
    ++bulkErrors;
    return;
    }
  if (mrm == 0 && ellipsoid.scaleTm == 0.9999 && (zone < 1 || zone > 32))
      alert("MTM zone numbers only confirmed for 1-32, in Canada\nContinuing anyway");

  // reuse precise values if available (a very "boff" feature)
  // if (northing == floor(preciseNorthing + .5) && easting == floor(preciseEasting + .5))
  // {
  //   northing = preciseNorthing;
  //   easting = preciseEasting;
  // }
  // but only once, see later

  var axis = ellipsoid.axis;
  var eccent = ellipsoid.eccentricity;
  var scaleTm = ellipsoid.scaleTm;
  var eastingOrg = ellipsoid.eastingOrg;
  var k0 = scaleTm;

  var e1 = (1 - Math.sqrt(1 - eccent)) / (1 + Math.sqrt(1 - eccent));
  var x = easting - eastingOrg; //remove 500,000 meter offset for longitude
  var y = northing;

  // correlate index with letter
  var cx ="1C2D3E4F5G6H7J8K9L10M11N12P13Q14R15S16T17U18V19W20X";
  var cxl = String(zl);
  var cxi = cx.indexOf(cxl,0);
  var zletter = cx.charAt(cxi + cxl.length);
  var nhemisphere = 0;
  if (zletter >= "N" || scaleTm == 0.9999)  // MTM in north hemisphere only
    nhemishere = 1;
  else
    y -= 10000000.0; //remove 10,000,000 meter offset used for southern hemisphere

  var longorig = (zone - 1) * 6 - 180 + 3;  //+3 puts origin in middle of zone
  // @dc 180 - (7+76) * 3 - 1.5
  if (scaleTm == 0.9999)
  {
      // longorig = 180 - (zone*1 + 76) * 3 - 1.5;  // without hack, did string concat !!!
      if (mrm == 0)
      {
         zonerResult = gsrugZoner(0., 0., zone);
         longorig = zonerResult.refMeridian;
      }
      else
         longorig = mrm;
  }


  var eccPrimeSquared = (eccent) / (1-eccent);
  var M = y / k0;
  var mu = M / (axis * (1 - eccent / 4 - 3 * eccent * eccent / 64 - 5 * eccent * eccent * eccent / 256));
  var phi1Rad = mu + (3 * e1 / 2 - 27 * e1 * e1 * e1 / 32) * Math.sin(2 * mu) + (21 * e1 * e1 / 16 - 55 * e1 * e1 * e1 * e1 / 32) * Math.sin(4 * mu) + (151 * e1 * e1 * e1 / 96) * Math.sin(6 * mu);
  var phi1 = phi1Rad * Math.rad2deg;
  var N1 = axis / Math.sqrt(1 - eccent * Math.sin(phi1Rad) * Math.sin(phi1Rad));


  var T1 = Math.tan(phi1Rad) * Math.tan(phi1Rad);
  var C1 = eccPrimeSquared * Math.cos(phi1Rad) * Math.cos(phi1Rad);
  var R1 = axis * (1 - eccent) / Math.pow(1-eccent * Math.sin(phi1Rad) * Math.sin(phi1Rad), 1.5);
  var D = x / (N1 * k0);


  var lat = phi1Rad - (N1 * Math.tan(phi1Rad) / R1) * (D * D / 2 - (5 + 3 * T1 + 10 * C1 - 4 * C1 * C1 - 9 * eccPrimeSquared) * D * D * D * D / 24 + (61 + 90 * T1 + 298 * C1 + 45 * T1 * T1 - 252 * eccPrimeSquared - 3 * C1 * C1) * D * D * D * D * D * D / 720);

  var lat = lat * rad2deg;

  var lon = (D - (1 + 2 * T1 + C1) * D * D * D / 6 + (5 - 2 * C1 + 28 * T1 - 3 * C1 * C1 + 8 * eccPrimeSquared + 24 * T1 * T1) * D * D * D * D * D / 120) / Math.cos(phi1Rad);
  var lon = longorig + lon * rad2deg;

  todms3(lat, lon);

var myCoordinates = new Array();
myCoordinates[0] = lat;
myCoordinates[1] = lon;
return myCoordinates;
}
//**********************************************************************
function sanitize_refMeridian()
{
 //var rm = document.myform3.mtmRefMeridian.value.replace(/^\s+|\s+$/g, '');
 // return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
var rm="";

   if (rm == "")
      return 0.0;

   if (isNaN(rm))
   {
      alert("MTM ref meridian must be a number between -51 and -143, or totally blank\nIgnoring MTM ref meridian");
      return 0.0;
   }

   rm = Number(rm);

   if (rm > 0.0)  // fix sign if needed
   {
      rm = 0-rm;
   }

   if (rm > (-51) || rm < (-143))
   {
      alert("MTM ref meridian must be a number between -51 and -143, or totally blank\nIgnoring MTM ref meridian");
      return 0.0;
   }

   //document.myform3.mtmRefMeridian.value = rm;

   return rm;
}
//**********************************************************************
function geo_constants(ellipsoid, xtm)
{
  // returns ellipsoid values
  ellipsoid_axis = new Array();
  ellipsoid_eccen = new Array();
  ellipsoid_axis[0] = 6377563.396; ellipsoid_eccen[0] = 0.00667054;  //airy
  ellipsoid_axis[1] = 6377340.189; ellipsoid_eccen[1] = 0.00667054;  // mod airy
  ellipsoid_axis[2] = 6378160; ellipsoid_eccen[2] = 0.006694542;  //aust national
  ellipsoid_axis[3] = 6377397.155; ellipsoid_eccen[3] = 0.006674372;  //bessel 1841
  ellipsoid_axis[4] = 6378206.4; ellipsoid_eccen[4] = 0.006768658;  //clarke 1866 == NAD 27 (TBC)
  ellipsoid_axis[5] = 6378249.145; ellipsoid_eccen[5] = 0.006803511;  //clarke 1880
  ellipsoid_axis[6] = 6377276.345; ellipsoid_eccen[6] = 0.00637847;  //everest
  ellipsoid_axis[7] = 6377304.063; ellipsoid_eccen[7] = 0.006637847;  // mod everest
  ellipsoid_axis[8] = 6378166; ellipsoid_eccen[8] = 0.006693422;  //fischer 1960
  ellipsoid_axis[9] = 6378150; ellipsoid_eccen[9] = 0.006693422;  //fischer 1968
  ellipsoid_axis[10] = 6378155; ellipsoid_eccen[10] = 0.006693422;  // mod fischer
  ellipsoid_axis[11] = 6378160; ellipsoid_eccen[11] = 0.006694605;  //grs 1967
  ellipsoid_axis[12] = 6378137; ellipsoid_eccen[12] = 0.00669438;  //  grs 1980
  ellipsoid_axis[13] = 6378200; ellipsoid_eccen[13] = 0.006693422;  // helmert 1906
  ellipsoid_axis[14] = 6378270; ellipsoid_eccen[14] = 0.006693422;  // hough
  ellipsoid_axis[15] = 6378388; ellipsoid_eccen[15] = 0.00672267;  // int24
  ellipsoid_axis[16] = 6378245; ellipsoid_eccen[16] = 0.006693422;  // krassovsky
  ellipsoid_axis[17] = 6378160; ellipsoid_eccen[17] = 0.006694542;  // s america
  ellipsoid_axis[18] = 6378165; ellipsoid_eccen[18] = 0.006693422;  // wgs-60
  ellipsoid_axis[19] = 6378145; ellipsoid_eccen[19] = 0.006694542;  // wgs-66
  ellipsoid_axis[20] = 6378135; ellipsoid_eccen[20] = 0.006694318;  // wgs-72
  ellipsoid_axis[21] = 6378137; ellipsoid_eccen[21] = 0.00669438;  //wgs-84

  if (ellipsoid == 0)
      ellipsoid = 22;

  --ellipsoid; // table indexed differently

  if (xtm == 1) // WAS: if (ellipsoid > 22)
  {
     scaleTm = 0.9999;
     eastingOrg = 304800.;
  }
  else
  {
     scaleTm = 0.9996;
     eastingOrg = 500000.;
  }

  // return values as an object
  var ellipsoid = { axis: ellipsoid_axis[ellipsoid],
                    eccentricity: ellipsoid_eccen[ellipsoid],
                    eastingOrg: eastingOrg,
                    scaleTm: scaleTm
                    };
  return ellipsoid;
}
//**********************************************************************
function todms3(lat, lon)
// convert decimal degrees to dms
{
  var latbrg = 1;
  var lonbrg = 2;
  if (lat < 0)
    latbrg = 2
  if (lon < 0)
    lonbrg = 1;
  var tlat = Math.abs(lat) + 0.5 / 360000;  // round up 0.005 seconds (1/100th)
  var tlon = Math.abs(lon) + 0.5 / 360000;

  var tlatdm = Math.abs(lat) + 0.5 / 60000;  // round up 0.0005 minutes (1/1000th)
  var tlondm = Math.abs(lon) + 0.5 / 60000;

  var deglat = Math.floor(tlat);

  var t = (tlat - deglat) * 60;
  var minlat = Math.floor(t);

  var minlatdm = Math.floor((tlatdm - Math.floor(tlatdm)) * 60 * 1000) / 1000;

  var seclat = (t - minlat) * 60;
  seclat = Math.floor(seclat * 100) / 100;  //  works in js 1.4
  var deglon = Math.floor(tlon);

  t = (tlon - deglon) * 60;
  var minlon = Math.floor(t);

  var minlondm = Math.floor((tlondm - Math.floor(tlondm)) * 60 * 1000) / 1000;

  var seclon = (t - minlon) * 60;
  seclon = Math.floor(seclon * 100) / 100;  // js 1.4
}
//**********************************************************************
function showMap(zone,letter,northing,easting, mtitle)
{
var myCoordinates = new Array();
var myCoordinates = val_utm(zone,letter,northing,easting);
var lat = myCoordinates[0];
var lon = myCoordinates[1];
var Marker = mtitle;

if (GBrowserIsCompatible()) {
	var map = new GMap2(document.getElementById("map"));
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	map.enableScrollWheelZoom();
        	map.setCenter(new GLatLng(lat, lon), 16);
	var point = new GLatLng(lat, lon);
          	map.addOverlay(new GMarker(point));
        	var marker = new GMarker(point);
        	map.addOverlay(marker);
        	//marker.openInfoWindowHtml(Marker);
        	}
}
//***************************************************************
function showAddress(title,street,city,state,zip,phone)
 { 
if (GBrowserIsCompatible()) { 
var map = new GMap2(document.getElementById("detailMap")); 
} 
geocoder = new GClientGeocoder(); 
var latitude = ''; 
var longitude = ''; 
if (geocoder) { 
var address = street + ", " + city + "," + state + " " + zip; 
geocoder.getLatLng(address, 
function(point) { 
if (point) { 
map.addControl(new GSmallMapControl()); 
map.addControl(new GMapTypeControl()); 
map.setCenter(point, 15); 
map.addOverlay(new GMarker(point)); 
function createMarker(point) { 
var marker = new GMarker(point); 
GEvent.addListener(marker, "click", function() { 

marker.openInfoWindowHtml("<img border='0' align='absmiddle' src = '/new_style/texas_star.gif' width='20'><strong>" + title + "</strong><br>" + street + "<br>" + city + " " + state + " " + zip + "<br>" + phone); }); 
return marker; 
} 
map.addOverlay(createMarker(point)); 
} 
} 
); 
} 
} 

//***************************************************************//
