// Purpose : Validate email address
// Accepts : email address
// Returns : true for valid email address
//			 false for invalid email address
function validateEmailAddress(EmailAddress) {
	var isValidEmail;
	var strEmail = new String(EmailAddress);
	isValidEmail = true;
	// a@b.c should be the shortest an address could be
	if (strEmail.length < 5) 
		isValidEmail = false;
	else if (strEmail.indexOf("@") < 0)  // has at least one "@"
		isValidEmail = false;
	else if (strEmail.indexOf(".") < 0) // has at least one "."
		isValidEmail = false;
	else {
		//has only one "@"
		var atCnt;
		var i;
		atCnt = 0
		for (i = 1; i <= strEmail.length; i++) {
			if  (strEmail.charAt(i) == "@") 
				atCnt = atCnt + 1
		}
		if (atCnt > 1) 
			isValidEmail = false
	}
	return isValidEmail;
}

// Purpose : Check for null value
// Accepts : string value
// Returns : true for length of the string is equal to 0
//			 false for length of the string is greater than 0
function IsNull(strValue) {
	var newString = new String(strValue);
	newString = newString.replace(/^\s*(.*)/, "$1");
	newString = newString.replace(/(.*?)\s*$/, "$1");
	if (newString.length <= 0) 
		return true;
	else
		return false;
}

// Purpose : Check the length of the string
// Accepts : string value
// Returns : true if the length of the string is greater than 
//or equal to specified length otherwise false

function isValidLength(strValue, intLength) {
	var newString = new String(strValue);
	newString = newString.replace(/^\s*(.*)/, "$1");
	newString = newString.replace(/(.*?)\s*$/, "$1");
	if (newString.length >= intLength) 
		return true;
	else
		return false;
}

// Purpose : Check a string against a pattern of allowed characters
// Accepts : string value
// Returns : true if the string contains characters defined in the character
// pattern otherwise false

function isValidString(strValue, strValidChars) {
	var newString = new String(strValue);
	for (var i = 0; i < newString.length; i++) {
		if (strValidChars.indexOf(newString.charAt(i)) == -1) {
			return false;
		}	
	}
	return true;
}

function isValidDate(intDay, intMonth, intYear) {
	if (((intMonth == 1) || (intMonth == 3) || (intMonth == 5) || (intMonth == 7) || (intMonth == 8) || (intMonth == 10) || (intMonth == 12)) && ((intDay > 31) || (intDay < 1))) {
		return false;
	}
	if (((intMonth == 4) || (intMonth == 6) || (intMonth == 9) || (intMonth == 11)) && ((intDay > 30) || (intDay < 1))) {
		return false;
	}
	if (intMonth == 2) {
		if (intDay < 1) {
				return false;
		}
		if (LeapYear(intYear) == true) {
			if (intDay > 29) {
				return false;
			}
		}
		else {
			if (intDay > 28) {
				return false;
			}
		}
	}
	return true;
}

function LeapYear(intYear) {
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { 
			return true; 
		}
	}
	else {
		if ((intYear % 4) == 0) { 
			return true; 
		}
	}
	return false;
}

function SelectALL(frmname,selectall)	{					
	var intchecked = 0;
	var intunchecked = 0;
	var StrBtnName;
	var frm = frmname;
	StrBtnName = new String();
	StrBtnName = selectall.value;

	for(var i = 0;i < frm.length; i++)	{
		if ((frm.elements[i].disabled == false) && (frm.elements[i].type == 'checkbox'))	{						      
			if(StrBtnName.substring(0,2) == "De")	{
				frm.elements[i].checked = false;
				selectall.value = "Select All";
			}
			else	{
				frm.elements[i].checked = true;
				selectall.value="Deselect All";
			}
	 	}
	}
}
	
function DeSelectALL(frmname)	{					
	var frm = frmname;
	for (var i = 0; i < frm.length; i++)	
		if ((frm.elements[i].disabled == false) && (frm.elements[i].type == 'checkbox'))
			if(frm.elements[i].checked)	
				frm.elements[i].checked = false;
}
				
function CheckAtleast(frmname, strMsg1, strMsg2)	{
	var Selected = 0;
	var frm = frmname;
	var strIDs = "";
	for(var i = 0;i < frm.length; i++)	{						
		if ((frm.elements[i].disabled == false) && (frm.elements[i].type == 'checkbox'))		{
			if(frm.elements[i].checked == true) {
				Selected = 1; 
				strIDs = strIDs + frm.elements[i].value + ",";
			}	
		}
	}

	if(Selected == 0)	{
		alert(strMsg1);
		return false;
	}
	if(confirm(strMsg2)) {
		frm.IDs.value = strIDs.substr(0, (strIDs.length - 1));
		return true;
	}
	else
		return false;
}	

function validateEnableDisable(frmname, strMsg1, strMsg2)	{
	var Selected = 0;
	var frm = frmname;
	for(var i = 0;i < frm.length; i++)	{						
		if ((frm.elements[i].disabled == false) && (frm.elements[i].type=='checkbox'))	{
			if(frm.elements[i].checked==true)
				Selected=1; 
		}
	}

	if(Selected == 0)	{
		alert(strMsg1);
		return false;
	}

	if(confirm(strMsg2))	{
		return true;
		frm.submit();
	}
	else
		return false;
}	

function validateDeveloper(){
	if (IsNull(document.frmLogin.txtUserName.value)){
		alert("Please enter a valid user name");
		document.frmLogin.txtUserName.focus();
		return false;
	}
	if (IsNull(document.frmLogin.txtPassword.value)){
		alert("Please enter a valid password");
		document.frmLogin.txtPassword.focus();
		return false;
	}
	return true;
}

function validateUsername(){
	if (IsNull(document.fmrPasswordDetails.txtUserName.value)){
		alert("Please enter a valid user name");
		document.fmrPasswordDetails.txtUserName.focus();
		return false;
	}
	return true;
}