var EMPTY=''
var whitespace = " \t\n\r";

function SPACE(iNumSpaces) {
    var sSpaces = EMPTY
    for (var iLoopCnt=0; iLoopCnt < iNumSpaces; iLoopCnt++) {
        sSpaces = sSpaces + ' ';
    }
    return sSpaces;
}

/* Input is string to be trimmed and return value is the result after trimming */
function TextTrim(sText_Value) {
    /* Strip leading spaces from input */
    if (sText_Value == '')
        return EMPTY;

    var iStart_Position = 0;
    var iEnd_Position = 0;

    while((sText_Value.charAt(iStart_Position) == SPACE(1)) && ( iStart_Position < sText_Value.length)) {
        iStart_Position++;
    }

    if (iStart_Position == sText_Value.length) {
        sText_Value = EMPTY;
    } else {
    /* If anything left of string after stripping leading spaces strip trailing */
    /* spaces as well */
        sText_Value = sText_Value.substring(iStart_Position,sText_Value.length);
        iEnd_Position=(sText_Value.length)-1;

        while(sText_Value.charAt(iEnd_Position) == SPACE(1)) {
            iEnd_Position--;
        }

         sText_Value = sText_Value.substring(0, iEnd_Position + 1);
    }

    return(sText_Value);
}

function isWhitespace (s) {
    var i;
    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++) {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
    }
    // All characters are whitespace.
    return true;
}

    // Email address must be of form a@b.c -- in other words:
    // * there must be at least one character before the @
    // * there must be at least one character before and after the .
    // * the characters @ and . are both required

function isEmail (s) {
    if (isEmpty(s)) return false;

    // is s whitespace?
    if (isWhitespace(s)) return false;

    // there must be >= 1 character before @, so we
    // start looking at character position 1
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@")) {
        i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != ".")) {
        i++
    }

    // there must be at least one character after the .
    if ((i >= sLength-1 ) || (s.charAt(i) != ".")) return false;
    else return true;
}

function isEmpty(s) {
    return ((s == null) || (s.length == 0))
}

function validateForm (str) {
    if(isEmail(str) == true) {
        return true;
    } else {
        alert('Please enter a valid email address');
        return false;
    }
}

var opener = null;

function readCookie(cookiename) {
    var num = document.cookie.length;
    var name = cookiename + "=";
    var len = name.length;
    var x = 0;
    while (x <= num) {
        var y = (x + len);
        if (document.cookie.substring(x,y) == name)
            return (extractCookieValue(y));
        x = document.cookie.indexOf(" ", x)+1;
        if (x == 0)
            break;
    }
    return null;
}

function extractCookieValue(val) {
    if ((endOfCookie = document.cookie.indexOf(";", val)) == -1 ) {
        endOfCookie = document.cookie.length;
    }
    return unescape(document.cookie.substring(val,endOfCookie));
}

//CHECK frmCIMLogin name hardcoded
function loadUp() {
    username = readCookie("website_username");
    password = readCookie("website_password");
    if ((username != null) && (password != null)) {
        document.frmLogin.username.value = username;
        document.frmLogin.password.value = password;
        document.frmLogin.remember.checked = true;
    }
    return(true);
}

function createCookie(name,value,days) {
    var expireday = new Date();
    expireday.setTime(expireday.valueOf() + 1000*3600*24*days);
    document.cookie = name + "=" + value + "; expires=" + expireday.toGMTString() + ";";
}

function doSubmit() {
    var focusField = ''
    var fatalErrorList='';
    username = document.frmLogin.username.value;
    password = document.frmLogin.password.value;
    if ((username != "") && (password != "")) {
        days = document.frmLogin.remember.checked ? 90 : -1;
        createCookie("website_username",username,days);
        createCookie("website_password",password,days);
        document.frmLogin.submit();
        return(true);
    } else {
        if (document.frmLogin.username.value == '') {
            fatalErrorList = fatalErrorList  + 'User Name\n'
            if (focusField == ''){focusField = "username"}
        }
        if (document.frmLogin.password.value == '') {
            fatalErrorList = fatalErrorList  + 'Password\n'
            if (focusField == ''){focusField = "password"}
        }
        alert('There was at least one problem in the form you submitted.\n\nPlease enter the following information and try again:\n' + fatalErrorList)
        if (eval("frmLogin." + focusField + ".focus()"))
            eval("frmLogin." + focusField + ".focus()")
        focusField = ''
        return false;
    }
}

function validateLogin() {
    var focusField = ''
    var fatalErrorList='';
    username = document.frmLogin.username.value;
    password = document.frmLogin.password.value;
    if (document.frmLogin.remember.checked == true) {
        if ((username != "") && (password != "")) {
            days = document.frmLogin.remember.checked ? 90 : -1;
            createCookie("website_username",username,days);
            createCookie("website_password",password,days);
            document.frmLogin.submit();
            return(true);
        } else {
            if (document.frmLogin.username.value == '') {
                fatalErrorList = fatalErrorList  + 'User Name\n'
                if (focusField == ''){focusField = "username"}
            }
            if (document.frmLogin.password.value == '') {
                fatalErrorList = fatalErrorList  + 'Password\n'
                if (focusField == ''){focusField = "password"}
            }
            alert('There was at least one problem in the form you submitted.\n\nPlease enter the following information and try again:\n' + fatalErrorList)
            if (eval("frmLogin." + focusField + ".focus()"))
                eval("frmLogin." + focusField + ".focus()")
            focusField = ''
            return false;
        }
    }
}
