function is_numeric( mixed_var ) { return !isNaN( mixed_var ); }

function is_postal(strValue)
{
	strValue = strValue.replace(' ', '');
	var objRegExp = /^([A-Z][0-9]){3}$/i;
	return objRegExp.test(strValue);
}

function trace(msg){ alert("Please correct the following:\n"+msg); }

var postBody = _postBody = "";
function build_post(form, act)
{
	var data = msg = "";
	var obj = form.elements;
	for (var i=0; i<obj.length; i++)
	{
		var elm = obj[i];
		var name = elm.name;
		var value = (elm.type == "select")?$(elm.id).options[$(elm.id).selectedIndex].value:elm.value;
		data += "&"+name+"="+value;
		
		if (name == "city" && value == "") msg += "- Please enter a valid City or Postal Code\n";
		else if (name == "city")
		{
			//if (!is_postal($("city").value)) if ($("city").value.length != 5) msg += "- Please enter a valid US Zip Code\n";
		}
		else if (!is_postal($("city").value) && name == "state" && value == "") msg += "- Please select a State\n";
	}
	
	return([data,msg]);
}

function validate(form)
{
	if (checking_city) setTimeout(validate(form), 900);
	
	var data = build_post(form);
	postBody = data[0];
	
	if (data[1] != "")
	{ 
		trace(data[1]); 
		return false 
	}
	else 
	{
		sendSearch();
		return true 
	}
}

var checking_city = false;
var lc_save = "";
function __check_city(city)
{
	$("city").style.borderColor = "";
	$("searchbutton").className = "searchbutton-disabled";
	if (is_postal(city)) return true;
	
	if (lc_save == city || city == "") return false; else lc_save = "";
	checking_city = true;
	
	new Ajax.Request("/actions.php", {
		postBody: "&action=checkcity&city="+city,
		onFailure: function() {},
		onComplete: function(transport)
		{
			var response = transport.responseText;
			var json = response.evalJSON();
			if (json.valid == "1")
			{
				lc_save = "";
				for (var i=0; i<$("state").options.length; i++)
				{
					if ($("state").options[i].value == json.state)
					{
						$("state").selectedIndex = i;
						checking_city = false;
						break;
					}
				}
				$("searchbutton").className = (_postBody != postBody)?"searchbutton":"searchbutton-disabled";
				return true;
			}
			else
			{
				checking_city = false;
				lc_save = city;
				trace("- The city \""+city+"\" is invalid or not a canadian city.");
				$("city").value = "";
				$("city").style.borderColor = "#FF0000";
				$("searchbutton").className = "searchbutton-disabled";
				return false;
			}
			delete(transport, response, json);
		}
	});
}

function sendSite(site)
{
	window.open("point.php?site="+site+postBody);
}

function sendSearch()
{
	__view_searching();
	__notifyer();
	__advertisers();
	__zipcodes();
	if (is_numeric($("city").value)) $("state").selectedIndex = 0;
	new Ajax.Request("/gather.php", {
		postBody: postBody,
		onFailure: function() {},
		onComplete: function(transport)
		{
			var response = transport.responseText;
			$("results").innerHTML = response;
			setTimeout("__view_results();", 1500);
			delete(transport, response);
		}
	});
}

function __zipcodes()
{
	if (is_numeric($('city').value))
	{
		if ($('zipcodewarning').style.display == "none")
		{
			$('zipcodewarning').style.display = "inline";
		}
	}
	else
	{
		if ($('zipcodewarning').style.display != "none")
		{
			$('zipcodewarning').style.display = "none";
		}
	}
}

var default_advertiser_container='';
function __advertisers()
{
	//$("advertisers").style.display = "block";
	$("advertiser_container").style.display = "block";
	new Ajax.Request("/actions.php", {
		postBody: '&action=listings'+postBody,
		onFailure: function() {},
		onComplete: function(transport)
		{
			var response = transport.responseText;
			//alert(response);
			if (response != "") $("advertiser_container").innerHTML = response;
			else $("advertiser_container").innerHTML = default_advertiser_container;
			delete(transport, response);
		}
	});
}

function __notifyer()
{
	if ($('notifyer')) $('notifyer').style.display = "inline";
	new Effect.Appear('notifyer', {speed:0.15, afterFinishInternal:function()
	{
		new Effect.Pulsate('notifyer', {pulses: 8});
	}});
}

function __view_searching()
{
	$('resultsin').style.display = "none";
	$('resultsin').style.top = "200px";
	$("searchbutton").disable();
	$("searchbutton").className = "searchbutton-disabled";
	$("results").setStyle({ 'display':'none' });
	$("resultswhat").setStyle({ 'display':'none' });
	$("resultssearching").setStyle({ 'display':'block' });
}

function __flash_results()
{
	//new Effect.Appear('resultsin', {speed:0.15, afterFinishInternal:function()
	//{
		//new Effect.Pulsate('resultsin', {pulses: 8});
	//}});
	//new Effect.Pulsate($("resultsin"), {pulses: 10, duration: 3});
	//new Effect.Highlight($("resultsin"), {keepBackgroundImage:true, duration: 1.5});
	new Effect.Move($("resultsin"), { x: 0, y: -30, mode: 'absolute' });
	setTimeout('new Effect.Pulsate($("resultsin"), {pulses: 1, duration: 4});', 500);
}

function __view_results()
{
	$("resultssearching").setStyle({ 'display':'none' });
	$("results").setStyle({ 'display':'block' });
	
	if (name == "" && access == "") setTimeout("__window_login('true');", 500);
	else __do_flash();
}

function __do_flash()
{
	$('resultsin').style.display = "inline";
	setTimeout("__flash_results();", 500);
}

function __window_login(login)
{
	OpenWindow('login', {noclose:true});
	$('wfp').style.display='none';
	$('wfl').style.display='inline';
	$("_datasignup").value = postBody;
	$("_datalogin").value = postBody
}

function __view_original()
{
	$("searchbutton").enable();
	$("searchbutton").className = "searchbutton";
	$("resultssearching").setStyle({ 'display':'none' });
	$("results").setStyle({ 'display':'none' });
	$("resultswhat").setStyle({ 'display':'block' });
}

function __data_changed()
{
	if (!$('searchform')) return(false);
	var data = build_post($('searchform'));
	_postBody = data[0];
	if (_postBody != postBody) $("searchbutton").enable(); else $("searchbutton").disable();
	if (!checking_city) $("searchbutton").className = (_postBody != postBody)?"searchbutton":"searchbutton-disabled";
	else $("searchbutton").className = "searchbutton-disabled";
}

var _alpha = "A";
function GlossFetch(alpha)
{
	$("glossary-content-"+_alpha).setStyle({'display':'none'});
	$("glossary-content-"+alpha).setStyle({'display':'inline'});
	_alpha = alpha;
}

function Fetch(url, to)
{
	//Fetch('gterms/A.inc.htm');
	new Ajax.Request(url, {
		onComplete: function(transport)
		{
			$(to).innerHTML = transport.responseText;
			delete(transport);
		}
	});
}

var lh = "";
function Error(error, highlight)
{
	if (lh != "") $(lh).style.borderColor = "";
	OpenWindow("warning", {content:error});
	if (highlight != "")
	{
		$(highlight).style.borderColor = "#FF0000";
		lh = highlight;
	} else lh = "";
}

function Success(id, message)
{
	if (lh != "") $(lh).style.borderColor = "";
	lh = "";
	
	if (id == "forgotpass" || id == "landlordforgotpass" || id == "supplierforgotpass")
	{
		if (id == "forgotpass") $("wfp").innerHTML = message;
		else if (id == "landlordforgotpass") $("wfp_landlord").innerHTML = message;
		else if (id == "supplierforgotpass") $("wfp_supplier").innerHTML = message;
	}
	else
	{
		if ($(id+"form")) $(id+"form").style.display = "none";
		$(id+"done").style.display = "inline";
		$(id+"done").innerHTML = message;
	}
}



