function ValidateEmail(txt){
	//regular expression
	var b=/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/;
	return b.test(txt);
}

function decimalCheck(e,elm)
	{
		var keynum;
		var ctrlPressed=false;
		var val = elm.value;
		if(window.event) // IE
		{
			keynum = e.keyCode;
		}
		else if(e.which) // Netscape/Firefox/Opera
		{
			keynum = e.which;
			ctrlPressed = e.ctrlKey;
		}

		if(ctrlPressed){ //Allow ctrl + ...
			//we need to check the format of the copied text
			return true;
		}
		
		if (keynum == 8 || keynum==37 || keynum==36 || keynum==35 || keynum==46 || keynum==39 || keynum==9){
			return true;
		}else if (keynum==190 || keynum==110){
			if (val.indexOf('.')==-1){
				return true;
			}
			else{
				return false;
			}
		}else if (keynum==109){
			if (val.indexOf('-')==-1 ){
				return true;
			}
			else{
				return false;
			}
		}
		return (keynum>=48 && keynum<=57) || (keynum>=96 && keynum<=105);
		
	}
	
	//Makes sure lat and lng are in range
	function validateGeoCoordinates(lat,lng,alertLat,alertLng,alertYesNo) {

		if(lat==null) lat='';
		if(lng==null) lng='';
		if(alertYesNo==null) alertYesNo=false;
		if(lat.length==0  || !(lat>=-90 && lat<=90)){
			if (alertYesNo) alert(alertLat);
			document.getElementById('lat').focus;
			return false;
		}
		if(lng.length==0 || !(lng>=-180 && lng<=180)){
			if (alertYesNo) alert(alertLng);
			document.getElementById('lng').focus;
			return false;
		}					
		return true;
	}

	function Validate( varname, varemail, varinvalidemail, varcaptcha,InvalidMessage,varphone,varlocation){
	if(document.getElementById('name').value.length==0){
		alert(varname);
		document.getElementById('name').focus;
		return(false);
	}
	if(document.getElementById('email').value.length==0){
		alert(varemail);
		document.getElementById('email').focus;
		return(false);
	}
	if(document.getElementById('email').value.length>0){
		if (!ValidateEmail(document.getElementById('email').value)){
			alert(varinvalidemail);
			document.getElementById('email').focus;
			return(false);
		}
	}
	//Mcooling validate phone number too
	if(document.getElementById('phone').value.length<9){
		alert(varphone);
		document.getElementById('phone').focus;
		return(false);
	}
	//Mcooling validate geo location
	if (document.getElementById('lng')!=null){
		if(document.getElementById('lng').value.length==0 || document.getElementById('lat').value.length==0){
			alert(varlocation);
			return(false);
		}
		if(document.getElementById('lat').value<=-90 || document.getElementById('lat').value>=90 
		   ||document.getElementById('lng').value<=-180 || document.getElementById('lng').value>=180){
			alert(varlocation);
			return(false);
		}
	}

	if(document.getElementById('security_code').value.length==0){
		alert(varcaptcha);
		document.getElementById('security_code').focus;
		return(false);
	}
	
	return(true);
}

function ValidateNotes( varname, varemail, varinvalidemail, varcaptcha,InvalidMessage,varphone,varlocation){
	if(document.getElementById('name').value.length==0){
		alert(varname);
		document.getElementById('name').focus;
		return(false);
	}
	if(document.getElementById('email').value.length==0){
		alert(varemail);
		document.getElementById('email').focus;
		return(false);
	}
	if(document.getElementById('email').value.length>0){
		if (!ValidateEmail(document.getElementById('email').value)){
			alert(varinvalidemail);
			document.getElementById('email').focus;
			return(false);
		}
	}
	//Mcooling validate phone number too
	if(document.getElementById('phone').value.length<9){
		alert(varphone);
		document.getElementById('phone').focus;
		return(false);
	}
	//Mcooling validate geo location
	if (document.getElementById('lng')!=null){
		if(document.getElementById('lng').value.length==0 || document.getElementById('lat').value.length==0){
			alert(varlocation);
			document.getElementById('geoMap').focus;
			return(false);
		}
	}
	if(document.getElementById('notes').value.length>600){
		alert(InvalidMessage);
		document.getElementById('notes').focus;
		return(false);
	}	
	if(document.getElementById('security_code').value.length==0){
		alert(varcaptcha);
		document.getElementById('security_code').focus;
		return(false);
	}
	
	return(true);
}

function ValidateNotesNew( varname, varemail, varinvalidemail, varcaptcha,InvalidMessage,varphone,varlocation,varLat,varLng){

	if(document.getElementById('name').value.length==0){
		alert(varname);
		document.getElementById('name').focus;
		return(false);
	}

	if(document.getElementById('email').value.length==0){
		alert(varemail);
		document.getElementById('email').focus;
		return(false);
	}

	if(document.getElementById('email').value.length>0){
		if (!ValidateEmail(document.getElementById('email').value)){
			alert(varinvalidemail);
			document.getElementById('email').focus;
			return(false);
		}
	}

	//Mcooling validate phone number too
	if(document.getElementById('phone').value.length<9){
		alert(varphone);
		document.getElementById('phone').focus;
		return(false);
	}

	if (document.getElementById('lat').value.length==0){
		alert(varLat);
		return(false);
	}

	if (document.getElementById('lng').value.length==0){
		alert(varLng);
		return(false);
	}
	
	//Mcooling validate geo location
	if (!validateGeoCoordinates(document.getElementById('lat').value,document.getElementById('lng').value,varLat,varLng))
	{
		return(false);
	}
	
	if(document.getElementById('notes').value.length>600){
		alert(InvalidMessage);
		document.getElementById('notes').focus;
		return(false);
	}	

	if (document.getElementById('security_code')!=null){
		if(document.getElementById('security_code').value.length==0){
			alert(varcaptcha);
			document.getElementById('security_code').focus;
			return(false);
		}
	}
	return(true);
}

function ValidateContact( varname, varemail, varinvalidemail, varcaptcha,InvalidMessage,varphone,varlocation,varLat,varLng,varreason){

	var reasonIdForCheckingGeo=1;
	
	if(document.getElementById('reason').value==0){
		alert(varreason);
		document.getElementById('reason').focus;
		return(false);
	}
	
	if(document.getElementById('name').value.length==0){
		alert(varname);
		document.getElementById('name').focus;
		return(false);
	}

	if(document.getElementById('email').value.length==0){
		alert(varemail);
		document.getElementById('email').focus;
		return(false);
	}

	if(document.getElementById('email').value.length>0){
		if (!ValidateEmail(document.getElementById('email').value)){
			alert(varinvalidemail);
			document.getElementById('email').focus;
			return(false);
		}
	}

	//Mcooling validate phone number too
	if(document.getElementById('phone').value.length<9){
		alert(varphone);
		document.getElementById('phone').focus;
		return(false);
	}
	
	if (document.getElementById('reason').value==reasonIdForCheckingGeo){

		if (document.getElementById('lat').value.length==0){
			alert(varLat);
			return(false);
		}

		if (document.getElementById('lng').value.length==0){
			alert(varLng);
			return(false);
		}
		
		//Mcooling validate geo location
		if (!validateGeoCoordinates(document.getElementById('lat').value,document.getElementById('lng').value,varLat,varLng))
		{
			return(false);
		}
	}
	
	if(document.getElementById('notes').value.length>600){
		alert(InvalidMessage);
		document.getElementById('notes').focus;
		return(false);
	}	

	if (document.getElementById('security_code')!=null){
		if(document.getElementById('security_code').value.length==0){
			alert(varcaptcha);
			document.getElementById('security_code').focus;
			return(false);
		}
	}
	return(true);
}

//Makes sure lat and lng are in range
function validateGeoCoordinates2(lat,lng,alertLatLng,minLat,maxLat,minLng,maxLng) {

	if(lat==null) lat='';
	if(lng==null) lng='';
	if(lat.length==0  || !(lat>=minLat && lat<=maxLat)){
		alert(alertLatLng);
		document.getElementById('lat').focus;
		return false;
	}
	if(lng.length==0 || !(lng>=minLng && lng<=maxLng)){
		alert(alertLatLng);
		document.getElementById('lng').focus;
		return false;
	}					
	return true;
}

function ValidateContact2( varname, varemail, varinvalidemail, varcaptcha,InvalidMessage,varphone,varlocation,varLatLng,varreason,minLat,maxLat,minLng,maxLng){

	var reasonIdForCheckingGeo=1;
	
	if(document.getElementById('reason').value==0){
		alert(varreason);
		document.getElementById('reason').focus;
		return(false);
	}
	
	if(document.getElementById('name').value.length==0){
		alert(varname);
		document.getElementById('name').focus;
		return(false);
	}

	if(document.getElementById('email').value.length==0){
		alert(varemail);
		document.getElementById('email').focus;
		return(false);
	}

	if(document.getElementById('email').value.length>0){
		if (!ValidateEmail(document.getElementById('email').value)){
			alert(varinvalidemail);
			document.getElementById('email').focus;
			return(false);
		}
	}

	//Mcooling validate phone number too
	if(document.getElementById('phone').value.length<9){
		alert(varphone);
		document.getElementById('phone').focus;
		return(false);
	}
	
	if (document.getElementById('reason').value==reasonIdForCheckingGeo){
		
		//Mcooling validate geo location
		if (!validateGeoCoordinates2(document.getElementById('lat').value,document.getElementById('lng').value,varLatLng,minLat,maxLat,minLng,maxLng))
		{
			return(false);
		}
	}
	
	if(document.getElementById('notes').value.length>600){
		alert(InvalidMessage);
		document.getElementById('notes').focus;
		return(false);
	}	

	if (document.getElementById('security_code')!=null){
		if(document.getElementById('security_code').value.length==0){
			alert(varcaptcha);
			document.getElementById('security_code').focus;
			return(false);
		}
	}
	return(true);
}