// 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 ) // if it's ie
    {
       var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); // xmlhttp is global
       xmlhttp.onreadystatechange = function ()
       {
          if(xmlhttp.readyState == 4) writesearchcontent(xmlhttp.responseText);
       };
    }
    else
    {
       if (window.XMLHttpRequest) // if it's firefox etc.
       {
         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;
//	searchparams = document.getElementById('searchparams');
//       alert("here" + propid +  " - " +  searchparams + " - ");
	if(searchparams != "" && propid != "")
	{
       if (window.location.href.match(/\/property-for-sale\/([0-9]*).shtml/))
	   {
		searchparams = searchparams.replace('mapresults=map%20search','');
	   }
	   searchparams = searchparams.replace(/map_view=[0-9]+&/,'');
	   if(searchparams.match(/forsale_search_results.cgi/))
	   {
	     var url = "http://" + document.location.hostname + "/" + searchparams + "&mini=1" + "&propid=" + propid;
	   }
	   else
	   {
	     var url = "http://" + document.location.hostname + "/property-for-sale/forsale_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('searchHomes');
  delCookie('searchHomesPrev');


  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('searchHomes');
  delCookie('searchHomesNext');


  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;
	    var nextpageurl = document.mainform.next_Button.value + ".shtml";;

	    if(shownexturl == 1)
		{
		    var imgs = getElementsByClass('nextimg');
                    //alert(imgs.length);
		    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 );
			}

			targs1 = getElementsByClass('next_prop1');
		    for(i=0; i < targs1.length; i++)
			{
			    var link = document.createElement( 'A' );
			    targs1[i].appendChild( link );
			    link.href = nextpageurl;
			    link.appendChild( document.createTextNode("next 5 >") );
			    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;

      var prevpageurl = document.mainform.prev_Button.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 );
			    }

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



		}
	}
}

addLoader( getSearchResultsOnDetailsPage  );

