function deleteBlanks(entry)
{
	var len = entry.length ;
	var foundBlank = 1;
	while(foundBlank == 1 && len > 0) 
	{
		var indx = entry.indexOf(" ");
		if(indx == -1) 
			foundBlank = 0 ;
		else
			entry = entry.substring(0,indx) + entry.substring(indx+1,len);
		len = entry.length;
	}
	return entry;
}
	
// function to check empty controls in the form
function isEmpty(val,valName) {
	retVal = true;
if (!deleteBlanks(val.value)) {
	alert(valName + " is required");
	val.focus();
	retVal = false;	
	}
	return retVal;
}
	
/*Function To Check Entered Data is number or not */
function isNumber(val) {
	retVal = true;
	count=0;
	str = val.toString();
	for (i=0;i<str.length;i++) {
		ch = str.substr(i, 1);
		if (ch<"0" || ch>"9") {
			retVal = false;
		}
	}
	 return retVal;
}
	
// function to check the emailid is valid or not<br>
function isEmail(val) {
	retVal = true; 
	tmp = val.value;
	if (isEmpty(val,"Email Address")) {
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(tmp)){
			retVal = true;	
		}
		else{
			alert("Email Address is invalid")
			val.focus();
			val.select();
			retVal = false;	
		}
	}
	else {
		retVal = false;
	}	
	return retVal;
}
	
// function to validate the length of zip code
function isLength(zipVal){

	var str = zipVal.toString();
	var strLength = str.length;
	var retVal = false;
	while(1) {
		if (strLength>5) {
		return retVal;
			break;
		}
		retVal = true;
		break;
	}
	return retVal;
}

// function to check valid telephone number
function isTel(val1,val2,val3,valName) {
	inv=0;
	v=val1.value+val2.value+val3.value;
	if (v!="") {
		if (v.length<10)
			inv=1;
		for (var i=0;i<v.length && inv==0;i++) {
			if ( v.charAt(i)<"0" || v.charAt(i)>"9")
				inv=1;
		}
		if (inv==1) {
			alert (valName + " is invalid")
			val1.focus();
			val1.select();
			return false;
		}
	}
	return true;
}
	//alows only A-Z, 0-9 and spaces
function isValidText(frmElement, fieldName) {
	myRegExp = new RegExp("[^a-z,0-9,\\s]", "i"); 
	if(myRegExp.test(frmElement.value)) {
		alert("Special characters not allowed in " + fieldName);
		frmElement.focus();
		frmElement.select();
		retVal = false;
	}
	else {
		retVal = true;
		}
	return retVal;
}


function isValidChar(frmElement, fieldName) {
	myRegExp = new RegExp("[^a-z,\\s]", "i"); 
	if(myRegExp.test(frmElement.value)) {
		alert("Only characters allowed in " + fieldName);
		frmElement.focus();
		frmElement.select();
		retVal = false;
	}
	else {
		retVal = true;
		}
	return retVal;
}

// function to validate the whole form
function validate() {
	retValue = false;
	while(true) {
		if(!isEmpty(document.contact.name1,"Name")) {
			break;
		}
		if(!isValidChar(document.contact.name1,"Name")) {
			break;
		}
		if(!isValidText(document.contact.name1,"Name")) {
			break;
		}
		if(!isEmpty(document.contact.address,"Address")) {
			break;
		}
		if(!isEmpty(document.contact.city,"City")) {
			break;
		}
		if(!isEmpty(document.contact.state,"State")) {
			break;
		}
		if(!isEmpty(document.contact.zip,"Zip Code")) {
			break;
		}
 		if (!isNumber(document.contact.zip.value)) {
			alert("Zip Code can only be numeric");
			document.contact.zip.focus();
			document.contact.zip.select();
			break;
		}		

		if((!deleteBlanks(document.contact.telephone1.value)) && (!deleteBlanks(document.contact.telephone2.value)) && (!deleteBlanks(document.contact.telephone3.value)) ) {
				alert("Contact Phone Number is required");
				document.contact.telephone1.focus();
				break;	
		}
			 if(!isTel(document.contact.telephone1,document.contact.telephone2,document.contact.telephone3,"Contact Phone Number")) {
				break;
		}	
			if((deleteBlanks(document.contact.cell1.value)!="") || (deleteBlanks(document.contact.cell2.value)!="") || (deleteBlanks(document.contact.cell3.value)!="") ) {
		if(!isTel(document.contact.cell1,document.contact.cell2,document.contact.cell3,"Cell Number"))				{				break;
		}	
			}

		if (document.contact.email.value != deleteBlanks(document.contact.email.value))			{
			if (confirm("The spaces in E-mail will be removed")) {
					document.contact.email.value =  deleteBlanks(document.contact.email.value);
					document.contact.email.focus();
					document.contact.email.select();
			}
			 break;
		}
		if(!isEmpty(document.contact.email,"E-mail")) {
			break;
		}	
		else {		
			if (deleteBlanks(document.contact.email.value)) {
				if(!isEmail(document.contact.email)) {
					break;
				}
			}
		}
		retValue = true;
		break;
	}
		
	return retValue;	 
}


	