/* Create a new XMLHttpRequest object to talk to the Web server */
var xmlHttp = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
    try {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e2) {
      xmlHttp = false;
    }
  }
@end @*/

if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
  xmlHttp = new XMLHttpRequest();
}

var mouseX;
var mouseY;

function getCoord( event ) {
   mouseX = event.pageX;
   mouseY = event.pageY;
}

if( document.all ) { 
 } else if( document.getElementById ) {
  document.captureEvents( Event.MOUSEMOVE );
  document.onmousemove = getCoord;
}



/*Put Item into basket*/
var c_product_price;
var c_product_quantity;
var c_product_tprice;
var c_product_name;
var c_section_name;
var c_product_id_remove;

function PutItemIntoCart(type, id, price, product_pack, product_name, section_name, quantity){  
  if(quantity.search('[0-9]+') == -1 || quantity == '0') quantity = 1;
  if(!quantity) quantity = 1;                        
  if(!price) price = 0;  
  if(!product_name) product_name = '';
  if(!section_name) section_name = '';

  c_product_id = id;
  c_product_quantity = quantity;
  c_product_price = price;
  c_product_name = product_name;
  c_section_name = section_name;
  c_product_id_remove = null;

//  price = quantity * price;
  c_product_tprice = price;


  // Build the URL to connect to
  if(product_pack)
	var url = "/dinamic/st_cart.php?action=putItem&type="+type+"&id="+id+'&price='+price+'&product_pack='+product_pack+"&name="+product_name+"&section_name="+section_name+"&quantity="+quantity;
  else
	var url = "/dinamic/st_cart.php?action=putItem&type="+type+"&id="+id+'&price='+price+"&name="+product_name+"&section_name="+section_name+"&quantity="+quantity;
  xmlHttp.open("GET", url, true);
  xmlHttp.onreadystatechange = updateCart;
  xmlHttp.send(null);
}

/*Remove Item From basket*/
function RemoveItemFromCart(type, id){
  // Build the URL to connect to
  var url = "/dinamic/st_cart.php?action=removeItem&type="+type+"&id="+id;
  c_product_id = null;
  c_product_id_remove = id;
  xmlHttp.open("GET", url, true);
  xmlHttp.onreadystatechange = updateCart;
  xmlHttp.send(null);
}


function updateCart() {
  if (xmlHttp.readyState == 4) {
      var response = xmlHttp.responseText;
	  if(response == '0') {alert('Товар уже в корзине.');return;}
 	  response = response.split("=");
 	  try{
    	  document.getElementById("cartStat_count").innerHTML = response[0]+' тов.';
	      document.getElementById("cartStat_price").innerHTML = response[1]+' у.е.';
	  }catch (e) {}
    }
}


function RecalculateTPrice(price, quantity){
	
	try{
	  if(quantity.search('[0-9]+') == -1 || quantity == '0') quantity = 1;
		document.getElementById('tprice').innerHTML = price*quantity;
		document.getElementById('tprice').innerHTML += ' у.е.';
	}catch (e) {}
}

function RecalculateTPrice2(price, quantity, caption_id){
	
	try{
	  if(quantity.search('[0-9]+') == -1) quantity = 1;
		document.getElementById(caption_id).innerHTML = price*quantity;
		document.getElementById(caption_id).innerHTML += ' у.е.';
	}catch (e) {}
}



/*Wish Lists START*/
///////////////////////////////////////////////////////////////
function ShowWishlistTypes(id, price_num, product_status){

  if( document.all ) {
	var XXX = event.clientX+document.body.scrollLeft;
	var YYY = event.clientY+document.body.scrollTop;
  }
  else if ( document.getElementById ) {
	var XXX = mouseX;
	var YYY = mouseY;
  }


  var replaceTo = document.getElementById("replaceTo");
  replaceTo.style.top = YYY+'px';
  replaceTo.style.left = XXX+'px';
  replaceTo.innerHTML = '&nbsp;Загружаю...';
  replaceTo.style.display = '';

  // Build the URL to connect to
  var url = "/dinamic/st_cart.php?action=getWLTypes&id="+id+"&price_num="+price_num+"&product_status="+product_status;
  xmlHttp.open("GET", url, true);
  xmlHttp.onreadystatechange = updateShowWishlistTypes;
  xmlHttp.send(null);
}


function ShowWishlistTypes_1(id, price_num, product_status){

  if( document.all ) {
	var XXX = event.clientX+document.body.scrollLeft;
	var YYY = event.clientY+document.body.scrollTop;
  }
  else if ( document.getElementById ) {
	var XXX = mouseX;
	var YYY = mouseY;
  }

  var replaceTo = document.getElementById("replaceTo");
  replaceTo.style.top = YYY+'px';
  replaceTo.style.left = XXX-300+'px';
  replaceTo.innerHTML = '&nbsp;Загружаю...';
  replaceTo.style.display = '';

  // Build the URL to connect to
  var url = "/dinamic/st_cart.php?action=getWLTypes_1&id="+id+"&price_num="+price_num+"&product_status="+product_status;
  xmlHttp.open("GET", url, true);
  xmlHttp.onreadystatechange = updateShowWishlistTypes;
  xmlHttp.send(null);
}

function updateShowWishlistTypes() {
  if (xmlHttp.readyState == 4) {
    var response = xmlHttp.responseText;
	document.getElementById('replaceTo').innerHTML = response;
  }
}

function CloseSWT() {
	document.getElementById("replaceTo").style.display='none';
}



//Put Item into WishList
function PutItemIntoWishList(id, type, price_num, product_status){
  // Build the URL to connect to
  var url = "/dinamic/st_cart.php?action=putItemWL&id="+id+"&type="+type+"&price_num="+price_num+"&product_status="+product_status;
  xmlHttp.open("GET", url, true);
  xmlHttp.onreadystatechange = updateNull;
  xmlHttp.send(null);
}

//Put Item into WishList CHANGE PRICE
function PutItemIntoWishList_1(id, type, price_num, product_status, change_to, change_on){
  // Build the URL to connect to
  var url = "/dinamic/st_cart.php?action=putItemWL&id="+id+"&type="+type+"&price_num="+price_num+"&product_status="+product_status+"&change_to="+change_to+"&change_on="+change_on;
  xmlHttp.open("GET", url, true);
  xmlHttp.onreadystatechange = updateNull;
  xmlHttp.send(null);
}


/*Remove Item From WishList*/
function RemoveItemFromWL(id){
  // Build the URL to connect to
  var url = "/dinamic/st_cart.php?action=removeItemFromWL&id="+id;
  xmlHttp.open("GET", url, true);
  xmlHttp.onreadystatechange = updatePage;
  xmlHttp.send(null);
}

function updatePage() {
  if (xmlHttp.readyState == 4) {
    var response = xmlHttp.responseText;
	history.go(0);
  }
}

function updateNull() {
  if (xmlHttp.readyState == 4) {
  }
}
/*Wish Lists END*/

function SetZayavkaStatus(id, status){
  // Build the URL to connect to
  var url = "/admin/st_zayavka?action=setZayavkaStatus&id="+id+"&set_status="+status;
  xmlHttp.open("GET", url, true);
//  xmlHttp.onreadystatechange = updatePage;
  xmlHttp.send(null);
}
