//--------------------------

function encrypt(pstring) {

//--------------------------
// Return true for null strings
if (typeof pstring != "string") return "";

pencrypt = new Array();
preturn = "";
pchar = "";

// Turn string to be encrypted into array
for (i=0;i<pstring.length;i++) {	
	pencrypt[i]= pstring.charCodeAt(i); }

// Apply encryption char by char
for (i=pstring.length-1;i>=0;i--) {	
	preturn = preturn+pencrypt[i];}

return preturn;

}

//-----------------------------
// authUser
//-----------------------------
function authUser(puser,ppwd) {

//document.getElementById('MAINMSG').innerHTML = '';
// Nullify any existing browser cookies
setBrowserCookie("userId","");
setBrowserCookie("userFullName","");
setBrowserCookie("tokenValue","");
setBrowserCookie("userType","");
setBrowserCookie("client","");

// Send the SQL
var sqlURL = "authenticate.htm?userName="
		        +escape(puser)
      		  +"&userPassword="
//            +ppwd;
		        +encrypt(ppwd.toUpperCase());
		        
//alert(sqlURL);
ajaxExecuteAuth(sqlURL);

}

//--------------------------------

function ajaxExecuteAuth(URL) {  
  
//--------------------------------

    var req = initRequest();
    req.onreadystatechange = function() {
       if (req.readyState == 4) {
           parseAuthMessage(req);
       }
    };
    req.open("GET", URL, true);
    req.send(null);
  }
  
  

//-------------------------------

function parseAuthMessage(req) {

//-------------------------------

//alert(req.responseText);
var authResult = req.responseText;

resultArray = authResult.split("~");

authUserId = resultArray[1];
authUserFullName = resultArray[2];
authTokenValue = resultArray[3];
authUserType = resultArray[4];
authClient = resultArray[5];

//if (resultArray[0].match("Welcome")) {
if (Math.abs(resultArray[1]) > 0) 
{
  setBrowserCookie("userId",authUserId);
  setBrowserCookie("userFullName",authUserFullName);
  setBrowserCookie("tokenValue",authTokenValue);
  setBrowserCookie("userType",authUserType);
  setBrowserCookie("client",authClient);  
  setBrowserCookie("tooltips",1);

    close_form(document.getElementById('LOGONDIV'));
    
    if (window.location.port != 80) 
    {
        redirectURL="http://"+window.location.hostname+":"+window.location.port+"/tgmprint/main.htm";
    } else {
        redirectURL="http://"+window.location.hostname+"/tgmprint/main.htm";
    }
    document.location.href=redirectURL;
}
else 
{
  failedlogins = getBrowserCookie("failedLogins");
  if (failedlogins == "") failedlogins = 0;
  failedlogins = parseInt(failedlogins)+1;
  setBrowserCookie("failedLogins",failedlogins);
  document.getElementById('LOGONMSG').innerHTML = "Invalid logon. Forgotten your user name or password? Please call TGM on 759 8957";

  // Die if 3 attempts have been made
  if (Math.abs(failedlogins) > 2) 
  {
    alert("You have failed to login 3 times, closing browser"); 
    top.opener=null;top.close();return false; 
  }

} //else
}

//-----------------------------
// MENU
//-----------------------------
function menu () {

//1. call menu bean passing user id as parameter.
//bean will return menu name, giff, link for available menus for this user

// Send the SQL
var sqlURL = "menu.htm?"
   +"userId="
   +getBrowserCookie("userId")
	 +"&tokenValue="
	 +getBrowserCookie("tokenValue")
   +"&userType="
   +getBrowserCookie("userType")
   +"&userFullName="
   +getBrowserCookie("userFullName")
   +"&rand="+Math.random()*5;

//alert(sqlURL);

ajaxExecuteMenu(sqlURL);

}

//--------------------------------

function ajaxExecuteMenu(URL) {  
  
//--------------------------------

    var req = initRequest();
    req.onreadystatechange = function() {
       if (req.readyState == 4) {
           parseMenuMessage(req);
       }
    };
    req.open("GET", URL, true);
    req.send(null);
  }

//--------------------------------
  
function parseMenuMessage(req) {

//--------------------------------

divContents = req.responseText;

//temp work around for lots of menu items:
if (divContents.indexOf("WAZZA123") > -1)
{
  document.getElementById("MENUBIGDIV").innerHTML = divContents.substring(divContents.indexOf('WAZZA123')+8, divContents.indexOf('WAZZA456'));

  var contents = divContents.substring(0,divContents.indexOf('WAZZA123'));
  contents = contents + divContents.substring(divContents.indexOf('WAZZA456')+8,divContents.length);
  
  document.getElementById("MENUDIV").innerHTML = contents;
}
else
{
  //3. write this string to the div
  document.getElementById("MENUDIV").innerHTML = divContents;
}

}