var invalidCharText = "letters (A-Z,a-z), numbers(0-9), or the special characters -#@(.,'.";
var allowSpaceText = " Spaces are allowed with an entry.";
var invalidChars  = " may not contain < > & or % characters";
function valForm(theForm) {
  var validForm = true;
  var errText = "";
  var invalidNumber = "\n--> Please enter a valid Home Phone Number";
  //errText = "The following fields are required:\n";
  if (isBlank(theForm.firstName.value)) {
	errText += "\n--> Please enter valid First Name ";
        if (validForm) theForm.firstName.focus();
        validForm = false;
  } 
  else if (removeSpecialChar(theForm.firstName.value) != null)
  {
        errText += "\n--> First Name " +  invalidChars;
        theForm.firstName.value = removeSpecialChar(theForm.firstName.value);
        if (validForm) theForm.firstName.focus();
        validForm = false;
  }

  if (isBlank(theForm.lastName.value)) {
	errText += "\n--> Please enter valid Last Name ";
        if (validForm) {
	    theForm.lastName.focus();
	}
	validForm = false;
  } 
  else if (removeSpecialChar(theForm.lastName.value) != null)
  {
        errText += "\n--> Last Name " +  invalidChars;
        theForm.lastName.value = removeSpecialChar(theForm.lastName.value);
        if (validForm) theForm.lastName.focus();
        validForm = false;
  }

  var validNum = true;
  if (isBlank(theForm.homeNPA.value) || 
     !isNumber(theForm.homeNPA.value,3)) {
            errText += invalidNumber;
	if (validForm) {
	    theForm.homeNPA.focus();
	}
	validNum = false;
	validForm = false;
  }
  if (isBlank(theForm.homeNXX.value) 
	|| !isNumber(theForm.homeNXX.value,3)) {
	if (validNum) {
       		errText += invalidNumber;
		validNum = false;
	}
	
	if (validForm)  {
		theForm.homeNXX.focus();
   	}
        validForm = false;
  }
  if (isBlank(theForm.homeLine.value) 
	|| !isNumber(theForm.homeLine.value,4)) {
	if (validNum) {
       		errText += invalidNumber;
		validNum = false;
	}
	if (validForm)  {
		theForm.homeLine.focus();
        }
	validForm = false;
  }
  if (!isBlank(theForm.workNPA.value) ||
      !isBlank(theForm.workNXX.value) ||
      !isBlank(theForm.workLine.value) ) {
	if (isBlank(theForm.workNPA.value) ||
	    isBlank(theForm.workNXX.value) ||
	    isBlank(theForm.workLine.value) ||
	    !isNumber(theForm.workNPA.value,3) ||
	    !isNumber(theForm.workNXX.value,3) ||
	    !isNumber(theForm.workLine.value,4) ) {
	    errText += "\n--> Please enter a valid Work Phone Number";
	    if (validForm) {
		theForm.workNPA.focus();
	    }
	    validForm = false;
	}
   }
	
 
  if (!isBlank(theForm.mobileNPA.value) ||
      !isBlank(theForm.mobileNXX.value) ||
      !isBlank(theForm.mobileLine.value) ) {
	if (isBlank(theForm.mobileNPA.value) ||
	    isBlank(theForm.mobileNXX.value) ||
	    isBlank(theForm.mobileLine.value) ||
	    !isNumber(theForm.mobileNPA.value,3) ||
	    !isNumber(theForm.mobileNXX.value,3) ||
	    !isNumber(theForm.mobileLine.value,4) ) {
	    errText += "\n--> Please enter a valid Mobile Phone Number";
	    if (validForm) {
		theForm.mobileNPA.focus();
	    }
	    validForm = false;
	}
  }
  if (isBlank(theForm.email.value)) { 
	errText += "\n--> Please enter a valid Email Address ";
	if (validForm)  {
		theForm.email.focus();
        }
	validForm = false;
  }
  else if (removeSpecialChar(theForm.email.value) != null)
  {
        errText += "\n--> Email Address " +  invalidChars;
        theForm.email.value = removeSpecialChar(theForm.email.value);
        if (validForm) theForm.email.focus();
        validForm = false;
  }

  if (isBlank(theForm.BBProviderSelect.value)) { 
	errText += "\n--> Please select a Broadband Provider ";
	if (validForm)  {
		theForm.BBProviderSelect.focus();
        }
	validForm = false;
  } else if (theForm.BBProviderSelect.value == other &&
	     theForm.BBProviderText != null &&
	     isBlank(theForm.BBProviderText.value) ) { 
	errText += "\n--> Please enter a Broadband Provider ";
	if (validForm)  {
		theForm.BBProviderText.focus();
        }
	validForm = false;
  } else if (theForm.BBProviderSelect.value == other &&
	     theForm.BBProviderText != null &&
	   removeSpecialChar(theForm.BBProviderText.value) != null )
  { 
        errText += "\n--> Broadband Provider  " +  invalidChars;
        theForm.BBProviderText.value = removeSpecialChar(theForm.BBProviderText.value);
        if (validForm) theForm.BBProviderText.focus();
        validForm = false;
  }
  
  if (theForm.serviceAddrLine1) {
  	if (isBlank(theForm.serviceAddrLine1.value)) { 
	    errText += "\n--> Please enter the first line of your address ";
	    if (validForm)  {
		theForm.serviceAddrLine1.focus();
            }
	    validForm = false;
  	}
        else if ((removeSpecialChar(theForm.serviceAddrLine1.value) != null) 
         ||  (!validateString2(theForm.serviceAddrLine1.value))) 
        {
            if (removeSpecialChar(theForm.serviceAddrLine1.value) != null) 
               theForm.serviceAddrLine1.value=removeSpecialChar(theForm.serviceAddrLine1.value);
            if (!validateString2(theForm.serviceAddrLine1.value)) 
	         errText += "\n--> the first line of address " + invalidCharText + allowSpaceText;
       	     else
                 errText += "\n-->the first line of address  " +  invalidChars;
        
             if (validForm) theForm.serviceAddrLine1.focus();
             validForm = false;
        } 
  } else {
	if (isBlank(theForm.houseNum.value)) {
		errText += "\n--> Please enter a House #";
		if (validForm) {
			theForm.houseNum.focus();
		}
	       validForm = false;
  }
  else if ((removeSpecialChar(theForm.houseNum.value) != null) || 
   (!validateString2(theForm.houseNum.value)))
  {
        if (removeSpecialChar(theForm.houseNum.value) != null) 
          theForm.houseNum.value = removeSpecialChar(theForm.houseNum.value);
        if (!validateString2(theForm.houseNum.value)) 
	    errText += "\n--> House # must be 1-10 " + invalidCharText +
			  allowSpaceText;
        else
            errText += "\n--> House # " +  invalidChars;
        if (validForm) theForm.houseNum.focus();
        validForm = false;
  } 
  if (!isBlank(theForm.houseNumSuffix.value) &&
     ((removeSpecialChar(theForm.houseNumSuffix.value) != null) || 
            !validateString2(theForm.houseNumSuffix.value))) 
  {
     if (removeSpecialChar(theForm.houseNumSuffix.value) != null) 
       theForm.houseNumSuffix.value = removeSpecialChar(theForm.houseNumSuffix.value);
       if(!validateString2(theForm.houseNumSuffix.value)) 
	    errText += "\n--> House # Suffix must be 1-4 " + invalidCharText;          else
	    errText += "\n--> House # Suffix " + invalidChars;
	if (validForm) { theForm.houseNumSuffix.focus(); }
                validForm = false;
  }

  if (isBlank(theForm.strName.value)) {
	errText += "\n--> Please enter a Street Name";
	if (validForm) {
		theForm.strName.focus();
	}
       validForm = false;
   } 

   if ((removeSpecialChar(theForm.strName.value) != null) ||
     (!validateString2(theForm.strName.value))) 
    {
        if (removeSpecialChar(theForm.strName.value) != null)
            theForm.strName.value = removeSpecialChar(theForm.strName.value);
        if (!validateString2(theForm.strName.value)) 
	    errText += "\n--> Street Name must be 1-30  " + invalidCharText;
        else
	    errText += "\n--> Street Name " + invalidChars;
	if (validForm) {
		theForm.strName.focus();
	}
       validForm = false;
  }

  if (!isBlank(theForm.serviceAddrLine2.value) ) {
   if ((removeSpecialChar(theForm.serviceAddrLine2.value) != null)
     || (!validateString2(theForm.serviceAddrLine2.value))) 
  {
         if (removeSpecialChar(theForm.serviceAddrLine2.value) != null)
           theForm.serviceAddrLine2.value = removeSpecialChar(theForm.serviceAddrLine2.value);
        if (!validateString2(theForm.serviceAddrLine2.value)) 
	    errText += "\n--> Building/Apt/Suite #/Other must be 1-30 " + invalidCharText;
        else
	    errText += "\n--> Building/Apt/Suite #/Other " + invalidChars;
	invalidCharText;
	if (validForm) {
		theForm.serviceAddrLine2.focus();
	}
       validForm = false;
   } else if (theForm.serviceAddrLine2.value.length > 0) {
	        var pound = false;
                for (var i = 0; i < theForm.serviceAddrLine2.value.length; i++)
                   {
                        var c = theForm.serviceAddrLine2.value.charAt(i);

                        if ((i!=0 && c == "#") ||
                             (i==0 && c == "#" &&
                             (theForm.serviceAddrLine2.value.length < 3 ||
                             (theForm.serviceAddrLine2.value.length >= 3 &&
                             (theForm.serviceAddrLine2.value.charAt(1) != " " ||
                              theForm.serviceAddrLine2.value.charAt(2) == " " ))))) {
                              pound = true;
                              break;
                        }
                   }

                   if (pound) {
			errText += "\n-->If the pound sign is used for Building/Apt/Suite #/Other, it must be the first character, and must be followed by a space and one or more letters or numbers."
		        if (validForm) {
			   theForm.serviceAddrLine2.focus();
		        }
	                validForm = false;
                        theForm.serviceAddrLine2.focus();
    		   }

		}

	}
  }
  
  if (isBlank(theForm.serviceCity.value)) { 
	errText += "\n--> Please enter a valid City ";
	if (validForm)  {
		theForm.serviceCity.focus();
        }
	validForm = false;
  } 
  else if (removeSpecialChar(theForm.serviceCity.value) != null)
  {
        errText += "\n--> City " +  invalidChars;
        theForm.serviceCity.value = removeSpecialChar(theForm.serviceCity.value);
        if (validForm) theForm.serviceCity.focus();
        validForm = false;
  }

  if (isBlank(theForm.serviceZipCode.value) || 
      (isValZipCode(theForm.serviceZipCode.value,5)&&
      isValZipCode(theForm.serviceZipCode.value,9))) { 
	errText += "\n--> Please enter a valid Zip Code ";
	if (validForm)  {
		theForm.serviceZipCode.focus();
        }
	validForm = false;
  }

  if (isBlank(theForm.password1.value)) { 
	errText += "\n--> Please enter a valid Password ";
	if (validForm)  {
		theForm.password1.focus();
        }
	validForm = false;
  }

  if (isBlank(theForm.securityQuestion.value)) { 
	errText += "\n--> Please select a Security Question ";
	if (validForm)  {
		theForm.securityQuestion.focus();
        }
	validForm = false;
  } else  if (isBlank(theForm.securityAnswer.value)) {
	errText += "\n--> Please enter a Security Answer ";
	if (validForm)  {
		theForm.securityAnswer.focus();
        }
	validForm = false;

  }
  else if (removeSpecialChar(theForm.securityAnswer.value) != null)
  {
        errText += "\n--> Security Answer  " +  invalidChars;
        theForm.securityAnswer.value = removeSpecialChar(theForm.securityAnswer.value);
        if (validForm) theForm.securityAnswer.focus();
        validForm = false;
  }
  if (!validForm) {
     alert(errText);
  }
  return validForm;

}

function changeBBProviderSelect() {
    	var si = document.dataForm.BBProviderSelect.selectedIndex;
        var sOther = document.dataForm.BBProviderSelect[si].value;
	if (sOther == other) {
		show("bbText");
		//document.dataForm.BBProviderText.focus();
	} else {
		hide("bbText");
                //if (document.dataForm.email) {
		  //document.dataForm.email.focus();
		//}
	}


}
function show(object) {
	if (document.getElementById) {
		document.getElementById(object).style.visibility = "visible";
	} else if (document.all) {
		document.all[object].style.visibility = "visible";
	} else if (document.layers) {
		document.layers[object].visibility = "visible";
	}

}
function hide(object) 
{
    if (document.getElementById) {
          document.getElementById(object).style.visibility = "hidden";
     } else if (document.all) {
          document.all[object].style.visibility = "hidden";
     } else if (document.layers) {
          document.layers[object].visibility = "hidden";
     }
}     
function validateString2(checkValue)
{
  var checkOK = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-#@(.,' ";
  for (var i = 0; i < checkValue.length; i++)
  {
    ch = checkValue.charAt(i);
    for (var j = 0; j < checkOK.length; j++)
    {
      if (ch == checkOK.charAt(j))
        break;
      if (j == (checkOK.length - 1))
      {
        return (false);
      }
    }
  }
  return (true);
}
     

