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() {
	sendRequest(loadData, "GET", "ajax_calc_price.php?id="+calcId+"&product="+prodId+"&color="+color2calc+"&width="+width2calc+"&height="+height2calc+"&variant="+variant+"&value="+v_value, "");
}

var calcId = 0;
var prodId = 0;
var minW = 0;
var minH = 0;
var width2calc = minW;
var height2calc = minH;
var color2calc = "30-10";
var aspectRatio = minW/minH;
var defaultSize = "vlastna_w";
var variant = "";
var v_value = "";

function setDefault(x) {
	defaultSize = x;
}

function setProductSize(w, h) {
	if(w == "vlastne" || h == "vlastne") {
		if(defaultSize == "vlastna_w") {
				customW = document.getElementById("vlastna_w") ? document.getElementById("vlastna_w").value.replace(/,/, ".") : 0;
			if(constH == true) customH = minH;
			else customH = Math.round(customW/aspectRatio*10)/10;
			if(document.getElementById("vlastna_h")) document.getElementById("vlastna_h").value = customH;
		}
		if(defaultSize == "vlastna_h") {
			customH = document.getElementById("vlastna_h") ? document.getElementById("vlastna_h").value.replace(/,/, ".") : 0;
			if(constW == true) customW = minW;
			else customW = Math.round(customH*aspectRatio*10)/10;
			if(document.getElementById("vlastna_w")) document.getElementById("vlastna_w").value = customW;
		}
		width2calc = customW;
		height2calc = customH;
		if(document.getElementById("vlastnyBox")) document.getElementById("vlastnyBox").style.display = "inline";
	}
	else {
		width2calc = w;
		height2calc = h;
		if(document.getElementById("vlastnyBox")) document.getElementById("vlastnyBox").style.display = "none";
	}
	if(width2calc < minW || height2calc < minH) {
		if(document.getElementById("vlastna_w")) document.getElementById("vlastna_w").value = minW;
		if(document.getElementById("vlastna_h")) document.getElementById("vlastna_h").value = minH;
		width2calc = minW;
		height2calc = minH;
		alert("Rozmery musia byť minimálne "+minW+" cm × "+minH+" cm");
	}
	if(width2calc > 55 && height2calc > 55) {
		if(constW == true) {
			maxW = minW;
			maxH = 55;
		}
		if(constH == true) {
			maxW = 55;
			maxH = minH;
		}
		if(width2calc > height2calc && constW == false && constH == false) {
			maxW = Math.round(55*aspectRatio*10)/10;
			maxH = 55;
		}
		if(width2calc < height2calc && constW == false && constH == false) {
			maxW = 55;
			maxH = Math.round(55/aspectRatio*10)/10;
		}
		if(document.getElementById("vlastna_w")) document.getElementById("vlastna_w").value = maxW;
		if(document.getElementById("vlastna_h")) document.getElementById("vlastna_h").value = maxH;
		width2calc = maxW;
		height2calc = maxH;
		alert("Rozmery môžu byť maximálne "+maxW+" cm × "+maxH+" cm");
	}
	if(document.getElementById("rozmery_"+w+"x"+h)) document.getElementById("rozmery_"+w+"x"+h).checked = true;
	getPrice();
}

function setProductColor(color, extrapay) {
	if(document.getElementById("color_"+color2calc)) document.getElementById("color_"+color2calc).style.backgroundColor = "white";
	if(document.getElementById("color_"+color)) document.getElementById("color_"+color).style.backgroundColor = "#e9a839";
	if(document.getElementById("extraPay")) document.getElementById("extraPay").innerHTML = extrapay;
	if(document.getElementById("farba")) document.getElementById("farba").value = color;
	color2calc = color;
	getPrice();
}

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>";

	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);
}
