﻿function getSearchResults() {
    var str = ReadCookie('favlisting');
    //alert(str);
    if (str != null && str != '') {
        var sVal = document.getElementById('hdSortByVal').value;
        var poststr = "method=getToFavoriteListing&listingIds=" + encodeURI(str) + "&sortybyval=" + encodeURI(sVal);
        makePOSTRequest('../listing/SearchListingHelper.aspx', poststr, displayListingResults);
    } else {
        document.getElementById('mylistingresultsspan').innerHTML = '';
    }
}

function displayListingResults() {
    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 lId = getNodeValue(data[i], 'listingId');
                var ploc = getNodeValue(data[i], 'photo');
                if (ploc == '') {
                    ploc = '../images/forrent.gif';
                }
                dc = dc + "<li><div class=\"upper\"><div class=\"leftcp\">" +
                     getNodeValue(data[i], 'ListingShortTitle') + "</div>" +
                     "</div>" +
                     "<div class=\"lower\"><div class=\"one\"><div class=\"photo\"><a href=\"#\" onclick=\"viewListingDetail('" + lId + "');return false;\"><img src=\"" + ploc +
                     "\" alt=\"\" title=\"\" width=\"130\" height=\"97\" /></a></div>" +
                     "<div class=\"desc\">" +
                     "<table cellpadding=\"0\" cellspacing=\"0\" class=\"form-table\">" +
                     "<tr>" +
                     "<td style=\"padding-left:2px;padding-bottom:2px;width:130px;\"><strong>Address:</strong></td><td style=\"padding-left:2px;padding-bottom:2px;width:130px;\">" + getNodeValue(data[i], 'Address') + "</td>" +
                     "</tr><tr><td style=\"padding-left:2px;padding-bottom:2px;width:130px;\"><strong>City:</strong></td><td style=\"padding-left:2px;padding-bottom:2px;width:130px;\">" + getNodeValue(data[i], 'CityName') + "</td>" +
                     "</tr><tr><td style=\"padding-left:2px;padding-bottom:2px;width:130px;\"><strong>Vancouver District:</strong></td><td style=\"padding-left:2px;padding-bottom:2px;width:130px;\">" + getNodeValue(data[i], 'CommunityName') + "</td>" +
                     "</tr><tr><td style=\"padding-left:2px;padding-bottom:2px;width:130px;\"><strong>Rent:</strong></td><td style=\"padding-left:2px;padding-bottom:2px;width:130px;\">$" + getNodeValue(data[i], 'MonthlyRent') + "</td>" +
                     "</tr><tr><td style=\"padding-left:2px;padding-bottom:2px;width:130px;\"><strong>Square Feet:</strong></td><td style=\"padding-left:2px;padding-bottom:2px;width:130px;\">" + getNodeValue(data[i], 'SquareFeet') + "</td>" +
                     "</tr><tr><td style=\"padding-left:2px;padding-bottom:2px;width:130px;\"><strong>Bedrooms:</strong></td><td style=\"padding-left:2px;padding-bottom:2px;width:130px;\">" + getNodeValue(data[i], 'BedroomType') + "</td>" +
                     "</tr><tr><td style=\"padding-left:2px;padding-bottom:2px;width:130px;\"><strong>Bathrooms:</strong></td><td style=\"padding-left:2px;padding-bottom:2px;width:130px;\">" + getNodeValue(data[i], 'BathroomType') + "</td>" +
                     "</tr></table>" +
                     "</div><div style=\"clear: left;\"></div>" +
                     "<p style=\"margin-top: 3px;\">" + getNodeValue(data[i], 'ListingLongDescription') + "</p>" +
                     "</div><div class=\"two\"><a href=\"#\" onclick=\"viewListingDetail('" + lId + "');return false;\"><img src=\"images/view_btn.gif\" alt=\"\" title=\"\" /></a>" +
                     "<a href=\"#\" onclick=\"sendListingEmail('" + lId + "');return false;\"><img src=\"images/email_btn.gif\" alt=\"\" title=\"\" /></a>" +
                     "<a href=\"#\" onclick=\"deleteFavourite('" + lId + "');return false;\"><img src=\"images/delete.gif\" alt=\"\" title=\"\" /></a>" +
                     "</div></div></li>";
            }
            dc = dc + "</ul>";
            document.getElementById('mylistingresultsspan').innerHTML = dc;

        } else {
            //alert('There was a problem with the request.');
        }
    }
}

function viewListingDetail(lId) {
    window.location = "../searchByIdDetails.aspx?listingId=" + lId + "&returnPage=myfavourite.html";
}

function SortListings(sval) {
    document.getElementById('hdSortByVal').value = sval;
    getSearchResults('');
}

function deleteFavourite(lId) {
    var str2 = ReadCookie('favlisting');
    if (str2 != null && str2 != '') {
        var str = '';
        if (str2.indexOf(lId + ',') > -1) {
            str = str2.replace(lId + ',', "");
        } else if (str2.indexOf(',' + lId + ',') > -1) {
            str = str2.replace(',' + lId, "");
        } else if (str2.indexOf(',' + lId) > -1) {
            str = str2.replace(',' + lId, "");
        }
        setCookie('favlisting', str, 30);
    }
    getSearchResults();
}

function getFavResult() {
    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('listingresult');
            var resultstatus = getNodeValue(data[0], 'result');
            if (resultstatus == '1') {
                alert("This listing had been removed from your favourite listing.");
                getSearchResults();
            } else {
                alert("This listing is already on your favourite listing.");
            }

        } else {
            //alert('There was a problem with the request.');
        }
    }
}

function myAccount() {
    window.location = "../tentantMyAccountEdit.aspx";
}

function myFavourite() {
    window.location = "../myfavourite.html";
}

function sendListingEmail(lId) {
    var dc = "<form class=\"cmxform\" id=\"form2\" method=\"post\" action=\"\">" +
             "<input type=\"hidden\" name=\"emailListingId\" id=\"emailListingId\" value=\"" + lId + "\" />" +
             "<div class=\"lcp\"><span id=\"emailmsgspan\"></span></div>" +
             "<div class=\"lcp\"><p><strong>E-mail to a friend listing " + lId + " :</strong></p></div>" +
             "<div class=\"lcp\">Friends Email:</div>" +
             "<div class=\"rs\"><input type=\"text\" id=\"email\" name=\"femail\" style=\"width: 340px; background: #ececec; border: 1px solid #b4b4b4; border-collapse: collapse;\" /></div>" +
             "<div class=\"ls\">Your Name:</div>" +
             "<div class=\"rs\"><input id=\"yname\" type=\"text\" name=\"yname\" style=\"width: 340px; background: #ececec; border: 1px solid #b4b4b4; border-collapse: collapse;\" /></div>" +
             "<div class=\"ls\">Your Email:</div>" +
             "<div class=\"rs\"><input id=\"yemail\" type=\"text\" name=\"yemail\" style=\"width: 340px; background: #ececec; border: 1px solid #b4b4b4; border-collapse: collapse;\" /></div>" +
             "<div class=\"ls\">Your Message:</div>" +
             "<div class=\"rs\"><textarea id=\"ymessage\" name=\"ymessage\" style=\"width: 340px; height: 80px; background: #ececec; border: 1px solid #b4b4b4; border-collapse: collapse;\" rows=\"\" cols=\"\"></textarea></div>" +
             "<div class=\"ls\">&nbsp;</div>" +
             "<div class=\"rs\"><a href=\"#\" onclick=\"processListingEmail();return false;\"><img src=\"images/send_btn.gif\" alt=\"\" title=\"\" /></a> &nbsp; <a href=\"#\" onclick=\"getSearchResults();return false;\"><img src=\"images/go_back.gif\" width=\"74px\" height=\"31\" alt=\"go back to listing result\" title=\"\" /></a></div>" +
             "</form>";

    document.getElementById('mylistingresultsspan').innerHTML = dc;
}

function processListingEmail() {
        var iscont = '1';
        var dc = "<br/><div class=\"error\"><div class=\"lcp\">Please correct the following error(s):";
    var str = document.getElementById('email').value;
    if (stringEmpty(str)) {
        dc = dc + getHTMLErrorMsg('Please enter Friends Email.');
        iscont = '0';
    } else if(noAtSign(str) || nothingBeforeAt(str) || noValidPeriod(str) || noValidSuffix(str)) {
        dc = dc + geHTMLErrorMsg('Friends Email is entered in an invalid format. Please enter email address in XXX@XXX.XX format.');
        iscont = '0';
    }

    str = document.getElementById('yname').value;
    if (stringEmpty(str)) {
        dc = dc + getHTMLErrorMsg('Please enter Your Name.');
        iscont = '0';
    }

    str = document.getElementById('yemail').value;
    if (stringEmpty(str)) {
        dc = dc + getHTMLErrorMsg('Please enter Your Email.');
        iscont = '0';
    } else if (noAtSign(str) || nothingBeforeAt(str) || noValidPeriod(str) || noValidSuffix(str)) {
        dc = dc + getHTMLErrorMsg('Your Email is entered in an invalid format. Please enter email address in XXX@XXX.XX format.');
        iscont = '0';
    }

    str = document.getElementById('email').value;
    var str2 = document.getElementById('yemail').value;
    if (str == str2) {
        dc = dc + getHTMLErrorMsg('Friends Email and Your Email cannot be the same.');
        iscont = '0';
    }

    if (iscont == '0') {
        document.getElementById('emailmsgspan').innerHTML = dc;
    } else {
        var poststr = "method=sendListingEmail&listingId=" + document.getElementById('emailListingId').value +
                  "&femail=" + encodeURI(document.getElementById('email').value) +
                  "&yname=" + encodeURI(document.getElementById('yname').value) +
                  "&yemail=" + encodeURI(document.getElementById('yemail').value) +
                  "&ymessage=" + encodeURI(document.getElementById('ymessage').value);
        makePOSTRequest('../listing/SearchListingHelper.aspx', poststr, getListingEmailResult);
    }
}

function getListingEmailResult() {
    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('listingemail');
            var resultstatus = getNodeValue(data[0], 'result');

            var dc = "<div class=\"lcp\">";

            if (resultstatus == '1') {
                dc = dc + "<p><strong>The listing you selected had been send to your friend.</strong></p>";
            } else {
                dc = dc + "<p><strong>Sorry RentSooner was not able to send the listing you selected to your friend. Please try again.</strong></p>";
            }

            dc = dc + "</div><div class=\"lcp\"><a href=\"#\" onclick=\"getSearchResults();return false;\"><img src=\"images/go_back.gif\" width=\"74px\" height=\"31\" alt=\"go back to listing result\" title=\"\" /></a></div>";
            document.getElementById('mylistingresultsspan').innerHTML = dc;
        } else {
            //alert('There was a problem with the request.');
        }
    }
}

function renterContent() {
    window.location = "../cp_tenant.html";
}
