// ******************************************************************
// make sure a collection of checkbox is selected
// ******************************************************************
	function ValidateCheckBoxCollection(checkbox_group,min,max)
	{
	   var elementsObject = document.getElementsByTagName("input")
       var checkBoxCheckedCounter=0;
	   for(var i=0;i<elementsObject.length  ;i++) //searching all elements in page
	   {
			if (elementsObject[i].checkbox_group==checkbox_group)
			{
				if (elementsObject[i].checked==true)
					checkBoxCheckedCounter++;			
			} 
	   }
		if (checkBoxCheckedCounter>max || checkBoxCheckedCounter<min)
			return 1;
		else
			return 0;	
	}
	
	
function checkdate(inputTest)
{
	var returnval = false;
	var validformat=/^\d{1,2}\/\d{1,2}\/\d{4}$/ //Basic check for format validity
	
	if (validformat.test(inputTest))
	{
		var monthfield=inputTest.split("/")[0];
		var dayfield=inputTest.split("/")[1];
		var yearfield=inputTest.split("/")[2];
			
		var dayobj = new Date(yearfield, monthfield-1, dayfield);
		
		if (dayobj.getDate() == dayfield && (dayobj.getMonth() + 1) == monthfield && dayobj.getFullYear() == yearfield)
		{
			returnval = true;
		}
	}
	
	return returnval
}

function checkValidity()
{
	var returnValue = true;
	if (document.Formi.FromState.value == 0)
	{
		alert("From state must be selected");
		document.Formi.FromState.focus();
		returnValue = false;
	}
	else if (document.Formi.ToState.value == 0)
	{
		alert("To state must be selected");
		document.Formi.ToState.focus();
		returnValue = false;
	}
	else if (document.Formi.Datetext.value == "")
	{
		alert("Please select your moving date");
		document.Formi.Datetext.focus();
		returnValue = false;
	}
	else if (!checkdate(document.Formi.Datetext.value))
	{
		alert("Moving Date format is: MM/DD/YYYY");
		document.Formi.Datetext.focus();
		returnValue = false;
	}
	
	if (returnValue)
	{
		returnValue = ScanMandetoryFields('Formi', '#');
	}
	
	return returnValue;
	//return ScanMandetoryFields('Formi','#');
}
function checkValidityAll()
{
	var returnValue = true;

	if (document.Formi.FromState.value == 0)
	{
		alert("From state must be selected");
		document.Formi.FromState.focus();
		returnValue = false;
	}
	else if (document.Formi.FromZip.value == "")
	{
		alert("From Zip Code must be entered");
		document.Formi.FromZip.focus();
		returnValue = false;
	}
	else if (document.Formi.ToState.value == 0)
	{
		alert("Please select moving to state");
		document.Formi.ToState.focus();
		returnValue = false;
	}
	else if (document.Formi.FName.value == "")
	{
		alert("Please insert your name");
		document.Formi.FName.focus();
		returnValue = false;
	}
	else if (document.Formi.Email.value == "")
	{
		alert("Please insert email");
		document.Formi.Email.focus();
		returnValue = false;
	}
	else if (document.Formi.Phone_Day_Pre.value == "" || document.Formi.Phone_Day.value == "")
	{
		alert("Please insert your phone");
		document.Formi.Phone_Day_Pre.focus();
		returnValue = false;
	}
	else if (document.Formi.Datetext.value == "")
	{
		alert("Please select your moving date");
		document.Formi.Datetext.focus();
		returnValue = false;
	}
	else if (!checkdate(document.Formi.Datetext.value))
	{
		alert("Moving Date format is: MM/DD/YYYY");
		document.Formi.Datetext.focus();
		returnValue = false;
	}
	else if (document.Formi.MovingSize.value == 0)
	{
		alert("Please select moving size");
		document.Formi.MovingSize.focus();
		returnValue = false;
	}
	
	if (returnValue)
	{
		returnValue = ScanMandetoryFields('Formi', '#');
	}
	
	return returnValue;
	//return ScanMandetoryFields('Formi','#');
}

function checkValidityForm2()
{
	var returnValue = true;
	
	
	if (document.Formi.FromZip.value == "")
	{
		alert("Please insert from zip");
		document.Formi.FromZip.focus();
		returnValue = false;
	}
	else if (document.Formi.ToCity.value == "")
	{
		alert("Please insert to city");
		document.Formi.ToCity.focus();
		returnValue = false;
	}
	else if (document.Formi.FName.value == "")
	{
		alert("Please insert your name");
		document.Formi.FName.focus();
		returnValue = false;
	}
	else if (document.Formi.Email.value == "")
	{
		alert("Please insert email");
		document.Formi.Email.focus();
		returnValue = false;
	}
	else if (document.Formi.Phone_Day_Pre.value == "" || document.Formi.Phone_Day.value == "")
	{
		alert("Please insert your phone");
		document.Formi.Phone_Day_Pre.focus();
		returnValue = false;
	}
	else if (document.Formi.MovingSize.value == 0)
	{
		alert("Please select moving size");
		document.Formi.MovingSize.focus();
		returnValue = false;
	}
	
	if (returnValue)
	{
		returnValue = ScanMandetoryFields('Formi', '#');
	}
	
	return returnValue;
}
// ******************************************************************
// IsNotValidString: returns true if string is not consist of only the 
//				     given chars.
// ******************************************************************
function IsNotValidString(str,ValidChars)
{
	var flag_ok;
	flag_ok=false
	for (i=0;i<str.length;i++)
	{	
		if (ValidChars.indexOf(str.charAt(i))==-1)
		{
			flag_ok=true;
			return flag_ok;			
		}				
	}
	return flag_ok;
}

	// ******************************************************************
	// ScanMandetoryFields: scan all mandetory fields to see they have
	//					    value. incase they don't it warns user.
	// ******************************************************************
	function ScanMandetoryFields(formName,goToURL)
	{
	   var isFirstField = 0;
	   var isLocalError;
	   
	   var elementsObject = document.getElementsByTagName("select")
	   for(var i=0;i<elementsObject.length  ;i++) //searching all elements in page
	   {
			isLocalError = 0;
         // ***************************
         // check if field is mandetory
         // ***************************
         if (elementsObject[i].isMandetory=='1')
			if (elementsObject[i][elementsObject[i].selectedIndex].value=='-1') //includes triming
			{
				// ============================
				// no value, do mark this field
				// ============================
				if (elementsObject[i].ErrorStyle!='')
					elementsObject[i].className=elementsObject[i].ErrorStyle;
				else
					elementsObject[i].className="nameError";
				isLocalError=1;
				if (isFirstField==0)
				{
					isFirstField=1;
					elementsObject[i].focus();
				}
			}
			else
			{
				// ============================
				// there is a value, clear style
				// ============================
				if (elementsObject[i].originalStyle!='')
					elementsObject[i].className=elementsObject[i].originalStyle;
				else
					elementsObject[i].className="name";
			}
		}  
	   
	   
	   
	   
	   
	   var elementsObject = document.getElementsByTagName("input")
	   for(var i=0;i<elementsObject.length  ;i++) //searching all elements in page
	   {
			isLocalError = 0;
         // ***************************
         // check if field is mandetory
         // ***************************
         if (elementsObject[i].isMandetory=='1')
			if (elementsObject[i].value.replace(/^\s+|\s+$/g,"") =="") //includes triming
			{
				// ============================
				// no value, do mark this field
				// ============================
				if (elementsObject[i].ErrorStyle!='')
					elementsObject[i].className=elementsObject[i].ErrorStyle;
				else
					elementsObject[i].className="nameError";
					
				isLocalError=1;
				if (isFirstField==0)
				{
					isFirstField=1;
					elementsObject[i].focus();
				}
			}
			else
			{
				// ============================
				// there is a value, clear style
				// ============================
				if (elementsObject[i].originalStyle!='')
					elementsObject[i].className=elementsObject[i].originalStyle;
				else
					elementsObject[i].className="name";
			}
			
         

		if  (elementsObject[i].isEmail=='1' && isLocalError==0)
			if ((((elementsObject[i].value.indexOf('@') < 0) || ((elementsObject[i].value.charAt(elementsObject[i].value.length-4) != '.') && (elementsObject[i].value.charAt(elementsObject[i].value.length-3) != '.')))) || (elementsObject[i].value.replace(/^\s+|\s+$/g,"") ==""))
			{
				// ============================
				// no value, do mark this field
				// ============================
				if (elementsObject[i].ErrorStyle!='')
					elementsObject[i].className=elementsObject[i].ErrorStyle;
				else
					elementsObject[i].className="nameError";
				isLocalError=1;
				if (isFirstField==0)
				{
					isFirstField=1;
					elementsObject[i].focus();
				}
			}
			else
			{
				// ============================
				// there is a value, clear style
				// ============================
				if (elementsObject[i].originalStyle!='')
					elementsObject[i].className=elementsObject[i].originalStyle;
				else
					elementsObject[i].className="name";
			}

		if  (elementsObject[i].isInteger=='1' && isLocalError==0)
			if (IsNotValidString(elementsObject[i].value,'0123456789'))
			{
				// ============================
				// no value, do mark this field
				// ============================
				if (elementsObject[i].ErrorStyle!='')
					elementsObject[i].className=elementsObject[i].ErrorStyle;
				else
					elementsObject[i].className="nameError";
				isLocalError=1;
				if (isFirstField==0)
				{
					isFirstField=1;
					elementsObject[i].focus();
				}
			}
			else
			{
				// ============================
				// there is a value, clear style
				// ============================
				if (elementsObject[i].originalStyle!='')
					elementsObject[i].className=elementsObject[i].originalStyle;
				else
					elementsObject[i].className="name";
			}
		}
		// ============================
		// no error found, 
		// ============================
		if (isFirstField==1)
			{
				document.all('errorSpan').innerText='\nError, Please fill all Mandatory (*) Fields correctly\n\n\n';
				//alert("Fields with '*' are Mandatory, Please make sure you complete them")
				return false;
			}
		else
			{
				document.all('errorSpan').innerText='';
				//document.all(formName).action=goToURL;
				//document.all(formName).submit();
			//document.contactForm.submit();
			}
	}

