

var isNN = ( navigator.appName.indexOf( "Netscape" ) != -1 ); 



function ereg(objText, spCh)
{
  if(spCh == null || spCh == "") spCh = "@._-";

  var rx=/[^A-Za-z\d @._-]/;

  if(rx.test(objText.value) == true)
  {
    objText.value = "";
    alert("You may enter only spaces, alphanumeric and/or these special characters: " + spCh);
    objText.focus();
    return (false);
  } 
  return (true);
}




function ValidateThis(theForm)
{
  if (!theForm.becomeDist.checked && !theForm.bookParty.checked && !theForm.requestCat.checked )
  {
    alert("You must select a request from the top of the form");
    theForm.becomeDist.focus();
    return (false);
  }
  
  if (theForm.FirstName != null && theForm.FirstName.value == "")
  {
    alert("Please enter a value for the \"First Name\" field.");
    theForm.FirstName.focus();
    return (false);
  }

  if (theForm.FirstName != null && theForm.FirstName.value.length < 1)
  {
    alert("Please enter at least 1 characters in the \"First Name\" field.");
    theForm.FirstName.focus();
    return (false);
  }

  if (theForm.FirstName != null && theForm.FirstName.value.length > 50)
  {
    alert("Please enter at most 50 characters in the \"First Name\" field.");
    theForm.FirstName.focus();
    return (false);
  }

  if (theForm.FirstName != null && !/^[ a-z0-9]+$/i.test(theForm.FirstName.value))
  {
    alert("The \"First Name\" field may contain only alphanumeric characters and spaces.");
    theForm.FirstName.value = "";
    theForm.FirstName.focus();
    return (false);
  }

  if (theForm.LastName != null && theForm.LastName.value == "")
  {
    alert("Please enter a value for the \"Last Name\" field.");
    theForm.LastName.focus();
    return (false);
  }

  if (theForm.LastName != null && theForm.LastName.value.length < 1)
  {
    alert("Please enter at least 1 characters in the \"Last Name\" field.");
    theForm.LastName.focus();
    return (false);
  }

  if (theForm.LastName != null && theForm.LastName.value.length > 50)
  {
    alert("Please enter at most 50 characters in the \"Last Name\" field.");
    theForm.LastName.focus();
    return (false);
  }

  if (theForm.LastName != null && !/^[ a-z0-9]+$/i.test(theForm.LastName.value))
  {
    alert("The \"Last Name\" field may contain only alphanumeric characters and spaces.");
    theForm.LastName.value = "";
    theForm.LastName.focus();
    return (false);
  }

  if (theForm.Email != null && theForm.Email.value == "")
  {
    alert("Please enter a value for the \"Email\" field.");
    theForm.Email.focus();
    return (false);
  }

  if (theForm.Email != null)
  {
    if (!/^([\w]+)(\.[\w]+)*@([\w\-]+)(\.[\w]{2,7})(\.[a-z]{2})?$/i.test(theForm.Email.value))
    {
      alert("'" + theForm.Email.value + "' is not a valid email address.  Please enter a valid Email Address.");
      theForm.Email.value = "";
      theForm.Email.focus();
      return (false);
    }
  }

  if (theForm.Harea != null && theForm.Harea.value == "")
  {
    alert("Home Phone # cannot be blank");
    //theForm.Harea.focus();
    return (false);
  }

  if (theForm.Hnum1 != null && theForm.Hnum1.value == "")
  {
    alert("Home Phone # cannot be blank");
    //theForm.Hnum1.focus();
    return (false);
  }

  if (theForm.Hnum2 != null && theForm.Hnum2.value == "")
  {
    alert("Home Phone # cannot be blank");
    //theForm.Hnum2.focus();
    return (false);
  }


  if (theForm.Address != null && theForm.Address.value == "")
  {
    alert("Please enter a value for the \"Street Address\" field.");
    theForm.Address.focus();
    return (false);
  }

  if (theForm.Address != null && theForm.Address.value.length < 1)
  {
    alert("Please enter at least 1 characters in the \"Street Address\" field.");
    theForm.Address.focus();
    return (false);
  }

  if (theForm.Address != null && theForm.Address.value.length > 50)
  {
    alert("Please enter at most 50 characters in the \"Street Address\" field.");
    theForm.Address.focus();
    return (false);
  }

  if (theForm.Address != null && !/^[#. a-z0-9]+$/i.test(theForm.Address.value))
  {
    alert("The \"Address\" field may contain only alphanumeric or the (#.) characters.");
    theForm.Address.value = "";
    theForm.Address.focus();
    return (false);
  }

  if (theForm.City != null && theForm.City.value == "")
  {
    alert("Please enter a value for the \"City\" field.");
    theForm.City.focus();
    return (false);
  }

  if (theForm.City != null && theForm.City.value.length < 1)
  {
    alert("Please enter at least 1 characters in the \"City\" field.");
    theForm.City.focus();
    return (false);
  }

  if (theForm.City != null && theForm.City.value.length > 50)
  {
    alert("Please enter at most 50 characters in the \"City\" field.");
    theForm.City.focus();
    return (false);
  }

  if (theForm.City != null && !/^[. a-z]+$/i.test(theForm.City.value))
  {
    alert("The \"City\" field may contain only alphabetic or the (.) character.");
    theForm.City.value = "";
    theForm.City.focus();
    return (false);
  }

  if (theForm.State != null && theForm.State.value == "" || theForm.State.value == "0")
  {
    alert("Please enter a value for the \"State\" field.");
    theForm.State.focus();
    return (false);
  }

  if (theForm.ZipCode != null && theForm.ZipCode.value == "")
  {
    alert("Please enter a value for the \"Zip Code\" field.");
    theForm.ZipCode.focus();
    return (false);
  }

  if (theForm.ZipCode != null && theForm.ZipCode.value.length < 5)
  {
    alert("Please enter at least 5 characters in the \"Zip Code\" field.");
    theForm.ZipCode.focus();
    return (false);
  }

  if (theForm.ZipCode != null && theForm.ZipCode.value.length > 12)
  {
    alert("Please enter at most 12 characters in the \"Zip Code\" field.");
    theForm.ZipCode.focus();
    return (false);
  }

  if (theForm.ZipCode != null && !/^[-a-z0-9]+$/i.test(theForm.ZipCode.value))
  {
    alert("The \"ZipCode\" field may contain only alphanumeric or the (-) character.");
    theForm.ZipCode.value = "";
    theForm.ZipCode.focus();
    return (false);
  }

  if (theForm.dobVerifier != null && theForm.dobVerifier.checked == false)
  {
    alert("You must affirm that you are 18 or older!");
    theForm.dobVerifier.focus();
    return (false);
  }

  if (theForm.Referred != null && !/^[ a-z0-9]+$/i.test(theForm.Referred.value))
  {
    alert("The \"Referred\" field may contain only alphanumeric characters.");
    theForm.Referred.value = "";
    theForm.Referred.focus();
    return (false);
  }

  
  return (true);
}



function isCardOK(cardNo) {
     var a=""
     a=cardNo.value
   
        isCreditCard = false ;
        lCard=a.length-1 ;
 if (a.length>0)       {
        lC=a.substr(lCard,1) ;
        cStat=0 ;
        for (i=(lCard-1);i>=0; --i)
			{
            tempChar= a.substr(i,1) ;
            d= parseInt(tempChar);
            b=lCard % 2
           if ((lCard % 2) == 1)
                temp=d*(1+((i+1) % 2)) ;
            else 
               temp=d*(1+(i % 2)) ;
           
            if (temp < 10)
                cStat = cStat + temp  ;
            else 
                cStat = cStat + temp - 9 ;
        }
        cStat = (10-(cStat % 10)) % 10 ;
        x = parseInt(lC)
        if (parseInt(lC) == cStat ){
          return true ;}
        else{
          cinfo.CCNumber.focus();
          alert("Credit Card Number is not entered correctly");
          cinfo.CCNumber.select();
          return (false);
         }
         }
 }
 
 
 
 function autoTab( input,len, e ) { 
	var keyCode	= ( isNN ) ? e.which : e.keyCode; 
	var filter	= ( isNN ) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46]; 
	if( input.value.length >= len && !containsElement( filter, keyCode )) { 
	input.value = input.value.slice( 0, len ); 
	input.form[( getIndex( input ) + 1 ) % input.form.length].focus(); 
	} 
	return true; 
} 
 
function containsElement( arr, ele ) { 
	var found = false, index = 0; 
	while( !found && index < arr.length ) 
	if( arr[index] == ele ) { 
		found = true; 
	} else { 
		index++; 
	} 
	return found; 
} 
 
function getIndex( input ) { 
	var index = -1, i = 0, found = false; 
	while ( i < input.form.length && index == -1 ) 
	if ( input.form[i] == input ) { 
		index = i; 
	} else { 
		i++; 
	} 
	return index; 
} 

function changeCase(frmObj) {
var index;
var tmpStr;
var tmpChar;
var preString;
var postString;
var strlen;
tmpStr = frmObj.value.toLowerCase();
strLen = tmpStr.length;
if (strLen > 0)  {
for (index = 0; index < strLen; index++)  {
if (index == 0)  {
tmpChar = tmpStr.substring(0,1).toUpperCase();
postString = tmpStr.substring(1,strLen);
tmpStr = tmpChar + postString;
}
else {
tmpChar = tmpStr.substring(index, index+1);
if (tmpChar == " " && index < (strLen-1))  {
tmpChar = tmpStr.substring(index+1, index+2).toUpperCase();
preString = tmpStr.substring(0, index+1);
postString = tmpStr.substring(index+2,strLen);
tmpStr = preString + tmpChar + postString;
         }
      }
   }
}
frmObj.value = tmpStr;
}


function validateDate( strValue ) {
  var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
 
  //check to see if in correct format
  if(!objRegExp.test(strValue))
    return false; //doesn't match pattern, bad date
  else{
    var arrayDate = strValue.split(RegExp.$1); //split date into month, day, year
	var intDay = parseInt(arrayDate[1],10); 
	var intYear = parseInt(arrayDate[2],10);
    var intMonth = parseInt(arrayDate[0],10);
	
	//check for valid month
	if(intMonth > 12 || intMonth < 1) {
		return false;
	}
	
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
  
    //check if month value and day value agree
    b = arrayDate[0];
    if (b.length==1){
      arrayDate[0] = "0"+ b;}
    //alert(arrayDate[0]);
    if(arrayLookup[arrayDate[0]] != null) {
      if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
        return true; //found in lookup table, good date
    }
		
    //check for February
	var booLeapYear = (intYear % 4 == 0 && (intYear % 100 != 0 || intYear % 400 == 0));
    if( ((booLeapYear && intDay <= 29) || (!booLeapYear && intDay <=28)) && intDay !=0)
      return true; //Feb. had valid number of days
  }
  return false; //any other values, bad date
}

// The test function for integers.
function integerCheck(a) 
{
   // The test itself.
   if ( (a*a) == (a*a) )   
      return true;

   return false;
} // End of the integer checking function.



function ValidateAge(theForm) {

var datestring = theForm.dob.value;
	if (datestring!=null){
 	  isdate=validateDate(datestring) 
 	  if (isdate) {
	  	x=Date.parse(datestring);
			y=new Date();
    	s = (y.getMonth() + 1) + "/";
    	s += y.getDate() + "/";
    	s += y.getFullYear();
			
			//date2 = new Date(x);
			date2 = new Date(x);
    	t = (date2.getMonth() + 1) + "/";
    	t += date2.getDate() + "/";
    	t += date2.getFullYear();
		 
			//alert ((s));
    	//alert ((x));
      x= ((Date.parse(s)-Date.parse(t))/(1000 * 60 * 60 * 24)/365);
 	  }
		else {
  		x = 0;
  	}
		
		if (x < 18) {
			alert("Please enter a value for the \"Birthday\" field.");
    	theForm.dob.focus();
    	return (false);
  	}
	}
}
