
var numeric = "1234567890";
var alphaNumSpace = "abcdefghijklmnopqrstuvwxyz 1234567890"
var emailalphameric = "aeioubcdfghjklmnpqrstvwxyz1234567890-.@~_";

/*
Auto tabbing script- By JavaScriptKit.com
http://www.javascriptkit.com
This credit MUST stay intact for use
*/
function autotab(original,destination){
if (original.getAttribute&&original.value.length==original.getAttribute("maxlength"))
destination.focus()
}

function testNumber(t)
   {
   var numberList = '1234567890';
	  t = t.toLowerCase();
	  for (i=1; i<numberList.length; i++){
        alert('testing: ' + t + ' against '+ i +' ' + numberList.charAt(i) )
        
		 if (t.indexOf(numberList.charAt(i)) != -1){
				break;
                }
         //alert(t + ' testNumber ' + numberList.charAt(i)) 
         
	     if (i == numberList.length)
		 {
		 return false;
		 }
	     return true;
      }
   }
function testAlphaNumSpace(t)
   {
   
	  t = t.toLowerCase();
	  for (i=0;i<alphaNumSpace.length;i++)
		 if (t.indexOf(alphaNumSpace.charAt(i)) != -1){
				break;
        }
	  if (i == alphaNumSpace.length)
		 {
		 return false;
		 }
	  return true;
   }
   
function validZip(zip)
   {
	  len=zip.length
	  //digits="0123456789"

	  if(len < 5)
		 {
		 return false;
		 }
	  return true;
   }
   
function ClearNA(chkbx){
	if (chkbx.checked){
		chkbx.checked = false;
	}
}
function ClearToFly(){
	if (document.theForm.q16[3].checked){
		document.theForm.q16[0].checked = false;
		document.theForm.q16[1].checked = false;
		document.theForm.q16[2].checked = false;
	}
}

function ClearPreferedContact(){

	if (document.theForm.optin[2].checked){
		document.theForm.optin[0].checked = false;
		document.theForm.optin[1].checked = false;
		//document.theForm.optin[2].checked = false;
	}
}
function ClearEventsCelebrate(){

	if (document.theForm.Q7[6].checked){
		document.theForm.Q7[0].checked = false;
		document.theForm.Q7[1].checked = false;
		document.theForm.Q7[2].checked = false;
		document.theForm.Q7[3].checked = false;
		document.theForm.Q7[4].checked = false;
		document.theForm.Q7[5].checked = false;
	}
}
function ClearEventsCelebrate2(){

	if (document.theForm.Q7[0].checked ||
		document.theForm.Q7[1].checked ||
		document.theForm.Q7[2].checked ||
		document.theForm.Q7[3].checked ||
		document.theForm.Q7[4].checked ||
		document.theForm.Q7[5].checked){
		document.theForm.Q7[6].checked = false;
	}
}
function validEmail(email) {
   invalidChars = " /:,;"

   for (i=0; i<invalidChars.length; i++) {	// does it contain any invalid characters?
	  badChar = invalidChars.charAt(i)
	  if (email.indexOf(badChar,0) > -1) {
		 return false
	  }
   }
   atPos = email.indexOf("@",1)			// there must be one "@" symbol
   if (atPos == -1) {
	  return false
   }
   if (email.indexOf("@",atPos+1) != -1) {	// and only one "@" symbol
	  return false
   }
   periodPos = email.indexOf(".",atPos)
   if (periodPos == -1) {					// and at least one "." after the "@"
	  return false
   }
   if (periodPos+3 > email.length)	{		// must be at least 2 characters after the "."
	  return false
   }
   return true
}

function valEmail()
{
    // If Email is blank, do not validate
    var valEmailEntry = /\S/
    if (document.getElementById("email").value.search(valEmailEntry)==-1)
    {
        return false;
    }
    else
    {
        // Checks for valid email format.  Ex: scott@mail.com
        var valEmail = /^[a-zA-Z0-9_\.\-]+\@([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]{2,4}$/
          
        if (document.getElementById("email").value.search(valEmail)==-1)
        {
            return false;
        }
        return true;
    }
}



function is_int(value){ 
   for (i = 0 ; i < value.length ; i++) { 
      if ((value.charAt(i) < '0') || (value.charAt(i) > '9')) return false 
    } 
   return true; 
}



function validate_data(theForm)
{

	// Perform only if zipcode value present
	var zipcode;
	zipcode = theForm.zip.value;
	
	if (zipcode != ""){

    /*if (!testNumber(zipcode)) {
		alert("The ZIP code you provided is invalid.")
		theForm.zip.focus();
		return false;
		}
    */
       //alert(!testAlphaNumSpace(zipcode));
        if (!testAlphaNumSpace(zipcode)) {
		alert("The ZIP code you provided is invalid.")
		theForm.zip.focus();
		return false;
		}
		if (!validZip(zipcode)) {
		alert("The ZIP code you provided is not the correct length.")
		theForm.zip.focus();
		return false;
		}
	}
	
    

    
    
    
	// Perform only if phone areacode value present
	//var phoneChbx = theForm.optin[1];
    var aphone;
	aphone = theForm.areacode.value;
	                             
	if (aphone != "") {
		if (!is_int(aphone)) {
		alert("The area code you provided is invalid.")
		theForm.areacode.focus();
		return false;
		}
        
		if (aphone.length!=3) {
		alert("The area code you provided is not the correct length.")
		theForm.areacode.focus();
		return false;
		}
	}
	
	// Perform only if phone prefix value present
	var pphone;
	pphone = theForm.phnprefix.value;
	
	if (pphone != ""){
		if (!is_int(pphone)) {
		alert("The phone prefix you provided is invalid.")
		theForm.phnprefix.focus();
		return false;
		}
        if (pphone.length!=3) {
		alert("The phone prefix you provided is not the correct length.")
		theForm.phnprefix.focus();
		return false;
		}
	}
	
	// Perform only if phone suffix value present
	var sphone;
	sphone = theForm.phnsuffix.value;
	
	if (sphone != ""){
		if (!is_int(sphone)) {
		alert("The phone suffix you provided is invalid.")
		theForm.phnsuffix.focus();
		return false;
		}
        if (sphone.length!=4) {
		alert("The phone suffix number you provided is not the correct length.")
		theForm.phnsuffix.focus();
		return false;
		}
	}


    //test that if part of the phone exists that all of the phone should exist
    var CompletePhone;
	CompletePhone = theForm.areacode.value +'-'+ theForm.phnprefix.value +'-'+ theForm.phnsuffix.value 
    if (CompletePhone.length>2 && CompletePhone.length<12) {
		alert("Please provide a complete phone number.")
        if (theForm.areacode.value < 3) {theForm.areacode.focus();}
        else if (theForm.phnprefix.value <3) {theForm.phnprefix.focus();}
        else  {theForm.phnsuffix.focus();}
		return false;
		}
    
    
    
    
    
    
    
    
    
    
	// Perform email only if value present
	ttext = theForm.email.value;
	
	if (ttext != ""){
	   if (!validEmail(ttext)) {
	   alert("The email address you provided is invalid.")
	   theForm.email.focus();
	   return (false);
   }
	}
	return(true);
}



