







var doc = null;


function addWishListItem(cardId) {
   var onLoad = function(result){
      var el = document.getElementById("addWish" + cardId);
      var str = "Remove from wish list";
      var FGCOLOR = '#EAE0C5';
      var TEXTCOLOR = '#333333';
      el.innerHTML = '<a style="margin-right: 12px; _margin-right: 18px;" onclick="removeWishListItem(\''+cardId+'\')" onmouseover="return overlib(\''+str+'\', WIDTH, 120, FGCOLOR, \''+FGCOLOR+'\', TEXTCOLOR, \''+TEXTCOLOR+'\');" onmouseout="return nd();" style="cursor: pointer;"><img src="/luxurylisting/images/icons/added_wishlist.gif" alt=""/></a>';

      var el2 = document.getElementById("addWish" + cardId + "2");
      el2.innerHTML = '<a style="margin-right: 12px; _margin-right: 18px;" onclick="removeWishListItem(\''+cardId+'\')" onmouseover="return overlib(\''+str+'\', WIDTH, 120, FGCOLOR, \''+FGCOLOR+'\', TEXTCOLOR, \''+TEXTCOLOR+'\');" onmouseout="return nd();" style="cursor: pointer;"><img src="/luxurylisting/images/icons/added_wishlist.gif" alt=""/></a>';
    };	

   doc = new XML();  
   doc.addListener(onLoad);
   doc.setAttribute("cardId",cardId);	  
   doc.setAttribute("uniqId",Math.round(Math.random()*10000));
   doc.sendAndLoad("/luxurylisting/myaccount/wishlist/add.do");	
}





function addWishListItemNotLogged(cardId)
{
    if(confirm("To add an item to your wish list, you must be registered and logged in. Do you want to be taken to the registration form?"))
    {
        location.href="/luxurylisting/show-register-form-addToWishlist-" + cardId + ".do";
    }
}

function removeWishListItem(cardId)
{      
    if(confirm("Do you really want to remove this listing from your wish list ?"))
    {
        var onLoad = function(result)
        {
            var el = document.getElementById("addWish" + cardId);
	
            var str = "Add to wish list";
                      var FGCOLOR = '#EAE0C5';
                      var TEXTCOLOR = '#333333';
                      el.innerHTML = '<a style="margin-right: 12px; _margin-right: 18px;" onclick="addWishListItem(\''+cardId+'\')" onmouseover="return overlib(\''+str+'\', WIDTH, 120, FGCOLOR, \''+FGCOLOR+'\', TEXTCOLOR, \''+TEXTCOLOR+'\');" onmouseout="return nd();" style="cursor: pointer;"><img src="/luxurylisting/images/icons/add_wishlist.gif" alt=""/></a>';
                
                      var el2 = document.getElementById("addWish" + cardId + "2");
                      el2.innerHTML = '<a style="margin-right: 12px; _margin-right: 18px;" onclick="addWishListItem(\''+cardId+'\')" onmouseover="return overlib(\''+str+'\', WIDTH, 120, FGCOLOR, \''+FGCOLOR+'\', TEXTCOLOR, \''+TEXTCOLOR+'\');" onmouseout="return nd();" style="cursor: pointer;"><img src="/luxurylisting/images/icons/add_wishlist.gif" alt=""/></a>';
        };

        doc = new XML();
        doc.addListener(onLoad);
        doc.setAttribute("cardId",cardId);	  
        doc.setAttribute("uniqId",Math.round(Math.random()*10000));
        doc.sendAndLoad("/luxurylisting/myaccount/wishlist/remove.do");
    }
}

function removeWishListItemFromAccount(cardId)
{      
    if(confirm("Do you really want to remove this listing from your wishlist ?"))
    {
        var onLoad = function(result)
        {
            var tab = document.getElementById("wishTable");
            var row = document.getElementById("wish" + cardId);
            tab.deleteRow(row.rowIndex);	
        };

        doc = new XML();
        doc.addListener(onLoad);
        doc.setAttribute("cardId",cardId);	  
        doc.setAttribute("uniqId",Math.round(Math.random()*10000));
        doc.sendAndLoad("/luxurylisting/myaccount/wishlist/remove.do");
    }
}



function checkIfCardIsUsedInCommunication(type, cardId, callback)
{      	
    var onLoad = function(result)
    {
      callback(type, cardId, result.responseText);
    };

    doc = new XML();
    doc.addListener(onLoad);
    doc.setAttribute("cardId",cardId);	  
    doc.setAttribute("uniqId",Math.round(Math.random()*10000));
    doc.sendAndLoad("/luxurylisting/communication/campaign/isCardUsed.do");	
}



function saveSearch(searchId)
{      	
    var onLoad = function(result)
    {
      alert("The search was saved successfully.");
      document.getElementById("saveSearch").style.display = "none";
    };

    doc = new XML();
    doc.addListener(onLoad);
    doc.setAttribute("searchId",searchId);	  
    doc.setAttribute("uniqId",Math.round(Math.random()*10000));
    doc.sendAndLoad("/luxurylisting/myaccount/search/save.do");	
}


function removeListing(type, id, isUsedInCommunication)
{
    if(isUsedInCommunication == "true"){
        if(confirm("This listing is used in a Luxury Communication campaign. If you delete the listing now, the campaign will be deleted too. Do you want to delete this listing anyway ?"))
        {
            location.href = "/luxurylisting/remove/" + type + "-" + id + ".do";
        }
    }else{
        if(confirm("Do you really want to delete the listing #" + id + " ?"))
        {
            location.href = "/luxurylisting/remove/" + type + "-" + id + ".do";
        }
    }
}


function closeReadingTable(cardId){
   resetReadingTable(cardId); 
   document.getElementById("reading"+cardId).style.display = "none";
}

function resetReadingTable(cardId){
   var masterTable = document.getElementById("reading"+cardId);
   var readingTable = masterTable.getElementsByTagName("table")[0];  
   while(readingTable.rows.length > 1){
    readingTable.deleteRow(1);
   }  
   readingTable.setAttribute("hasPager","false");
}

function createPager(cardId, limit, offset, count)
{    
    var output = "<strong>Go to page &gt;&gt;</strong> ";

    // s'il y a plus d'une page
    if(count > limit)
    {   
        var length = Math.ceil(count/limit);
        var temp = offset/limit;
       
        for(var i=0; i<length; i++){            
            if(i == temp){
                 output += "<strong style='font-size:12px; margin:2px;text-decoration:underline;'>" + (i+1) + "</strong> ";
            }else{
                 output += "<a href=\"javascript:showReadings('"+cardId+"',"+limit+","+(i*limit)+")\" >" + (i+1) + "</a> ";
            }
        }
    }
    
    return output;
}

function showReadings(cardId, limit, offset)
{   
    if(isNaN(limit)) limit = 10;
    if(isNaN(offset)) offset = 0;

    var onLoad = function(result)
    {   
       
        var j = 1;
        var resultDoc = result.responseXML.firstChild;
        
        //patch pour IE
        while(resultDoc.childNodes.length == 0){       
            resultDoc = result.responseXML.childNodes.item(j++);
        }
       
        var readings = resultDoc.childNodes;   
        var masterTable = document.getElementById("reading"+cardId);
        var readingTable = masterTable.getElementsByTagName("table")[0];

        var cpt = 0;
               
        var i;
        
        try{

            if(readingTable.getAttribute("hasPager") == "true"){
                 readingTable.deleteRow(readingTable.rows.length-1);
            }
        
            for(i=0; i<readings.length; i++)
            {
                var node = readings[i];
                
                if(node.nodeType == 1)
                {                   
                    if(!readingTable.rows.item(cpt+1))
                    {
                        var atts = node.attributes;  
                        var row = readingTable.insertRow((cpt++)+1);
                        row.bgColor = "#FFFFFF";
                        row.className = "myListingTopRow";
                        row.insertCell(0).appendChild(document.createTextNode(node.getAttribute("rname")));
                        row.cells.item(0).align="left";
                        var timeCell = row.insertCell(1);
                        timeCell.appendChild(document.createTextNode(node.getAttribute("rtime")));
                        timeCell.setAttribute("colspan", "2");
                        timeCell.colSpan = 2;
                        timeCell.align = "right";
                    }
                    else
                    {                       
                        var row = readingTable.rows.item((cpt++)+1);
                        row.cells.item(0).innerHTML = node.getAttribute("rname");
                        row.cells.item(1).innerHTML = node.getAttribute("rtime");                     
                    }
                }
            }            
        }catch(e){
           
        }
        
        
      
        var count = parseInt(resultDoc.getAttribute("count"));
        var limit = parseInt(resultDoc.getAttribute("limit"));
        var offset = parseInt(resultDoc.getAttribute("offset"));
    
        if(cpt < limit){
            while(readingTable.rows.length > (cpt+1)){
                readingTable.deleteRow(readingTable.rows.length-1);
            }
        }
        
        if(Math.ceil(count/limit) > 1)
        {                  
            readingTable.setAttribute("hasPager","true");
        
            var pagerHTML = createPager(cardId, limit, offset, count);

            var row = readingTable.insertRow((cpt++)+1);
            row.bgColor = "#FFFFFF";
            row.className = "myListingTopRow statsPagerLink";    
            var timeCell = row.insertCell(0);

            var pagerContainer = document.createElement("div");

            pagerContainer.innerHTML = pagerHTML;

            timeCell.appendChild(pagerContainer);
            timeCell.setAttribute("colspan", "3");
            timeCell.colSpan = 3;
            timeCell.align = "left";

        }else{
            readingTable.setAttribute("hasPager","false");
        }
       
        masterTable.style.display = document.all ? "block" : "table-row";
    }

    doc = new XML();  
    doc.addListener(onLoad);
    doc.setAttribute("cardId",cardId);	  
    doc.setAttribute("limit",limit);	  
    doc.setAttribute("offset",offset);	  
    doc.setAttribute("uniqId",Math.round(Math.random()*10000));
    doc.sendAndLoad("/luxurylisting/myaccount/getCardReadings.do");	    
}


function checkIfSearchIsUsedInAlert(searchId)
{      	
    var onLoad = function(result)
    {
        if(result.responseText == "true"){
            if(confirm("This search is used by an alert that you have created. If you delete the search now, the alert will be deleted too. Do you want to delete this search anyway ?"))
            {
                location.href = "/luxurylisting/myaccount/search/remove.do?searchId=" + searchId + "#searchSection";
            }
        }else{
            if(confirm("Do you really want to delete this search?"))
            {
                location.href = "/luxurylisting/myaccount/search/remove.do?searchId=" + searchId + "#searchSection";
            }
        }        
    };

    doc = new XML();
    doc.addListener(onLoad);
    doc.setAttribute("searchId",searchId);	  
    doc.setAttribute("uniqId",Math.round(Math.random()*10000));
    doc.sendAndLoad("/luxurylisting/myaccount/search/isUsed.do");	
}



function checkNbBeforeMonthExpiration(cardId)
{      	
    var onLoad = function(result)
    {
        if(!isNaN(result.responseText)){
            var days = Number(result.responseText);
            if(confirm("The activation of listing #"+cardId+" is still valid for "+days+" days for the current month.  If you desactivate it now, you will lose these days.  Do you really want to desactivate this card ?")){
                window.location = "/luxurylisting/activation/card/desactivate.do?cardId="+cardId;
            }
        }
    };

    doc = new XML();
    doc.addListener(onLoad);
    doc.setAttribute("cardId",cardId);	  
    doc.setAttribute("uniqId",Math.round(Math.random()*10000));
    doc.sendAndLoad("/luxurylisting/activation/card/daysBeforeMonthExpiration.do");	
}

function deleteUserPhoto(type){       
    if(confirm("Are you sure that you want to delete this picture ?")){
       var onLoad = function(result){
            document.getElementById(type+"_infos").innerHTML = "&nbsp;";
       };

       doc = new XML();  
       doc.addListener(onLoad);
       doc.setAttribute("type",type);	  
       doc.setAttribute("uniqId",Math.round(Math.random()*10000));
       doc.sendAndLoad("/luxurylisting/myaccount/photo/delete.do");	
   }
}


 function sendToFriend(cardId, txtEmail, txtComment, cardType){
    var email = document.getElementById(txtEmail).value;
    var comment = document.getElementById(txtComment).value;

    var onLoad = function(result){
        if(result.responseText.indexOf("true") != -1){
            alert("The email message was successfully sent.");
        }else{
            alert("Please enter a valid email address.");
        }
        document.getElementById(txtEmail).value = "";
        showSendToFriendForm('sendToFriendForm');
    }

    doc = new XML();
    doc.addListener(onLoad);
    doc.setAttribute("cardId", cardId);	  
    doc.setAttribute("email", email);
    doc.setAttribute("comment", comment);
    doc.setAttribute("type", cardType);
    doc.setAttribute("cardUrl", window.location); 
    doc.sendAndLoad("/luxurylisting/card/sendToFriend.do");
}