﻿function getListings() {
    var poststr = "method=listings&userSession=" + ReadCookie('uSess');
    makePOSTRequest('../listing/landlordListingHelper.aspx', poststr, displayListings);
}

function displayListings() {
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
            var xmlstring = http_request.responseText;
            //alert(xmlstring);
            var xmlobject;
            if (typeof (DOMParser) == 'undefined') {
                xmlobject = new ActiveXObject('MSXML.DomDocument');
                xmlobject.async = false;
                xmlobject.loadXML(xmlstring);
            } else {
                xmlobject = (new DOMParser()).parseFromString(xmlstring, 'text/xml');
            }

            var data = xmlobject.getElementsByTagName('listing');
            var dc = "<ul>";
            for (var i = 0; i < data.length; i++) {
                var sd = getNodeValue(data[i], 'shortTitle');
                if (!isdefined(sd) || sd == null) {
                    sd = "Not Available";
                }
                var lId = getNodeValue(data[i], 'listingId');
                var lstatusid = getNodeValue(data[i], 'statusId');

                var ploc = getNodeValue(data[i], 'photo');
                if (ploc == '') {
                    ploc = '../images/forrent.gif';
                }
                dc = dc + "<li><div class=\"upper\"><div class=\"leftcp\">" +
                     sd + "</div>" +
                     "<div class=\"rightcp\">Listing ID " + lId + "</div></div>" +
                     "<div class=\"lower\"><div class=\"onecp\"><div class=\"photo\"><a href=\"#\"";
                     
                if(lstatusid == '1') {
                    dc = dc + "onclick=\"addPreviewListing('" + lId + "');return false;\""; 
                }
                     
                dc = dc + "><img src=\"" +
                     ploc + "\" alt=\"\" title=\"\" width=\"130\" height=\"97\" /></a></div>" +
                     "<div class=\"desc2\"><span>Status:</span><span class=\"red\">" + getNodeValue(data[i], 'statusName');

                if (lstatusid == '1') {
                    dc = dc + "</span><br /><span>Subscription:</span>" + getNodeValue(data[i], 'numDays') + "Days Remaining <a href=\"#\" onclick=\"renewListings('" + lId + "');return false;\">Renew</a><br />";
                } else if (lstatusid == '6') {
                    dc = dc + "</span><br /><span>Subscription:</span>0 Days Remaining <a href=\"#\" onclick=\"renewListings('" + lId + "');return false;\">Renew</a><br />";
                } else if (lstatusid == '7') {
                    dc = dc + "</span><br /><span>Subscription:</span>" + getNodeValue(data[i], 'numDays') + "Days Remaining<br />";
                } else {
                    dc = dc + "</span><br /><span>Subscription:</span>N/A<br />";
                }

                dc = dc + "<span>Photos:</span>" + getNodeValue(data[i], 'photoCount') + "<br />" +
                     "<span>Views:</span>" + getNodeValue(data[i], 'viewCount') + "<br />" +
                     "</div><div style=\"clear: left;\"></div></div><div class=\"twocp\">";

                if (lstatusid == '1') {
                    dc = dc + "<a href=\"#\" onclick=\"addPreviewListing('" + lId + "');return false;\">Preview</a><br />";
                }

                if (lstatusid != '6' && lstatusid != '8' && lstatusid != '7') {
                    dc = dc + "<a href=\"#\" onclick=\"editListing('" + lId + "');return false;\">Edit Details</a><br />" +
                         "<a href=\"#\" onclick=\"addEditPhoto('" + lId + "');return false;\">Add/Edit Photos</a><br />";
                }

                if (lstatusid == '1') {
                    dc = dc + "<a href=\"#\" onclick=\"deleteListings('" + lId + "');return false;\">Disable</a><br />" +
                     "<p style=\"margin-top: 4px;\"><strong>Rented:</strong><br />" +
                     "<input type=\"radio\" name=\"rented" + lId + "\" value=\"no\" checked=\"checked\" onClick=\"updateRent('0','" + lId +
                     "')\"/> No <input type=\"radio\" name=\"rented" + lId + "\" value=\"yes\" onClick=\"updateRent('1','" + lId +
                     "')\"/> Yes<br /><span class=\"italicp\">Marking YES, will archive your listing.</span></p>";
                }

                if (lstatusid == '7') {
                    dc = dc + "<a href=\"#\" onclick=\"deleteListings('" + lId + "');return false;\">Disable</a><br />" +
                     "<p style=\"margin-top: 4px;\"><strong>Rented:</strong><br />" +
                     "<input type=\"radio\" name=\"rented" + lId + "\" value=\"no\" onClick=\"updateRent('0','" + lId +
                     "')\"/> No <input type=\"radio\" name=\"rented" + lId + "\" value=\"yes\" checked=\"checked\" onClick=\"updateRent('1','" + lId +
                     "');return false;\"/> Yes<br /><span class=\"italicp\">Marking YES, will archive your listing.</span></p>";
                }

                dc = dc + "</div></div></li>";
            }

            dc = dc + "</ul>";

            document.getElementById('mylistsspan').innerHTML = dc;
        } else {
            //alert('There was a problem with the request.');
        }
    }
}

function addEditPhoto(lId) {
    window.location = "../cp_landlordphoto.html?listingId=" + lId;
}

function addPreviewListing(lId) {
    window.location = "../searchByIdDetails.aspx?listingId=" + lId + "&returnPage=landlordlisting.aspx";
}

function editListing(lId) {
    window.location = "../editListing.aspx?listingId=" + lId;
}

function deleteListings(lId) {
    var input_box = confirm("You choose to disable listing " + lId + ". Once the listing had been disabled, you can only renew it inorder for it to be reactivated. Click OK to disable this listing or Cancel to cancel this request.");
    if (input_box == true) {
        var poststr = "method=deletelistings&userSession=" + ReadCookie('uSess') + "&listingId=" + lId;
        makePOSTRequest('../listing/landlordListingHelper.aspx', poststr, displayListings);
    }
}

function renewListings(lId) {
    var poststr = "method=renewlisting&userSession=" + ReadCookie('uSess') + "&listingId=" + lId;
    makePOSTRequest('../listing/landlordListingHelper.aspx', poststr, displayListings);
}

function myListing() {
    window.location = "../cp_landlord.html";
}

function updateRent(rentedstatus, lId) {
    var poststr = "method=updateRentedlisting&userSession=" + ReadCookie('uSess') + "&listingId=" + lId + "&rented=" + rentedstatus;
    makePOSTRequest('../listing/landlordListingHelper.aspx', poststr, displayListings);
}

function myAccount() {
    window.location = "../landlordMyAccountEdit.aspx";
}

function displayName() {
    displayHeader('landlords');
    displayFooter();

    var str = ReadCookie('uSess');
    if (str == null || str == '') {
        document.getElementById('notsignin').style.display = "block";
        document.getElementById('alreadysignin').style.display = "none";
    } else {
        document.getElementById('notsignin').style.display = "none";
        document.getElementById('alreadysignin').style.display = "block";
        document.getElementById('usernamespan').innerHTML = ReadCookie('dName');
    }
}

function logout() {
    var str = ReadCookie('uSess');
    window.location = "../user/processLogOut.aspx?userSession=" + encodeURI(str);
}

function addListing() {
    window.location = "../step1_property_type.html";
}

function processUserAccess() {
    var poststr = "method=validateLandLordCPAccess&userSession=" + ReadCookie('uSess');
    makePOSTRequest('../user/validateUser.aspx', poststr, getAccessResult);
}

function getAccessResult() {
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
            var xmlstring = http_request.responseText;
            //alert(xmlstring);
            var xmlobject;
            if (typeof (DOMParser) == 'undefined') {
                xmlobject = new ActiveXObject('MSXML.DomDocument');
                xmlobject.async = false;
                xmlobject.loadXML(xmlstring);
            } else {
                xmlobject = (new DOMParser()).parseFromString(xmlstring, 'text/xml');
            }

            var data = xmlobject.getElementsByTagName('landlordcp');
            if (data.length == 1) {
                var r = getNodeValue(data[0], 'result');
                if (r == '1') {
                    getListings();
                } else {
                    window.location = "../createaccount.aspx?ErrCode=PG001";
                }
            } else {
                window.location = "../createaccount.aspx?ErrCode=PG001";
            }
        } else {
            window.location = "../createaccount.aspx?ErrCode=PG001";
        }
    }
}

