/*******************************
Common Java Script Functions 
Developed By- Atul chauhan
Date- 01-05-06
Updation on 18-05-06 
		(Add maxLen Function)
Update By- Atul Chauhan
Updation on 19-05-06
		(Add chkRadioList Function)
Update By- Atul Chauhan
Updation on 23-05-06
		(Add isEmail Function)
******************************/
var strError
function chkValue(objValue,value,strError)
	{
		if (objValue.value==value)
			{
				alert(strError)
				objValue.focus()
				strError=''
				return false
			}
		else
			return true
	
	}
function chkAlphaNum(objValue,strError)
	{
		var charpos = objValue.value.search("[^A-Za-z0-9' ']"); 
			if(objValue.value.length > 0 &&  charpos >= 0) 
				{ 
					if(!strError || strError.length ==0) 
						{ 
							strError = objValue.name+": Only alpha-numeric characters allowed "; 
						}//if 
					alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
					strError=''
					objValue.focus()
					return false
				}
		
	}
function chkMaxLen(objValue,cmdvalue,strError)
	{
		if(eval(objValue.value.length) >  eval(cmdvalue)) 
			{ 
				if(!strError || strError.length ==0) 
					{ 
						strError = objValue.name + " : " + cmdvalue + " characters minimum  "; 
					}//if               
				alert(strError + "\n[Current length = " + objValue.value.length + " ]"); 
				objValue.focus();
				strError=''
				return false;                 
			}//if 
		return true
	}
function req(objValue,strError)
	{
		if(eval(objValue.value.length) == 0) 
			{ 
				if(!strError || strError.length ==0) 
				{ 
					strError = objValue.name + " : Required Field"; 
				}//if 
				objValue.focus();
				alert(strError); 
				strError=''
				return false; 
			}//if 
		else
			return true
	}
	
function chkDesc(objValue,strError)
	{
		var charpos = objValue.value.search("[^A-Za-z0-9\-_\\/\ ]"); 
		if(objValue.value.length > 0 &&  charpos >= 0) 
			{ 
				if(!strError || strError.length ==0) 
			{ 
			strError = objValue.name+": characters allowed are A-Z,a-z,0-9,- and _"; 
		}//if                             
		alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
		objValue.focus();
		return false; 
		}//if 	
	}
function chkNum(objValue,strError)
	{
		var charpos = objValue.value.search("[^0-9]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                if(!strError || strError.length ==0) 
					{ 
						strError = objValue.name+": Only digits allowed "; 
						
					}//if               
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
                strError=""
                objValue.focus()
                return false; 
              }//if 
             else
				return true
	}
function maxLen(objValue,value,strError)
	{
		if(parseInt(objValue.value.length)>parseInt(value))
			{
				alert(strError+ "\n [Current length is " + objValue.value.length);
				return false;
			}
	
	}
function chkRadioList(objValue,value,strError)
	{
		var chk=false
		for(i=0;i<value;i++)
			{
				//objValue[i].style.border='none'
				if(objValue[i].checked)
					chk=true
			}
		if(chk==false)
			{
				alert(strError)
				objValue[0].focus()
			}
		return chk
	}
function setRadioBorder(objValue,value)
	{
		for(i=0;i<value;i++)
			{
				objValue[i].style.border='none'
			}
	}
function charCode(objValue,charCode)
	{
		output=0
		for (var i = 0; i <objValue.value.length; i++) 
						{
							if (objValue.value.charCodeAt(i) == charCode) 
								{
									output +=1 
								} 
						}
		return output
	}
function isEmail(objValue)
{
	
	theValue=objValue.value
	objValue.focus()
	if(theValue!="")
		{
			var chk_mail = "true"
			var ch = ""
			var at = ""
			var dot=""
			theValue = trim(theValue) ;  /* ------  removes leading & trailing spaces ------ */
			var theLen = theValue.length 
											
			if(theValue.indexOf('@', 0) == -1)  /*---- if @ is not present ---- */
			{
				alert("Email must contain domain name");
				return(false) ;
			}
			if(theValue.charAt(0)=='@' || theValue.charAt(theLen)=='@' || theValue.charAt(theLen-1)=='@')
			{
				alert("@ not allowed at start or end of email") ;
				return false ;
			}
			
			/* ------  no dot allowed at start, end  ------ */
			if(theValue.charAt(0)=='.' || theValue.charAt(theLen)=='.' || theValue.charAt(theLen-1)=='.')
			{
				alert("Dot(.) not allowed at start or end of email") ;
				return false ;
			}
			
			at = theValue.indexOf('@', 0) ;

			dotat=theValue.indexOf('.',0) ;	

			if( theValue.charAt(dotat+1) == '.' || theValue.charAt(dotat+1) == '@' )
			{
				alert("Dot and @ are not allowed immediately following the dot in email") ;
				return false ;
			}

			/*  ----  no @ or dot allowed following @ ----  */
			if( theValue.charAt(at+1) == '.' || theValue.charAt(at+1) == '@' )
			{
				alert("Dot and @ are not allowed immediately following the @ in email") ;
				return false ;
			}
				
			if ((theValue.indexOf('@', at+1))!=-1)  /* ---  multiple @ in email-id  ---  */
			{
				alert("Multiple @ are not allowed in email ");
				return false ;
			}	
			
			/* ------  no @ allowed at start, end  ------ */
			
						
			if(theValue.indexOf(" ",0) != -1)  /* ---- blank spaces present in email-id  ----  */
			{
				alert("Blank spaces are not allowed in email") ;
				return false ;
			}

			for (k = 0 ; k < theLen ; ++k)
			{
				ch =  theValue.substring(k, k+1)
				if (ch =="@")
				{
					if(theValue.substring(k, theLen)=='@')
					{
						return false ;
					}
					if(theValue.indexOf('.', k) == -1) /* --- dot not present after @ ---  */
					{
						alert("Invalid Email Id")
						return false ;
					}
				}
				
				/*  ------  no dot allowed immediately following @  ------  */
				if ((ch == '.')&&(theValue.substring(k+1, k+2) == "."))
				{
					alert("Consecutive dots are not allowed in email") ;
					dot = k
					return false ;					
				}

				if (!( (ch >= 'a' && ch <= "z") || (ch >= 'A' && ch <= "Z") || (ch >= '0' && ch <= "9") || ch =="@" || ch =="." || ch =="_" || ch =="-" ))
				{
					alert("No special characters allowed except @, dot(.)") ;
					return false ;					
				}
			}  /* ---  end of for loop  ---  */

			for(var j=theValue.indexOf('.', at);j<=theLen;++j)
			{
				if(theValue.charAt(j)>='0' && theValue.charAt(j)<='9')
				{
					alert("Invalid domain name  ");
					return false;
					break;
				}
			}

		}
	return true            /*  ------ if no above condition found to be true ------  */
}  //   end of isEmail()

// -----------The Function trim() is used to trim the balnk Spaces ----------------
function trim(str)
{
	var strReturn = str ;
	var i = 0 ;
	for(i = 0;i< strReturn.length;i++)
	{
		if(!(strReturn.charAt(i)==' '))
			break ;
	}
	strReturn = strReturn.substring(i);
	for(i = strReturn.length ; i>0 ; i--)
	{
		if(!(strReturn.charAt(i-1)==' '))
			break;
	}
	strReturn = strReturn.substring(0,i);
	return strReturn;
}

//-----------------------END of Function trim ------------------------------------------


function persent(objValue,strError)
{
	if(objValue.value>100)
		{
			alert(strError)
			objValue.focus()
			return false
		}
	return true
}
function chkPassMatch(objValue,objCValue,strError)
{
if (objValue.value!=objCValue.value)
	{
		alert(strError)
		objValue.focus()
		strError=''
		return false
	}
else
	return true
} 

function isValidDate(strValue)
{	
	DateValidate = true
	msg = "\nPlease enter a Valid Date\n- Format: dd/mm/yyyy"
	strDate = new Array()
	strDate = strValue.split("/")	
	if (strDate.length !=3)
	{
		DateValidate = false
		alert(msg)
		return DateValidate
	}
	if ((strDate[0].indexOf('.', 0) != -1) || (strDate[1].indexOf('.',0) != -1) || (strDate[2].indexOf('.', 0) != -1))
	{
			msg = msg+"\n- Dot not Allowed in Date"
			DateValidate = false
			alert(msg)
			return DateValidate
	}
	if (isNaN(strDate[0]) || isNaN(strDate[1]) || isNaN(strDate[2]))
	{
			msg=msg+"\n- Character not Allowed"
			DateValidate = false
			alert(msg)
			return DateValidate
	}
	isleapyear =false
	if (strDate[2] % 4 == 0)
	{
	   if (strDate[2] % 100 == 0)
		{	if (strDate[2] % 400 == 0)
			{
			    isleapyear = true
			}
		}
		else
		{
			isleapyear = true
		}
	}

	if (strDate[2]>2100 || strDate[2]<1900)
	{
		msg=msg+"\n- Year out of Range"
		DateValidate = false
		alert(msg)
		return DateValidate
	}
	if (strDate[1]<=0 || strDate[1]>12)
	{
		msg=msg+"\n- Month Out of Range"
		DateValidate = false
		alert(msg)
		return DateValidate
	}
	if (strDate[0] <= 0 || (parseInt(strDate[1]) == 1 || parseInt(strDate[1]) == 3 || parseInt(strDate[1]) == 5 || parseInt(strDate[1]) == 7 || parseInt(strDate[1]) == 8 || parseInt(strDate[1]) == 10 || parseInt(strDate[1]) == 12) && parseInt(strDate[0])>31)
	{
		DateValidate = false
		alert(msg)
		return DateValidate
	}
	if (strDate[0] <= 0 ||  (parseInt(strDate[1]) == 4 || parseInt(strDate[1]) == 6 || parseInt(strDate[1]) == 9 || parseInt(strDate[1]) == 11)   &&  parseInt(strDate[0])>30)
	{
		DateValidate = false
		alert(msg)
		return DateValidate
	}
	if (strDate[0] <= 0 ||  parseInt(strDate[1])==2 && isleapyear==false && strDate[0]>28)
	{
		DateValidate = false
		alert(msg)
		return DateValidate
	}
	if (strDate[0] <= 0 || parseInt(strDate[1])==2 && isleapyear==true && strDate[0]>29)
	{
		DateValidate = false
		alert(msg)
		return DateValidate
	}
}