function showQuery (label, status){
	var query_id = document.getElementById('queryId_' + label);
	var show_id = document.getElementById('showId_' + label);
	var hide_id = document.getElementById('hideId_' + label);

	if (status == 'hide'){
		show_id.style.display = "none";
		hide_id.style.display = "block";
		query_id.style.display = "block";
		query_id.style.backgroundColor = "#f1f1f1";
	} else {
		hide_id.style.display = "none";
		show_id.style.display = "block";
		query_id.style.display = "none";
	}
}

function clearCriteriaField (id) {
	var element = document.getElementById(id);
	
	element.value = "";
}

function toggleVisibility(id) {
	var el = document.getElementById(id);
	if (el.style.display == 'none') {
		el.style.display = 'block';
		_UTIL_setIFrameBackground(id);
	} else {
		el.style.display = 'none';
		_UTIL_hideIFrameBackground(id);
	}
}

//tracking
function rfs_details(ad_id, scraper_id, agent_id, event) {
	var url = "/mason/tracker/ForSale/rfs_logs.html?ad_id="+ad_id+"&scraper_id="+scraper_id+"&agent_id="+agent_id+"&"+event+"=1";
	ajaxLoader(url);
}

function addToCalendar(listing_id, oh_date, s_time, e_time) {
	var url = "/mason/RFS/components/rfs_openHouse.html?listing_id="+listing_id+"&oh_date="+oh_date+"&start_time="+s_time+"&end_time="+e_time+"&add_to_cal=1";

	var obj = document.getElementById("oh_div_"+listing_id+"_"+oh_date);
	ohloading(obj, "Saving"); 

	setValues(url, listing_id, oh_date);
}

function removeFromCalendar(listing_id, oh_date, s_time, e_time) {
	if (!window.confirm("Are you sure you want to remove listing from calendar?"))
		return;

	var obj = document.getElementById("oh_div_"+listing_id+"_"+oh_date);
	ohloading(obj, "Removing"); 

	var url = "/mason/RFS/components/rfs_openHouse.html?listing_id="+listing_id+"&oh_date="+oh_date+"&start_time="+s_time+"&end_time="+e_time+"&remove=1";
	setValues(url, listing_id, oh_date);
}

function rfs_mailfriend(listing_id, sname, rname, smail, rmail, subj, emailType, emailTo, mess) {
	var url = "/mason/RFS/components/mail.html?listing_id="+listing_id+"&sname="+sname+"&smail="+smail+"&rname="+rname+"&rmail="+rmail+"&subj="+subj+"&send=1"+"&mess="+mess+"&emailType="+emailType+"&emailTo="+emailTo;
	var obj = document.getElementById("email_div_"+listing_id);
	loading_msg_s(obj);
	ajaxLoader(url, "email_div_"+listing_id);
}

function confirmDelete(listing_id) {
	if (!window.confirm("Are you sure you want to remove this listing?"))
		return;
	window.location = "/mason/RFS/components/saved_listings.html?remove=1&listing_id="+listing_id;
}

function ohloading(obj,txt){
	obj.innerHTML = ' <a href="javascript:void(0)" class="ohloading">'+txt+'</a> ';
}

//New layout
function toggleCateg(o){
	var obj = o.nextSibling;
	if (obj.style.display == "block"){
		obj.style.display = "none";
		o.className = "";
	}else{
		obj.style.display = "block";
		o.className = "minus";
	}
}
function toggleCrit(o){
	var obj = document.getElementById("c_"+o);
	var check = document.getElementById("check_"+o);
	var c1 = document.getElementById("criteriaOn1");
	var c2 = document.getElementById("criteriaOn2");
	var coff = document.getElementById("criteriaOff");
	var ctxt = document.getElementById("criteriaTxt");
	
	if (obj.parentNode.id == "criteriaOn2"){
		coff.appendChild(obj);
		check.checked = false;
	}else{
		obj.style.display = '';
		c2.appendChild(obj);
		check.checked = true;
	}

	var dls = c2.getElementsByTagName("dl");
	ctxt.className = (dls.length)?"hide":"";
//	if (c2.clientHeight < c1.clientHeight){
//		c2.style.height = c1.clientHeight+"px";
//	}else{
//		c2.style.height = "auto";
//	}
}
function removeCrit(o){
	var obj = document.getElementById("c_"+o);
	var check = document.getElementById("check_"+o);
	var coff = document.getElementById("criteriaOff");

	check.checked = false;
	addClass(obj,"hide");
}
function clearCrit(o){
	var tags = ["input","select","div"];
	for (var i=0;i<tags.length;i++){
		var inputs = o.parentNode.getElementsByTagName(tags[i]);
		for (var j=0;j<inputs.length;j++){
			var e = inputs[j];
			var type = e.getAttribute("type") || e.nodeName.toLowerCase();
			if (type=="text"){
				e.value = "";
			} else if (type=="hidden"){
				e.value = "";
			}else if (type=="checkbox"){
				e.checked = false;
			}else if (type=="radio"){
				e.checked = false;
			}else if (type=="select" || type=="select-one"){
				e.selectedIndex = 0;
			}else if (type=="div"){
				if (e.className=="sel_regions") e.className = "hide";
			}
		}
	}
}

RFSCHECK = {};
function checkForm(f){
	// check for valid data
	for (var i in RFSCHECK){
		var coff = document.getElementById("criteriaOff");
		var cobj = document.getElementById("c_"+i);
		if (!hasClass(cobj,'hide')){
			var r = RFSCHECK[i]();
			if (r){
				alert("Error on '"+r.name+"' criterion:\n"+r.err);
				return(false);
			}
		}
	}

	_UTIL_setPageMsg("<div class='loading'>Please wait for search results to load ...</div>",true);
	$("#results")[0].innerHTML = "";
	var s = new AjaxService();
	s.setURL("/mason/RFS/listings.html?"+loadFormElements().join("&")+"&content_only=1");
	s.setDelegate(function(r){
		//Dumper(r)
		$("#results")[0].innerHTML = r.split("<!-- results -->")[1] || "<b style='color:#ff0000'>Incorrect server response. Please try again.</b>";
		_UTIL_setPageMsg("",true);
	});
	s.callService();

	return(false);
}

function loadFormElements(){
	var f = document.rfsform;

	// disable form elements that should not be submitted
	var visible_criteria = [];
	var obj_advanced_toggle = document.getElementById("rfs_advanced_search_toggle");
	if ( !hasClass(obj_advanced_toggle,"active") ){
		// disable all advanced criteria
		disableChildFormElements("rfs_advanced_search");
	}else{
		var obj_advanced = document.getElementById("rfs_advanced_search");
		if ( obj_advanced ){
			// disable all advanced criteria that are not currently checked as active
			var input_elements = obj_advanced.getElementsByTagName("input");
			for (var i=0; i<input_elements.length; i++){
				if (input_elements[i].id.indexOf("check_rfs_") == 0){
					if (!input_elements[i].checked)
						disableChildFormElements( input_elements[i].id.replace("check_rfs_","c_rfs_") );
					else
						visible_criteria.push(input_elements[i].id.replace("check_",""));
				}
			}
		}	
	}
	f.visible_criteria.value = visible_criteria.join(",");

	var a = [];
	for (var i=f.elements.length-1; i>=0; i--){
		var elem = f.elements[i];
		if (!elem.name) continue;
		if (elem.getAttribute("off")){
			elem.removeAttribute("off");
			continue;
		}
		if (elem.type == "checkbox" & elem.checked) {
			a.push(elem.name+"="+elem.value);
		}
		if (elem.type == "radio" & elem.checked) {
			a.push(elem.name+"="+elem.value);
		}
		if ((elem.type == "select-one" || elem.type == "select") && elem.value != "" && elem.value != "\r" ) {
			a.push(elem.name+"="+elem.options[elem.selectedIndex].value);
		}
		if ((elem.type == "text" || elem.type == "hidden") && elem.value != "") {
			a.push(elem.name+"="+elem.value);
		}
	}
	return(a);
}

function disableChildFormElements(id){
	var obj = document.getElementById(id);
	if ( obj ){
		var input_elements = obj.getElementsByTagName("input");
		var select_elements = obj.getElementsByTagName("select");
		
		for (var i=0; i<input_elements.length; i++){
			input_elements[i].setAttribute("off","1");
		}
		for (var i=0; i<select_elements.length; i++){
			select_elements[i].setAttribute("off","1");
		}
	}	
}

function RfsTrack (page, region) {
	var trackUrl = "/mason/tracker/ForSale/rfs_general.html?page="+encodeURIComponent(page)+"&region="+encodeURIComponent(region);
	var service = new AjaxService();
	service.setURL(trackUrl);
	service.method = "POST";
	service.callService();
}

function saveListing(listing_id, detail_id, open_house, oh_date) {
	if (oh_date) {
		var url = "/mason/RFS/components/save_listing.html?listing_id="+listing_id+"&detail_id="+detail_id+"&open_house="+open_house+"&oh_date="+oh_date+"&save=1";
	} else {
		var url = "/mason/RFS/components/save_listing.html?listing_id="+listing_id+"&detail_id="+detail_id+"&open_house="+open_house+"&save=1";
	}

	if (oh_date) {
		var obj = document.getElementById("save_listing_"+listing_id+"_"+oh_date);
	} else {
		var obj = document.getElementById("save_listing_"+listing_id);
	}
	ohloading(obj, "Saving"); 
	
	setValuesSave(url, listing_id, oh_date);

}

function removeListing(listing_id, detail_id, oh_date) {
	if (oh_date) {
		var url = "/mason/RFS/components/save_listing.html?listing_id="+listing_id+"&detail_id="+detail_id+"&oh_date="+oh_date+"&remove=1";
	} else {
		var url = "/mason/RFS/components/save_listing.html?listing_id="+listing_id+"&detail_id="+detail_id+"&remove=1";
	}
// 	var url = "/mason/RFS/components/save_listing.html?listing_id="+listing_id+"&detail_id="+detail_id+"&remove=1";

	if (oh_date) {
		var obj = document.getElementById("save_listing_"+listing_id+"_"+oh_date);
	} else {
		var obj = document.getElementById("save_listing_"+listing_id);
	}
	ohloading(obj, "Removing"); 

	setValuesSave(url, listing_id, oh_date);
}

function setValuesSave(url, listing_id, oh_date) {
	var service = new AjaxService();
	service.setDelegate(function(r){

		if (oh_date) {
			var save_div = document.getElementById("save_listing_"+listing_id+"_"+oh_date);
		} else {
			var save_div = document.getElementById("save_listing_"+listing_id);
		}
		
		var JSON = eval("(" + r + ")");

		save_div.innerHTML = JSON.save_div_sent;
	});
	service.setURL(url);
	service.callService();
}

function checkDateFormat(date) {
	var dateFormat = /\d\d\/\d\d\/\d\d\d\d/;
	
	if (date.match(dateFormat) ) {
		return true;
	}
	return false;
}

function updateListing(listing_id, detail_id, form_id, response_id, action, btn) {
	var updateForm = document.getElementById(form_id);
	var url = "/mason/RFS/components/update_listing.html?listing_id="+listing_id+"&detail_id="+detail_id+"&action="+action;
	var queryString = "";

	if(updateForm) {
		for (i=0; i<updateForm.elements.length; i++) {
			if (updateForm.elements[i].value) {
				if(updateForm.elements[i].name.match('date') && (action == "save")) {
					if(checkDateFormat(updateForm.elements[i].value)) {
						queryString += "&"+updateForm.elements[i].name+"="+encodeURIComponent(updateForm.elements[i].value);
					} else {
						alert("Please enter a valid date");
						break;
					}
				} else {
					queryString += "&"+updateForm.elements[i].name+"="+encodeURIComponent(updateForm.elements[i].value);
				}
			}
		}

		if (queryString) {
			var loading = document.createElement("span");
			loading.className = "loading";
			loading.innerHTML = "&nbsp;&nbsp;&nbsp;";
			loading.style.display = "inline";
			btn.parentNode.appendChild(loading);
			
			url += queryString;
			var s = new AjaxService();
			s.setDelegate(function(r){
				var JSON = eval("(" + r + ")");
				loading.parentNode.removeChild(loading);
				$("#"+response_id)[0].innerHTML = JSON.response;
			});
			s.setURL(url);
			s.callService();
		}else{
			alert("Please fill in the form field(s).");
		}
	}
}

function toggleCrit2(o){
	var obj = document.getElementById("c_"+o);
	var check = document.getElementById("check_"+o);
	var c2 = document.getElementById("criteriaOn2");
	
	if (obj && check){
		if (check.checked){
			removeClass(obj,"hide");
		}else{
			addClass(obj,"hide");
		}
	}
}

function advancedSearchToggle(){
	toggleClass('rfs_advanced_search_toggle','active');
	toggleClass('rfs_advanced_search','hide');
	toggleClass('rfs_simple_search_submit','hide')
	var active = hasClass(document.getElementById('rfs_advanced_search_toggle'),'active');
	var simple = document.getElementById("rfs_simple_search_submit");
	var advanced = document.getElementById("rfs_advanced_search_submit");
	var submit_content = document.getElementById("rfs_search_submit_container");
	if ( active ){
		advanced.appendChild(submit_content);
	}else{
		simple.appendChild(submit_content);
	}
}

function toggleClass(id, class_name){
	var obj = document.getElementById(id);
	if (obj){
		if (hasClass(obj,class_name)){
			removeClass(obj,class_name);
		}else{
			addClass(obj,class_name);
		}
	}
}

function toggle_save_search(id){
	var target_obj = document.getElementById(id)
	var anchor_obj = document.getElementById("rfs_search_form");
	target_obj.style.left = _UTIL_getPosleft(anchor_obj) + "px";
	toggleVisibility(id);
}

function saveSearch( id, save ) {
	var url = "";
	if (save){
		var search_name_obj = document.getElementById("rfs_save_search_name");
		var user_email_obj = document.getElementById("rfs_save_search_user_email");
		var user_name_obj = document.getElementById("rfs_save_search_user_name");
		
		var url;
		
		if (user_email_obj && user_name_obj){
			var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
			if (user_email_obj.value.search(emailRegEx) == -1) {
				alert("Please enter a valid email address.");
				return;
			}

			url = "/mason/RFS/components/rfs_saveSearch.html?id="+id+"&save=1&name="+search_name_obj.value+"&user_email="+user_email_obj.value+"&user_name="+user_name_obj.value+"&track=1";
		} else {
			url = "/mason/RFS/components/rfs_saveSearch.html?id="+id+"&save=1&name="+search_name_obj.value+"&track=1";
		}
	url = encodeURI(url);
	var obj = document.getElementById("save_search_"+id);
	obj.innerHTML = '<span style="font: 10px arial; color: #175EA5;">updating...</span>';
	obj.style.display = "block";
	 
	var service = new AjaxService();
	service.setDelegate(function(r){
		var JSON = eval("(" + r + ")");
		obj.innerHTML = JSON.content;
		
	});
	service.setURL(url);
	service.callService();
	}
}

function showListings (label, id, show_hide_id){
	var show_id = document.getElementById(id);
	var show_icon_id = document.getElementById('show_listings_'+show_hide_id);
	var hide_icon_id = document.getElementById('hide_listings_'+show_hide_id);

	if (label == 'Hide'){
		show_id.style.display = "none";
		show_icon_id.style.display = "";
		hide_icon_id.style.display = "none";
	} else {
		show_id.style.display = "";
		show_icon_id.style.display = "none";
		hide_icon_id.style.display = "";
	}
}


function toggleBuildingListings (id, listing_ids){
	var building_obj = document.getElementById('building_'+id);
	var listings_obj = document.getElementById('listings_in_building_'+id);
	var show_icon_obj = document.getElementById('show_listings_'+id);
	var hide_icon_obj = document.getElementById('hide_listings_'+id);
	
	if (building_obj && listings_obj && show_icon_obj && hide_icon_obj){
		if ( !hasClass(building_obj,"cur") ){
			if ( !hasClass(listings_obj,"loaded") ){
				var url = "/mason/RFS/components/rfs_listing_list.html?show=1&listing_ids=" + listing_ids.join(',');
				var obj = document.getElementById('listings_in_building_'+id+'_content');
				var s = new AjaxService();
				s.setURL(url);
				var listings = (listing_ids.length > 1) ? listing_ids.length + " listings..." : " this listing...";
				listings_obj.innerHTML = "<div style='padding: 14px; font:bold 14px/14px arial; color; text-align:center;'><img src='/img/jk/loader_w.gif' />&nbsp;Loading " + listings + "</div>";
				s.setDelegate(function(r){
					var content = r.split("<!-- listing table -->")[1];
					listings_obj.innerHTML = content;
				});
				s.callService();
				addClass(listings_obj,"loaded");
			}
			addClass(building_obj,"cur");
//			addClass(listings_obj,"cur");
//			removeClass(listings_obj,"hide");
			hide_icon_obj.style.display = "inline-block";
			show_icon_obj.style.display = "none";
		}else{
			removeClass(building_obj,"cur");
//			removeClass(listings_obj,"cur");
//			addClass(listings_obj,"hide");
			hide_icon_obj.style.display = "none";
			show_icon_obj.style.display = "inline-block";
		}
	}
}

function pagerAjax(page,url){
	$("#page"+page+"_label")[0].className = "next loading";
	if (!url){
		document.rfsform.page.value = page;
		var a = loadFormElements();
		document.rfsform.page.value = 1;
		url = a.join("&");
	}
	url = "/mason/RFS/listings.html?"+url+"&content_only=1";
	var s = new AjaxService();
	s.setURL(url);
	s.setDelegate(function(r){
		with ($("#page"+page+"_label")[0]){
			innerHTML = "Page "+page;
			className = "next";
			style.textDecoration = "none";
			removeAttribute("onclick");
		}
		$("#page"+page+"_content")[0].innerHTML = r.split("<!-- results -->")[1];
		//getBanner(r, page);
	});
	s.callService();
}

function getBanner(r, page){
	var found = r.match(/\[CDATA\[([\w\W]*)\/\/\]\]/);
	if (!found) return;

	var a = [];
	eval(found[1].replace(/document\.write/g, "a.push"));

	var found = a.join("").match(/src='([^']+)'/);
	if (!found) return;

	var s = new AjaxService();
	s.setURL("/mason/Banners/get_banner_code.html?url="+encodeURIComponent(found[1]));
	s.setDelegate(function(r){
		var bc = $("#banner_container")[0];

		var iframe = document.createElement("iframe");  
		iframe.frameSpacing = 0;
		iframe.frameBorder = 0;
		iframe.scrolling = "no";
		iframe.width = 160;
		iframe.height = 600;
		iframe.marginWidth = 0;
		iframe.marginHeight = 0;
		bc.appendChild(iframe);

		if(iframe.contentDocument)
			iframe.doc = iframe.contentDocument; // Firefox, Opera
		else if(iframe.contentWindow)
			iframe.doc = iframe.contentWindow.document; // Internet Explorer
		else if(iframe.document)
			iframe.doc = iframe.document;

		iframe.doc.write("<script>"+ r +"<\/script>");

		iframe.style.marginTop = ( _UTIL_getPostop($("#page"+page+"_content")[0]) - _UTIL_getPostop(bc) - bc.offsetHeight + iframe.offsetHeight ) + "px";
		iframe.doc.close();
	});
	s.callService();
}

function loadListingsInBuilding(url){
	url = "/mason/RFS/components/rfs_listing_list.html?listing_ids="+ids;
	var s = new AjaxService();
	s.setURL(url);
	s.setDelegate(function(r){
		with ($("#page"+page+"_label")[0]){
			innerHTML = "Page "+page;
			className = "next";
			style.textDecoration = "none";
			removeAttribute("onclick");
		}
		$("#page"+page+"_content")[0].innerHTML = r.split("<!-- results -->")[1];
		//getBanner(r, page);
	});
	s.callService();
}

function featureListing(listing_id, label) {
	var url = "/mason/RFS/components/feature_listing.html?listing_id="+listing_id+"&save=1&ok_to_feature=1";

	var obj = document.getElementById("feature_listing_"+listing_id);
	loading_msg_s(obj);

	ajaxLoader(url, "feature_listing_"+listing_id);
}

function unFeatureListing(listing_id, label) {
	var url = "/mason/RFS/components/feature_listing.html?listing_id="+listing_id+"&remove=1&ok_to_feature=1";

	var obj = document.getElementById("remove_feature_listing_"+listing_id);
	loading_msg_s(obj);

	ajaxLoader(url, "remove_feature_listing_"+listing_id);
}

function deletePhoto(property_id, photo_id, propkey, photo_type) {
	var url = "/mason/RFS/components/uploaded_pics.html?image_id="+photo_id+"&delete=1&propkey="+propkey+"&property_id="+property_id+"&type="+photo_type;

	var service = new AjaxService();
	service.setDelegate(function(r){

		var my_div = document.getElementById("remove_photos");
		
		var JSON = eval("(" + r + ")");

		my_div.innerHTML = JSON.remove_photos_sent;
	});
	service.setURL(url);
	service.callService();

}

function rfstabs(o){
	if (typeof(o) != "object") o = document.getElementById("tab_"+o);
	if (!o) return;

	var submit = $("#rfs_search_submit_container")[0];
	var items = $("#rfstabs")[0].getElementsByTagName("a");
	for (var i=0;i<items.length;i++){
		var c = items[i];
		var v = c.id.replace(/tab_/, '');
		if (c==o){
			c.parentNode.className = "sel";
			$("#"+v)[0].className = "";

			document.rfsform.tab.value = v;
			if (v=="src_criteria"){
				var simple_submit = $("#rfs_simple_search_submit")[0];
				if (simple_submit.className.match(/hide/))
					$("#rfs_advanced_search_submit")[0].appendChild(submit);
				else
					simple_submit.appendChild(submit);
			}else{
				$("#submit_"+v)[0].appendChild(submit);
			}
		}else{
			c.parentNode.className = "";
			$("#"+v)[0].className = "hide";
		}
	}
	o.blur();
}
