function submit_form(frm, div_update, frm_action) {
    loading('div_loading');
    document.getElementById('div_loading').className = "";
    var log = $(div_update);
    $(frm).set('load', {
        url: frm_action,
        method: $(frm).get("method"),
        update: log,
        onComplete: function() {}
    });
    $(frm).load();   
}

function switch_check_all(checkbox_name) {
    chkbox = document.getElementsByTagName("input");
    chk_check = false;
    for (i in chkbox) {
        if (chkbox[i].type == "checkbox" && chkbox[i].name == checkbox_name) {
            if (chkbox[i].checked == false) {
                chk_check = true;
            }
        }
    }
    for (i in chkbox) {
        if (chkbox[i].type == "checkbox" && chkbox[i].name == checkbox_name) {
            chkbox[i].checked = chk_check;
        }
    }
}

function change_order(order) {
    old_order = $('order').value;
    $('order').value = order;
    if (old_order == order && $('asc_desc').value == 'ASC') {
        $('asc_desc').value = 'DESC';
    } else {
        $('asc_desc').value = 'ASC';
    }
}

function loading(layer_id) {
    //$(layer_id).set('html', '<span class="loading">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loading</span>')
    $(layer_id).set('html', '<span class="loading">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>')
}

function change_page_offset(page, offset) {
    $('page').value = page;
    $('offset').value = offset;
}

function showLayer(layer_id) {
    if ($(layer_id).style.display == "") {
        $(layer_id).style.display = "none";
    } else {
        $(layer_id).style.display = "";
    }
}

function showHideLayers(show_layers, hide_layers) {
    arr_show_layers = show_layers.split(",");
    arr_hide_layers = hide_layers.split(",");
    for (i in arr_show_layers) {
        if (document.getElementById(arr_show_layers[i])) {
            document.getElementById(arr_show_layers[i]).style.display = "";
        }
    }
    for (i in arr_hide_layers) {
        if (document.getElementById(arr_hide_layers[i])) {
            document.getElementById(arr_hide_layers[i]).style.display = "none";
        }
    }
}

function updateActionForm(group_id) {
    switch (group_id) {
        case "0":
            show_layers = "avatar_action,pet_action";
            hide_layers = "ship_action,blog_action";
            break;
        case "1":
            show_layers = "ship_action";
            hide_layers = "avatar_action,pet_action,blog_action";
            break;
        case "2":
            show_layers = "blog_action";
            hide_layers = "avatar_action,pet_action,ship_action";
            break;
        case "3":
            show_layers = "";
            hide_layers = "avatar_action,pet_action,ship_action,blog_action";
            break;
        case "4":
            show_layers = "avatar_action,pet_action";
            hide_layers = "blog_action,ship_action";
            break;
        case "5":
            show_layers = "";
            hide_layers = "avatar_action,pet_action,ship_action,blog_action";
            break;
    }
    showHideLayers(show_layers, hide_layers);
}

function updateActionFormEdit(group_id) {
    switch (group_id) {
        case "0":
            show_layers = "avatar_action_edit,pet_action_edit";
            hide_layers = "ship_action_edit,blog_action_edit";
            break;
        case "1":
            show_layers = "ship_action_edit";
            hide_layers = "avatar_action_edit,pet_action_edit,blog_action_edit";
            break;
        case "2":
            show_layers = "blog_action_edit";
            hide_layers = "avatar_action_edit,pet_action_edit,ship_action_edit";
            break;
        case "3":
            show_layers = "";
            hide_layers = "avatar_action_edit,pet_action_edit,ship_action_edit,blog_action_edit";
            break;
        case "4":
            show_layers = "avatar_action_edit,pet_action_edit";
            hide_layers = "blog_action_edit,ship_action_edit";
            break;
        case "5":
            show_layers = "";
            hide_layers = "avatar_action_edit,pet_action_edit,ship_action_edit,blog_action_edit";
            break;
    }
    showHideLayers(show_layers, hide_layers);
}

function submit_user_action(frm, frm_action, action) {
    f = eval('document.'+frm);
    f.action = frm_action;
    f.action_type.value = action;
    f.submit();
}

function load_npc_animations(entity_id, select_id, npc_times, option_id) {
    if (entity_id == "kid") {
        $('kid_info').setStyle('display', '');
        $('npcs_phrases').setStyle('display', 'none');
        $('npc_times').set('html', '0');
    } else {
        $('kid_info').setStyle('display', 'none');
        $('npcs_phrases').setStyle('display', '');
        var myHTMLRequest = new Request.HTML(
            {
                update:$('npc_poses'),
                encoding: 'utf-8',
                urlEncoded: true
            }
        ).get("/admin/get_npc_animations/?entity_id="+entity_id+"&select_id="+select_id+"&npc_times="+npc_times+"&option_id="+option_id);
    }
}

function load_npc_animations_create(entity_id, select_id, npc_times) {
    if (entity_id == "kid") {
        $('kid_info_c').setStyle('display', '');
        $('npcs_phrases_c').setStyle('display', 'none');
        $('npc_times_c').set('html', '0');
    } else {
        $('kid_info_c').setStyle('display', 'none');
        $('npcs_phrases_c').setStyle('display', '');
        var myHTMLRequest = new Request.HTML(
            {
                update:$('npc_poses_c'),
                encoding: 'utf-8',
                urlEncoded: true
            }
        ).get("/admin/get_npc_animations/?entity_id="+entity_id+"&select_id="+select_id+"&npc_times="+npc_times);
    }
}

function load_npc_phrases(channel_id, entity_id) {
    if (entity_id == "kid") {
        $('kid_info').setStyle('display', '');
        $('npcs_phrases').setStyle('display', 'none');
        $('npc_times').set('html', '0');
    } else {
        $('kid_info').setStyle('display', 'none');
        $('npcs_phrases').setStyle('display', '');
        var myHTMLRequest = new Request.HTML(
            {
                update:$('npc_poses'),
                encoding: 'utf-8',
                urlEncoded: true
            }
        ).get("/admin/get_npc_phrases/?channel_id="+channel_id+"&entity_id="+entity_id);
    }
}

function load_npc_phrases_create(entity_id) {
    if (entity_id == "kid") {
        $('kid_info_c').setStyle('display', '');
        $('npcs_phrases_c').setStyle('display', 'none');
    } else {
        $('kid_info_c').setStyle('display', 'none');
        $('npcs_phrases_c').setStyle('display', '');
    }
}

function add_select_option(select_id, opt_value, opt_text) {
    if (document.getElementById(select_id)) {
        var y   = document.createElement('option');
        y.text  = opt_text;
        y.value = opt_value;
        try {
            $(select_id).add(y,null); // standards compliant
        } catch(ex) {
            $(select_id).add(y); // IE only
        }
    }
}

function clean_select_options(select_id) {
    if (document.getElementById(select_id)) {
        var x = $(select_id);
        while (x.length > 0) {
            x.remove(0);
        }
    }
}

function search_kid(frm_name) {
    f = eval("document."+frm_name);
    kid_name = f.search_kid_name.value;
    $('kid_name').set('html', 'loading...');
    var myHTMLRequest = new Request.HTML(
        {
            update:$('kid_name'),
            encoding: 'utf-8',
            urlEncoded: true
        }
    ).get("/admin/search_kid/?kid_name="+kid_name+"&create=false");
}

function search_kid_c(frm_name) {
    f = eval("document."+frm_name);
    kid_name = f.search_kid_name.value;
    $('kid_name_c').set('html', 'loading...');
    var myHTMLRequest = new Request.HTML(
        {
            update:$('kid_name_c'),
            encoding: 'utf-8',
            urlEncoded: true
        }
    ).get("/admin/search_kid/?kid_name="+kid_name+"&create=true");
}

function check_phrase_type(layer_id, phrase_type) {
    if (phrase_type == "1") {
        document.getElementById(layer_id).style.display = "";
    } else {
        document.getElementById(layer_id).style.display = "none";
    }
}

function pet_central_add_pet(frm_name, channel_id){
    f = eval("document."+frm_name);
    pet_id  = f.add_pet_id.value;
    total   = f.total_add_pets.value;
    var myHTMLRequest = new Request.HTML(
        {
            update:$('pet_list'),
            encoding: 'utf-8',
            urlEncoded: true
        }
    ).get("/admin/pet_central_add_pet/?channel_id="+channel_id+"&pet_id="+pet_id+"&total="+total);
}

function check_select_item(select_id, option_id) {
    el = document.getElementById(select_id);
    idx = 0;
    for (i in el.options) {
        if (el.options[i].value==option_id) {
            idx = i;
        }
    }
    el.selectedIndex = idx;
}

function autocomplete_tag() {
    post_url = '/admin/autocomplete_tag';
    
    var inputWord = $('input_tags');
    
    new Autocompleter.Request.HTML(inputWord, post_url, {
		multiple: true,
		delay: 200,
		'injectChoice': function(choice) {
			// choice is one <li> element
			var text = choice.getFirst();
			// the first element in this <li> is the <span> with the text
			var value = text.innerHTML;
			// inputValue saves value of the element for later selection
			choice.inputValue = value;
			// overrides the html with the marked query value (wrapped in a <span>)
			text.set('html', this.markQueryValue(value));
			// add the mouse events to the <li> element
			this.addChoiceEvents(choice);
		}
	});
	
	if (document.getElementById('input_tags_edit')) {
	    var inputWordEdit = $('input_tags_edit');
        
        new Autocompleter.Request.HTML(inputWordEdit, post_url, {
        	multiple: true,
        	delay: 200,
        	'injectChoice': function(choice) {
        		// choice is one <li> element
        		var text = choice.getFirst();
        		// the first element in this <li> is the <span> with the text
        		var value = text.innerHTML;
        		// inputValue saves value of the element for later selection
        		choice.inputValue = value;
        		// overrides the html with the marked query value (wrapped in a <span>)
        		text.set('html', this.markQueryValue(value));
        		// add the mouse events to the <li> element
        		this.addChoiceEvents(choice);
        	}
        });
	}
}

function autocomplete_tag_pet() {
    post_url = '/admin/autocomplete_tag';
    
    var inputWord = $('likes');
    
    new Autocompleter.Request.HTML(inputWord, post_url, {
		multiple: true,
		delay: 200,
		'injectChoice': function(choice) {
			// choice is one <li> element
			var text = choice.getFirst();
			// the first element in this <li> is the <span> with the text
			var value = text.innerHTML;
			// inputValue saves value of the element for later selection
			choice.inputValue = value;
			// overrides the html with the marked query value (wrapped in a <span>)
			text.set('html', this.markQueryValue(value));
			// add the mouse events to the <li> element
			this.addChoiceEvents(choice);
		}
	});
	
	var inputWord2 = $('dislikes');
    
    new Autocompleter.Request.HTML(inputWord2, post_url, {
		multiple: true,
		delay: 200,
		'injectChoice': function(choice) {
			// choice is one <li> element
			var text = choice.getFirst();
			// the first element in this <li> is the <span> with the text
			var value = text.innerHTML;
			// inputValue saves value of the element for later selection
			choice.inputValue = value;
			// overrides the html with the marked query value (wrapped in a <span>)
			text.set('html', this.markQueryValue(value));
			// add the mouse events to the <li> element
			this.addChoiceEvents(choice);
		}
	});
	
	if (document.getElementById('likes_edit') && document.getElementById('dislikes_edit')) {
	    var inputWordEdit = $('likes_edit');
        
        new Autocompleter.Request.HTML(inputWordEdit, post_url, {
        	multiple: true,
        	delay: 200,
        	'injectChoice': function(choice) {
        		// choice is one <li> element
        		var text = choice.getFirst();
        		// the first element in this <li> is the <span> with the text
        		var value = text.innerHTML;
        		// inputValue saves value of the element for later selection
        		choice.inputValue = value;
        		// overrides the html with the marked query value (wrapped in a <span>)
        		text.set('html', this.markQueryValue(value));
        		// add the mouse events to the <li> element
        		this.addChoiceEvents(choice);
        	}
        });
        
        var inputWordEdit2 = $('dislikes_edit');
        
        new Autocompleter.Request.HTML(inputWordEdit2, post_url, {
        	multiple: true,
        	delay: 200,
        	'injectChoice': function(choice) {
        		// choice is one <li> element
        		var text = choice.getFirst();
        		// the first element in this <li> is the <span> with the text
        		var value = text.innerHTML;
        		// inputValue saves value of the element for later selection
        		choice.inputValue = value;
        		// overrides the html with the marked query value (wrapped in a <span>)
        		text.set('html', this.markQueryValue(value));
        		// add the mouse events to the <li> element
        		this.addChoiceEvents(choice);
        	}
        });
	}
}

function autocomplete_tag_home_blog() {
    post_url = '/admin/home/autocomplete_tag';
    
    var inputWord = $('tags');
    
    new Autocompleter.Request.HTML(inputWord, post_url, {
		multiple: true,
		delay: 200,
		'injectChoice': function(choice) {
			// choice is one <li> element
			var text = choice.getFirst();
			// the first element in this <li> is the <span> with the text
			var value = text.innerHTML;
			// inputValue saves value of the element for later selection
			choice.inputValue = value;
			// overrides the html with the marked query value (wrapped in a <span>)
			text.set('html', this.markQueryValue(value));
			// add the mouse events to the <li> element
			this.addChoiceEvents(choice);
		}
	});
	
	if (document.getElementById('tags_edit')) {
	    var inputWordEdit = $('tags_edit');
        
        new Autocompleter.Request.HTML(inputWordEdit, post_url, {
        	multiple: true,
        	delay: 200,
        	'injectChoice': function(choice) {
        		// choice is one <li> element
        		var text = choice.getFirst();
        		// the first element in this <li> is the <span> with the text
        		var value = text.innerHTML;
        		// inputValue saves value of the element for later selection
        		choice.inputValue = value;
        		// overrides the html with the marked query value (wrapped in a <span>)
        		text.set('html', this.markQueryValue(value));
        		// add the mouse events to the <li> element
        		this.addChoiceEvents(choice);
        	}
        });
	}
}

function get_npc_animation_editor(npc_id) {
    var myHTMLRequest = new Request.HTML(
        {
            update:$('npc_animation_editor'),
            encoding: 'utf-8',
            urlEncoded: true
        }
    ).get("/admin/get_npc_animation_editor/?npc_id="+npc_id);
}

function get_draw_viewer(url_info) {
    var myHTMLRequest = new Request.HTML(
        {
            update:$('canvas_viewer'),
            encoding: 'utf-8',
            urlEncoded: true
        }
    ).get(url_info);
}

function slugUpdater(from, to) {
    if (document.getElementById(from)) {
        document.getElementById(from).toChange = to;
        document.getElementById(from).onchange = function() {
            if (document.getElementById(this.toChange).value == '' || !document.getElementById(this.toChange).changedByUser) {
                document.getElementById(this.toChange).value = URLify(this.value)
            }
        }
    }
    if (document.getElementById(to)) {
        document.getElementById(to).onchange = function() {
            this.value = URLify(this.value)
            this.changedByUser = true;
        }
    }
}

function goto(destination) {
    document.location = destination;
}

function submit_home_blog_post_action(frm, frm_action, action) {
    q = confirm('Do you really want to delete this post(s)');
    if (q) {
        f = eval('document.'+frm);
        f.action = frm_action;
        f.action_type.value = action;
        f.submit();
    }
}

function submit_home_testimonial_action(frm, frm_action, action) {
    q = confirm('Do you really want to delete this testimonial(s)');
    if (q) {
        f = eval('document.'+frm);
        f.action = frm_action;
        f.action_type.value = action;
        f.submit();
    }
}

function submit_home_highlight_action(frm, frm_action, action) {
    q = confirm('Do you really want to delete this highlight(s)');
    if (q) {
        f = eval('document.'+frm);
        f.action = frm_action;
        f.action_type.value = action;
        f.submit();
    }
}

function submit_form_home_blog(frm, frm_action) {
    f = eval('document.'+frm);
    f.action = frm_action;
    f.method = 'get';
    f.submit();
}

function ec_goto_cart_action(frm_action) {
    f = eval('document.frm_cart');
    f.frm_action.value = frm_action;
    f.submit();
}

function ec_submit_form_login(frm, frm_action) {
    f = eval('document.'+frm);
    f.action = frm_action;
    f.submit();
}

function ec_check_same_address(check) {
    if (check.checked) {
        document.getElementById('delivery_name').disabled       = 'disabled';
        document.getElementById('delivery_phone').disabled      = 'disabled';
        document.getElementById('delivery_zipcode').disabled    = 'disabled';
        document.getElementById('delivery_address').disabled    = 'disabled';
        document.getElementById('delivery_complement').disabled = 'disabled';
        document.getElementById('delivery_city').disabled       = 'disabled';
        document.getElementById('delivery_state').disabled      = 'disabled';
        document.getElementById('delivery_country').disabled    = 'disabled';
    } else {
        document.getElementById('delivery_name').disabled       = '';
        document.getElementById('delivery_phone').disabled      = '';
        document.getElementById('delivery_zipcode').disabled    = '';
        document.getElementById('delivery_address').disabled    = '';
        document.getElementById('delivery_complement').disabled = '';
        document.getElementById('delivery_city').disabled       = '';
        document.getElementById('delivery_state').disabled      = '';
        document.getElementById('delivery_country').disabled    = '';
    }
}

function ec_submit_pag_seguro(frm_name) {
    f = eval('document.'+frm_name);
    setTimeout('f.submit();', 3000);
}