function checkJoin() {
	var why = "";
    
    why += checkFirst(document.survey["a[T-Q782]"].value);
    why += checkLast(document.survey["a[T-Q783]"].value);
    why += checkEmail(document.survey["a[T-Q784]"].value);
    why += checkEmail2(document.survey["a[T-Q784]"].value, document.survey["a[T-Q785]"].value);
	why += checkDOB(
			document.survey["a[DM-Q770]"].selectedIndex,
			document.survey["a[DD-Q770]"].selectedIndex,
			document.survey["a[D-Q770]"].selectedIndex
		);
	why += checkGender(document.survey["a[R-Q755]"]);
	why += checkMailingAddress(
			document.survey["a[T-Q657]"].value,
			document.survey["a[T-Q658]"].value,
			document.survey["a[S2T-Q659]"].options[document.survey["a[S2T-Q659]"].selectedIndex].value,
			document.survey["a[T-Q660]"].value
		);
	// why += checkPhone(document.survey["a[T-Q760]"], "");
	why += checkPhone(document.survey["a[T-Q760]"], "Home");
	why += checkPhone(document.survey["a[T-Q662]"], "Cell");
	// why += checkPhone(document.survey["a[T-Q663]"], "Business");
    		
	if (why != "") {
	
       alert("Oops! Please fix the errors listed below:\n\n" + why);
       return false;
    
    }
	
    return true;
}

function checkFirst (strng) {
	var error = "";
    if (strng == "") { error = "You forgot to enter your First name.\n"; }
    return error;
}

function checkLast (strng) {
	var error = "";
	if (strng == "") { error = "You forgot to enter your Last name.\n"; }
	return error;
} 

function checkEmail (strng) {
	var error="";
	var emailFilter=/^.+@.+[.].{2,4}$/;
	if (!(emailFilter.test(strng))) { 
		  error = "You did not enter a valid Email address.\n";
	}
	return error;
}

function checkEmail2 (strng, strng2) {
	var error = "";
	if (strng != strng2) {
		error = "You entered 2 different Email addresses.\n";
	}
	return error;
}

function checkRemoteZip(zip,state,city){
    msg = ""    
    
    $.ajax({
	url: 'check_zip.php',
	data: 'zip='+zip+'&state='+state+'&city='+city,
	type: 'GET',
	async: false,
	cache: false,
	timeout: 60000,
	error: function(){
	    msg = "Error while checking zip code"
	},
	success: function(data){
	    msg = data
	} 
    })
    return msg
}


function checkMailingAddress (address, city, state, zip) {
	var error = "";
	if (address == "") { error += "You forgot to enter your Address in Mailing address.\n"; }
	if (city == "") { error += "You forgot to enter your City in Mailing address.\n"; }
	if (state == "") { error += "You forgot to enter your State in Mailing address.\n"; }
	//if ( zip.length != 5 || !isInteger(zip) ) { error += "Your Zip Code must be five digits long.\n"; }
	error += checkRemoteZip(zip, state, city)
	return error;
}

function checkGender (gender) {
	var error = "You forgot to select your Gender.\n";
	for(i=0; i < gender.length; i++) {
		if (gender[i].checked == true) {
			var error = "";
		}
	}
	return error;
}

function checkDOB (strng, strng2, strng3) {
	var error = "";
	if(strng == 0 || strng2 == 0 || strng3 == 0) {
		error = "You forgot to select your Birthday.\n";
	}
	return error;
}

function checkPhone (field, phoneName) {
	var error = "You did not enter a valid " + phoneName + " Phone Number.\n";
	if ( null == field ) {
		return error;
	}
	var phone = field.value;
	if ( (phone == null) || (phone == "") ) {
		return error;
	}
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < phone.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = phone.charAt(i);
        if ("()- +".indexOf(c) == -1) returnString += c;
    }
	phone = returnString;
	if ( phone.length < 9 ) { //minDigitsInIPhoneNumber
		return error;
	}
	if ( !isInteger(phone) ) {
		return error;
	}
	return "";
}

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;
}
