function Get_Cookie(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}
 
function Set_Cookie(name,value,expires,path,domain,secure) {
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") + 
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}
 
var today = new Date();
var expires = new Date(today.getTime() + (21 * 86400000));
 
function set() {
    if (document.forms[0].save.checked) {
        Set_Cookie("Userid",document.forms[0].Userid.value,expires);
        Set_Cookie("Password",document.forms[0].Password.value,expires);
        }
    if (document.forms[0].save.checked==false) {
        Set_Cookie("Userid","",expires);
        Set_Cookie("Password","",expires);
        }
}

function get() {
    Userid = Get_Cookie("Userid")
    if (Userid != null) {
        document.forms[0].Userid.value = Userid;
        document.forms[0].save.checked = true;
    }
    Password = Get_Cookie("Password")
    if (Password != null) {
        document.forms[0].Password.value = Password;
        document.forms[0].save.checked = true;
    }
}

function EnDeCryptJS(myStr, strPwd) 
{
	//RC4 Encryption/Decryption 
 	var EnCryptStr;
	
	if (myStr.length == 0 || strPwd.length == 0) {
		EnCryptStr = "";
		return EnCryptStr;
	}	
	
	var key = new Array();
	var sbox = new Array();
	var tempSwap, temp;
	var a, b, c, i, j, k;
	var cipherby, cipher;
	var intLength;

	b = 0;
    i = 0;
    j = 0;
	cipher = "";

    intLength = strPwd.length;
    
	//Initialize arrays
	for (a = 0; a <= 255; a++)
	{
    	key[a] = strPwd.substring(a % intLength, a % intLength + 1).charCodeAt(0);		
        sbox[a] = a;
	}
	
	for (a = 0; a <= 255; a++)
	{
        b = (b + key[a] + sbox[a]) % 256;
        tempSwap = sbox[a];
        sbox[a] = sbox[b];
        sbox[b] = tempSwap;		
    }
	
	//Encrypt/Decrypt
	for (c = 1; c <= myStr.length; c++)
	{
		i = (i + 1) % 256;
        j = (j + sbox[i]) % 256;
        temp = sbox[i];
        sbox[i] = sbox[j];
        sbox[j] = temp;
   
        k = sbox[(sbox[i] + sbox[j]) % 256];

        cipherby = myStr.substring(c - 1, c).charCodeAt(0) ^ k;
		cipher = cipher + String.fromCharCode(cipherby);
     }
	  
	return cipher;

}


function EncodeToHEXJS(sURL)
{	
	var EncodeToHEX;
	
	if (sURL.length == 0)
	{
		return "";
	}
	else	   	
	{
		//Convert characters to 2-digit ASCII HEX separated by _ sign
		var sOutput, str, i;
		sOutput = "";
		
      	for (i = 0; i < sURL.length; i++)
		{
			str = sURL.substring(i, i+1).charCodeAt(0).toString(16);
			
			if (str.length < 2)
			{ 
				str = Array(2 - str.length + 1).join("0") + str;	
			}			
			
        	if (sOutput.length > 0)
			{
				sOutput = sOutput + "_" + str;
			}
			else				
			{
				sOutput = str;
			}
      	} 
		
    	return sOutput;		
	}	
}


function DecodeFrHEXJS(sURL)
{  
    if (sURL.length == 0)
	{
		return "";
	}
	else
    {
		//Convert 2-digit ASCII HEX (separated by _ sign) back to characters
		var sOutput, aSplit, i;
		sOutput = ""
		  	
    	aSplit = sURL.split("_");
		   	   		
      	for (i = 0; i < aSplit.length; i++)
		{
        	sOutput = sOutput + String.fromCharCode("0x" + aSplit[i]);
      	}
		
    	return sOutput;
	}	
}


function DoAllThese(myRndNo)
{
	var HashedPsw;
	var myKey, myPsw;
	var str;
	
	set();
	myKey = document.frmLogin.Password.value + myRndNo;
	myPsw = myRndNo + document.frmLogin.Password.value;
	HashedPsw = EncodeToHEXJS(EnDeCryptJS(myPsw, myKey));
	document.getElementsByName("Password").value = "";
	document.getElementById("HashedPsw").value = HashedPsw;
}


function whatIsThis(){
alert("Checking this box will put a cookie on your computer that will remember your User name and Password. Use this feature only if you are sure your computer is secure.")
}
