' --------------------------------------------------------------------------------'
' Name and address validation routine                                             '
' --------------------------------------------------------------------------------'

	// validation functions for form submit
	function validateForm(theForm, submitted)
	{
        ErrArray  		 = new Array(10)
		var Count     	 = 0;
		var res          = true;
	    
		var firstName    = document.getElementById("firstname");
		var lastName     = document.getElementById("lastname");
		var address      = document.getElementById("address");
		var city         = document.getElementById("city");
		var state        = document.getElementById("state");
		var zip          = document.getElementById("zip");
		var email        = document.getElementById("email");
		var phonearea    = document.getElementById("phonearea");
		var phone3       = document.getElementById("phone3");
		var phone4       = document.getElementById("phone4");
		
        var NameRegEx = /^[a-zA-Z_]{0,50}$/

        if (firstName.value == '')
		{
		  Count++;
          ErrArray[Count] = Count + ') First Name is missing\n';		
		}
        else
		{
          str = firstName.value;
          if (!str.match(NameRegEx))
		  {
		  Count++;
          ErrArray[Count] = Count + ') First name Must Be all alphabetic\n';		
          }
		}
		
		if (lastName.value == '')
		{
		  Count++;
          ErrArray[Count] = Count + ') Last Name is missing\n';		
		}
        else
		{
          str = lastName.value;
          if (!str.match(NameRegEx))
		  {
		  Count++;
          ErrArray[Count] = Count + ') Last name Must Be all alphabetic\n';		
          }
		}

  	    var addr = address.value;
		if (addr == '')
		{
		  Count++;
          ErrArray[Count] = Count + ') Address is missing\n';		
		}

		if (addr.indexOf(',') > 0 )
		{
		  Count++;
          ErrArray[Count] = Count + ') Address can not contain commas\n';		
		}

        var cty = city.value;
		if (cty.value == '')
		{
		  Count++;
          ErrArray[Count] = Count + ') City is missing\n';		
		}

		if (cty.indexOf(',') > 0 )
		{
		  Count++;
          ErrArray[Count] = Count + ') City can not contain commas\n';		
		}

		if (state.selectedIndex == 0)
		{
		  //Count++;
          //ErrArray[Count] = Count + ') State not selected\n';		
		}

		if (zip.value == '')
		{
		  Count++;
          ErrArray[Count] = Count + ') Zip Code is missing\n';		
		}

        var PhoneArea =  phonearea.value; 			
		if (PhoneArea.length != 3)
		{
		  Count++;
          ErrArray[Count] = Count + ') Phone area code is missing or invalid\n';		
		}
		else
		{
			if (isNaN(PhoneArea))
			 {
     		  Count++;
              ErrArray[Count] = Count + ') Phone area code is not numeric\n';		
			 }
		}
        var Phone3  =  phone3.value; 					
		if (Phone3.length != 3)
		{
		  Count++;
          ErrArray[Count] = Count + ') Phone exchange number is missing or invalid\n';		
		}
		else
		{
			if (isNaN(Phone3))
			 {
     		  Count++;
              ErrArray[Count] = Count + ') Phone exchange number is not numeric\n';		
			 }
		}
        var Phone4  =  phone4.value; 							
		if (Phone4.length != 4)
		{
		  Count++;
          ErrArray[Count] = Count + ') Phone number is missing or invalid\n';		
		}
		else
		{
			if (isNaN(Phone4))
			 {
     		  Count++;
              ErrArray[Count] = Count + ') Phone number is not numeric\n';		
			 }
		}

		res = CheckPhone(PhoneArea, Phone3) 
		
     	if (res == false)
		{
		  Count++;
          ErrArray[Count] = Count + ') Phone number is not valid\n';		
		}

		if (email.value == '')
		{
		  Count++;
          ErrArray[Count] = Count + ') Email address is missing\n';		
		}
		else
		{
          var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
          str = email.value;
          if (!str.match(emailRegEx))
		  {
		  Count++;
          ErrArray[Count] = Count + ') Email address is invalid\n';		
          }
         }

		if (Count > 0)
		{
			out = ",";
			add = " ";
			temp = "" + ErrArray;

            while (temp.indexOf(out)>-1) 
			 {
              pos= temp.indexOf(out);
              temp = "" + (temp.substring(0, pos) + add + 
              temp.substring((pos + out.length), temp.length));
             }
			alert(temp);
			return false;
		}
		else
		{
			return true;
		}
	}

function GetXmlHttpObject()
  {
   var xmlHttp=null;
   try
    {
     // Firefox, Opera 8.0+, Safari
     xmlHttp=new XMLHttpRequest();
    }
   catch (e)
    {
     // Internet Explorer
    try
     {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
     }
    catch (e)
     {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
     }
    }
   return xmlHttp;
  }
  
function CheckPhone(NPA, NXX) 
{
  var strResponse = '';
    
  // See if this browser supports Ajax, if not then return
  xmlHttp=GetXmlHttpObject();
  if (xmlHttp==null)
   {
    return;
   }
  URL   = "http://www.dualpowerberries.com/PhoneVal.asp?NPA=" + NPA + "&NXX=" + NXX;
  xmlHttp.open("GET",URL,false);
  xmlHttp.send(null);
  
  if (xmlHttp.readyState==4 && xmlHttp.status==200) 
   {       
     strResponse = xmlHttp.responseText;
     var posNum = strResponse.indexOf('Success');
     if (posNum>=0)
      {
        return true;
	  }
	 else	
      {
        return false;
	  }
   } 
  else 
   {        
     alert('Server Error');   
     return false;
   }
}