// JavaScript Document

function validatecontactform(frm)
{
	if(trimString(frm.elements["fullname"].value)==""){
		alert("Please enter your full name.");
		frm.elements["fullname"].focus();
		return false;
	}
	
	if(trimString(frm.elements["firstemail"].value)==""){
		alert("Please enter email address");		
		frm.elements["firstemail"].focus();
		return false;
	}
	
	if(!ValidateEmail(trimString(frm.elements["firstemail"].value)))
	{
		alert("your email address is not correct");		
		frm.elements["firstemail"].focus();
		return false;
	}
	
	if(trimString(frm.elements["secondemail"].value)==""){
		alert("Please enter again email address for confirmation.");		
		frm.elements["secondemail"].focus();
		return false;
	}
	
	if(!ValidateEmail(trimString(frm.elements["secondemail"].value)))
	{
		alert("Please enter valid email address for confirmation.");		
		frm.elements["secondemail"].focus();
		return false;
	}

	if(trimString(frm.elements["secondemail"].value) != trimString(frm.elements["firstemail"].value))
	{
		alert("Please check the e-mail addresses you entered.  Both addresses need to match before you can continue.  Thank you");		
		frm.elements["secondemail"].focus();
		return false;
	}
	return true;
}

function ValidateEmail(S)
{
	var R=false;
	if (typeof(S) != "undefined")
	{
		if (/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/.test(S))
			R=true;
	}
	return R;
}

function checkNumber(number){
	//check numbers in 123,123,122.23 or 123123123.123 or 123,123,123 or 123123123 format
	var rex = /^((\d{1,3},)?(\d{3},)?(\d{3})|(\d{1,}))((\.(\d{1,}))?)$/;
	return rex.test(number);
}

function isPureNumber(number) {
	var valid = "0123456789"
	var temp;
	for (var i=0; i<number.length; i++) {
		temp = number.substring(i, i+1);		
		if (valid.indexOf(temp) == -1) 
			return false;
	}
	return true
}

function trimString(inputString) {
	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); 
	}
	return retValue;
}

