﻿// JScript File
function validateZipCode(field) 
{
    var valid = "0123456789";
    if(field != "Zip Code") {
        if (field.length!=5) {
	        alert("Please enter your 5 digit zip code.");
	        return false;
        }
        for (var i=0; i < field.length; i++) 
        {
	        temp = "" + field.substring(i, i+1);
	        if (valid.indexOf(temp) == "-1")
                {
		        alert("Invalid characters in your zip code.  Please try again.");
		        return false;
	        }
        }
        if(field < 501)
        {
                alert("Invalid zip code.  Please try again.");
		return false;
        }
    }
    return true;
}

function checkSubmit(form2)
{	
    var msg = "";
	if(form2.name.value==""){
		alert("Please enter your name.\n");
		form2.name.focus();
		return false;
	}
	
	if(form2.company.value.length<1){
		alert("Please enter your company.\n");
		form2.company.focus();
		return false;
	}
	
	
		if(form2.work_phone.value==""){
			alert("Please enter your phone.\n");
			form2.work_phone.focus();
			return false;
		}
		else if(!phoneCheck(form2.work_phone.value,"phone")){
			return false;
		}
	
	
           if(form2.email.value==""){
        	alert("Please enter your email.\n");
        	form2.email.focus();
        	return false;
           }
           else if(!emailCheck(form2.email.value)){
              	   return false;
              	}
	return true;
}

    //Check if the given phone number is valid
    function phoneCheck(phoneStr,phoneOrFax){
        var digitCount=0;
   	
   	//Count the number of digits in the phone number
        for(var i=0;i<phoneStr.length;i++){
            if( (phoneStr.charAt(i) >= '0') && (phoneStr.charAt(i) <= '9') )
              digitCount++;
            else if( phoneStr.charAt(i) != ' ' && phoneStr.charAt(i) != '(' && 
                      phoneStr.charAt(i) != ')' && phoneStr.charAt(i) != '-' ){
                      alert("Invalid format for " + phoneOrFax + " number");
                      return false;
                  }
        }
        //If the number of digits is not 10, then it is an incorrect phone number
        if(digitCount != 10){
        	//If the number of digits is 7, then prompt for area code
		if(digitCount == 7){
		    alert("Please enter the 3 digit area code for your " + phoneOrFax + " number");
            	}
            	else{//Prompt for correct number
            	    alert("Please enter a correct " + phoneOrFax + " number");
            	}
            	return false;
        }

        return true;
    }
    
  function emailCheck (emailStr)
   {
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) {
		alert("Email address seems incorrect (check @ and .'s)");
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	// See if "user" is valid
	if (user.match(userPat)==null) {
	    // user is not valid
	    alert("The username of the email address doesn't seem to be valid.");
	    return false;
	}
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
	    // this is an IP address
		  for (var i=1;i<=4;i++) {
		    if (IPArray[i]>255) {
			alert("Destination IP address is invalid!");
			return false;
		    }
	    }
	    return true;
	}
	// Domain is symbolic name
	var domainArray=domain.match(domainPat);
	if (domainArray==null) {
		alert("The domain name doesn't seem to be valid.");
	    return false;
	}
	var atomPat=new RegExp(atom,"g");
	var domArr=domain.match(atomPat);
	var len=domArr.length;
	if (domArr[domArr.length-1].length<2 ||
	    domArr[domArr.length-1].length>4) {
	   // the address must end in a two letter or three letter word.
	   alert("The domain name doesn't seem to be valid.");
	   return false;
	}
	// Make sure there's a host name preceding the domain.
	if (len<2) {
	   var errStr="This address is missing a hostname!";
	   alert(errStr);
	   return false;
	}
	// If we've gotten this far, everything's valid!
	return true;
  }

 function IsNumber(field)
  {
  if(field == null)
    {
        alert("Please enter Number");
        return false;
    }
    else
    {
        var valid = "0123456789";
        for (var i=0; i < field.length; i++)
         {
            temp = "" + field.substring(i, i+1);
            if (valid.indexOf(temp) == "-1") {
	            alert("Invalid characters. Please enter only Numbers.");
	            return false;
            }
        }
    }
  }
