/*******************************************************************************
* Miscellaneous.js
*
* @package firstsolar
* @author prestonm3@mcmurry.com
* @version 1.0
* @copyright (C) Copyright 2007 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(form) {
    var el;

//alert( "element count = ~" + form.elements.length + "~" );
    for ( el = 0; el < form.length; el++ ) {
//alert( "element #" + el + " = ~" + form.elements[el].name + " (type: " + form.elements[el].type + " )~" );
//if ( el == 5 ) {
//break;
//}

        if ( ( form.elements[el].type == "password" )
        ||   ( form.elements[el].type == "text" )
        ||   ( form.elements[el].type == "textarea" ) ) {
            form.elements[el].value = trim( form.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
*
* Adapted from http://www.smartwebby.com/DHTML/email_validation.asp
***************************************************************************/
function IsValidEMail( ctrl ) {
    var str = ctrl.value;
    var bugchars = '!#$^&*()+|}{[]?><~%:;/,=`"\'';
    var idx;
    var lchar="";
    var at="@";
    var dot=".";
    var lat=str.indexOf(at);
    var lstr=str.length;
    var ldot=str.indexOf(dot);
    var lastdot=str.lastIndexOf(dot);

    if ( str.indexOf(at) == -1 ) {
       return false;
    }

    if ( str.indexOf(at) == 0 || str.indexOf(at) == lstr ) {
       return false;
    }

    if ( str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr || str.substring(lastdot+1) == "" ) {
        return false;
    }

     if ( str.indexOf(at,(lat+1)) != -1 ) {
        return false;
     }

     if ( str.substring(lat-1,lat) == dot || str.substring(lat+1,lat+2) == dot ) {
        return false;
     }

     if ( str.indexOf(dot,(lat+2)) == -1 ) {
        return false;
     }

     if ( str.indexOf(" ") != -1 ) {
        return false;
     }

    /*
    ** Search through string's characters one by one. If character is not in bag.
    */
    for (idx = 0; idx < str.length; idx++ ) {
        /*
        ** Check that current character isn't whitespace.
        */
        var char_to_check = str.charAt(idx);

        if ( idx > 0 ) {
            lchar = str.charAt( idx - 1 )
        };

        if ( bugchars.indexOf(char_to_check) != -1 || (lchar=="." && char_to_check==".") ) {
            return false;
        }
    }

    var arrEmail = str.split("@")
    var ldot = arrEmail[1].indexOf(".")

    if ( isInt( arrEmail[1].substring(ldot+1) ) == true ) {
        return false;
    }

    return true;
}

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

