/************************************************
	login.js
	used by login.xsl to handle login form
 ************************************************/
//this variable is for user's selected project name
var strSelectedProjectName = "";
//this is for Guest's userId
//which will be overwritten in login.xsl
var strGuestUserId = "GUEST";

//used by new opened window for look up password in ab-lookup-password.js
var strUserId = "";
var key = "0@1Z2y3X4^5w6*7=8-9+";

/////////////////////////////////////////
function openWindow(strURL){
	/*if(self != window.top)
	{
		//check if there is any opener is existing, if yes, close it
		if(window.top.opener != null)
		{
			//open url in opener's top window
			window.top.opener.top.location.href = strURL;
			//close self		
			window.top.close();
		}
		else
		{
			//open url in top window
			window.top.location.href = strURL;
		}
	}
	else
	{*/
		onPageLoaded();
	//}
}

//called when login page is loaded into browser
function onPageLoaded()
{
	var objForm = document.afmInputsForm;
	var userID = "";
	//javascript function getCookie() is in common.js
	userID = getCookie("afm_user_id_per_computer");
	if(objForm != null)
	{
		if(userID != null && userID != "")
		{
			//if there is a user_id stored in this computer
			//show it in user_id field
			objForm.elements['user_id'].value = userID;
			//make check box checked??
			objForm.elements['remember_user_id'].checked = 1;
			strUserId = userID;
		}
		//set project name
		if(objForm.elements['projects'] != null)
		{
			strSelectedProjectName = objForm.elements['projects'].value;
		}
		if(objForm.elements['user_id'].value=="")
			objForm.elements['user_id'].focus();
		else if(objForm.elements['user_pwd'].value=="")
			objForm.elements['user_pwd'].focus();
	}
	/////////////////////////////////////////
	var afm_user_language = getCookie("afm_user_language_per_computer");
	if(afm_user_language == null)
	{
		//default as en_US????
		afm_user_language = "it_IT";
		//set up cookies
		var today = new Date()  ;
		var expires = new Date() ;
		//one year?
		expires.setTime(today.getTime() + 1000*60*60*24*365);
		//set up cookies
		setCookie("afm_user_language_per_computer", afm_user_language, expires);
		//show it in the interface
		//document.getElementById('afm_user_language').value = afm_user_language;
	}else{
		//show it in the interface
		//document.getElementById('afm_user_language').value = afm_user_language;
	}
	//document.getElementById('comun').disabled = true;
}
//select language from list
function setUpUserLanguage(strLocaleName)
{
	var today = new Date()  ;
	var expires = new Date() ;
	//one year?
	expires.setTime(today.getTime() + 1000*60*60*24*365);
	//set up cookies
	setCookie("afm_user_language_per_computer", strLocaleName, expires);
	//refresh to get new language
	if (document.images)
		window.location.reload();
	else
		window.location.href = objFrame.location.href;
	
}

//called when project is selected in project list
function onSelectProject(projectName)
{
	strSelectedProjectName = projectName;
}

//called when clicking signIn buttons by SignedIn users or Guests
//bGuest=false;=> signedIn, otherwise => guest
function onLogin(strSerialized, bGuest)
{
	var objForm = document.afmInputsForm;
	var userID	= objForm.elements['user_id'].value;
	var userPWD = objForm.elements['user_pwd'].value;
	var bStore	= objForm.elements['remember_user_id'].checked;
	var strXML	= "";

	if(bGuest)
	{
		//setting userID to strGuestUserId, userPWD to "afm"		userPWD = "";
		userID = doSecure(strGuestUserId);

		//insert record into strSerialized
		strXML = generatingXMLDataString(strSerialized,strSelectedProjectName,userID,userPWD);
		
		//??????????????????????????
		///////////////////strXML = strSerialized;
		//check if strXML is empty, if not, send request to server
		if(strXML != "")
		{	
			//sending  requirement to server
			//???since this is a login form, target will be  _top???
			//javascript function sendingDataFromHiddenForm() is in
			//common.js
			sendingDataFromHiddenForm("", strXML, "", "", false,"");
		}

	}else{
		if(userID == "")
		{
			objForm.elements['user_id'].focus();
		}
		else
		{
			if(userPWD == "")
			{
				objForm.elements['user_pwd'].focus();
			}
			else
			{
				//store user-id?
				saveUserID(userID, bStore);
				//fixing 3008455
				userID = doSecure(userID);
				userPWD = doSecure(userPWD);

				//insert record into strSerialized
				strXML = generatingXMLDataString(strSerialized, strSelectedProjectName,userID,userPWD);

			}
		}
	}
	//check if strXML is empty, if not, send request to server
	if(strXML != "")
	{
		//sending  requirement to server
		//???since this is a login form, target will be  _top???
		//javascript function sendingDataFromHiddenForm() is in
		//common.js
		sendingDataFromHiddenForm("", strXML, "", "", false,"");
	}

}

//called to set up user's login information in this computer
//javascript function setCookie() is in common.js
function saveUserID(userID, bStore)
{
	if(bStore)
	{
		//set up cookies
		var today = new Date()  ;
		var expires = new Date() ;
		//one year?
		expires.setTime(today.getTime() + 1000*60*60*24*365); 
		setCookie("afm_user_id_per_computer", userID, expires);
	}
	else
	{
		//set the cookies to null
		setCookie("afm_user_id_per_computer", "", null);
	}
}

//forming xml data string to be sent to server
function generatingXMLDataString(strSerialized, projectName,userID,userPWD)
{
	var strXML  = "";
	var strData = "";
	if(strSerialized != "")
	{
		//calling setSerializedInsertingDataVariables() in
		//common.js to set up related JS variables in common.js
		//strSerializedStartTag, strSerializedCloseTag,
		//strSerializedInsertingDataFirstPart,strSerializedInsertingDataRestPart	
		setSerializedInsertingDataVariables(strSerialized);
		//strData =  strSerializedStartTag + 'records' + strSerializedCloseTag;
		strData =  strData + strSerializedStartTag + 'record ';
		strData =  strData + ' login.projectName="' + projectName + '" ';
		//if(userID != "")
			strData =  strData + ' login.userId="' + userID + '" ';
		//if(userPWD != "")
			strData =  strData + ' login.password="' + userPWD + '" ';

		strData =  strData + ' /' + strSerializedCloseTag;
		//strData =  strData + strSerializedStartTag + '/records' + strSerializedCloseTag;
		strXML = strSerializedInsertingDataFirstPart + strData + strSerializedInsertingDataRestPart;
	}
	return strXML;
}

function onLookUpPassword(strSerialized, strTarget, newWindowSettings)
{
	var objForm = document.afmInputsForm;
	var userID	= objForm.elements['user_id'].value;
	var strXML	= "";
	if(userID != "")
	{
		strXML = generatingXMLDataString(strSerialized, strSelectedProjectName,userID,"");
	}
	else
	{
		objForm.elements['user_id'].focus();
	}
	if(strXML != "")
	{
		//javascript function sendingDataFromHiddenForm() is in
		//common.js
		sendingDataFromHiddenForm("", strXML, strTarget, "", false,newWindowSettings);
	}
}
//enable the enter key for html form's input field only in simple html forms
//netscape
if(!document.all)
	document.captureEvents(Event.KEYDOWN);
	document.onkeydown = invokeSubmitButton;

function invokeSubmitButton(e)
{
	if (((!document.all)&&(e.which && e.which == 13))|| ((document.all)&&(event.keyCode && event.keyCode == 13)))
	{
		var objForm = document.afmInputsForm;
	//signIn is defined in login.xsl
		var signInButtonObj = objForm.elements["signIn"];
	//dont' use signInButtonObj.focus();
		signInButtonObj.click();
		return false;
	}
	else
		return true;
}
///////////////////////////////////////////////////////

function permutationGenerator(nNumElements) {
this.nNumElements     = nNumElements;
this.antranspositions = new Array;
var k = 0;
for (i = 0; i < nNumElements - 1; i++)
for (j = i + 1; j < nNumElements; j++)
this.antranspositions[ k++ ] = ( i << 8 ) | j;
// keep two positions as lo and hi byte!
this.nNumtranspositions = k;
this.fromCycle = permutationGenerator_fromCycle;
}
function permutationGenerator_fromCycle(anCycle) {
var anpermutation = new Array(this.nNumElements);
for (var i = 0; i < this.nNumElements; i++) anpermutation[i] = i;
for (var i = 0; i < anCycle.length; i++) {
var nT = this.antranspositions[anCycle[i]];
var n1 = nT & 255;
var n2 = (nT >> 8) & 255;
nT = anpermutation[n1];
anpermutation[n1] = anpermutation[n2];
anpermutation[n2] = nT;
}
return anpermutation;
}
function password(strpasswd) {
this.strpasswd = strpasswd;
this.getHashValue   = password_getHashValue;
this.getpermutation = password_getpermutation;
}
function password_getHashValue() {
var m = 907633409;
var a = 65599;
var h = 0;
for (var i = 0; i < this.strpasswd.length; i++) 
h = (h % m) * a + this.strpasswd.charCodeAt(i);
return h;
}
function password_getpermutation() {
var nNUMELEMENTS = 4;
var nCYCLELENGTH = 9;
pg = new permutationGenerator(nNUMELEMENTS);
var anCycle = new Array(nCYCLELENGTH);
var npred   = this.getHashValue();
for (var i = 0; i < nCYCLELENGTH; i++) {
npred = 11 * npred + 11;
anCycle[i] = npred % pg.nNumtranspositions;
}
return pg.fromCycle(anCycle);
}

function SecureContext(strText, strSignature, bEscape) {
this.strSIGNATURE = strSignature || '';
this.bESCApE      = bEscape || false;
this.strText = strText;
this.escape        = SecureContext_escape;
this.unescape      = SecureContext_unescape;
this.transliterate = SecureContext_transliterate;
this.encypher      = SecureContext_encypher;
this.decypher      = SecureContext_decypher;
this.sign          = SecureContext_sign;
this.unsign        = SecureContext_unsign;
this.secure   = SecureContext_secure;
this.unsecure = SecureContext_unsecure; 

}
function SecureContext_escape(strToEscape) {
var strEscaped = '';
for (var i = 0; i < strToEscape.length; i++) {
var chT = strToEscape.charAt( i );
switch(chT) {
case '\r': strEscaped += '\\r'; break;
case '\n': strEscaped += '\\n'; break;
case '\\': strEscaped += '\\\\'; break;
default: strEscaped += chT;
   }
}
return strEscaped;
}
function SecureContext_unescape(strToUnescape) {
var strUnescaped = '';
var i = 0;
while (i < strToUnescape.length) {
var chT = strToUnescape.charAt(i++);
if ('\\' == chT) {
chT = strToUnescape.charAt( i++ );
switch( chT ) {
case 'r': strUnescaped += '\r'; break;
case 'n': strUnescaped += '\n'; break;
case '\\': strUnescaped += '\\'; break;
default: // not possible
   }
}
else strUnescaped += chT;
}
return strUnescaped;
}
function SecureContext_transliterate(btransliterate) {
var strDest = '';

var nTextIter  = 0;
var nTexttrail = 0;

while (nTextIter < this.strText.length) {
var strRun = '';
var cSkipped   = 0;
while (cSkipped < 7 && nTextIter < this.strText.length) {
var chT = this.strText.charAt(nTextIter++);
if (-1 == strRun.indexOf(chT)) {
strRun += chT;
cSkipped = 0;
}
else cSkipped++;
}
while (nTexttrail < nTextIter) {
var nRunIdx = strRun.indexOf(this.strText.charAt(nTexttrail++));
if (btransliterate) {
nRunIdx++
if (nRunIdx == strRun.length) nRunIdx = 0;
}
else {
nRunIdx--;
if (nRunIdx == -1) nRunIdx += strRun.length;
}
strDest += strRun.charAt(nRunIdx);
   }
}
this.strText = strDest;
}
function SecureContext_encypher(anperm) {
var strEncyph = '';
var nCols     = anperm.length;
var nRows     = this.strText.length / nCols;
for (var i = 0; i < nCols; i++) {
var k = anperm[ i ];
for (var j = 0; j < nRows; j++) {
strEncyph += this.strText.charAt(k);
k         += nCols;
   }
}
this.strText = strEncyph;
}
function SecureContext_decypher(anperm) {
var nRows    = anperm.length;
var nCols    = this.strText.length / nRows;
var anRowOfs = new Array;
for (var i = 0 ; i < nRows; i++) anRowOfs[ anperm[ i ] ] = i * nCols;
var strplain = '';
for (var i = 0; i < nCols; i++) {
for (var j = 0; j < nRows; j++)
strplain += this.strText.charAt(anRowOfs[ j ] + i);
}
this.strText = strplain;
}
function SecureContext_sign(nCols) {
if (this.bESCApE) {
this.strText      = this.escape(this.strText);
this.strSIGNATURE = this.escape(this.strSIGNATURE);
}
var nTextLen     = this.strText.length + this.strSIGNATURE.length;
var nMissingCols = nCols - (nTextLen % nCols);
var strpadding   = '';  
if (nMissingCols < nCols)
for (var i = 0; i < nMissingCols; i++) strpadding += ' ';
var x = this.strText.length;
this.strText +=  strpadding + this.strSIGNATURE;
}
function SecureContext_unsign(nCols) {
if (this.bESCApE) {
this.strText      = this.unescape(this.strText);
this.strSIGNATURE = this.unescape(this.strSIGNATURE);
}
if ('' == this.strSIGNATURE) return true;
var nTextLen = this.strText.lastIndexOf(this.strSIGNATURE);
if (-1 == nTextLen) return false;
this.strText = this.strText.substr(0, nTextLen);
return true;
}
function SecureContext_secure(strpasswd) {
var passwd = new password(strpasswd);
var anperm   = passwd.getpermutation()
this.sign(anperm.length);
this.transliterate(true);
this.encypher(anperm);
}
function SecureContext_unsecure(strpasswd) {
var passwd = new password(strpasswd);
var anperm = passwd.getpermutation()
this.decypher(anperm);
this.transliterate(false);
return this.unsign(anperm.length);
}

function doSecure(str){
var afm_user_language = getCookie("afm_user_language_per_computer");

if(afm_user_language!="zh_CN" && afm_user_language!="zh_TW" && afm_user_language!="ko_KR" && afm_user_language!="ja_JP")
{
	str = str.toUpperCase();
}
var sc = new SecureContext(str, key, false);
sc.secure(key);
//Handle the special characters
var tmp = sc.strText;
tmp = tmp.replace(/&/g, '&amp;');
tmp = tmp.replace(/>/g, "&gt;");
tmp = tmp.replace(/</g, "&lt;");
tmp = tmp.replace(/\'/g, "&apos;");
tmp = tmp.replace(/\"/g, '&quot;');
return tmp;

}