// $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;
}