//Define an object that will be used as validation functions return value.
function ReturnInfo(iReturnCode, sErrorDescription, iErrorField) {
  this.ReturnCode = iReturnCode;
  this.ErrorDescription = sErrorDescription;
  this.ErrorField = iErrorField;
}
	
//User Error Object
function UserError(iErrorCode, sErrorDesc, sErroneousField) {
    
	this.ErrorCode = iErrorCode;
	this.ErrorDesc = sErrorDesc;
	this.ErroneousField = sErroneousField;
	this.DisplayError = function () {

	    var hErroneousLabel = window.document.getElementById('lbl' + this.ErroneousField.slice(3));
	    var hErroneousObject = window.document.getElementById(this.ErroneousField);
          			
		with (window.document.getElementById('lblErrorMessage')) {
			parentNode.className = 'ErrorMessageOn';
			innerHTML = '<IMG src="images/error_icon.gif" align="absmiddle">&nbsp;' + this.ErrorDesc;
			scrollIntoView(true);
		}

		if (hErroneousLabel != null)
		    hErroneousLabel.style.color = '#ff0000';
    		
		if (hErroneousObject != null)
		    hErroneousObject.focus();
    }
}

// Create and discard an initial Circle object.
// This forces the prototype object to be created in JavaScript 1.1.
new UserError(0, '', 0, 1);
	
//Reset Error Message in Form
function ResetErrorMessage() {
    var hErroneousLabels = window.document.getElementsByTagName('LABEL');
	var iIndex;
	
	with (window.document.getElementById('lblErrorMessage')) {
        parentNode.className = 'ErrorMessageOff';
        innerHTML = '';
    }
    	
	for (iIndex = 0; iIndex < hErroneousLabels.length; iIndex++)
		if (hErroneousLabels[iIndex].getAttribute("name") == 'FieldCaption')
			hErroneousLabels[iIndex].style.color = "#000000";
}

//Check if field is empty
function IsEmpty(sValue) {
	var bReturnInfo = false;
	
	if (CheckChars(sValue, ' \t\n\r', true) == '')
		bReturnInfo = true;
			
	return bReturnInfo;
}

//Remove a set of characters or keep nothing but those characters in a string
function CheckChars (sString, sChars, bRemove) {
    var iIndex, sCurrentChar;
    var sReturnString = '';

    if(bRemove)
	    for (iIndex = 0; iIndex < sString.length; iIndex++) {   
	        sCurrentChar = sString.charAt(iIndex);
	        if (sChars.indexOf(sCurrentChar) == -1) 
			    sReturnString += sCurrentChar;
	    }
	
	else
        for (iIndex = 0; iIndex < sString.length; iIndex++) {   
	        sCurrentChar = sString.charAt(iIndex);
	        if (sChars.indexOf(sCurrentChar) != -1) 
			    sReturnString += sCurrentChar;
	    }
	    
	return sReturnString;
}

//Check if phone number has 10 digits
function IsValidPhoneNumber(sPhoneNumber, bCanBeEmpty) {
    var sPhoneNumber;
    var expPhone = /^1?\d{10}$/
    var hUserError = new UserError(0, '', '');

	if (!bCanBeEmpty && IsEmpty(sPhoneNumber)) {
		hUserError.ErrorCode = 1;
		hUserError.ErrorDesc = 'Please enter your phone number.';
	}

	if (hUserError.ErrorCode == 0)
		if (!IsEmpty(sPhoneNumber)) {
			sPhoneNumber = CheckChars(sPhoneNumber, '0123456789', false);
			if (!expPhone.test(sPhoneNumber)) {
				hUserError.ErrorCode = 2;
				hUserError.ErrorDesc = 'Please enter a valid phone number to help us reach you.';
			}
		}                   		     
	
	return hUserError;
}

//Check if email is in the form xy9@y9.xxx where x=letter y9=letter or number
function IsValidEMailAddress(sEMailAddress, bCanBeEmpty) {
    var expEMail = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/
    //   /^[a-z][a-z_0-9\.]+@[a-z_0-9\.]+\.[a-z]{3}$/i
    var hUserError = new UserError(0, '', '');

	if (!bCanBeEmpty && IsEmpty(sEMailAddress)) {
		hUserError.ErrorCode = 1;
		hUserError.ErrorDesc = 'Please enter your e-mail address.';
	}

	if (hUserError.ErrorCode == 0)
		if (!IsEmpty(sEMailAddress) && !expEMail.test(sEMailAddress)) {
			hUserError.ErrorCode = 2;
			hUserError.ErrorDesc = 'Please enter a valid e-mail address to help us reach you.';
		}         
  
	return hUserError;
}

//Check if zip is in format 12345 or 12345-1234
function IsValidZipCode(sZipCode, bCanBeEmpty) {
var expZipCode = /(^\d{5}$)|(^\d{5}-\d{4}$)/
var hUserError = new UserError(0, '', '');

	if (!bCanBeEmpty && IsEmpty(sZipCode)) {
		hUserError.ErrorCode = 1;
		hUserError.ErrorDesc = 'Please enter your zip code.';
	}

	if (hUserError.ErrorCode == 0) {
		if (!IsEmpty(sZipCode) && !expZipCode.test(sZipCode)) {
			hUserError.ErrorCode = 2;
			hUserError.ErrorDesc = 'Please enter a valid 5 digit zip code.';
		}
	}                   
		    
	return hUserError;
}

//Put dashes into phone number
function FormatPhoneNumber(sPhoneNumber) {
var sPhoneFormat = '';

	sPhoneNumber = CheckChars(sPhoneNumber, '0123456789', false);
	if (sPhoneNumber.length == 10)
		sPhoneFormat = sPhoneNumber.substring(0,3) + '-' + sPhoneNumber.substring(3,6) + '-' + sPhoneNumber.substring(6,10);
		
	if (sPhoneNumber.length == 11)
		sPhoneFormat = sPhoneNumber.substring(0,1) + '-' + sPhoneNumber.substring(1,4) + '-' + sPhoneNumber.substring(4,7) + '-' + sPhoneNumber.substring(7,11);

	return sPhoneFormat;
}

//Check form before submitting	
function frmEstimate_onsubmit(iLeadType) {
	
	var hReturnInfo;
	var sValidateType;
	var hUserError;
	var bReturn = true;
	var dCurrentDate = new Date();
	var sCurrentDate = (dCurrentDate.getMonth() + 1) + '/' + dCurrentDate.getDate() + '/' + dCurrentDate.getFullYear();

	//Clear the error messages and the marked fields
	ResetErrorMessage(); 
    
	try {
    	
		//Check if the user entered a full name
		if (IsEmpty(document.getElementById('txtFullName').value))
			throw new UserError(1, 'Please enter your full name.', 'txtFullName');

		//Check if the user entered a valid phone number
		hUserError = IsValidPhoneNumber(document.getElementById('txtPhone').value, false); 
		if (hUserError.ErrorCode > 0) {
			hUserError.ErroneousField = 'txtPhone';
			throw hUserError;
		}

		//Format the phone number
		document.getElementById('txtPhone').value = FormatPhoneNumber(document.getElementById('txtPhone').value);

		//Check if the user entered a valid additional phone number
		hUserError = IsValidPhoneNumber(document.getElementById('txtAdditionalPhone').value, true); 
		if (hUserError.ErrorCode > 0) {
			hUserError.ErroneousField = 'txtAdditionalPhone';
			throw hUserError;
		}

		//Format the additional phone number
		document.getElementById('txtAdditionalPhone').value = FormatPhoneNumber(document.getElementById('txtAdditionalPhone').value);

		//Check if the user entered a valid e-mail address. 
		hUserError = IsValidEMailAddress(document.getElementById('txtEMail').value, false); 
		if (hUserError.ErrorCode > 0){
			hUserError.ErroneousField = 'txtEMail';
			throw hUserError;
		}
    
    	//Format the move date properly.
			document.getElementById('dateOfMove').value = FormatDate(document.getElementById('dateOfMove').value);
		
		//Check if the user entered a valid date
			hReturnInfo = CompareDates(document.getElementById('dateOfMove').value, sCurrentDate, 'GE');
		
		if (hReturnInfo.ReturnCode > 0) {
				if (hReturnInfo.ReturnCode != 9)
					throw new UserError(hReturnInfo.ReturnCode, hReturnInfo.ErrorDescription ,'aaadateOfMove');
				else
					throw new UserError(hReturnInfo.ReturnCode, 'Please enter a future date.' ,'aaadateOfMove');
			}
			
		/*	    
		//Check if the user selected a moving date
		if (document.getElementById('cboMonth').value == 0)
			throw new UserError(1, 'Please enter the date you\'d like to move', 'cboMonth');

		if (document.getElementById('cboDay').value == 0)
			throw new UserError(1, 'Please enter the date you\'d like to move', 'cboDay');

		if (document.getElementById('cboYear').value == 0)
			throw new UserError(1, 'Please enter the date you\'d like to move', 'cboYear');
		*/

		//Check if the user selected a move size
		if (document.getElementById('cboMoveSize').selectedIndex == 0) 
			throw new UserError(1, 'Please select the approximate size of your move.', 'cboMoveSize');

		//If storage (different error messages)
                if(iLeadType==6){

			//Check if the user entered a valid from zip code (except when lead is International) 
			hUserError = IsValidZipCode(document.getElementById('txtFromZipCode').value, false); 
			if (hUserError.ErrorCode > 0) {
				hUserError.ErroneousField = 'txtFromZipCode';
				throw hUserError;
			}

			//Check if the user has entered the from city
			if (IsEmpty(document.getElementById('txtFromCity').value))
				throw new UserError(1, 'Please enter the city you would like to store in.', 'txtFromCity');

        		//Check if the user selected the from state (except when the lead is Local or International)
			if (document.getElementById('cboFromState').selectedIndex == 0)
		                throw new UserError(1, 'Please select the state you would like to store in.', 'cboFromState');

		}else{

			//Check if the user entered a valid from zip code (except when lead is International) 
			if(iLeadType!=5){
				hUserError = IsValidZipCode(document.getElementById('txtFromZipCode').value, false); 
				if (hUserError.ErrorCode > 0) {
					hUserError.ErroneousField = 'txtFromZipCode';
					throw hUserError;
				}
        		}

			//Check if the user has entered the from city
			if (IsEmpty(document.getElementById('txtFromCity').value))
				throw new UserError(1, 'Please enter the city you\'re moving from.', 'txtFromCity');

	        	//Check if the user selected the from state (except when the lead is Local or International)
			if((iLeadType!=1)&&(iLeadType!=5)) {
				if (document.getElementById('cboFromState').selectedIndex == 0)
			                throw new UserError(1, 'Please select the state you\'re moving from.', 'cboFromState');
			}

			//Check if the user entered the from country (only if the lead is International)
			if(iLeadType==5) {
				if(IsEmpty(document.getElementById('txtFromCountry').value))
			                throw new UserError(1, 'Please enter the country you\'re moving from', 'txtFromCountry');
			}

			//Check if the user has entered the to city
			if (IsEmpty(document.getElementById('txtToCity').value))
				throw new UserError(1, 'Please enter the city you\'re moving to.', 'txtToCity');

			//Check if the user selected the to state (except when the lead is Local or International)
			if((iLeadType !=1)&&(iLeadType!=5)) {
				if (document.getElementById('cboToState').selectedIndex == 0)
					throw new UserError(1, 'Please select the state you\'re moving to.', 'cboToState');
			}

		        //Check if the user entered the to country (only if the lead is International)
			if(iLeadType==5) {
				if(IsEmpty(document.getElementById('txtToCountry').value))
					throw new UserError(1, 'Please enter the country you\'re moving to', 'txtTOCountry');
			}
		}
	}

	//If an exception was raised an error message will be displayed
	catch (hUserError) {
		hUserError.DisplayError();
		bReturn = false;
	}

	//If no error was found then submit form
	return bReturn;
	//alert(sSelected);
	
}