function toggleVisibility(myId){
    var myObject = document.getElementById(myId);
    if(myObject.style){
        if(myObject.style.display == 'block'){
            myObject.style.display = 'none';
        }
        else{
            myObject.style.display = 'block';
        }
    }
}

function submitLoginWindow(){
    if(validateForm('loginwindow')){
        document.forms['loginwindow'].submit();
    }
}

// function to validate any given form
function validateForm(myForm){
    var isValid = true;
    var requiredTextFields = Array();
    var requiredSelectFields = Array();

    // make sure required text fields are filled
    switch(myForm){
        case 'loginwindow' : requiredTextFields = Array('username','password'); break;
        case 'contactform' : requiredTextFields = Array('fromAddress','subject','message'); break;
    }
    for(myTextField in requiredTextFields){
        if(field_is_empty(myForm,requiredTextFields[myTextField])){
            try{document.getElementById(requiredTextFields[myTextField]).style.backgroundColor = "yellow";}
                catch(e){}
            try{document.getElementById(requiredTextFields[myTextField]).parentNode.style.color = "red";}
                catch(e){}
            isValid = false;
        }
        else{
            try{document.getElementById(requiredTextFields[myTextField]).style.backgroundColor = document.getElementById(requiredTextFields[myTextField]).parentNode.style.backgroundColor;}
                catch(e){document.getElementById(requiredTextFields[myTextField]).parentNode.style.backgroundColor = document.getElementById(requiredTextFields[myTextField]).parentNode.parentNode.style.backgroundColor;}
            try{document.getElementById(requiredTextFields[myTextField]).parentNode.style.color = document.getElementById(requiredTextFields[myTextField]).parentNode.parentNode.style.color;}
                catch(e){document.getElementById(requiredTextFields[myTextField]).parentNode.style.color = document.getElementById(requiredTextFields[myTextField]).parentNode.parentNode.style.color;}
        }
    }
 // make sure select dropdown fields are not set to default positions
    switch(myForm){
        case 'loginwindow' : requiredSelectFields = Array(); break;
        case 'contactform' : requiredTextFields = Array('contact'); break;
    }
    for(mySelectField in requiredSelectFields){
        switch(mySelectField){
            case 'contact' : var defaultValue = 0; break;
            default : var defaultValue = 0; break;
        }
        if(dropdown_has_default_value(myForm,mySelectField,defaultValue)){
            isValid = false;
        }
        else{
            try{document.getElementById(requiredSelectFields[mySelectField]).style.backgroundColor = document.getElementById(requiredSelectFields[mySelectField]).parentNode.style.backgroundColor;}
                catch(e){document.getElementById(requiredSelectFields[mySelectField]).parentNode.style.backgroundColor = document.getElementById(requiredSelectFields[mySelectField]).parentNode.parentNode.style.backgroundColor;}
            try{document.getElementById(requiredSelectFields[mySelectField]).parentNode.style.color = document.getElementById(requiredSelectFields[mySelectField]).parentNode.parentNode.style.color;}
                catch(e){document.getElementById(requiredSelectFields[mySelectField]).parentNode.parentNode.style.color = document.getElementById(requiredSelectFields[mySelectField]).parentNode.parentNode.parentNode.style.color;}
        }
    }
    if(!isValid)
        alert("Some required information is missing.\nPlease add the missing information.");
    return isValid;
} // END ValidateForm

function field_is_empty(form,textfield){  // Make sure that a required field is filled in.
    if(document.forms[form][textfield].value.length == 0) {
        document.forms[form][textfield].focus();
        document.forms[form][textfield].select();
        return true;
    }
    return false;
} // End field_is_empty()

function dropdown_has_default_value(myForm,myId,myDefaultValue){
    var myElement = document.forms[myForm][myId];
    if(myElement.options[myElement.selectedIndex].value == myDefaultValue){
        myElement.style.backgroundColor = "yellow";
        myElement.parentNode.parentNode.style.color = "red";
        return true;
    }
    else{
        try{ myElement.style.backgroundColor = myElement.parentNode.style.backgroundColor; }
            catch(e){ myElement.style.backgroundColor = '#FFFFFF'; }
        try{ myElement.parentNode.parentNode.style.color = myElement.parentNode.parentNode.parentNode.style.color; }
            catch(e){ myElement.parentNode.parentNode.style.color = '#000000'; }
        return false;
    }
} // END dropdown_has_default_value
