// Validator Object
var valid = new Object();

// Message Object
var msg = new Object();

// REGEX Elements

// matches zip codes
valid.zipCode = /\d{5}(-\d{4})?/;
msg.zipCode = "Please enter a valid Zip Code";

// matches $17.23 or $14,281,545.45 or ...
valid.Currency = /\$\d{1,3}(,\d{3})*\.\d{2}/;
msg.Currency = "Please enter an amount";

// matches 5:04 or 12:34 but not 75:83
valid.Time = /^([1-9]|1[0-2]):[0-5]\d$/;
msg.Time = "Please enter a valid time";

//matches email
valid.emailAddress = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
msg.emailAddress = "Please enter a valid e-mail address";

// matches phone ###-###-####
valid.phoneNumber = /^\(?\d{3}\)?\s|-\d{3}-\d{4}$/;
msg.phoneNumber = "Please enter a valid phone number";

// International Phone Number
valid.phoneNumberInternational = /^\d(\d|-){7,20}/;
msg.phoneNumberInternational = "Please enter a valid phone number including the Country Code";

// IP Address
valid.ipAddress = /^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
msg.ipAddress = "Please enter a valid IP address";

// Date xx/xx/xxxx
valid.aDate = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
msg.aDate = "Please enter a valid date";

// State Abbreviation
valid.State = /^(AK|AL|AR|AZ|CA|CO|CT|DC|DE|FL|GA|HI|IA|ID|IL|IN|KS|KY|LA|MA|MD|ME|MI|MN|MO|MS|MT|NB|NC|ND|NH|NJ|NM|NV|NY|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VA|VT|WA|WI|WV|WY)$/i;
msg.State = "Please enter a valid US state code";

// Social Security Number
valid.SSN = /^\d{3}\-\d{2}\-\d{4}$/;
msg.SSN = "Please enter a valid US Social Security Number";

// Name must not be empty and must not start with a special character
valid.aName = /^[^\][}{:;"'<>?/ _.,!@#$%^&*()+=|\\-][^ ]+$/;
msg.aName = "Please enter your name";

// Field must not be empty
valid.NotEmpty = /^.*$/;
msg.NotEmpty = "Input is required";

// Field value must not be zero
valid.NotZero = /^.*$/;	// pattern is ignored - this is a special case see validateForm below
msg.NotZero = "Input is required";

// Field value must not be zero
valid.NoSelection = /^.*$/;	// pattern is ignored - this is a special case see validateForm below
msg.NoSelection = "Input is required";

function validateForm(theForm) {
    var elArr = theForm.elements; 

    for(var i = 0; i < elArr.length; i++) {

       with(elArr[i]) { 
          var v = elArr[i].validator; 
          var m = elArr[i].valMsg;

          if(!v) continue; 

			if (v == "NotZero") {
		    if (elArr[i].value == 0) {
          	  if (!m)	m = msg[v];
              alert(m);
              elArr[i].select();
              elArr[i].focus(); 
              return false;
			}
		  }
		  else if (v == "NoSelection") {
		    if (elArr[i].selectedIndex == -1) {
          	  if (!m)	m = msg[v];
              alert(m);
			  // cannot put focus on a select box...
              return false;
			}
		  }
		  else {
				var thePat = valid[v]; 
				var gotIt = thePat.exec(value);
				if (gotIt == "")	gotIt = false;

				if(! gotIt){
					if (!m)	m = msg[v];
					alert(m);                  
					elArr[i].select();
					elArr[i].focus(); 
					return false;
				}
		  }
       }
    }

    return true;

}

function confirmSubmit() {
var agree=confirm("Are you sure you wish to continue?");
if (agree)
	return true ;
else
	return false ;
}

