//ajax_func.js

var aok;

//Function to create an XMLHttp Object.
function getxmlhttp() {
  //Create a boolean variable to check for a valid IE instance.
  var xmlhttp = false;

  //Check if we are using IE.
  try {
    //If the javascript version is greater than 5.
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    //If not, then use the older active x object.
    try {
      //If we are using IE.
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      //Else we must be using a non-IE browser.
      xmlhttp = false;
    }
  }

  //If we are using a non-IE browser, create a JavaScript instance of the object.
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    xmlhttp = new XMLHttpRequest();
  }

  return xmlhttp;
}

//Function to process an XMLHttpRequest.
function processajax (serverPage, obj, getOrPost, str, busytext) {
  //Get an XMLHttpRequest object for use.
  xmlhttp = getxmlhttp ();
  if (getOrPost == "get") {
    xmlhttp.open("GET", serverPage);
    xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        obj.innerHTML = xmlhttp.responseText;
      } else {
        if (busytext!="") {
          obj.innerHTML = busytext;
        }
      }
    }
    xmlhttp.send(null);
  } else {
    xmlhttp.open("POST", serverPage, true);
    xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
    xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        obj.innerHTML = xmlhttp.responseText;
      } else {
        if (busytext!="") {
          obj.innerHTML = busytext;
        }
      }
    }
    xmlhttp.send(str);
  }
}

function showHideDiv(objDiv, objImg, imgShow, imgHide, addr) {
  if (document.getElementById(objImg).src == imgShow){
    //Show the div
    var obj = document.getElementById(objDiv);
    document.getElementById(objImg).src = imgHide;
    processajax (addr, obj, "get", "", "&nbsp;&nbsp;&nbsp;&nbsp;<b>Loading - Please Wait....</b>");
  } else {
    //Hide the div
    document.getElementById(objImg).src = imgShow;
    document.getElementById(objDiv).innerHTML = "";
  }
}

function updateDiv(objDiv, objImg, imgHide, addr) {
  // alert(objImg+": "+document.getElementById(objImg).src+" = "+imgHide);
  if (document.getElementById(objImg).src == imgHide){
    //div is visible
    var obj = document.getElementById(objDiv);
    processajax (addr, obj, "get", "", "&nbsp;&nbsp;&nbsp;&nbsp;<b>Loading - Please Wait....</b>");
  }
}

function createClientForm(e, sPage) {
  theObject = document.getElementById("addclient");
  theObject.style.visibility = "visible";
  theObject.style.height = "600px";
  theObject.style.width = "560px";

  var posx = 400;
  var posy = 0;
  //posx = e.clientX + document.body.scrollLeft;
  posy = e.clientY + getScrollY();

  theObject.style.left = posx + "px";
  theObject.style.top = posy + "px";

  //The location we are loading the page into.
  var objID = "addclient";
  var serverPage = sPage;
  var obj = document.getElementById(objID);
  obj.innerHTML = "";

  processajax (serverPage, obj, "get", "", "");
}

function closeClientForm() {
  theObject = document.getElementById("addclient");
  theObject.style.visibility = "hidden";
  theObject.style.height = "0px";
  theObject.style.width = "0px";
}

function createResendForm(e, sPage) {
  theObject = document.getElementById("resend_email");
  theObject.style.visibility = "visible";
  theObject.style.height = "300px";
  theObject.style.width = "400px";

  var posx = 0;
  var posy = 0;
  posx = e.clientX + getScrollX() - 200;
  posy = e.clientY + getScrollY() - 320;

  theObject.style.left = posx + "px";
  theObject.style.top = posy + "px";

  //The location we are loading the page into.
  var objID = "resend_email";
  var serverPage = sPage;
  var obj = document.getElementById(objID);
  obj.innerHTML = "";

  processajax (serverPage, obj, "get", "", "");
}

function closeResendForm() {
  theObject = document.getElementById("resend_email");
  theObject.style.visibility = "hidden";
  theObject.style.height = "0px";
  theObject.style.width = "0px";
}

function createMailFixForm(e, sPage) {
  theObject = document.getElementById("resend_email");
  theObject.style.visibility = "visible";
  theObject.style.height = "340px";
  theObject.style.width = "400px";

  var posx = 0;
  var posy = 0;
  posx = e.clientX + getScrollX() - 200;
  posy = e.clientY + getScrollY() - 320;

  theObject.style.left = posx + "px";
  theObject.style.top = posy + "px";

  //The location we are loading the page into.
  var objID = "resend_email";
  var serverPage = sPage;
  var obj = document.getElementById(objID);
  obj.innerHTML = "";

  processajax (serverPage, obj, "get", "", "");
}

//Functions to submit a form.
function getformvalues(fobj, valfunc) {
  //alert("getformvalues");
  var str = "";
  aok = true;
  var val;
  //Run through a list of all objects contained within the form.
  for (var i = 0; i < fobj.elements.length; i++) {
    if(valfunc) {
      if (aok == true) {
        val = valfunc(fobj.elements[i].value,fobj.elements[i].name);
        if (val == false) {
          aok = false;
        }
      }
    }
    str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
  }
  //Then return the string values.
  return str;
}

function submitform (theform, serverPage, objID, valfunc) {
  //alert("submitform");
  document.getElementById("form_message").innerHTML = "Processing - please wait...";
  var file = serverPage;
  var str = getformvalues(theform,valfunc);
  //If the validation is ok.
  if (aok == true) {
    //alert("objID = "+objID);
    obj = document.getElementById(objID);
    //alert("obj = "+obj);
    processajax (serverPage, obj, "post", str, "");
  }
}

function submitClientSearch (theform, serverPage, objID, valfunc) {
  //alert("submitSearchform");
  var file = serverPage;
  var str = getformvalues(theform,valfunc);
  //If the validation is ok.
  if (aok == true) {
    obj = document.getElementById(objID);
    processajax (serverPage, obj, "post", str, "<b>Searching - please wait...</b>");
  }
}

function getScrollX() {
  var scrOfX = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfX = document.documentElement.scrollLeft;
  }
  return scrOfX;
}

function getScrollY() {
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  return scrOfY;
}

function findPosX(obj) {
  var curleft = 0;
  if (obj.offsetParent) {
    while (obj.offsetParent) {
      curleft += obj.offsetLeft
      obj = obj.offsetParent;
    }
  } else if (obj.x) {
    curleft += obj.x;
  }
  return curleft;
}

function findPosY(obj) {
  var curtop = 0;
  if (obj.offsetParent) {
    while (obj.offsetParent) {
      curtop += obj.offsetTop
      obj = obj.offsetParent;
    }
  } else if (obj.y) {
    curtop += obj.y;
  }
  return curtop;
}

function trim (inputString) {
  // Removes leading and trailing spaces from the passed string. Also removes
  // consecutive spaces and replaces them with one space. If something besides
  // a string is passed in (null, custom object, etc.), then return the input.
  if (typeof inputString != "string") {
    return inputString;
  }
  var retValue = inputString;
  var ch = retValue.substring(0, 1);
  while (ch == " ") {
    // Check for spaces at the beginning of the string
    retValue = retValue.substring(1, retValue.length);
    ch = retValue.substring(0, 1);
  }
  ch = retValue.substring(retValue.length-1, retValue.length);
  while (ch == " ") {
    // Check for spaces at the end of the string
    retValue = retValue.substring(0, retValue.length-1);
    ch = retValue.substring(retValue.length-1, retValue.length);
  }
  while (retValue.indexOf(" ") != -1) {
    // Note there are two spaces in the string
    // Therefore look for multiple spaces in the string
    retValue = retValue.substring(0, retValue.indexOf(" ")) + retValue.substring(retValue.indexOf(" ")+1, retValue.length);
    // Again, there are two spaces in each of the strings
  }
  return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

//Function to validate the new client form.
function validateClient (thevalue, thename){
  var nowcont = true;
  if (thename == "last_name") {
    if (trim (thevalue) == "") {
      document.getElementById("form_message").innerHTML = "Please enter the clients surname...";
      document.getElementById("newClient").last_name.focus();
      nowcont = false;
    }
  }
  if (nowcont == true) {
    if (thename == "first_name") {
      if (trim (thevalue) == "") {
        document.getElementById("form_message").innerHTML = "Please enter the clients first name...";
        document.getElementById("newClient").first_name.focus();
        nowcont = false;
      }
    }
  }
  if (nowcont == true) {
    if (thename == "email") {
      if (trim (thevalue) == "") {
        document.getElementById("form_message").innerHTML = "Please enter the clients e-mail address...";
        document.getElementById("newClient").email.focus();
        nowcont = false;
      }
    }
  }
  return nowcont;
}

//Function to validate the resend e-mail form.
function validateResend (thevalue, thename){
  var nowcont = true;
  if (thename == "un") {
    if (trim (thevalue) == "") {
      document.getElementById("form_message").innerHTML = "Please enter the clients username...";
      document.getElementById("newClient").un.focus();
      nowcont = false;
    }
  }
  if (nowcont == true) {
    if (thename == "ky") {
      if (trim (thevalue) == "") {
        document.getElementById("form_message").innerHTML = "Please enter the clients password...";
        document.getElementById("newClient").ky.focus();
        nowcont = false;
      }
    }
  }
  return nowcont;
}

//Function to validate the new client form.
function validateSearchClient (thevalue, thename){
  var nowcont = true;
  return nowcont;
}

function submitbutton_admin_client() {
    var form = document.mosForm;
    var r = new RegExp("[\<|\>| |\"|\'|\%|\;|\(|\)|\&|\+|\-]", "i");

    // do field validation
    if (form.last_name.value == "") {
        alert( "Please enter clients Surname" );
    } else if (form.first_name.value == "") {
        alert( "Please enter clients First Name" );
    } else if (form.email.value == "") {
        alert( "Please enter clients E-mail Address" );
    } else {
        form.submit();
    }
}
