/*
    Simple object to hold a key/value pair.
    Used by the map class

    KeyPart:    string/int key value
    ValuePart:  string/int value value
*/
function KeyValuePair(KeyPart,ValuePart){

    /*
        Assign the supplied parameters
        to the variables.
    */
    this.ThisKey = KeyPart;
    this.ThisValue = ValuePart;

}

/*
    Emulates an associative array by placing
    key/value pairs in an array of KeyValuePair objects
    It might be slow, but I want my associative arrays, or
    at least something that resembles them somewhat.  :o)
*/
function map(){

    /*
        Create the map array. This array will contain
        a bunch of KeyValuePair objects.
    */
    var map = [];

    /*
        Return ThisValue of the KeyValuePair object
        KeyPart matches KeyValuePair.ThisKey

        KeyPart:    string/int key value
    */
    this.get = function(KeyPart){

        for(i = 0; i < map.length; i++){

            if(map[i].ThisKey == KeyPart){

                return map[i].ThisValue;

            }

        }

    }

    /*
        Either set a new key/value pair, or
        update an existing one.
    */
    this.set = function(KeyPart,ValuePart){

        var Isset = false;

        for(i = 0; i < map.length; i++){

            if(map[i].ThisKey == KeyPart){

                map[i].ThisValue = ValuePart;
                Isset = true;
                break;

            }

        }

        /*
            No current key/value pair found, so lets
            create a new KeyValuePair object and insert
            it into the map array.
        */
        if(!Isset){

            var NewKeyValueObj = new KeyValuePair(KeyPart,ValuePart);
            map.push(NewKeyValueObj);

        }

    }

    /*
        A simple isset function. Return TRUE if key
        is set, and FALSE if not.
    */
    this.isset = function(KeyPart){

        for(i = 0; i < map.length; i++){

            if(map[i].ThisKey == KeyPart){

                return true;
                break;

            }

        }

        return false;

    }

    /*
        Delete KeyValuePair object from map array.
    */
    this.unset = function(KeyPart){

        for(i = 0; i < map.length; i++){

            if(map[i].ThisKey == KeyPart){

                /**
                    Remove the current index in the map array.
                    Remember that this re-indexes the entire array!
                */
                var trash = map.splice(i,1);
                break;

            }

        }

    }

    /*
        Clear the entire map array.
    */
    this.empty = function(){

        map = [];

    }

    /*
        Return length of the map array.
    */
    this.length = function(){

        return map.length;

    }

    /*
        Function to return the KeyValuePair object key
        of a specific numeric index in the map array.
    */
    this.rawKey = function(NumKey){

        if(NumKey < map.length){
            return map[NumKey].ThisKey;
        }

    }

    /*
        Function to return the KeyValuePair object value
        of a specific numeric index in the map array.
    */
    this.rawValue = function(NumKey){

        if(NumKey < map.length){
            return map[NumKey].ThisValue;
        }

    }

}

/*
    Login function.
*/
function LogIn(){

    var username = document.getElementById("main_visible_Username").value;
    var password = document.getElementById("main_visible_Password").value;
    
    if(username != "" && password != ""){
        
        document.getElementById("user_Username").value = username;
        document.getElementById("user_Password").value = password;

        if(document.getElementById("visible_StayLoggedIn").checked == true){

            document.getElementById("user_StayLoggedIn").value = 1;   

        }else{

            document.getElementById("user_StayLoggedIn").value = 0; 

        }
        
        document.getElementById("user_LogInForm").submit();

    }else{

        alert("Please enter both username and password.");

    }

}

/*
    Logout function
*/
function LogOut(){

    document.getElementById("user_LogOutForm").submit();

}

/*
    Clear whitespace in textarea tags.
    We do this because the XSLT processor refuse to leave our
    textarea elements alone when they are empty. This causes
    the processor to close them XML style, like this <textarea/>,
    which obviously does not work.
*/
function ClearTextareaWhitespace(){

    var elems = document.getElementsByTagName("textarea");

    for(i = 0; i < elems.length; i++){
        
        var content = elems[i].value;
        if(content == " "){

            elems[i].value = "";

        }

    }

}

/*
    Create a map object to hold a few variables.
*/
MAP = new map();