
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

var subcacsp = new Array();
subcacsp["MSEB"] = "Rainbow iKey";
subcacsp["MSDCL"] = "Rainbow iKey";
subcacsp["ADWEB"] = "";
	
/** 
  *  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 checkPANNumber(str)
{
    //var isnotFirstFive="0123456789!@#$%^&*()+=|\~`?><,:;{[}]-_\"";  
    var isNotAlphabet = "abcdefghijklmnopqrstuvwxyz 0123456789!@#$%^&*()+=|\~`?><,:;{[}]-_\"";  
    var isNotNumber = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ !@#$%^&*()+=|\~`?><,:;{[}]-_\"";  
    var inval = 0;
    var x;
    var firstFive = str.substring(0,5);
    var secondFour = str.substring(5,9);
    var thirdOne = str.substring(9,10);
    if (str.length != 10)
    {
        inval = 1;
    }
    if (inval != 1)
    {
        for (var i = 0; i < firstFive.length; i++)
        {
            for (x = 0; x < isNotAlphabet.length; x++)
            {
                if (firstFive.charAt(i) == (isNotAlphabet.charAt(x)))
                {
                    inval = 1;
                    break;
                }
            }
        }
        if (inval != 1)
        {
            for (var i = 0; i < secondFour.length; i++)
            {
                for (x = 0; x < isNotNumber.length; x++)
                {
                    if (secondFour.charAt(i) == (isNotNumber.charAt(x)))
                    {
                        inval = 1;
                        break;
                    }
                }
            }
            if (inval != 1)
            {
                for (var i = 0; i < thirdOne.length; i++)
                {
                    for (x = 0; x < isNotAlphabet.length; x++)
                    {
                        if (thirdOne.charAt(i) == (isNotAlphabet.charAt(x)))
                        {
                            inval = 1;
                            break;
                        }
                    }
                }
            }
        }
    }
    return(inval);
}

/* 
	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 checkDnFormat(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 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 
	if(typeof(id1) != "undefined") id1.innerHTML = "";
	var imgMsg = "&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") id1.innerHTML = imgMsg;
			   return (msg.length==0)?txt:sep+txt;
		}
		else if(eval(fnName+"(str)") == 1 )
		{
			if(typeof(id1) != "undefined") id1.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") id1.innerHTML = imgMsg;
				return (msg.length==0)?txt:sep+txt;
			}
			else if(eval(fnName+"(str)") == 1)
			{
				if(typeof(id1) != "undefined") id1.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 with no spaces. It must contain atleast two special characters.",
				"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",
				"Invalid IP Address"
				);

	return text+" "+eMsgs[type];
}

function sessionExpire()
{
		var ans=confirm("Your session has expired. \n Go back to login page?");
		if(ans==true)
			location.href="../index.jsp";
}

function writeHeader()
{
	document.write("<tr><td class='celColourDarkGray'>");
	document.write("<table width='100%' border='0' cellspacing='0' cellpadding='0' height='100%'><tr>");
	document.write("<td colspan='3' class='tabheaderblue'>");
	document.write("<table width='100%' border='0' cellspacing='0' cellpadding='0'>");
	document.write("<tr><td>");
	document.write("<table width='100%'><tr>");
	document.write("<td align='left' class='boldBlack' width='33%'>User Id: "+userid+"</td>");
	document.write("<td align='right' class='boldBlack' width='33%'>RA Name: "+raName+"</td>");
	document.write("</tr></table>");
	document.write("</td></tr>");
	document.write("<tr><td colspan='3'><img width='10' height='5' src='images/transparent.gif'></td></tr>");
	document.write("</table></td></tr></table></td></tr>");
}

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 postURL(str,fileName)
{
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];
}

if(typeof(fileName) == 'undefined')
	document.write("<form name='sbmt' action='index.jsp' method='post'>");
else
	document.write("<form name='sbmt' action='"+fileName+"' method='post'>");
for(i=0;i<paramNames.length;i++)
{
	document.write("<input type='hidden' name='"+paramNames[i]+"' value='"+paramValues[i]+"'>");
}
document.write("</form>");
var obj = document.sbmt;
if(typeof(obj)=='undefined')
{
	var params="";
	for(i=0;i<paramNames.length;i++)
	{
		if(i==paramNames.length-1)
			params+=paramNames[i]+"="+paramValues[i];
		else
			params+=paramNames[i]+"="+paramValues[i]+"&";
		
	}

	if(typeof(fileName) == 'undefined')
		location.href="index.jsp?"+params;
	else
		location.href=fileName+"?"+params;
}	
else
	document.sbmt.submit();
}

function splitDn(str)
{
	var splitParams = str.split(",");
	//var Arr = new Array("CN","EmailAddress","O","OU","L","ST","C");
	var arr = new Array();
	arr["CN"] = "Common Name";
	arr["EMAILADDRESS"] = "E-mail Address";
	arr["O"] = "Organisation";
	arr["OU"] = "Organisation Unit";
	arr["L"] = "Locality/ City";
	arr["ST"] = "State";
	arr["C"] = "Country";
	
	for(i=0;i<splitParams.length;i++)
	{
		var tmp = splitParams[i];
		var tmpSplit = tmp.split("=");
		document.write('<tr><td width="30%" class="rowColour1">'+ arr[trim(tmpSplit[0])] +'</td>');
     document.write(' <td width="70%" class="rowColour1"><span class="boldBlack"><b>'+tmpSplit[1] +'</b></span></td>         </tr> '); 
	//	document.write(arr[trim(tmpSplit[0])] + " = " + tmpSplit[1] + "<br>");	
	}
}

function splitDnValue(str,name)
{
	var splitParams = str.split(",");
	//var Arr = new Array("CN","EmailAddress","O","OU","L","ST","C");
	var arr = new Array();
	arr["CN"] = "Common Name";
	arr["EmailAddress"] = "E-mail Address";
	arr["O"] = "Organisation";
	arr["OU"] = "Organisation Unit";
	arr["L"] = "Locality/ City";
	arr["ST"] = "State";
	arr["C"] = "Country";
	
	for(i=0;i<splitParams.length;i++)
	{
		var tmp = splitParams[i];
		var tmpSplit = tmp.split("=");
		if(trim(tmpSplit[0])==name)
		{
			return tmpSplit[1];
		}
	}		
//document.write('<tr><td width="30%" class="rowColour1">'+ arr[trim(tmpSplit[0])] +'</td>');
     //document.write(' <td width="70%" class="rowColour1"><span class="boldBlack"><b>'+tmpSplit[1] +'</b></span></td>         </tr> '); 
	//	document.write(arr[trim(tmpSplit[0])] + " = " + tmpSplit[1] + "<br>");	
	
}


function certificateDetails(sNo, dn)
{
	document.write('<tr><td colspan="2" class="bodytest" align="center"> Your Digital Certificate Information       </td></tr>');
    splitDn(dn);          
     document.write('<tr><td width="30%" class="rowColour1">Serial Number</td>');
     document.write(' <td width="70%" class="rowColour1"><span class="boldBlack"><b>'+sNo +'</b></span></td>         </tr> ');       
}

function createLink(params, txt, classId, 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 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>");
}

function errorDisplay(message)
{
	document.write('<table width="100%" border="0" cellspacing="0" cellpadding="0" >');
	document.write('<td class="celColourDarkGray">');
    document.write('<table width="100%" border="0" cellspacing="0" 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 replaceAll( str, from, to ) 
{
	var idx = str.indexOf( from );
	while ( idx > -1 ) 
	{
		str = str.replace( from, to );
		idx = str.indexOf( from );
	}
	return str;
}
