// JavaScript Document

function insert_link() {
	var option = document.getElementById('product-option');
	index = option.selectedIndex;
	
	if(option.options[index].value != "") {
		//alert("You select: " + option.value + ".")
		var ingredients = document.getElementById('ingredients');
		ingredients.value = ingredients.value + '<a href="product.php?pid='+option.options[index].value+'" target="_blank">'+document.getElementById('link-text').value+'</a>';
	}
	else {
		alert("You select nothing.")
	}
}

function get_options() {
	query = document.getElementById("option_query").value;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else {// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			document.getElementById("product-option").innerHTML=xmlhttp.responseText;
		}
	}
	xmlhttp.open("GET","getoption.php?query="+query,true);
	xmlhttp.send();
}

function edit_category(catid) {
	window.location = 'category.php?catid='+catid+'&action=edit';
}

function goto_url(url) {
	window.location = url;
}

function change_page_size(s, id) {
	var option = document.getElementById('page-size');
	index = option.selectedIndex;
	
	if (s=='recipe') {
		goto_url('recipe.php?tid='+id+'&ps='+option.options[index].value);
	}
	else {
		goto_url('product.php?cid='+id+'&ps='+option.options[index].value);
	}
}

function post_to_url(path, params, method) {
    method = method || "post"; // Set method to post by default, if not specified.

    // The rest of this code assumes you are not using a library.
    // It can be made less wordy if you use one.
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for(var key in params) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", key);
        hiddenField.setAttribute("value", params[key]);

        form.appendChild(hiddenField);
    }

    document.body.appendChild(form);    // Not entirely sure if this is necessary
    form.submit();
}

function windows_popup(url,windowname,w,h,x,y){
	window.open(url,windowname,"resizable=no,toolbar=no,scrollbars=no,menubar=no,status=no,directories=no,width="+w+",height="+h+",left="+x+",top="+y+"");
}
