function GetXmlHttpObject()
{
  var xmlHttp=null;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
  return xmlHttp;
}


function updateSideCart(){

	var xmlHttp = GetXmlHttpObject();
	
    var params = "";
    xmlHttp.open("POST", "/browse/cart/action/updateSideCart/", true);

    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
	  xmlHttp.onreadystatechange = function() { 
 	    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			var response = new String(xmlHttp.responseText);
			document.getElementById('sideCart').innerHTML = response;
		  
        }
	}
	xmlHttp.send(params);
}

function updateProducQuantytInCart(idop, idproduct){

	var xmlHttp = GetXmlHttpObject();
	quanty = document.getElementById('opQuanty_' + idproduct).value;
    var params = "idop=" + idop + "&quanty=" + quanty;
    xmlHttp.open("POST", "/browse/cart/action/updateProductQuanty/", true);

    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
	  xmlHttp.onreadystatechange = function() { 
 	    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			var response = new String(xmlHttp.responseText);
			document.getElementById(idproduct).innerHTML = response;
		  
        }
	}
	xmlHttp.send(params);
}

function addToCart(idproduct, idprice){

	var xmlHttp = GetXmlHttpObject();
    var params = "idproduct=" + idproduct + "&idprice=" + idprice;
    xmlHttp.open("POST", "/browse/cart/action/add/", true);

    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
	  xmlHttp.onreadystatechange = function() { 
 	    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			var response = new String(xmlHttp.responseText);
			document.getElementById(idproduct).innerHTML = response;
		  
        }
	}
	xmlHttp.send(params);
}

function jUpdate(idproduct, idprice, price, money, cntid){

	var xmlHttp = GetXmlHttpObject();
    var params = "idproduct=" + idproduct + "&idprice=" + idprice + "&split=1";
    xmlHttp.open("POST", "/browse/cart/action/add/", true);

    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
	  xmlHttp.onreadystatechange = function() { 
 	    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			var response = new String(xmlHttp.responseText);
			//document.getElementById(idproduct).innerHTML = response;		  
			var containertxt = response.split("|");

			var cnt = document.getElementById(cntid).value;
			var idop = containertxt[0];
			var idorder = containertxt[1];
			var inner = document.getElementById(idproduct);
	
			if(cnt > 1){
		
				changeProductCount(idop, idorder, cnt);
		
				for(i=1; i<=cnt; i++){
					updateSidebarCart('1', price, money);
				}
		
			} else {
				updateSidebarCart('1', price, money);   
			}
			
			var top_cnt = parseInt(document.getElementById("top_cart_products").innerHTML);
			top_cnt += parseInt(cnt);
			document.getElementById("top_cart_products").innerHTML = top_cnt;
			
			inner.innerHTML = '<div style="margin-left: 15px; "><a href="/browse/cart/" style="color: #4b66a0; text-decoration: none;" onMouseOver=\'this.style.color = \"#3e5ea1\"; document.addcart.src=\"/htdocs/images/default/shopping_cart_hover.png\" \' onMouseOut=\'this.style.color = \"#1844A1\"; document.addcart.src=\"/htdocs/images/default/shopping_cart.png\" \'><img name="addcart" src="/htdocs/images/default/shopping_cart.png" style="border: none;">&nbsp;&nbsp;<br><div>Виж кошницата</div></a></div>';			
        }
	}
	xmlHttp.send(params);
}




function addToCart1(idproduct, idprice, divname, cnt){

	var xmlHttp = GetXmlHttpObject();
    var params = "idproduct=" + idproduct + "&idprice=" + idprice;
    xmlHttp.open("POST", "/browse/cart/action/add/", true);

    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
	  xmlHttp.onreadystatechange = function() { 
 	    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			var response = new String(xmlHttp.responseText);
			document.getElementById(divname+"_"+idproduct).innerHTML = '<div style="margin-left: 15px;  font-size:12px; margin-bottom:4px;"><a href="/browse/cart/" style="color: #4b66a0; text-decoration: none;" onMouseOver=\'this.style.color = \"#3e5ea1\"; document.addcart.src=\"/htdocs/images/default/shoping_cart_1.png\" \' onMouseOut=\'this.style.color = \"#1844A1\"; document.addcart.src=\"/htdocs/images/default/shoping_cart_1.png\" \'><img name="addcart" src="/htdocs/images/default/shoping_cart_1.png" style="border: none;">&nbsp;&nbsp;<br><div>Виж кошницата</div></a></div>';			
                       
                       var top_cnt = parseInt(document.getElementById("top_cart_products").innerHTML);
			top_cnt += parseInt(cnt);
			document.getElementById("top_cart_products").innerHTML = top_cnt;
        }
	}
	xmlHttp.send(params);
}

function changeProductCount(idop, idorder, count){ 

	var xmlHttp = GetXmlHttpObject();
    var params = "idop=" + idop + "&idorder=" + idorder +"&count=" + count;
    xmlHttp.open("POST", "/browse/cart/action/count/", true);

    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
	  xmlHttp.onreadystatechange = function() { 
 	    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			var response = new String(xmlHttp.responseText);
			//document.getElementById("all_price").innerHTML = response;
        }
	}
	xmlHttp.send(params);
}

function changeMark(mark){ 

	var xmlHttp = GetXmlHttpObject();
    var params = "mark=" + mark;
    xmlHttp.open("POST", "/browse/mark/name/"+mark, true);

    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
	  xmlHttp.onreadystatechange = function() { 
 	    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			var response = new String(xmlHttp.responseText);
			document.getElementById("mark").innerHTML = response;
        }
	}
	xmlHttp.send(params);
}


function updateSidebarCart(addtotal, addprice, money_format){

	var total = document.getElementById('cart_total_input').value;
	var price = document.getElementById('cart_sum_input').value;
	var total_inner = document.getElementById('cart_total');
	var price_inner = document.getElementById('cart_sum');
	var newtotal;
	var newprice;
	
	newtotal = Number(total)+Number(addtotal);
	newprice = Number(price)+Number(addprice);
	newprice = Math.round(newprice*100) / 100;
	
	total_inner.innerHTML = '<input type="hidden" id="cart_total_input" name="cart_total_input" value="'+newtotal+'">'+newtotal+'';
	price_inner.innerHTML = '<input type="hidden" id="cart_sum_input" name="cart_sum_input" value="'+newprice+'">'+newprice+' '+money_format+'';
	
}

function emp(id){
	var val = document.getElementById(id).value;
	var bval = document.getElementById('b'+id).value;
	
	document.getElementById('b'+id).value = val;
	document.getElementById(id).value = '';
}

function re(id){
	
	var val = document.getElementById(id).value;
	var bval = document.getElementById('b'+id).value;
	
	if(val == '' || val == '0')
		document.getElementById(id).value = bval;
}




function openDialog(idproduct) {


   Dialog.confirm($('email_send').innerHTML, {className:"alphacube", width:400,
                                      okLabel: "Изпрати", cancelLabel: "Затвори",


                                       onOk:function(){
                                        var email = $('send_email').value;

                                         addEmailWaiting(email, idproduct);

                                       return true;

                                    }

                        });

}

function addEmailWaiting(email, idproduct){
     var xmlHttp = GetXmlHttpObject();

    var params = "idproduct="+ idproduct +"&email="+ email;
    xmlHttp.open("POST", "/browse/addEmailWaiting/", true);

    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {


    }
    }

    xmlHttp.send(params);
}


function changeStatusSend(id, status){
    var xmlHttp = GetXmlHttpObject();
        var params = "id=" + id +"&status="+ status;
        xmlHttp.open("POST", "/admin/products/action/changeStatusSend/", true);

    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
	  xmlHttp.onreadystatechange = function() {
 	    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {


		}
	}


	xmlHttp.send(params);
}


function openPopupWait(message,  idproduct){
    $('#waiting').modal({
		closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
		position: ["20%",],
		overlayId: 'confirm-overlay',
		containerId: 'confirm-container',
		onShow: function (dialog) {
			var modal = this;
                        $('.message', dialog.data[0]).append(message);


                        // if the user clicks "yes"
			$('.yes', dialog.data[0]).click(function () {
				// call the callback
                              var email = document.getElementById('input_email').value;
				addEmailWaiting(email, idproduct);
				// close the dialog
				modal.close(); // or $.modal.close();
			});
		}
	});

}



function addEmailWaiting(email, idproduct){

    var xmlHttp = GetXmlHttpObject();

    var params = "idproduct="+ idproduct +"&email="+ email;
    xmlHttp.open("POST", "/browse/addEmailWaiting/", true);

    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {


    }
    }

    xmlHttp.send(params);
}

