/***********************************************************
	* FUNCTION NAME : trimAll()
	* PURPOSE : To trim all the leading & trailing whitespaces
	* USAGE : if (trimAll(document.v2form.name.value)=="")
			  
***********************************************************/
	
function trimAll( strValue ) 
{
    var objRegExp = /^(\s*)$/; 
	
    //check for all spaces
    if(objRegExp.test(strValue)) {
        strValue = strValue.replace(objRegExp, '');
        if( strValue.length == 0)
            return strValue;
    }
	    
    //check for leading & trailing spaces
    objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
    if(objRegExp.test(strValue)) {
        //remove leading and trailing whitespace characters
        strValue = strValue.replace(objRegExp, '$2');
    }
    return strValue;
}


/***********************************************************
	* FUNCTION NAME : isNumeric()
	* PURPOSE : To check whether the value entered is numeric
	* USAGE : if(!isNumeric(document.frmName.controlName,"Please enter a numeric value"))
			  
***********************************************************/
function isNumeric(arg,msg)
{
	if(isNaN(arg.value))
	{
		alert(msg);
		arg.focus();
		arg.select();
		return false;
	}
	return true;
}


/***********************************************************
	* FUNCTION NAME : isValidEmail()
	* PURPOSE : To validate the email id
	* USAGE : if(!isValidEmail(document.frmName.controlName))
			  
***********************************************************/
function isValidEmail(arg)
{
	// check v2form.email for valid 'email-id'
	// declaring variables
	var email=arg.value
	var atindex=email.indexOf("@");           //index of @
		            
	var atindex1=email.lastIndexOf("@");      //index of second @
	var dotindex=email.indexOf(".");      //index of dot
	var dotindex1=email.lastIndexOf(".");     //index of second dot
		            
	var domain=dotindex1-atindex;           //characters between @ and dot
	var lengths=email.length;           //length of email
	var leng=lengths-email.lastIndexOf(".");  //no of characters after dot
		
	//declaring array for storing illegal characters
	var arr=new Array(21)
	arr[0]="?"
	arr[1]=";"
	arr[2]="!"
	arr[3]="#"
	arr[4]="$"
	arr[5]="%"
	arr[6]="^"
	arr[7]="*"
	arr[8]="("
	arr[9]=")"
	arr[10]=")"
	arr[11]="/"
	arr[12]=">"
	arr[13]="="
	arr[14]="<"
	arr[15]=","
	arr[16]=":"
	arr[17]="'\'"
	arr[18]="\\"
	arr[19]=' '
	arr[20]="`"
	arr[21]="~"
		    
	//checking for illegal characters
	for(j=0;j<lengths;j++)
	{
	    for(i=0;i<arr.length;i++)
	    {
	        var ex=email.charAt(j)
	        if(ex==arr[i])
	        {
	            alert("Please enter valid email address")
	            arg.focus()
	            arg.select() 
	            return false
	        }
	    }
	}   
		            
	//checking for null
	if(email=="")
	{
	    //alert("E-Mail field should not be left blank")
	    //arg.focus()
	    //return false
	} else {
		
	    //checking for length       
	    if(email.length<5)
	    {
	            alert("Please enter valid email address")
	        arg.focus()
	        arg.select()
	        return false
	    }   
		    
	    //checking for dot      
	    if(dotindex < 0)
	    {           
	     	            alert("Please enter valid email address")
	        arg.focus()
	        arg.select()
	        return false
	    }
		
	    //checking for @        
	    if(atindex < 0)
	    {           
      	            alert("Please enter valid email address")
	        arg.focus()
	        arg.select()
	        return false
	    }
		
	    //if dot before @ then check for dot after @
	    if( atindex >dotindex )
	    {
	        if ((dotindex1-dotindex)<2)
	        {
	            alert("Please enter valid email address")
	            arg.focus()
	            arg.select()
	            return false
	        }               
	    }
		            
	    //email should not start with dot
	    if (email.charAt(0)==".")
	    {
	            alert("Please enter valid email address")
	        arg.focus()
	        arg.select()
	        return false
	    }
		                
	    //check if there is a dot after @           
	    if( (dotindex1 - atindex )<1 )
	    {
	            alert("Please enter valid email address")
	        arg.focus()
	        arg.select()
	        return false
	    }
		
	    //checking for two @        
	    if(atindex != atindex1)
	    {           
	            alert("Please enter valid email address")
	        arg.focus()
	        arg.select()
	        return false
	    }
	    if(atindex == 0 )
	    {
	            alert("Please enter valid email address")
	        arg.focus()
	        arg.select()
	        return false
	    }   
		
	    //checking for domain name      
	    if(domain<=1)
	    {
	            alert("Please enter valid email address")
	        arg.focus()
	        arg.select()
	        return false
	    }
		
	    //checking for domain name              
	    if(leng<=1)
	    {
	            alert("Please enter valid email address")
	        arg.focus()
	        arg.select()
	        return false
	    }
		                
	    //immediate character before @ should not be dot
	    if (email.charAt(atindex-1)==".")
	    {
	            alert("Please enter valid email address")
	         arg.focus()
	         arg.select()
	         return false
	    }
		    
	    //immediate character after @ should not be dot
	    if (email.charAt(atindex+1)==".")
	    {
	            alert("Please enter valid email address")
	         arg.focus()
	         arg.select()
	         return false
	    }       
	}
	return true;
}//function ends

/***********************************************************
	* FUNCTION NAME : isValidDate()
	* PURPOSE : To validate the selected date from three dropdowns
	* USAGE : if(!isValidDate(document.frmName.controlName))
			  
***********************************************************/
function isValidDate(dayArg,monthArg,yearArg)
{
	
	day = dayArg.options[dayArg.selectedIndex].value
	month = monthArg.options[monthArg.selectedIndex].value
	year = yearArg.options[yearArg.selectedIndex].value
	
	Day = parseInt(day,10);
    Month = parseInt(month,10);
    Year = parseInt(year,10);
	
	if(Month==4 || Month==6 || Month==9 || Month==11){
      month_length=30;
    }
    else if (Month==1 || Month==3 || Month==5 || Month==7 || Month==8 || Month==10 || Month==12){
      month_length=31;
    }
    else if(Month==2 && Year%4==0 && Year%100!=0 || Year%400==0){
      month_length=29;
    }
    else if(Month==2 && Year%4!=0 || Year%100==0 && Year%400!=0){
      month_length=28;
    }
    if (Day>=1 && Day<=month_length && Month>=1 && Month<=12)
    {
		 return true;
    }
    else
    {
		alert("Please enter valid date");
		dayArg.focus();
		return false;
    }
}//function ends
/*
function isValidDate(day,month,year)
{
	
//	day = dayArg.options[dayArg.selectedIndex].value
//	month = monthArg.options[monthArg.selectedIndex].value
//	year = yearArg.options[yearArg.selectedIndex].value

	Day = parseInt(day,10);
    Month = parseInt(month,10);
    Year = parseInt(year,10);
	
	if(Month==4 || Month==6 || Month==9 || Month==11){
      month_length=30;
    }
    else if (Month==1 || Month==3 || Month==5 || Month==7 || Month==8 || Month==10 || Month==12){
      month_length=31;
    }
    else if(Month==2 && Year%4==0 && Year%100!=0 || Year%400==0){
      month_length=29;
    }
    else if(Month==2 && Year%4!=0 || Year%100==0 && Year%400!=0){
      month_length=28;
    }
    if (Day>=1 && Day<=month_length && Month>=1 && Month<=12)
    {
		 return true;
    }
    else
    {
		alert("Selected date is invalid, Please Check");
		day.focus();
		return false;
    }
}//function ends
*/

/***********************************************************
	* FUNCTION NAME : CheckDateDifference()
	* PURPOSE : To validate the difference between the two selected dates
	* USAGE : if(!CheckDateDifference(document.frmName.ddStart,document.frmName.mmStart,document.frmName.yyyyStart,document.frmName.ddEnd,document.frmName.mmEnd,document.frmName.yyyyEnd,"Invalid date message"))
			  
***********************************************************/
function CheckDateDifference(Arg_sFirstDtD,Arg_sFirstDtM,Arg_sFirstDtY,Arg_sSecondDtD,Arg_sSecondDtM,Arg_sSecondDtY,msg)
{
	FirstDtD = Arg_sFirstDtD.options[Arg_sFirstDtD.options.selectedIndex].value;
	FirstDtM = Arg_sFirstDtM.options[Arg_sFirstDtM.options.selectedIndex].value;
	FirstDtY = Arg_sFirstDtY.options[Arg_sFirstDtY.options.selectedIndex].value;
	SecondDtD = Arg_sSecondDtD.options[Arg_sSecondDtD.options.selectedIndex].value;
	SecondDtM = Arg_sSecondDtM.options[Arg_sSecondDtM.options.selectedIndex].value;
	SecondDtY = Arg_sSecondDtY.options[Arg_sSecondDtY.options.selectedIndex].value;
	
	if(SecondDtY > FirstDtY)
	{
		//alert("'Purchase Date' is greater than the 'Delivery Date'.");
		alert(msg);
		Arg_sSecondDtY.focus();
		return false;
	}
	else if(FirstDtY == SecondDtY)
	{
		if(SecondDtM > FirstDtM)
		{
			//alert("'Purchase Date' is greater than the 'Delivery Date'.");
			alert(msg);
			Arg_sSecondDtM.focus();
			return false;
		}
		else if(FirstDtM == SecondDtM)
		{
		 	if(SecondDtD > FirstDtD)
		 	{
				//alert("'Purchase Date' is greater than the 'Delivery Date'.");
				alert(msg);
				Arg_sSecondDtD.focus();
				return false;
			}
			else
			{
				return true;
			}
		}
		else
		{
			return true;
		}
	}
	else
	{
		return true;
	}
}//function ends


/***********************************************************
	* FUNCTION NAME : isValidPhoneNo()
	* PURPOSE : To validate the phone no
	* USAGE : if(!isValidPhoneNo(document.frmName.controlName))
			  
***********************************************************/
function isValidPhoneNo(arg,msg)
{
	var phno,len;
	phno = new String(arg.value);
	len = phno.length;
	for(i=0; i<len; i++)
	{
		ch = phno.charAt(i);
		if((ch >= '0' && ch <= '9') || (ch == '-') || (ch == ' ') || (ch == '/'))
		{
		}
		else
		{
			alert("Invalid "+msg+".\nApart from numeric values only the following characters are allowed in the phone no\nhypen\tspace\tforward slash");
			arg.focus();
			arg.select();
			return false;
		}
	}
	return true;
}



/***********************************************************
	* FUNCTION NAME : isEmpty()
	* PURPOSE : To validate the phone no
	* USAGE : if(isEmpty(document.frmName.controlName))
			  
***********************************************************/
function isEmpty(arg,msg)
{
	str = arg.value;
	
	if(trimAll(str)=="")
	{
		alert(msg);
		arg.focus();
		return true;
	}
	
	return false;
}

/*
function isEmpty(arg)
{
	str = arg.value;
	if(trimAll(str)=="")
	{
		//arg.focus();
		return true;
	}
	return false;
}
*/

function isValidUserName(arg)
{
	var userName,len,count;
	count = 0;
	userName = new String(arg.value);
	len = userName.length;
	for(i=0; i<len; i++)
	{
		ch = userName.charAt(i);
		if((ch >= '0' && ch <= '9') || (ch == '-') || (ch == '_') || (ch == '.') || (ch == ' ') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
		{
			if((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
			{
				count++;
			}
		}
		else
		{
			alert("Please enter only alphanumeric characters\nwith some special characters('-','dot','space' and '_') in the Username field");
			arg.focus();
			arg.select();
			return false;
		}
	}
	if(count == 0)
	{
			alert("Please enter atleast one alphanumeric character in the Username field");
			arg.focus();
			arg.select();
			return false;
	}
	return true;
}




function isValidPassword(arg)
{
	var passwd,len;
	passwd = new String(arg.value);
	len = passwd.length;
	for(i=0; i<len; i++)
	{
		ch = passwd.charAt(i);
		if((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
		{
			
		}
		else
		{
			alert("Please enter only alphanumeric characters in the Password field");
			arg.focus();
			arg.select();
			return false;
		}
	}
	
	return true;
}




function chkSpace(arg)
{
	var invalid = " ";
	if (arg.value.indexOf(invalid) > -1)
	{
		arg.focus();
		arg.select();
		return false;
	}
	return true;
}



function isComboSelected(arg,msg)
{
	//alert(arg.selectedIndex);
	if(arg.selectedIndex == 0)
	{
		alert(msg);
		arg.focus();
		return false;
	}
	return true;
}






//This function checks the no of characters entered in the textarea


// count the text in al fields we know about
function countCharsInFields(field,maxChar,countField)
{
	var totalLength=0;
	
	//MAXCHAR = maxChar;
	     
    fieldValue = field.value ;
    totalLength = fieldValue.length;
       
    charValue = countField.value;
       
    // tell 'em if they've got too many chars
    if (totalLength > maxChar){
         alert("The maximum number of characters\nthat can be entered is " +
                maxChar + ".\n\n You have entered " +
                totalLength + " characters.");
         field.value=field.value.substring(0,field.value.length+maxChar-totalLength);
                
         countField.value = 0;
        }
    else{
          countField.value = maxChar-totalLength;
        }
}






function isValidImageFile(arg,msg)
{
	var str,len,lastIndex,extn;
	str = new String(arg.value);
	len = str.length;
	lastIndex = str.lastIndexOf(".");
	extn = str.substr(lastIndex+1);
	if(extn.toUpperCase() == "GIF" || extn.toUpperCase() == "JPG")
	{
	}
	else
	{
		alert(msg);
		arg.select();
		arg.focus();
		return false;
	}
	return true;
}






/***********************************************************
	* FUNCTION NAME : isRadioSelected()
	* PURPOSE : To check whether a radio button 
				(out of a group of radio buttons with the same name) 
				is checked
	* USAGE : if(isRadioSelected(document.frmName.radioName,"error message"))
			  
***********************************************************/
function isRadioSelected(arg,msg)
{
	//len = arg.length;
	var flagChecked = 0;
	if(arg.length)
	{
		for(i=0; i<arg.length; i++)
		{
			//alert(arg[i].value);
			if(arg[i].checked)
			{
				//alert(arg[i].value);
				flagChecked = 1;
			}
		}
	}
	else
	{
		if(arg.checked)
			flagChecked = 1;
	}
	//alert(arg[);
	if(flagChecked==0)
	{
		alert(msg);
		if(arg.length)
			arg[0].focus();
		else
			arg.focus();
		return false;
	}
	return true;
}





function isCheckboxSelected(arg,msg)
{
	//len = arg.length;
	var flagChecked = 0;
	if(arg.length)
	{
		for(i=0; i<arg.length; i++)
		{
			//alert(arg[i].value);
			if(arg[i].checked)
			{
				//alert(arg[i].value);
				flagChecked = 1;
			}
		}
	}
	else
	{
		if(arg.checked)
			flagChecked = 1;
	}
	//alert(arg[);
	if(flagChecked==0)
	{
		alert(msg);
		if(arg.length)
			arg[0].focus();
		else
			arg.focus();
		return false;
	}
	return true;
}

/***********************************************************
	* FUNCTION NAME : isEmpty()
	* PURPOSE : To validate the Name of the user
	* MODIFIED BY : SUDHA R
	* USAGE : if(isEmpty(document.frmName.controlName))
			  
***********************************************************/
function isEmpty(arg,msg)
{
	str = arg.value;
	
	if(trimAll(str)=="")
	{
		alert(msg);
		arg.focus();
		return true;
	}
	
	return false;
}

function isValidName(arg)
{
	var Name,len,count;
	count = 0;
	Name = new String(arg.value);
	len = Name.length;
	for(i=0; i<len; i++)
	{
		ch = Name.charAt(i);
		if((ch == '.') || (ch == ' ') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
		{
			if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
			{
				count++;
			}
		}
		else
		{
			alert("Please enter only alphabet characters\nwith some special characters('dot' and 'space') in the Name field");
			arg.focus();
			arg.select();
			return false;
		}
	}	
	return true;
}

/***********************************************************
	* FUNCTION NAME : isEmpty()
	* PURPOSE : To validate the Name of the City
	* MODIFIED BY : SUDHA R
	* USAGE : if(isEmpty(document.frmName.controlName))
			  
***********************************************************/
function isEmpty(arg,msg)
{
	str = arg.value;
	
	if(trimAll(str)=="")
	{
		alert(msg);
		arg.focus();
		return true;
	}
	
	return false;
}

function isValidCity(arg)
{
	var City,len,count;
	count = 0;
	City = new String(arg.value);
	len = City.length;
	for(i=0; i<len; i++)
	{
		ch = City.charAt(i);
		if((ch == ' ') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
		{
			if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
			{
				count++;
			}
		}
		else
		{
			alert("Please enter only alphabet characters\nwith some special character('space') in the City field");
			arg.focus();
			arg.select();
			return false;
		}
	}	
	return true;
}

/***********************************************************
	* FUNCTION NAME : isEmpty()
	* PURPOSE : To validate the Address Field
	* MODIFIED BY : SUDHA R
	* USAGE : if(isEmpty(document.frmName.controlName))
			  
***********************************************************/

function isValidAddress(arg)
{
	var address = document.v2form.address.value;
	var regAlphaNumericWithSpace = /^[a-zA-Z0-9\s\-\/\()\,]+$/;		
	
		if(address == "")

			{
				alert("Please Enter Contact Address");
				v2form.address.focus();
				return false;
			}

			if(!regAlphaNumericWithSpace.test(address))

			{
				alert("Please enter only alphanumeric characters\nwith some special characters('-' , 'space','()'and '/') in the Address field!");
				v2form.address.focus();
				return false;
			}

	return true;
}



/*function isValidAddress(arg)
{
	
	var Address,len,count;
	count = 0;
	Address = new String(arg.value);
	len = Address.length;
	for(i=0; i<len; i++)
	{
		ch = Address.charAt(i);
		alert(ch);
		if((ch >= '0' && ch <= '9') || (ch == '-') || (ch == '')|| (ch == ' ') || (ch == '/') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
		{
			if((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
			{
				count++;
			}
		}
		else
		{
			alert("Please enter only alphanumeric characters\nwith some special characters('-' , 'space' and '/') in the Address field");
			arg.focus();
			arg.select();
			return false;
		}
	}	
	if(count == 0)
	{
			alert("Please enter atleast one alphanumeric character in the Address field");
			arg.focus();
			arg.select();
			return false;
	}
	return true;
}
*/
/*--------------------------------------Validation for MembershipNo---------------------------------------------*/

function isValidMembershipno(arg)
{
	var Member,len;
	Member = new String(arg.value);
	len = Member.length;
	for(i=0; i<len; i++)
	{
		ch = Member.charAt(i);
		if((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
		{
			
		}
		else
		{
			alert("Please enter only alphanumeric characters in the MembershipNo field");
			arg.focus();
			arg.select();
			return false;
		}
	}
	
	return true;
}

/*--------------------------------------Validation for State,Organization and Designation---------------------------------------------*/

function isValidAlphanumericwithSpace(arg)
{
	var Alpha,len;
	Alpha = new String(arg.value);
	len = Alpha.length;
	for(i=0; i<len; i++)
	{
		ch = Alpha.charAt(i);
		if((ch >= '0' && ch <= '9') || (ch == ' ') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
		{
			
		}
		else
		{
			alert("Please enter only alphanumeric characters and space");
			arg.focus();
			arg.select();
			return false;
		}
	}
	
	return true;
}
