//	--------------------------------------------------------------
//	Mahnken Enterprises Inc
//	All Rights Reserved
//	Aug 2, 2005
//	--------------------------------------------------------------

//  --------------------------------------------------------------
//  FILE:   forms.js
//
//  Form validation and manipulation tools
//
//  FUNCTIONS:
//      validateEmail(fieldId, szError, bSelect, szColor)
//      validateValue(fieldId, min, max, bInt, szError, bSelect, szColor)
//      validateLength(fieldId, min, max, szError, bSelect, szColor)
//      validateSelect(fieldId, szError, bSelect, szColor)
//      validateText(fieldId, szError, bSelect, szColor)
//  --------------------------------------------------------------

szColor = '#FF8';

// ===============================================================
//  FUNCTION: elementCheck()
//
//  given a full path to a field, or a field name, return the field id
//
//      id - document.formname.fieldname OR fieldname
// ---------------------------------------------------------------
function elementCheck( id )
{
    if (typeof(id) != "object")
    {
        return document.getElementById(id);
    }
    else
    {
        return id;
    }
}

// ---------------------------------------------------------------------------
//  JAVASCRIPT FUNCTION: ValidateClear()
//      ARGS: fieldId
// ---------------------------------------------------------------------------
function validateClear(fieldId)
{
	var fid = elementCheck(fieldId);
	fid.style.backgroundColor = '';
	return true;
}

// ===============================================================
//  FUNCTION: validateEmail()
//
//  Validate that an email field is possibly a real address
//
//      fieldID - document.formname.fieldname
//      szError - "You messed up, please fix it"
//      bSelect - On error, reselect the field
//      szColor - On error, set the color of the field to this "#FF0"
// ---------------------------------------------------------------
function validateEmail(fieldId, szError, bSelect)
{
    var fid = elementCheck(fieldId);
    var sz = fid.value;

        // Verify that it's possibly an email address
    var pattern = /^.+@.+\..{2,4}$/;
    var b = pattern.test(sz);

    if (b)
    {
        fid.style.backgroundColor = '';
        return true;
    }

        // b was false, so pop the error if it was passed
    if (szError) { alert (szError); }

        // set the background color if it was passed
    if ( szColor.length > 3 ) { fid.style.backgroundColor = szColor; }

        // Select the field if asked
    if ( bSelect ) { fid.focus(); }

        // Finally, let the caller know we failed
    return false;
}

// ---------------------------------------------------------------------------
//  JAVASCRIPT FUNCTION: validateSame()
//      ARGS: thisField, thatField, szError, bSelect
// ---------------------------------------------------------------------------
function validateSame(thisField, thatField, szError, bSelect)
{
	var fid = elementCheck(thisField);
	var oid = elementCheck(thatField);

    var sz_f = fid.value.toLowerCase();
    var sz_o = oid.value.toLowerCase();

	//if (fid.value == oid.value)
	if (sz_f == sz_o)
    {
        fid.style.backgroundColor = '';
        return true;
    }

	    // Error, so pop the error if it was passed
    if (szError) { alert (szError); }

        // set the background color if it was passed
    if ( szColor.length > 3 ) { fid.style.backgroundColor = szColor; }

        // Select the field if asked
    if ( bSelect ) { fid.focus(); }

        // Finally, let the caller know we failed
    return false;
}

// ===============================================================
//  FUNCTION: validateValue()
//
//  Validate that a field is between value A and value B
//
//      fieldID - document.formname.fieldname
//      min     - minimum value
//      max     - maximum value
//      bInt    - require an integer
//      szError - "You messed up, please fix it"
//      bSelect - On error, reselect the field
//      szColor - On error, set the color of the field to this "#FF0"
// ---------------------------------------------------------------
function validateValue(fieldId, min, max, bInt, szError, bSelect)
{
    var fid = elementCheck(fieldId);

        // if it isn't an int, pass to next test
        // if it is an int, check to see if it's int value and float value are equal
    if (!bInt || (parseInt(fid.value) == parseFloat(fid.value)))
    {
        if ( fid.value >= min && fid.value <= max )
        {
            fid.style.backgroundColor = '';
            return true;
        }
    }

        // Everything below here is a fail condition
    if (szError) { alert (szError); }

        // set the background color if it was passed
    if ( szColor.length > 3 ) { fid.style.backgroundColor = szColor; }

        // Select the field if asked
    if ( bSelect ) { fid.focus(); }

        // let the caller know we failed
    return false;
}

// ===============================================================
//  FUNCTION: validateLength()
//
//  Validate that an field has more than X and less than Y chars
//
//      fieldID - document.formname.fieldname
//      min     - minimum value
//      max     - maximum value
//      szError - "You messed up, please fix it"
//      bSelect - On error, reselect the field
//      szColor - On error, set the color of the field to this "#FF0"
// ---------------------------------------------------------------
function validateLength(fieldId, min, max, szError, bSelect)
{
    var fid = elementCheck(fieldId);
    var sz = fid.value;

    if (sz.length >= min && sz.length <= max)
    {
        fid.style.backgroundColor = '';
        return true;
    }

        // everything below here is an error state
    if (szError) { alert (szError); }

        // set the background color if it was passed
    if ( szColor.length > 3 ) { fid.style.backgroundColor = szColor; }

        // Select the field if asked
    if ( bSelect ) { fid.focus(); }

        // let the caller know we failed
    return false;
}


// ---------------------------------------------------------------------------
//  JAVASCRIPT FUNCTION: qv_select()
//      ARGS: eid = Element ID
//            szError - "error message" or blank to not pop an error
//            bSelect - Select the item, or let them tab out
//            szColor - Alert Color
// ---------------------------------------------------------------------------
function validateSelect(fieldId, szError, bSelect)
{
    fid = elementCheck(fieldId);
    sz = fid.options[fid.selectedIndex].value;

	if (sz == '' || sz == 'NULL' || sz == 'NONE' || sz == 'ERROR')
	{
        if (szColor.length > 3)
        {
            fid.style.backgroundColor = szColor;
            //fid.style.fontWeight = 'bold';
        }
        if (szError) { alert (szError); }
        if ( bSelect ) { fid.focus(); }

        return false;
    }
    else
    {
        fid.style.backgroundColor = '';
        //fid.style.fontWeight = '';
        return true;
    }
}

// ===============================================================
//  FUNCTION: validateText()
//
//  Validate that a field has text
//
//      fieldID - document.formname.fieldname
//      szError - "You messed up, please fix it"
//      bSelect - On error, reselect the field
//      szColor - On error, set the color of the field to this "#FF0"
// ---------------------------------------------------------------
function validateText(fieldId, szError, bSelect)
{
    var fid = elementCheck( fieldId );

    if ( fid.value != null && fid.value != '' )
    {
        fid.style.backgroundColor = '';
        return true;
    }

        // everything below here is an error state
    if (szError) { alert (szError); }

        // set the background color if it was passed
    if ( szColor.length > 3 ) { fid.style.backgroundColor = szColor; }

        // Select the field if asked
    if ( bSelect ) { fid.focus(); }

        // let the caller know we failed
    return false;
}

// =========================================================================
//  FUNCTION: validateVisible()
//
//  Validate a field only if it is visible
//
//      fieldId - document.formname.fieldname
//      szError - Optional - "Hey, you forgot field XYZ"
//      bSelect - On error, reselect the field (usually FALSE)
//      visId   - The ID of the DIV or other container that is either
//                visible or hidden. If it's not visible, just ignore this.
//      szType  - Type of check to perform
// -------------------------------------------------------------------------
function validateVisible( fieldId, szError, bSelect, visId, szType)
{
    var oid = elementCheck( visId );
    if ( oid.style.display == 'none')
    {
        return true;
    }

    var fid = elementCheck( fieldId );
    switch (szType)
    {
        case 'text':
            return validateText( fieldId, szError, bSelect );
            break;
        case 'select':
            return validateSelect( fieldId, szError, bSelect );
            break;
        default:
            return
    }
}

/*
    // Sample calls to the functions above

    // validate an email field, pop an error if it's bad,
    // reselect the field, and color it light yellow
validateEmail( document.f.f_email, "Please enter your email address, true, "#FFFFAA");

    // validate a number field, above 0, below 120, only integers,
    // no error message, don't select, color light green
validateValue(this, 0, 120, true, '', false, "#AAFFAA");

    // Validate a field for length, pop an error, don't select, don't color
validateLength(myTextField, 1, 28, "Please enter between 1 and 28 characters", false, '');

    // Validate a selection has been made, no error, no select, no color
validateSelect(this, '', false, '');

    // validate a text field has contents
validateText(document.f.f_text, '', false, '');

*/
