/* Create a new XMLHttpRequest object to talk to the Web server */
var cartflag=false;
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, upd){ 
	//alert(quantity	);
  if(quantity.search('[0-9]+') == -1 || quantity == '0') quantity = 1;
  if(upd === undefined) upd = 0;
  //if(upd.search('[0-9]+') == -1) upd = 0;
  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+'&upd='+upd;
  else
	var url = "/dinamic/st_cart.php?action=putItem&type="+type+"&id="+id+'&price='+price+"&name="+product_name+"&section_name="+section_name+"&quantity="+quantity+'&upd='+upd;
  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{
    	  $("#cartStat_count").html(response[0]);
		  $("#cartStat_price").html(response[1]);

	  }catch (e) {}
    }
}

/*Remove Item From basket*/
function RemoveItemFromCart(type, id, product_pack){
	if(product_pack === undefined || product_pack == 0)
		var pack = 'action=removeItem&type='+type+'&id='+id;
	else
		var pack ='action=removeItem&type='+type+'&id='+id+ '&product_pack='+product_pack;
	$.ajax({
		url:	'/dinamic/st_cart.php',
		data:	pack,
		type:	'get',
		success: function (msg) {
			msg = msg.split('=');
			$('#d_cart_count').html(msg[0]);
			$('#d_cart_total').html(msg[1]);
	    	$("#cartStat_count").html(msg[0]);
			$('#d_cart_product_'+id).hide('slow');
		}
	});
}

function RemoveItemFromSWCart(type, id, product_pack){
	cartflag=true;
	if(product_pack === undefined || product_pack == 0)
		var pack = 'action=removeItem&type='+type+'&id='+id;
	else
		var pack ='action=removeItem&type='+type+'&id='+id+ '&product_pack='+product_pack;
	$('show_cart').hide('slow');
	$.ajax({
		url:	'/dinamic/st_cart.php',
		data:	pack,
		type:	'get',
		success: function(msg){

			$.ajax({
				url:	'/dinamic/st_cart.php',
				data:	'action=show_CartPopUp',
				type:	'get',
				success: function (msg) {
					$('#show_cart').html(msg);
					//$('#show_cart').show('slow');
				}
				});

			$.ajax({
				url:	'/dinamic/st_cart.php',
				data:	'action=recalc',
				type:	'get',
				success: function (response) {
			 	  response = response.split("=");
				 	  try{
				    	  $("#cartStat_count").html(response[0]);
						  $("#cartStat_price").html(response[1]);

					  }catch (e) {}
				}
				});
			$('show_cart').show('slow');
			//$('.f-cart .mc-pop').css('display', 'block');		
/*
			$('tr[tag="d_cart_product_'+id+'_'+product_pack+'"]').each(function(){
				$(this).hide('slow');
			});
*/
		}
	});
}


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-300+'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 recalc_cart() {
	cartflag=true;
	var ids = '', flag = 0;
//	$('.hat .yWrap div.active .nCrt-1').css('display', 'none');
	for(i=0;i<$('#cart_count').val();i++){
		if(isNaN($('#cart_quantity_'+i).val())){
			flag=1;
			i=$('#cart_count').val();
		}
		ids = ids + '&id['+i+']='+$('#cart_id_'+i).val()+'&quantity['+i+']='+$('#cart_quantity_'+i).val();
	}

	if(flag){
		alert('введите корректное число в поле количество');
		//$('.hat .yWrap div.active .nCrt-1').css('display', 'block');		
		return;
	}


	$.ajax({
		url:	'/dinamic/st_cart.php',
		data:	'action=show_CartPopUp' + ids,
		type:	'get',
		success: function (msg) {
			$('#show_cart').html(msg);
			//$('.hat .yWrap div.active .nCrt-1').css('display', 'block');
		}
	});
			$.ajax({
				url:	'/dinamic/st_cart.php',
				data:	'action=recalc',
				type:	'get',
				success: function (response) {
			 	  response = response.split("=");
				 	  try{
				    	  $("#cartStat_count").html(response[0]);
						  $("#cartStat_price").html(response[1]);

					  }catch (e) {}
				}
				});

//	cartflag=false;
	//$('.hat .yWrap div.active .nCrt-1').css('display', 'block');		
}

