// JavaScript Document to load up the mini search results on the left of the details page.
//
var eventHolder = new Object;
eventHolder['run'] = null;

function addLoader(func) {
    if (eventHolder['run'] && eventHolder['run'] != null) {
	    var oldRun = eventHolder['run'];
	    eventHolder['run'] = function(e) { oldRun(e); func(e) }
	} else {
	    eventHolder['run'] = function(e) { func(e) }
	}
}

function addEvent(oTarget, sType, fpDest) {
    sType = 'on'+sType;
    var oOldEvent = oTarget[sType];

    if (typeof oOldEvent != "function") {
        oTarget[sType] = fpDest;
    } else {
        oTarget[sType] = function(e) {
            oOldEvent(e);
            fpDest(e);
        }
    }
}

function getSearchResultsOnDetailsPage ()  {
    if (window.ActiveXObject) {
       var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); // xmlhttp is global
       xmlhttp.onreadystatechange = function () {	
          if(xmlhttp.readyState == 4) writesearchcontent(xmlhttp.responseText);
       };
    } else {
       if (window.XMLHttpRequest) {
         var xmlhttp = new XMLHttpRequest() // xmlhttp is global
         xmlhttp.onload = function () {
            writesearchcontent(xmlhttp.responseText)
        };
       }
       else return;
    }

	var propid = document.mainform.propid.value;
	var searchparams = document.mainform.searchparams.value;

	if (searchparams != "" && propid != "") {
        if (window.location.href.match(/\/renting\/search\/([0-9]*).shtml/)) {
            searchparams = searchparams.replace('mapresults=map%20search','');
        }
        searchparams = searchparams.replace(/map_view=[0-9]+&/,'');

        if (searchparams.match(/forrent_search_results.cgi/)) {
            var url = "http://" + document.location.hostname + "/" + searchparams + "&mini=1" + "&propid=" + propid;
        } else {
            var url = "http://" + document.location.hostname + "/renting/search/forrent_search_results.cgi?" + searchparams + "&mini=1" + "&propid=" + propid;
        }
	   
        xmlhttp.open("GET", url , true);
        xmlhttp.send(null);
	}
}

function writesearchcontent (content) {
    document.getElementById('searchResultsOnDetailsPage').innerHTML = content;
	write_next_page_link ();
	write_prev_page_link ();
}

function transfercookieNext() {
    delCookie('searchRentalHomes');
    delCookie('searchRentalHomesPrev');
  
    var value = document.mainform.next_prop_count.value;
    var ExpireDate = new Date ();
    var tmp = new Date();
    ExpireDate.setTime(ExpireDate.getTime() + 600000);
    var curCookie = "thispropcount=" + value + "; expires=" + ExpireDate.toGMTString() + "; path=/";
    document.cookie = curCookie;
}

function transfercookiePrev() {
    delCookie('searchRentalHomes');
    delCookie('searchRentalHomesNext');
  
    var value = document.mainform.prev_prop_count.value;
    var ExpireDate = new Date ();
    var tmp = new Date();
    ExpireDate.setTime(ExpireDate.getTime() + 600000);
    var curCookie = "thispropcount=" + value + "; expires=" + ExpireDate.toGMTString() + "; path=/";
    document.cookie = curCookie;
}

function getElementsByClass(searchClass,node,tag) {
    var classElements = new Array();
    
    if (node == null)
	node = document;
    if (tag == null)
	tag = '*';
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
    for (i = 0, j = 0; i < elsLen; i++) {
        if (pattern.test(els[i].className)) {
            classElements[j] = els[i];
            j++;
        }
    }
    return classElements;
}

function write_next_page_link () {
    if (document.mainform.next_propd_url) {
	    var nexturl = document.mainform.next_propd_url.value + ".shtml";
	    var shownexturl = document.mainform.show_next_details_url.value;
	          
	    if(shownexturl == 1) {
		    var imgs = getElementsByClass('nextimg');
		    for(i=0; i < imgs.length; i++) {
			    imgs[i].style.visibility =  'visible';  
			}
		         
		    targs = getElementsByClass('next_prop'); 
		    for (i=0; i < targs.length; i++) {
			    var link = document.createElement('A');
			    targs[i].appendChild(link);
			    link.href = nexturl;
			    link.appendChild( document.createTextNode("next"));
			    addEvent( link, 'click', transfercookieNext );
			} 
		}
	}
}

function write_prev_page_link () {
    if (document.mainform.prev_propd_url) {
	    var prevurl = document.mainform.prev_propd_url.value + ".shtml";
	    var showprevurl = document.mainform.show_prev_details_url.value;
	          
	    if (showprevurl == 1) {
		    var imgs = getElementsByClass('previmg')
			for (i=0; i < imgs.length; i++) {
				imgs[i].style.visibility =  'visible';
			}
		           
		    targs = getElementsByClass('prev_prop') 

            for (i=0; i < targs.length; i++) {
				var link = document.createElement( 'A' );
				targs[i].appendChild( link );
				link.href = prevurl;
				link.appendChild( document.createTextNode("previous") );
				addEvent( link, 'click', transfercookiePrev );
			}
		}
	}
}

addLoader(getSearchResultsOnDetailsPage );

