var ESHOP_URL = "http://www.lip-poljcane.si/spletna-trgovina/";
var xmlHttp;

//****** hide loading image *********//
function hideLoadingImg() {
	document.getElementById('loadingImg').display = "none";
}

//****** change product main image src *********//
function showProdImg(imgURLm,imgURLl)
{
	//alert(upload_path);
	//alert(imgURL);
	//alert(ROOT_URL + upload_path + imgURL);
	document.getElementById('mainImg').src = ROOT_URL + upload_path + imgURLm;
	document.getElementById('mainImgHref').href = ROOT_URL + upload_path + imgURLl;
	document.getElementById('mainImgEnlargeHref').href = ROOT_URL + upload_path + imgURLl;
}

//****** build product cart data *********//
function buildProdCartData(prodId,varElentsIds,propId,valId) {
	//alert(prodId);
	//alert(varElentsIds);
	//alert(propId);
	//alert(valId);
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
        alert (lang_HTTP_request_error)
        return
    }
    
	//reset
	if (valId == 0) {
	    pid = prodId;
	    resetProdCartData(pid);
	}
	
	//else
	else {
		//create array from varElentsIds
		var varElentsIds_array = new Array();
		varElentsIds_array = varElentsIds.split(",");
		//alert(varElentsIds_array.length);
		//alert(varElentsIds_array.join("\n"));
		
		//check all drop downs values
		var ddValuesIds = ""; //dd vaules IDs for php
		var first = 1; //dd vaules IDs for php
		for (var i=0;i<=varElentsIds_array.length - 1; i++)
		{
			//alert("Loop: " + i);
			var ddId = "variationElentID-" + varElentsIds_array[i];
			//alert(ddId);
			var ddValueId = document.getElementById(ddId).value;
			//alert(ddValueId);
			if (first == 1) {
				ddValuesIds = ddValueId;
				first = 0;
			}
			else {
				ddValuesIds = ddValuesIds + "," + ddValueId;
			}
		}
		//alert(ddValuesIds);
		
	    pid = prodId;
	    veids = varElentsIds;
	    valids = ddValuesIds;
	    ppid = propId;
	    pvid = valId;
	    
	    var url=ESHOP_URL+"ajax/build_product_cart_data.php"
	    url=url+"?pid="+pid
	    url=url+"&veids="+veids
	    url=url+"&valids="+valids
	    url=url+"&ppid="+ppid
	    url=url+"&pvid="+pvid
	    url=url+"&sid="+Math.random()
	    
	    xmlHttp.onreadystatechange = stateChanged_prodcartdata
	    xmlHttp.open("GET",url,true)
	    xmlHttp.send(null)
    }
}

//****** reset product variations dropdowns *********//
function resetProdCartData(prodId) {
	//alert(prodId);
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
        alert (lang_HTTP_request_error)
        return
    }
    
    pid = prodId;
    
    var url=ESHOP_URL+"ajax/build_product_cart_data.php"
    url=url+"?pid="+pid
    url=url+"&reset=1"
    url=url+"&sid="+Math.random()
    
    xmlHttp.onreadystatechange = stateChanged_prodcartdata
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
}

//****** add product to cart *********//
function addToCart(productVariationID,quantity) {
    //alert(productVariationID);
    //alert(quantity);
    
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
        alert (lang_HTTP_request_error)
        return
    }
    if (!(quantity)) quantity = 1;
    //alert(quantity);
    
    var url=ESHOP_URL+"ajax/product_add_to_cart.php"
    url=url+"?product_variation_id="+productVariationID
    url=url+"&quantity="+quantity
    url=url+"&sid="+Math.random()
    
    //xmlHttp.onreadystatechange=showShoppingList
    xmlHttp.open("GET",url,false)
    xmlHttp.send(null)
    
    var url=ESHOP_URL+"kosarica/ajax/rolldown.php"
    
    xmlHttp.open("GET",url,false)
    xmlHttp.send(null)
    document.getElementById("cartdd").innerHTML = xmlHttp.responseText
}

//****** add product to wishlist *********//
function addToWishlist(productID) {
    //alert(productID);
    
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
        alert (lang_HTTP_request_error)
        return
    }
    if (productID > 0) {
	    var url=ESHOP_URL+"ajax/product_add_to_wishlist.php"
	    url=url+"?product_id="+productID
	    url=url+"&sid="+Math.random()
	    
	    xmlHttp.onreadystatechange = stateChanged_add_to_wishlist
	    xmlHttp.open("GET",url,true)
	    xmlHttp.send(null)
	}
}

//****** add product review *********//
function showProductReviewForm(productID) {
    //alert(productID);
    
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
        alert (lang_HTTP_request_error)
        return
    }
    
    if (productID > 0) {
	    var url=ESHOP_URL+"ajax/product_add_review.php"
	    url=url+"?pid="+productID
	    url=url+"&sid="+Math.random()
	    
	    xmlHttp.onreadystatechange = stateChanged_form_product_review
	    xmlHttp.open("GET",url,true)
	    xmlHttp.send(null)
	}
}

function buildProductReview() {
	if (document.getElementById("pass_input_shown").value == 1)
		var pass = document.getElementById("password").value;
	else
		var pass = "";
	
	var post_data = "nlAction=execute" +
					"&pass_input_shown=" + encodeURI( document.getElementById("pass_input_shown").value ) +
					"&product_id=" + encodeURI( document.getElementById("product_id").value ) +
					"&author=" + encodeURI( document.getElementById("author").value ) +
					"&email=" + encodeURI( document.getElementById("email").value ) +
					"&password=" + pass +
					"&review=" + encodeURI( document.getElementById("review").value ) +
					"&rating=" + encodeURI( document.getElementById("rating").value ) +
					"&recommend=" + encodeURI( document.getElementById("recommend").checked );
    //alert(post_data);
    updateProductReview(post_data);
}

function buildProductReview_UserLoggedIn() {
	var post_data = "nlAction=execute" +
					"&product_id=" + encodeURI( document.getElementById("product_id").value ) +
					"&review=" + encodeURI( document.getElementById("review").value ) +
					"&rating=" + encodeURI( document.getElementById("rating").value ) +
					"&recommend=" + encodeURI( document.getElementById("recommend").checked );
    //alert(post_data);
    updateProductReview(post_data);
}

function updateProductReview(post_data)
{
    //alert(post_data)
	
	xmlHttpReview=GetXmlHttpObject()
	if (xmlHttpReview==null)
	{
		alert (lang_HTTP_request_error)
		return
	}
	
    var url=ESHOP_URL+"ajax/product_add_review.php"
    //alert(url)
	xmlHttpReview.onreadystatechange = stateChanged_form_product_review_submit
	xmlHttpReview.open("POST",url,true)
	xmlHttpReview.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttpReview.setRequestHeader("Content-length", post_data.length);
    xmlHttpReview.setRequestHeader("Connection", "close");
	xmlHttpReview.send(post_data)
}



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

		}
	}
}

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

function stateChanged_form_product_review_submit()
{
	if (xmlHttpReview.readyState==4 || xmlHttpReview.readyState=="complete")
	{
		if (xmlHttpReview.responseText.length > 0)
		{
			document.getElementById("form_product_review").innerHTML = xmlHttpReview.responseText;
		}
	}
}

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

function GetXmlHttpObject()
{
	var objXMLHttp=null
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}

