//***************************************** 
//	JDAY AREA/PREFIX CHECKER
//***************************************** 
var AreaPrefixOK = false;
function checkAreaPrefix() {
		AreaPrefixOK = false;
		var sendMe = "";
		sendMe += 'rand='			+ Math.random();
		sendMe += '&day_area=' 		+ document.Entry.day_phone_area.value;
		sendMe += '&day_prefix=' 	+ document.Entry.day_phone_prefix.value;
		sendMe += '&night_area=' 	+ document.Entry.night_phone_area.value;
		sendMe += '&night_prefix=' 	+ document.Entry.night_phone_prefix.value;
		go2url_FIELDCHECK = '?checkAreaPrefix=true&'+sendMe;
		var cObj = YAHOO.util.Connect.asyncRequest('get',go2url_FIELDCHECK,callback_FIELDCHECK);
}
var responseSuccess_FIELDCHECK = function(o) {
	//ResponseArray will always be atleast a 2 item array
	//Item 1:  Useless (html title tags, etc... unused)
	//Item 2:  The Field that cause the error
	//Item 3:  Will be "invalid" if Item2 is > ""
	responseArray = o.responseText.split("|||");
	//ERROR!
	if (responseArray.length == 3) {
		// o.responseText looks like "uselessHTML|||field|||errorMsg"
		field = responseArray[1];
		errorMsg = responseArray[2];
		invalCase = "bad_areaprefix_"+field;
		if (document.getElementById("basic_info") != null) {
			document.getElementById("basic_info").style.display = "block";
		}
		invalidPop(invalCase);
	//NO ERRORS
	}else{
		AreaPrefixOK = true;
		checkFormOK = checkForm();
		if (checkFormOK != false) {
			document.Entry.submit();
		}else{
			AreaPrefixOK = false;
		}
	}
}
var responseFailure_FIELDCHECK = function(o) {alert("An Error has occured, please try again.");}
var callback_FIELDCHECK = {success:responseSuccess_FIELDCHECK,failure:responseFailure_FIELDCHECK}


function popAlert(intl_err,fieldToCheck) {
	if (typeof document.getElementById('basic_info') != "undefined") {
		document.getElementById('basic_info').style.display = 'block';
	}
	alert(intl_err);
	fieldToCheck.focus();
	return false;
}	

var checkNightPhone = true;
var errZipCodeIsBlank = "Please Fill in your ZIP Code.";
var errZipCodeIsInvalid = "Please enter a valid zip code.";
var errInvalidPostalCode = "Please enter a valid postal code.";
var errInvalidFirstName = "Please enter a valid first name.";
var errInvalidLastName = "Please enter a valid last name.";
var errInvalidAddress = "Please enter a valid address.";
var errInvalidCity = "Please fill in your city.";
var errStateNotSelected = "You must select a state.";
var errInvalidState = "You must specify the state/province you currently reside in.";
var errInvalidEmailAddress = "Please include a proper e-mail address of the form \nxxx@xxx.xxx\nor\nxxx@xxx.xx.xxx";
var errInvalidCountry = "You must select a country.";
var errInvalidDayphoneAreacode = "Please enter a valid area code.";
var errInvalidDayphonePrefix = "Please enter a valid prefix.";
var errInvalidDayphoneSuffix = "Please enter a valid suffix.";
var errInvalidDayphone = "Please enter a valid phone number.";
var errInvalidNightphoneAreacode = "Please enter a valid work phone area code.\n(If you cannot be contacted at work supply your home phone as your work phone number. )";
var errInvalidNightphonePrefix = "Please enter a valid work phone prefix.\n(If you cannot be contacted at work supply your home phone as your work phone number. )";
var errInvalidNightphoneSuffix = "Please enter a valid work phone suffix.";
var errInvalidNightphone = "Please enter a valid work phone number.\n(If you cannot be contacted at work supply your home phone as your work phone number. )";
var errInvalidDayphoneAreacodeLength = "Your area or city code must be at least 3 digits.";
var errInvalidDayphonePrefixLength = "Your phone prefix must be 3 digits.";
var errInvalidDayphoneSuffixLength = "Your phone suffix must be 4 digits.";
var errInvalidNightphoneAreacodeLength = "Your work phone area or city code must be at least 3 digits.";
var errInvalidNightphonePrefixLength = "Your work phone prefix must be 3 digits.";
var errInvalidNightphoneSuffixLength = "Your work phone suffix must be 4 digits.";
var errInvalidDayphoneINTLCountryCode = "You must provide a valid day phone country code.";
var errInvalidDayphoneINTLAreacode = "You must provide a valid day phone area code.";
var errInvalidDayphoneINTL = "You must provide a valid day phone number.";
var errInvalidNightphoneINTLCountryCode = "You must provide a valid work phone country code.";
var errInvalidNightphoneINTLAreacode = "You must provide a valid work phone area code.";
var errInvalidNightphoneINTL = "You must provide a valid work phone number.";

function CheckBasicInfo(){
		
		//zip code check
		//check the length of the zip code
				
		var mycountry = trim(document.Entry.country.options[document.Entry.country.selectedIndex].text);
		var sel_country = 'US';
	
		
		switch (mycountry.toUpperCase())
		{
			case 'UNITED STATES':
			case 'U.S.A.':
			case 'USA':
			case 'U.S.':
			case 'US':
			case 'UNITED STATES OF AMERICA':
				sel_country = 'US';
				break;
			case 'CANADA':
			case 'CAN':
			case 'CA':
				sel_country = 'CAN';
				break;
			default:
				sel_country = 'other';
				break;
		}
		
		if (sel_country != "other" && typeof document.Entry.zip != "undefined")
		{
			if (document.Entry.zip.value == null || document.Entry.zip.value.length == 0) 
			{
				document.getElementById('basic_info').style.display = 'block';
				alert(errZipCodeIsBlank);
				document.Entry.zip.focus();
				return false;
			}
		}

		switch (sel_country)
		{
			case 'US':
				//make sure it is all digits
				var numfieldDisplayArray = new Array("Zip Code");
				var numfieldArray = new Array("zip");
			
				if (typeof document.Entry.zip != "undefined" && !checknum(numfieldArray,numfieldDisplayArray,document.Entry))
				{
					return false;
				}
				
				//make sure it is 5 digits
				if (typeof document.Entry.zip != "undefined" && document.Entry.zip.value.length != 5)
				{
					alert(errZipCodeIsInvalid);
					document.Entry.zip.focus();
					return false;
				}
				break;
			case 'CAN':
				//make sure it is 6 long
				//remove all spaces inside the string

				if (typeof document.Entry.zip != "undefined") {
					var can_zip='';;
					var myzip = document.Entry.zip.value;
					for (i=0; i<myzip.length; i++)
					{
						if (myzip.charAt(i) != ' ')
						{
							can_zip += myzip.charAt(i);
						}
					}
					if (can_zip.length != 6)
					{
						alert(errInvalidPostalCode);
						document.Entry.zip.focus();
						return false;
					}
				}
				break;
			default:
				//do no validation
				break;
		}

		//first name check
		if (document.Entry.first_name.value == null || document.Entry.first_name.value.length < 2) 
		{
			document.getElementById('basic_info').style.display = 'block';
			alert(errInvalidFirstName);
			document.Entry.first_name.focus();
			return false;
		}
		//last name check
		if (document.Entry.last_name.value == null || document.Entry.last_name.value.length < 2) 
		{
			document.getElementById('basic_info').style.display = 'block';
			alert(errInvalidLastName);
			document.Entry.last_name.focus();
			return false;
		}
		
		//address check
		if (typeof document.Entry.address != "undefined" && (document.Entry.address.value == null || document.Entry.address.value.length < 2)) 
		{
			document.getElementById('basic_info').style.display = 'block';
			alert(errInvalidAddress);
			document.Entry.address.focus();
			return false;
		}
		
		//city check
		if (document.Entry.city.value == null || document.Entry.city.value.length == 0) 
		{
			document.getElementById('basic_info').style.display = 'block';
			alert(errInvalidCity);
			document.Entry.city.focus();
			return false;
		}
		
		//state check
		if (sel_country != "other" && typeof document.Entry.state != "undefined")
		{
			//US or canadian drop-down, validate it
			if (typeof document.Entry.state.options != "undefined" && document.Entry.state.options[document.Entry.state.selectedIndex].value=="0")
			{
				document.getElementById('basic_info').style.display = 'block';
				alert(errStateNotSelected);
				document.Entry.state.focus();
				return false;
			//US or canadian input box, validate it
			}else if(document.Entry.state.value == ""){
				document.getElementById('basic_info').style.display = 'block';
				alert(errInvalidState);
				document.Entry.state.focus();
				return false;
			}
		}

		//Email check
		if (!email_check(document.Entry.email.value))
		{
			document.getElementById('basic_info').style.display = 'block';
			alert(errInvalidEmailAddress);
			document.Entry.email.focus();
			return false;
		}

		//country check
		if ((document.Entry.country.options[document.Entry.country.selectedIndex].value=="0") || (document.Entry.country.options[document.Entry.country.selectedIndex].value==""))
		{
			document.getElementById('basic_info').style.display = 'block';
			alert(errInvalidCountry);
			document.Entry.country.focus();
			return false;
		} 
		





		//***************************************** 
		//	DOMESTIC (US/CANADA) PHONE NUMBER CHECK
		//***************************************** 
		if (sel_country != "other"){


			if (AreaCheck(document.Entry.day_phone_area.value))
			{
				document.getElementById('basic_info').style.display = 'block';
				alert(errInvalidDayphoneAreacode);
				document.Entry.day_phone_area.focus();
				return false;
			}
			
			if (PrefixCheck(document.Entry.day_phone_prefix.value))
			{
				document.getElementById('basic_info').style.display = 'block';
				alert(errInvalidDayphonePrefix);
				document.Entry.day_phone_prefix.focus();
				return false;
			}
			
			temp=document.Entry.day_phone_prefix.value+document.Entry.day_phone_suffix.value;
			if (Phone7(temp))
			{
				document.getElementById('basic_info').style.display = 'block';
				alert(errInvalidDayphone);
				document.Entry.day_phone_area.focus();
				return false;
			}
			
			temp=document.Entry.day_phone_prefix.value+document.Entry.day_phone_prefix.value+document.Entry.day_phone_suffix.value;
			if (Phone10(temp))
			{
				document.getElementById('basic_info').style.display = 'block';
				alert(errInvalidDayphone);
				document.Entry.day_phone_area.focus();
				return false;
			}
			//night phone
			if (document.Entry.night_phone_area && checkNightPhone)
			{
				if (AreaCheck(document.Entry.night_phone_area.value))
				{
					document.getElementById('basic_info').style.display = 'block';
					alert(errInvalidNightphoneAreacode);
					document.Entry.night_phone_area.focus();
					return false;
				}
				
				if (PrefixCheck(document.Entry.night_phone_prefix.value))
				{
					document.getElementById('basic_info').style.display = 'block';
					alert(errInvalidNightphonePrefix);
					document.Entry.night_phone_prefix.focus();
					return false;
				}
				
				temp=document.Entry.night_phone_prefix.value+document.Entry.night_phone_suffix.value;
				if (Phone7(temp))
				{
					document.getElementById('basic_info').style.display = 'block';
					alert(errInvalidNightphone);
					document.Entry.night_phone_area.focus();
					return false;
				}
				
				temp=document.Entry.night_phone_prefix.value+document.Entry.night_phone_prefix.value+document.Entry.night_phone_suffix.value;
				if (Phone10(temp))
				{
					document.getElementById('basic_info').style.display = 'block';
					alert(errInvalidNightphone);
					document.Entry.night_phone_area.focus();
					return false;
				}
			}
			//Begin phone check
			if (document.Entry.day_phone_area.value == null || document.Entry.day_phone_area.value.length == 0)
			{
				document.getElementById('basic_info').style.display = 'block';
				alert(errInvalidDayphoneAreacode);
				document.Entry.day_phone_area.focus()
				return false;
			}
			else
			{
				if (document.Entry.day_phone_area.value.length	< 3)
				{
					document.getElementById('basic_info').style.display = 'block';
					alert(errInvalidDayphoneAreacodeLength);
					document.Entry.day_phone_area.focus();
					return false;
				}
			}
			var numfieldDisplayArray = new Array("Phone Area Code");
			var numfieldArray = new Array("day_phone_area");
			if (!checknum(numfieldArray,numfieldDisplayArray,document.Entry)){return false;}
			
			if (document.Entry.day_phone_prefix.value == null || document.Entry.day_phone_prefix.value.length == 0)
			{
				document.getElementById('basic_info').style.display = 'block';
				alert(errInvalidDayphonePrefix);
				document.Entry.day_phone_prefix.focus()
				return false;
			}
			else
			{
				if (document.Entry.day_phone_prefix.value.length	< 3)
				{
					document.getElementById('basic_info').style.display = 'block';
					alert(errInvalidDayphonePrefixLength);
					document.Entry.day_phone_prefix.focus();
					return false;
				}
			}
			
			var numfieldDisplayArray = new Array("Phone Prefix");
			var numfieldArray = new Array("day_phone_prefix");
		
			if (!checknum(numfieldArray,numfieldDisplayArray,document.Entry)){return false;}
			
			if (document.Entry.day_phone_suffix.value == null || document.Entry.day_phone_suffix.value.length == 0)
			{
				document.getElementById('basic_info').style.display = 'block';
				alert(errInvalidDayphoneSuffix);
				document.Entry.day_phone_suffix.focus()
				return false;
			}
			else
			{
				if (document.Entry.day_phone_suffix.value.length	< 4)
				{
					document.getElementById('basic_info').style.display = 'block';
					alert(errInvalidDayphoneSuffixLength);
					document.Entry.day_phone_suffix.focus();
					return false;
				}
			}
			
			var numfieldDisplayArray = new Array("Phone Suffix");
			var numfieldArray = new Array("day_phone_suffix");
			if (!checknum(numfieldArray,numfieldDisplayArray,document.Entry)){return false;}
			//end phone check
			
			//Night phone checks
			if (document.Entry.night_phone_area && checkNightPhone)
			{
				if (document.Entry.night_phone_area.value == null || document.Entry.night_phone_area.value.length == 0)
				{
					document.getElementById('basic_info').style.display = 'block';
					alert(errInvalidNightphoneAreacode);
					document.Entry.night_phone_area.focus()
					return false;
				}
				else
				{
					if (document.Entry.night_phone_area.value.length	< 3)
					{
						document.getElementById('basic_info').style.display = 'block';
						alert(errInvalidNightphoneAreacodeLength);
						document.Entry.night_phone_area.focus();
						return false;
					}
				}
				
				var numfieldDisplayArray = new Array("Phone Area Code");
				var numfieldArray = new Array("night_phone_area");
				if (!checknum(numfieldArray,numfieldDisplayArray,document.Entry)){return false;}
				
				
				if (document.Entry.night_phone_prefix.value == null || document.Entry.night_phone_prefix.value.length == 0)
				{
					document.getElementById('basic_info').style.display = 'block';
					alert(errInvalidNightphonePrefix);
					document.Entry.night_phone_prefix.focus()
					return false;
				}
				else
				{
					if (document.Entry.night_phone_prefix.value.length	< 3)
					{
						document.getElementById('basic_info').style.display = 'block';
						alert(errInvalidNightphonePrefixLength);
						document.Entry.night_phone_prefix.focus();
						return false;
					}
				}
				
				
				var numfieldDisplayArray = new Array("Phone Prefix");
				var numfieldArray = new Array("night_phone_prefix");
			
				if (!checknum(numfieldArray,numfieldDisplayArray,document.Entry)){return false;}
			
				if (document.Entry.night_phone_suffix.value == null || document.Entry.night_phone_suffix.value.length == 0)
				{
					document.getElementById('basic_info').style.display = 'block';
					alert(errInvalidNightphoneSuffix);
					document.Entry.night_phone_suffix.focus()
					return false;
				}
				else
				{
					if (document.Entry.night_phone_suffix.value.length	< 4)
					{
						document.getElementById('basic_info').style.display = 'block';
						alert(errInvalidNightphoneSuffixLength);
						document.Entry.night_phone_suffix.focus();
						return false;
					}
				}
				
				var numfieldDisplayArray = new Array("Phone Suffix");
				var numfieldArray = new Array("night_phone_suffix");
				if (!checknum(numfieldArray,numfieldDisplayArray,document.Entry)){return false;}
			}
			//end phone check	

			//***************************************** 
			//	JDAY AREA/PREFIX CHECKER
			//***************************************** 
			if (AreaPrefixOK) {
				return true;
			}else{
				checkAreaPrefix();
				return false;
			}





		//***************************************** 
		//	INTERNATIONAL PHONE NUMBER CHECK
		//***************************************** 
		}else if (sel_country == "other" && typeof document.Entry.day_phone_ccode != "undefined"){
			//#######################INTL Day Phone Sanity Check####################//		
				fieldToCheck = document.Entry.day_phone_ccode;
				if (fieldToCheck.value == null || fieldToCheck.value.length < 1 || isNumber(fieldToCheck.value) == false) {intl_err = errInvalidDayphoneINTLCountryCode;popAlert(intl_err,fieldToCheck);return false;}
				fieldToCheck = document.Entry.day_phone_area_intl;
				if (fieldToCheck.value == null || fieldToCheck.value.length < 3 || intl_num_check(fieldToCheck.value) == false)	{intl_err = errInvalidDayphoneINTLAreacode;popAlert(intl_err,fieldToCheck);return false;}
				fieldToCheck = document.Entry.day_phone_intl;
				if (fieldToCheck.value == null || fieldToCheck.value.length < 3 || intl_num_check(fieldToCheck.value) == false)	{intl_err = errInvalidDayphoneINTL;popAlert(intl_err,fieldToCheck);return false;}
			//#######################INTL Day Phone Sanity Check####################//

			//#######################INTL Night Phone Sanity Check####################//
				fieldToCheck = document.Entry.night_phone_ccode;
				if (fieldToCheck.value == null || fieldToCheck.value.length < 1 || isNumber(fieldToCheck.value) == false) {intl_err = errInvalidNightphoneINTLCountryCode;popAlert(intl_err,fieldToCheck);return false;}
				fieldToCheck = document.Entry.night_phone_area_intl;
				if (fieldToCheck.value == null || fieldToCheck.value.length < 3 || intl_num_check(fieldToCheck.value) == false) {intl_err = errInvalidNightphoneINTLAreacode;popAlert(intl_err,fieldToCheck);return false;}
				fieldToCheck = document.Entry.night_phone_intl;
				if (fieldToCheck.value == null || fieldToCheck.value.length < 3 || intl_num_check(fieldToCheck.value) == false) {intl_err = errInvalidNightphoneINTL;popAlert(intl_err,fieldToCheck);return false;}
			//#######################INTL Night Phone Sanity Check####################//
		
			return true;

		}else{

		//Do Nothing for now...no validation

		}
}