// Form Checking
var isUnload  = true;

function doQualify(){
	if(isFIrstNameOK() && isLastNameOK() && isGenderOK() && isEMailOK() && isZipOK() && isAddressOK() && ValidateBirthDate() && isPnoneOK() && ValidateCaptcha()){

		if(document.form1.offer_66[0].checked || document.form1.offer_66[1].checked){
			//Ok, Nothing to do.
		}else{
			alert('Please Answer YES or NO for all offers');
			return false;
		}
		
		if(document.form1.offer_68[0].checked || document.form1.offer_68[1].checked){
			//Ok, Nothing to do.
		}else{
			alert('Please Answer YES or NO for all offers');
			return false;
		}
		
		isUnload = false;
		document.form1.f_action.value = 1
		document.form1.submit();
	}
	return false;
}

//******************************************************************
// Validates content of fields
//******************************************************************
function checkfield(fieldlabel,fieldname,acceptonly){
var s= new String();
	characters = new RegExp ("[^A-Za-z ]","i");
	numbers = new RegExp ("[^0-9]","i");
	numchar = new RegExp ("[^0-9-^A-Za-z ]","i");

	s = document.form1[fieldname].value;
	result = s.search(eval(acceptonly));
	if (result != -1){
		if (acceptonly == "numchar")
		{
			msg = fieldlabel + " can only contain numbers and characters" + ".";
		}
		else if  (acceptonly == "numbers")
		{
			msg = fieldlabel + " can only contain numbers" + ".";
		}
		else
		{
			msg = fieldlabel + " can only contain characters" + ".";
		}
		document.form1[fieldname].value = ""
		alert(msg);
		document.form1[fieldname].focus();
		return false;
	}
}
function isFIrstNameOK(){
	var field_val=document.form1.first_name.value.replace(/[ ]/g, "");
	if (field_val.length==0){
		alert('Please type your first name');
		document.form1.first_name.value=""
		document.form1.first_name.focus();
		return false;
		}
	return true;
}

function isLastNameOK(){
	var field_val=document.form1.last_name.value.replace(/[ ]/g, "");
	if (field_val.length==0){
		alert('Please type your last name');
		document.form1.last_name.value=""
		document.form1.last_name.focus();
		return false;
		}
	return true;
}

function isGenderOK(){
	if(!document.form1.gender[0].checked && !document.form1.gender[1].checked){
		alert('Please select your gender.');
		document.form1.gender[0].focus();
		return false;
	}
return true;
}
function isZipOK(){
	var field_val=document.form1.zip.value.replace(/[ ]/g, "");
	if (field_val.length<5){
		alert('Please type your postal Zip Code.');
		document.form1.zip.value=""
		document.form1.zip.focus();
		return false;
		}
		
	if (!checkTxtField(field_val,"numbers")){
		alert('Invalid postal Zip Code.');
		document.form1.zip.value=""
		document.form1.zip.focus();
		return false;
		}
		
	return true;
}

function checkTxtField(field_value,acceptonly){
var s= new String();
	characters = new RegExp ("[^A-Za-z ]","i");
	numbers = new RegExp ("[^0-9]","i");
	numchar = new RegExp ("[^0-9-^A-Za-z ]","i");
	s = field_value;
	result = s.search(eval(acceptonly));
	if (result != -1){
		if (acceptonly == "numchar")
		{
			return false;
		}
		else if  (acceptonly == "numbers")
		{
			return false
		}
		else
		{
			return false
		}
		return false;
	}
	return true;
}

function isAddressOK(){
	var field_val=document.form1.address.value.replace(/[ ]/g, "");
	if (field_val.length==0){
		alert('Please type your address');
		document.form1.address.value=""
		document.form1.address.focus();
		return false;
		}
	return true;
}

function isStateOK(){
	if (document.form1.state.options.selectedIndex==0){
		alert('Please select your State.');
		document.form1.state.focus();
		return false;
		}
	//document.form1.full_state.value = document.form1.state.options[document.form1.state.options.selectedIndex].text;
	return true;
}

function isEMailOK(){
	var emstr;  // e-mail string
	var emstr_confirm;  // e-mail string
	var sht;    // '@' position in e-mail
	var dot;    // '.' position in e-mail
	var Wrongsim='"' + " :*'!%+=;,/#";
	
	emstr=document.form1.email.value.toLowerCase();

	sht=emstr.indexOf('@');
	dot=emstr.indexOf('.');

	if(sht==-1 || dot==-1)
		{
		alert("Invalid E-mail!\nPlease Type Again.");
		document.form1.email.value="";
		document.form1.email.focus();
		return false;
		}
	if(sht==0||dot==0)
		{
		alert("Invalid E-mail!\nPlease Type Again.");
		document.form1.email.value="";
		document.form1.email.focus();
		return false;
		}
	if(emstr.charAt(emstr.length-1)=='@'||emstr.charAt(emstr.length-1)=='.')
		{
		alert("Invalid E-mail!\nPlease Type Again.");
		document.form1.email.value="";
		document.form1.email.focus();
		return false;
		}
	if(emstr.split('@').length!=2)
		{
		alert("Invalid E-mail!\nPlease Type Again.");
		document.form1.email.value="";
		document.form1.email.focus();
		return false;
		}
	if(emstr.split('@')[1].indexOf('.')==-1||emstr.split('@')[1].indexOf('.')==0)
		{
		alert("Invalid E-mail!\nPlease Type Again.");
		document.form1.email.value="";
		document.form1.email.focus();
		return false;
		}
	for(i=0;i<emstr.length;i++)
		{
		if(Wrongsim.indexOf(emstr.charAt(i))!=-1)
			{
			alert("Invalid E-mail!\nPlease Type Again.");
			document.form1.email.value="";
			document.form1.email.focus();
			return false;
			}
		}
	return true;
}

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isPnoneOK(){

	var field_val1=document.form1.mphone1.value.replace(/[ ]/g, "");
	var field_val2=document.form1.mphone2.value.replace(/[ ]/g, "");
	var field_val3=document.form1.mphone3.value.replace(/[ ]/g, "");
	
	if (field_val1.length<3){
		alert('Please type your phone Area Code.');
		document.form1.mphone1.value=""
		document.form1.mphone1.focus();
		return false;
	}
	if (field_val2.length<3){
		alert('Please type your phone prefix.');
		document.form1.mphone2.value=""
		document.form1.mphone2.focus();
		return false;
	}
	if (field_val3.length<4){
		alert('Please type your phone number.');
		document.form1.mphone3.value=""
		document.form1.mphone3.focus();
		return false;
	}
return true;
}

function isDate(InMonth,InDay,InYear){
	var daysInMonth = DaysArray(12)
	var strMonth=InMonth
	var strDay=InDay
	var strYear=InYear
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)

	if (strMonth.length<1 || month<1 || month>12 || !isInteger(month)){
		alert("Please enter a valid month")
		document.form1.birth_month.focus();
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || !isInteger(day) || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		document.form1.birth_day.focus();
		return false
	}
	if (strYear.length != 4 || year==0 || !isInteger(year)){
		alert("Please enter a valid 4 digit year")
		document.form1.birth_year.focus();
		return false
	}
return true
}

function ValidateBirthDate(){
	var month = document.form1.birth_month.value
	var day = document.form1.birth_day.value
	var year = document.form1.birth_year.value
	
	//alert(month + "/" + day + "/" + year)
	
	if (isDate(month,day,year)==false){
		return false
	}
    return true
 }

function ValidateCaptcha(){
		var field_val=document.form1.strCAPTCHA.value;
		if (field_val.length<4){
			alert('Enter the 4 digit code.');
			document.form1.strCAPTCHA.focus();
			return false;
		}
		return true;
	}

