function trim(str){
while (str.charAt(0)  == " "){
// remove leading spaces
str = str.substring(1);
}
while (str.charAt(str.length - 1) == " "){
// remove trailing spaces
str = str.substring(0,str.length - 1);
}
return str;
}

function isValid(email) {
	if(trim(email).indexOf(" ")>=0)
	{
		return false;
	}
	if(trim(email).indexOf("'")>=0)
	{
		return false;
	}
	if(trim(email).indexOf("\"")>=0)
	{
		return false;
	}
	len= email.length;
	posAt= email.indexOf("@");
	posDot= email.lastIndexOf(".");
	if ((len >= 5) && (posAt+1 < posDot) && (posAt+1 > 1) && (posDot+1 < len))
	   return true;
	else
	   {
	    return false;
	}
}

function frmCheckNum(event,argObj)
{
		if ((event.keyCode>45) && (event.keyCode<58))
		{
			return true;	
		}
		else
		{
			return false;	
		}
}

function frmCheck(){
	//setDesc();
	//validateRadioBox(field, field2, msg);
//	radioButtons()
	var val="";
	var additionalComments;
	var val1="";
	//alert("hello testing");
	var len1 = document.form1.phone.value;
	var len2 = len1.length;
	//alert(len2);
	var statebool=1;
	var hyphen="-";
	f=document.form1;
	j=document.form1.length;
	for (var k=0; k<j;k++)
	{
		cntr=0;
		elem=f.elements[k];
				
			if(elem.name=="country")
			{
				if(elem.value=="")
				{
					alert("Please select a country");
					cntr=1;
					elem.focus();
				}
				else
				{
					if (elem.value=="US")
					{

						if (document.form1.state.value=="") 
						{
							alert("Please select a state");
							cntr=1;
							document.form1.state.focus();
						}
					}
					else 
					{
						document.form1.state.value="";
					}
				}
			}

			if((elem.name=="first_name") && (trim(elem.value)==""))
			{
				alert("Please enter a first name");
				cntr=1;
				elem.focus();
			}
			if((elem.name=="last_name") && (trim(elem.value)==""))
			{
				alert("Please enter a last name");
				cntr=1;
				elem.focus();
			}
			if((elem.name=="company") && (trim(elem.value)==""))
			{
				alert("Please enter a company name");
				cntr=1;
				elem.focus();
			}
			if((elem.name=="city") && (trim(elem.value)==""))
			{
				alert("Please enter a city");
				cntr=1;
				elem.focus();
			}
			if((elem.name=="zip") && (trim(elem.value)==""))
			{
				alert("Please enter a zip/postal code");
				cntr=1;
				elem.focus();
			}
			if((elem.name=="phone") && (trim(elem.value)==""))
			{
				alert("Please enter a telephone number");
				cntr=1;
				elem.focus();
				
			}
			if((elem.name=="phone") && (trim(elem.value)!=""))
			{
				if (len2<10)
				{
					alert("The telephone number should be at least 10 digits long.");
					cntr=1;
					elem.focus();
				}
				/*if (len2>10)
				{
					alert("The telephone number should not be more than 10 digits long");
					cntr=1;
					elem.focus();
				}*/
				
			}
			
			if((elem.name=="title") && (trim(elem.value)==""))
			{
				alert("Please enter a title");
				cntr=1;
				elem.focus();
			}
			if((elem.name=="street") && (trim(elem.value)==""))
			{
				alert("Please enter an address");
				cntr=1;
				elem.focus();
			}
			if((elem.name=="email")&&(elem.value!=""))
			{
			if (!(isValid(elem.value)))
			{
				alert("Incorrect Email Format");
				cntr=1;
				elem.focus();
			}
			}
			if((elem.name=="email") && (trim(elem.value)==""))
			{
				alert("Please enter an email");
				cntr=1;
				elem.focus();
			}
			if (cntr==1) 
			{
				break;
			}
	}
if (cntr==1) return false;
return true;
}
