/*
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)
{	
	//alert(errLabel);
	
	if (fieldName.value=="")
    {
		 fieldName.select();
		 fieldName.focus();

		 alert("Please Note : The "+errLabel+" cannot be Null");
		 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 validDate(fieldName)
{
	
	fieldvalue=fieldName.value;
	str=fieldvalue;

	if(fieldvalue.length<9)
	{
	alert("Enter Date in (dd-mm-yyyy) Format");
	return false;
	}
	fieldvalue=new Date(fieldvalue);
    bRet=false;
	sMsg="Invalid Date. Correct date.";
	a=str.split("-");
	
    for(var i=0; i<a.length; i++)
	{
		 if (isNaN(a[i]))
		{
			   bRet=false;
			   sMsg="Please Enter Correct Date Format";
			   alert(sMsg);
			   fieldName.select();
			   fieldName.focus();
			   return bRet;
			   break;
		}
		else
		{
		bRet=true;
		}
	}
	
	if(bRet)
	{
	if (a.length == 3)
		{
		     f=a[2];
			
				if(bRet && f.length!=4)
				{
						sMsg="Enter the Year in four digits";
						bRet=false;
				}
				
				if(bRet && a[1].length!=2)
				{
						sMsg="Enter the month in two digits";
						bRet=false;
				}
				if(bRet && a[0].length!=2)
				{
						sMsg="Enter the date in two digits";
						bRet=false;
				}
		
		      if ((a[0] >0) && (a[1] >0) && (a[2] >0) && bRet)
			{
				s=new Date(a[1] +"/" + a[0] + "/" + a[2])
				if ((s.getMonth()+1) == a[1] && bRet)
				{
					
					sMsg="";
					bRet=true;
				}
				else
				{
					sMsg="Please Enter Correct Month and Day of the Date";
					bRet=false;
				}

			}else
			{
				bRet=false;
				sMsg="Please enter Correct Date Format";
			}
			}else
			{
			bRet=false;
			sMsg="Please enter Correct Date Format";
				}
	if (!bRet) 
	{
		fieldName.select();
		fieldName.focus();
		alert(sMsg);
	}
	}
	
	return bRet;
}

function getLongDate(fieldvalue)
{
	str=fieldvalue;
	fieldvalue=new Date(fieldvalue);

	sDate=null;

	if (!isNaN(fieldvalue))
	{
		a=str.split("/");
		if (a.length == 3)
		{
			if ((a[0] >0) && (a[1] >0) && (a[2] >0))
			{
				s=new Date(a[1] +"/" + a[0] + "/" + a[2])
				if ((s.getMonth()+1) == a[1]) {sDate=s;}
			}
		}

	}
	return sDate;
}

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)
{
	//alert(fieldValue);
		//alert(msg);
	if ((fieldValue=="null") || (fieldValue=="-1")|| (fieldValue==" ")|| (fieldValue==""   ))
		{
//			fieldName.focus();
			if (msg=="") msg="Select one from the List.";
			alert(msg);
			return false;
		}
	else
		return true;
}

// This function used to display the title at the top

function getheader(pagetitle,subvalue)
{
document.write("<table width='700' border='0' cellspacing='0' cellpadding='0' align='center'  bgcolor='#F5F4FF'>");
document.write("<tr>");
document.write("<td height='45' align='right'>");
document.write("<table cellpadding='0' cellspacing='0' border=0>");
document.write("<tr>");
document.write("<td align='top'>");
document.write("<a href = 'home.jsp' target='_parent'><img src='toplogo.jpg' border ='0'></a>");
document.write("</td>");
document.write("<td valign='top'>");
document.write("<table cellpadding='1' width=100% cellspacing='0' border=0 >");
document.write("<tr>");
document.write("<td colspan='2'valign='top'  align='left'><img src='../image/topname.jpg'></td>");
document.write("</tr>");
document.write("<tr bgcolor='#434343' >");
document.write("<td align='center' width='80%'>");
if (subvalue == "")
{
	document.write("<b><font color='#FFFFFF'>"+pagetitle+"</b><font>");
}
else
{
	document.write("<b><font color='#FFFFFF'>"+pagetitle+"</font></b> <font color='#FFFFFF'>["+subvalue+"]<font>");
}
document.write("</td >");
document.write("<td align = 'right' width='20%'>");
//document.write("<a href ='logout.jsp' target='_parent'><font face=arial size=2 color='#0066FF'> <b>Logoff</b></font></a>&nbsp; &nbsp; &nbsp;");
document.write("<a href ='../index.jsp' target='_parent'><font face=arial size=2 color='#0066FF'> <b>Logoff</b></font></a>&nbsp; &nbsp; &nbsp;");
document.write("</td>");
document.write("</tr>");
document.write("</table>");
document.write("</td>");
document.write("</tr>");
document.write("</table>");
document.write("</td>");
document.write("</tr>");
document.write("</table>");
}

function getstyle()
{
	document.write("<style>");
	document.write("A:link");
	document.write("{");
	document.write("COLOR: #0000A0;");
	document.write("TEXT-DECORATION: none");
	document.write("}");
	document.write("A:visited");
	document.write("{");
	document.write("COLOR: #8282FF;");
	document.write("TEXT-DECORATION: none");
	document.write("}");
	document.write("A:hover");
	document.write("{");
	document.write("COLOR: red;");
	document.write("TEXT-DECORATION: UNDERLINE");
	document.write("}");
	document.write("</style>");
}

//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)
{
//alert("Test");
//alert(field1);
//alert(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;


}

