
var isNS4 = (document.layers) ? true : false; //to detect if the browser is NS4
var isNS6 = (!document.all && document.getElementById) ? true : false; //to detect if the browser is NS6 and above

/** 
  *  Function to detect the type of the Browser
  *  @return Name of the browser to detect the type of the Browser
  */

function browserDetect()
{
    var browsername = navigator.appName;
	if (browsername.indexOf("Netscape") != -1)
    {
 		browsername = "NS"
    }
    else
    {
        if (browsername.indexOf("Microsoft") != -1)
        {
            browsername = "MSIE"
        }
        else
        {
            browsername = "N/A"
        }
    }
    return(browsername);
}                      

/** 
  *  Function to check if the given character is spaces or not
  *  @param inChar the character to check
  *  @return true if is space 
  *  @return false if is not space
  */

function isSpace(inChar)
{
    return (inChar == ' ' || inChar == '\t' || inChar == '\n');
}


/** 
  *  Function to trim the spaces of the given string
  *  @param tmpStr the String to trim the spaces
  *  @return the trimed string
  */

function trim(tmpStr)
{
    var atChar;
	tmpStr = new String(tmpStr);
    if (tmpStr.length > 0)
    {
        atChar = tmpStr.charAt(0);
    }
    while (isSpace(atChar))
    {
        tmpStr = tmpStr.substring(1,tmpStr.length);
        atChar = tmpStr.charAt(0);
    }
    if (tmpStr.length > 0)
    {
        atChar = tmpStr.charAt(tmpStr.length - 1);
    }
    while (isSpace(atChar))
    {
        tmpStr = tmpStr.substring(0,(tmpStr.length - 1));
        atChar = tmpStr.charAt(tmpStr.length - 1);
    }
    return tmpStr;
}

/** 
  *  Function to check if the string is empty or not
  *  @param str the String to check 
  *  @return 1 if str is empty
  *  @return 0 if str is not empty
  */

function stringIsEmpty(str)
{
    var retVal = 0;
	if ((trim(str)).length == 0)
    {
 		retVal = 1;
    }
	return retVal;    
}
/** 
  *  Function to check if the value entered is 0 
  *  @param str
  *  @return 1 :- indicates is 0, 0 :- indicates not 0 
  */

function checkValue(str)
{
    var isZero = 0;
    if (str == '0' || str == 0)
        isZero = 1;

    return isZero;
}

function isAlphabetic(chr)
{
    var inval = 1;
    var isNotAlpha = "1234567890-+=_~`<,>./?:;\"\'{[}]|\()&^%$#@!* ";

    for (x = 0; x < isNotAlpha.length; x++)
    {
        if (chr.charAt(0) == isNotAlpha.charAt(x))
            inval = 0;
    }
    return(inval);
}


/** 
  *  Function to check if the string contains any alpha numeric or not
  *  @param str the Numeric String to check 
  *  @return 1 if str contains alpha numeric 
  *  @return 0 if str contain only numbers
  */

function checkNumber(str)
{
    var isnot = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()+=|-\~`?>.<,':;{[}]_ ";  
    var inval = 0;
    var x;
    for (var i = 0; i < str.length; i++)
    {
        for (x = 0; x < isnot.length; x++)
        {
            if (str.charAt(i) == (isnot.charAt(x)))
            {
                inval = 1;
            }
        }
    }
    return(inval);
}
/** 
  *  Function to check if the string contains any alpha numeric or not
  *  @param str the Numeric String to check 
  *  @return 1 if str contains alpha numeric 
  *  @return 0 if str contain only numbers
  */

function checkPhoneAndFax(str)
{
    var isnot = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*=|\~`?>.<,':;{[}]";  
    var inval = 0;
    var x;
    for (var i = 0; i < str.length; i++)
    {
        for (x = 0; x < isnot.length; x++)
        {
            if (str.charAt(i) == (isnot.charAt(x)))
            {
                inval = 1;
            }
        }
    }
    return(inval);
}


/** 
  *  Function to check the data validity 
  *  @param day   day of the date 
  *  @param month   month of the date 
  *  @param year  year of the date 
  *  @return 1 if it is invalid date
  *  @return 0 if it is valid date
  */

function validateDate(day, month, year)
{
    var arrMonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
    var err;
    var retVal = 0;
    if (day == "0" || month == "0" || year == "0")
	{
		return 1;
	}
	if ((day > 30) &&
        ((month == "APR") ||
         (month == "JUN") ||
         (month == "SEP") ||
         (month == "NOV")))
    {
        retVal = 1;
    }

    if (month == "FEB")
    {
        if (day >= 30)
            retVal = 1;
        else
        {
            flg = leapYearTest(year);
            if (flg)
            {
                if (day > 28)
                    retVal = 1;
            }
            else
            {
                if (day > 29)
                    retVal = 1;
            }
        }
    }

    return retVal;
}


/** 
  *  Function to check the leap year test
  *  @param year  year of the date 
  *  @return 1 if it is not leap year
  *  @return 0 if it is leap year 
  */

function leapYearTest(year)
{
    if (((year % 4 == 0) && (year % 100 != 0)) || year % 400 == 0)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}


/** 
  *  Function to check the Email address Syntax
  *  @param mail  string containing the mail address
  *  @return 0 if it has valid E-mail Syntax
  *  @return 1 if it does not have valid E-mail Syntax
  */

function checkEmail(mail)
{
    var isnot = "!#$%^&*()+=|,\\~`?><:;{[}]\"\'";  
    var inval = 0;
    var x;

        for (var i = 0; i < mail.length; i++)
        {
            for (x = 0; x < isnot.length; x++)
            {
                if (mail.charAt(i) == (isnot.charAt(x)))
                {
                    inval = 1;
                }
            }
        }
    if(inval == 1)
		return(inval);
	
	var result = 1;
    var email = mail;
    var theStr = new String(email)
    var index = theStr.indexOf("@");
	var indexLast =  theStr.lastIndexOf("@");
    var indexDot =  theStr.indexOf("..");
	var indxComma = theStr.indexOf(",");
    var AtArr = theStr.split("@");
    var lastChar = theStr.charAt(theStr.length - 1);
    /*** If last Char is not an @ or . ****/
    if (lastChar != "@" && 
	    lastChar != "." && 
		indxComma == -1 && 
		index > 0 && 
		index == indexLast && 
		indexDot == -1  ) 
    {
		
		var pindex = theStr.indexOf(".",index);
		if ((pindex > index + 1) && (theStr.length > pindex + 1))
		{
			if (AtArr.length != 2)
				result = 1;
			else
				result = 0;
		}
    }
   return result;
}


/**
  * Function to check that Common Name is in the proper format or not.
  * Names can contain only . / - _ and must begin with an Alphabet.
  */
function checkNameFormat(name)
{
    var isnot = "!@#$%^&*()+=|,\\/~`?><:;{[}]\"";  
    var inval = 0;
    var x;

        for (var i = 0; i < name.length; i++)
        {
            for (x = 0; x < isnot.length; x++)
            {
                if (name.charAt(i) == (isnot.charAt(x)))
                {
                    inval = 1;
                }
            }
        }
     return(inval);
}

function checkAlphaNumeric(str)
{
    var isnot = "!@#$%^&*()+=|\~`?><,:;{[}]-_\"";
    var inval = 0;
    var x;
    for (var i = 0; i < str.length; i++)
    {
        for (x = 0; x < isnot.length; x++)
        {
            if (str.charAt(i) == (isnot.charAt(x)))
            {
                inval = 1;
            }
        }
    }
    return(inval);
}




/**
  * Function to check that PAN Number is in the proper format or not.
  */


/* 
	Function to check if the password is in secured or not.
	@param pwd - password to check
	@return 0 if password is secured
	@return 1 if unsecure
*/

function checkPassword(pwd)
{
	
  	pwd = trim(pwd);
	if(pwd.length < 8)
		return 1;
	if(pwd.indexOf(" ") > 0)
		return 1;
	return 0;	
	
}


/*
	Function is used to call any function which returns 0 or 1
	@param fnName - name of the funtion
	@param str - string to check for errors
	@param msg - variable to check if form alerady contains errors or not
	@param txt - the error message 
	@param sep - character which separates two error messages
	@param opt - if true validation is done on a optional field
	@return zero length string it str does not contains any errors
	@return txt if str validation is failed
 */


function validateField(fnName, str, msg, txt, sep, opt, id1)
{
	
	// if sep is undefined , is used as seperator 
	var id2;
	if(browserDetect()=="MSIE")
	{
		if(typeof(id1) != "undefined") 
		{
			id2 = document.getElementById(id1);
			id2.innerHTML = "";
		}
	}
	var imgMsg = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='images/2.gif' valign='middle' border=0>";
	if(typeof(sep) == "undefined")
		sep=", ";
	
	if(typeof(opt) == "undefined")
		opt = false;

	if(opt == false)
	{
		if(fnName=="validateDate" && eval(fnName+"("+str+")") == 1) 
		{
			if(typeof(id1) != "undefined" && browserDetect()=="MSIE") id2.innerHTML = imgMsg;
			return (msg.length==0)?txt:sep+txt;
		}
		else if(eval(fnName+"(str)") == 1 )
		{
			if(typeof(id1) != "undefined" && browserDetect()=="MSIE") id2.innerHTML = imgMsg;
			return (msg.length==0)?txt:sep+txt;
		}
		else
		{
			return "";
		}
	}
	else if(opt == true)
	{
		if(stringIsEmpty(str) == 1  || str == "0,\"0\",0")
		{
			return "";
		}
		else
		{
			if(fnName=="validateDate" && eval(fnName+"("+str+")") == 1) 
			{
				if(typeof(id1) != "undefined" && browserDetect()=="MSIE") id2.innerHTML = imgMsg;
				return (msg.length==0)?txt:sep+txt;
			}
			else if(eval(fnName+"(str)") == 1)
			{
				if(typeof(id1) != "undefined" && browserDetect()=="MSIE") id2.innerHTML = imgMsg;
				return (msg.length==0)?txt:sep+txt;
			}
			else
			{
				return "";
			}
		}
	}
}

/*
	function which contains all error messages 
*/

function messages(text,type)
{
	eMsgs=new Array("must have only alphabets, numbers, spaces, dots, - and '.",
                "must have only alphabets, spaces, dots and numbers.",
			    "must contain only numbers, - and ().",
			    "must contain only numbers.",
			    "must be a valid one.", 
			    ": fill all the details",
				"must contain a minimum of 8 characters without spaces.",
				"entered should contain only alphabetical or numerical characters and please do not use any of these symbols\, ! @ $ % ^ & * ( ) ~ ?> < / \"",
				"needs to be in the format of name@domain.com");

	return text+" "+eMsgs[type];
}


function getStr(str)
{
	if(str=="null")
		return "";
	else
		return str;
}

function getIndexFromList(lst,str)
{
	for(i=0;i<lst.options.length;i++)
		if(lst.options[i].value == str)
			break;
	return i;
}

function getDateStr(day, month, year)
{
	dayIndex   = day.selectedIndex;
	monthIndex = month.selectedIndex;
	yearIndex  = year.selectedIndex;
	tday = day.options[dayIndex].value;
	tmonth = month.options[monthIndex].value;
	tyear = year.options[yearIndex].value;
	dateStr = tday + tmonth + tyear;
	if(dateStr == "000")
		dateStr = "";
	return trim(dateStr);
}

function empty(val)
{
	if(val == "0")
		return "";
	else
		return val;
}

function popup(file,wdt,hgt)
{
	if(typeof(wdt)=="undefined")
		wdt = 300;
	if(typeof(hgt)=="undefined")
		hgt = 400;
	if(typeof(myWindow)=='object')
		if(!myWindow.closed)	
			myWindow.close();
	myWindow = window.open(file, "help", "fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width="+wdt+",height="+hgt)
}

function errorDisplay(message)
{
	document.write('<img src="images/transparent.gif" width="2" height="10"><br>');
    document.write('<table width="771" border="0" cellspacing="0" cellpadding="0" >');
	document.write('<tr><td background="images/headerBack.gif" class="boldBlack" height="22">&nbsp;&nbsp;</td></tr>');
	document.write('<td class="celColourDarkGray">');
    document.write('<table width="100%" border="0" cellspacing="1" cellpadding="40" height="370">');
	document.write('<tr><td class="whiteBack" align="center" valign="top">'); 
	document.write('<table width="80%" border="0" cellspacing="1" cellpadding="0" align="center">');
    document.write('<tr><td class="celColourDarkGray">');
	document.write('<table width="100%" border="0" cellspacing="1" cellpadding="4">');
    document.write('<tr><td class="whiteBack" ><font size="+1">'+message+'</font></td>');
	document.write('<td class="active_in" width="6%"><a href="javascript:history.go(-1)" class="topLink">Back</a></td>');
    document.write('</tr></table></td></tr></table></td></tr></table></td></tr></table>');
}



function createButtonLink(params, txt, frmName)
{
  var str=params;
  var splitParams = str.split("&");
  var paramNames = new Array();
  var paramValues = new Array();
  
  for(i=0;i<splitParams.length;i++)
  {
     var tmp = splitParams[i];
     var tmpSplt = tmp.split("=");
     paramNames[i] = tmpSplt[0];
     paramValues[i] = tmpSplt[1];
  }

  temp="<form name='"+frmName+"' action='index.jsp' method='post'>";

  for(i=0;i<paramNames.length;i++)
  {
   temp+="<input type='hidden' name='"+paramNames[i]+"' value='"+paramValues[i]+"'>";
  }
  temp+="<input type='button' value='"+txt+"' onClick='javascript:document."+frmName+".submit()'>"
  temp+="</form>";
  document.write(temp);
  
  
}


function createForm(params, frmName)
{
  var str=params;
  var splitParams = str.split("&");
  var paramNames = new Array();
  var paramValues = new Array();
  
  for(i=0;i<splitParams.length;i++)
  {
     var tmp = splitParams[i];
     var tmpSplt = tmp.split("=");
     paramNames[i] = tmpSplt[0];
     paramValues[i] = tmpSplt[1];
  }

  temp="<form name='"+frmName+"' action='index.jsp' method='post'>";

  for(i=0;i<paramNames.length;i++)
  {
   temp+="<input type='hidden' name='"+paramNames[i]+"' value='"+paramValues[i]+"'>";
  }
  temp+="</form>";
  document.write(temp);
//  document.write("<a href='javascript:document."+frmName+".submit()' class='"+classId+"'>"+txt+"</a>");
  
}


function createLink2(params, txt, frmName)
{
  var str=params;
  var splitParams = str.split("&");
  var paramNames = new Array();
  var paramValues = new Array();
  
  for(i=0;i<splitParams.length;i++)
  {
     var tmp = splitParams[i];
     var tmpSplt = tmp.split("=");
     paramNames[i] = tmpSplt[0];
     paramValues[i] = tmpSplt[1];
  }

  temp="<form name='"+frmName+"' action='index.jsp' method='post'>";

  for(i=0;i<paramNames.length;i++)
  {
   temp+="<input type='hidden' name='"+paramNames[i]+"' value='"+paramValues[i]+"'>";
  }
  temp+="</form>";
  document.write(temp);
  document.write("<a href='javascript:document."+frmName+".submit()'>"+txt+"</a>");
}