    var http_request = false;
    var validation_error = false;
	var image_div = 1;
	var track_div = 1;
	var output_div = "arep";

    function HideDiv() {
        for(i=0; i<arguments.length; i++) {
    	    document.getElementById(arguments[i]).style.display = 'none';
    	    
        }
    }

    function ShowDiv() {
        for(i=0; i<arguments.length; i++) {
			if(document.getElementById(arguments[i]).rowIndex && !window.ActiveXObject){
			    document.getElementById(arguments[i]).style.display = 'table-row';
			} else {
	    	    document.getElementById(arguments[i]).style.display = 'inline';
		    }
        }
    }

    function swapElementDisplayState() {
        for(i=0; i<arguments.length; i++) {
    	    var el = document.getElementById(arguments[i]);
            if(el.style.display == 'none') {
    	        el.style.display = 'inline';
            } else {
    	        el.style.display = 'none';
            }
        }
    }

    function makeGetRequest(url, request) {

        http_request = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Error: Cannot create an XMLHTTP instance');
            return false;
        }

        if(request == "genre_styles") {
            var genre_select = document.getElementById("ajax-toplevel_genre");
            var genre_id = genre_select.options[genre_select.selectedIndex].value;
            url += "&genre_id=" + genre_id;
            http_request.onreadystatechange = updateGenreStyles;
        } 

        http_request.open('GET', url, true);

        http_request.send(null);

    }

    function makePostRequest(url, input_container, update_type, override_output_div) {
        
        var data = Form.serialize(input_container);

        if(override_output_div && override_output_div != "arep") {
            output_div = override_output_div; 
        }

        switch(update_type) {

            case "list":
       	        var myAjax = new Ajax.Request( url, { method: 'post', parameters: data, onComplete: showListRepresentation });
            break;

            default:
       	        var myAjax = new Ajax.Request( url, { method: 'post', parameters: data, onComplete: showReadOnlyRepresentation });
            break;

        }

        var the_update_container = document.getElementById(output_div);
        if(output_div.style && output_div.style.display == "none") {
            output_div.style.display = "block";
        }

    }

    function showReadOnlyRepresentation(originalRequest) {

        if(originalRequest.responseText.match(/error.*/)) {
            alert("There was an error processing your request.\n" + originalRequest.responseText);
            return;
        } else {
            //alert(originalRequest.responseText);
            var data = eval('(' + originalRequest.responseText + ')');
            return updateReadOnlyEntity( data );
        }
    }

    function showListRepresentation(originalRequest) {

        if(originalRequest.responseText.match(/error.*/)) {
            alert("There was an error processing your request.\n" + originalRequest.responseText);
            return;
        } else {
            //alert(originalRequest.responseText);
            var data = eval('(' + originalRequest.responseText + ')');
            return updateListView( data );
        }
    }


    function clearUserInput(form_name) {
		if (!form_name) {
        	var obj = document.getElementById("aform");
		}
		else {
            var obj = document.getElementById(form_name);
		}

        var inputs = obj.getElementsByTagName("INPUT");
        if(inputs.length>0) {
            for (i=0; i<inputs.length; i++) {
                if (inputs[i].type == "text") {
        		    inputs[i].value="";
                } else if (inputs[i].type == "checkbox") {
                    if (inputs[i].checked) {
        		        inputs[i].checked=false;
                    }
                }
            }
        }

        var textareas = obj.getElementsByTagName("TEXTAREA");
        if(textareas.length>0) {
            for (i=0; i<textareas.length; i++) {
                   textareas[i].value = "";
               }
        }

        return;

    }


    function updateEntityId(doc){
        var x = doc.getElementById('your_track_id').value;
        if(x.match(/error.*/)) {
            alert(x);
            return;
        } else {
            document.getElementById('entity_id').value = x;
            var add_form = document.getElementById("form");
            add_form.style.display="block";
            var add_form = document.getElementById("upload_form_div");
            add_form.style.display="none";
        }
    }

    function updateGenreStyles() {
        if (http_request.readyState == 4) {

            if (http_request.status == 200) {

                // Uncomment this line to see the raw text data
                // Comment out lines 107 - 113
                //alert(http_request.responseText);

                if(http_request.responseText.match(/error.*/)) {
                    alert("There was an error processing your request. Please try again later \n" + http_request.responseText);
                    return;
                } else {
                    var data = eval('(' + http_request.responseText + ')');
                }

                // Get the subgenre drop downs
                var sub1 = document.getElementById("ajax-subgenre_one");
                var sub2 = document.getElementById("ajax-subgenre_two");
                var div_sub1 = document.getElementById("div-subgenre_one");
                var div_sub2 = document.getElementById("div-subgenre_two");

                // Dump the original options
                sub1.options.length = 0;
                sub2.options.length = 0;


                // Add the new subgenres
                for(genre in data) {
                    if(data[genre]["value"]) {
                        var option = document.createElement("option");
                        // IE requires that the option be adding to the select prior to setting
                        // the option's attributes.
                        sub1.appendChild(option);

                        option.value = data[genre]["value"]["genre_id"];
                        option.text = data[genre]["value"]["genre_name"];
                    }
                }

                // Sadly, we have to do this all again for subgenre_two
                for(genre in data) {
                    if(data[genre]["value"]) {
                        var option = document.createElement("option");
                        // IE requires that the option be adding to the select prior to setting
                        // the option's attributes.
                        sub2.appendChild(option);

                        option.value = data[genre]["value"]["genre_id"];
                        option.text = data[genre]["value"]["genre_name"];
                    }
                }

                div_sub1.style.display = "inline";
                div_sub2.style.display = "inline";


            } else {
                alert('There was a problem with the request.');
            }
        }
    }

    /*
     * Updates the find user results box on the artist admin form
     */
    function updateUserResults() {
        if (http_request.readyState == 4) {

            if (http_request.status == 200) {

                // Uncomment this line to see the raw text data
                // Comment out lines 107 - 113
                //alert(http_request.responseText);

                if(http_request.responseText.match(/error.*/)) {
                    alert("There was an error processing your request. \n" + http_request.responseText);
                    return;
                } else {
                    var data = eval('(' + http_request.responseText + ')');
                }

                // Get the subgenre drop downs
                var sub1 = document.getElementById("ajax-user_results_sel");

                if(sub1.hasChildNodes()) {
                    sub1.options.length = 0;
                }

                // Add the new subgenres
                for(user in data) {
                    if(data[user]["value"]) {
                        var option = document.createElement("option");
                        // IE requires that the option be adding to the select prior to setting
                        // the option's attributes.
                        sub1.appendChild(option);

                        option.value = data[user]["value"]["user_id"];
                        option.text = data[user]["value"]["username"];
                    }
                }

            } else {
                alert('There was a problem with the request.');
            }
        }
        return;
    }

    /*
     * Updates the find artist results box on the similar artist form
     */
    function updateSimilarArtistResults() {
        if (http_request.readyState == 4) {

            if (http_request.status == 200) {

                // Uncomment this line to see the raw text data
                // Comment out lines 107 - 113
                //alert(http_request.responseText);

                if(http_request.responseText.match(/error.*/)) {
                    alert("There was an error processing your request. \n" + http_request.responseText);
                    return;
                } else {
                    var data = eval('(' + http_request.responseText + ')');
                }

                // Get the subgenre drop downs
                var sub1 = document.getElementById("ajax-sim_artist_results_sel");

                if(sub1.hasChildNodes()) {
                    sub1.options.length = 0;
                }

                // Add the new subgenres
                for(user in data) {
                    if(data[user]["value"]) {
                        var option = document.createElement("option");
                        // IE requires that the option be adding to the select prior to setting
                        // the option's attributes.
                        sub1.appendChild(option);

                        option.value = data[user]["value"]["similar_artist_id"];
                        option.text = data[user]["value"]["similar_artist_name"];
                    }
                }

            } else {
                alert('There was a problem with the request.');
            }
        }
        return;
    }



    /*
     * Updates the hidden entity_id element in the user registration image upload form
     * Called from the hidden iframe in /templates/user_submission/add_artist/step2.tpl
     */
    function updateHiddenImage(doc){
        var x = doc.getElementById('your_track_id').value;
        var image_div_id = "image_div_"+image_div;

        if(x.match(/error.*/)) {
            alert(x);
            return;
        } else {
            document.getElementById(image_div_id).innerHTML = '<img src=\"'+x+'\" width=\"132\" height=\"102\">';
            document.getElementById(image_div_id).style.display="block";
            image_div++;
        }
    }



    /*
     * Updates the hidden entity_id element in the user registration image upload form
     * Called from the hidden iframe in /templates/user_submission/add_artist/step2.tpl
     */
    function updateHiddenTrackDiv(doc){
        var track_id          = doc.getElementById('your_track_id').value;
        var track_name        = doc.getElementById('track_name').value;
        var track_description = doc.getElementById('track_description').value;
        var minors            = doc.getElementById('minors').value;
        var composers         = doc.getElementById('track_composers').value;
        var track_div_id = "track_div_"+track_div;

        if(track_id.match(/error.*/)) {
            alert(track_id);
            return;
        } else {
            document.getElementById(track_div_id).innerHTML = "<b>TRACK ID:</b> "+track_id+"<BR>";
            document.getElementById(track_div_id).innerHTML += "<b>TRACK NAME:</b> "+track_name+"<BR>";
            document.getElementById(track_div_id).innerHTML += "<b>TRACK DESCRIPTION:</b> "+track_description+"<BR>";
            document.getElementById(track_div_id).innerHTML += "<b>COMPOSERS:</b> "+composers+"<BR>";
            document.getElementById(track_div_id).innerHTML += "<b>SUITABLE FOR MINORS:</b> "+minors+"<HR>";
            document.getElementById(track_div_id).style.display="block";
            track_div++;
        }
    }

    /**
     * Require user confirmation prior to submitting a form
     */
    function confirmSubmit(formName, msg) {
        var answer = confirm(msg);
        if (answer){
            formName.submit();
        }
    }

