// JavaScript Document
//function does client side validation of main_form
function validate_mini_form(theForm) {

	if (theForm.move_type_id.selectedIndex == "") {
		alert("Please choose a type of move!");
		theForm.move_type_id.focus();
		return false;
	}

	if (theForm.move_size_id.selectedIndex == "") {
		alert("Please choose a size of move!");
		theForm.move_size_id.focus();
		return false;
	}

	if (theForm.move_size_id.selectedIndex == "7") {
		if (!v_minLength(theForm, 'CarMake', 2, 'Car Make')) {
			return false;
		}
		if (!v_minLength(theForm, 'CarModel', 2, 'Car Model')) {
			return false;
		}
	} 

	// MOVE FROM ////////////////////////////
	if (theForm.move_from_country.selectedIndex == "" || theForm.move_from_country.selectedIndex == "1") {
		if (!v_minZip(theForm, 'move_from_zip', 5, 'Moving FROM ZIP')) {
			return false;
		}
	} else {
		if (theForm.move_from_country.selectedIndex == "") {
			alert("Please choose a Move FROM Country!");
			theForm.move_from_country.focus();
			return false;
		}
	}

	// MOVE TO //////////////////////////////
	if (theForm.move_to_country.selectedIndex == "" || theForm.move_to_country.selectedIndex == "1") {
		if (!v_minZip(theForm, 'move_to_zip', 5, 'Moving TO ZIP')) {
			return false;
		}
	} else {
		if (theForm.move_to_country.selectedIndex == "") {
			alert("Please choose a Move TO Country!");
			theForm.move_to_country.focus();
			return false;
		}
	}
	
	if (!v_minLength(theForm, 'first_name', 2, 'First Name')) {
		return false;
	}
	
	if (!v_minLength(theForm, 'last_name', 2, 'Last Name')) {
		return false;
	}

	if (!emailCheck(theForm.email_1.value)) {
		theForm.email_1.focus();
		return false;
	}
	
	if (!v_minLength(theForm, 'phone_1', 10, 'Phone')) {
		return false;
	}

	return true;
}



//validates the select companies form
//at least one must be checked
//In future may set a limit
function validate_select_companies(theForm) {
	var bolChecked = false;   
	for (var i = 0; i < theForm.company_ids.length; i++) {      
		if (theForm.company_ids[i].checked) {   
			//alert(theForm.company_ids[i].value + "!");      
			bolChecked = true;  
		}   
	}
	if (!bolChecked) {
		alert("Please select at least one of the companies!");
		return false;
	}
		
	return true;
}

//validates form that pops up when click subscribe
function validate_list_sign_up(theForm) {
	//validate first_name
	if (!v_minLength(theForm, 'first_name', 2, 'First Name')) {
		return false;
	}
	//validate last_name
	if (!v_minLength(theForm, 'last_name', 2, 'Last Name')) {
		return false;
	}
	//validate email_1
	if (!emailCheck(theForm.email_1.value)) {
		theForm.email_1.focus();
		return false;
	}
}


function validate_list_sign_up(theForm) {

	//validate first_name
	if (!v_minLength(theForm, 'first_name', 2, 'First Name')) {
		return false;
	}
	//validate last_name
	if (!v_minLength(theForm, 'last_name', 2, 'Last Name')) {
		return false;
	}

	//validate email
	if (!emailCheck(theForm.email_1.value)) {
		theForm.email_1.focus();
		return false;
	}
	
}

//validate form that pops up when you click email this a friend
function validate_email_to_a_friend(theForm) {

	//validate sender_email
	if (!emailCheck(theForm.sender_email.value)) {
		theForm.sender_email.focus();
		return false;
	}
	
	//validate friend_email
	if (!emailCheck(theForm.friend_email.value)) {
		theForm.friend_email.focus();
		return false;
	}
}

//validates form on companies.asp page
//not requiring too many fields right now
function validate_company_form(theForm) {

	//validate company_name
	if (!v_minLength(theForm, 'company_name', 2, 'Company Name')) {
		return false;
	}
	
	
	//check if previously on trial, then make PAYING?
	var radiogroup = theForm.trial; //el[el[i].name]; get the whole set of radio buttons.
	var itemchecked = false;
	for(var j = 0 ; j < radiogroup.length ; ++j) {
	// TRIAL
	if(radiogroup[0].checked) {
				//Wanting to change from PAYING ==> TRIAL
				if (theForm.trial_prev.value == "0") {
					var doyou = confirm("Change PAYING to Trial?"); //Your question.
					if (doyou == false){
						alert("Please make sure monthly fees are entered"); //If your question is answered No.
						radiogroup[1].checked = true;
						return false;
					}	
				}
			itemchecked = true;
			break;
			}
	// PAYING
	if(radiogroup[1].checked) {
				// Do not validate CHILD Companies
				if (theForm.parent_company.value == " " || theForm.parent_company.value == "" || theForm.parent_company.value == "0") {
					//check if previously on trial, then make PAYING?
					if (theForm.month1.value == "0" || theForm.month1.value == "" || theForm.month1.value == "0.00") {
						alert("For a PAYING Account, you must enter Monthly Fees!");
						theForm.month1.focus;
						return false;
					}
					if (theForm.month2.value == "0" || theForm.month2.value == "" || theForm.month2.value == "0.00") {
						alert("For a PAYING Account, you must enter Monthly Fees!");
						theForm.month2.focus;
						return false;
					}
					if (theForm.month3.value == "0" || theForm.month3.value == "" || theForm.month3.value == "0.00") {
						alert("For a PAYING Account, you must enter Monthly Fees!");
						theForm.month3.focus;
						return false;
					}
					if (theForm.monthly_fee.value == "0" || theForm.monthly_fee.value == "" || theForm.monthly_fee.value == "0.00") {
						alert("For a PAYING Account, you must enter Monthly Fees!");
						theForm.monthly_fee.focus;
						return false;
					}
					//Wanting to change from TRIAL ==> PAYING
					if (theForm.trial_prev.value == "1") {
						var doyou2 = confirm("You are changing this account to PAYING status.\n\nPress OK to proceed.\nPress Cancel to return to TRIAL status."); //Your question.
						if (doyou2 == false){
							alert("Please reset monthly fees to 0"); //If your question is answered No.
							radiogroup[0].checked = true;
							return false;
						}		
					}
				} else {
					if (theForm.month1.value != "0" || theForm.month2.value != "0" || theForm.month3.value != "0" || theForm.monthly_fee.value != "0") {
						alert("Monthly fees cannot be set on a CHILD company.\n\nResetting values to 0");
					}
					theForm.month1.value = "0"; theForm.month2.value = "0"; theForm.month3.value = "0"; theForm.monthly_fee.value = "0";
				}
			itemchecked = true;
			break;
			}
		}
		// NOTHING Checked
		if(!itemchecked) { 
			alert("You must choose TRIAL or PAYING");
			theForm.trial.focus();
			return false;
		}

		
	//validate company_name
	if (!v_minLength(theForm, 'zip', 5, 'Zip code')) {
		return false;
	}
	
	//validate email_1
	if (!emailCheck(theForm.email_1.value)) {
		theForm.email_1.focus();
		return false;
	}
	
	//validate phone 1 -- in three parts
	if (!v_minLength(theForm, 'area_code_1', 3, 'Area Code')) {
		return false;
	}
	
	if (!v_minLength(theForm, 'phone_3_1', 3, 'Phone')) {
		return false;
	}
	
	if (!v_minLength(theForm, 'phone_4_1', 4, 'Phone')) {
		return false;
	}
	
	//validate company_name
	if (!v_minLength(theForm, 'first_name_1', 2, 'Contact First Name')) {
		return false;
	}
	
	//validate company_name
	if (!v_minLength(theForm, 'last_name_1', 2, 'Contact Last Name')) {
		return false;
	}

	if (theForm.sid_from.value == "") {
		alert("Please enter Moving FROM States or Area Codes!");
//		theForm.sid_from.focus();
		return false;
	}

}

//reusable function for text inputs
//send form, name of element to be checked, minimum length
//and the name of the field you want to display to the user
function v_minLength(theForm, fieldname, minLength, name) {
	var elementFocus = "theForm." + fieldname + ".focus()";
	var elementLength = "theForm." + fieldname + ".value.length";
	//validate length
	if (eval(elementLength) < minLength) {
		alert("Please make a valid entry in the " + name + " field!");
		eval(elementFocus);
		return false;
	} else {
		return true;
	}
}

function v_minZip(theForm, fieldname, minLength, name) {
	var elementFocus = "theForm." + fieldname + ".focus()";
	var elementLength = "theForm." + fieldname + ".value.length";
	//validate length
	if (eval(elementLength) < minLength) {
		alert("Please use the Zip Code locator\nfor help with a correct " + name + "!");
		eval(elementFocus);
		return false;
	} else {
		return true;
	}
}

//reusable function for listboxes
//send form, name of element to be checked, and the name of the field you want to display to the user
//It checks to make sure at least one has been selected
function v_select(theForm, fieldname, name) {
	var elementFocus = "theForm." + fieldname + ".focus()";
	var elementSelectedIndex = "theForm." + fieldname + ".selectedIndex";
	if (eval(elementSelectedIndex) == "") {
		alert("Please make a selection in the " + name + " field!");
		eval(elementFocus);
		return false;
	} else {
		return true;
	}
}

//validates the form on request.asp
function validate_send_request(theForm) {
	
	var submitcount=0;
	
	// Validate number of bedrooms
	if (theForm.room_num.selectedIndex == "") {
		alert("Please choose a size of move!");
		theForm.room_num.focus();
		return false;
	}
	
	//validate move_from_zip
	if (!v_minZip(theForm, 'move_from_zip', 5, 'Moving From ZIP')) {
		return false;
	}
	
	//validate move_from_zip
	if (!v_minZip(theForm, 'move_to_zip', 5, 'Moving To ZIP')) {
		return false;
	}

	//validate first_name
	if (!v_minLength(theForm, 'first_name', 2, 'First Name')) {
		return false;
	}
	
	//validate last_name
	if (!v_minLength(theForm, 'last_name', 2, 'Last Name')) {
		return false;
	}
	
	//validate email_1
	if (!emailCheck(theForm.email_1.value)) {
		theForm.email_1.focus();
		return false;
	}
	
	//validate phone 1 -- in three parts
	if (!v_minLength(theForm, 'area_code_1', 3, 'Area Code')) {
		return false;
	}
	
	if (!v_minLength(theForm, 'phone_3_1', 3, 'Phone')) {
		return false;
	}
	
	if (!v_minLength(theForm, 'phone_4_1', 4, 'Phone')) {
		return false;
	}
	
//	if (theForm.item_list.value==""){
//		var doyou = confirm("\nRECOMMENDED!\n\nPlease fill out the ITEM LIST for an ACCURATE ESTIMATE\n\nIf not, each moving company may ask you to complete a full Item List at a later time \n\n\nClick OK to Continue WITHOUT AN ITEM LIST   \n\nClick CANCEL to return and fill out the Item List\n\n\n" ); //Your question.
//				if (doyou == false){
//				return false;
//			}		
//	}

	//Hide SUBMIT button
	document.all("SubmitLayer").style.visibility = "hidden";
	document.all("SubmitLayer_sm").style.visibility = "hidden";
		
	}

//validates the INTERNATIONAL form on request.asp
function validate_intl_request(theForm) {
	
	var submitcount=0;

	// Validate number of bedrooms
	if (theForm.room_num.selectedIndex == "") {
		alert("Please choose a size of move!");
		theForm.room_num.focus();
		return false;
	}
	
	//validate move_from_city
	if (!v_minLength(theForm, 'move_from_city', 3, 'Moving From City')) {
		return false;
	}
	
	//validate move_from_country
	if (!v_select(theForm, 'move_from_country', 'Moving From Country')) {
		return false;
	}
	
	//validate move_from_city
	if (!v_minLength(theForm, 'move_to_city', 3, 'Moving To City')) {
		return false;
	}
	
	//validate move_from_country
	if (!v_select(theForm, 'move_to_country', 'Moving To Country')) {
		return false;
	}
	
	//validate first_name
	if (!v_minLength(theForm, 'first_name', 2, 'First Name')) {
		return false;
	}
	
	//validate last_name
	if (!v_minLength(theForm, 'last_name', 2, 'Last Name')) {
		return false;
	}
	
	//validate email_1
	if (!emailCheck(theForm.email_1.value)) {
		theForm.email_1.focus();
		return false;
	}
	
	//validate phone (INTERNATIONAL)
	if (!v_minLength(theForm, 'phone_1', 10, 'Phone')) {
		return false;
	}

		
//	if (theForm.item_list.value==""){
//		var doyou = confirm("\nRECOMMENDED!\n\nPlease fill out the ITEM LIST for an ACCURATE ESTIMATE\n\nIf not, each moving company may ask you to complete a full Item List at a later time \n\n\nClick OK to Continue WITHOUT AN ITEM LIST   \n\nClick CANCEL to return and fill out the Item List\n\n\n" ); //Your question.
//			if (doyou == false){
//			return false;
//		}		
//	}

	//Hide SUBMIT button
	document.all("SubmitLayer").style.visibility = "hidden";
	document.all("SubmitLayer_sm").style.visibility = "hidden";

	}


function give_focus(e) {
		var element = "document.move_form." + e + ".focus()";
		eval(element);
}

//function called for email verification
function emailCheck (emailStr) {
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


/* Finally, let's start trying to figure out if the supplied address is
   valid. */
if (!emailStr) {
	alert("Please enter your email address!");
	return false;
}
/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	alert("Email address seems incorrect (check @ and .'s)")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    alert("The username doesn't seem to be valid.")
    return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("Destination IP address is invalid!")
		return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("The domain name doesn't seem to be valid.")
    return false
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>4) {
   // the address must end in a two letter or four letter word.
   alert("The address must end in a four-letter domain, or two letter country.")
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This address is missing a hostname!"
   alert(errStr)
   return false
}

// If we've gotten this far, everything's valid!
return true;
}