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);   
			}
	
			inner.innerHTML = '<a href="/browse/cart/" style="color: #b75d05;  text-decoration: none;" onMouseOver=\'this.style.color = \"#de740d\"; document.addcart.src=\"/htdocs/images/default/shopping_cart_hover.png\" \' onMouseOut=\'this.style.color = \"#b75d05\"; document.addcart.src=\"/htdocs/images/default/shopping_cart.png\" \'><img name="addcart" src="/htdocs/images/default/shopping_cart.png" style="border: none; float:right; ">&nbsp;&nbsp;<br/><div style="border: none; float:right; width:230px; text-align:right; cursor:  pointer;" >Маркирани стоки до момента</div> <div style="clear:both;"></div></a>';			
        }
	}
	xmlHttp.send(params);
}






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

	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 = "<a href='/browse/cart'>"+cart_lang+"</a>";
		  
        }
	}
	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", "/admin69/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);
}
