﻿if (!TD)
	var TD = {};

TD.projectLifeTimes = {
    POWERED_BY_DEFAULT: "metroland",
    POWERED_BY_COOKIE_NAME: "PoweredBy",

    ReadCookie: function(name) {
        var cookieValue = "";
        var search = name + "=";
        if (document.cookie.length > 0) {
            offset = document.cookie.indexOf(search);
            if (offset != -1) {
                offset += search.length;
                end = document.cookie.indexOf(";", offset);
                if (end == -1) end = document.cookie.length;
                cookieValue = unescape(document.cookie.substring(offset, end))
            }
        }
        return cookieValue;
    },

    PopupContactForm: function(listingID) {
        var prefix = TD.projectLifeTimes.GetPoweredByValue();
        Shadowbox_iframe('/' + prefix + '/app/contactformpopup/' + listingID, 'Contact Form', 680, 360);
        return false;
    },
    /// <reference path="file:///C:\DOCUME~1\mamin\LOCALS~1\Temp\r6vnw9q7.bmp" />


    SetPoweredByValue: function(value) {
        //      var url = new String(document.location.href);
        //      var urlArray = url.split('/');
        var expiryDate = new Date();
        //    var poweredByValue = new String(TD.projectLifeTimes.POWERED_BY_DEFAULT); // default value
        var cookieValue = new String(TD.projectLifeTimes.ReadCookie(TD.projectLifeTimes.POWERED_BY_COOKIE_NAME)).toLowerCase();
        //   alert('inside set, before setting cookie, read cookie value = ' + cookieValue);
        // if (poweredByValue.toLowerCase() != "control-update" && poweredByValue.toLowerCase() != "toplets") {
        //            if (value == "" || urlArray.length < 4) {
        //                document.location = "/" + poweredByValue;
        //                return;
        //            }

        //            poweredByValue = new String(urlArray[3]);
        //            var endIndex = poweredByValue.indexOf("?", 0);
        //            if (endIndex > -1)
        //                poweredByValue = poweredByValue.substring(0, endIndex);

        // check first if the cookie exists:
        // if the cookie does not exist or is empty, create it OR
        // if the cookie already exists and does not equal the current poweredByValue, change it
        if (cookieValue == "" | cookieValue != value) {
            expiryDate.setDate(expiryDate.getDate() + 1);
            document.cookie = TD.projectLifeTimes.POWERED_BY_COOKIE_NAME + "=" + escape(value) + ";expires=" + expiryDate.toGMTString() + "; path=/";

            // redirect to ensure all controls on the page pick up the new
            // cookie value, which has been set to equal poweredByValue
            //                if (cookieValue != "")
            //                    document.location = "/" + poweredByValue;
            // }
        }
    },

    GetPoweredByValue: function() {
        var cookieValue = new String(TD.projectLifeTimes.ReadCookie(TD.projectLifeTimes.POWERED_BY_COOKIE_NAME)).toLowerCase();
        if (cookieValue == "")
            cookieValue = TD.projectLifeTimes.POWERED_BY_DEFAULT;
        return cookieValue;
    },

    ProcessLogin: function(returnurl) {
        if (window.event) window.event.returnValue = false;
        var loc = '/' +
        TD.projectLifeTimes.GetPoweredByValue() + '/app/Login?ReturnUrl=' + escape(returnurl);
        if (TD.Framework.IsInFrame())
            window.top.document.location.href = loc;
        else
            document.location.href = loc;
    },

    ProcessLogout: function(returnurl) {
        var loc = '/' +
        TD.projectLifeTimes.GetPoweredByValue() + '/app/logout?ReturnUrl=' + escape(returnurl);
        if (TD.Framework.IsInFrame())
            window.top.document.location.href = loc;
        else
            document.location.href = loc;
    },

    ProcessLinkToMyProfile: function() {
        var loc = '/' +
        TD.projectLifeTimes.GetPoweredByValue() + '/SecureArea/MyProfile';
        if (TD.Framework.IsInFrame())
            window.top.document.location.href = loc;
        else
            document.location.href = loc;
    },

    datePickerChanged: function(calendar, date, datepickerID, textBoxID) {
        var dateObj = document.getElementById(datepickerID);
        var textObj = document.getElementById(textBoxID);
        if (dateObj)
            dateObj.innerHTML = "";
        if (textObj) {
            date = date.toString().substring(0, date.toString().indexOf(" ", 0));
            textObj.value = date == "" ? "YYYY-MM-DD" : date;
        }
    },

    AdvancedSearch: function(lnameID, categoryID, publicationID, keywordID, path, refineSearch) {
        var lnameObj = document.getElementById(lnameID);
        var categoryObj = document.getElementById(categoryID);
        var keywordObj = document.getElementById(keywordID);
        var pubObj = document.getElementById(publicationID);

        var url = "/search?atype=Classified&sort=EnterDate+desc";
        var qs = "";

        url = "/" + path + url;

        // last name:
        if (lnameObj && lnameObj.value != "")
            qs += "&r=(LastName1:" + lnameObj.value + " OR LastName2:" + lnameObj.value + ")&kw=" + lnameObj.value;

        // keyword(s):
        if (keywordObj && keywordObj.value != "")
            qs += "&q=" + keywordObj.value;

        // category:
        if (categoryObj && categoryObj.value != "0")
            qs += "&r=FullCategory:%22/Classified/" + escape(categoryObj.value) + "%22";

        // publication:
        if (pubObj && pubObj.value != "0") {
            if (pubObj.value == "/Publication/Toronto/Inside Toronto") {
                qs += "&r=(FullCategory:%22" + pubObj.value + "%22" + " OR FullCategory:%22/Publication/York/York Region%22)";
            }
            else {
                qs += "&r=FullCategory:%22" + pubObj.value + "%22";
            }
        }

        if (refineSearch != "")
            qs += "&r=FullCategory:\"" + refineSearch + "\"";

        if (qs == "")
            qs = "&r=all:1";

        url += qs;

        window.location = url;
    },

    // move bookmark1 into bookmark2
    // this is done so that we can load the bookmark stuff at the end of a file.
    MoveBookmarkTool: function(bookmarkID1, bookmarkID2) {
        var bm1 = document.getElementById(bookmarkID1);
        var bm2 = document.getElementById(bookmarkID2);
        if (bm1 && bm2) {
            bm2.innerHTML = bm1.innerHTML;
        }
    },

    //Open new window shorter height
    openWinSmall: function(url) {
        aWindow = window.open(url, 'cgiwindow', 'Toolbar=no, Location=no, Directories=no, Status=0, Menubar=0, Scrollbars=1,  Resize=1, width=780, height=500');
    },

    CheckUserAvailability: function(UserName, Id) {
        var reg = /^(\w){3,}$/;
        if (!(reg.exec(UserName))) {
            $('#' + Id)[0].innerHTML = '';
            return;
        }

        $.get('/toplets/login/ValidateUser.ashx?user=' + UserName,
		function(data) {
		    var expression = '#' + Id;
		    $(expression)[0].innerHTML = data + "<br />";
		    if (data.indexOf("Yes") != -1) {
		        $(expression).removeClass("red");
		        $(expression).addClass("green");
		    }
		    else {
		        $(expression).removeClass("green");
		        $(expression).addClass("red");
		    }
		});
    },

    initial: function() {
        $("a.js-plusLink").live("click", function(event) {
            var $this = $(this);
            event.preventDefault();
            $this.hide().nextAll(".js-plusBody").eq(0).slideDown('slow');
        });
    }
}