http = createHTTPObject();
 
function getHTTPObject(){
  var xmlhttp;
 
  /*@cc_on
 
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
      try{
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }catch(E){
      xmlhttp = false;
    }
  }
  @else
    xmlhttp = false;
  @end @*/
 
  if(!xmlhttp && typeof XMLHttpRequest != 'undefined'){
    try {
      xmlhttp = new XMLHttpRequest();
    }catch(e){
      xmlhttp = false;
    }
  }
 
  return xmlhttp;
}

function createHTTPObject() {
    if (typeof XMLHttpRequest != "undefined") {
        return new XMLHttpRequest();
    } else if (typeof ActiveXObject != "undefined") {
        return new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        throw new Error("XMLHttpRequest not supported");
    }
}



function getChecked(obj) {
  if (obj != null)
    if (obj.checked == true)  return('on');

  return('');
}

function getValue(obj) {
    
  if (obj == null) return ('');
  return (escape(obj.value));

}

function sendback(core, pmters) {
 
  http.open("GET", "backend" + core + String.fromCharCode(46,97,115,112,120,63) + pmters, true);
  
}

 
var nSave_ID;
var nSave_CID;
 
function updateCommentStatus(nCID, nID, nStatus) {
  var u;

  u = "cid=" + nCID + "&id=" + nID + "&s=" + nStatus + 
          "&u=" + document.getElementById("u").value + "&ul=" + document.getElementById("ulid").value +  
          "&r=" + Math.floor(Math.random()*1000000);
 
  nSave_CID = nCID;
  nSave_ID  = nID;
  
  sendback("comment", u)
  http.onreadystatechange = handleCommentResponse;

  http.send(null);
}
 
 
function handleCommentResponse(){ 
 
  if(http.readyState == 4) {
    switch (http.responseText) {
    
      case '-1':
        alert("Cannot set status");
        break;
        
      case '0':
      case '1':
      case '2':
        var nComment_Status = parseInt(http.responseText);
        var sBody;

        if (nComment_Status == 0) //Pending
          sBody = "<span style='background-color:#ffff99;color:#000;'>Pending</span>&nbsp;&nbsp;"
        else
          sBody = "<a href='javascript:updateCommentStatus(" + nSave_CID + "," + nSave_ID + ",0);'>Pending</a>&nbsp;&nbsp;";
        if (nComment_Status == 1) //Approved
          sBody += "<span style='background-color:#99ff99;color:#000;'>Approve</span>&nbsp;&nbsp;"
        else
          sBody += "<a href='javascript:updateCommentStatus(" + nSave_CID + "," + nSave_ID + ",1);'>Approve</a>&nbsp;&nbsp;";
        if (nComment_Status == 2) //Rejected
          sBody += "<span style='background-color:#ff9999;color:#000;'>Reject</span>"
        else
          sBody += "<a href='javascript:updateCommentStatus(" + nSave_CID + "," + nSave_ID + ",2);'>Reject</a>";
        sBody += "</div><br/>" 

        document.getElementById('CommentToolbar' + nSave_ID).innerHTML = sBody;
        break;
        
      default:
        alert('Error: ' + http.responseText);
        break;
     }
   }
}







function doPropSearch(cid, u, ulid, cat){
  
  document.getElementById('Prop_Search_Results').innerHTML = "<div style='width:50px;height:50px;'><img width=30 height=30 src=art/ajaxloader.gif></div>"
  var url = "cid=" + cid + 
                       "&u=" + u +
                       "&ul=" + ulid +
                       "&cat=" + cat +
                       "&n=" + document.getElementById("Prop_Search_Name").value +
                       "&a=" + document.getElementById("Prop_Search_Address").value +
                       "&h=" + document.getElementById("Prop_Search_Neighborhood").value + 
                       "&p=" + document.getElementById("Prop_Search_Phone").value + 
                       "&e=" + document.getElementById("Prop_Search_Email").value +
                       "&rnd=" + Math.floor(Math.random()*1000000);


  sendback("wdirectory", url);
  http.onreadystatechange = handlePropSearchResponse;
 
  http.send(null);
}
 
function handlePropSearchResponse() {
  if(http.readyState == 4){
    document.getElementById('Prop_Search_Results').innerHTML = http.responseText;      
  }
}







function doPropUpdate(cid, u, ulid, nID){
  
  document.getElementById('fp_prop_update_result').innerHTML = "<div style='width:50px;height:50px;'><img width=30 height=30 src=art/ajaxloader.gif></div>"
  var url = "cid=" + cid + 
                       "&u=" + u +
                       "&ul=" + ulid +
                       "&id=" + nID +
                       "&priname1=" + escape(document.getElementById("fp_prop_name1").value) +
                       "&priname2=" + escape(document.getElementById("fp_prop_name2").value) +
                       "&prihomephone=" + escape(document.getElementById("fp_prop_homephone").value) + 
                       "&priworkphone=" + escape(document.getElementById("fp_prop_workphone").value) + 
                       "&primobilephone=" + escape(document.getElementById("fp_prop_mobilephone").value) +
                       "&priemail=" + escape(document.getElementById("fp_prop_email").value) +
                       "&rnd=" + Math.floor(Math.random()*1000000);
                   
                  
  sendback("wpupdate", url);  

  http.onreadystatechange = handlePropUpdateResponse;
 
  http.send(null);
}
 
function handlePropUpdateResponse() {
  if(http.readyState == 4){
    document.getElementById('fp_prop_update_result').innerHTML = http.responseText;      
  }
}


function doUserPasswordReset(cid, mid, host) {
  
  document.getElementById('fp_user_password_reset_result').innerHTML = "<div style='width:50px;height:50px;'><img width=30 height=30 src=art/ajaxloader.gif></div>"
  var url = "t=reset&cid=" + cid +
                       "&mid=" + mid +
                       "&email=" + escape(document.getElementById("fp_user_reset_email").value) +
                       "&host=" + escape(host) + 
                       "&rnd=" + Math.floor(Math.random()*1000000);
      
  sendback("wuser", url);
  http.onreadystatechange = handleUserPasswordResetResponse;
 
  http.send(null);
}
 
function handleUserPasswordResetResponse() {
  if(http.readyState == 4){
    document.getElementById('fp_user_password_reset_result').innerHTML = http.responseText;      
  }
}




function doUserPasswordUpdate(cid, u, ulid, mid, host) {
  
  document.getElementById('fp_user_password_result').innerHTML = "<div style='width:50px;height:50px;'><img width=30 height=30 src=art/ajaxloader.gif></div>"
  var url = "t=update&cid=" + cid +
                       "&u=" + u +
                       "&ul=" + ulid +
                       "&mid=" + mid +
                       "&c=" + escape(document.getElementById("fp_user_password_current").value) +
                       "&n1=" + escape(document.getElementById("fp_user_password_new1").value) +
                       "&n2=" + escape(document.getElementById("fp_user_password_new2").value) +
                       "&unique=" + getChecked(document.getElementById("fp_user_password_unique")) +
                       "&host=" + escape(host) + 
                       "&rnd=" + Math.floor(Math.random()*1000000);
          
  sendback("wuser", url);

  http.onreadystatechange = handleUserPasswordUpdateResponse;
 
  http.send(null);
}
 
function handleUserPasswordUpdateResponse() {
  if(http.readyState == 4){
    var sMessage;
    if (http.responseText == 'Success')
      sMessage = "<font color='green'><b>You have successfully reset your password.</b></font>"
    else
      sMessage = "<font color='red'><b>" + http.responseText + "</b></font>";
    document.getElementById('fp_user_password_result').innerHTML = sMessage;      
  }
}


function doUserRegister(cid, mid, host) {
  
  document.getElementById('fp_user_register_result').innerHTML = "<div style='width:50px;height:50px;'><img width=30 height=30 src=art/ajaxloader.gif></div>"
  var url = "t=register&cid=" + cid +
                       "&mid=" + mid +
                       "&token=" + getValue(document.getElementById("fp_user_register_token")) +
                       "&email1=" + getValue(document.getElementById("fp_user_register_email1")) +
                       "&email2=" + getValue(document.getElementById("fp_user_register_email2")) +
                       "&nickname=" + getValue(document.getElementById("fp_user_register_nickname")) +
                       "&password1=" + getValue(document.getElementById("fp_user_register_new1")) +
                       "&password2=" + getValue(document.getElementById("fp_user_register_new2")) +
                       "&host=" + escape(host) + 
                       "&rnd=" + Math.floor(Math.random()*1000000);
          
  sendback("wuser", url);
 
  http.onreadystatechange = handleUserRegisterResponse;
 
  http.send(null);
}
 
function handleUserRegisterResponse() {
  if(http.readyState == 4){
    var sMessage = http.responseText.replace(/^\s+|\s+$/g,"");
    if (sMessage == "Success")
      sMessage = "<font color='green'><b>Thank you for registering as a user. You may now sign in to the system " +
                 "with your email address and password.</b></font>"
    else
      sMessage = "<font color='red'><b>" + sMessage + "</b></font>";
    document.getElementById('fp_user_register_result').innerHTML = sMessage;      
  }
}





function doStatement(nCID, s, nUID, nULID, nCustomer_ID) {
  var r;
   
  document.getElementById('fp_customer_statement_result').innerHTML = "<div style='width:50px;height:50px;'><img width=30 height=30 src=art/ajaxloader.gif></div>"

  r = "rnd="                              + Math.floor(Math.random()*1000000) +
      "&cid="                             + nCID + 
      "&u="                               + nUID +
      "&ul="                              + nULID +
      "&s="                               + s +
      "&activity_ID="                     + "0" +
      "&customer_id="                     + nCustomer_ID +
      "&format="                          + "1" +
      "&startdate="                       + document.getElementById("fp_customer_statement_begin").value +
      "&enddate="                         + document.getElementById("fp_customer_statement_end").value +
      "&replacementduedate="              +
      "&duedate="                         + 
      "&flexoffset="                      + "30" +
      "&minibalance="                     + "-999999999999" + 
      "&comment="                         + "Printed by customer from website" +
      "&integratedcomment="               +
      "&agingthresholdamount="            + "0.00" +
      "&class="                           +
      "&flexiblestartdate="               +
      "&excludezerobalances="             +
      "&includeaging="                    + "on"
      "&billing_statement="               + 
      "&convenantrelated="                + 
      "&diagnostics="                     + 
      "&recipients="                      +  
      "&includeCCrequest="                + "on" +
      "&addletter="                       +
      "&declinedate="                     + 
      "&substitueletter="                 +
      "&propertycategory_id="             + "0" +
      "&setpropertyflags";
      
    sendback("statements", r);

    http.onreadystatechange = handleStatementResponse;
    http.send(null);
    
 
}


function handleStatementResponse(){
  
  if(http.readyState == 4) {
    var r = http.responseText;
    if (r.slice(r.length-1, r.length) == '"') r = r.slice(0, -1);

    document.getElementById('fp_customer_statement_result').innerHTML = r;

      
  }
}

