var browserName = navigator.appName;
var browserVer = parseInt(navigator.appVersion);

/*  function setMenuAt(theItem)
    This function takes the passed id that refers to an item in the main menu 
        and changes its style to be 'menuItemAt' so that it is properly displayed.
    Inputs:
       theItem - the id of the item to be changed.
    Return Value:
        None
   1-18-2005 DV
*/
function setMenuAt(theItem){
    if (browserName == "Microsoft Internet Explorer"){
        document.all[theItem].className = "menuItemAt";
    } else {     
        document.getElementById(theItem).className = "menuItemAt";
    }
}

function logout(){
    alert("loggin out");    
}

// function to write the passed in text to the given span on the template
function writeInnerHtml(newHtml, spanName){
    if (browserName == "Microsoft Internet Explorer"){
        document.all[spanName].innerHTML = newHtml;
    } else {     
        document.getElementById(spanName).innerHTML = newHtml;
    }
}

/*  Function to open a centered window and pass it the passed in image name to display.
*/
function openCenteredWindow(imgName, imageWidth, imageHeight){
    var width = imageWidth + 60;
    var height = imageHeight + 60;
    var left = parseInt((screen.availWidth/2) - (width/2));
    var top = parseInt((screen.availHeight/2) - (height/2));
    var windowFeatures = "width=" + width + ",height=" + height + ",status,resizable,scrollbars,left=" + left + ",top=" + top;
    window.open("sampleViewer.html?" + imgName, "subWind", windowFeatures);
}


/* **********************************************************************************
                            NOT USED YET
********************************************************************************** */





/*  Function to clear all form fields of entries. Using a function to do this vs a normal reset button
    because some of the entries are pre-populated by the asp and wouldn't be cleared by a reset button.
*/
function clearAll(currentForm){
    for (i=0; i<currentForm.length; i++) {
        var elem = currentForm.elements[i];
        if (elem.type == "hidden" || elem.type == "button" || elem.type == "reset" || elem.type == "submit"){
            // ignore these fields
        } else {
            elem.value = "";
        }
    } // end loop through all form elements
    if (currentForm.quantity){
        currentForm.quantity.selectedIndex = 0;
    }
}

// Function to show the passed in element name
function showElement(elemName){
    if (browserName == "Microsoft Internet Explorer"){
        document.all[elemName].style.display = "";
    } else {     
        document.getElementById(elemName).style.display = "";
    }
}

// Function to hide the passed in element name
function hideElement(elemName){
    if (browserName == "Microsoft Internet Explorer"){
        document.all[elemName].style.display = "none";
    } else {     
        document.getElementById(elemName).style.display = "none";
    }
}

// function to get the inner HTML of the passed in element
function getInnerHtml(elemName){
    if (browserName == "Microsoft Internet Explorer"){
        return document.all[elemName].innerHTML;
    } else {     
        return document.getElementById(elemName).innerHTML;
    }
}



// function checks text entries to make sure they are not all whitespace characters
function isBlank(tVal){
    for (var i = 0; i < tVal.length; i++){
        var c = tVal.charAt(i);
        if((c != ' ') && (c != '\n') && (c != '\t'))
            return false;
    }
    return true;
}

/* function to determine if a passed in date is properly formatted (according to 
    the defined reg exp) and if it is a valid date. 
    Returns: 0 for improper format
             1 for a valid date
             2 for improper number of days in the month, or improper month
             3 for improper month format (ie a zero in front)
             4 for improper date format (ie a zero in front)
*/
function isValidDate(dateValue){
    var monthLength = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
    var dateFormatRegEx = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
    var isValid = 1;
    
    if (!dateFormatRegEx.test(dateValue)){
        isValid = 0;
    }
    
    var slash = dateValue.indexOf("/");         // slash after the month
    var slash2 = dateValue.indexOf("/", slash + 1); // slash after the date
    
    if (isValid == 1){
        if (dateValue.substr(0, 1) == "0"){
            isValid = 3;
        } else if(dateValue.substr(slash + 1, 1) == "0"){
            isValid = 4;
        }
    }
    
    if (isValid == 1){
        // parse out the month, day, and year entered
        var userMonth = parseInt(dateValue.substring(0,slash), 10) - 1;
        var userDay = parseInt(dateValue.substring(slash + 1, slash2));
        var userYear = parseInt(dateValue.substring(slash2 + 1));
        var yearString = String(userYear);
                
        // check for leap year
        if (userYear/4 == parseInt(userYear/4)){ // could be a leap year
            if ((yearString.substr(2) != "00") || (userYear/400 == parseInt(userYear/400))){
                monthLength[1] = 29;
            }
        }
        
        // check for correct number of days in month
        if (userDay > monthLength[userMonth]){
            isValid = 2;
        }
        
        // check for valid month
        if (userMonth < 0 || userMonth > 11){
            isValid = 2;
        }
    }
    
    return(isValid);
}

/*  function formatCurrency(textCurrency)
    This function takes the passed in text and formats it to currency. It uses US dollars and 
        returns a string with the dollar sign and 2 decimal places. It will insert commas where 
        appropriate.
    Inputs:
       textCurrency - A sting representing the currency value to be returned.
    Return Value:
        -1 - if there was an error in converting the passed in text. Otherwise it returns the 
            value converted to a currency format.
   8-23-2004 DV
*/
function formatCurrency(textCurrency){
    var tempValue = parseFloat(textCurrency);
    var retVal;
    if (isNaN(tempValue)){
        retVal = -1;
    } else {        
        tempValue = tempValue.toFixed(2);
        // now add the commas
        var dollarsLength = tempValue.length - 3;
        var decIndex = dollarsLength;
        
        if (dollarsLength <= 3){
            retVal = "$" + tempValue;
        } else {
            var formattedDollars = insertCurrencyComma(tempValue.substring(0, decIndex));
            retVal = "$" + formattedDollars + tempValue.substring(decIndex);
        }
        
    }        
    return retVal;
}

/*  function insertCurrencyComma(dollarValue)
    This function takes the passed in dollar value (left of the decimal) and adds commas to it 
        in the appropriate places. ie every third digit from the left of the decimal.
    Inputs:
       dollarValue - A sting representing the dollar value (left of the decimal) to have commas added to
    Return Value:
        The passed in dollar value with commas added at the appropriate places
   8-23-2004 DV
*/
function insertCurrencyComma(dollarValue){
    var dollarLength = dollarValue.length
    if (dollarLength >= 4 && dollarLength <= 6){
        var endString = dollarValue.substring(dollarLength - 3);
        var startString = dollarValue.substring(0, dollarLength - 3);
        return startString + "," + endString;
    } else {
        var newDollarValue = dollarValue.substring(0, dollarLength - 3);
        var tempStart = insertComma(newDollarValue);
        return tempStart + "," + dollarValue.substring(dollarLength - 3);
    }
}