var n;
var p;
var p1;
function validateFields()
{
//validates all required fields when submited
//window.alert("validate company name");
 		if (document.New_Account.Company.value.length<1)
 		{
		 window.alert("You must your company's name.");
		 document.New_Account.Company.focus();
		 return false;
		}
//window.alert("validate address_1");
		if (document.New_Account.Address_1.value.length<1)
 		{
		 window.alert("You must enter an address.");
		 document.New_Account.Address_1.focus();
		 return false;
		}
//window.alert("validate city");
		if (document.New_Account.City.value.length<1)
 		{
		 window.alert("You must enter a city.");
		 document.New_Account.City.focus();
		 return false;
		}
//window.alert("validate state name");
		if (document.New_Account.state_name.value != null)
 		{
		 document.New_Account.State.value = document.New_Account.state_name.value
		}
//window.alert("validate selected state");
		if (document.New_Account.State.value.length<1)
 		{
		 window.alert("You must select a state or province.");
		 document.New_Account.state_name.focus();
		 return false;
		}
//window.alert("validate zip us");
		if (document.New_Account.Zip.value.length < 1)
 		{
		 if (document.New_Account.state_name.selectedIndex >= 14 && document.New_Account.state_name.selectedIndex <= 63)
		  {
  		 window.alert("You must enter your 5 digit zip code.");
  		 document.New_Account.Zip.focus();
			 return false;
			}
		else if (document.New_Account.state_name.selectedIndex >= 1 && document.New_Account.state_name.selectedIndex <= 13)
  		{
  		 window.alert("You must enter your 6 digit postal code with a space after the first three characters.");
  		 document.New_Account.Zip.focus();
			 return false;
  		}
		}
//window.alert("validate zip canada");
		if (document.New_Account.state_name.selectedIndex >=1 && document.New_Account.state_name.selectedIndex <=13 && document.New_Account.Zip.value.length!=7)
		{
    	window.alert("You have entered an invalid postal code for Canada, Please enter your 6 digit postal code with a space after the first three characters.");
    	document.New_Account.Zip.focus();
			return false;
    }
		if (document.New_Account.state_name.selectedIndex >=14 && document.New_Account.state_name.selectedIndex <=63) 
				{
					if (document.New_Account.Zip.value.length!=5 || isNaN(document.New_Account.Zip.value))
       			{
      		 	window.alert("You have entered an invalid zip code, Please enter your 5 digit zip code.");
      		 	document.New_Account.Zip.focus();
						return false;
      			}
    	  }
//window.alert("validate phone");
		if (document.New_Account.Phone.value.length < 1)
			 {
			 	window.alert("You must enter your business phone number.");
				document.New_Account.Phone.focus();
				return false;
			 }
//window.alert("validate phone isNaN");
if (document.New_Account.Phone.value.indexOf("(") == -1)
{
		if (document.New_Account.Phone.value.length != 10 || isNaN(document.New_Account.Phone.value))
     {
     	window.alert("You have entered an invalid phone number.");
			var str = document.New_Account.Phone.value;
    	var trimmed = str.replace(/^\s+|\s+$/g, '');
    	document.New_Account.Phone.value = trimmed;
    	document.New_Account.Phone.focus();
    	return false;
     }else{
		 			 var str = document.New_Account.Phone.value;
     			 var area_code = str.substring(0,3);
    			 var prefix = str.substring(3,6);
    			 var local_number = str.substring(6,10)
    			 var new_str = "(" +area_code+ ") " +prefix+ "-" +local_number;
					 document.New_Account.Phone.maxlength = 14;
    			 document.New_Account.Phone.value = new_str;
     			 }
}
//window.alert("validate firstname");
		if (document.New_Account.Firstname.value.length<1)
 		{
		 window.alert("You must enter a First Name.");
		 document.New_Account.Firstname.focus();
		 return false;
		}
//window.alert("validate lastname");
		if (document.New_Account.Lastname.value.length<1)
 		{
		 window.alert("You must enter a Last Name.");
		 document.New_Account.Lastname.focus();
		 return false;
		}
//window.alert("validate email");
		if (document.New_Account.Email.value.length<1)
 		{
		 window.alert("You must enter an E-mail address.");
		 document.New_Account.Email.focus();
		 return false;
		}
//window.alert("validate pwd");
		if (document.New_Account.pwd1.value.length<1)
		{
		 window.alert("You must enter a password.");
		 document.New_Account.pwd1.focus();
		 return false;
		}
//window.alert("validate v_pwd");
		if (document.New_Account.v_pwd1.value.length<1)
		{
		 window.alert("You must verify your password.");
		 document.New_Account.v_pwd1.focus();
		 return false;
		}
//window.alert("compair pwd to v_pwd");
		if (document.New_Account.pwd1.value != document.New_Account.v_pwd1.value)
		{
		 window.alert("Your passwords do not match.\n\rPlease try again");
		 document.New_Account.pwd1.value = "";
		 document.New_Account.v_pwd1.value = "";
		 document.New_Account.pwd1.focus();
		 return false;
		}
		//window.alert(document.New_Account.Phone.value);
		document.getElementById('submit').disabled = true;
		return true;
	}
function ValidatePhone(){
p=p1.value
if(p.length==3){
	//d10=p.indexOf('(')
	pp=p;
	d4=p.indexOf('(')
	d5=p.indexOf(')')
	if(d4==-1){
		pp="("+pp;
	}
	if(d5==-1){
		pp=pp+")";
	}
	//pp="("+pp+")";
	document.New_Account.Phone.value="";
	document.New_Account.Phone.value=pp;
}
if(p.length>3){
	d1=p.indexOf('(')
	d2=p.indexOf(')')
	if (d2==-1){
		l30=p.length;
		p30=p.substring(0,4);
		//alert(p30);
		p30=p30+")"
		p31=p.substring(4,l30);
		pp=p30+p31;
		//alert(p31);
		document.New_Account.Phone.value="";
		document.New_Account.Phone.value=pp;
	}
	}
if(p.length>5){
	p11=p.substring(d1+1,d2);
	if(p11.length>3){
	p12=p11;
	l12=p12.length;
	l15=p.length
	//l12=l12-3
	p13=p11.substring(0,3);
	p14=p11.substring(3,l12);
	p15=p.substring(d2+1,l15);
	document.New_Account.Phone.value="";
	pp="("+p13+")"+p14+p15;
	document.New_Account.Phone.value=pp;
	//obj1.value="";
	//obj1.value=pp;
	}
	l16=p.length;
	p16=p.substring(d2+1,l16);
	l17=p16.length;
	if(l17>3&&p16.indexOf('-')==-1){
		p17=p.substring(d2+1,d2+4);
		p18=p.substring(d2+4,l16);
		p19=p.substring(0,d2+1);
		//alert(p19);
	pp=p19+p17+"-"+p18;
	document.New_Account.Phone.value="";
	document.New_Account.Phone.value=pp;
	//obj1.value="";
	//obj1.value=pp;
	}
}
//}
setTimeout(ValidatePhone,100)
}
function getIt(m){
n=m.name;
//p1=document.forms[0].elements[n]
p1=m
ValidatePhone()
}
function testphone(obj1){
p=obj1.value
//alert(p)
p=p.replace("(","")
p=p.replace(")","")
p=p.replace("-","")
p=p.replace("-","")
//alert(isNaN(p))
if (isNaN(p)==true){
alert("Check phone");
return false;
}
}
function hidediv(pass)
{ 
    var divs = document.getElementsByTagName('div'); 
    for(i=0;i<divs.length;i++)
		{ 
    	if(divs[i].id.match(pass))
			{ 
    	if (document.getElementById) 
    	divs[i].style.visibility="hidden"; 
    	else 
    	if (document.layers) // Netscape 4 
    	document.layers[divs[i]].display = 'hidden'; 
    	else // IE 4 
    	document.all.hideshow.divs[i].visibility = 'hidden'; 
    	} 
    } 
} 
function showdiv(pass)
{ 
    var divs = document.getElementsByTagName('div'); 
    for(i=0;i<divs.length;i++)
		{ 
    	if(divs[i].id.match(pass))
			{ 
    	if (document.getElementById) 
    	divs[i].style.visibility="visible"; 
    	else 
    	if (document.layers) // Netscape 4 
    	document.layers[divs[i]].display = 'visible'; 
    	else // IE 4 
    	document.all.hideshow.divs[i].visibility = 'visible'; 
    	} 
    } 
} 
function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
				 //changed first value from 31 to 32 to allow spaces
         if (charCode > 32 && (charCode < 48 || charCode > 57))
				 {
            return false;
				 }else{
							 return true;
							}
        
      }
