var xmlHttpForLogin;

function checkLogin() {
  document.getElementById("loginres").innerHTML = "<img src=\"/static/images/ajax-loader-bar.gif\" width=\"220\" height=\"19\" />";
  var username = document.getElementById("clientLoginForm").username.value;
  var password = document.getElementById("clientLoginForm").password.value;
  var message = "username=" + username + "&password=" + password;  
  loginpage = "/login";
  createXMLHttpRequestForLogin();
  xmlHttpForLogin.onreadystatechange = handleStateChangeForLogin;
  xmlHttpForLogin.open("POST", loginpage);
  xmlHttpForLogin.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xmlHttpForLogin.send(message);
}

function createXMLHttpRequestForLogin() {
  if (window.ActiveXObject) {
    xmlHttpForLogin = new ActiveXObject("Microsoft.XMLHTTP");
  } else {
    if (window.XMLHttpRequest) {
      xmlHttpForLogin = new XMLHttpRequest();
    }
  }
}

function handleStateChangeForLogin() {
  if(xmlHttpForLogin.readyState == 4) {
    if(xmlHttpForLogin.status == 200) {
      var result = xmlHttpForLogin.responseText.split('|');
      if (result[0]=='OK'){
        location.href = result[1];
      } else {
          if (result[0]=='D'){
              document.getElementById("loginres").innerHTML = "ACCESS DENIED";
          } else {
            document.getElementById("loginres").innerHTML = "PLEASE TRY AGAIN";
          } 
      }
    }
  }
}

//////////////////////////////////////////////////

var xmlHttpForLoadingPage;

function loadPage(){
  startpage = document.URL.replace(/index.migo/,"") + "en/index.txt";
  createXMLHttpRequestForLoadingPage();
  xmlHttpForLoadingPage.open("GET", startpage);
  xmlHttpForLoadingPage.onreadystatechange = handleStateChangeForLoadingPage;
  xmlHttpForLoadingPage.send(null); 
}
      
function createXMLHttpRequestForLoadingPage() {
  if (window.ActiveXObject) {
    xmlHttpForLoadingPage = new ActiveXObject("Microsoft.XMLHTTP");
  } else {
    if (window.XMLHttpRequest) {
      xmlHttpForLoadingPage = new XMLHttpRequest();
    }
  }
}
     
function handleStateChangeForLoadingPage() {
  if(xmlHttpForLoadingPage.readyState == 4) {
    if(xmlHttpForLoadingPage.status == 200) {
      document.getElementById("body_text").innerHTML = xmlHttpForLoadingPage.responseText;
      changeText('homepage');
    }
  }
}

//////////////////////////////////////////////////

var accFormValue= new Array();
errorText0="Please enter your name";
errorText1="Please enter your email address";
errorText1a="Your email address format is incorrect";
errorText2="Please enter the subject";
errorText3="Please enter your message";
accFormValue[0]=true;
accFormValue[1]=true;
accFormValue[2]=true;
accFormValue[3]=true;

function submitMessage(tForm) {
  var sender = tForm.sender.value;
  var senderEmail = tForm.senderEmail.value;
  var subject = tForm.subject.value;
  var message = tForm.message.value;
  var postmessage = "sender=" + sender + "&senderEmail=" + senderEmail + "&subject=" + subject + "&message=" + message; 
  accFormValue[0]=true;
  accFormValue[1]=true;
  accFormValue[2]=true;
  accFormValue[3]=true;
  if ((tForm.sender.value=="")|(tForm.sender.value==errorText0)) {
    accFormValue[0]=false;
    tForm.sender.style.color="#FF0000";
    tForm.sender.value=errorText0;
  }
  if ((tForm.senderEmail.value=="")|(tForm.senderEmail.value==errorText1)) {
    accFormValue[1]=false;
    tForm.senderEmail.style.color="#FF0000";
    tForm.senderEmail.value=errorText1;
  } else {
    if (tForm.senderEmail.value.indexOf("@")==-1) {
      accFormValue[1]=false;
      tForm.senderEmail.style.color="#FF0000";
      tForm.senderEmail.value=errorText1a;
    } 
  } 
  if ((tForm.subject.value=="")|(tForm.subject.value==errorText2)) {
    accFormValue[2]=false;
    tForm.subject.style.color="#FF0000";
    tForm.subject.value=errorText2;
  } 
  if ((tForm.message.value=="")|(tForm.message.value==errorText3)) {
    accFormValue[3]=false;
    tForm.message.style.color="#FF0000";
    tForm.message.value=errorText3;
  } 
  if (accFormValue[0]&accFormValue[1]&accFormValue[2]&accFormValue[3]){  
    document.getElementById("ajaxtext").innerHTML = "<p align=\"center\"><br /><br /><img alt=\"Please wait...\" src=\"/static/images/ajax-loader.gif\" width=\"32\" height=\"32\" /></p>";
    mailpage = "/contact";
    createXMLHttpRequestForMail();
    xmlHttpForMail.onreadystatechange = handleStateChangeForMail;   
    xmlHttpForMail.open("POST", mailpage);
    xmlHttpForMail.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttpForMail.send(postmessage);   
  }
}

function createXMLHttpRequestForMail() {
  if (window.ActiveXObject) {
    xmlHttpForMail = new ActiveXObject("Microsoft.XMLHTTP");
  } else {
    if (window.XMLHttpRequest) {
      xmlHttpForMail = new XMLHttpRequest();
    }
  }
}

function handleStateChangeForMail() {
  if(xmlHttpForMail.readyState == 4) {
    if(xmlHttpForMail.status == 200) {
      document.getElementById("ajaxtext").innerHTML = xmlHttpForMail.responseText
    }
  }
}

function resetField(obj,i){
  if (!accFormValue[i]){
    obj.style.color="#000000";
    obj.value="";
  }
}

function sChange(i){
  accFormValue[i] = true;
}

function disableAutocomplete(f){
  document.getElementById(f).setAttribute("autocomplete","off");
}
