/*
General functions used in various places to validate the fields.
----------------------------------------------------------------
1.	fn. hasOthers will check the passed field 'fieldName' has any invalid chars
	other than the 'validChars'

2.	fn. validField will do appropriate type validations based upon the parameter 'sType' and
	display the passed message if fails

	The special characters can also be appended with the general validate characters

	The sType may have 'C' - Alpha, 'A' - Alphanumeric and 'N' - Numeric

*/

function hasOthers(fieldName,validChars)
{

	fieldvalue=fieldName.value;
	fieldvalue=fieldvalue.toLowerCase();

	newChars=validChars.toLowerCase();
 
	 for (i=0;i<fieldvalue.length;i++)
	 {
		sChar=fieldvalue.charAt(i);
		if (newChars.indexOf(sChar,0)<0)
		{
			return sChar;
		}
		 
	 }

	 return null;
}

function validField(sType,fieldName,errDescription,sSpecial)
{
	
	message="";
	sChars="abcdefghijklmnopqrstuvwxyz";
	sNumbers="1234567890";
	validchars="";

	if (sType=="C")	// Alpha
	{
		validchars=sChars;
		message="(A-Z), (a-z)";
	}
	else
	{
		if (sType=="N")	// Numeric
		{
		   validchars=sNumbers;
		message="(0-9)";
		}
		else
		{
			if (sType=="A")	// Alphanumeric
				validchars=sChars+sNumbers;
		message="(A-Z), (a-z), (0-9)";
		}
	}

	validchars=validchars+sSpecial;

	sChar=hasOthers(fieldName,validchars);

	if (sChar != null)
	{
		fieldName.select();
		fieldName.focus();
		if (errDescription=="")
		{
			alert("Please Enter Characters like "+message);
		}
		else
		{
			alert(errDescription);
		}
		return false;
	}

	return true;

}


function lengthch(fieldName,min,max,errLabel)
{
    fieldvalue=fieldName.value;

	if (min > 0)
	{
		if (fieldvalue.length < min)
		{
			fieldName.select();
			fieldName.focus();
				  
			alert("Please Note : The "+errLabel+" length should be greater than or equal to minimum "+ min);
			return false;
		}

	}

	if (max > 0)
	{
		if (fieldvalue.length > max)
		{
			fieldName.select();
			fieldName.focus();
				  
			alert("Please Note : The "+errLabel+" length should be less than or equal to maxmium "+ max);
			return false;
		}

	}

	if (min==max)
	{
		if (fieldvalue.length!=max)
		{
			fieldName.select();
		    fieldName.focus();

			alert("Please Note : The "+errLabel+" length should be equal to "+ max);
			return false;
		}

	}
	return true;
}


function isNotNull(fieldName,errLabel)
{		
	if (fieldName.value=="")
    {
		 fieldName.select();
		 fieldName.focus();

		 alert("Please Note : The "+errLabel+"");
		 return false;
	}
	return true;
}  


//Validation for Emails
function isEmail(email1)
{
	var email = email1.value;
        var newstr = "";
        var at = false;
        var dot = false;
        for (var i = 0; i < email.length; i++) 
        {
                ch = email.substr(i,1)

                if ((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z") || (ch == "@") || (ch == ".") || (ch == "_") || (ch == "-") || (ch >= "0" && ch <= "9")) 
                {
                        newstr += ch;
                        if (ch == "@") 
                        {
                           // if (email.indexOf("@") > 0) 
									
                                at=true;
                           
                        }
                        if (ch == ".") 
                        {
                               
								//if (email.indexOf(".") >3) 
									 dot=true;
                        }
                }
        }
			var x=0;
			x=email.length;
			if ((email.substr(x-1,x)=="@")||(email.substr(x-1,x)=="."))
				{
				at=false;
				dot=false;
				}
   
        if ((at == true) && (dot == true)) 
        {
                return true;
        }
        else 
        {
			   email1.select();
               email1.focus();

               alert ("Not a Valid E Mail ID");
			   return false;
        }
}


function Phone(fieldName,errDescription)
{
	if (validField("N",fieldName,errDescription,"-"))
	{

		a=fieldName.value.split("-");

		if (a != null)
		{
			if ((a[0].length==0) && (a.length>1))
			{
				fieldName.select();
				fieldName.focus();

				alert("Please Note : The First character cannot be -");
				return false;
			}
		}

		return true;
	
	}

	else 
		return false;

}

function isNumber(fieldName,min,max,noDec)
{
	sSpcl="";
	if (noDec>0) sSpcl=".";

	if (validField("N",fieldName,"Numbers cannot have any special Characters.",sSpcl))
	{

		if (noDec>0)
		{
			a=fieldName.value.split(".");

			if (a != null)
			{
				if (a.length>2)
				{
					fieldName.select();
					fieldName.focus();

					alert(" Please Note :Number cannot contain more than one dot.");
					return false;
				}
				else
				if (a.length==2)
				{
					if (a[1].length>noDec)
					{
						fieldName.select();
						fieldName.focus();

						alert("Please Note : Number of decimal places cannot be greater than "+noDec);
						return false;
					}
				}

			}
		}

		if (fieldName.value<min)
		{
			fieldName.select();
			fieldName.focus();

			alert("Please Note : Number should be greater than or equal to "+min);
			return false;
		}

		if ((max>0) && (fieldName.value>max))
		{
			fieldName.select();
			fieldName.focus();

			alert("Please Note : Number should be less than or equal to "+max);
			return false;
		}

		return true;

	}
	else
		return false;
}
function isSelected(fieldName,msg)
{
	if ((fieldName.selectedIndex==0))
		{
			fieldName.focus();
			if (msg=="") msg="Select one from the List.";
			alert(msg);
			return false;
		}
	else
		return true;
}

function isSelected1(fieldValue,msg)
{
	if ((fieldValue=="null") || (fieldValue=="-1")|| (fieldValue==" ")|| (fieldValue==""   ))
		{
			if (msg=="") msg="Select one from the List.";
			alert(msg);
			return false;
		}
	else
		return true;
}
//function to take a page to previous page
function cancel()
{
	window.history.back();
}

function isFirstNum(fieldName)

{
        var firstChar = fieldName.value.substr(0,1);
        ret = isNaN(firstChar);
        if (ret)
        {
                fieldName.select();
                fieldName.focus();
                alert ("Please Note : First Character Should be Numeric");
                return false;
        }
/*
        else
        {
                return ;
        }*/
	return true;
}

function isAutP(field1,field2)
{
if (field1.value != field2.value)
 {
        field1.select();
        field1.focus();
        alert("Invalid Authority :Please Provide Authorized Password .");
        return false;
 }
else
 {
        return true;
 }
}

function isFirstAlpha(fieldName)

{
        var firstChar = fieldName.value.substr(0,1);
        validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
                if (validChars.indexOf(firstChar,0)<0)
                {
                        alert("Please Note :The First Character Should be an Alphabet");
                        fieldName.select();
                        fieldName.focus();
                        return false;
                }
                return true;
}
function truDate(fieldname)
{
     var first=fieldname.value.substr(0,10);
        fieldname.value=first;
     return true;
}
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                     
                                                                                        
                                                                                              
                                                                                        
                                                                             
                                                                             
                                                                             
                                                                             
                                                                             
                                                                             
                                                                             
                                                                             
                                                                                             
                                                                                             
                                                                                        
                                                                                        
                                                                                        
                                                                                        
                                                                                        
                                                                                        
                                                                                          
                                                                                               
                                                                                               
                                                                                               
                                                                                               
                                                                                               
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                             
                                                                                           
                                                                                           
                                                                                           
                                                                                           
                                                                                           
                                                                                           
                                                                                                 
                                                                                                 
                                                                                                 
                                                                            