var http = null; //make global

function toggleClipboardEntry(rental_id){
           
    var img = document.getElementById("main_"+rental_id);

    if(img == null)
        modClipboard(rental_id, "rem");
    else{
        if(img.src.indexOf("clipboard.gif") !=-1){
            img.src = 'new-images/icons/clipboard_rem.gif';
            modClipboard(rental_id, "add");        
        }else{
            img.src = 'new-images/icons/clipboard.gif';
            modClipboard(rental_id, "rem");
        }
    }
}
function toggleClipboardEntryMap(rental_id){
           
    var img_map = document.getElementById("main_"+rental_id);
    var img_sidebar = document.getElementById("sidebar_"+rental_id);

    if(img_sidebar.src.indexOf("clipboard.gif") !=-1){
        if(img_map != null)
            img_map.src = 'new-images/icons/clipboard_rem.gif';
        img_sidebar.src = 'new-images/icons/clipboard_rem.gif';
        modClipboard(rental_id, "add");        
    }else{
        if(img_map != null)
            img_map.src = 'new-images/icons/clipboard.gif';
        img_sidebar.src = 'new-images/icons/clipboard.gif';
        modClipboard(rental_id, "rem");
    }
}
function modClipboard(rental_id, action){

    http = getHTTPObject(); // We create the HTTP Object 
    var url = "clipboard.php?action="+action+"&rental_id="+rental_id;
    http.open("GET", url, true);

    http.onreadystatechange = handleHttpResponse;

    http.send(null);

}

function handleHttpResponse() {

  if (http.readyState == 4) {

    // Split the comma delimited response into an array

    //var xmlDoc = http.responseText.split(",");

    //document.getElementById('city').value = results[0];

    //document.getElementById('state').value = results[1];
    
    //alert(http.status+": "+http.statusText);
    //alert(http.getAllResponseHeaders());
    //alert(http.responseXML.getElementsByTagName("rental_id").item(0).firstChild.data);
    //alert(http.responseXML.getElementsByTagName("rental_id").item(0).firstChild.data);
    //alert(http.responseText);
    var shell = document.getElementById("clipboard_shell");
    if(shell){// this is for pages which may not have the clipboard embedded
        shell.innerHTML = http.responseText;
        //alert(http.responseText);
        //alert(shell.innerHTML);
        paintTable(document.getElementById("clipboard")); 
        sortables(document.getElementById("clipboard"));
    }

  }

}
function paintTable(table){

    var bgcolors = new Array(LISTINGS_TABLE_BGCOLOR_ODD, LISTINGS_TABLE_BGCOLOR_EVEN);
    
    color=0;
    if(table != null){
        for (var i=1;i< table.rows.length;i++)
            if(table.rows[i].style.display!="none"){           
                table.rows[i].style.backgroundColor = bgcolors[(color)%2];
                color++;
            }
        }
}
function setRowTextColor(row, color){

    for(var i = 0; i < row.cells.length; i++)
        row.cells[i].style.color=color;
    
}
function getAddRemClipboardImage(rental_id, shell){    
    http = getHTTPObject(); // We create the HTTP Object 
    http.onreadystatechange = function() {
        handleClipboardImageHttpResponse(shell); 
    };
    var url = "get_clipboard_image.php?rental_id="+rental_id;
    http.open("GET", url, true);
    http.send(null);
    //return src;

}
function handleClipboardImageHttpResponse(shell){
  
  var shell = document.getElementById(shell.id);
  //var shell_2 = document.getElementById("shell_2");
      
  if(http.readyState == 4) {
    //alert(http.responseText);
    alert(shell.id);
    //shell2.innerHtml =  "foo";
    shell.innerHtml =  http.responseText;
  }
}
function hideRentalContactNotes(rental_id){
    var cell = document.getElementById("rental_id_"+rental_id);
    cell.innerHTML = "<div id='div_"+rental_id+"' onclick='editRentalContactNotes("+rental_id+");'>"+document.getElementById("notes_"+rental_id).value+"</div>";
}