// validate email addresh. obj:object, msg:error message for invalid email, bAllowBlank: whether email is allowed to be blank or not
function validateEmail(obj, msg, bAllowBlank)
{
	var str = obj.value;
	if (jsTrim(str) == "")
	{
		if (bAllowBlank)
		{
			return true;
		}
		else
		{
			alert(msg);
			obj.focus();
			return false;
		}
	}

	var at = "@";
	var dot = ".";
	var lat = str.indexOf(at);
	var lstr = str.length;
	var ldot = str.indexOf(dot);

	if ( (str.indexOf(at)==-1)
	  || (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	  || (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	  || (str.indexOf(at,(lat+1))!=-1)
	  || (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	  || (str.indexOf(dot,(lat+2))==-1)
	  || (str.indexOf(" ")!=-1) )
	{
		alert(msg);
		obj.focus();
		return false;
	}

	return true;
}
 

//Remove all white spaces in "s" string
function jsTrim(s)
{
	if ((s == null) || (typeof (s) != 'string') || !s.length)
	{
		return '';
	}
	return s.replace(/\s+/g, '');
}


//Check whether the value of 'obj' is blank or not. If it is blank, alert message(msg).
function validatePresence(obj, msg)
{
	if (jsTrim(obj.value) == '')
	{
		alert(msg);
		obj.focus();
		return false;
	}
	return true;
}


//Check value of 'obj'. It must an positive number
function validatePresenceNonNegNumber(obj, msg)
{
	if (jsTrim(obj.value) == '')
	{
		alert(msg);
		obj.focus();
		return false;
	}
	if (!isFinite(obj.value) || obj.value < 0)
	{
		alert(msg);
		obj.focus();
		return false;
	}
	return true;
}


//Check a group of check box, make sure at least on of them is checked
function validateCheckbox(obj, msg, minNumNeedChecked)
{
	//Get Length of the checkbox
	if (!obj.length) var lenOfCheckbox = 1;//for just one checkbox. 
	else var lenOfCheckbox = obj.length;//for more than one checkboxes
	if (minNumNeedChecked == -1) minNumNeedChecked = lenOfCheckbox;//-1 means all boxes are mean to be checked.
	var checkboxCheckedCounter = 0;

	if (lenOfCheckbox == 1)//for just one checkbox
	{
		if (obj.checked) checkboxCheckedCounter = 1;
	}
	else//for more than one checkboxes
	{
		for (var i = 0; i < lenOfCheckbox; i++) 
		{
			if (obj[i].checked) 
			{
				checkboxCheckedCounter++;
			}
		}
	}

	//alert("counter = " + checkboxCheckedCounter + "| lenOfCheckbox = " + lenOfCheckbox + " | minNumNeedChecked = " + minNumNeedChecked);
	
	if (checkboxCheckedCounter < minNumNeedChecked)
	{
		highlightDisHighlightField(obj, true);//highlight the checkboxes
		alert(msg);
		return false;
	}
	
	highlightDisHighlightField(obj,false);
	return true;
}


//For hightlighting one or more checkboxes.
//<><><> this doesn't work for Firefox
function highlightDisHighlightField(obj, bHighlight)
{
	if (!obj.length)//Group Checkbox.For more than one checkbox
	{
		if (bHighlight) obj.style.cssText = "border:1px solid red"; //highlight the checkbox if "bHighlight" is true
		else obj.style.cssText = ""; //dis-highlight the checkbox
	}
	else//One Checkbox
	{	
		for (var i = 0; i < obj.length; i++)
		{
			if (bHighlight) obj[i].style.cssText = "border:1px solid red"; //highlight the checkbox if "bHighlight" is true
			else obj[i].style.cssText = ""; //dis-highlight the checkbox
		}
	}
}



