var popup = null;

function hideEmailForm(redirect)
{
	if (redirect != null)
	{
		window.location = redirect;
	}

	if (popup != null)
		popup.style.display = 'none';

	if (timer != null)
		clearTimeout(timer);
}

function showEmailConfirmation(success)
{
	if (popup != null)
		popup.style.display = 'none';

	if (success)
		popup = document.getElementById('email_confirmation_success');
	else
		popup = document.getElementById('email_confirmation_error');

	positionDiv(popup);
	timer = setInterval("positionDiv(popup)", 250);
	popup.style.display = 'block';
}

function positionDiv(obj)
{
	if (obj.style.display != 'block') {
		return;
	}

	scrollTop = document.documentElement.scrollTop;
	if (scrollTop == 0)
		scrollTop = document.body.scrollTop;

	y = scrollTop + 150;
	x = parseInt((document.body.clientWidth - 600) / 2);

	obj.style.top = y + 'px';
	obj.style.left = x + 'px';
}

function showForm(id) {
	if (document.getElementById(id).style.display == 'block')
		return;

	document.getElementById('residential').style.display = 'none';
	document.getElementById('condominium').style.display = 'none';
	document.getElementById('lot').style.display = 'none';

	document.getElementById(id).style.display = 'block';
	document.getElementById('div_error').innerHTML = '';
}

function validEmail(email)
{
	return email.match(/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i);
}

function isValid(required)
{
	error_elem = document.getElementById('div_error');
	error_elem.innerHTML = '';

	all_valid = true;
	for (var i in required) {
		valid = false;
		for (var j in required[i]) {
			elem = document.getElementById(required[i][j]);
			if (elem.value != "")
				valid = true;
		}

		if (!valid) {
			if (error_elem.innerHTML != '')
				error_elem.innerHTML += '; ';
			if (required[i].length > 1)
				error_elem.innerHTML += 'one of ';
		}

		for (var j in required[i]) {
			elem = document.getElementById(required[i][j]);
			if (valid) {
				elem.className = 't';
			} else {
				elem.className = 't_error';

				try
				{
					if (j > 0) {
						error_elem.innerHTML += ', ';
						if (j == required[i].length - 1)	{
							error_elem.innerHTML += 'or ';
						}
					}

					error_elem.innerHTML += document.getElementById(required[i][j] + '_error').innerHTML;
				}
				catch (error)
				{}
			}
		}

		if (!valid) {
			all_valid = false;
		}
	}

	return all_valid;
}

function validate()
{
	var required = [
		['g_first_name'], 
		['g_last_name'], 
		['g_street_address'], 
		['g_city'], 
		['g_province'], 
		['g_postal_code'],
		['g_day_phone', 'g_cell_phone', 'g_phone', 'g_email']
	];

	if (document.getElementById('t_residential').checked) {
		required.push(['r_location']);
		required.push(['r_house_size']);
		required.push(['r_bedrooms']);
		required.push(['r_bathrooms']);
	} else if (document.getElementById('t_condominium').checked) {
		required.push(['c_location']);
		required.push(['c_house_size']);
		required.push(['c_bedrooms']);
		required.push(['c_bathrooms']);
	} else if (document.getElementById('t_lot').checked) {
		required.push(['l_location']);
		required.push(['l_preferred_size']);
	}

	all_valid = isValid(required);

	elem = document.getElementById('div_error');
	if (!all_valid) {
		elem.innerHTML = 'Please enter ' + elem.innerHTML + '.';
	}
	
	email = document.getElementById('g_email').value
	if (email != '' && !validEmail(email)) {
		elem.innerHTML += '  Please enter a valid email address.';
		document.getElementById('g_email').className = 't_error';
		return false;
	}

	return all_valid;
}

function homeEvalValidate()
{
	var required = [
		['g_first_name'], 
		['g_last_name'], 
		['g_street_address'], 
		['g_city'], 
		['g_province'], 
		['g_postal_code'],
		['g_day_phone', 'g_cell_phone', 'g_phone', 'g_email'],
		['home_style'],
		['lot_size'],
		['sqft'],
		['age'],
		['CaptchaStr']
	];

	if (document.getElementById('address_matches').checked == false) {
		required.push(['p_street_address']);
		required.push(['p_city']);
		required.push(['p_province']);
		required.push(['p_postal_code']);
	} else {
		addressMatches(true);
	}

	all_valid = isValid(required);

	elem = document.getElementById('div_error');
	if (!all_valid) {
		elem.innerHTML = 'Please enter ' + elem.innerHTML + '.';
	}
	
	email = document.getElementById('g_email').value
	if (email != '' && !validEmail(email)) {
		elem.innerHTML += '  Please enter a valid email address.';
		document.getElementById('g_email').className = 't_error';
		return false;
	}

	return all_valid;
}

function copyValue(to, from)
{
	document.getElementById(to).value = document.getElementById(from).value;
}

function addressMatches(copy)
{
	var fields = ['street_address', 'city', 'province', 'postal_code', 'unit'];

	for (var i in fields) {
		document.getElementById('p_' + fields[i]).disabled = copy;
		document.getElementById('p_' + fields[i]).className = 't';
	}

	if (!copy)
		return;

	for (var i in fields) {
		document.getElementById('p_' + fields[i]).style.border = '1px solid #777777';
		copyValue('p_' + fields[i], 'g_' + fields[i]);
	}
}

function PropertyInquiryValidate()
{
	var hasError = false;
	var fields = ['name', 'email', 'phone', 'captcha'];

	for (var i in fields) {
		err_elem = document.getElementById('err_client_' + fields[i]);
		pi_removeChildren(err_elem);
		if (document.getElementById('pi_client_' + fields[i]).value == "") {
			if (fields[i] != 'captcha') {
				pi_showError(err_elem, '<< Please enter your ' + fields[i]);
			} else {
				pi_showError(err_elem, '<< Please enter the web form code');
			}
			hasError = true;
		}
	}

	email = document.getElementById('pi_client_email').value
	if (email != '' && !validEmail(email)) {
		pi_showError(document.getElementById('err_client_email'), '<< Please enter a valid email address');
		return false;
	}

	return !hasError;
}
