function OpenLoanCalc(total)
{
	window.open("/Help/LoanCalc.php?total="+total,"","height=200,width=400,left=80,top=80,scrollbars=1,resizable=1")
}

function checkAllInList(form,name,chk,cb)
{  
	f = eval("document."+form);
	
	for( i = 0; i < f.elements.length; i++)
	{
		if( f.elements[i].name == name )
		{
			if( (!f.elements[i].checked && chk) || (f.elements[i].checked && !chk) )
			{
				f.elements[i].click();
			}
		}
	}
}

function checkAllInListByID(form,name,id,chk)
{  
	f = eval("document."+form);
	
	for( i = 0; i < f.elements.length; i++)
		if( (f.elements[i].name == name) && (f.elements[i].id == id) )
			f.elements[i].checked = chk;
}


function helpWindow()
{
	p = window.open('/Help/HelpCatList.php', 'help', 'width=600,height=300,resizable=yes,scrollbars=yes,status=0');
}

function openWindow(url, name) {
	popupWin = window.open(url, name, "width=640,height=480");
}

function openPopup(name,url,width,height)
{
	if(typeof width == 'undefined')		width = 400;
	if(typeof height == 'undefined')		height = 400;
	
	switch(name)
	{
		case "editor":
			width=600;
			height=600;
			break;
		case "smalleditor":
			width=350;
			height=250;
			break;
	}

	var win = window.open(url, name, 'width='+width+',height='+height+',resizable=yes,scrollbars=yes,status=0');
}

function uploadImage(src)
{
	url = "/Admin/MediaMaint/MediaUpload.php?uploadLoc="+src;
	var win = window.open(url, "imageupload", 'width=640,height=480,resizable=yes,scrollbars=yes');
}

function help(text)
{
	p = window.open('/Help/Help.php?query='+text, 'help', 'width=600,height=300,resizable=yes,scrollbars=yes,status=0');
}

function emailWindow(text)
{
	var w = _emailWindow('/Email/SendEmailMsg.php?'+text);
}

function _emailWindow(url)
{
	p = window.open(url, 'email', 'width=600,height=600,resizable=yes,scrollbars=yes,status=0,toolbar=yes');
}

function showStatus(msg)
{
	window.status=msg;
	return true;
}

function check_county_towns(cb, cnty)
{
	regexp = "^[TA]-(.*)-" + cnty + "$";
	check_and_disable_children(cb, regexp);
}

function check_town_areas(cb, cnty, tnum)
{
	regexp = "^A-(.*)-" + tnum + "-" + cnty + "$";
	check_and_disable_children(cb, regexp);
}

function check_and_disable_children(cb, regexp)
{
	f = cb.form;
	re = new RegExp(regexp);

   for( i = 0; i < f.elements.length; i++ )
   {
      s = new String(f.elements[i].value);
		res1 = s.search(re);

		if( res1 != -1 )
		{
			if( cb.checked == 1 )
			{
				f.elements[i].checked = 0;
				f.elements[i].disabled = 1;
			}
			else
			{
				f.elements[i].disabled = 0;
			}
		}
	}
}

function check_all_in_county(fn, ms, c)
{
	for( i = 0; i < ms.length; i++ )
	{
		e = document.getElementByID(ms[i]);
		e.checked = c;
	}

	return 1;

	f = eval("document." + fn);
	n = f.elements.length;
	cn = "";

	for( i = 0; i < n; i++)
	{
		if( f.elements[i].className == ms )
		{
			cn = f.elements[i].className;
			f.elements[i].checked = c;
		}
		else
		{
			// bail out when done
			if( f.elements[i].className != cn && cn != "" )
			{
				return 1;
			}

			cn = "";
		}
	}

/*
   // ms = match string, match it at the end of the value
   re1 = new RegExp("^T-(.*)-" + ms + "$");
   re2 = new RegExp("^A-(.*)-" + ms + "$");

   for( i = 0; i < f.elements.length; i++ )
   {
      s = new String(f.elements[i].value);

      res1 = s.search(re1);
      res2 = s.search(re2);

      if( res1 != -1 || res2 != -1 )
      {
         f.elements[i].checked = c;
      }
   }
*/
}

function doRedirect(formName, formVar, reset)
{
   f = eval("document." + formName + "." + formVar);
   url = f.options[f.selectedIndex].value;

   if( url != "" )
   {
		var is_js_code = (url.substr(0, 11).toLowerCase() == 'javascript:');

		// execute code instead, IE url handling is dumb
		if( is_js_code )
		{
			var js_code = url.substr(11, url.length-1);
			eval(js_code);
		}
		else
		{
      	window.location = url;
		}

		if( reset )
		{
			f.selectedIndex = 0;
		}
   }

   return 1;
}

function resetMenu(menu)
{
	if( menu.options[menu.selectedIndex].value == "" )
	{
		menu.selectedIndex = 0;
	}
}

function replaceSpanText(span, text)
{
	var spanElem = document.getElementById(span)
	var newText = document.createTextNode(text);
	var replaced = spanElem.replaceChild(newText,spanElem.firstChild);
}

function getSpanText(span)
{
	var spanElem = document.getElementById(span);
	return spanElem.firstChild.nodeValue;
}

function update_cb_count(n)
{
	var text = getSpanText("CBCount");

	if( text == "no" ) text = 0;

	var count = text - 0;
	count = n == 1 ? count + 1 : count - 1;

	if( count >= 0 )
	{
		if( count == 0 ) count = "no";

		replaceSpanText("CBCount", count);
	}
}

function _toggle_cb(cb, img_url, img_name, form, field)
{
	var img = document.getElementById(img_name);
	img.src = img_url;
	var toggle = cb.checked ? 1 : 0;
	update_cb_count(toggle);
	setdef_ps_toggle(form, field);

	toggle_cb_update_hook();
}

function toggle_cb_prop(cb, type, listnum, form, field)
{
	var toggle = cb.checked ? 1 : 0;
	var img_url = "/Properties/CBToggle.php/" + type + "/" + toggle + "/" + listnum + "/cb.gif";
	_toggle_cb(cb, img_url, "CBBug", form, field);
}

function toggle_cb_member(cb, uid, form, field)
{
	var toggle = cb.checked ? 1 : 0;
	var img_url = "/Members/CBToggle.php/" + toggle + "/" + uid + "/cb.gif";
	_toggle_cb(cb, img_url, "CBBug", form, field);
}

function _toggle_cb_page(cb, form, field, img_url, img_name)
{
	var i = 0;
	var check = '';
	var f = document.getElementById(form);
	var toggle = cb.checked + 0;
	var ids = '';

	for( i = 0; i < f.elements.length; i++)
	{
		check = f.elements[i];

		if( check.name == field )
		{
			if( (!toggle && check.checked) || (toggle && !check.checked) )
			{
				if( ids != '' ) ids = ids + ",";
				ids = ids + check.value;
				check.checked = toggle;
				update_cb_count(toggle + 0);
			}
		}
	}

	setdef_ps_toggle(form, field);
	var img = document.getElementById(img_name);
	img.src = img_url + ids + "/cb.gif";

	toggle_cb_update_hook();
}

function toggle_cb_prop_page(cb, form, type)
{
	var toggle = cb.checked + 0;
	var img_url = "/Properties/CBToggle.php/" + type + "/" + toggle + "/";
	_toggle_cb_page(cb, form, "MarkedProps", img_url, "CBBug");
}

function toggle_cb_member_page(cb, form)
{
	var toggle = cb.checked + 0;
	var img_url = "/Members/CBToggle.php/" + toggle + "/";
	_toggle_cb_page(cb, form, "MarkedUsers", img_url, "CBBug");
}

function setdef_ps_toggle(form, field)
{
	var i = 0;
	var check = '';
	var f = document.getElementById(form);
	var allMarked = true;

	for( i = 0; i < f.elements.length; i++)
	{
		check = f.elements[i];

		if( check.name == field && !check.checked )
		{
			allMarked = false;
			break;
		}
	}

	var replaceText = allMarked ? "Deselect All" : "Select All";
	var check = allMarked ? true : false;

	var cb = document.getElementById('SelectAllTop');
	cb.checked = check;
	replaceSpanText("SelectAllTextTop", replaceText);
	cb = document.getElementById('SelectAllBottom');
	replaceSpanText("SelectAllTextBottom", replaceText);
	cb.checked = check;

	return true;
}

function confirm_cb_action(url)
{
	count = getSpanText("CBCount") - 0;

	if( count > 0 )
	{
		window.location = url;
	}
	else
	{
		alert("You have no Selected Properties");
	}
}

function delete_saved_search(sid, sname, ret)
{
	var url = "http://" + location.host + "/Properties/DeleteSearch.php?SID=" + sid + "&ret=" + ret;
	var prompt = "Are you sure you want to delete the search " + 
							sname + "?";

	if( confirm(prompt) )
	{
		window.location = url;
	}
}

function howtousemysite()
{
	p = window.open('/Help/Manual/HowToUseMySite.html', 'howtousemysite', 
						'width=700,height=500,resizable=yes,scrollbars=yes,status=0');
}

function requestcma()
{
	p = window.open('/Forms/RequestCMA.php?blank=1', 'requestcma', 
						'width=700,height=500,resizable=yes,scrollbars=yes,status=0');
}


function editcellphone()
{
	p = window.open('/Properties/EditCellPhone.php', 'editcellphone', 
						'width=350,height=250,resizable=yes,scrollbars=yes,status=0');
}

function showhide(v, e, o) {
	var type = o ? "inline" : "block";
	var el = document.getElementById(e);
	if( el ) el.style.display = v ? type : "none";
}

function openHouseWindow(id)
{
	p = window.open('/Properties/ViewOpenHouse.php?ListingID=' + id, 'openhouse', 'width=300,height=200,resizable=yes,scrollbars=yes,status=0,toolbar=no');
}


function miscWindow(link,width,height)
{
	p = window.open(link, 'miscwin', 'width=' + width + ',height=' + height + ',resizable=yes,scrollbars=yes,status=0,toolbar=no');
}
