// ******************************************************************
// 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;	
	}

// ******************************************************************
// 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()
	{
	   var error_bg = '#DBE5EF';
	   var isFirstField = 0;
	   var isLocalError;
	   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
				// ============================
				elementsObject[i].style.background= error_bg;
				isLocalError=1;
				if (isFirstField==0)
				{
					isFirstField=1;
					elementsObject[i].focus();
				}
			}
			else
			{
				// ============================
				// there is a value, clear style
				// ============================
                elementsObject[i].style.background= '';
			}
			
         // ***************************
         // minimum chars
         // ***************************
		if  (elementsObject[i].min !=null && isLocalError==0)
			if (elementsObject[i].value.replace(/^\s+|\s+$/g,"").length < elementsObject[i].min)
			{
				// ============================
				// no value, do mark this field
				// ============================
                elementsObject[i].style.background= error_bg;
				isLocalError=1;
				if (isFirstField==0)
				{
					isFirstField=1;
					elementsObject[i].focus();
				}
			}
			else
			{
				// ============================
				// there is a value, clear style
				// ============================
                elementsObject[i].style.background= '';
			}
         // ***************************
         // maximum chars
         // ***************************
		if  (elementsObject[i].max !=null && isLocalError==0)
			if (elementsObject[i].value.replace(/^\s+|\s+$/g,"").length > elementsObject[i].max)
			{
				// ============================
				// no value, do mark this field
				// ============================
                elementsObject[i].style.background= error_bg;
				isLocalError=1;
				if (isFirstField==0)
				{
					isFirstField=1;
					elementsObject[i].focus();
				}
			}
			else
			{
				// ============================
				// there is a value, clear style
				// ============================
                elementsObject[i].style.background= '';
			}
         // ***************************
         // match other field
         // ***************************
		if  (elementsObject[i].mustMatch !=null && isLocalError==0)
			if (elementsObject[i].value != document.all(elementsObject[i].mustMatch).value)
			{
				// ============================
				// no value, do mark this field
				// ============================
                elementsObject[i].style.background= error_bg;
				isLocalError=1;
				if (isFirstField==0)
				{
					isFirstField=1;
					elementsObject[i].focus();
				}
			}
			else
			{
				// ============================
				// there is a value, clear style
				// ============================
                elementsObject[i].style.background= '';
			}

		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
				// ============================
                elementsObject[i].style.background= error_bg;
				isLocalError=1;
				if (isFirstField==0)
				{
					isFirstField=1;
					elementsObject[i].focus();
				}
			}
			else
			{
				// ============================
				// there is a value, clear style
				// ============================
                elementsObject[i].style.background= '';
			}

		if  (elementsObject[i].isInteger=='1' && isLocalError==0)
			if (IsNotValidString(elementsObject[i].value,'0123456789'))
			{
				// ============================
				// no value, do mark this field
				// ============================
                elementsObject[i].style.background= error_bg;
				isLocalError=1;
				if (isFirstField==0)
				{
					isFirstField=1;
					elementsObject[i].focus();
				}
			}
			else
			{
				// ============================
				// there is a value, clear style
				// ============================
                elementsObject[i].style.background= '';
			}
		}
		// ============================
		// no error found, 
		// ============================
		if (isFirstField==1)
			{
				//document.all('errorSpan').innerText='השדות המסומנים לא הוזנו כהלכה.';
				return false;
			}
		else
			{
			    return true;
			}
	}
