//$(document).ready(function() {
//	$("div.panel_button").click(function(){
//		$("div#panel").animate({
//			height: "240px"
//		})
//		.animate({
//			height: "225px"
//		}, "fast");
//		$("div.panel_button").toggle();
//	
//	});	
//	
//   $("div#hide_button").click(function(){
//		$("div#panel").animate({
//			height: "0px"
//		}, "fast");
//		
//	
//   });	
//	
//    //alert("ready!");
//});

function setLoginGetObj(name) {
    if (document.getElementById) {
	    return document.getElementById(name);
    }
    else if (document.all) {
	    return document.all[name];
    }
    else if (document.layers) {
   	    return document.layers[name];
    }
}


function setLogin()
{
    var show_button = setLoginGetObj("show_button");
    var hide_button = setLoginGetObj("hide_button");
    
    if(show_button != null || hide_button != null)
    {
        //alert("div_panel_button!");
	    $("div.panel_button").click(function(){
		    $("div#panel").animate({
			    height: "240px"
		    })
		    .animate({
			    height: "225px"
		    }, "fast");
		    $("div.panel_button").toggle();
    	
	    });	
    	
       $("div#hide_button").click(function(){
		    $("div#panel").animate({
			    height: "0px"
		    }, "fast");
    		
    	
       });
   }
   else{
        setTimeout("setLogin();", 5);
   }
}

setTimeout("setLogin();", 5);


    function getHTTPObject() {
        if (typeof XMLHttpRequest != 'undefined') { 
            return new XMLHttpRequest()             ; 
        }

        try { 
            return new ActiveXObject("Msxml2.XMLHTTP") ; 
        } 
        catch (e) { 
            try { 
                return new ActiveXObject("Microsoft.XMLHTTP") ; 
            } 
            catch (e) {} 
        } 

        return false                                ; 
    }

    var Profession;
    var RealTAG;

    function setChannelVariables(channelVariables)
    {
//        var page = new PageQuery(window.location.search);
//        Profession = unescape(page.getValue("Profession"));
//        RealTAG = unescape(page.getValue("RealTAG"));
        if(channelVariables && channelVariables.length > 0)
        {
            var ProfessionField = setLoginGetObj(channelVariables[0]);
            //alert(ProfessionField.value);
            if(ProfessionField)
            {
                Profession = ProfessionField.value;
            }
        }
        
        if(channelVariables && channelVariables.length > 1)
        {
            var RealTAGField = setLoginGetObj(channelVariables[1]);
            if(RealTAGField)
            {
                RealTAG = RealTAGField.value;
            }
        }
    }

    function getChannelVariables()
    {
        var ChannelVariables = "";
//        var page = new PageQuery(window.location.search);
//        Profession = unescape(page.getValue("Profession"));
//        RealTAG = unescape(page.getValue("RealTAG"));
       if(Profession && Profession != "")
       {
           ChannelVariables += "|amp|Profession="+Profession;
       }
       if(RealTAG && RealTAG != "")
       {
           ChannelVariables += "|amp|RealTAG=" + RealTAG;
       }
       return ChannelVariables;
    }

//    function PageQuery(q) {
//        if(q.length > 1) this.q = q.substring(1, q.length);
//        else this.q = null;
//        this.keyValuePairs = new Array();
//        if(q) {
//	    for(var i=0; i < this.q.split("&").length; i++) {
//	        this.keyValuePairs[i] = this.q.split("&")[i];
//	    }
//        }
//        this.getKeyValuePairs = function() { return this.keyValuePairs; }
//        this.getValue = function(s) {
//	    for(var j=0; j < this.keyValuePairs.length; j++) {
//	        if(this.keyValuePairs[j].split("=")[0] == s)
//		        return this.keyValuePairs[j].split("=")[1];
//	    }
//	    return false;
//        }
//        this.getParameters = function() {
//	    var a = new Array(this.getLength());
//	    for(var j=0; j < this.keyValuePairs.length; j++) {
//	        a[j] = this.keyValuePairs[j].split("=")[0];
//	    }
//	    return a;
//        }
//        this.getLength = function() { return this.keyValuePairs.length; }
//    }

    function getFullAdScript(zoneId)
    {
        var adRequest = "";
        
        adRequest = getAdScriptURL(zoneId).replace(/\|amp\|/g,"&amp;");

        document.write ("<scr"+"ipt type='text/javascript' src='" + adRequest);
        document.write ("'><\/scr"+"ipt>");
    }
    
    function getAdScriptURL(zoneId)
    {
       var adRequest = "";
       
       var m3_u = (location.protocol=='https:'?'https://d1.openx.org/ajs.php':'http://d1.openx.org/ajs.php');
       var m3_r = Math.floor(Math.random()*99999999999);
       if (!document.MAX_used) document.MAX_used = ',';
//           adRequest += "<scr"+"ipt type='text/javascript' src='";
       adRequest += m3_u;
       adRequest += "?zoneid=" + zoneId;
       adRequest += '|amp|cb=' + m3_r;
       
       adRequest += getChannelVariables();

       if (document.MAX_used != ',') adRequest += "|amp|exclude=" + document.MAX_used;
       adRequest += document.charset ? '|amp|charset='+document.charset : (document.characterSet ? '|amp|charset='+document.characterSet : '');
       adRequest += "|amp|loc=" + escape(window.location);
       if (document.referrer) adRequest += "|amp|referer=" + escape(document.referrer);
       if (document.context) adRequest += "|amp|context=" + escape(document.context);
       if (document.mmm_fo) adRequest += "|amp|mmm_fo=1";
//           adRequest += "'><\/scr"+"ipt>";
           
//           alert(adRequest);
        return adRequest;
    }

    function getAdiFrameURL(zoneId)
    {
       var adRequest = "";
       
       var m3_u = (location.protocol=='https:'?'https://d1.openx.org/afr.php':'http://d1.openx.org/afr.php');
       var m3_r = Math.floor(Math.random()*99999999999);
       if (!document.MAX_used) document.MAX_used = ',';
//           adRequest += "<scr"+"ipt type='text/javascript' src='";
       adRequest += m3_u;
       adRequest += "?zoneid=" + zoneId;
       adRequest += '|amp|cb=' + m3_r;
       
       adRequest += getChannelVariables();

//       if (document.MAX_used != ',') adRequest += "|amp|exclude=" + document.MAX_used;
//       adRequest += document.charset ? '|amp|charset='+document.charset : (document.characterSet ? '|amp|charset='+document.characterSet : '');
//       adRequest += "|amp|loc=" + escape(window.location);
//       if (document.referrer) adRequest += "|amp|referer=" + escape(document.referrer);
       if (document.referrer) adRequest += "|amp|referer=" + escape("http://www.epropertywatch.com/epwApp/Dashboard.aspx");
       if (document.context) adRequest += "|amp|context=" + escape(document.context);
//       if (document.mmm_fo) adRequest += "|amp|mmm_fo=1";
//           adRequest += "'><\/scr"+"ipt>";
           
//           alert(adRequest);
        return adRequest;
    }

    function getAdLinkURL(zoneId)
    {
       var adRequest = "";
       
       var m3_u = (location.protocol=='https:'?'https://d1.openx.org/ck.php':'http://d1.openx.org/afr.php');
       var m3_r = Math.floor(Math.random()*99999999999);
       if (!document.MAX_used) document.MAX_used = ',';
//           adRequest += "<scr"+"ipt type='text/javascript' src='";
       adRequest += m3_u;
       adRequest += "?zoneid=" + zoneId;
//       adRequest += '|amp|n=a2bdf3a1';
       adRequest += '|amp|cb=' + m3_r;
       
       adRequest += getChannelVariables();

       if (document.MAX_used != ',') adRequest += "|amp|exclude=" + document.MAX_used;
       adRequest += document.charset ? '|amp|charset='+document.charset : (document.characterSet ? '|amp|charset='+document.characterSet : '');
       adRequest += "|amp|loc=" + escape(window.location);
       if (document.referrer) adRequest += "|amp|referer=" + escape(document.referrer);
       if (document.context) adRequest += "|amp|context=" + escape(document.context);
       if (document.mmm_fo) adRequest += "|amp|mmm_fo=1";
//           adRequest += "'><\/scr"+"ipt>";
           
//           alert(adRequest);
        return adRequest;
    }

    function getAdIMGURL(zoneId)
    {
       var adRequest = "";
       
       var m3_u = (location.protocol=='https:'?'https://d1.openx.org/avw.php':'http://d1.openx.org/afr.php');
       var m3_r = Math.floor(Math.random()*99999999999);
       if (!document.MAX_used) document.MAX_used = ',';
//           adRequest += "<scr"+"ipt type='text/javascript' src='";
       adRequest += m3_u;
       adRequest += "?zoneid=" + zoneId;
//       adRequest += '|amp|n=a2bdf3a1';
       adRequest += '|amp|cb=' + m3_r;
       
       adRequest += getChannelVariables();

       if (document.MAX_used != ',') adRequest += "|amp|exclude=" + document.MAX_used;
       adRequest += document.charset ? '|amp|charset='+document.charset : (document.characterSet ? '|amp|charset='+document.characterSet : '');
       adRequest += "|amp|loc=" + escape(window.location);
       if (document.referrer) adRequest += "|amp|referer=" + escape(document.referrer);
       if (document.context) adRequest += "|amp|context=" + escape(document.context);
       if (document.mmm_fo) adRequest += "|amp|mmm_fo=1";
//           adRequest += "'><\/scr"+"ipt>";
           
//           alert(adRequest);
        return adRequest;
    }

    var adDIV;
    var xmlhttp = getHTTPObject();
    var gettingAd = false;
    function refreshAd(zoneId, adDivId)
    {
        var async = true;
        var adRequest = "";
     
        adDIV = setLoginGetObj(adDivId);
        
        adRequest = getAdScriptURL(zoneId);

//        alert(xmlhttp.readyState);
        xmlhttp.open("GET", "./getAd.aspx?URL=" + adRequest, async);
//           xmlhttp.open("GET", adRequest, false);
//        alert(xmlhttp.readyState);
        xmlhttp.send(null);
//        alert(xmlhttp.readyState);
        if(async && !gettingAd)
        {
            gettingAd = true;
            setTimeout("refreshAdAsyncCheck();", 100);
        }
        else if(!gettingAd)
        {
            refreshAdCallback();
        }
    }
    
    function refreshAd2(zoneId, adiFrameId, adLINKId, adIMGId)
    {
//        alert("refreshAd2");
        var adRequest = "";
     
        adiFrame = setLoginGetObj(adiFrameId);
        if(adiFrame) {
            adRequest = getAdiFrameURL(zoneId).replace(/\|amp\|/g,"&");
//            alert("adiFrame.src = " + adiFrame.src);
//            alert("adRequest = " + adRequest);
            adiFrame.src = adRequest;
//            adiFrame.refresh();
//            alert("adiFrame.src = " + adiFrame.src);
        }
        
        adLINK = setLoginGetObj(adLINKId);
        if(adLINK) {
            adRequest = getAdLinkURL(zoneId).replace(/\|amp\|/g,"&");
//            alert("adLINK.href = " + adLINK.href);
//            alert("adRequest = " + adRequest);
            adLINK.href = adRequest;
//            alert("adLINK.href = " + adLINK.href);
        }
        
        adIMG = setLoginGetObj(adIMGId);
        if(adIMG) {
            adRequest = getAdIMGURL(zoneId).replace(/\|amp\|/g,"&");
//            alert("adIMG.src = " + adIMG.src);
//            alert("adRequest = " + adRequest);
            adIMG.src = adRequest;
//            alert("adIMG.src = " + adIMG.src);
        }
    }
    
    function refreshAdAsyncCheck()
    {
//        alert(xmlhttp.readyState);
        if(xmlhttp.readyState == 4)
        {
//            alert(xmlhttp.readyState);
            refreshAdCallback();
        }
        else 
        {
            setTimeout("refreshAdAsyncCheck();", 100);
        }
    }
    
    function refreshAdCallback()
    {
        var newHTML = "";
        
        gettingAd = false;

        newHTML = xmlhttp.responseText;

        if(newHTML != "ERROR")
        {
//          alert(newHTML);
//          alert(adDIV.innerHTML);
            adDIV.innerHTML = "";
//          alert(adDIV.innerHTML);
            adDIV.innerHTML = newHTML;
//          alert(adDIV.innerHTML);
        }
        else
        {
//            alert("ERROR");
        }
    }


        function RemoveTag(openTag, closeTag, identifier, originalContent, removeAll)
        {
            var modifiedContent = originalContent;
            var identifierIndex = -1;       // Location of seached text
            var openTagIndex = -1;         // Saves Location of opening tag for container
            var closeTagIndex = -1;        // Saves location of closing tag for container
            var currentIndex = 0;       // used for iterating through the string
            var PassesMade = 0;
            
            if(removeAll == null)
            {
                removeAll = true;
            }

//            alert(identifier);
//            alert(modifiedContent);
            identifierIndex = modifiedContent.toLowerCase().indexOf(identifier.toLowerCase());
//            alert(identifierIndex);

            while (identifierIndex > 0 && (removeAll || PassesMade < 1))
            {
                currentIndex = 0;

                while (currentIndex < identifierIndex && currentIndex >= 0)
                {
                    openTagIndex = currentIndex;
                    currentIndex = modifiedContent.toLowerCase().indexOf(openTag.toLowerCase(), currentIndex + 1);
                }

//                alert(currentIndex);
//                alert(openTagIndex);
                
                if (openTagIndex >= 0)
                {
                    closeTagIndex = modifiedContent.toLowerCase().indexOf(closeTag.toLowerCase(), openTagIndex) + closeTag.length;
                    if (closeTagIndex >= closeTag.length)
                    {
//                        alert(modifiedContent.substr(openTagIndex, closeTagIndex - openTagIndex));
                        modifiedContent = modifiedContent.substr(0, openTagIndex) + modifiedContent.substr(closeTagIndex);
                    }
                    else
                    {
                        return modifiedContent;
                    }
                }
//                alert(modifiedContent);

                identifierIndex = modifiedContent.toLowerCase().indexOf(identifier.toLowerCase());
                PassesMade += 1;
            }
            return modifiedContent;
        }
        
        function ReplaceTag(openTag, closeTag, identifier, originalContent, newContent, removeAll)
        {
            var modifiedContent = originalContent;
            var identifierIndex = -1;       // Location of seached text
            var openTagIndex = -1;         // Saves Location of opening tag for container
            var closeTagIndex = -1;        // Saves location of closing tag for container
            var currentIndex = 0;       // used for iterating through the string
            var PassesMade = 0;
            
            if(removeAll == null)
            {
                removeAll = true;
            }

//            alert("newContent = " + newContent);
//            alert("identifier = " + identifier);
//            alert("modifiedContent = " + modifiedContent);
            identifierIndex = modifiedContent.toLowerCase().indexOf(identifier.toLowerCase());
//            alert("identifierIndex = " + identifierIndex);

            while (identifierIndex > 0 && (removeAll || PassesMade < 1))
            {
                currentIndex = 0;

                while (currentIndex < identifierIndex && currentIndex >= 0)
                {
                    openTagIndex = currentIndex;
                    currentIndex = modifiedContent.toLowerCase().indexOf(openTag.toLowerCase(), currentIndex + 1);
                }

//                alert("currentIndex = " + currentIndex);
//                alert("openTagIndex = " + openTagIndex);
                
                if (openTagIndex >= 0)
                {
                    closeTagIndex = modifiedContent.toLowerCase().indexOf(closeTag.toLowerCase(), openTagIndex) + closeTag.length;
                    if (closeTagIndex >= closeTag.length)
                    {
//                        alert("modifiedContent.substr = " + modifiedContent.substr(openTagIndex, closeTagIndex - openTagIndex));
                        modifiedContent = modifiedContent.substr(0, openTagIndex) + newContent + modifiedContent.substr(closeTagIndex);
                    }
                    else
                    {
                        return modifiedContent;
                    }
                }
//                alert("modifiedContent = " + modifiedContent);

                identifierIndex = modifiedContent.toLowerCase().indexOf(identifier.toLowerCase());
                PassesMade += 1;
            }
            return modifiedContent;
        }

    function ReplaceMenuItem(ul, identifier, newContent, foo)
    {
//        var items = ul.childNodes;
        var i;
        var loc;
        
//        alert("newContent = " + newContent);
        loc = newContent.indexOf("<li>");
        if(loc >= 0)
        {
            newContent = newContent.substr(loc + 4)
        }
        loc = newContent.indexOf("</li>");
        if(loc >= 0)
        {
            newContent = newContent.substr(0, loc);
        }
//        alert("newContent = " + newContent);

        for(i = 0; i < ul.childNodes.length; i++)
        {
//            alert("ul.childNodes[" + i + "].innerHTML = '" + ul.childNodes[i].innerHTML + "'");
//            alert("ul.childNodes.length = " + ul.childNodes.length);
            if(ul.childNodes[i].innerHTML.indexOf(identifier) >= 0)
            {
//                alert("*trim('" + newContent + "') = '" + trim(newContent) + "'");
                if(trim(newContent) == "")
                {
//                    alert("Removing item " + i + " from ul.childNodes");
//                    var removedItem = ul.childNodes.splice(i, 1);
                    var childRemoved = ul.removeChild(ul.childNodes[i]);
//                    if(childRemoved && childRemoved.nodeName)
//                    {
//                        alert("childRemoved.nodeName = '" + childRemoved.nodeName + "'");
//                    }
//                    else
//                    {
//                        alert("Either childRemoved or childRemoved.nodeName is undefined");
//                    }
                    if(childRemoved)
                    {
                        i--;
                    }
//                    alert("removedItem.innerHTML = '" + removedItem.innerHTML + "'");
                    
                }
                else
                {
//                    alert("Replacing innerHTML for item " + i + " in ul.childNodes");
                    ul.childNodes[i].innerHTML = newContent;
//                    alert(ul.childNodes[i].innerHTML);
                }
            }
//            alert("ul.childNodes.length = " + ul.childNodes.length);
//            alert("ul.childNodes[" + i + "].innerHTML = '" + ul.childNodes[i].innerHTML + "'");
        }
    }
    
    function trim(stringToTrimFrom, stringToTrimOff)
    {
//        alert("stringToTrimOff = '" + stringToTrimOff + "'");
        if(stringToTrimOff == null || stringToTrimOff == '')
        {
            stringToTrimOff = " ";
        }
//        alert("stringToTrimOff = '" + stringToTrimOff + "'");
        
//        alert("'" + stringToTrimFrom + "'.toLowerCase().indexOf('" + stringToTrimOff + "'.toLowerCase()) = " + stringToTrimFrom.toLowerCase().indexOf(stringToTrimOff.toLowerCase()));
        while(stringToTrimFrom.length >= 1 && stringToTrimFrom.toLowerCase().indexOf(stringToTrimOff.toLowerCase()) == 0)
        {
            stringToTrimFrom = stringToTrimFrom.substr(stringToTrimOff.length);
        }
        
//        alert("'" + stringToTrimFrom + "'.toLowerCase().lastIndexOf('" + stringToTrimOff + "'.toLowerCase()) = " + stringToTrimFrom.toLowerCase().lastIndexOf(stringToTrimOff.toLowerCase()));
        while(stringToTrimFrom.length >= 1 && stringToTrimFrom.toLowerCase().lastIndexOf(stringToTrimOff.toLowerCase()) == stringToTrimFrom.length - stringToTrimOff.length)
        {
            stringToTrimFrom = stringToTrimFrom.substr(0, stringToTrimFrom.length - stringToTrimOff.length);
        }
//        alert("'" + stringToTrimFrom + "'");
        return stringToTrimFrom;
    }
