/*******************************************************************************
* Miscellaneous.js
* 
* @package bondurant.com
* @author prestonm3@mcmurry.com
* @version 1.0
* @copyright (C) Copyright 2008 by McMurry, Inc.
*******************************************************************************/

/***************************************************************************
* trimall()
*
* Only trim elements which may contain text. Trimming other elements may
* cause undesirable side effects that take many wasted hours to figure out.
* (For instance, trimming a multiple select control, lops off all but the
* first selected row.)
*
* These are javascript elements:
*
*    button          =
*    checkbox        =
*    file            =
*    hidden          =
*    image           =
*    password        = trim
*    radio           =
*    reset           =
*    select-one      =
*    select-multiple =
*    submit          =
*    text            = trim
*    textarea        = trim
***************************************************************************/
function trimall() {
    var el;
 
//alert( "element count = ~" + document.Maindocument.MainForm.elements.length + "~" );   
    for ( el=0; el<document.MainForm.elements.length; el++ ) {
//alert( "element #" + el + " = ~" + document.Maindocument.MainForm.elements[el].name + " (type: " + document.Maindocument.MainForm.elements[el].type + " )~" );  
//if ( el == 5 ) {
//break;
//}

        if ( ( document.MainForm.elements[el].type == "password" )
        ||   ( document.MainForm.elements[el].type == "text" ) 
        ||   ( document.MainForm.elements[el].type == "textarea" ) ) {
            document.MainForm.elements[el].value = trim( document.MainForm.elements[el].value );
        }
    }
}

/***************************************************************************
* trim()
*
* @param var trimMe
* @return var str
***************************************************************************/
function trim( trimMe )
{
    str = new String( trimMe );
   
    return str.replace( /^\s*|\s*$/g, "" );
}

/***************************************************************************
* IsValidEMail()
*
* @param object ctrl
* @return boolean True if the e-mail is valid, otherwise false
***************************************************************************/
function IsValidEMail( ctrl ) {
 
    var strEMail = new String( ctrl.value );
   
    var expEMail = new RegExp( "^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+", "i" );
 
    var RetVal = expEMail.test( strEMail );
   
    return( RetVal );
}

/***************************************************************************
* isInt()
*
* @param var str
* @param boolean ignoreBlank The value may true, false or non-existant
* @return bool true/false
***************************************************************************/
function isInt() {
 
    str = arguments[0];
 
    /*
    ** If a second argument is not passed in, default it to false. (Treat
    ** blank as "not an integer".
    */
    if ( !(ignoreBlank = arguments[1]) ) {
        ignoreBlank = false;
    }
 
    if ( ignoreBlank
    &&   str == "" ) {
        /*
        ** String is blank/empty string and should be treated as an integer.
        */
        return true;
    }
 
    var i = parseInt (str);
   
    if (isNaN (i))
        return false;
   
    i = i . toString ();
 
    if (i != str)
        return false;
   
    return true;
}
        
