function FillSMSFromCell()
{
	if(document.main.carea.value.length==3 &&
	   document.main.cpre.value.length==3 &&
	   document.main.cpost.value.length==4 && 
	   document.main.sarea.value=="" &&
	   document.main.spre.value=="" &&
	   document.main.spost.value=="")
	{
		document.main.sarea.value = document.main.carea.value;
		document.main.spre.value = document.main.cpre.value;
		document.main.spost.value = document.main.cpost.value;
	}
}

function RemoveTextOnFocus(element,str)
{
	if(element.value == str)
	{
		element.value = "";
	}
}

function AddTextOnBlur(element,str)
{
	if(Trim(element.value) == "")
	{
		element.value = str;
	}
}

function ZipTextChange(hidden)
{
	if(document.main.zip.value.length==5)
	{
		GetTimeZone(document.main.zip.value, hidden);
	}
}

function GetTimeZone(zip, hidden)
{
	if (!Sarissa || !document.getElementById) //doesn't support Ajax
	{
		document.main.ajaxInProgress.value = 'false';
		return false;
	}

	// Check if an ajax request is already happening. 
	// If so, don't allow it to happen again.
	if (document.main.ajaxInProgress.value=='true')
	{
		return false;
	}

	// Set up the request
	var xmlhttp =  new XMLHttpRequest();
	xmlhttp.open('POST', 'TimeZoneAjax.php', true);

	// The callback function
	xmlhttp.onreadystatechange = function()
	{
		if (xmlhttp.readyState == 4)
		{
			if (xmlhttp.status == 200)
			{
				GetTimeZoneFeedback(xmlhttp.responseXML, hidden);
			}
			else //server error with TimeZoneAjax.php
			{
				document.main.ajaxInProgress.value = 'false';
				return false;
			}
		}
	}

	document.main.ajaxInProgress.value = 'true';
	
	// Send the POST request
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('zip=' + zip);

	return true;
}

function GetTimeZoneFeedback(responseXML, hidden)
{
	var offset = responseXML.getElementsByTagName('offset')[0].firstChild.data;
	var dst = responseXML.getElementsByTagName('dst')[0].firstChild.data;
	var result = responseXML.getElementsByTagName('result')[0].firstChild.data;
	
	if(result==0)
	{
	  	if(hidden)
	  	{
		  	document.main.timezone.value = offset;
			
			if(dst=="1") document.main.dst.value = "1";
			else document.main.dst.value = "0";
		}
		else
		{
			for (loop=0; loop < document.main.timezone.options.length; loop++)
			{
				if(document.main.timezone.options[loop].value==offset)
				{
					document.main.timezone.selectedIndex = loop;
				}
			}
			
			if(dst=="0") document.main.dst.selectedIndex = 1;
			else document.main.dst.selectedIndex = 0;
		}
	}
	
	// Free up the ajax to go again
	document.main.ajaxInProgress.value = 'false';
}

function ValidateUserForm()
{
	var str = ValidateCreateAccount();
	
	if(str!="")
	{
		alert(str);
		return false;
	}
	
	return true;
}

function ValidateUpdateForm()
{
	var str = ValidateUpdateAccount();
		
	if(str!="")
	{
		alert(str);
		return false;
	}
		
	return true;
}

function ValidateCreateAccount()
{
	if(!document.main.utype[0].checked && !document.main.utype[1].checked) return "Please choose a user type (babysitter or parent)";
	if(!HasValue(document.main.fname.value) || !IsSqlSafe(document.main.fname.value)) return "Please enter a valid first name";
	if(!HasValue(document.main.lname.value) || !IsSqlSafe(document.main.lname.value)) return "Please enter a valid last name";
	if(!HasValue(document.main.street1.value) || !IsSqlSafe(document.main.street1.value)) return "Please enter a street address";
	if(HasValue(document.main.street2.value) && !IsSqlSafe(document.main.street2.value)) return "Please enter a valid value for address 2";
	if(!HasValue(document.main.city.value) || !IsSqlSafe(document.main.city.value)) return "Please enter a valid city name";
	if(!IsLength(document.main.state.value,2) || !IsAlpha(document.main.state.value)) return "Please select a state";
	if(!IsLength(document.main.zip.value,5) || !IsNumeric(document.main.zip.value)) return "Please enter a valid zip code";
	if(!IsLength(document.main.bmonth.value,2) || !IsNumeric(document.main.bmonth.value) ||
	parseInt(document.main.bmonth.value,10) < 1 || parseInt(document.main.bmonth.value,10) > 12) return "Please enter a valid 2-digit birthday month";
	if(!IsLength(document.main.bday.value,2) || !IsNumeric(document.main.bday.value) ||
	parseInt(document.main.bday.value,10) < 1 || parseInt(document.main.bday.value,10) > 31) return "Please enter a valid 2-digit birthday day";
	if(!IsLength(document.main.byear.value,4) || !IsNumeric(document.main.byear.value) ||
	parseInt(document.main.byear.value,10) < 1900 || parseInt(document.main.byear.value,10) > (new Date()).getFullYear()) return "Please enter a valid 4-digit birthday year";
	
	var home = document.main.harea.value + document.main.hpre.value + document.main.hpost.value;
	var cell = document.main.carea.value + document.main.cpre.value + document.main.cpost.value;
	var work = document.main.warea.value + document.main.wpre.value + document.main.wpost.value;
	var sms = document.main.sarea.value + document.main.spre.value + document.main.spost.value;
	
	//check for at least one phone number
	if((!IsLength(home,10) && !IsLength(cell,10) && !IsLength(work,10)) ||
	   (!IsNumeric(home) && !IsNumeric(cell) && !IsNumeric(work))) return "You must enter at least one valid home, cell, or work phone number";
	
	//check each phone number
	if(HasValue(home) && (!IsLength(home,10) || !IsNumeric(home))) return "The home phone number you entered is not valid";
	if(HasValue(cell) && (!IsLength(cell,10) || !IsNumeric(cell))) return "The cell phone number you entered is not valid";
	if(HasValue(work) && (!IsLength(work,10) || !IsNumeric(work))) return "The work phone number you entered is not valid";
	if(HasValue(sms) && (!IsLength(sms,10) || !IsNumeric(sms))) return "The sms phone number you entered is not valid";
	if(HasValue(sms) && (document.main.phone_carrier.value==null || document.main.phone_carrier.value=="" || parseInt(document.main.phone_carrier.value)==0)) return "You must choose a phone carrier if you enter a text messaging phone number";
	
	if(!emailCheck(document.main.email.value)) return "Please enter a valid email address";
	if(document.main.email.value != document.main.confirmemail.value) return "Your email addresses do not match";
	
	if((document.main.password.value.length < 8) || !IsAlphaNumeric(document.main.password.value)) return "Your password must be at least 8 characters long and contain only letters and numbers";
	if(document.main.password.value != document.main.confirmpassword.value) return "Your passwords do not match";
	
	return "";
}

function ValidateUpdateAccount()
{
	if(!HasValue(document.main.fname.value) || !IsSqlSafe(document.main.fname.value)) return "Please enter a valid first name";
	if(!HasValue(document.main.lname.value) || !IsSqlSafe(document.main.lname.value)) return "Please enter a valid last name";
	if(!HasValue(document.main.street1.value) || !IsSqlSafe(document.main.street1.value)) return "Please enter a street address";
	if(HasValue(document.main.street2.value) && !IsSqlSafe(document.main.street2.value)) return "Please enter a valid value for address 2";
	if(!HasValue(document.main.city.value) || !IsSqlSafe(document.main.city.value)) return "Please enter a valid city name";
	if(!IsLength(document.main.state.value,2) || !IsAlpha(document.main.state.value)) return "Please select a state";
	if(!IsLength(document.main.zip.value,5) || !IsNumeric(document.main.zip.value)) return "Please enter a valid zip code";
	if(!IsLength(document.main.bmonth.value,2) || !IsNumeric(document.main.bmonth.value) ||
	parseInt(document.main.bmonth.value,10) < 1 || parseInt(document.main.bmonth.value,10) > 12) return "Please enter a valid 2-digit birthday month";
	if(!IsLength(document.main.bday.value,2) || !IsNumeric(document.main.bday.value) ||
	parseInt(document.main.bday.value,10) < 1 || parseInt(document.main.bday.value,10) > 31) return "Please enter a valid 2-digit birthday day";
	if(!IsLength(document.main.byear.value,4) || !IsNumeric(document.main.byear.value) ||
	parseInt(document.main.byear.value,10) < 1900 || parseInt(document.main.byear.value,10) > (new Date()).getFullYear()) return "Please enter a valid 4-digit birthday year";
	
	var home = document.main.harea.value + document.main.hpre.value + document.main.hpost.value;
	var cell = document.main.carea.value + document.main.cpre.value + document.main.cpost.value;
	var work = document.main.warea.value + document.main.wpre.value + document.main.wpost.value;
	var sms = document.main.sarea.value + document.main.spre.value + document.main.spost.value;
	
	//check for at least one phone number
	if((!IsLength(home,10) && !IsLength(cell,10) && !IsLength(work,10)) ||
	   (!IsNumeric(home) && !IsNumeric(cell) && !IsNumeric(work))) return "You must enter at least one valid home, cell, or work phone number";
	
	//check each phone number
	if(HasValue(home) && (!IsLength(home,10) || !IsNumeric(home))) return "The home phone number you entered is not valid";
	if(HasValue(cell) && (!IsLength(cell,10) || !IsNumeric(cell))) return "The cell phone number you entered is not valid";
	if(HasValue(work) && (!IsLength(work,10) || !IsNumeric(work))) return "The work phone number you entered is not valid";
	if(HasValue(sms) && (!IsLength(sms,10) || !IsNumeric(sms))) return "The sms phone number you entered is not valid";
	if(HasValue(sms) && (document.main.phone_carrier.value==null || document.main.phone_carrier.value=="" || parseInt(document.main.phone_carrier.value)==0)) return "You must choose a phone carrier if you enter a text messaging phone number";
	
	if(!emailCheck(document.main.email.value)) return "Please enter a valid email address";
	if(document.getElementById("changepassword").checked && ((document.main.password.value.length < 8) || !IsAlphaNumeric(document.main.password.value))) return "Your password must be at least 8 characters long and contain only letters and numbers";
	
	return "";
}
