// $Id: validators.js,v 1.3 2004/12/15 12:49:47 cscharf Exp $
// $Date: 2004/12/15 12:49:47 $
// $Author: cscharf $

// -- Contact Manager Validators --

function isInvalidEmail( toCheck, parameters ) {
	var str = toCheck.value;
	
	if(!str)
		return false;
	
	var re = /^[\w- | ']+(\.[\w- | ']+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
	if( !str.match(re) )
		return true;
	else
		return false;
}

function formatPhone( toCheck ) {
	var AreaCode, PreFix, SuFix, parts;
	if (toCheck.value.length == 10) {
		AreaCode = toCheck.value.substring(0, 3);
		PreFix = toCheck.value.substring(3, 6);
		SuFix = toCheck.value.substring(6, 10);
		toCheck.value = '(' + AreaCode + ') ' + PreFix + '-' + SuFix;
	}
	else if (toCheck.value.length == 7) {
		PreFix = toCheck.value.substring(0, 3);
		SuFix = toCheck.value.substring(3, 7);
		toCheck.value = PreFix + '-' + SuFix;
	}
	else if (toCheck.value.length == 12) {
		if (toCheck.value.indexOf('.') > 0) {
			parts = toCheck.value.split('.');
			if( parts.length >= 3 ) {
				AreaCode = parts[0];
				PreFix = parts[1];
				SuFix = parts[2];
				toCheck.value = '(' + AreaCode + ') ' + PreFix + '-' + SuFix;
			}
			else if( parts.length == 2 ) {
				PreFix = parts[0];
				SuFix = parts[1];
				toCheck.value = PreFix + '-' + SuFix;
			}
		}
		else if (toCheck.value.indexOf('-') > 0) {
			parts = toCheck.value.split('-');
			if( parts.length >= 3 ) {
				AreaCode = parts[0];
				PreFix = parts[1];
				SuFix = parts[2];
				toCheck.value = '(' + AreaCode + ') ' + PreFix + '-' + SuFix;
			}
			else if( parts.length == 2 ) {
				PreFix = parts[0];
				SuFix = parts[1];
				toCheck.value = PreFix + '-' + SuFix;
			}
		}
		else if (toCheck.value.indexOf('(') == 0 && toCheck.value.indexOf(')') == 4) {
			AreaCode = toCheck.value.substring(1, 4);
			PreFix = toCheck.value.substring(5, 8);
			SuFix = toCheck.value.substring(8, 12);
			toCheck.value = '(' + AreaCode + ') ' + PreFix + '-' + SuFix;
		}
	}
	else if (toCheck.value.length == 8) {
		if (toCheck.value.indexOf('.') > 0) {
			parts = toCheck.value.split('.');
			if( parts.length == 2 ) {
				PreFix = parts[0];
				SuFix = parts[1];
				toCheck.value = PreFix + '-' + SuFix;
			}
		}
		else if (toCheck.value.indexOf('-') > 0) {
			parts = toCheck.value.split('-');
			if( parts.length == 2 ) {
				PreFix = parts[0];
				SuFix = parts[1];
				toCheck.value = PreFix + '-' + SuFix;
			}
		}
	}
	else if (toCheck.value.length == 13) {
		if (toCheck.value.indexOf('(') == 0 && toCheck.value.indexOf(')') == 4 && toCheck.value.indexOf(' ') == 5) {
			AreaCode = toCheck.value.substring(1, 4);
			PreFix = toCheck.value.substring(6, 9);
			SuFix = toCheck.value.substring(9, 13);
			toCheck.value = '(' + AreaCode + ') ' + PreFix + '-' + SuFix;
		}
	}
	return false;
}


// -- SignUp Validators --

function validateRequired(value, errorid)
{
	if(value == '' || value == 0)
	{
		document.getElementById(errorid).style.display = 'block';
		continueValidation = false;
		return false;
	}
	else
	{
		document.getElementById(errorid).style.display = 'none';
		continueValidation = true;
		return true;
	}
}

function validateEmail(value1, value2)
{
	// our email regular expression (http://www.regexlib.com)
	var regex=/^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	var emailtest = regex.test(value1);
	
	if(emailtest == true)
	{
		if(value1 !== value2)
		{
			document.getElementById('email2Error').style.display = 'block';
			return false;
		}
		else
		{
			document.getElementById('email1Error').style.display = 'none';
			document.getElementById('email2Error').style.display = 'none';
			return true;
		}
	}
	else
	{
		document.getElementById('email1Error').style.display = 'block';
		return false;
	}	
}

function validateWatson(value1, value2)
{
	// our email regular expression (http://www.regexlib.com)
	//var regex=/^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	var regex=/^[a-zA-Z0-9._-]+@(watsonrealtycorp\.com|watsontitleservices\.com|watsonmortgagecorp\.com|watsontitle\.net|watsoncommercial\.com)$/i;
	var emailtest = regex.test(value1);
	
	if(emailtest == true)
	{
		if(value1 !== value2)
		{
			document.getElementById('email2Error').style.display = 'block';
			return false;
		}
		else
		{
			document.getElementById('email1Error').style.display = 'none';
			document.getElementById('email2Error').style.display = 'none';
			return true;
		}
	}
	else
	{
		document.getElementById('email1Error').innerText = 'Please enter a valid Watson Realty email address to complete signup.';
		document.getElementById('email1Error').style.display = 'block';
		return false;
	}	
}

function validateUsername(value1, value2)
{
	if(value1 == value2)
	{
		document.getElementById('usernameError').style.display = 'none';
		return true;
	}
	else
	{
		document.getElementById('usernameError').innerText = 'Please use your email address for your username';
		document.getElementById('usernameError').style.display = 'block';
		return false;
	}	
}

function validateZip(value, errorid)
{	
	//matches US zipcodes not allowing all zeros in first 5 or +4 (http://www.regexlib.com)
	//examples: [12345], [12345-6789], [123456789]
	if (value.match(/^[0-9]{5}$/)) {
        return true;
    }
    value=value.toUpperCase();
    if (value.match(/^[A-Z][0-9][A-Z][0-9][A-Z][0-9]$/)) {
        return true;
    }
    if (value.match(/^[A-Z][0-9][A-Z].[0-9][A-Z][0-9]$/)) {
        return true;
    }
	else
	{
		document.getElementById(errorid).style.display = 'block';
		return false;
	}
}

function validatePhone(value, errorid)
{
    if(value.match(/^(\+[0-9]{1,3}[-. ]|[0-9]{1,3}[-. ])?(\([0-9]{2,6}\)|[0-9]{2,6}\)?) ?[-.]? ?[0-9]{3,4} ?[-.]? ?[0-9]{4,6}( ?([-.x]|ext[ens]?|extens?|extension) ?[0-9]+)?$/i))
        return true;
    else
    {
        document.getElementById(errorid).style.display = 'block';
        return false;
    }
}

function validatePassword(value1, value2)
{ 
	// make sure the passwords are at least 6 characters
	if(value1.length < 6)
	{
		document.getElementById('password1Error').style.display = 'block';
		return false;
	}
	else
	{
		if(value1 !== value2)
		{
			document.getElementById('password2Error').style.display = 'block';
			return false;
		}
		else
		{
			document.getElementById('password1Error').style.display = 'none';
			document.getElementById('password2Error').style.display = 'none';
			return true;
		}
	}	
}

function validateNumeric(value, errorid)
{ 
	//match on numeric values. Handles negatives, and comma formatted values. Also handles a single decimal point
	//Matches: 	[5,000], [-5,000], [100.044] (http://www.regexlib.com)
	var regex=/^(\d|-)?(\d|,)*\.?\d*$/;
 
	if (value !== '' && regex.test(value))
	{
		document.getElementById(errorid).style.display = 'none';
		return true;
	}
	else
	{
		document.getElementById(errorid).style.display = 'block';
		return false;
	}
}

function validateCheckbox(id, errorid)
{
	if(document.getElementById(id).checked)
	{
		document.getElementById(errorid).style.display = 'none';
		return true;
	}
	else
	{
		document.getElementById(errorid).style.display = 'block';
		return false;
	}
}
