//login scripts
//this script requires jsAjax.js and modal-message/js/modal-message.js Login.php LoginAjax.php


messageObj = new DHTML_modalMessage();  // We only create one object of this class
messageObj.setShadowOffset(5);  // Large shadow

//if enter key pressed at DropDownList

function onEnterpress(e)
{
    //define any varible
     var KeyPress
     //if which property of event object is supported
     if(e && e.which)
     {
         e = e
         //character code is contained in NN4's which property
         KeyPress = e.which
     }
     else
     {
        e = event
         KeyPress = e.keyCode
     }
     //13 is the key code of enter key
     if(KeyPress == 13)

     {
         //submit
         subLoginForm();
     }
}

function ShowLogonBox() {
//display login box
var html = "<a href=\"javascript:closeMessage();\"><img src=\"../../close.png\" alt=\"close\" style=\"float:right;position:relative;top:0px;\" title=\"\"/></a><br /><h1>Log in</h1>"+
"<div style = \"text-align:left; margin-left: 3em;\">"+
"<form name = \"Log\">"+
"Username &nbsp <input type=\"text\" size = \"15\" name=\"username\" /><br /><br />"+
"Password &nbsp <input type=\"password\" size = \"15\" name=\"Pword\" onKeyPress = \"javascript:onEnterpress(event);\"><br /><br />"+

"<div class =\"button\" ><a class =\"button\" href=\"javascript:subLoginForm();\" id = \"login\"><span>Log in</span></a></div>"+

"</form>"+
"</div>";

displayStaticMessage(html, false)
//cookies and focus
  cookie()
}

/*
function displayMessage(url)
{
  
  messageObj.setSource(url);
  messageObj.setCssClassMessageBox(false);
  messageObj.setSize(400,200);
  messageObj.setShadowDivVisible(true);  // Enable shadow for these boxes
  messageObj.display();
}
*/

function displayStaticMessage(messageContent,cssClass)
{
  messageObj.setHtmlContent(messageContent);
  messageObj.setSize(300,220);
  messageObj.setCssClassMessageBox("modalDialog_loginDiv");
  messageObj.setSource(false);  // no html source since we want to use a static message here.
  messageObj.setShadowDivVisible(true);  // Enable shadow for these boxes
  messageObj.display();


}

function closeMessage()
{
  messageObj.close();  
}

function displayLoggedInMessage(messageContent,cssClass)
{
  messageObj.setHtmlContent(messageContent);
  messageObj.setSize(300,100);
  messageObj.setCssClassMessageBox("modalDialog_loginDiv");
  messageObj.setSource(false);  // no html source since we want to use a static message here.
  messageObj.setShadowDivVisible(true);  // Enable shadow for these boxes
  messageObj.display();


}

function subLoginForm() {
if (validLoginForm(document.Log)) {
expireDate  = new Date
   expireDate.setMonth(expireDate.getMonth()+ 1)
   username = document.Log.username.value
   document.cookie = "username=" + username + ";expires=" + expireDate.toGMTString()

//document.Log.submit();{
username = document.Log.username.value;
Pword = document.Log.Pword.value;

var oParams = {
                "callback": "LoginResult",
                "username": username,
                "Pword" : Pword

            };

            makeRequestPost("../../LoginAjax.php", oParams);
//end fucntion jsPopulatePorts


}
}

function validLoginForm(form) {
if (form.username.value == "" || form.Pword.value == "") {
alert("You must enter a valid username and password.");
return false;
}
return true
}

function cookie() {
if (document.cookie != ""){
   document.Log.username.value = cookieVal("username");
   document.Log.Pword.focus()
   } else {
   document.Log.username.focus()
   }
}

function cookieVal(cookieName) {
thisCookie = document.cookie.split("; ")
for (i=0; i <thisCookie.length; i++) {
   if (cookieName == thisCookie[i].split("=")[0]) {
      return thisCookie[i].split("=")[1]
   }
}
return ""
}

function LoginResult(result) {
if (result == "fail") {
alert("The password or username is incorrect. Check capslock is off and try again")
document.Log.Pword.value="";
document.Log.Pword.focus();

} else {
//determine the look of the web page

closeMessage()
displayLoggedInMessage("<br /><h2>" +result + " is logged on</h2>",false)
setTimeout ("closeMessage()",1000)
window.location.reload()
//document.getElementById("hider").style.display = "none";
}
//endfunction Login
}

function Logout() {

//Run the login script with a blank password and this will log the user out


var oParams = {
                "callback": "ShowLogon",
                "username": "",
                "Pword" : ""

            };

            makeRequestPost("../../LoginAjax.php", oParams);

//end function Logout()
}

function ShowLogon(fail) {
window.location.reload()
//end function ShowLogon
}
