
var latLongResults = "";
function validateUserForm(theForm){
    validateLatLong(theForm);
    theForm.geo_lat.value = theForm.geo_lat_deg_only.value;
    theForm.geo_long.value = theForm.geo_long_deg_only.value;
    var err_desc =  "Please select/enter the exact location of your rain gauge within your parcel.\n"+
                    "Zoom in/out as necessary to get a better view of your land.\n" +
                    "Select the Hybrid map to see street names.\n" +
                    "Use the map arrows to navigate accross the map.";
    if (typeof gLatFound != "undefined" && 
        typeof gLongFound != "undefined"){
            if (gLatFound == theForm.geo_lat.value && gLongFound == theForm.geo_long.value){
                alert("Error: You are using the default latitude and longitude that our server found for your address.\n" +
                      err_desc);
                return;
            }
    }
    if (theForm.geo_lat.value == DEFAULT_LAT && theForm.geo_long.value == DEFAULT_LONG){
        alert("Error: You are using the default latitude and longitude of our website.\n" +
              err_desc);
        return;
     }

    var m = 0;
    var reqFields = new Array();
    reqFields[m++] = new ReqField(theForm.userName, "text", new Array("Username is required"), new Array("== \"\""));
    reqFields[m++] = new ReqField(theForm.password, "text", 
			    new Array("Password is required", "The passwords don't match.\nPlease re-type them."), 
			    new Array("== \"\"", "!= \"" + theForm.password2.value + "\""));

    reqFields[m++] = new ReqField(theForm.firstName, "text", new Array("First name is required"), new Array("== \"\""));
    reqFields[m++] = new ReqField(theForm.lastName, "text", new Array("Last name is required"), new Array("== \"\""));
    reqFields[m++] = new ReqField(theForm.email, "email", new Array("Email is required and must be a valid email address"), new Array("== \"\""));
//    reqFields[m++] = new ReqField(theForm.address, "text", new Array("Address is required"), new Array("== \"\""));
//    reqFields[m++] = new ReqField(theForm.city, "text", new Array("City is required"), new Array("== \"\""));
//    reqFields[m++] = new ReqField(theForm.state, "select", new Array("State is required"), new Array("== \"\""));
    reqFields[m++] = new ReqField(theForm.zip, "text", new Array("Zip is required"), new Array("== \"\""));
    if (needAffiliationCode(theForm)){  //see userFields.js
        reqFields[m++] = new ReqField(theForm.affiliation_code, "text", 
        new Array("An affiliation code is needed for the selected group."), 
        new Array("== \"\""));
    }

    if (latLongResults != ""){
    	reqFields[m++] = new ReqField(theForm.geo_lat_deg, "other", new Array(latLongResults), new Array("1==1"));
    }
    //Phone not required
//    reqFields[m++] = new ReqField(theForm.phone, "text", new Array("Phone number is required"), new Array("== \"\""));
    if (typeof theForm.stbn_id != "undefined"){
        reqFields[m++] = new ReqField(theForm.stbn_id, "text", 
        new Array("Rain Gauge Id is required (look inside data logger)", "Rain Gauge Id must be 6-8 characters long"), 
        new Array("== \"\"", ".length < 6"));
    }

    if (validateForm(theForm, reqFields)){
	theForm.submit();
    }

}


function calcLatLongFromDegs(theForm){
    var latDegVal = theForm.geo_lat_deg_only.value;
    var latDeg = parseFloat(latDegVal);
    latDeg = isNaN(latDeg) || latDeg <= 0 ? 0 : latDeg;
    var tmp = dms(latDeg);
    theForm.geo_lat_deg.value = tmp[0];
    theForm.geo_lat_min.value = tmp[1];
    theForm.geo_lat_sec.value = tmp[2];

    var longDegVal = theForm.geo_long_deg_only.value;
    var longDeg  = parseFloat(longDegVal);
    longDeg = isNaN(longDeg) ? 0 : longDeg;
    var tmp = dms((longDeg < 0) ? longDeg *-1 : longDeg);
    theForm.geo_long_deg.value = tmp[0];
    theForm.geo_long_min.value = tmp[1];
    theForm.geo_long_sec.value = tmp[2];
}

function dms(deg){
    var retD = parseInt(deg);
    var retM = parseInt((deg - retD)  * 60);
    var retS = (deg - retD) * 3600 - retM*60;
    var returnValues = new Array(retD, retM, retS);
    return returnValues;

}

function calcLatLong(theForm){
    //All ok to proceed with this format
    //Convert all fields to numbers
    latd = parseInt(theForm.geo_lat_deg.value);
    latm = parseInt(theForm.geo_lat_min.value);
    lats = parseFloat(theForm.geo_lat_sec.value);
    longd = parseInt(theForm.geo_long_deg.value);
    longm = parseInt(theForm.geo_long_min.value);
    longs = parseFloat(theForm.geo_long_sec.value);
    latd = isNaN(latd) || latd <= 0 ? 0 : latd;
    latm = isNaN(latm) || latm <= 0 ? 0 : latm;
    lats = isNaN(lats) || lats <= 0 ? 0 : lats;
    longd = isNaN(longd) || longd <= 0 ? 0 : longd;
    longm = isNaN(longm) || longm <= 0 ? 0 : longm;
    longs = isNaN(longs) || longs <= 0 ? 0 : longs;

    //Calc. real degrees
    latDegReal = latd + (latm/60) + (lats/3600);
    longDegReal = longd + (longm/60) + (longs/3600);
    longDegReal = (longDegReal > 0) ? longDegReal *-1 : longDegReal;

    //All set
    //set geo_lat and geo_long fields in form
    theForm.geo_lat_deg_only.value = latDegReal;
    theForm.geo_long_deg_only.value = longDegReal;

    return;
}

function validateLatLong(theForm){
    latLongResults = "";
    var latDegReal = 0.0;
    var longDegReal = 0.0;
    var fieldsChecked = false;
    //See if the user filled any of the lat/long fields
    if (theForm.geo_lat_deg.value != "" || theForm.geo_lat_min.value != "" || theForm.geo_lat_sec.value != "" ||
	    theForm.geo_long_deg.value != "" || theForm.geo_long_min.value != "" || theForm.geo_long_sec.value != "" ||
	    theForm.geo_lat_deg_only.value != "" || theForm.geo_long_deg_only.value != "" ){
    } else {
	    //User didn't fill anything, return with no output msg.
	    theForm.geo_lat.value = latDegReal;
	    theForm.geo_long.value = longDegReal;
	    latLongResults = "Need your location in DMS or Degrees-only format. Use map if needed.";
	    return;
    }


    //If user filled any field then check that he filled all the set of fields for the given format
    //Check DMS format
    if (theForm.geo_lat_deg.value != "" || theForm.geo_lat_min.value != "" || theForm.geo_lat_sec.value != "" ||
	    theForm.geo_long_deg.value != "" || theForm.geo_long_min.value != "" || theForm.geo_long_sec.value != ""){
	    //Check all fields are filled for this format
	    if (theForm.geo_lat_deg.value != "" && theForm.geo_lat_min.value != "" && theForm.geo_lat_sec.value != "" &&
		    theForm.geo_long_deg.value != "" && theForm.geo_long_min.value != "" && theForm.geo_long_sec.value != ""){
		    //All ok to proceed with this format
		    //Convert all fields to numbers
		    latd = parseInt(theForm.geo_lat_deg.value);
		    latm = parseInt(theForm.geo_lat_min.value);
		    lats = parseFloat(theForm.geo_lat_sec.value);
		    longd = parseInt(theForm.geo_long_deg.value);
		    longm = parseInt(theForm.geo_long_min.value);
		    longs = parseFloat(theForm.geo_long_sec.value);
		    //Check all are numbers
		    if (isNaN(latd) || isNaN(latm) || isNaN(lats) || 
				     isNaN(longd)  || isNaN(longm)  || isNaN(longs)){
				     //There's an error with one of the fields. Return error.
				     latLongResults = "There's a non-numerical value in one of the fields for the DMS format";
				     return;
		    } 
		    if (latd <= 0.0 || latm < 0.0 || lats < 0.0 || 
				     longd <= 0.0 || longm < 0.0 || longs < 0.0){
				     //There's an error with one of the fields. Return error.
				     latLongResults = "Values in the DMS format can't be less than 0";
				     return;
		    } 
		    //Calc. real degrees
		    latDegReal = latd + (latm/60) + (lats/3600);
		    longDegReal = longd + (longm/60) + (longs/3600);
		    longDegReal = (longDegReal > 0) ? longDegReal *-1 : longDegReal;
		    fieldsChecked = true;
	    } else {
		    //There's an error here. Return to calling funnction sending this error
		    latLongResults = "Need to enter values for all the fields in the DMS format";
		    return;
	    }
    } 

    //Check Degrees Only format if didn't check with the other fields
    if (!fieldsChecked){
	    if (theForm.geo_lat_deg_only.value != "" || theForm.geo_long_deg_only.value != "" ){
		    //Check all fields are filled for this format
		    if (theForm.geo_lat_deg_only.value != "" && theForm.geo_long_deg_only.value != "" ){
			    //All ok to proceed with this format
			    latDegReal = parseFloat(theForm.geo_lat_deg_only.value);
			    longDegReal = parseFloat(theForm.geo_long_deg_only.value);
			    if (isNaN(latDegReal) || isNaN(longDegReal)){
				 //There's an error with one of the fields. Return error.
				 latLongResults = "There's a non-numerical value in one of the fields for the Degrees-only format";
				 return;
			    } 		
		    } else {
			    //There's an error here. Return to calling funnction sending this error
			    latLongResults = "Need to enter values for all the fields in the Degrees-only format";
			    return;
		    }
	    }
    }

    //Check lat/long to be within the State of Arizona 
    //		Latitude 	31�20'N to 37�N
    //		Longitude 	109�3'W to 114�50'W
    //		source: http://en.wikipedia.org/wiki/Arizona 
    /*
    if (latDegReal < 31 || latDegReal > 37 || longDegReal > -109 || longDegReal < -115){
	     //There's an error with the range of coordinates
	     latLongResults = "Verify the coordinates. They don\'t match our records for the area of the State of Arizona";
	     return;
    }*/

    return;
}

function validateRecoverForm(theForm){
//In this form the following fields are required: 
//Email (username)

	var reqFields = new Array();
	reqFields[0] = new ReqField(theForm.email, "email", new Array("Email is required and must be a valid email address"), new Array("== \"\""));

	if (validateForm(theForm, reqFields)){
			theForm.submit();
		
	}

}

function validateCommentForm(theForm){
//In this form the following fields are required: 
//Email (username)

	var reqFields = new Array();
	reqFields[0] = new ReqField(theForm.comment, "text", new Array("Your comments will be welcome. Please enter them in the appropriate box."), new Array("== \"\""));

	if (validateForm(theForm, reqFields)){
			theForm.submit();
		
	}

}

/********************* BACKUP OF OLD JS FUNCTION  ****************
function calcLatLong_OLD(){
    latLongResults = "";
    var latDegReal = 0.0;
    var longDegReal = 0.0;
    var fieldsChecked = false;
    //See if the user filled any of the lat/long fields
    if (theForm.geo_lat_deg.value != "" || theForm.geo_lat_min.value != "" || theForm.geo_lat_sec.value != "" ||
	    theForm.geo_long_deg.value != "" || theForm.geo_long_min.value != "" || theForm.geo_long_sec.value != "" ||
	    theForm.geo_lat_deg_only.value != "" || theForm.geo_long_deg_only.value != "" ){
    } else {
	    //User didn't fill anything, return with no output msg.
	    theForm.geo_lat.value = latDegReal;
	    theForm.geo_long.value = longDegReal;
	    latLongResults = "Need your location in DMS or Degrees-only format. Use map if needed.";
	    return;
    }


    //If user filled any field then check that he filled all the set of fields for the given format
    //Check DMS format
    if (theForm.geo_lat_deg.value != "" || theForm.geo_lat_min.value != "" || theForm.geo_lat_sec.value != "" ||
	    theForm.geo_long_deg.value != "" || theForm.geo_long_min.value != "" || theForm.geo_long_sec.value != ""){
	    //Check all fields are filled for this format
	    if (theForm.geo_lat_deg.value != "" && theForm.geo_lat_min.value != "" && theForm.geo_lat_sec.value != "" &&
		    theForm.geo_long_deg.value != "" && theForm.geo_long_min.value != "" && theForm.geo_long_sec.value != ""){
		    //All ok to proceed with this format
		    //Convert all fields to numbers
		    latd = parseInt(theForm.geo_lat_deg.value);
		    latm = parseInt(theForm.geo_lat_min.value);
		    lats = parseFloat(theForm.geo_lat_sec.value);
		    longd = parseInt(theForm.geo_long_deg.value);
		    longm = parseInt(theForm.geo_long_min.value);
		    longs = parseFloat(theForm.geo_long_sec.value);
		    //Check all are numbers
		    if (isNaN(latd) || isNaN(latm) || isNaN(lats) || 
				     isNaN(longd)  || isNaN(longm)  || isNaN(longs)){
				     //There's an error with one of the fields. Return error.
				     latLongResults = "There's a non-numerical value in one of the fields for the DMS format";
				     return;
		    } 
		    if (latd <= 0.0 || latm < 0.0 || lats < 0.0 || 
				     longd <= 0.0 || longm < 0.0 || longs < 0.0){
				     //There's an error with one of the fields. Return error.
				     latLongResults = "Values in the DMS format can't be less than 0";
				     return;
		    } 
		    //Calc. real degrees
		    latDegReal = latd + (latm/60) + (lats/3600);
		    longDegReal = longd + (longm/60) + (longs/3600);
		    longDegReal = (longDegReal > 0) ? longDegReal *-1 : longDegReal;
		    fieldsChecked = true;
	    } else {
		    //There's an error here. Return to calling funnction sending this error
		    latLongResults = "Need to enter values for all the fields in the DMS format";
		    return;
	    }
    } 

    //Check Degrees Only format if didn't check with the other fields
    if (!fieldsChecked){
	    if (theForm.geo_lat_deg_only.value != "" || theForm.geo_long_deg_only.value != "" ){
		    //Check all fields are filled for this format
		    if (theForm.geo_lat_deg_only.value != "" && theForm.geo_long_deg_only.value != "" ){
			    //All ok to proceed with this format
			    latDegReal = parseFloat(theForm.geo_lat_deg_only.value);
			    longDegReal = parseFloat(theForm.geo_long_deg_only.value);
			    if (isNaN(latDegReal) || isNaN(longDegReal)){
				 //There's an error with one of the fields. Return error.
				 latLongResults = "There's a non-numerical value in one of the fields for the Degrees-only format";
				 return;
			    } 		
		    } else {
			    //There's an error here. Return to calling funnction sending this error
			    latLongResults = "Need to enter values for all the fields in the Degrees-only format";
			    return;
		    }
	    }
    }

    //Check lat/long to be within the State of Arizona 
    //		Latitude 	31�20'N to 37�N
    //		Longitude 	109�3'W to 114�50'W
    //		source: http://en.wikipedia.org/wiki/Arizona 
    if (latDegReal < 31 || latDegReal > 37 || longDegReal > -109 || longDegReal < -115){
	     //There's an error with the range of coordinates
	     latLongResults = "Verify the coordinates. They don\'t match our records for the area of the State of Arizona";
	     return;
    }

    //All set
    //set geo_lat and geo_long fields in form
    theForm.geo_lat.value = latDegReal;
    theForm.geo_long.value = longDegReal;
    theForm.geo_lat_deg_only.value = latDegReal;
    theForm.geo_long_deg_only.value = longDegReal;
    return;
}
********************************************************************/