//check for a valid date
function isValidDate(dtValue,dtFormat)
{
	//var sDate=ctlDate.value;
	
		if(dtValue == "")
		return true;	
	
	var sDate=convertToStdFormat(dtValue,dtFormat);
	
	if(dtValue.length!=10 || dtValue.charAt(2)!='/' || dtValue.charAt(5) !='/')  
	{
		alert("Enter valid date in the format '" + dtFormat + "'");
		return false;
	}
	
	//1 - > MM/DD/YYYY , 2 - > DD/MM/YYYY
	
	var mm=parseInt(sDate.substring(0,2),10);
	var dd=parseInt(sDate.substring(3,5),10);
			
	var year=parseInt(sDate.substring(6,10),10);
	var months = new Array([31],[28],[31],[30],[31],[31],[31],[31],[30],[31],[30],[31]);

	if(( year%4==0 && year%100!=0) || year%400==0 ) 
	    months[1]=29;
	if(mm > 12 || isNaN(mm) || mm<=0 || sDate.charAt(1)<'0' || sDate.charAt(1)>'9')  
	{
		 alert("Please enter a valid month");
		 return false;
	}
	if( months[mm-1]< dd || isNaN(dd) || dd <=0 || sDate.charAt(4)<'0' || sDate.charAt(4)>'9')  
	{
		 alert("Please enter a valid day");
		 return false;
	}
	if(year<1900 || isNaN(year)) 
	{
		 alert("Not a valid Year. Should be >= 1900");
		 return false;
	}
	if(year>2070 || isNaN(year))
	{
		 alert ("Not a valid Year. Should be <= 2070");
		 return false;
	}
	 return true;
}


//Validate function to check for Positive Integers
		function validateInts(stNum,bAllowZero)
		{
			var sMsg;
			if(bAllowZero == true)
				sMsg = "Please enter an integer value";
			else
				sMsg = "Please enter an integer value greater than zero";
			
			if(isNaN(stNum) || trim(stNum).length == 0) 
			{
				alert(sMsg);
				return false;
			}
			if(!bAllowZero)
			{				
				if(parseInt(stNum,10) <= 0)
				{
					alert(sMsg);
					return false;
				}
			}	
			if(stNum.indexOf(".") > -1)
			{
				alert(sMsg);
				return false;			
			}
			return true;	
		}
		
		
		
//function to validate Numbers		
		function validateNums(stNum)
		{
			var sMsg = "Please enter a relevant number";
			if(isNaN(stNum) || trim(stNum).length == 0) 
			{
				alert(sMsg);
				return false;
			}	
			if(parseFloat(stNum,10) < 0)
			{
				alert(sMsg);
				return false;
			}
			return true;	
		}

//function to trim the given string 
		function trim(strToTrim)
		{
			var i;
			var tempstr="";
			//loop for the trailing spaces
			for(i = 0; i < strToTrim.length; i++){
				if((strToTrim.charAt(i) != " ") && (strToTrim.charAt(i) != "\n") && (strToTrim.charAt(i) != "\r") )
					break;				
				
			}
			strToTrim = strToTrim.substring(i,strToTrim.length);
			//loop for the leading spaces
			for(i = strToTrim.length-1; i >= 0 ; i--)
				if((strToTrim.charAt(i) != " ") && (strToTrim.charAt(i) != "\n") && (strToTrim.charAt(i) != "\r") )
					break;	
			//Substring ignoring the leading and trailing spaces
			strToTrim = strToTrim.substring(0,i+1)	
			return strToTrim;
		}	
		
//function	to convert the dateformats to the standard format (mm/dd/yyy)
function convertToStdFormat(dtValue, dtFormat)
{
	var mm,dd;
	
	if(dtFormat == "mm/dd/yyyy")
	{
		var mm=dtValue.substring(0,2);
		var dd=dtValue.substring(3,5);
	}
	else if(dtFormat == "dd/mm/yyyy")	
	{
		var dd=dtValue.substring(0,2);
		var mm=dtValue.substring(3,5);	
	}
	var sFinal = mm + "/" + dd + "/" + dtValue.substring(6,10);
	
	return sFinal;
	
}

//Function to check the HTML tag chars
function chkHTMLChars(objFrm)
{
	var frmCtl = objFrm;
	var frmLength = frmCtl.length;		
			
	//Loop through all the form elements values to find the characters entered
	//	as "<" or ">"
	for(var nIndex = 0; nIndex < frmLength; nIndex++)
	{
		var sVal = frmCtl[nIndex].value;				
				
		if(!sVal)
		 continue;			 
			//if exists, return false
			if((sVal.indexOf("<") >= 0) || (sVal.indexOf(">") >= 0))
			{
				alert("< and > are not allowed");					
				frmCtl[nIndex].select();
				frmCtl[nIndex].focus();
				return false;	
			}
	}	
				
	return true;
}



//Function to check for single quotes		
function chkSplChars(frmCtl,strValue,sDisp)
{
	if((strValue.indexOf("'") < 0) && (strValue.indexOf('"') < 0) 
			&& (strValue.indexOf("\\") < 0) && (strValue.indexOf("/") < 0) 
			&& (strValue.indexOf(":") < 0) && (strValue.indexOf("?") < 0) 
			&& (strValue.indexOf("*") < 0) && (strValue.indexOf("|") < 0))
	{		
		return true;	
	}	
			
	alert(sDisp+" cannot contain \', \", \\, /, :, *, ?, <, > and |");
	frmCtl.select();
	frmCtl.focus();
	return false;
}



function validateEmail(email)
{
	//check for space
	if (!spaceBetChars(email))
	{
		return false;
	}

	//check for special chars
	var charBag = '<>/?,;:"~`!#$%^&*()-=+\|[]{}' + "'";
	if (!validateSplChars(email,charBag)) return false;		

	 // there must be >= 1 character before @
	 var sLength = email.length;
	 var lPos = email.indexOf("@");

	 //check additional cond for sencond occurance of @	and atleast one occurance of dot
	 if ((lPos<=0) || (lPos >= sLength) || (email.indexOf("@",lPos+1)>=0) || (email.indexOf(".",lPos+2)==-1))
	 {
		return false;
	 }

	 return true;
}


function spaceBetChars(str)
{
	for (var i=0;i<str.length;i++)
	{
		if (str.charAt(i) == " ") return false
	}

	return true;
}


function validateSplChars(str,charBag)
{
	var j;
	var charBag = '<>/?,;:"~`!#$%^&*()-=+\|[]{}' + "'";
	if (str.length>0 || charBag.length>0)
	{
		//loop for not allowed special chars
		for (j=0;j<charBag.length; j++)
		{
			if (str.indexOf(charBag.charAt(j))>=0)
			{
				return false;
			}
		}
	}
	return true;
}



function validateMails(strIds)
{
	var mailArray,mailString;
	var bValidId = 0;
	mailString = strIds;
	mailArray = mailString.split(",");

	for (var i=0;i < mailArray.length; i++)
	{
	  if((validateEmail(mailArray[i])) == false)
		bValidId =1;
	}

	if (bValidId==1)
	{
	  return false;
	}

	return true;
}
