// Survey Personal Info Registration Form JScript source code
var isPopUp = true;

//******************************************************************
// Validates content of fields
//******************************************************************
function checkfield(fieldlabel,fieldname,acceptonly){
var s= new String();
	characters = new RegExp ("[^A-Za-zUOAuoa ]","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 isAddres1OK(){
	var field_val=document.form1.address1.value.replace(/[ ]/g, "");
	if (field_val.length==0){
		alert('Please type your post address.');
		document.form1.address1.value=""
		document.form1.address1.focus();
		return false;
		}
	return true;
}

function isCityOK(){
	var field_val=document.form1.city.value.replace(/[ ]/g, "");
	if (field_val.length==0){
		alert('Please type your city.');
		document.form1.city.value=""
		document.form1.city.focus();
		$("#row_city_state").show();
		return false;
		}
	return true;
}

function month_of_birthOK(){
	if(document.form1.birth_month.value=="N"){
	if (document.form1.birth_month.options[document.form1.birth_month.selectedIndex].value == "N")
		{
		alert('Plese select your month of birth.');
		document.form1.birth_month.focus();
        return false;
		}
	}	
return true;
}
	
function day_of_birthOK(){
	if(document.form1.birth_day.value=="N"){
	if (document.form1.birth_day.options[document.form1.birth_day.selectedIndex].value == "N")
		{
		alert('Plese select your day of birth.');
		document.form1.birth_day.focus();
        return false;
		}
	}	
return true;
}

function year_of_birthOK(){
	if(document.form1.birth_year.value=="N"){
		if (document.form1.birth_year.options[document.form1.birth_year.selectedIndex].value == "N")
		{
		alert('Plese select your year of birth.');
		document.form1.birth_year.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;
		}
	return true;
}

function isPnone1OK(){
	var field_val1=document.form1.phone1_1.value.replace(/[ ]/g, "");
	var field_val2=document.form1.phone1_2.value.replace(/[ ]/g, "");
	var field_val3=document.form1.phone1_3.value.replace(/[ ]/g, "");
	
	if (field_val1.length<3){
		alert('Please type your phone Area Code.');
		document.form1.phone1_1.value=""
		document.form1.phone1_1.focus();
		return false;
	}
	if (field_val2.length<3){
		alert('Please type your phone prefix.');
		document.form1.phone1_2.value=""
		document.form1.phone1_2.focus();
		return false;
	}
	if (field_val3.length<4){
		alert('Please type your phone number.');
		document.form1.phone1_3.value=""
		document.form1.phone1_3.focus();
		return false;
	}
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 isPnone2OK(){
return true;
}
function isMaritalStatusOk(){
return true;
/*
var isSelected = false
	for(var i=0;i<document.form1.marital.length;i++){
		if(document.form1.marital[i].checked){
			isSelected=true;
		}
	}	
	if(!isSelected){
		alert("Please select your Marital Status");
		return false;
	}
	return true;
*/	
}

function isemployedOk(){
	return true;
}
function isHHIncomeOk(){
	return true;
}
function isEducationOk(){
return true;
/*
	if(document.form1.education.selectedIndex==0){
		alert("Please select your education level");
		document.form1.education.focus();
		return false;
	}
	return true;
*/	
}
function isOccupationOk(){
return true;
}
function isConsiderOk(){
	if(document.form1.constobe.selectedIndex==0){
		alert("Please select your ethnicity in \"Which do you consider yourself to be?\"");
		document.form1.constobe.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 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
 }

//--- 09/19/2010 get city - state by zip 
function getCSByZ(sZIP){
$("#city").val("");
$("#state").val("AL");					
$.ajax({
        type: "POST",
        url: "/projlib/itsrv_cmn/wsUtils.asp",
        data: "m=CITYSTATEBYZIP&zip="+sZIP,
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        dataType: "json",
        timeout: 20000,
        success: function(ret) {
			if(ret.Err==0){
				if(ret.Result.City!="" && ret.Result.State!=""){
					$("#city").val(ret.Result.City);
					$("#state").val(ret.Result.State);					
					return true;
				}
			}
			$("#row_city_state").show();
        },
        error: function(ret) {
            //AJAX failure, show city-state
			$("#row_city_state").show();
        }
    });
}
