/*************************
// Functions that don't belong in any one class, or 
// that will be used all over go in this file.
*************************/

/*
    A series of simple functions to test the browser so we can catch specific bugs in browsers
*/
function isWindows()
{   return (navigator.userAgent.toLowerCase().indexOf("windows") > -1) ? true : false;    }
function isNotWindows()
{   return !(isWindows());    }
function isMacintosh()
{   return (navigator.userAgent.toLowerCase().indexOf("macintosh") > -1) ? true : false;    }
function isNotMacintosh()
{   return !(isMacintosh());    }
function isLinux()
{   return (navigator.userAgent.toLowerCase().indexOf("linux") > -1) ? true : false;    }
function isNotLinux()
{   return !(isLinux());    }

function isExplorer()
{   return (navigator.userAgent.toLowerCase().indexOf("opera") < 0 && navigator.userAgent.toLowerCase().indexOf("msie") > -1) ? true : false;    }
function isNotExplorer()
{   return !(isExplorer());    }
function isNetscape()
{   return (navigator.userAgent.toLowerCase().indexOf("netscape") > -1) ? true : false;    }
function isNotNetscape()
{   return !(isNetscape());    }
function isMozilla()
{   return (navigator.userAgent.toLowerCase().indexOf("khtml") < 0 && navigator.userAgent.toLowerCase().indexOf("gecko") > -1) ? true : false;    }
function isNotMozilla()
{   return !(isMozilla());    }
function isSafari()
{   return (navigator.userAgent.toLowerCase().indexOf("safari") > -1) ? true : false;    }
function isNotSafari()
{   return !(isSafari());    }
function isOpera()
{   return (navigator.userAgent.toLowerCase().indexOf("opera") > -1) ? true : false;    }
function isNotOpera()
{   return !(isOpera());    }

function isExplorer55Up()
{   return (isExplorer() && (navigator.userAgent.toLowerCase().indexOf("msie 5.5") > -1 || navigator.userAgent.toLowerCase().indexOf("msie 6") > -1)) ? true : false;    }
function isNotExplorer55Up()
{   return !(isExplorer55Up());    }
function isExplorer6Up()
{   return (isExplorer() && (navigator.userAgent.toLowerCase().indexOf("msie 6") > -1)) ? true : false;    }
function isNotExplorer6Up()
{   return !(isExplorer6Up());    }
function isNetscape6Up()
{   return (navigator.userAgent.toLowerCase().indexOf("netscape/6") > -1 || navigator.userAgent.toLowerCase().indexOf("netscape/7") > -1) ? true : false;    }
function isNotNetscape6Up()
{   return !(isNetscape6Up());    }
function isNetscape7Up()
{   return (navigator.userAgent.toLowerCase().indexOf("netscape/7") > -1) ? true : false;    }
function isNotNetscape7Up()
{   return !(isNetscape7Up());    }
function isSafari1Up()
{   return (navigator.userAgent.toLowerCase().indexOf("safari/1") > -1) ? true : false;    }
function isNotSafari1Up()
{   return !(isSafari1Up());    }
function isSafari112Up()
{   alert("HERE\n" + navigator.userAgent.toLowerCase());return (navigator.userAgent.toLowerCase().indexOf("safari/12") > -1) ? true : false;    }
function isNotSafari12Up()
{   return !(isSafari12Up());    }
function isMozilla15Up()
{   return (isMozilla() && (navigator.userAgent.toLowerCase().indexOf("rv:1.5") > -1 || navigator.userAgent.toLowerCase().indexOf("rv:1.6") > -1 || navigator.userAgent.toLowerCase().indexOf("rv:1.7") > -1 || navigator.userAgent.toLowerCase().indexOf("rv:1.8") > -1) ) ? true : false;    }
function isNotMozilla15Up()
{   return !(isMozilla15Up());    }
function isMozilla16Up()
{   return (isMozilla() && (navigator.userAgent.toLowerCase().indexOf("rv:1.6") > -1 || navigator.userAgent.toLowerCase().indexOf("rv:1.7") > -1 || navigator.userAgent.toLowerCase().indexOf("rv:1.8") > -1) ) ? true : false;    }
function isNotMozilla16Up()
{   return !(isMozilla16Up());    }
function isMozilla17Up()
{   return (isMozilla() && (navigator.userAgent.toLowerCase().indexOf("rv:1.7") > -1 || navigator.userAgent.toLowerCase().indexOf("rv:1.8") > -1) ) ? true : false;    }
function isNotMozilla17Up()
{   return !(isMozilla17Up());    }


/*
// isSet takes a variable and makes sure it isn't empty.
// returns true on success/ false on failure.
*/
function isSet(value)
{   return (value == null || value == "" || typeof value == 'undefined' || (typeof value == "object" && (value.length) && value.length == 0) || value == "false") ? false : true;   }
/*
// isNotSet takes a variable and makes sure it is empty.
// returns true on success/ false on failure.
*/
function isNotSet(value)
{   return !isSet(value);    }


/*
// isDom takes elem and makes sure it is part of the DOM.
// returns true on success/ false on failure.
*/
function isDom(elem)
{   return (elem && typeof elem == "object") ? true : false;    }
/*
// isDom takes elem and makes sure it is not part of the DOM.
// returns true on success/ false on failure.
*/
function isNotDom(elem)
{   return !isDom(elem);    }

/*
    This function takes a field and gets the USER CHOSEN / CREATED value from it.  
    returns
        null - nothing selected/written or failure
        value - can be an array of values, if you have multiples of a field coming in.
*/
function getFieldValue(theField)
{
    var value = null;
    if (isDom(theField))
    {
       //if it is not set, we have an array of the same name... check the first one to see what is up
        if (theField.length && theField.length > 0 && isSet(theField[0].type))
        {
            value = new Array();
            for (var i = 0; i < theField.length; i++)
            {
                var field = theField[i];
                var type = field.type;
                switch (type)
                {
                    case "select-one" :
                        if (theField.selectedIndex >= 0)
                        {    value[value.length] = field.options[field.selectedIndex].value;    }
                    break;
                    case "select-multiple" :
                        temp = new Array();
                        for (var i = 0; i < field.options.length; i++)
                        {   if (field.options[i].selected == true) {   temp[temp.length] = field.options[i].value;    }    }
                        if (temp.length > 0)
                        {   value[value.length] = temp;    }
                    break;
                    case "radio" :
                        for (var i = 0; i < theField.length; i++)
                        {   if (theField[i].checked == true) {   value = theField[i].value;    }    }
                    break;
                    case "checkbox" :
                        if (field.checked == true)
                        {   value[value.length] = field.value;    } 
                    break;
                    case "fileupload" :
                        //Currently you cannot set this for security reasons
                    break;
                    case "hidden" :
                    case "textarea" :
                    case "password" :
                    case "button" :
                    case "submit" :
                    case "reset" :
                    case "text" :
                        if (isSet(field.value))
                        {   value[value.length] = field.value;    }
                    break;
                }//end type switch
            }//end field for loop
            value = (value.length > 0) ? value : null;
        }
        else
        {
            var type = theField.type;
            switch (type)
            {
                case "select-one" :
                    if (theField.selectedIndex >= 0)
                    {   value = theField.options[theField.selectedIndex].value;    }
                break;
                case "select-multiple" :
                    value = new Array();
                    for (var i = 0; i < theField.options.length; i++)
                    {   if (theField.options[i].selected == true) {   value[value.length] = theField.options[i].value;    }    }
                    value = (value.length > 0) ? value : null;
                break;
                case "radio" :
                case "checkbox" :
                    if (theField.checked == true)
                    {   value = theField.value;    } 
                break;
                case "fileupload" :
                    //Currently you cannot set this for security reasons
                break;
                case "hidden" :
                case "textarea" :
                case "password" :
                case "button" :
                case "submit" :
                case "reset" :
                case "text" :
                    value = theField.value;
                break;
            }//end type switch
        }
    }//end isDom(theField)
    
   //We do this because if there is only one value coming back we just want the value, not an array.
    return (isSet(value) && value.length && value.length == 1) ? value[0] : value;
}

/*
// This function takes a value and sets a form field with it.  
// Why a seperate one you ask?  Because sometimes you switch what type of 
// element it is, and this takes care of the ugly details for you.
    TODO:
        should be able to pass in either a value array, or a field array, or both
        value array / single field - all the values go to the field (overriding previous values if possible)
        single value / field array - each field gets the same value
        value array / field array - a 1 to 1 match of values to fields occurs EX field[0] = value[0]
*/
function setFieldValue(theValue, theField)
{
    if (isDom(theField))
    {
       //if it is not set, we have an array of the same name... check the first one to see what is up
        var type = (theField.length && theField.length > 0 && isSet(theField[0].type)) ? theField[0].type : theField.type;
        switch (type)
        {
            case "select-one" :
            case "select-multiple" :
                for (var i = 0; i < theField.options.length; i++)
                {   if (theField.options[i].value == theValue) {   theField.selectedIndex = i;    }    }
            break;
            case "checkbox" :
            case "radio" :
                if (theField.length && theField.length > 1)
                {
                    for (var i = 0; i < theField.length; i++)
                    {   if (theField[i].value == theValue) {   theField[i].checked = true;    }    }
                }
                else
                {   theField.value = theValue;    }
            break;
            case "fileupload" :
                //Currently you cannot set this for security reasons
            break;
            case "hidden" :
            case "textarea" :
            case "password" :
            case "button" :
            case "submit" :
            case "reset" :
            case "text" :
                theField.value = theValue;
            break;
        }
    }
    return false;
}

