// JavaScript Document
var the_hider = document.getElementById('content');
the_hider.style.display="none";
var the_loader = document.getElementById('please_wait');
the_loader.style.display="block";

function show_body(){
	var the_content = document.getElementById('content');
	the_content.style.display="block";
	the_loader.style.display="none";
	//load_styles();
}

function repath_all(){

	image_repath();
	link_repath();
//	form_repath();
	color_repath();
	td_repath();
	div_repath();
//	th_repath();
	table_repath();
	h2_repath();
	show_body();
}

//fix to remove pseudo folders created by mod_rewrite
var pseudo_folders = new Array("featured-properties/residential-for-sale/","featured-properties/commercial-for-sale/","featured-properties/commercial-for-lease/","featured-properties/vacant-land-for-sale/","featured-properties/condominiums-for-sale/","featured-properties/investment-income/","featured-properties/","properties-for-rent/","commercial-rentals/","property-search/");
var paginate_filenames = new Array("paginate_commercial_for_lease.php","paginate_commercial_for_sale.php","paginate_commercial_rentals.php","paginate_condos_for_sale.php","paginate_investment_income.php","paginate_residential_for_sale.php","paginate_residential_rentals.php","paginate_vacant_for_sale.php");
var pf_length = pseudo_folders.length;
var pg_length = paginate_filenames.length;

function image_repath(){
	// Set specific variable to represent all img tags.
	var image_tags = document.getElementsByTagName('img');
	//	alert (image_tags.length);
	// Iterate through all img tags in the page. - starting with 1 to omit the headline graphic!
	// we also need to omit the ads - make an exception for 
	for (var i = 1, j = image_tags.length; i < j; i++){
		// modify path for image srcs
		var mls_string = "/images";
		var new_domain = "rpcservers.com/mls/";
		var old_domain = "www.traditionsjax.com/wp/";
		var ad_domain = "www.traditionsjax.com/wp/wp-content/uploads/";
		if(image_tags[i].src.indexOf(ad_domain)!=-1){
			//image is an ad, do nothing
		}else{
			//image is not an ad, proceed as usual
		if(image_tags[i].src.indexOf(old_domain)!=-1){
			image_tags[i].src = image_tags[i].src.replace(old_domain, new_domain);
		}
		for(k=0; k<pf_length; k++){
		  if(image_tags[i].src.indexOf(pseudo_folders[k])!=-1){
			image_tags[i].src = image_tags[i].src.replace(pseudo_folders[k], "");
		  }	
		}
		}
	}
	//alert (image_tags[6].src);	
}
function link_repath(){

	// Set specific variable to represent all <a> tags.
	var a_tags = document.getElementsByTagName('a');
/*
	alert (a_tags.length);
	alert (a_tags[1].href);
	*/
	// Iterate through all a tags in the page.
	for (var i = 0, j = a_tags.length; i < j; i++){
		// modify path for image srcs
		var mls_string = "/images";
		var new_domain = "rpcservers.com/mls/mls_process.php";
		var old_domain = "www.traditionsjax.com/wp/";
		var paginate_filename = "paginate_residential_rentals.php";
		//loop through for "see detail" links
		if(a_tags[i].innerHTML.indexOf("See Detail")!=-1){
			//replace domain
			a_tags[i].href = a_tags[i].href.replace(old_domain, new_domain);
			//save php variables
			var a_page = a_tags[i].href.replace(paginate_filename,"");
			var a_page = a_page.replace("?","&");
			//remove pseudo dir
			for(k=0; k<pf_length; k++){
		  		if(a_page.indexOf(pseudo_folders[k])!=-1){
					a_page = a_page.replace(pseudo_folders[k], "");
		  		}
			}
			//remove paginate filename
			for(g=0; g<pg_length; g++){
				//alert("iterate: "+g);
		  		if(a_page.indexOf(paginate_filenames[g])!=-1){
					alert(g);
					a_page = a_page.replace(paginate_filenames[g], "");
		  		}	
			}
			
			a_tags[i].href = "/property_details.php?to_include="+a_page;
		}
		//loop through for "see image" links
		if(a_tags[i].innerHTML.indexOf("<img")!=-1){
			//replace directory
			a_tags[i].href = a_tags[i].href.replace("./images", "http://rpcservers.com/mls/images");
		}
		//loop through for "new search" links
		if(a_tags[i].innerHTML.indexOf("New Search")!=-1){
			//erase new search
			a_tags[i].innerHTML = a_tags[i].innerHTML.replace("New Search", "");
		}
		//loop through for "pagination" links
		if(a_tags[i].innerHTML.indexOf("First")!=-1 || a_tags[i].innerHTML.indexOf("Previous")!=-1 || a_tags[i].innerHTML.indexOf("Next")!=-1 || a_tags[i].innerHTML.indexOf("Last")!=-1){
			//replace function
//			href="javascript:submit_form('desc', 2);
			a_tags[i].href = a_tags[i].href.replace("submit_form", "goto_page");
		}
	}
	/*
	alert (a_tags[0].href);
	alert (a_tags[5].href);
	alert (a_tags[10].href);
	*/
	
}

/*
function form_repath(){
	// Set specific variable to represent all <form> tags.
	var form_tags = document.getElementsByTagName('form');
	// Iterate through all form tags in the page.
	for (var i = 0, j = form_tags.length; i < j; i++){
//		var new_text = "http://www.rpcservers.com/mls/";
//		var old_text = "mls_process.php";
		var new_domain = "http://rpcservers.com/mls/mls_process.php";
		var old_domain = "www.traditionsjax.com/wp/";
		if(typeof form_tags[i].attributes["action"].value != "undefined"){
			if(form_tags[i].attributes["action"].value.indexOf(old_domain)!=-1){
				//alert(form_tags[i].action.indexOf(old_text));
				var filename = form_tags[i].attributes["action"].value;
				form_tags[i].attributes["action"].value = new_domain;
				//alert("new value = "+form_tags[i].attributes["action"].value);
				for(k=0; k<pf_length; k++){
		  			if(form_tags[i].attributes["action"].value.indexOf(pseudo_folders[k])!=-1){
						//alert("pseudo replaced");
						form_tags[i].attributes["action"].value = form_tags[i].attributes["action"].value.replace(pseudo_folders[k], "");
		  			}
				}
			}
		}
	}
}
*/
function color_repath(){
	
	// Set specific variable to represent all <tr> tags.
	var tr_tags = document.getElementsByTagName('tr');
	// Iterate through all tr tags in the page.
	for (var i = 0, j = tr_tags.length; i < j; i++){
		var new_color = "#B4DAF1";
		var old_color = "#c0dcc0";
		if(typeof tr_tags[i].bgColor != "undefined"){
			if(tr_tags[i].bgColor.indexOf(old_color)!=-1){
				//tr_tags[i].style.background = tr_tags[i].style.background.replace(old_color, new_color);
				tr_tags[i].bgColor=new_color;
			}
		}
	}
}
function td_repath(){
	// Set specific variable to represent all <td> tags.
	var td_tags = document.getElementsByTagName('td');
	// Iterate through all td tags in the page.
	for (var i = 0, j = td_tags.length; i < j; i++){
		//make padding 3px for all td tags
		td_tags[i].style.padding="3px";
		//hide the non-functional listings per page controls
		if(td_tags[i].innerHTML.indexOf("Listings to Display per Page")!=-1){
			td_tags[i].style.display="none";
		}
	}
}
function div_repath(){
	// Set specific variable to represent all <div> tags.
	var div_tags = document.getElementsByTagName('div');
	// Iterate through all div tags in the page.
	for (var i = 0, j = div_tags.length; i < j; i++){
		//find the darkblue text and change the color to #4787B2
		var the_class = "disclaimer";
		var old_color = "darkblue";
		var new_color = "#4787B2";
		if(typeof div_tags[i].style.color != "undefined"){
			//alert("not undefined");
			if(div_tags[i].className.indexOf(the_class)!=-1){
				//alert("darkblue found");
				div_tags[i].style.color=new_color;
			}
		}
	}
//	alert("classname = "+td_tags[20].className);
}
/*
function th_repath(){
	// Set specific variable to represent all <th> tags.
	var th_tags = document.getElementsByTagName('th');
	// Iterate through all td tags in the page.
	for (var i = 0, j = th_tags.length; i < j; i++){
		th_tags[i].style.padding="3px";
	}
}
*/
function table_repath(){
	// Set specific variable to represent all <table> tags.
	var table_tags = document.getElementsByTagName('table');
	// Iterate through all table tags in the page.
	for (var i = 0, j = table_tags.length; i < j; i++){
		var new_width = "95%";
		var old_width = "70%";
		if(typeof table_tags[i].width != "undefined"){
			if(table_tags[i].width.indexOf(old_width)!=-1){
				table_tags[i].width=new_width;
			}
		}
		/*
		var old_style = "border: 1px solid;";
		var new_style = "border: none;";
		if(typeof table_tags[i].style != "undefined"){
			if(table_tags[i].style.indexOf(old_style)!=-1){
				table_tags[i].style=new_style;
			}
		}
		*/
	}
}

function h2_repath(){
	// Set specific variable to represent all <table> tags.
	var h2_tags = document.getElementsByTagName('h2');
	// Iterate through all table tags in the page.
	for (var i = 0, j = h2_tags.length; i < j; i++){
		var new_text = "";
		var old_text = new Array("Featured Properties", "Rentals", "Commercial For Rent");
		if(typeof h2_tags[i].innerHTML != "undefined"){
			//h2_tags[i].style.fontsize="18px";
			for(var k = 0; k < old_text.length; k++){
				if(h2_tags[i].innerHTML.indexOf(old_text[k])!=-1){
					//tr_tags[i].style.background = tr_tags[i].style.background.replace(old_color, new_color);
					h2_tags[i].innerHTML=new_text;
				}
			}
		}
	}
}





//mls javascript code
	var pu;
	var image_num = 0;

	function popup(url, width, height)
	{
		if (pu && !pu.closed)
			pu.close();
			img_path="http://www.rpcservers.com/mls/";
			url=url.slice(1);
		pu = window.open(url, "mls_image", "toolbar=0, scrollbars=1, width=" + width + ", height=" + height + ", left=0, top=0");
	}

	function show_image(i)
	{
		image_num += i;
		if (image_num < 0)
			image_num = total_images - 1;
		image_num %= total_images;

		document.listing_image.src = listing_images[image_num];
		document.links["listing_image_url"].href = "javascript:popup('" + listing_images[image_num] + "', 500, 400);";
		document.getElementById('listing_image_num').innerHTML = (image_num + 1) + "/" + total_images;
	}

	function disable(short_cat, form)
	{
		for (i=0; i<form.elements.length; i++)
			form.elements[i].disabled = false;
		if (short_cat == null)
			form.reset();

		switch (short_cat)
		{
			case "CSL":
				form.elements['advanced[6]'].disabled = true; // gated community
			case "CLE":
				form.beds.disabled = true;
				form.baths.disabled = true;
				form.elements['advanced[8]'].disabled = true; // fireplace
				form.elements['advanced[13]'].disabled = true; // foreclosed
				form.elements['advanced[14]'].disabled = true; // pre-foreclosure
				form.elements['advanced[15]'].disabled = true; // short sale
				form.subdivision.disabled = true; // subdivision
			case "CND":
			case "INV":
				form.elements['advanced[9]'].disabled = true; // horse facilities
				break;
			case "LND":
				form.beds.disabled = true;
				form.baths.disabled = true;
				form.elements['advanced[6]'].disabled = true; // gated community
				form.elements['advanced[8]'].disabled = true; // fireplace
				break;
			case "RNT":
				form.elements['advanced[13]'].disabled = true; // foreclosed
				form.elements['advanced[14]'].disabled = true; // pre-foreclosure
				form.elements['advanced[15]'].disabled = true; // short sale
				break;
		}
	}

	function toggle_checkbox(cb)
	{
		cb.checked = !cb.checked;
	}


	function submit_form(sort_order, page_num)
	{
		document.getElementById('sort_order').innerHTML = "<input type=\"hidden\" name=\"sort_order\" value=\"" + sort_order + "\"/>";
		document.getElementById('page_num').innerHTML = "<input type=\"hidden\" name=\"page_num\" value=\"" + page_num + "\"/>";
		document.forms['summary'].submit();
	}
	


