function outCountry()
{
	document.registerForm.zip.value='00001';
}

function validateForm(f)
{
	if(!validateName(f)) return false;
	if(!validateLast(f)) return false;
	if(!validateEmail(f)) return false;
	if(!validateZip(f)) return false;
	if(!validatePassword(f)) return false;

	if(!validateSelling(f)) return false;
	if(!validateMortgage(f)) return false;
	if(!validateMax(f)) return false;
	if(!validateMin(f)) return false;

	navigationOk = true;

	var elem = document.getElementById('register');
	elem.disabled = true;
	elem.value = 'Processing';
	return true;
}

function isEmpty(s)
{
	return ((s == null) || (s.length == 0))
}

function validatePassword(f)
{
	pass1 = f.txtPswd.value;

	if((pass1.indexOf(' ')!=-1))
	{
		alert("Your password cannot contain the space character.")
		f.txtPswd.focus();

		if(document.all || document.getElementByID)
		{
			f.txtPswd.style.background = "#edf6fd";
		}

		return false;
	}

	if(pass1.length < 4)
	{
		alert("Your password must be at least 4 characters long.");
		f.txtPswd.focus();

		if(document.all || document.getElementByID)
		{
			f.txtPswd.style.background = "#edf6fd";
		}
		return false;
	}

	return true;
}

function validateSelling(f)
{

	myOption1 = -1;
	for (i=0; i<f.selling.length; i++)
	{

		if (f.selling[i].checked)
		{
			myOption1 = i;
		}
	}

	if (myOption1 == -1)
	{
		alert("Please let us know whether you are planning to sell your property.");
		return false;
	}
	else
	{
		return true;
	}
}

function validateMax(f)
{
	var myOption3 = -1;
	for (i=0; i < f.maximum.length; i++)
	{
		if (f.maximum[i].checked)
		{
			myOption3 = i;
		}
	}

	if(myOption3 == -1)
	{
		alert("Please let us know whether you are already working with a real estate agent.");
		return false;
	}
	else
	{
		return true;
	}
}

function validateMin(f)
{
	myOption4 = -1;

	for (i=0; i<f.minimum.length; i++)
	{
		if (f.minimum[i].checked)
		{
			myOption4 = i;
		}
	}

	if (myOption4 == -1)
	{
		alert("Please let use know your estimated timeframe for buying.");
		return false;
	}
	else
	{
		return true;
	}
}

function validateName(f)
{
	if (f.firstname.value == "")
	{
		alert("Please supply your first name.");
		f.firstname.focus();
		if(document.all || document.getElementByID)
		{
			f.firstname.style.background = "#edf6fd";
		}
		return false;
	}
	return true;
}

function openforgot()
{
	window.open("forgot.html","print","height=140,width=400,status=0,scrollbars=0,location=0,menubar=0,resizable=0");
}

function openPol()
{
	window.open("privacysmall.html","privacy","height=600,width=540,status=0,scrollbars=1,location=0,menubar=0,resizable=0");
}

function openTerms()
{
	window.open("termsmall.html","privacy","height=600,width=540,status=0,scrollbars=1,location=0,menubar=0,resizable=0");
}

function validateLast(f)
{
	if (f.lastname.value == "")
	{
		alert("Please supply your last name.");
		f.lastname.focus();
		if(document.all || document.getElementByID)
		{
			f.lastname.style.background = "#edf6fd";
		}
		return false;
	}
	return true;
}

function validateZip(f)
{
	if (f.zip.value == "" || f.zip.value.length != 5)
	{
		alert("Make sure your zip code is 5 digits long.");
		f.zip.focus();
		if(document.all || document.getElementByID)
		{
			f.zip.style.background = "#edf6fd";
		}
		return false;
	}

	return true;
}

function validateEmail(f)
{
	if (f.email.value == "")
	{
		alert("Please type your email address.");
		f.email.focus();
		return false;
	}

	if(!check_email(f.email.value))
	{
		alert("Please enter a proper email address.");
		f.email.focus();
		// if the browser is Netscape 6 or IE

		if(document.all || document.getElementByID)
		{
			// change the color of text field
			f.email.style.background = "#edf6fd";
		}

		// make sure the form is not submitted
		return false;
	}

	if( haswww(f.email.value))
	{
		alert("Please remove the WWW from the beginning of your email address.");
		f.email.focus();

		if(document.all || document.getElementByID)
		{
			f.email.style.background = "#edf6fd";
		}
		return false;
	}

	if(isshort(f.email.value))
	{
		alert("Your email address doesn't appear legitimate, please enter a real email address.");
		f.email.focus();

		if(document.all || document.getElementByID)
		{
			f.email.style.background = "#edf6fd";
		}

		return false;
	}

	return true;
}

function haswww(email)
{
	val = email.substring(0,4);
	val = val.toLowerCase();

	if( val == 'www.')
	{
		return true;
	}
	else
	{
		return false;
	}
}

function isshort(email)
{
	email = email.toLowerCase();
	temp = email.split('@');
	after = temp[1];

	if( after.indexOf('hotmail.com') == -1 && after.indexOf('msn.com')  == -1 && after.indexOf('aol.com')  == -1 && after.indexOf('yahoo.com')  == -1 && after.indexOf('usa.com')  == -1 && after.indexOf('mo.com')  == -1)
	{
		isfree = false;
	}
	else
	{
		isfree = true;
	}

	if(temp[0].length < 4 && isfree == true)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function xmlhttpPost(strURL)
{
	var xmlHttpReq = false;
	var self = this;
	// Mozilla/Safari
	if (window.XMLHttpRequest)
	{
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject)
	{
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open('POST', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq.onreadystatechange = function()
	{
		if (self.xmlHttpReq.readyState == 4)
		{
			updatepage(self.xmlHttpReq.responseText);
		}
	}
	self.xmlHttpReq.send(getquerystring());
}

function getquerystring()
{
	var word = document.registerForm.email.value;
	qstr = 'email=' + escape(word);  // NOTE: no '?' before querystring
	return qstr;
}

function updatepage(jsonText)
{
	var resultObj = eval('(' + jsonText + ')');
	if(resultObj.valid == 'yes')
	{
		// auto correct email address
		if(resultObj.semail && resultObj.semail != '') {
			document.registerForm.email.value = resultObj.semail;
		}
		return true;
	}
	else
	{
		alert("Error:  Bad email format.\nPlease correct your email address.");
		document.registerForm.email.focus();
		return false;
	}
}

function check_email(e)
{
	ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";

	for(i=0; i < e.length ;i++)
	{
		if(ok.indexOf(e.charAt(i))<0)
		{
			return (false);
		}
	}

	if (document.images)
	{
		re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
		re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;

		if (!e.match(re) && e.match(re_two))
		{
			return (-1);
		}
	}
}

function onLoad()
{
	// on error page can't set these
	if(!document.getElementById('day'))
		return;
	setCanCall();
	addEvent(document.getElementById('day'), 'blur', setCanCall);
	addEvent(document.getElementById('night'), 'blur', setCanCall);
}

function setCanCall()
{
	var phone = document.getElementById('day');
	var cell = document.getElementById('night');

	var radios = document.getElementsByName('cancall');

	if(phone.value == '' && cell.value == '')
	{
		for(var i = 0; i < radios.length; i++)
			radios[i].disabled = false;
	}
	else
	{
		for(var i = 0; i < radios.length; i++)
			radios[i].disabled = true;
	}
}


