IE=document.all;
//onLoad="setup_fields(document.Contact);"


var redo=false;

function setup_fields( form )
{
    with( form )
    {
	if( typeof( ContactName ) == "object" )
	{
	    ContactName.xmin = 1;
		ContactName.maxLength = 100;
	    ContactName.xlabel = "Name";
	    ContactName.xvalidate = isString;
	    ContactName.xerror = showError;
	    ContactName.xerrmsg = "You must enter your name.";
	}
	if( typeof( ContactHomePhone ) == "object" )
	{
	    ContactHomePhone.xmin = 10;			// Min numbers required. Set to 0 for not required, 10 (in this case) if it is.
	    ContactHomePhone.xeditmax = 10;			// Max numbers in phone number.
	    ContactHomePhone.maxLength = 14;			// Max characters including ()'s and spaces.
	    ContactHomePhone.xlabel = "Phone Number";	// A more visually appealing name for the field.
	    ContactHomePhone.onkeypress = editPhone;
	    ContactHomePhone.xvalidate = isPhone;
	    ContactHomePhone.xerror = showError;
	    ContactHomePhone.xerrmsg = "Your home phone number is incomplete. It must contain the full number including Area Code.";
	}
	if( typeof(ContactEmail ) == "object" )
	{
	    ContactEmail.xmin = 0;
	    ContactEmail.xlabel = "Email Address";
	    ContactEmail.xvalidate = isEmail;
	    ContactEmail.xerror = showError;
	    ContactEmail.xerrmsg = "Please enter a valid email address.";
	}
	if( typeof( ContactWorkPhone ) == "object" )
	{
	    ContactWorkPhone.xlabel = "Phone Number";
	    ContactWorkPhone.onkeypress = editPhone;
	}
	if( typeof( ContactCellPhone ) == "object" )
	{
	   ContactCellPhone.xlabel = "Phone Number";
	    ContactCellPhone.onkeypress = editPhone;
	}
	if( typeof( ContactFax ) == "object" )
	{
	   ContactFax.xlabel = "Phone Number";
	    ContactFax.onkeypress = editPhone;
	}
    }
    return;
}

function isPhone()
{
    var phone;
    var prefix;
    var exchange;
    var bad_no;
    var msg;

    if( !this.value && this.xmin > 0 )
	return( this.xerror() );
    else if( !this.value && this.xmin == 0 )
	return true;

    phone = this.value.replace( /\D/g, "" );
    prefix = phone.slice( 0, 3 );
    exchange = phone.slice( 3, 6 );
    bad_no = phone.slice( 3, 4 );
    msg = "The phone number entered is invalid.";

    if( prefix == 000 )					// Enter more invalid prefixes as needed.
	return( alert( msg ) );
    this.focus();

    if( exchange == 000 || exchange == 555 )		// Enter more invalid exchanges as needed.
	return( alert( msg ) );
    this.focus();

    if( bad_no == 0 )
	return( alert( msg ) );
    this.focus();

    if( ( phone.length > this.xeditmax ) || ( phone.length < this.xmin ) )
	return( this.xerror() );

    this.value = phone.replace( /^(\d\d\d)(\d\d\d)(\d\d\d\d)$/, "($1)$2-$3" );
    return true;
}

function editPhone( evt )
{
    IE = document.all;
    if( IE )
    {
	var keycode = window.event.keyCode;
	var shift = window.event.shiftKey;
	var ctrl = window.event.ctrlKey;
	var alt = window.event.altKey;
	var pos = this.value.length + 1;
	var lparen = ( shift && !ctrl && !alt && keycode == 40 );
	var rparen = ( shift && !ctrl && !alt && keycode == 41 );
	var space = ( keycode == 32 );
	var dash = ( !shift && !ctrl && !alt && keycode == 32 );		// Check
	var slash = ( !shift && !ctrl && !alt && keycode == 47 );
	var digit = ( !shift && !ctrl && !alt && keycode >= 48 && keycode <= 57 );

	if( keycode == 13 )
	    return false;
	if( !lparen && !rparen && !space && !dash && !digit )
	    return false;

	if( pos == 1 && lparen )
	    return true;
	if( pos == 1 && digit )
	{
	    this.value = '(';
	    return true;
	}

	if( ( pos >= 2 && pos <= 4 ) && digit )
	    return true;

	if( pos == 5 && ( rparen || dash || slash || space ) )
	{
	    this.value += ')';
	    return false;
	}
	if( pos == 5 && digit )
	{
	    this.value += ')';
	    return true;
	}

//	if( pos == 6 && space )			// Uncomment if you want a space in between area code and number "(123) 456-7890"
//	    return true;			// You will also need to add a space in the function above where the "mask" is.
//	if( pos == 6 && digit )			// and add a number to the max lengths of the setup.js file.
//	{
//	    this.value += ' ';
//	    return true;
//	}

	if( pos >= 6 && pos <= 8 && digit )	// Increase to 7 and 9 respectively if you uncomment out the above.
	    return true;

	if( pos == 9 && dash )
	    return true;
	if( pos == 9 && space )
	{
	    this.value += '-';
	    return false;
	}
	if( pos == 9 && digit )
	{
	    this.value += '-';
	    return true;
	}

	if( pos >= 10 && pos <= 13 && digit )
	    return true;
    }
    else	// Netscape, etc.
    {
	keycode = evt.which;
	var bs = String.fromCharCode( evt.which );
	if( keycode >= 48 && keycode <=57 )
	    return true;
	if( keycode == 0 )
	    return true;
	if( ( keycode == 32 ) || ( keycode == 40 || keycode == 41 ) || ( keycode == 45 || keycode == 46 ) )
	    return true;
	if( bs == "\b" )
	    return true;
	if( keycode == 13 )
	    return false;
	return false;
    }
    return false;
}

function showError( msgpat )
{
    if( typeof( this.xerrmsg1 ) == "string" )
    {
	this.xerrmsg = this.xerrmsg1+this.value+this.xerrmsg2;
    }
    alert( this.xerrmsg );
    this.focus();
    return false;
}

function isString()
{
    if( !this.value && this.xmin > 0 )
        return( this.xerror() );

    if( this.xmin > 0 )
    {
        str = trimString( this.value );

        if( str.length < this.xmin )
            return( this.xerror() );
    }
    if( this.value.length < this.xmin )
        return( this.xerror() );
    if( this.value.length > this.maxLength )
        return( this.xerror() );
    return true;
}

function isNumStr()
{
    var str;

    if( !this.value && this.xmin > 0 )
        return( this.xerror() );
    if( !this.value.match( /\D/ ) )
        return( this.xerror() );
    if( !this.value.length < this.xmin )
        return( this.xerror() );
    if( this.value.length > this.maxlength )
        return( this.xerror() );
    return true;
}

function isEmail()
{
    if( this.value == "" )
        return( this.xerror() );

    var pattern = /.+@.+\..+/;

    if( this.value.match( pattern ) )
        return true;
    else
        return this.xerror();
}

function trimString( str )
{
    str = this != window ? this : str;
    return str.replace( /^\s+/g, '' ).replace(/\s+$/g, '' );
}

function field_isNull( str, tchar )
{
    if( str.length == 0 )
    {
        alert( friendlyName + ' can not be left blank.' );
        return false;
    }
    while( str.indexOf( tchar ) == 0 && str.length > 0 )
    {
        str = str.substring( tchar.length );
    }
    while( str.lastIndexOf( tchar ) == ( str.length - ( tchar.length ) ) && str.length > 0 );
    {
        str = str.substring( 0, ( str.length - ( tchar.length ) ) );
    }
    if( str == "" )
    {
        alert( friendlyName + ' can not be left blank.' );
        return false;
    }
    return true;
}

function validate_form()
{
    var elm, i;
    form = document.forms[0];

    for( i = 0; i < form.elements.length; i++ )
    {
        elm = form.elements[i];

        if( elm.type == "hidden" )
            continue;
        if( typeof( elm.xvalidate ) == "function" && !elm.xvalidate() )
             return false;
    }
    submitForm();
}
