function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function





function isValidvarPcode(varPcode) {
   if (varPcode.length == 5)
   	return (varPcode.search(/^\d\d\d\d\d$/) != -1);
   if (varPcode.length == 9)
   	return (varPcode.search(/^\d\d\d\d\d\d\d\d\d$/) != -1);
   if (varPcode.length == 10)
   	return (varPcode.search(/^\d\d\d\d\d-\d\d\d\d$/) != -1);
   	
   return false;
}



function isValidAUPostalcode(postalcode) {
   if (postalcode.length == 4)
   	return (postalcode.search(/^\d\d\d\d$/) != -1);
   return false;
}

function isValidCAPostalcode(postalcode) {
	  trimmedpostalcode = trim(postalcode);
      if (trimmedpostalcode.length == 6 && trimmedpostalcode.search(/^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d$/) != -1) return true;
      else if (trimmedpostalcode.length == 7 && trimmedpostalcode.search(/^[a-zA-Z]\d[a-zA-Z](-|\s)\d[a-zA-Z]\d$/) != -1) return true;
      else return false;
}


function isValidUKPostalcode(postalcode) {
	 size = postalcode.length
	 postalcode = postalcode.toUpperCase(); //Change to uppercase
	 while (postalcode.slice(0,1) == " ") //Strip leading spaces
	  {postalcode = postalcode.substr(1,size-1);size = postalcode.length
	  }
	 while(postalcode.slice(size-1,size)== " ") //Strip trailing spaces
	  {postalcode = postalcode.substr(0,size-1);size = postalcode.length
	  }

	 if (size < 6 || size > 8){ //Code length rule
	  return false;
	  }

	 if (!(isNaN(postalcode.charAt(0)))){ //leftmost character must be alpha character rule
	   return false;
	  }

	 if (isNaN(postalcode.charAt(size-3))){ //first character of inward code must be numeric rule
	   return false;
	  }

	 if (!(isNaN(postalcode.charAt(size-2)))){ //second character of inward code must be alpha rule
	   return false;
	  }

	 if (!(isNaN(postalcode.charAt(size-1)))){ //third character of inward code must be alpha rule
	   return false;
	  }

	 if (!(postalcode.charAt(size-4) == " ")){//space in position length-3 rule
	   return false;
	   }

	 count1 = postalcode.indexOf(" ");count2 = postalcode.lastIndexOf(" ");
	 if (count1 != count2){//only one space rule
	   return false;
	  }
	return true;
  }


function submitPostalCodeRequest() {

	var varLatitude = document.getElementById("pcodeLatitude");
	var varLongitude = document.getElementById("pcodeLongitude");
	var varPcode = document.getElementById("pcode").value;

	var varCountryValue = "CA";
	var theForm =  document.mainForm;

	for (var i=0; i <theForm.rb_country.length; i++) {
		if (theForm.rb_country[i].checked) {
			varCountryValue =theForm.rb_country[i].value;
			break;
		}
	}


	if (varCountryValue == "AU") {
		if (! isValidAUPostalcode(varPcode)) {
			alert("Invalid Australian Postal Pode Format");
			return;
			}
	} else if (varCountryValue == "CA") {
		if (! isValidCAPostalcode(varPcode)) {
			alert("Invalid Canadian Postal Code Format");
			return;
			}
	} else if (varCountryValue == "US") {
		if (! isValidvarPcode(varPcode)) {
			alert("Invalid United States Zip Code Format");
			return;
			}
	} else if (varCountryValue == "UK") {
		if (! isValidUKPostalcode(varPcode)) {
			alert("Invalid United Kingdom Postal Code Format");
			return;
			}
	}


	document.forms[0].submit();
	
}




function loadMap(lat, lon) {
//	var desc = "Hello";
//	var dlraddress = "Address";

	if (GBrowserIsCompatible()) {

		var map = new GMap2(document.getElementById("map"));
        
        map.setCenter(new GLatLng(lat, lon), 13);
        


		map.addControl(new GLargeMapControl());
		map.enableDoubleClickZoom();

		function createMarker(point) {
			var locIcon = new GIcon();
			locIcon.image = "/media/MLDpin.png";
			locIcon.shadow = "/media/MLDpin_shadow.png";
			locIcon.iconSize = new GSize(29, 31);
			locIcon.shadowSize = new GSize(38, 31);
//			locIcon.iconAnchor = new GPoint(29, 31);
			locIcon.iconAnchor = new GPoint(15, 31);
			locIcon.infoWindowAnchor = new GPoint(15, 1);

			map.setCenter(point, 15);

			var marker = new GMarker(point,locIcon);

//			GEvent.addListener(marker, "click", function() {
//				marker.openInfoWindowHtml("" + point.lat() + ", "  + point.lng());
//			});

			return marker;
		}


		function createMarker2(point) {
			var locIcon = new GIcon();
			locIcon.image = "/media/MLDpin.png";
			locIcon.shadow = "/media/MLDpin_shadow.png";
			locIcon.iconSize = new GSize(29, 31);
			locIcon.shadowSize = new GSize(38, 31);
//			locIcon.iconAnchor = new GPoint(29, 31);
			locIcon.iconAnchor = new GPoint(15, 31);
			locIcon.infoWindowAnchor = new GPoint(15, 1);

//			map.setCenter(point, 15);

			var marker = new GMarker(point);

			GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml("" + point.lat() + ", "  + point.lng());
			});

			return marker;
		}


		function createMarkerForAddress(place) {
			var geocoder = new GClientGeocoder();
			geocoder.getLatLng(place,function (point) {
						if (!point) {
							alert(dlraddress + " not found");
						} else {
							 var dealerMarker = createMarker2(point);
							 map.addOverlay(dealerMarker);
						}
					}
				);
		}

		if ((lat == "") ||  (lon == "")) {
			createMarkerForAddress(dlraddress);
		} else {
			var point2 = new GLatLng(lat, lon);
			map.addOverlay(createMarker(point2));
		}
//			createMarkerForAddress(dlraddress);

		map.setZoom(14)
	}
	
	
	
}


