
//document.onkeypress = keyPress;
//function keyPress() {
//    if (window.event.keyCode == 13){
//        event.returnValue=false;
//        event.cancel = true;
//    }
//}
var count = 1;


var __isIE =  navigator.appVersion.match(/MSIE/);
var __userAgent = navigator.userAgent;
var __isFireFox = __userAgent.match(/firefox/i);
var __isFireFoxOld = __isFireFox && 
   (__userAgent.match(/firefox\/2./i) || __userAgent.match(/firefox\/1./i));
var __isFireFoxNew = __isFireFox && !__isFireFoxOld;
    
if (!__isIE){
    window.addEventListener("click", checkForPopups, false);
}
    if (document.attachEvent) {
    document.attachEvent('onclick', checkForPopups);
}
   
    
    //Gets the browser specific XmlHttpRequest Object
    function getXmlHttpRequestObject() {
        if (window.XMLHttpRequest) {
            return new XMLHttpRequest(); //Not IE
        } else if(window.ActiveXObject) {
            return new ActiveXObject("Microsoft.XMLHTTP"); //IE
        } else {
            //Display your error message here. 
            //and inform the user they might want to upgrade
            //their browser.
            alert("Your browser doesn't support the XmlHttpRequest object.  Better upgrade to Firefox.");
        }
    }
    var form_id = '';
    var url_name = '';
    var div_id = '';            
    //Get our browser specific XmlHttpRequest object.
    var receiveReq = getXmlHttpRequestObject();        
    //Initiate the asyncronous request.
    
    
    


function __parseBorderWidth(width) {
    var res = 0;
    if (typeof(width) == "string" && width != null 
                && width != "" ) {
        var p = width.indexOf("px");
        if (p >= 0) {
            res = parseInt(width.substring(0, p));
        }
        else {
             //do not know how to calculate other 
             //values (such as 0.5em or 0.1cm) correctly now
             //so just set the width to 1 pixel
            res = 1; 
        }
    }
    return res;
}

//returns border width for some element
function __getBorderWidth(element) {
    var res = new Object();
    res.left = 0; res.top = 0; res.right = 0; res.bottom = 0;
    if (window.getComputedStyle) {
        //for Firefox
        var elStyle = window.getComputedStyle(element, null);
        res.left = parseInt(elStyle.borderLeftWidth.slice(0, -2));  
        res.top = parseInt(elStyle.borderTopWidth.slice(0, -2));  
        res.right = parseInt(elStyle.borderRightWidth.slice(0, -2));  
        res.bottom = parseInt(elStyle.borderBottomWidth.slice(0, -2));  
    }
    else {
        //for other browsers
        res.left = __parseBorderWidth(element.style.borderLeftWidth);
        res.top = __parseBorderWidth(element.style.borderTopWidth);
        res.right = __parseBorderWidth(element.style.borderRightWidth);
        res.bottom = __parseBorderWidth(element.style.borderBottomWidth);
    }
   
    return res;
}

//returns absolute position of some element within document
function getElementAbsolutePos(element) {
    var res = new Object();
    res.x = 0; res.y = 0;
    if (element !== null) {
        res.x = element.offsetLeft;
        res.y = element.offsetTop;
        
        var offsetParent = element.offsetParent;
        var parentNode = element.parentNode;
        var borderWidth = null;

        while (offsetParent != null) {
            res.x += offsetParent.offsetLeft;
            res.y += offsetParent.offsetTop;
            
            var parentTagName = offsetParent.tagName.toLowerCase();    

            if ((__isIE && parentTagName != "table") || 
                (__isFireFoxNew && parentTagName == "td")) {            
                borderWidth = __getBorderWidth(offsetParent);
                res.x += borderWidth.left;
                res.y += borderWidth.top;
            }
            
            if (offsetParent != document.body && 
                offsetParent != document.documentElement) {
                res.x -= offsetParent.scrollLeft;
                res.y -= offsetParent.scrollTop;
            }

            //next lines are necessary to support FireFox problem with offsetParent
               if (!__isIE) {
                while (offsetParent != parentNode && parentNode !== null) {
                    res.x -= parentNode.scrollLeft;
                    res.y -= parentNode.scrollTop;
                    
                    if (__isFireFoxOld) {
                        borderWidth = __getBorderWidth(parentNode);
                        res.x += borderWidth.left;
                        res.y += borderWidth.top;
                    }
                    parentNode = parentNode.parentNode;
                }    
            }

            parentNode = offsetParent.parentNode;
            offsetParent = offsetParent.offsetParent;
        }
    }
    return res;
}
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
         //FIND COORDINATES
    function findPosX(obj){
        var curleft = 0;
        if(obj.offsetParent)
            while(1) 
            {
              curleft += obj.offsetLeft;
              if(!obj.offsetParent)
                break;
              obj = obj.offsetParent;
            }
        else if(obj.x)
            curleft += obj.x;
        return curleft;
  }

  function findPosY(obj){
        var curtop = 0;
        if(obj.offsetParent)
            while(1)
            {
              curtop += obj.offsetTop;
              if(!obj.offsetParent)
                break;
              obj = obj.offsetParent;
            }
        else if(obj.y)
            curtop += obj.y;
        return curtop;
  }
    
    

    function show_processing(current_div, offsetx, offsety, noBG){
        var text;
        var visibility;
        var loading_image = document.getElementById('loading_image');
        var loading_bg = document.getElementById('loading_bg');
        var status_div = document.getElementById('status_div'); 

        //alert('curdiv ' + current_div)
        //alert('findPosX ' + findPosX(current_div) + offsetx)
        //alert('findPosY ' + findPosY(current_div) + offsety)
        var pos = getElementAbsolutePos(current_div);
        //window.alert("Element's left: " + pos.x + " and top: " + pos.y); 
        
        //window.alert("Element's left: " + (pos.x + offsetx) + " and top: " + (pos.y + offsety));
        status_div.style.left = (pos.x + offsetx); 
        status_div.style.top = (pos.y + offsety);

        loading_bg.style.width = current_div.offsetWidth + "px";
        loading_bg.style.height = current_div.offsetHeight + "px"; 
        loading_bg.style.left = pos.x + "px";
        loading_bg.style.top = pos.y + "px";
        
        
        
//        if (parseInt(loading_bg.style.left) < 0){
//            loading_bg.style.left = 0;   
//        }
        //alert (findPosX(current_div) + (current_div.style.width /2));
        loading_image.style.left = parseInt(loading_bg.style.left) + (current_div.offsetWidth /2) - (loading_image.offsetWidth/2) + "px";
        loading_image.style.top = parseInt(loading_bg.style.top) + (current_div.offsetHeight /2) - (loading_image.offsetHeight/2) + "px"; 
//        alert(loading_image.style.left + " " + loading_image.style.top);                                  
//        alert(loading_bg.style.left);
//        alert(loading_bg.style.top);
//        alert(loading_bg.offsetWidth . " " . loading_bg.offsetHeight);  
//          
        
       
        if (receiveReq.readyState == 0) {
            text = "Processing Request...0";
            visibility = "visible";
        }
        if (receiveReq.readyState == 1) {
            text = "Processing Request...1";
            visibility = "visible";
        }
        if (receiveReq.readyState == 2) {
            text = "Processing Request...2";
            visibility = "visible";
        }
        if (receiveReq.readyState == 3) {
            text = "Processing Request...3";
            visibility = "visible";
        }
        if (receiveReq.readyState == 4) {
            text = "Processing Request...Done!";
            visibility = "hidden";  
        }
        document.getElementById('status_div').innerHTML = text;
        
        if (visibility == "hidden"){
//            setTimeout("document.getElementById('status_div').style.visibility = 'hidden'", 1000);
//            setTimeout("document.getElementById('loading_bg').style.visibility = 'hidden'", 1000);
//            setTimeout("document.getElementById('loading_image').style.visibility = 'hidden'", 1000);
            //document.getElementById('status_div').style.visibility = "hidden";
            document.getElementById('loading_bg').style.visibility = "hidden"; 
            document.getElementById('loading_image').style.visibility = "hidden";
        }else{
            //document.getElementById('status_div').style.visibility = visibility;
            //if(noBG == false){
                document.getElementById('loading_bg').style.visibility = visibility;    
            //}
            document.getElementById('loading_image').style.visibility = visibility;
        }
    }
    
  
    
    function searchFor() {
//        var current_div = document.getElementById('current_housing');
//        alert(document.getElementById('current_housing').style.width);
//        alert(document.getElementById('current_housing').style.offsetWidth);
//        alert(document.getElementById('current_housing').offsetWidth);
//        alert("offsetLeft: " + current_div.style.offsetLeft + " offsetHeight: " + current_div.style.offsetHeight);
        var str = escape(document.getElementById('txtSearch').value);
        receiveReq.open("GET", '../includes/current_housing.php?str=' + str, true);
        receiveReq.onreadystatechange = handleSearch; 
        receiveReq.send(null);
    }
    function handleSearch() {
        var current_div = document.getElementById('current_housing');
        noBackground = false;
        show_processing(current_div, 0, 0, noBackground);
        
        if (receiveReq.readyState == 4) {
            document.getElementById('current_housing').innerHTML = receiveReq.responseText;
        }
    }
    function details(str) {
        //var str = escape(document.getElementById('txtSearch').value);
            receiveReq.open("GET", '../includes/details.php?id=' + str, true);
            receiveReq.onreadystatechange = handledetails; 
            receiveReq.send(null);
    }
    function handledetails() {
        current_div = document.getElementById('current_new_contracts');
        noBackground = false;
        show_processing(current_div, 0, 0, noBackground);
        if (receiveReq.readyState == 4) {
            document.getElementById('current_new_contracts').innerHTML = receiveReq.responseText;
        }
    }
    function detailspark(str) {
        //var str = escape(document.getElementById('txtSearch').value);
            receiveReq.open("GET", '../includes/detailspark.php?id=' + str, true);
            receiveReq.onreadystatechange = handledetailspark; 
            receiveReq.send(null);
    }
    function handledetailspark() {
        current_div = document.getElementById('current_new_contracts');
        noBackground = false;
        show_processing(current_div, 0, 0, noBackground);
        if (receiveReq.readyState == 4) {
            document.getElementById('current_new_contracts').innerHTML = receiveReq.responseText;
        }
    }
    function searchForPark() {
        var str = escape(document.getElementById('txtSearchPark').value);
            receiveReq.open("GET", '../includes/current_parking.php?str=' + str, true);
            receiveReq.onreadystatechange = handleSearchPark; 
            receiveReq.send(null);            
    }
    function handleSearchPark() {
        current_div = document.getElementById('current_parking');
        noBackground = false;
        show_processing(current_div, 0, 0, noBackground);
        if (receiveReq.readyState == 4) {
            document.getElementById('current_parking').innerHTML = receiveReq.responseText;
        }
    }
    
    function refreshDetailsLeft(str) {
        receiveReq.open("GET", '../includes/details_left_edit.php?id=' + str, true);
        receiveReq.onreadystatechange = handleRefreshDetailsLeft; 
        receiveReq.send(null);            
    }
    function handleRefreshDetailsLeft() {
        current_div = document.getElementById('left_details');
        noBackground = false;
        show_processing(current_div, 0, 0, noBackground);
        if (receiveReq.readyState == 4) {
            document.getElementById('left_details').innerHTML = receiveReq.responseText;
        }
    }
    



    function updatePayments(contract_id) {
        receiveReq.open("GET", '../includes/details_payments.php?id=' + contract_id, true);
        receiveReq.onreadystatechange = handleUpdatePayments; 
        receiveReq.send(null);        
    }
    function handleUpdatePayments() {
        current_div = document.getElementById('payment_details');
        noBackground = false;
        show_processing(current_div, 0, 0, noBackground);
        if (receiveReq.readyState == 4) {
            document.getElementById('payment_details').innerHTML = receiveReq.responseText;
        }
    }
    function reloadDueDatesSummary(){
        var deposits = document.getElementById('deposits').checked;
        var payment_type_id = document.getElementById('payment_type_id').value;
        var payment_type_id = document.getElementById('payment_type_id').value;
        receiveReq.open("GET", '../includes/due_dates_summary.php?deposits=' + deposits + '&payment_type_id=' + payment_type_id, true);
        receiveReq.onreadystatechange = handleReloadDueDatesSummary; 
        receiveReq.send(null);        
    }
    function handleReloadDueDatesSummary() {
        current_div = document.getElementById('right_due_dates');
        noBackground = false;
        show_processing(current_div, 0, 0, noBackground);
        if (receiveReq.readyState == 4) {
            document.getElementById('right_due_dates').innerHTML = receiveReq.responseText;
        }
    }
    function dueDateSend(date, deposits, payment_type_id, property) {
        //alert(date);
            url = '../includes/due_dates.php?date=' + escape(date) + '&deposits=' + escape(deposits) + '&payment_type_id=' + escape(payment_type_id)+ '&property=' + escape(property); 
            //alert(url)
            receiveReq.open("GET", url, true);
            receiveReq.onreadystatechange = handleDueDateSend; 
            receiveReq.send(null);            
    }
    function handleDueDateSend() {
        current_div = document.getElementById('left_due_dates');
        noBackground = false;
        show_processing(current_div, 0, 0, noBackground);
        if (receiveReq.readyState == 4) {
            document.getElementById('left_due_dates').innerHTML = receiveReq.responseText;
        }
    }
    
    function refreshDetailsLeftNormal(form) {
        var str = '';
        var elem = document.getElementById('edit_contract_details').elements;
        for(var i = 0; i < elem.length; i++){
            if (i == 0){
                str = elem[i].name + "=" + escape(elem[i].value);    
            }else{
                str += "&" + elem[i].name + "=" + escape(elem[i].value);
            }
        }       
        var url = "../includes/details_left.php";
        receiveReq.open("POST", url, true);
        receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        receiveReq.onreadystatechange = handleRefreshDetailsLeftNormal;
        receiveReq.send(str);           
    }
    function handleRefreshDetailsLeftNormal() {
        current_div = document.getElementById('left_details');
        noBackground = false;
        show_processing(current_div, 0, 0, noBackground);
        if (receiveReq.readyState == 4) {
            document.getElementById('left_details').innerHTML = receiveReq.responseText;
        }
    }
    
    function refreshDetailsLeftNormalCancel(id) {       
        var deposits = document.getElementById('left_details').checked;
        var payment_type_id = document.getElementById('left_details').value;
        receiveReq.open("GET", '../includes/details_left.php?id_from_cancel=' + id, true);
        receiveReq.onreadystatechange = handleRefreshDetailsLeftNormal; 
        receiveReq.send(null);            
    }
    function handleRefreshDetailsLeftNormal() {
        current_div = document.getElementById('left_details');
        noBackground = false;
        show_processing(current_div, 0, 0, noBackground);
        if (receiveReq.readyState == 4) {
            document.getElementById('left_details').innerHTML = receiveReq.responseText;
        }
    }
    
    function processCharge(form) {
        var id = escape(form.payment_type_id.value);
        var amount = escape(form.charge_amount.value);
        var date = escape(form.charge_date.value);
        var notes = escape(form.charge_notes.value);
        var contract_id = escape(form.charge_lease_form_id.value);
        var submit = escape(form.submitCharge.value);
        var str = 'payment_type_id='+id+'&charge_amount='+amount+'&charge_date='+date+'&charge_notes='+notes+'&lease_form_id='+contract_id+'&submit='+submit;

        var url = "../includes/details_right.php";

        receiveReq.open("POST", url, true);
        //Send the proper header information along with the request
        receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        
        receiveReq.onreadystatechange = handleProcessCharge;

        receiveReq.send(str);
        
        
    }
    function handleProcessCharge() {
        current_div = document.getElementById('details_right');
        noBackground = false;
        show_processing(current_div, 0, 0, noBackground);
        if (receiveReq.readyState == 4) {
            document.getElementById('details_right').innerHTML = receiveReq.responseText;
            var contract_id = escape(document.getElementById('charge_lease_form_id').value); 
            updatePayments(contract_id);
        }
    }
    function processPayment(form) {
        var contract_id = escape(form.payment_lease_form_id.value);
        var submit = escape(form.submitPayment.value);
        var payment_type_id = escape(form.payment_type_id_payment.value);
        var payment_amount= escape(form.payment_amount.value);
        var payment_date= escape(form.payment_date.value);
        var payment_kind_id= escape(form.payment_kind_id.value);
        var payment_kind_info= escape(form.payment_kind_info.value);
        var payment_notes= escape(form.payment_notes.value);
        var str = 'charge_id='+payment_type_id+'&payment_notes='+payment_notes+'&payment_amount='+payment_amount+'&payment_date='+payment_date+'&payment_kind_id='+payment_kind_id+'&payment_kind_info='+payment_kind_info+'&lease_form_id='+contract_id+'&submit='+submit;

        var url = "../includes/details_right.php";

        receiveReq.open("POST", url, true);
        //Send the proper header information along with the request
        receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        //receiveReq.setRequestHeader("Content-length", str.length);
        //receiveReq.setRequestHeader("Connection", "close");
        receiveReq.onreadystatechange = handleProcessPayment;

        receiveReq.send(str);
//        if (receiveReq.readyState == 1) {
//            updatePayments(contract_id);
//        }
        
    }
    function handleProcessPayment() {
        current_div = document.getElementById('details_right');
        noBackground = false;
        show_processing(current_div, 0, 0, noBackground);
        if (receiveReq.readyState == 4) {
            document.getElementById('details_right').innerHTML = receiveReq.responseText;
            var contract_id = escape(document.getElementById('charge_lease_form_id').value); 
            updatePayments(contract_id);
        }
    }
    

    function popupMessage(obj,id) { 
        receiveReq.open("GET", '../includes/message.php?id=' + id, true);
        receiveReq.onreadystatechange = handlePopupMessage; 
        receiveReq.send(null);

          var curleft = 0;
          var curtop = 0;
          //var curtop = obj.offsetHeight + 5;
          var border;
          if (obj.offsetParent)
          {
            do
            {
              // XXX: If the element is position: relative we have to add borderWidth
              if (getStyle(obj, 'position') == 'relative')
              {
                if (border = _pub.getStyle(obj, 'border-top-width')) curtop += parseInt(border);
                if (border = _pub.getStyle(obj, 'border-left-width')) curleft += parseInt(border);
              }
              curleft += obj.offsetLeft;
              curtop += obj.offsetTop;
            }
            while (obj = obj.offsetParent)
          }
          else if (obj.x)
          {
            curleft += obj.x;
            curtop += obj.y;
          }
        var clickX = curleft + 10;
        var clickY = curtop;

          //show the popup message and hide with fading effect
          //document.getElementById('popup_div').style.left = clickX - document.getElementById('popup_div').offsetWidth;
          //document.getElementById('popup_div').style.top = clickY;
          //document.getElementById('popup_div').style.visibility = 'visible';
          
          document.getElementById('popup_div').style.left = (clickX - document.getElementById('popup_div').offsetWidth - (document.getElementById('popup_arrow').offsetWidth /2) +4) + "px"; 
          document.getElementById('popup_div').style.top = (clickY + (document.getElementById('popup_arrow').offsetHeight /2) - 6) + "px";
          document.getElementById('popup_div').style.visibility = 'visible';
          document.getElementById('popup_arrow').style.left = (clickX - document.getElementById('popup_arrow').offsetWidth) + "px";
          document.getElementById('popup_arrow').style.top = clickY + "px";

          
    }

    function getStyle(obj, styleProp)
    {
      if (obj.currentStyle)
        return obj.currentStyle[styleProp];
      else if (window.getComputedStyle)
        return document.defaultView.getComputedStyle(obj,null).getPropertyValue(styleProp);
    }
    
    function handlePopupMessage() {
        current_div = document.getElementById('popup_div');
        noBackground = true;
        show_processing(current_div, 0, 0, noBackground);
        if (receiveReq.readyState == 4) {
            document.getElementById('popup_div').innerHTML = receiveReq.responseText;
            document.getElementById('popup_arrow').style.visibility = 'visible';
        }
    }
    function closeMessage(){
        document.getElementById('popup_div').style.visibility = 'hidden';
        document.getElementById('popup_arrow').style.visibility = 'hidden'; 
    }
    function processMessage() {
        var contract_id = escape(document.getElementById('message_lease_form_id').value);
        var submit = escape(document.getElementById('message_save').value);
        var notes = escape(document.getElementById('message_notes').value);

        var str = 'contract_id='+contract_id+'&submit='+submit+'&notes='+notes;
        var url = "../includes/message.php";

        receiveReq.open("POST", url, true);
        receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        receiveReq.onreadystatechange = handleProcessMessage;
        receiveReq.send(str);
        

    }
    function handleProcessMessage() {
        
        if (receiveReq.readyState == 4) {
            closeMessage();
            universalRefresh('../includes/current_unprocessed_contracts_housing.php','current_new_contracts');
        }
    }
    
    
    
    
    
    
    
    //POPUP CHARGES
    
        function popupCharge(obj,id) {
        receiveReq.open("GET", '../includes/charge_popup.php?charge_id=' + id, true);
        receiveReq.onreadystatechange = handlePopupCharge; 
        receiveReq.send(null);

          var curleft = 0;
          var curtop = 0;
          //var curtop = obj.offsetHeight + 5;
          var border;
          if (obj.offsetParent)
          {
            do
            {
              // XXX: If the element is position: relative we have to add borderWidth
              if (getStyle(obj, 'position') == 'relative')
              {
                if (border = _pub.getStyle(obj, 'border-top-width')) curtop += parseInt(border);
                if (border = _pub.getStyle(obj, 'border-left-width')) curleft += parseInt(border);
              }
              curleft += obj.offsetLeft;
              curtop += obj.offsetTop;
            }
            while (obj = obj.offsetParent)
          }
          else if (obj.x)
          {
            curleft += obj.x;
            curtop += obj.y;
          }
        var clickX = curleft + 10;
        var clickY = curtop;

          
          document.getElementById('popup_charge').style.left = (clickX - document.getElementById('popup_charge').offsetWidth - (document.getElementById('popup_arrow').offsetWidth /2) +4) + "px"; 
          document.getElementById('popup_charge').style.top = (clickY + (document.getElementById('popup_arrow').offsetHeight /2) - 6) + "px";
          document.getElementById('popup_charge').style.visibility = 'visible';
          document.getElementById('popup_arrow').style.left = (clickX - document.getElementById('popup_arrow').offsetWidth) + "px";
          document.getElementById('popup_arrow').style.top = clickY  + "px";
 
    }
    
    function handlePopupCharge() {
        current_div = document.getElementById('popup_charge');
        noBackground = true;
        show_processing(current_div, 0, 0, noBackground);
        if (receiveReq.readyState == 4) {
            document.getElementById('popup_charge').innerHTML = receiveReq.responseText;
            document.getElementById('popup_arrow').style.visibility = 'visible';
        }
    }
    
    function processPopupCharge(form) {
        var lease_form_id = escape(form.lease_form_id.value);
        var submit = escape(form.save_charge.value);
        var charge_id = escape(form.charge_id.value);
        var charge_date = escape(form.charge_date.value); 
        var charge_amount = escape(form.charge_amount.value); 
        var payment_type_id = escape(form.payment_type_id.value); 
        var charge_notes = escape(form.charge_notes.value);
        var status_id = escape(form.status_id.value);  
        //var charge_balance = escape(document.getElementById('charge_balance').value);
        var last_updated_by = escape(form.charge_last_updated_by.value);
        var last_updated_date = escape(form.charge_last_updated_date.value);     

        var str =   'lease_form_id=' + lease_form_id + 
                    '&' + 'submit=' + submit + 
                    '&' + 'charge_id=' + charge_id + 
                    '&' + 'charge_date=' + charge_date + 
                    '&' + 'charge_amount=' + charge_amount + 
                    '&' + 'payment_type_id=' + payment_type_id + 
                    '&' + 'charge_notes=' + charge_notes + 
                    '&' + 'status_id=' + status_id +
                    '&' + 'last_updated_by=' + last_updated_by +
                    '&' + 'last_updated_date=' + last_updated_date 
                    //'&' + 'charge_balance=' + charge_balance 
                    ;          
        var url = "../includes/charge_popup.php";
        
        
        receiveReq.open("POST", url, true);
        receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        receiveReq.onreadystatechange = handleProcessPopupCharge;
        receiveReq.send(str);
        
    }
    function handleProcessPopupCharge(){
        current_div = document.getElementById('popup_charge');
        noBackground = false;
        show_processing(current_div, 0, 0, noBackground);
        if (receiveReq.readyState == 4) {
            //document.getElementById('popup_charge').innerHTML = receiveReq.responseText;
            var lease_form_id = escape(document.getElementById('lease_form_id').value);
            closeMessageCharge();
            updatePayments(lease_form_id);
        }
    }

    function closeMessageCharge(){
        document.getElementById('popup_charge').style.visibility = 'hidden';
        document.getElementById('popup_arrow').style.visibility = 'hidden'; 
    }
    
    
    
    

    
    
    //POPUP PAYMENTS
    
    function popupPayment(obj,id) {
        receiveReq.open("GET", '../includes/payment_popup.php?payment_id=' + id, true);
        receiveReq.onreadystatechange = handlePopupPayment; 
        receiveReq.send(null);

          var curleft = 0;
          var curtop = 0;
          //var curtop = obj.offsetHeight + 5;
          var border;
          if (obj.offsetParent)
          {
            do
            {
              // XXX: If the element is position: relative we have to add borderWidth
              if (getStyle(obj, 'position') == 'relative')
              {
                if (border = _pub.getStyle(obj, 'border-top-width')) curtop += parseInt(border);
                if (border = _pub.getStyle(obj, 'border-left-width')) curleft += parseInt(border);
              }
              curleft += obj.offsetLeft;
              curtop += obj.offsetTop;
            }
            while (obj = obj.offsetParent)
          }
          else if (obj.x)
          {
            curleft += obj.x;
            curtop += obj.y;
          }
        var clickX = curleft + 10;
        var clickY = curtop;

          //show the popup message and hide with fading effect
          document.getElementById('popup_payment').style.left = (clickX - document.getElementById('popup_payment').offsetWidth - (document.getElementById('popup_arrow').offsetWidth /2) +4) + "px"; 
          document.getElementById('popup_payment').style.top = (clickY + (document.getElementById('popup_arrow').offsetHeight /2) - 6) + "px";
          document.getElementById('popup_payment').style.visibility = 'visible';
          document.getElementById('popup_arrow').style.left = (clickX - document.getElementById('popup_arrow').offsetWidth)  + "px";
          document.getElementById('popup_arrow').style.top = clickY  + "px";
           
    }
    
    function handlePopupPayment() {
        current_div = document.getElementById('popup_payment');
        noBackground = true;
        show_processing(current_div, 0, 0, noBackground);
        if (receiveReq.readyState == 4) {
            document.getElementById('popup_payment').innerHTML = receiveReq.responseText;
            document.getElementById('popup_arrow').style.visibility = 'visible';
        }
    }
    
    function processPopupPayment(form) {
        var lease_form_id = escape(form.pmt_lease_form_id.value);
        var submit = escape(form.save_payment.value);
        var payment_id = escape(form.pmt_payment_id.value);
        var payment_date = escape(form.pmt_payment_date.value); 
        var payment_amount = escape(form.pmt_payment_amount.value); 
        var charge_id = escape(form.pmt_charge_id.value); 
        var status_id = escape(form.pmt_status_id.value);  
        var payment_kind_id = escape(form.pmt_payment_kind_id.value);    
        var payment_kind_info = escape(form.pmt_payment_kind_info.value);
        var payment_notes = escape(form.pmt_payment_notes.value);
        var last_updated_by = escape(form.pmt_last_updated_by.value);
        var last_updated_date = escape(form.pmt_last_updated_date.value);      
        
        var str =   'lease_form_id=' + lease_form_id + 
                    '&' + 'submit=' + submit + 
                    '&' + 'payment_id=' + payment_id + 
                    '&' + 'payment_date=' + payment_date + 
                    '&' + 'payment_amount=' + payment_amount + 
                    '&' + 'charge_id=' + charge_id +  
                    '&' + 'status_id=' + status_id +
                    '&' + 'payment_kind_id=' + payment_kind_id +
                    '&' + 'payment_kind_info=' + payment_kind_info +
                    '&' + 'last_updated_by=' + last_updated_by + 
                    '&' + 'payment_notes=' + payment_notes +
                    '&' + 'last_updated_date=' + last_updated_date 
                    ;
         
        var url = "../includes/payment_popup.php";

        receiveReq.open("POST", url, true);
        receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        receiveReq.onreadystatechange = handleProcessPopupPayment;
        receiveReq.send(str);

    }
    function handleProcessPopupPayment() {
        current_div = document.getElementById('popup_payment');
        noBackground = false;
        show_processing(current_div, 0, 0, noBackground);
        if (receiveReq.readyState == 4) {
            var lease_form_id = escape(document.getElementById('pmt_lease_form_id').value);
            closeMessagePayment();
            updatePayments(lease_form_id);
        }
    }
    
    function closeMessagePayment(){
        document.getElementById('popup_payment').style.visibility = 'hidden';
        document.getElementById('popup_arrow').style.visibility = 'hidden';
    }
    
    
    
    function acceptContract() {
        var accept_eligible = escape(document.getElementById('accept_eligible').value);
        var accept_deposit = escape(document.getElementById('accept_deposit').value);
        var accept_managerSignature = escape(document.getElementById('accept_managerSignature').value);
        var accept_ManagerDateSigned = escape(document.getElementById('accept_ManagerDateSigned').value);
        var contract_id = escape(document.getElementById('accept_contract_id').value);
        var status = escape(document.getElementById('accept_status').value);
        var str = 'eligible='+accept_eligible+'&paid_deposit='+accept_deposit+'&manager_sig_date='+accept_ManagerDateSigned+'&manager_sig='+accept_managerSignature+'&contract_id='+contract_id+'&status='+status;

        var url = "../includes/manager_accept.php";
        receiveReq.open("POST", url, true);
        receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        receiveReq.onreadystatechange = handleAcceptContract;
        receiveReq.send(str);
    }
    function handleAcceptContract() {
        current_div = document.getElementById('accept');
        noBackground = false;
        show_processing(current_div, 0, 0, noBackground);
        if (receiveReq.readyState == 4) {
            document.getElementById('accept').innerHTML = receiveReq.responseText;
        }
    }
    function updateStatus() {
        var details_status = escape(document.getElementById('details_status').value);
        var status_lease_form_id = escape(document.getElementById('status_lease_form_id').value);

        var str = 'status='+details_status+'&contract_id='+status_lease_form_id;

        var url = "../includes/details.php?id=" + status_lease_form_id;
        receiveReq.open("POST", url, true);
        receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        receiveReq.onreadystatechange = handleUpdateStatus;
        receiveReq.send(str);
    }
    function handleUpdateStatus() {
        current_div = document.getElementById('current_new_contracts');
        noBackground = false;
        show_processing(current_div, 0, 0, noBackground);
        if (receiveReq.readyState == 4) {
            document.getElementById('current_new_contracts').innerHTML = receiveReq.responseText;
        }
    }
    function searchAllHousing() {
        var housing_search_semester = escape(document.getElementById('housing_search_semester').value);
        var housing_search_year = escape(document.getElementById('housing_search_year').value);
        var housing_search_property = escape(document.getElementById('housing_search_property').value);
        var housing_search_status = escape(document.getElementById('housing_search_status').value);
        var housing_search_order = escape(document.getElementById('housing_search_order').value);
        var housing_search_text = escape(document.getElementById('housing_search_text').value);
        var housing_search_showing = escape(document.getElementById('total_showing').value);
        var housing_search_archive = escape(document.getElementById('housing_search_archive').checked);
        var search_all_fields = escape(document.getElementById('search_all_fields').checked);
        var str = 'semester='+housing_search_semester+'&total_showing='+housing_search_showing+'&year='+housing_search_year+'&property='+housing_search_property+'&status='+housing_search_status+'&order='+housing_search_order+'&text='+housing_search_text+'&archive='+housing_search_archive+'&search_all_fields='+search_all_fields;

        var url = "../includes/search_housing.php";
        receiveReq.open("POST", url, true);
        receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        receiveReq.onreadystatechange = handleSearchAllHousing;
        receiveReq.send(str);
    }
    function handleSearchAllHousing() {
        current_div = document.getElementById('search_housing');
        noBackground = false;
        show_processing(current_div, 0, -10, noBackground);
        if (receiveReq.readyState == 4) {
            document.getElementById('search_housing').innerHTML = receiveReq.responseText;
        }
    }
    function searchDueDates() {
        var due_dates_semester = escape(document.getElementById('due_dates_semester').value);
        var due_dates_year = escape(document.getElementById('due_dates_year').value);
        var due_dates_property = escape(document.getElementById('due_dates_property').value);
        var due_dates_status = escape(document.getElementById('due_dates_status').value);
        var due_dates_order = escape(document.getElementById('due_dates_order').value);
        //var due_dates_text = escape(document.getElementById('due_dates_text').value);
        var due_dates_begin = escape(document.getElementById('due_dates_begin').value);
        var due_dates_end = escape(document.getElementById('due_dates_end').value);
        var str = 'semester='+due_dates_semester+'&year='+due_dates_year+'&property='+due_dates_property+'&status='+due_dates_status+'&order='+due_dates_order+'&begin='+due_dates_begin+'&end='+due_dates_end;

        var url = "../includes/due_dates.php";
        receiveReq.open("POST", url, true);
        receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        receiveReq.onreadystatechange = handleSearchDueDates;
        receiveReq.send(str);
    }
    function handleSearchDueDates() {
        current_div = document.getElementById('left_due_dates');
        noBackground = false;
        show_processing(current_div, 0, 0, noBackground);
        if (receiveReq.readyState == 4) {
            document.getElementById('left_due_dates').innerHTML = receiveReq.responseText;
        }
    }
    
    
    
    
    
    

    
    
    
    //showing and hiding divs:
    function toggle_div(showHideDiv, switchImgTag) {
        var ele = document.getElementById(showHideDiv);
        var imageEle = document.getElementById(switchImgTag);
        if(ele.style.display == "block") {
                ele.style.display = "none";
        imageEle.innerHTML = '<img src="images/plus.png">';
        }
        else {
                ele.style.display = "block";
                imageEle.innerHTML = '<img src="images/minus.png">';
        }
    }
    
    
    function universalProcessForm(form_id_passed,url_name_passed,div_id_passed) {
        form_id = form_id_passed;
        url_name = url_name_passed;
        div_id = div_id_passed;
        var str = '';
        var elem = document.getElementById(form_id).elements;
        for(var i = 0; i < elem.length; i++){
            if (i == 0){
                if (elem[i].type == "checkbox"){ 
                    str = elem[i].name + "=" + escape(elem[i].checked);
                }else{
                    str = elem[i].name + "=" + escape(elem[i].value);
                }
                    
            }else{
                if (elem[i].type == "checkbox"){ 
                   str += "&" + elem[i].name + "=" + escape(elem[i].checked);
                }else{
                    str += "&" + elem[i].name + "=" + escape(elem[i].value);
                }
            }
        }       
        var url = url_name;
        receiveReq.open("POST", url, true);
        receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        receiveReq.onreadystatechange = handleUniversalProcessForm;
        //alert(str);
        receiveReq.send(str);           
    }
    function handleUniversalProcessForm() {
        current_div = document.getElementById(div_id);
        //alert(current_div.name);
        noBackground = false;
        show_processing(current_div, 0, 0, noBackground);
        if (receiveReq.readyState == 4) {
            document.getElementById(div_id).innerHTML = receiveReq.responseText;
        }
    }
    
    function universalRefresh(url_passed, div_passed) {
        div_name = div_passed;
        receiveReq.open("GET", url_passed, true);
        receiveReq.onreadystatechange = handleUniversalRefresh; 
        receiveReq.send(null);            
    }
    function handleUniversalRefresh() {
        current_div = document.getElementById(div_name);
        noBackground = false;
        show_processing(current_div, 0, 0, noBackground);
        if (receiveReq.readyState == 4) {
            document.getElementById(div_name).innerHTML = receiveReq.responseText;
        }
    }
    
    
        //POPUP attach contract
    
    function popupSearchHousing(obj,id) {
        var objid = document.getElementById(obj);
        receiveReq.open("GET", '../includes/popup_current_housing_search.php', true);
        receiveReq.onreadystatechange = handlePopupSearchHousing; 
        receiveReq.send(null);

        x = findPosX(objid);
        y = findPosY(objid);

          //show the popup message and hide with fading effect
          document.getElementById('popup_search_housing').style.left = x - document.getElementById('popup_search_housing').offsetWidth - (document.getElementById('popup_arrow').offsetWidth /2) +4; 
          if (parseInt(document.getElementById('popup_search_housing').style.left) < 0){
              document.getElementById('popup_search_housing').style.left = 0;
          }
          document.getElementById('popup_search_housing').style.top = y + (document.getElementById('popup_arrow').offsetHeight /2) - 6  + "px";
          document.getElementById('popup_search_housing').style.visibility = 'visible';
          document.getElementById('popup_arrow').style.left = x - document.getElementById('popup_arrow').offsetWidth  + "px";
          document.getElementById('popup_arrow').style.top = y  + "px";
          //alert(document.getElementById('popup_search_housing').style.left + " " + document.getElementById('popup_search_housing').style.top); 
    }
    
    function handlePopupSearchHousing() {
        current_div = document.getElementById('popup_search_housing');
        noBackground = true;
        show_processing(current_div, 0, 0, noBackground);
        if (receiveReq.readyState == 4) {
            document.getElementById('popup_search_housing').innerHTML = receiveReq.responseText;
            document.getElementById('popup_arrow').style.visibility = 'visible';
        }
    }
    
    function process_close_search_housing_small(id, firstname, lastname){
        document.getElementById('lease_form_id_parking_box').value = id;
        document.getElementById('name_student').innerHTML = unescape(lastname) + ", " + unescape(firstname);
        close_housing_small();   
    }
    function close_housing_small(){
        document.getElementById('popup_search_housing').style.visibility = 'hidden';
        document.getElementById('popup_arrow').style.visibility = 'hidden'; 
    }
    

    function checkForPopups(event) {
                
                if (popupOpen() && receiveReq.readyState == 4){
                    //alert(popupOpen());
                    // handle event here e.g.

                    var obj = event.srcElement ? event.srcElement : event.target;
                    var array = new Array(20); 
                    var cnt = 0;
                    var found = false;
                    while (obj.parentNode){
                        array[cnt] = obj.id  
                        obj = obj.parentNode;
                         cnt++;
                    }
                    
                    for (i=0; i<10; i++) {  
                    
                        if(array[i] == 'popup_div' || 
                            array[i] == 'popup_charge' || 
                            array[i] == 'popup_arrow' || 
                            array[i] == 'status_div' || 
                            array[i] == 'loading_bg' || 
                            array[i] == 'loading_image' || 
                            array[i] == 'popup_search_housing'){
                            found = true;
                        }
                    }
                    if (found == false){
                        close_all_popups();
                    } 
                }
            } 

 
    function close_all_popups(){
        document.getElementById('popup_div').style.visibility = 'hidden';
        document.getElementById('popup_charge').style.visibility = 'hidden';
        document.getElementById('popup_arrow').style.visibility = 'hidden';
        document.getElementById('status_div').style.visibility = 'hidden';
        document.getElementById('loading_bg').style.visibility = 'hidden';
        document.getElementById('loading_image').style.visibility = 'hidden';
        document.getElementById('popup_search_housing').style.visibility = 'hidden';
    }
    function popupOpen(){
        if (document.getElementById('popup_div').style.visibility == 'visible'){ return true;}
        if (document.getElementById('popup_charge').style.visibility == 'visible'){ return true;}
        if (document.getElementById('popup_arrow').style.visibility == 'visible'){ return true;}
        if (document.getElementById('status_div').style.visibility == 'visible'){ return true;}
        if (document.getElementById('loading_bg').style.visibility == 'visible'){ return true;}
        if (document.getElementById('loading_image').style.visibility == 'visible'){ return true;}
        if (document.getElementById('popup_search_housing').style.visibility == 'visible'){ return true;}
        return false;
        
    }
    
    function toggleCheckboxes() {
        // written by Daniel P 3/21/073.
        // toggle all checkboxes found on the page      
        var inputlist = document.getElementsByTagName("input");
        for (i = 0; i < inputlist.length; i++) {
            if ( inputlist[i].getAttribute("type") == 'checkbox' ) {
                // look only at input elements that are checkboxes            
                if (inputlist[i].checked)    
                inputlist[i].checked = false            
                else                                
                inputlist[i].checked = true;
            }    
        }
    }
    
    function addtotals(form) {

form.pmt1_total.value = (Number(form.pmt1_rent.value) + Number(form.pmt1_fees.value)).toFixed(2);
form.pmt2_total.value = (Number(form.pmt2_rent.value) + Number(form.pmt2_fees.value)).toFixed(2);
form.pmt3_total.value = (Number(form.pmt3_rent.value) + Number(form.pmt3_fees.value)).toFixed(2);
form.pmt4_total.value = (Number(form.pmt4_rent.value) + Number(form.pmt4_fees.value)).toFixed(2);
form.pmt5_total.value = (Number(form.pmt5_rent.value) + Number(form.pmt5_fees.value)).toFixed(2);
form.pmt6_total.value = (Number(form.pmt6_rent.value) + Number(form.pmt6_fees.value)).toFixed(2);
form.pmt7_total.value = (Number(form.pmt7_rent.value) + Number(form.pmt7_fees.value)).toFixed(2);
form.pmt8_total.value = (Number(form.pmt8_rent.value) + Number(form.pmt8_fees.value)).toFixed(2);
form.pmt9_total.value = (Number(form.pmt9_rent.value) + Number(form.pmt9_fees.value)).toFixed(2);
form.pmt10_total.value = (Number(form.pmt10_rent.value) + Number(form.pmt10_fees.value)).toFixed(2);
form.pmt11_total.value = (Number(form.pmt11_rent.value) + Number(form.pmt11_fees.value)).toFixed(2);
form.pmt12_total.value = (Number(form.pmt12_rent.value) + Number(form.pmt12_fees.value)).toFixed(2);

form.pmt_rent_total.value = (Number(form.pmt1_rent.value) + Number(form.pmt2_rent.value) + Number(form.pmt3_rent.value) + Number(form.pmt4_rent.value) + Number(form.pmt5_rent.value) + Number(form.pmt6_rent.value) + Number(form.pmt7_rent.value) + Number(form.pmt8_rent.value) + Number(form.pmt9_rent.value) + Number(form.pmt10_rent.value) + Number(form.pmt11_rent.value) + Number(form.pmt12_rent.value)).toFixed(2);
form.pmt_fees_total.value = (Number(form.pmt1_fees.value) + Number(form.pmt2_fees.value) + Number(form.pmt3_fees.value) + Number(form.pmt4_fees.value) + Number(form.pmt5_fees.value) + Number(form.pmt6_fees.value) + Number(form.pmt7_fees.value) + Number(form.pmt8_fees.value) + Number(form.pmt9_fees.value) + Number(form.pmt10_fees.value) + Number(form.pmt11_fees.value) + Number(form.pmt12_fees.value)).toFixed(2);
form.pmt_total_all.value = (Number(form.pmt1_total.value) + Number(form.pmt2_total.value) + Number(form.pmt3_total.value) + Number(form.pmt4_total.value) + Number(form.pmt5_total.value) + Number(form.pmt6_total.value) + Number(form.pmt7_total.value) + Number(form.pmt8_total.value) + Number(form.pmt9_total.value) + Number(form.pmt10_total.value) + Number(form.pmt11_total.value) + Number(form.pmt12_total.value)).toFixed(2);

if (form.pmt1_total.value == 0){form.pmt1_total.value = "";}
if (form.pmt2_total.value == 0){form.pmt2_total.value = "";}
if (form.pmt3_total.value == 0){form.pmt3_total.value = "";}
if (form.pmt4_total.value == 0){form.pmt4_total.value = "";}
if (form.pmt5_total.value == 0){form.pmt5_total.value = "";}
if (form.pmt6_total.value == 0){form.pmt6_total.value = "";}
if (form.pmt7_total.value == 0){form.pmt7_total.value = "";}
if (form.pmt8_total.value == 0){form.pmt8_total.value = "";}
if (form.pmt9_total.value == 0){form.pmt9_total.value = "";}
if (form.pmt10_total.value == 0){form.pmt10_total.value = "";}
if (form.pmt11_total.value == 0){form.pmt11_total.value = "";}
if (form.pmt12_total.value == 0){form.pmt12_total.value = "";}

}

function submitenter(myfield,e,form1){
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;
    
    if (keycode == 13)
       {
       //alert(document.search_housing_post.submit());
       //alert("hello!");
       myfield.form.submit();
       //alert(form1.submit());
//       document.myfield.form.submit();
        //form1.submit();
       return false;
       }
    else
       return true;
}