function checkIntraState(element){
	zip = element.form.ZipFrom.value;
	state = element.form.StateTo.value
	if(isValidZip(zip) &&  state.length > 1){
		isIntraState(zip,state, 
		function(req){
			if(req.responseText.trim() == 'true'){ showRedirect("Redirecting...","<b>Sorry, but our moving partner does not handle self-service moves within the same state. We'll redirect you to our truck rental page for a free estimate!</b><br />");}
		});
	}
}

function showRedirect(title,message){

	s = document.getElementsByTagName("select");
	
	for(i = 0; i < s.length; i++){ s[i].style.visibility = 'hidden' }
		
	var r = '<div style="visibility: visible; z-index: 898; position: absolute; top: 0px; left: 0px; width: 100%; height: 200%; background-color: white; filter:alpha(opacity=80); opacity: 0.8;"></div>';
	r+='<div id="popupBoxComplex" style="visibility: visible; top: 300px; font-size: 15px;"> '
	r+='	<a id="fullImage"></a><div id="fullImageBoxComplex"></div>'
	r+='	<div class="titlebox">'
	r+='			<div id="img_complex_name" style="font-size: 16px;">'
	r+=title
	r+='</div>'
	r+='	</div>'
	r+='	<div style="clear:both;"></div>'
	r+=message
	r+='</div>'

	
	document.body.innerHTML += r;
	
	window.setTimeout(
		function(){
			document.location = "/review/redir.jsp?id=partner_moving.com&addcid=F&to=http%3A%2F%2Fapartmentratings.moving.com%2Ftruck-rental%2Fmoving-truck-rental%2Findex.asp%3Fsource%3DTRCHPaptrt001";
		}
		,5000
	);
}

function isIntraState(fromZip, toState, dofunct){
	AjaxRequest.get({
		'url':'/review/ajax_isIntraState.jsp?state='+toState+'&zip='+fromZip,
		'onSuccess':dofunct
	});
}

function isValidZip(zip){
	return (null != zip.match(new RegExp('^[0-9][0-9][0-9][0-9][0-9]$')));
}


function setMonthDays(formName)
{
	var objForm = document.forms[formName];
	var optDay = objForm.MoveDay.options;
	var optMonth = objForm.MoveMonth.options;
	var selYear = objForm.MoveYear;
	var mvDate=new Date();
	mvDate.setDate(mvDate.getDate()+30);

	optMonth.selectedIndex = mvDate.getMonth();
	optDay.selectedIndex = mvDate.getDate();
	selectHelper( selYear, mvDate.getFullYear());
}

function OpenLegendPopUp()
			{
				var helpWin
				helpWin = window.open('/Moving_and_Planning/Quotes/AvailableServicesLegendPopUp.asp', 'helpWin', 'width=460,height=475,toolbar=no,scrollbars=yes,resizable=yes');
				helpWin.focus();
			}
			
function setFutureDate()
			{
				var dMDate = new Date(nYear, nMonth - 1, nDay);
				var objForm = document.frmMoveQuotes;
				alert(dMDate);
				
			}
			
function getMoveDate()
			{
				var dMDate;
				var objForm;
				var nMonth;
				var nDay;
				var nYear;
			
				objForm = document.frmMoveQuotes;
				nYear = objForm.MoveYear.options[objForm.MoveYear.selectedIndex].value;
				nMonth = objForm.MoveMonth.options[objForm.MoveMonth.selectedIndex].value;
				nDay = objForm.MoveDay.options[objForm.MoveDay.selectedIndex].value;
				dMDate = new Date(nYear, nMonth - 1, nDay);
			   
				return dMDate;
			}
			
function replaceAll(sSource, sFind, sReplace)
			{
				var nIndex = 0;
				var nNewIndex;
				var sReturn = "";
			
				nNewIndex = sSource.indexOf(sFind, nIndex);
				while (nNewIndex >= 0)
				{	sReturn = sReturn + sSource.substring(nIndex, nNewIndex) + sReplace;
					nIndex = nNewIndex + sFind.length;
					nNewIndex = sSource.indexOf(sFind, nIndex);
				}
				sReturn = sReturn + sSource.substring(nIndex, sSource.length);
			
				return sReturn;
			}
			
function validateField(sFieldName, sFieldLabel)
			{
				var objField = document.frmMoveQuotes.elements[sFieldName];
				if (objField.type == "select-one")
				{	
					if(isNaN(objField.options[objField.selectedIndex].value) && objField.options[objField.selectedIndex].value != '' && objField.selectedIndex != 0){
						return true;
					}
					else if (objField.options[objField.selectedIndex].value > 0)
						return true;
			
					alert(sFieldLabel + " must be selected");
					objField.focus();
					return false;
				}
			
				if (objField.type == "text")
				{	if (sFieldName == "CalcWeight")
					{	if (isNaN(objField.value))
						{	alert(sFieldLabel + " must be entered");
							objField.focus();
							return false;
						}
					}
					if (objField.value.length > 0)
						return true;
			
					alert(sFieldLabel + " must be entered");
					objField.focus();
					return false;
				}
			
				if (objField.type == "hidden")
					return true;
			
				alert("Unknown field type: " + sFieldName + " (" + objField.type + ")");
				return false;
			}

function validateForm()
			{

				var objForm = document.frmMoveQuotes;
				var nMoveTypeID = 1;

				// set MDate
				
				var nYear = objForm.MoveYear.options[objForm.MoveYear.selectedIndex].value;
				var nMonth = objForm.MoveMonth.options[objForm.MoveMonth.selectedIndex].value;
				var nDay = objForm.MoveDay.options[objForm.MoveDay.selectedIndex].value;
				
				document.getElementById( 'MDate' ).value = nMonth + '/' + nDay +'/'+ nYear;
		
				var now = new Date();
				var currDate = new Date(now.getFullYear(), now.getMonth(), now.getDate());
				var moveDate = getMoveDate();
			
				if (moveDate - orig5Date < 0)
				{
					alert( "Sorry we can't accept move dates earlier than 5 days from today. Please try again." );
					return false;
				}

				if (moveDate - orig180Date > 0)
				{
					alert( "Sorry we can't accept move dates later than 180 days from today. Please try again." );
					return false;
				}
			
				if (moveDate - currDate < 0)
				{
					alert ("Sorry we can't accept move dates earlier than current date. Please try again.");
					return false;
				}

				if (!validateField("Zip", "From Zip Code"))
					return false;
				if (!validateField("ToState", "To State"))
					return false;
				if (!validateField("ToCity", "To City"))
					return false;
				
			
				if ((nMoveTypeID == 1)||(nMoveTypeID == 2)||(nMoveTypeID == 4)){
					if (!validateField("ApproxWeight", "Move Size")) {
						return false;
					}
				}
				
				return true;
			}

			
function getRadioValue(radiobuttons) {
	for (var i = 0; i < radiobuttons.length; i++) {
		if (radiobuttons[i].checked) {
			return radiobuttons[i].value;
		}
	}
	return "";
}

			
function selectHelper( elemSelect, myValue )
{
	if( myValue == "~~*~~" )
		myValue = '';

	try {

		var setFlag = false;

		for( i=0; i<elemSelect.options.length; i++ )
		{
			if( elemSelect.options[i].value == myValue )
			{
				elemSelect.selectedIndex = i;
				setFlag = true;
				continue;
			}
		}

		if( !setFlag )
			elemSelect.selectedIndex = 0;
	}
	catch( e )
	{
		alert( "omg: "+e );
	}
}

function validateField(objForm, sFieldName, sFieldLabel)
			{
				try{
				var objField = objForm.elements[sFieldName];
				if (objField.type == "select-one")
				{	
					if(isNaN(objField.options[objField.selectedIndex].value) && objField.options[objField.selectedIndex].value != '' && objField.selectedIndex != 0){
						return true;
					}
					else if (objField.options[objField.selectedIndex].value > 0)
						return true;
			
					alert(sFieldLabel + " must be selected");
					objField.focus();
					return false;
				}
			
				if (objField.type == "text")
				{	if (sFieldName == "CalcWeight")
					{	if (isNaN(objField.value))
						{	alert(sFieldLabel + " must be entered");
							objField.focus();
							return false;
						}
					}
					if (objField.value.length > 0)
						return true;
			
					alert(sFieldLabel + " must be entered");
					objField.focus();
					return false;
				}
			
				if (objField.type == "hidden")
					return true;
			
				alert("Unknown field type: " + sFieldName + " (" + objField.type + ")");
				return false;
				}catch(err){
					alert(err);
					alert(sFieldName);
					return false;
				}
			}

function validateForm(formName){
				var objForm = document.forms[formName];
				var email = objForm.Email.value;

				var phone = objForm.DayPhone.value		
				
				// set MDate
				
				var nYear = objForm.MoveYear.options[objForm.MoveYear.selectedIndex].value;
				var nMonth = objForm.MoveMonth.options[objForm.MoveMonth.selectedIndex].value;
				var nDay = objForm.MoveDay.options[objForm.MoveDay.selectedIndex].value;
				
				
				var now = new Date();
				var currDate = new Date(now.getFullYear(), now.getMonth(), now.getDate());
				var moveDate = new Date(nYear, --nMonth, nDay);
/*					
				if (moveDate - orig5Date < 0)
				{
					alert( "Sorry we can't accept move dates earlier than 5 days from today. Please try again." );
					return false;
				}

				if (moveDate - orig180Date > 0)
				{
					alert( "Sorry we can't accept move dates later than 180 days from today. Please try again." );
					return false;
				}
*/			
				if (moveDate - currDate < 0)
				{
					alert ("Sorry we can't accept move dates earlier than current date. Please try again.");
					return false;
				}
				
				if (!validateField(objForm, "Email", "Email"))
					return false;
					
				if (!Validate_Email_Address(email)){
					alert("Please provide a valid email address");
					return false;					
				}
				
				if(!Validate_Phone(phone)){
					alert("Please provide a valid phone number");
					return false;
				}
				
				if (!validateField(objForm, "FirstName", "First Name"))
					return false;
				if (!validateField(objForm, "LastName", "Last Name"))
					return false;										
															
				if (!validateField(objForm, "ZipFrom", "Current Zip Code"))
					return false;

				if (!validateField(objForm, "StateTo", "Destination State"))
					return false;
				if (!validateField(objForm, "CityTo", "Destination City"))
					return false;
				
				if (!validateField(objForm, "ApproxWeight", "Move Size"))
					return false;
				
				if (!validateField(objForm, "SelectMoveType", "Type of move")) 
					return false;
				
				if (!validateField(objForm, "BestCallTime", "Best time to call")) {
						return false;
				}
									
				return true;
			}