function setStyle(obj1, obj2, obj3){
	var item_1 = document.getElementById(obj1);
	var item_2 = document.getElementById(obj2);
	var item_3 = document.getElementById(obj3);


	var windowH1 = item_1.offsetHeight;
	var windowH2 = item_2.offsetHeight;
	var windowH3 = item_3.offsetHeight;

	if(windowH1 > windowH2 && windowH1 > windowH3){
		item_2.style.height = windowH1+"px";
		item_3.style.height = windowH1+"px";
	}else if(windowH2 > windowH1 && windowH2 > windowH3){
		item_1.style.height = windowH2+"px";
		item_3.style.height = windowH2+"px";
	}else if(windowH3 > windowH2 && windowH3 > windowH1){
		item_2.style.height = windowH3+"px";
		item_1.style.height = windowH3+"px";
	}

}

curmenu = 0;
var si;
var opacity;

function rollOver(id) { if (id != curmenu) { document.getElementById("prodsel"+id).style.backgroundColor = "#eaeaea"; } }
function rollOut(id) { if (id != curmenu) {	document.getElementById("prodsel"+id).style.backgroundColor = "#ffffff"; } }
function prollOver(obj) { obj.style.backgroundColor = "#eaeaea"; }
function prollOut(obj) { obj.style.backgroundColor = "#ffffff"; }
function showImg(img) {
	if (img == "") {
		document.getElementById("imgSampler").innerHTML = "";
	} else {
		document.getElementById("imgSampler").innerHTML = "<img src='thumb.php?file=products/"+img+"&w=100&h=100' id='thePhoto'>";
		initImage();
	}
}
function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}
function fadeIn(objId) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity <= 100) {
      setOpacity(obj, opacity);
      opacity += 10;
//      window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 50);
    } else {
//    	alert("clear");
    	clearInterval(si);
    }
  }
}
function initImage() {
	clearInterval(si);
  imageId = 'thePhoto';
  image = document.getElementById(imageId);
  setOpacity(image, 0);
  image.style.visibility = 'visible';
  opacity = 0;
  si = setInterval('fadeIn(imageId)',30);
//  fadeIn(imageId,0);
}
function onSubmitForm()
{
	//alert(document.pressed);
	
	if(document.getElementById('conditions').checked == false)
	{
		alert(noConditionsCheck);
		return false;
	}
	
	if(document.pressed == "update")
	{
		document.removeform.method = "post";
		document.removeform.action = "?action=refreshcart";
		document.removeform.target = "";
	}
	if(document.pressed == "adder"){
		var size = document.s_size.value;
		alert(document.pressed);
		document.addNew.action = ""
	}
	if(document.pressed == "updateNew")
	{
		document.removeform.method = "post";
		document.removeform.action = "?action=refreshFullcartCintamani";
		document.removeform.target = "";
	}
	else if(document.pressed == "buy")
	{
		if(document.getElementById('conditions').checked)
		{
			document.removeform.method = "post";
			document.removeform.target = "gluggi";
			path = "https://secure.greind.is/cintamani.is/storenew/buy.php";
			window.open(path+'?webID=1&step=1','gluggi','width=750,height=550,scrollbars=1');
			document.removeform.action = path+"?webID=1&step=1";
		}
	}
	return true;
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function setInCart(obj){

	var Sizelen = document.cart.Size.length;
	var selectedColor;
	var selectedSize;
	for(i = 0; i < Sizelen; i++){
		if(document.cart.Size[i].checked) selectedSize = document.cart.Size[i].value;
	}
	var Collen = document.cart.Color.length;
	for(i = 0; i < Collen; i++){
		if(document.cart.Color[i].checked) selectedColor = document.cart.Color[i].value;
	}
	var selectedQuant  = document.getElementById('Quant').value;

	if(!selectedSize){ alert("Žaš veršur aš velja Stęrš!"); return false; }
	if(!selectedColor){ alert("Žaš veršur aš velja lit!"); return false; }

	//alert("Stęrš: " + selectedSize + " Litur: " + selectedColor + " Magn: " + selectedQuant);
	window.shoppingcart.location = 'grand_shopingcart.php?action=addcartFull&prodid='+obj+'&s_color='+selectedColor+'&s_size='+selectedSize+'&s_quant='+selectedQuant;
}


function upCart(){
	xmlhttpPost('addCart_ajax.php');
}
function upSizes(id){
	ColorsxmlhttpPost('addSizesColors.php?id="'+id+'"');
}

function removeFromCart(id){
	xmlhttpPost('addCart_ajax.php?id='+id+'&rm=true');
}

function addAjaxCart(){


	var prodid = document.getElementById('prodid').value;
	var quant = document.getElementById('quant').value;
	var name = document.getElementById('name').value;

	var color = document.getElementById('color').value;
	var size = document.getElementById('size').value;

	xmlhttpPost('addCart_ajax.php?id='+prodid+'&quant='+quant+'&name='+name+'&size='+size+'&color='+color);

}
function addFront(id,quant,size,color,name){
	xmlhttpPost('addCart_ajax.php?id='+id+'&quant='+quant+'&size='+size+'&color='+color+'&name='+name);

}

function addCartCount(vars) {
	var out;
	var fjoldi;
	splitStr = vars.split("\n");
	fjoldi = splitStr.length-1;

	out = "("+fjoldi+")";
	var cartDisplay = document.getElementById('countCart');

	cartDisplay.innerHTML = out;

}

function updateCart(vars){

	splitStr = vars.split("\n");
	var out;
	var updateColorArray = [];
	var updateSizeArray = [];
	out = "<table width='100%' border='1' cellpadding='2'>\n";
	for(i = 0; i < splitStr.length; i++){
		itemSplit = splitStr[i].split(":");
		if(itemSplit[0] != ""){
			out += "<tr>\n";
			//out += "<td>Key:</td><td>"+itemSplit[0]+"</td>\n";
			out += "<td>Name:</td><td><a href='?prodid="+itemSplit[1]+"'>"+itemSplit[5]+"</a></td>\n";
			out += "<td>Quant</td><td>"+itemSplit[2]+"</td>\n";
			out += "<td>Color</td><td>";
			out += "<select name='color["+itemSplit[0]+"]' id='colorer_"+itemSplit[0]+"'>\n";

			out += "</select>";
			out += "</td>\n";
			out += "<td>size:"+itemSplit[4]+"</td><td>";
			out += "<select name='size["+itemSplit[0]+"]' id='sizer_"+itemSplit[0]+"'>\n";

			out += "</select>";
			out += "</td>\n";
			out += "<td><a onClick=\"removeFromCart('"+itemSplit[0]+"');\"><img src='sysimages/crossbox.gif' border='0' alt='Click here to remove item from cart.'></a></td>\n";
			out += "</tr>\n";

			pid = itemSplit[1];
			pkey = itemSplit[0];
			pval = itemSplit[3];
			pval2 = itemSplit[4];


			updateColorArray[i] = [pkey, pid, pval];

			updateSizeArray[i] = [pkey, pid, pval2];

			//setTimeout("colorsUpdate("+itemSplit[1]+","+itemSplit[0]+",\""+itemSplit[3]+"\")",75 * i);
			//setTimeout("sizesUpdate("+itemSplit[1]+","+itemSplit[0]+",\""+itemSplit[4]+"\")",77 * i);
			//setTimeout("updateCart()",4000);

		}else if(i == 0 && itemSplit[0] == "" ) {
			out += "<tr><td>Shopping Cart is empty</td></tr>\n";
		}
	}
	out += "</table>\n";

	updateColorInCart(updateColorArray);
	updateSizeInCart(updateSizeArray);

	var cartDisplay = document.getElementById('remover');

	cartDisplay.innerHTML = out;
}


function updateColorInCart(array){

	//alert(array.length);

	for(i = 1; i <= array.length; i++){
		//alert("updateing"+i);
		setTimeout("colorspdate("+array[1]+","+array[0]+","+array[2]+");",73 * i);
	}

}
function updateSizeInCart(array){

	//alert("Sizes: "+array);

}

function colorsUpdate(id,key,sel) {

	alert("id:"+id+" Key:"+key+" sel:"+sel);

	strURL = "addColors.php?id="+id;
	var cout = "";

	var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
        	//out += colorsUpdate(self.xmlHttpReq.responseText);
        	str = self.xmlHttpReq.responseText;
        	splitStr = str.split("\n");
			for(i = 0; i < splitStr.length; i++){
				itemSplit = splitStr[i].split(":");
				if(itemSplit[1] != undefined){
					if(itemSplit[1] == sel){
						r = "selected";
					}else{
						r = "";
					}
					cout += "<option value='"+itemSplit[1]+"' "+r+">"+itemSplit[2]+"</option>\n";
				}
			}
			document.getElementById('colorer_'+key).innerHTML = cout;
        }
    }
	self.xmlHttpReq.send(strURL);

}

function sizesUpdate(id,key,sel) {

	strURL = "addSizes.php?id="+id;
	var cout = "";

	var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
        	//out += colorsUpdate(self.xmlHttpReq.responseText);
        	str = self.xmlHttpReq.responseText;
        	splitStr = str.split("\n");
			for(i = 0; i < splitStr.length; i++){
				itemSplit = splitStr[i].split(":");
				if(itemSplit[1] != undefined){
					if(itemSplit[1] == sel){
						r = "selected";
					}else{
						r = "";
					}
					cout += "<option value='"+itemSplit[1]+"' "+r+">"+itemSplit[2]+"</option>\n";
				}
			}
			document.getElementById('sizer_'+key).innerHTML = cout;
        }
    }
	self.xmlHttpReq.send(strURL);

}


// AJAX
function xmlhttpPost(strURL) {
	var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
        	if(document.getElementById('remover')){
           		updateCart(self.xmlHttpReq.responseText);
        	}
           	addCartCount(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(strURL);
}

function SizesxmlhttpPost(strURL) {
	var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
        	if(document.getElementById('sizer')){
           		updateCart(self.xmlHttpReq.responseText);
        	}
           	addCartCount(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(strURL);
}

function ColorsxmlhttpPost(strURL) {
	var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
        	if(document.getElementById('sizer')){
           		colorsUpdate(self.xmlHttpReq.responseText);
        	}
        }
    }
    self.xmlHttpReq.send(strURL);
}
