function sendRequest(service, method, url, content) {
	var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));
	if(!xmlhttp) {
		alert("Nepodarilo sa nadviazať spojenie so serverom!");
	}
	xmlhttp.open(method, url);
	xmlhttp.onreadystatechange = function() {
		service(xmlhttp);
	};
	xmlhttp.setRequestHeader("Cache-Control", "no-cache");
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=windows-1250");
	xmlhttp.send(content);
	return false;
}

function loadData(xmlhttp) {
	if(xmlhttp.readyState == 4) {
		if(xmlhttp.status == 200) {
			var result = xmlhttp.responseText.split(" | ");
			priceEUR = result[0];
			priceSKK = result[1];
			if(document.getElementById("pricePayEUR")) document.getElementById("pricePayEUR").innerHTML = priceEUR;
			if(document.getElementById("pricePaySKK")) document.getElementById("pricePaySKK").innerHTML = priceSKK;
		}
		else  {
			if(document.getElementById("pricePayEUR")) document.getElementById("pricePayEUR").innerHTML = "<img src='images/progress.gif' alt='nahrávam...' />";
			if(document.getElementById("pricePaySKK")) document.getElementById("pricePaySKK").innerHTML = "<img src='images/progress.gif' alt='nahrávam...' />";
		}
	}
	else {
		if(document.getElementById("pricePayEUR")) document.getElementById("pricePayEUR").innerHTML = "<img src='images/progress.gif' alt='nahrávam...' />";
		if(document.getElementById("pricePaySKK")) document.getElementById("pricePaySKK").innerHTML = "<img src='images/progress.gif' alt='nahrávam...' />";
	}
}

function getPrice(product_id) {
	sendRequest(loadData, "GET", "ajax_calc_price.php?id="+calc_id+"&product="+product_id+"&color="+color+"&variant="+variant+"&value="+v_value, "");
}

var color = "BI";
var variant = "";
var v_value = "";

function changeColor(product_id, color_key, is_pic) {
	if(document.getElementById("color_"+color)) document.getElementById("color_"+color).style.border = "1px solid #ebebeb";
	document.getElementById("color_"+color_key).style.border = "1px solid #8d1212";
	if(is_pic == 1) document.getElementById("productImg").src = "pictures/products/"+product_id+"/color_"+color_key+"_detail.jpg";
	else document.getElementById("productImg").src = "images/no_img_detail.png";
	document.getElementById("colorField").value = color_key;
	color = color_key;
	getPrice(product_id);
}

function changeVariant(product_id, variant_key, value_key) {
	variant = variant_key;
	v_value = value_key;
	getPrice(product_id);
}

function changeForm(obj) {
	if(obj.value == "NORMAL") {
		document.getElementById("fieldsSpecial").style.display = "none";
		document.getElementById("fieldsNormal").style.display = "block";
	}
	if(obj.value == "SPECIAL") {
		document.getElementById("fieldsNormal").style.display = "none";
		document.getElementById("fieldsSpecial").style.display = "block";

	}
}

function showRelated(field) {
	var obj = document.getElementById(field);
	var rel = document.getElementById(field+"_f");
	if(obj.checked == true) {
		if(rel.tagName == "DIV") rel.style.display = "block";
		if(rel.tagName == "SPAN") rel.style.display = "inline";
	}
	else rel.style.display = "none";
}

var id = 1;

function addVariant(key, value, operator, number, numbertype, items_div, field_name) {
	if(items_div == undefined) var items_div = "itemsPlace";
	if(field_name == undefined) var field_name = "variant_data";

	var itemsPlace = document.getElementById(items_div);
	var itemDiv = document.createElement("div");

	itemDiv.id = "item"+id;
	itemsPlace.appendChild(itemDiv);

	var operator_options = "";
	var numbertype_options = "";

	if(operator == "") operator = "+";
	if(numbertype == "") numbertype = "%";

	operator_options += operator == "=" ? "<option selected=\"selected\">=</option>" : "<option>=</option>";
	operator_options += operator == "+" ? "<option selected=\"selected\">+</option>" : "<option>+</option>";
	operator_options += operator == "-" ? "<option selected=\"selected\">-</option>" : "<option>-</option>";

	numbertype_options += numbertype == "%" ? "<option value=\"%\" selected=\"selected\">%</option>" : "<option value=\"%\">%</option>";
	numbertype_options += numbertype == "EUR" ? "<option value=\"EUR\" selected=\"selected\">&euro;</option>" : "<option value=\"EUR\">&euro;</option>";

	itemDiv.innerHTML += "<small>[<a href=\"javascript:delVariant("+id+", '"+items_div+"')\">×</a>]</small>\n";
	itemDiv.innerHTML += "<input type=\"text\" name=\""+field_name+"[values]["+id+"][key]\" value=\""+key+"\" class=\"field_xs\" />\n";
	itemDiv.innerHTML += "<input type=\"text\" name=\""+field_name+"[values]["+id+"][value]\" value=\""+value+"\" class=\"field_s\" />\n";
	itemDiv.innerHTML += "<select name=\""+field_name+"[values]["+id+"][operator]\">"+operator_options+"</select>\n";
	itemDiv.innerHTML += "<input type=\"text\" name=\""+field_name+"[values]["+id+"][number]\" value=\""+number+"\" class=\"field_xs\" />\n";
	itemDiv.innerHTML += "<select name=\""+field_name+"[values]["+id+"][numbertype]\">"+numbertype_options+"</select>\n";
	itemDiv.innerHTML += "<input type=\"file\" name=\"picture_"+id+"\" />\n";

	id++;
}

function delVariant(item_id, items_div) {
	if(items_div == undefined) var items_div = "itemsPlace";
	var itemsPlace = document.getElementById(items_div);
	var curVariant = document.getElementById("item"+item_id);
	itemsPlace.removeChild(curVariant);
}

