//--Form Validation

function doFocus() {
     document.forms["localcalc"].first.focus();
}

function notEmpty(elemvalue) {
     if (elemvalue=="") {
         return false;
     }
     return true;
}

function isSelected(selIndx) {
     if (selIndx==0) {
         return false;
     }
     return true;
}

function validPhone(phone) {
	phoneReg = "^(?:[\(][0-9]{3}[\)]|[0-9]{3})[-. ]?[0-9]{3}[-. ]?[0-9]{4}$";
	var regex = new RegExp(phoneReg);
	if (!regex.test(phone)) {
		return false;
	}
     return true;
}

function validEmail(email) {
     invalidChars=" /:,;";
     if (email=="") {
         return false;
     }
     for (i=0; i<invalidChars.length; i++) {
           badChar=invalidChars.charAt(i);
           if (email.indexOf(badChar, 0) !=-1) {
               return false;
           }
      }
      atPos=email.indexOf("@", 1);
      if (atPos==-1) {
          return false;
      }
      if (email.indexOf("@", atPos+1) !=-1) {
          return false;
      }
      periodPos=email.indexOf(".", atPos);
      if (periodPos==-1) {
          return false;
      }
      if (periodPos+3>email.length) {
          return false;
      }
     return true;
}

function validZip(zip) {
	zipReg = "^[0-9]{5}(|[-]?[0-9]{4})$";
	var regex = new RegExp(zipReg);
	if (!regex.test(zip)) {
		return false;
	}
	return true;
}

function isSelectedPair(selIndx1,selIndx2) {
     if ((selIndx1==0) && (selIndx2==0)) {
         return false;
     }
     return true;
}

/*
function validFirst(first) {
     if (first=="") {
         return false;
     }
     return true;
}
function validLast(last) {
     if (last=="") {
         return false;
     }
     return true;
}

function validDayPhone(dayphone) {
     if (dayphone=="") {
         return false;
     }
     return true;
}

function validEvePhone(evephone) {
     if (evephone=="") {
         return false;
     }
     return true;
}

function validZip(zip) {
     if (zip=="") {
         return false;
     }
     return true;
}

function validCountry(country) {
     if (country=="") {
         return false;
     }
     return true;
}
*/

function checkLocalCalc(form) {

     if (!notEmpty(form.first.value)) {
         alert("Please enter your first name...");
         form.first.focus();
         form.first.select();
         return false;
     }
     if (!notEmpty(form.last.value)) {
         alert("Please enter your last name...");
         form.last.focus();
         form.last.select();
         return false;
     }
     if (!validPhone(form.dayphone.value)) {
         alert("Please enter your day phone number...");
         form.dayphone.focus();
         form.dayphone.select();
         return false;
     }
     if (!validPhone(form.evephone.value)) {
         alert("Please enter your evening phone number...");
         form.evephone.focus();
         form.evephone.select();
         return false;
     }
     if (!validEmail(form.email.value)) {
         alert("Please enter a valid email address...");
         form.email.focus();
         form.email.select();
         return false;
     }
     if (!notEmpty(form.street.value)) {
         alert("Please enter your street address...");
         form.street.focus();
         form.street.select();
         return false;
     }
     if (!notEmpty(form.city.value)) {
         alert("Please enter your city...");
         form.city.focus();
         form.city.select();
         return false;
     }
	 if (!isSelected(form.state.selectedIndex)) {
         alert("Please select your state...");
         form.state.focus();
         return false;
     }
     if (!validZip(form.zip.value)) {
         alert("Please enter your zip code...");
         form.zip.focus();
         form.zip.select();
         return false;
     }
	 var totalItems = parseInt(document.forms["localcalc"].total_group1.value) + parseInt(document.forms["localcalc"].total_group2.value) + parseInt(document.forms["localcalc"].total_group3.value) + parseInt(document.forms["localcalc"].total_group4.value) + parseInt(document.forms["localcalc"].total_group5.value) + parseInt(document.forms["localcalc"].total_group6.value);
	 if (totalItems < 1) {
		 alert("Please select at least one item in any of the groups...");
         form.small_boxes.focus();
         form.small_boxes.select();
		 return false;
	 }
	 if (!isSelectedPair(form.out_of_house_details.selectedIndex,form.out_of_appart_details.selectedIndex)) {
         alert("Please specify what you are moving out of...");
         if (form.out_of_house_details.disabled) {
             form.out_of_appart_details.focus();
         } else {
             form.out_of_house_details.focus();
         }
         return false;
     }
	 if (!isSelectedPair(form.into_house_details.selectedIndex,form.into_appart_details.selectedIndex)) {
         alert("Please specify what you are moving into...");
         if (form.into_house_details.disabled) {
             form.into_appart_details.focus();
         } else {
             form.into_house_details.focus();
         }
         return false;
     }
/*	 if (!isSelected(form.out_city.selectedIndex)) {
         alert("Please select the city you are moving from...");
         form.out_city.focus();
         return false;
     }*/
     if (!notEmpty(form.out_city.value)) {
         alert("Please enter the city you are moving from...");
         form.out_city.focus();
         form.out_city.select();
         return false;
     }
     if (!validZip(form.out_zip.value)) {
         alert("Please enter the zip code of the city you are moving from...");
         form.out_zip.focus();
         form.out_zip.select();
         return false;
     }
/*	 if (!isSelected(form.in_city.selectedIndex)) {
         alert("Please select the city you are moving to...");
         form.in_city.focus();
         return false;
     }*/
     if (!notEmpty(form.in_city.value)) {
         alert("Please enter the city you are moving to...");
         form.in_city.focus();
         form.in_city.select();
         return false;
     }
     if (!validZip(form.in_zip.value)) {
         alert("Please enter the zip code of the city you are moving to...");
         form.in_zip.focus();
         form.in_zip.select();
         return false;
     }
   return true;
}

function checkInterCalc(form) {

     if (!notEmpty(form.first.value)) {
         alert("Please enter your first name...");
         form.first.focus();
         form.first.select();
         return false;
     }
     if (!notEmpty(form.last.value)) {
         alert("Please enter your last name...");
         form.last.focus();
         form.last.select();
         return false;
     }
     if (!validPhone(form.dayphone.value)) {
         alert("Please enter your day phone number...");
         form.dayphone.focus();
         form.dayphone.select();
         return false;
     }
     if (!validPhone(form.evephone.value)) {
         alert("Please enter your evening phone number...");
         form.evephone.focus();
         form.evephone.select();
         return false;
     }
     if (!validEmail(form.email.value)) {
         alert("Please enter a valid email address...");
         form.email.focus();
         form.email.select();
         return false;
     }
     if (!notEmpty(form.street.value)) {
         alert("Please enter your street address...");
         form.street.focus();
         form.street.select();
         return false;
     }
     if (!notEmpty(form.city.value)) {
         alert("Please enter your city...");
         form.city.focus();
         form.city.select();
         return false;
     }
	 if (!isSelected(form.state.selectedIndex)) {
         alert("Please select your state...");
         form.state.focus();
         return false;
     }
     if (!validZip(form.zip.value)) {
         alert("Please enter your zip code...");
         form.zip.focus();
         form.zip.select();
         return false;
     }
	 var totalItems = parseInt(document.forms["localcalc"].total_group_DiningRoom.value) + parseInt(document.forms["localcalc"].total_group_Bedroom.value) + parseInt(document.forms["localcalc"].total_group_Kitchen.value) + parseInt(document.forms["localcalc"].total_group_LivingRoom.value) + parseInt(document.forms["localcalc"].total_group_Miscellaneous.value) + parseInt(document.forms["localcalc"].total_group_PorchOutdoor.value)
parseInt(document.forms["localcalc"].total_group_Nursery.value) + 
parseInt(document.forms["localcalc"].total_group_Boxes.value) + 
parseInt(document.forms["localcalc"].total_group_Appliances.value);
	 if (totalItems < 1) {
		 alert("Please select at least one item in any of the categories...");
         form.benches_harvest.focus();
         form.benches_harvest.select();
		 return false;
	 }
     var boxPack = -1;
         for (i=0; i<document.forms["localcalc"].boxes_packaging.length; i++) {
	     if (document.forms["localcalc"].boxes_packaging[i].checked) {
             boxPack = i;
	    }
     }
	 if (boxPack == -1) {
        alert("Are you packing boxes yourself or want us to do it for you?..");
        return false;
    }
     if (!notEmpty(form.interstate_out_city.value)) {
         alert("Please select the city you are moving from...");
         form.interstate_out_city.focus();
         form.interstate_out_city.select();
         return false;
     }
	 if (!isSelected(form.interstate_out_state.selectedIndex)) {
         alert("Please select the state you are moving from...");
         form.interstate_out_state.focus();
         return false;
     }
     if (!validZip(form.interstate_out_zip.value)) {
         alert("Please enter the zip code of the city you are moving from...");
         form.interstate_out_zip.focus();
         form.interstate_out_zip.select();
         return false;
     }
     if ((!notEmpty(form.interstate_in_city.value)) && (!notEmpty(form.international_in_city.value))) {
         alert("Please select the city in or outside the USA you are moving to...");
         form.interstate_in_city.focus();
         form.interstate_in_city.select();
         return false;
     }
	 if ((!isSelected(form.interstate_in_state.selectedIndex)) && (!isSelected(form.international_in_country.selectedIndex))) {
         alert("Please select the state in the USA or the country you are moving to...");
         form.interstate_in_state.focus();
         return false;
     }
	 if (!isSelected(form.type_of_inter_move.selectedIndex)) {
         alert("Please indicate the type of your move...");
         form.type_of_inter_move.focus();
         return false;
     }
   interTotal();	 
   return true;
}
////items groups calculator validation//////////////////////////

function calculateGroup(inp,grp) {
	if (!isPositiveInt(inp)) {
		showError(inp);
		return;
	}
	hideError(inp);
	var total_group_value = 0;
	var newValue = 0;
	var inpName = "group" + grp + "_";
	for (i=0; i<document.forms["localcalc"].elements.length; i++) {
		if (document.forms["localcalc"].elements[i].id.indexOf(inpName) != -1) {
			newValue = (document.forms["localcalc"].elements[i].value == "") ? 0 : document.forms["localcalc"].elements[i].value;
			total_group_value = total_group_value + parseInt(newValue);
		}
	}
	//alert(inpName);
	var TG = document.forms["localcalc"].elements["total_group"+grp];
	TG.value = total_group_value;
}

////items groups calculator validation//////////////////////////

function calculateInter(inp,grp) {
	if (!isPositiveInt(inp)) {
		showError(inp);
		return;
	}
	hideError(inp);
	var total_group_value = 0;
	var total_group_weight = 0;
	var newValue = 0;
	var newWeight = 0;
	var inpName = grp + "_";
	for (i=0; i<document.forms["localcalc"].elements.length; i++) {
		if (document.forms["localcalc"].elements[i].id.indexOf(inpName) != -1) {
			var iElement = document.forms["localcalc"].elements[i];
			newValue = (iElement.value == "") ? 0 : iElement.value;
			newWeight = (iElement.value == "") ? 0 : iElement.value * iElement.alt;
			total_group_value = total_group_value + parseInt(newValue);
			total_group_weight = total_group_weight + parseInt(newWeight);
		}
	}
	//alert(inpName);
	var TG = document.forms["localcalc"].elements["total_group_"+grp];
	var WG = document.forms["localcalc"].elements["weight_"+grp];
	TG.value = total_group_value;
	WG.value = total_group_weight;
}

function interTotal() {
	var total_weight = 0;
	var newValue = 0;
	for (i=0; i<document.forms["localcalc"].elements.length; i++) {
		if (document.forms["localcalc"].elements[i].name.indexOf("weight_") != -1) {
			var iElement = document.forms["localcalc"].elements[i];
			newValue = (iElement.value == "") ? 0 : iElement.value;
			total_weight = total_weight + parseInt(newValue);
		}
	}
	var TW = document.forms["localcalc"].elements["total_weight"];
	TW.value = total_weight * 7;

/*	TW.value = (document.forms["localcalc"].elements["weight_DiningRoom"].value + document.forms["localcalc"].elements["weight_Bedroom"].value + document.forms["localcalc"].elements["weight_Kitchen"].value + document.forms["localcalc"].elements["weight_LivingRoom"].value + document.forms["localcalc"].elements["weight_Miscellaneous"].value + document.forms["localcalc"].elements["weight_PorchOutdoor"].value + document.forms["localcalc"].elements["weight_Nursery"].value + document.forms["localcalc"].elements["weight_Boxes"].value + document.forms["localcalc"].elements["weight_Appliances"].value) *7;
*/

}	
	
function isPositiveInt(formElem) {
	if ( (isNaN(formElem.value)) || (formElem.value.indexOf(".")!=-1) || (formElem.value.indexOf("-")!=-1) || (formElem.value.indexOf(" ")!=-1) ) {
		return false;
	} else {
		return true;
	}
}

function showError(formElem) {
	submitError = true;
	formElem.style.backgroundColor = "#FFCCCC";
	alert("Please enter a valid positive number...");
	formElem.value = "";
}
function hideError(formElem) {
	submitError = false;
	formElem.style.backgroundColor = "#FFFFFF";
}
//===================================

function disableType(formElem,typeName) {
	var radioValue = "house_" + typeName;
	if (typeName == "out") {
	
		if (formElem.value == radioValue) {		// house
			document.forms["localcalc"].out_of_house_details.disabled = false;
			document.forms["localcalc"].out_of_appart_details.disabled = true;
			document.forms["localcalc"].out_of_appart_details.selectedIndex = 0;
			document.forms["localcalc"].load_thru_garage.disabled = true;
			document.forms["localcalc"].load_thru_garage.checked = false;
		} else {								// apt
			document.forms["localcalc"].out_of_house_details.disabled = true;
			document.forms["localcalc"].out_of_house_details.selectedIndex = 0;
			document.forms["localcalc"].out_of_appart_details.disabled = false;
			document.forms["localcalc"].load_thru_garage.disabled = false;
		}
		
	} else {
	
		if (formElem.value == radioValue) {		// house
			document.forms["localcalc"].into_house_details.disabled = false;
			document.forms["localcalc"].into_appart_details.disabled = true;
			document.forms["localcalc"].into_appart_details.selectedIndex = 0;
			document.forms["localcalc"].unload_thru_garage.disabled = true;
			document.forms["localcalc"].unload_thru_garage.checked = false;
		} else {								// apt
			document.forms["localcalc"].into_house_details.disabled = true;
			document.forms["localcalc"].into_house_details.selectedIndex = 0;
			document.forms["localcalc"].into_appart_details.disabled = false;
			document.forms["localcalc"].unload_thru_garage.disabled = false;
		}
	}
	
}

/*
function isEmpty(formElem) {
	     if (formElem.value == "") {
		     return true;
	     } else {
		     return false;
	     }
}
function isNumber(formElem) {
	if ((isEmpty(formElem)) || (isNaN(formElem.value))) {
		return false;
	} else {
		return true;
	}
}
*/
// -->

