/**
 * Opens a popup containing the larger listing image
 * @param {int} id Id of image to show
 */
function popupListingImage(id) {
	window.open('listingImagePopup/?id='+id+'&thumb=0','SilverRealtyListingImagePopup','width=320,height=240,status=no,location=no,directories=no,toolbar=no,scrollbars=yes');
}

/**
 * Opens a popup containing the larger additional listing image
 * @param {int} id Id of image to show
 * @return void
 */
function popupAdditionalListingImage(id) {
	window.open('listingImagePopup/?id='+id+'&thumb=0&addit=true','SilverRealtyListingImagePopup','width=640,height=480,status=no,location=no,directories=no,toolbar=no,scrollbars=yes');
}

/**
 * Removes the old script tag and creates a new one forcing the script to reload
 */
function listingsTypeChanged() {
	//Remove the old script tag
	document.getElementById('listingFetchStyles').parentNode.removeChild(document.getElementById('listingFetchStyles'));
	
	//Create a new script tag
	var script=document.createElement('script');
	script.type="text/javascript"; //Set the type
	script.src=listingPageLink+'fetchStyles/'+document.getElementById('listingZone').value; //Set the url to the script
	script.id="listingFetchStyles"; //Set the id for use later on
	document.getElementsByTagName('body')[0].appendChild(script); //Add to the body tag
	
	//Clear the old list
    document.getElementById('listingStyle').innerHTML='';
	
    
	//---------------------------- LISTING TYPES ----------------------------//
	
	//Remove the old script tag
    document.getElementById('listingFetchTypes').parentNode.removeChild(document.getElementById('listingFetchTypes'));
    
	//Create a new script tag
    var script=document.createElement('script');
    script.type="text/javascript"; //Set the type
    script.src=listingPageLink+'fetchTypes/'+document.getElementById('listingZone').value; //Set the url to the script
    script.id="listingFetchTypes"; //Set the id for use later on
    document.getElementsByTagName('body')[0].appendChild(script); //Add to the body tag

    //Clear the old list
    document.getElementById('listingType').innerHTML='';
}

/**
 * Rebuilds the styles select box for the advanced search page
 * @return void
 */
function listingsRebuildStyles() {
	//Loop through the listing styles and add them to the select box
	for(var i=0;i<listingStyles.length;i++) {
		//Create an option tag
		var option=document.createElement('option');
		option.value=listingStyles[i]['value']; //Set it's value
		option.innerHTML=listingStyles[i]['label']; //Set the label
		
		document.getElementById('listingStyle').appendChild(option); //Add the option to the listingStyle select box
	}
}

/**
 * Rebuilds the styles select box for the advanced search page
 * @return void
 */
function listingsRebuildTypes() {
    //Loop through the listing styles and add them to the select box
    for(var i=0;i<listingTypes.length;i++) {
        //Create an option tag
        var option=document.createElement('option');
        option.value=listingTypes[i]['value']; //Set it's value
        option.innerHTML=listingTypes[i]['label']; //Set the label
        
        document.getElementById('listingType').appendChild(option); //Add the option to the listingStyle select box
    }
}

/**
 * Builds the url to request data from based on the form's elements
 * @param {DOMElement} search_form Search form
 */
function tryAdvancedSearch(search_form) {
	var url=listingPageLink+'searchTotal/?s=1';
	
	for(var i=0;i<search_form.elements.length;i++) {
		if(search_form.elements[i].value!='' && search_form.elements[i].value!=0 && search_form.elements[i].name!='') {
			var el_name=search_form.elements[i].name;
			
			if(search_form.elements[i].type=='checkbox' || search_form.elements[i].type=='radio') {
				if(search_form.elements[i].checked==true) {
					url=url+'&'+escape(search_form.elements[i].name)+'='+escape(search_form.elements[i].value);
				}
			}else if(search_form.elements[i].tagName.toLowerCase()=='select') {
				if(search_form.elements[i].multiple==true && search_form.elements[i].options.length>0) {
					for(var k=0;k<search_form.elements[i].options.length;k++) {
						if(search_form.elements[i].options[k].selected) {
							url+='&'+escape(search_form.elements[i].name)+'='+escape(search_form.elements[i].options[k].value);
						}
					}
				}else if(search_form.elements[i].options.length) {
					for(var k=0;k<search_form.elements[i].options.length;k++) {
						if(search_form.elements[i].options[k].selected) {
							url+='&'+escape(search_form.elements[i].name)+'='+escape(search_form.elements[i].options[k].value);
							break;
						}
					}
				}
			}else {
				url+='&'+escape(search_form.elements[i].name)+'='+search_form.elements[i].value;
			}
		}
	}
	
	if(document.getElementById('listingAdvSearchTotal')) {
		//Remove the old script tag
		document.getElementById('listingAdvSearchTotal').parentNode.removeChild(document.getElementById('listingAdvSearchTotal'));
	}
	
	//Create a new script tag
	var script=document.createElement('script');
	script.type="text/javascript"; //Set the type
	script.src=url; //Set the url to the script
	script.id="listingAdvSearchTotal"; //Set the id for use later on
	document.getElementsByTagName('body')[0].appendChild(script); //Add to the body tag
}

/**
 * Updates the count of the number of potential results
 * @param {int} count Number of items
 */
function listingsAdvSearchCount(count) {
	//Update the top count
	document.getElementById('silverRealtyPreResultsTop').innerHTML=count;
	
	//Update the bottom count
	document.getElementById('silverRealtyPreResultsBottom').innerHTML=count;
}

/**
 * Makes a string XML safe
 * @param {String} text Text to make html safe
 */
function htmlentites(text) {
	text=text.replace(eval('/&/g'), '&amp;');
	fromTo=new Array('&AElig;','Æ','&Aacute;','Á','&Acirc;','Â',' &Agrave;','À','&Aring;','Å','&Atilde;','Ã','&Auml; ','Ä','&Ccedil;','Ç','&ETH;','Ð','&Eacute;','É','& Ecirc;','Ê','&Egrave;','È','&Euml;','Ë','&Iacute;' ,'Í','&Icirc;','Î','&Igrave;','Ì','&Iuml;','Ï','&N tilde;','Ñ','&Oacute;','Ó','&Ocirc;','Ô','&Ograve; ','Ò','&Oslash;','Ø','&Otilde;','Õ','&Ouml;','Ö',' &THORN;','Þ','&Uacute;','Ú','&Ucirc;','Û','&Ugrave ;','Ù','&Uuml;','Ü','&Yacute;','Ý','&aacute;','á', '&acirc;','â','&aelig;','æ','&agrave;','à','&aring ;','å','&atilde;','ã','&auml;','ä','&brvbar;','¦', '&ccedil;','ç','&cent;','¢','&copy;','©','&deg;',' °','&eacute;','é','&ecirc;','ê','&egrave;','è','&e th;','ð','&euml;','ë','&frac12;','½','&frac14;','¼ ','&frac34;','¾','&gt;','>','&gt','>','&iacute;',' í','&icirc;','î','&iexcl;','¡','&igrave;','ì','&iq uest;','¿','&iuml;','ï','&laquo;','«','&lt;','<',' &lt','<','&mdash;','—','&micro;','µ','&middot;','· ','&ndash;','–','&not;','¬','&ntilde;','ñ','&oacut e;','ó','&ocirc;','ô','&ograve;','ò','&oslash;','ø','&otilde;','õ','&ouml;','ö','&para;', '¶','&plusmn;','±','&pound;','£','&quot;','\"','&r aquo;','»','&reg;','®','&sect;','§','­','¹','&sup2;','²','&sup3;','³','&szlig;','ß','&t horn;','þ','&tilde;','˜','&trade;','™','&uacute;', 'ú','&ucirc;','û','&ugrave;','ù','&uuml;','ü','&ya cute;','ý','&yen;','¥','&yuml;','ÿ');
	
	for(i=0;i<fromTo.length;i=i+2) {
		text=text.replace(eval('/'+fromTo[i+1]+'/g'), fromTo[i]);
	}
	
	return text;
}

/**
 * Gets the base href
 */
function getBaseHref() {
    var base=document.getElementsByTagName('base');
    
    if(base.length>0) {
        return base[0].href;
    }
}