
/**
 * Submits the given form.
 * @param form_id - The DOM id of the form to be submited
 */
function submit_form(form_id) {
    var f = document.getElementById(form_id);
    f.submit();
}

/**
 * Executes a AJAX call to the given URL.
 * @param id    - The ID of the element to be updated with new values
 * @param url   - The URL of the page that shuld be called via AJAX request
 * @param current_slected_value - The curent value of the list box, if any
 */
function ajaxUpdate(id, url, current_slected_value) {
    var xmlHttp;
    
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    } catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }
    
    if (id == 'dynamic_offer_row') {
        var offer_row = $(id);

        if (offer_row.hasChildNodes()) {
            offer_row.replace('<div id="dynamic_offer_row"></div>', '');
        }
        
        if (current_slected_value != "") {
            if (current_slected_value == 1) {
                url = "offer_row_business_place";
            } else if (current_slected_value == 2) {
                url = "offer_row_house";
            } else if (current_slected_value == 3) {
                url = "offer_row_land";
            } else {
                url = "offer_row_vehicle";
            }
        }
    } else if (id == "dynamic_image_row_delete") {
        url = "/admin/" + url + "?decrease=1";
    }

    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) {
            var rhtml_codes = xmlHttp.responseText;
            if (id == 'dynamic_offer_row' && current_slected_value != "") {
                new Insertion.Bottom('dynamic_offer_row', rhtml_codes);
            } else if (id == 'dynamic_image_row') {
                var image_row_count = $('image_row_count');
                if(image_row_count.value == 11){
                    alert("Maximum uploadable image count is met.");
                } else {
                    image_row_count.value = parseInt(image_row_count.value) + 1;
                    new Insertion.Bottom('dynamic_image_row', rhtml_codes);
                }
            }
        }
    }
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

/**
 * Removes the element from the DOM with the given element ID.
 *
 * @param element_id - The ID of the element to be removed from the DOM
 */
function remove_element(element_id) {
    var image_row_count = $('image_row_count');
    $(element_id).remove();
    image_row_count.value = parseInt(image_row_count.value) - 1;
    ajaxUpdate("dynamic_image_row_delete", "offer_row_image");
}

/**
 * Calls the functions after the page contents are loaded.
 */
window.onload = function() {
//    if (location.href.include("new_offer")) {
//        // Loads the dynamic offer row which was previously loaded.
//        var ofr_catgr = $('offer_offer_type_id');
//        ajaxUpdate('dynamic_offer_row','offer_row_business_place', ofr_catgr.options[ofr_catgr.selectedIndex].value);
//    }
}
