var ROOT_URL = "http://www.stular.com/";
var xmlHttp;

function inputKeyPressed(e, rid, start_pcs)
{
	var keynum
	var keychar
	
	if(window.event) // IE
	{
		keynum = e.keyCode
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which
	}
	if (keynum==13){
		checkPCSMoreThenZero(rid, start_pcs);
		return false;
	}
	return true;
}


function orderSend()
{	
	document.getElementById("action").value = "send";
	document.fmCart.submit();
}

function deleteProduct(row_id)
{
	
	xmlHttp=GetXmlHttpObject();
	
	if (xmlHttp==null)
	{
		alert ("Error: Browser does not support HTTP Request!");
		return;
	} 

	var url=ROOT_URL+"shop/ajax/delete_from_cart.php";
	url=url+"?rid="+row_id;

	xmlHttp.onreadystatechange=refreshList2;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function showList2()
{
	xmlHttp=GetXmlHttpObject();
	
	if (xmlHttp==null)
	{
		alert ("Error: Browser does not support HTTP Request!");
		return;
	} 
	
	var url=ROOT_URL+"shop/ajax/big_cart.php";

	xmlHttp.onreadystatechange=stateChanged2;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
} 

function stateChanged2() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		if (xmlHttp.responseText.length > 0)
		{ 
			document.getElementById("bigCart").innerHTML = xmlHttp.responseText;
		}
	} 
} 

function refreshList2() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		document.getElementById("bigCart").innerHTML = xmlHttp.responseText;
		showList2();
	} 
}


function changeDelivery()
{
	document.getElementById("delivery").value = document.getElementById("delivery_type").value;
	document.fmCart.submit();
}
function changeDelivery2()
{
	if(document.getElementById("delivery_type").value == "4,00")
	{
		document.getElementById("countryContainer").style.display = "block";
	}
	else
	{
		document.getElementById("countryContainer").style.display = "none";
		document.getElementById("country").value = "0";
		document.getElementById("delivery").value = "0";	
	}
}

function changeDelivery3()
{
	document.getElementById("delivery").value = document.getElementById("delivery_type").value;
	document.fmCart.submit();
}

function addToCard(row_id, p)
{
	if(document.getElementById("cartAddContain"+row_id) != null)
	{
		document.getElementById("cartAddContain"+row_id).className = "cartAddContainerRed";
	}
	if(document.getElementById("addProductToCard"+row_id) != null)
	{
		document.getElementById("addProductToCard"+row_id).className = "addToCartRed";
	}
	xmlHttp=GetXmlHttpObject();
	var pcs=0;
	
	if (xmlHttp==null)
	{
		alert ("Error: Browser does not support HTTP Request!");
		return;
	} 
	if(p == 0)
	{
		pcs=document.getElementById("quantity_"+row_id).value;
	}
	else
	{
		pcs=p;
	}
	
	var url=ROOT_URL+"shop/ajax/add_to_cart.php";
	url=url+"?rid="+row_id;
	url=url+"&pcs="+pcs;

	xmlHttp.onreadystatechange=refreshList;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function deleteFromCard(row_id)
{
	
	xmlHttp=GetXmlHttpObject();
	
	if (xmlHttp==null)
	{
		alert ("Error: Browser does not support HTTP Request!");
		return;
	} 

	var url=ROOT_URL+"shop/ajax/delete_from_cart.php";
	url=url+"?rid="+row_id;

	xmlHttp.onreadystatechange=refreshList;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function showList()
{
	xmlHttp=GetXmlHttpObject();
	
	if (xmlHttp==null)
	{
		alert ("Error: Browser does not support HTTP Request!");
		return;
	} 
	
	var url=ROOT_URL+"shop/ajax/cart.php";

	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
} 

function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		if (xmlHttp.responseText.length > 0)
		{ 
			document.getElementById("rightColCart").innerHTML = xmlHttp.responseText;
		}
	} 
} 

function refreshList() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		document.getElementById("rightColCart").innerHTML = xmlHttp.responseText;
		showList();
	} 
}

function GetXmlHttpObject()
{ 
	var objXMLHttp=null

	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
} 

function decrease(rid, decr)
{
	if(document.getElementById("quantity_"+rid).value != decr*2)
	{
		document.getElementById("quantity_"+rid).value = parseInt(document.getElementById("quantity_"+rid).value)-decr;
	}
	else
	{
		document.getElementById("quantity_"+rid).value = parseInt(document.getElementById("quantity_"+rid).value)-decr;
		document.getElementById("pcs_left_"+rid).innerHTML = '<img src="'+ROOT_URL+'/design/stular_pcs_left_gray.png" width="8" height="15" alt="Štular plus" />';
	}
}

function increase(rid, incer)
{
	document.getElementById("quantity_"+rid).value = parseInt(document.getElementById("quantity_"+rid).value)+incer;
	if(document.getElementById("quantity_"+rid).value == incer*2)
	{
		document.getElementById("pcs_left_"+rid).innerHTML = '<a href="javascript:decrease('+rid+','+incer+');" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage(\'quantityB_'+ROOT_URL+'\',\'\',\''+ROOT_URL+'/design/stular_pcs_left_black.png\',1)"><img src="'+ROOT_URL+'/design/stular_pcs_left_blue.png" alt="Štular plus" name="quantityB_'+ROOT_URL+'" width="8" height="15" border="0" id="quantityB_'+ROOT_URL+'" /></a>';
	}
}

function checkPCS(rid, start_pcs)
{
	if(document.getElementById("quantity_"+rid).value >= start_pcs*2)
	{
		document.getElementById("pcs_left_"+rid).innerHTML = '<a href="javascript:decrease('+rid+','+start_pcs+');" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage(\'quantityB_'+ROOT_URL+'\',\'\',\''+ROOT_URL+'/design/stular_pcs_left_black.png\',1)"><img src="'+ROOT_URL+'/design/stular_pcs_left_blue.png" alt="Štular plus" name="quantityB_'+ROOT_URL+'" width="8" height="15" border="0" id="quantityB_'+ROOT_URL+'" /></a>';
	}
	else
	{
		document.getElementById("pcs_left_"+rid).innerHTML = '<img src="'+ROOT_URL+'/design/stular_pcs_left_gray.png" width="8" height="15" alt="Štular plus" />';
	}
}

function checkPCSMoreThenZero(rid, start_pcs)
{
	if(document.getElementById("quantity_"+rid).value < start_pcs)
	{
		alert("OPOZIRILO: Količina je lahko najmanj "+start_pcs+"!");
		document.getElementById("quantity_"+rid).value = start_pcs;
	}
	if(document.getElementById("quantity_"+rid).value % start_pcs != 0)
	{
		alert("OPOZIRILO: Količina se lahko zvišuje in zmanšuje po "+start_pcs+"!");
		document.getElementById("quantity_"+rid).value = document.getElementById("quantity_"+rid).value - (document.getElementById("quantity_"+rid).value % start_pcs);
	}
	if(document.getElementById("fmCart") != null)
			document.fmCart.submit();
}

function SameShipping()
{
	if(document.getElementById("same").checked)
	{
		document.fmRegister.s_name.value = document.fmRegister.name.value;
		document.getElementById("s_name").readOnly = true;
		document.getElementById("s_name").className = "loginBottomFrameInputReadOnly";
		document.fmRegister.s_mail.value = document.fmRegister.mail.value;
		document.getElementById("s_mail").readOnly = true;
		document.getElementById("s_mail").className = "loginBottomFrameInputReadOnly";
		document.fmRegister.s_phone.value = document.fmRegister.phone.value;
		document.getElementById("s_phone").readOnly = true;
		document.getElementById("s_phone").className = "loginBottomFrameInputReadOnly";
		document.fmRegister.s_company.value = document.fmRegister.company.value;
		document.getElementById("s_company").readOnly = true;
		document.getElementById("s_company").className = "loginBottomFrameInputReadOnly";
		document.fmRegister.s_street.value = document.fmRegister.street.value;
		document.getElementById("s_street").readOnly = true;
		document.getElementById("s_street").className = "loginBottomFrameInputReadOnly";
		document.fmRegister.s_city.value = document.fmRegister.city.value;
		document.getElementById("s_city").readOnly = true;
		document.getElementById("s_city").className = "loginBottomFrameInputReadOnly";
		document.fmRegister.s_country.value = document.fmRegister.country.value;
		document.getElementById("s_country").readOnly = true;
		document.getElementById("s_country").className = "loginBottomFrameInputReadOnly";
	}
	else
	{
		document.getElementById("s_name").readOnly = false;
		document.getElementById("s_name").className = "loginBottomFrameInput";
		document.getElementById("s_mail").readOnly = false;
		document.getElementById("s_mail").className = "loginBottomFrameInput";
		document.getElementById("s_phone").readOnly = false;
		document.getElementById("s_phone").className = "loginBottomFrameInput";
		document.getElementById("s_company").readOnly = false;
		document.getElementById("s_company").className = "loginBottomFrameInput";
		document.getElementById("s_street").readOnly = false;
		document.getElementById("s_street").className = "loginBottomFrameInput";
		document.getElementById("s_city").readOnly = false;
		document.getElementById("s_city").className = "loginBottomFrameInput";
		document.getElementById("s_country").readOnly = false;
		document.getElementById("s_country").className = "loginBottomFrameInput";
	}
}

function BillingToShipping()
{
	document.fmRegister.s_name.value = document.fmRegister.name.value;
	document.fmRegister.s_mail.value = document.fmRegister.mail.value;
	document.fmRegister.s_phone.value = document.fmRegister.phone.value;
	document.fmRegister.s_company.value = document.fmRegister.company.value;
	document.fmRegister.s_street.value = document.fmRegister.street.value;
	document.fmRegister.s_city.value = document.fmRegister.city.value;
	document.fmRegister.s_country.value = document.fmRegister.country.value;
}

function redirect(url) {
	window.location = url;
}

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
