
function encryptPassword(textPassword) 
{
	var num_out = "";
	var str_in = escape(textPassword);
	for(i = 0; i < str_in.length; i++) 
	{
		num_out += str_in.charCodeAt(i) - 23;
	}
	return num_out;		
}

function decryptPassword(encPassword) 
{
	var str_out = "";
	var num_out = encPassword;  
	for(i = 0; i < num_out.length; i += 2) 
	{
		num_in = parseInt(num_out.substr(i,[2])) + 23;
		num_in = unescape('%' + num_in.toString(16));
		str_out += num_in;
	}
	var textPassword = unescape(str_out);
	return textPassword ;
}



function checkForNull(form)
{
	if(document.login.j_username.value == "" || document.login.j_password.value == "")
	{
		alert(document.getElementById("jserror").value);
		if(document.login.j_username.value=="")
		{
			document.login.j_username.focus();
		} 
		else
		{ 
			document.login.j_password.focus();
		}
		return false;
	}

	var expDate = new Date();
	var thisCookie;  
	if(document.login.checkbox.checked)
	{  
		expDate.setTime(expDate.getTime()+(24*60*60*1000*365));
		document.cookie="username= "+document.login.j_username.value+";expires= "+((expDate).toGMTString());
		//Encrypting the password for Cookie
		var textPassword = document.login.j_password.value ;
		var encPassword = encryptPassword(textPassword);
		document.cookie="password= "+ encPassword +";expires= "+((expDate).toGMTString());
	}
	else
	{   
		expDate.setTime(expDate.getTime());
		document.cookie="username= "+document.login.j_username.value+";expires= "+((expDate).toGMTString());       
		document.cookie="password= "+document.login.j_password.value+";expires= "+((expDate).toGMTString());
	}

	/*
	*
	* Opertions to be performed for Domain List 
	*
	*/
	var domainList = document.login.domain;
	if(domainList!=null)
	{
		if(domainList.value == "")
		{
			alert("Select a Domain.");
			domainList.focus();
			return false;
		}

		if(document.login.checkbox.checked)
		{  
			expDate.setTime(expDate.getTime()+(24*60*60*1000*365));
		}
		else
		{   
			expDate.setTime(expDate.getTime());
		}
		
		document.cookie="domainname= "+domainList.value+";expires= "+((expDate).toGMTString());       

	}

	
	/* ## To convert username to lowercase ## */
	document.login.username.value=document.login.username.value.toLowerCase();
		
	return true;
}

function setFocus(name)
{
        name.focus();
}

function getCookie()
 {  
    document.login.j_username.value="";
    document.login.j_username.focus();    
    init = (document.cookie).indexOf("username");        
    if(init != -1 )
    {
        userlen = "username".length;        
        beginIndex = ((document.cookie).indexOf("username")+userlen);        
        endIndex = (document.cookie).indexOf(";",beginIndex);
		if(endIndex == -1)
		{
			endIndex = (document.cookie).length;
		}
        username=(document.cookie).substring(beginIndex+1,endIndex);
        startIndex = ((document.cookie).indexOf("password")+"password".length);
        endInd = (document.cookie).indexOf(";",startIndex); 
        if(endInd == -1)
        {
            endInd=(document.cookie).length;
        }
        //Decrypting encrypted password..
        var encPassword=(document.cookie).substring(startIndex+1,endInd);
		password = decryptPassword(encPassword);
        document.login.j_username.value=username;
        document.login.j_password.value=password;
        document.login.checkbox.checked=true;
	
		/**********
		*  If domainList is available, cookie will be set 
		************/
		domainList = document.login.domain;
		if(domainList!=null)
		{
			domainStartIndex = ((document.cookie).indexOf("domainname")+"domainname".length);
			domainEndIndex = (document.cookie).indexOf(";",domainStartIndex); 
			if(domainEndIndex == -1)
			{
				domainEndIndex=(document.cookie).length;
			}
			domain=(document.cookie).substring(domainStartIndex+1,domainEndIndex);
			
			var domainCount = domainList.length;
			for(i=0; i<domainCount; i++)
			{
				var data = domainList[i].value;
				if(data == domain)
				{
				   domainList[i].selected = true;
				   break
				}
			}
		}
		//************ End of DomainList Operation
    }

}
