/* numeric validation functions */

function isANumber(inputValue) {
    var answer = true
    if (isNaN(inputValue)) {
        answer = false
    }
    else {
        for (var i=0; i < inputValue.length; i++) {
            if (isNaN(inputValue.charAt(i))) {
                if (inputValue.charAt(i) != ".") {                   answer = false
                   break
                 }            }
        }
    }
    if (inputValue.length == 0) {        answer = false     }    return answer
}

function isAnInteger(inputValue) {
    var answer = true
    if (isNaN(inputValue)) {
        answer = false
    }
    else {
        for (var i=0; i < inputValue.length; i++) {
            if (isNaN(inputValue.charAt(i))) {
                answer = false
                break
            }
        }
    }
    if (inputValue.length == 0) {        answer = false     }    return answer
}


function isPhone (val) {
    var ln = val.length

   if (ln == 10) {


       var areaCode = val.substring(0, 3)
       var exchange = val.substring(3, 6)
       var line = val.substring(6, 10)
       var dash1 = "-"
       var dash = "-"
       var openParen = "("
       var closeParen = ")"
   

    }

   if (ln == 12) {
        var openParen = "("
        var areaCode = val.substring(0, 3)
        var closeParen = ")"
        var dash1 = val.substring(3, 4)
        var exchange = val.substring(4, 7)
        var dash = val.substring(7, 8)
        var line = val.substring(8, 12)
    }

    if (ln == 13) {
        var openParen = val.substring(0, 1)
        var areaCode = val.substring(1, 4)
        var closeParen = val.substring(4, 5)
        var dash1 = "-"
        var exchange = val.substring(5, 8)
        var dash = val.substring(8, 9)
        var line = val.substring(9, 13)
    }

    if (ln == 14) {
        var openParen = val.substring(0, 1)
        var areaCode = val.substring(1, 4)
        var closeParen = val.substring(4, 5)
        var dash1 = "-"
        var exchange = val.substring(5, 9)
        var dash = val.substring(9, 10)
        var line = val.substring(10, 14)
    }

    var answer = true

    if (val) {
        if (
              	(openParen != "(" )     ||
                (!isAnInteger(areaCode))  ||
                (closeParen != ")")  ||
                (!isAnInteger(exchange))  ||
                (dash != "-")  ||
                ((dash != "-") && (dash != " "))  ||
                (!isAnInteger(line))) {
                    answer = false
        }
    }
    else {
        answer = false
    }
    return answer
}



function isEmail(val) {
    var answer = false

    for (var i=0; i < val.length; i++) {
        if (val.charAt(i) == "@") {
            if ((answer == true)  ||
                (i > (val.length - 3))) {
                    answer = false
                    break
            }
            else {
                answer = true
            }
        }
        else {
            if (
                (val == "#") ||
                (val == "$") ||
                (val == "%") ||
                (val == "^") ||
                (val == "&") ||
                (val == "*") ||
                (val == "=") ||
                (val == "+") ||
                (val == "?") ||
                (val == "/") ||
                (val == "{") ||
                (val == "}")) {
                    answer = false
                    break
            }
        }
    }
return answer
}

function isZip(val) {
    var answer = true

    if (val) {
        if (
            (val.length < 5)  ||
            (!isAnInteger(val))) {
                answer = false
        }
    }
    else {
        answer = false
    }
return answer
}


/* form validation functions */


function ValidateEmail()

{

	if (document.form1.txtName.value ==""){
		alert('Please enter your name!')
		document.form1.txtName.focus();
		return false;
	}


	if  ((document.form1.txtPhone.value =="") && (document.form1.txtEmail.value ==""))    {
		alert('Please enter either a phone number or email address so that we may contact you!')
		document.form1.txtEmail.focus();
 		return false;
	}

	if  (document.form1.txtPhone.value !=""){
      		if ((!isPhone(document.form1.txtPhone.value))) {
          		alert('Please enter phone numbers in the following format: 123-456-7890')
				document.form1.txtPhone.focus();
				document.form1.txtPhone.select();
           		return false;
      		}
 	}


	if (document.form1.txtEmail.value !="") {
      		if (!isEmail(document.form1.txtEmail.value)){
          		alert('Your email address does not appear to be valid!  Enter as name@domain.com.')
				document.form1.txtEmail.focus();
				document.form1.txtEmail.select();
          		return false;
      		}
  	}

	return true;  
}


function ValidateReg()

{

	if (document.form1.txtName.value ==""){
		alert('Please enter your name!')
		document.form1.txtName.focus();
		return false;
	}

	if (document.form1.txtAddr.value ==""){
		alert('Please enter your address!')
		document.form1.txtAddr.focus();
		return false;
	}


	if (document.form1.txtCity.value ==""){
		alert('Please enter your city!')
		document.form1.txtCity.focus();
		return false;
	}

	var st = document.form1.cmbState.selectedIndex;
	var stval = document.form1.cmbState.options[st].value;
	if (stval  == "none")  {
		alert('Please select a state!');
		document.form1.cmbState.focus();
		return false;
}
	if (document.form1.txtZip.value !=""){
      		if (!isZip(document.form1.txtZip.value)){
			alert('A valid zip code will contain at least 5 numbers!')
			document.form1.txtZip.focus();
			document.form1.txtZip.select();
      		return false;
      		}
  	}


	if  ((document.form1.txtPhone.value =="") && (document.form1.txtEmail.value ==""))    {
		alert('Please enter either a phone number or email address so that we may contact you!')
		document.form1.txtPhone.focus();
 		return false;
	}

	if  (document.form1.txtPhone.value !=""){
      		if ((!isPhone(document.form1.txtPhone.value))) {
          		alert('Please enter phone numbers in the following format: 123-456-7890')
				document.form1.txtPhone.focus();
				document.form1.txtPhone.select();
           		return false;
      		}
 	}


	if (document.form1.txtEmail.value !="") {
      		if (!isEmail(document.form1.txtEmail.value)){
          		alert('Your email address does not appear to be valid!  Enter as name@domain.com.')
				document.form1.txtEmail.focus();
				document.form1.txtEmail.select();
          		return false;
      		}
  	}

	if  (document.form1.txtFax.value !=""){
      		if ((!isPhone(document.form1.txtFax.value))) {
          		alert('Please enter fax numbers in the following format: 123-456-7890')
				document.form1.txtFax.focus();
				document.form1.txtFax.select();
           		return false;
      		}
 	}


	return true;  
}




/* populate country field with US if a state is chosen  */

function autoUS() {
	
	var st = document.form1.cmbState.selectedIndex;
	var stval = document.form1.cmbState.options[st].value;
	if ( (stval  != "none") && (stval != "nonUS")) {
		document.form1.txtCountry.value="USA"
		document.form1.txtZip.focus();
		}
	else	{
		document.form1.txtCountry.value=""
	}
		return true;
}


