/* -----------------------------------------------------------------------------------------
   $Id: jscript_ajax_cart.js 899 2007-06-30 20:14:56 VaM $   

   VaM Shop - open source ecommerce solution
   http://vamshop.ru
   http://vamshop.com

   Copyright (c) 2007 VaM Shop
   -----------------------------------------------------------------------------------------
   based on: 
   (c) 2006	 Andrew Weretennikoff (ajax_sc.js,v 1.1 2007/03/17); medreces@yandex.ru 

   Released under the GNU General Public License
   ---------------------------------------------------------------------------------------*/

SHOW_ADDED = 1; // set 0 if you no need show
Offset_X = -10;
Offset_Y = -30;

function addHandler(object, event, handler) { // Thanks xpoint.ru!
  if (typeof object.addEventListener != 'undefined')
    object.addEventListener(event, handler, false);
  else if (typeof object.attachEvent != 'undefined')
    object.attachEvent('on' + event, handler);
  else {
    var handlersProp = '_handlerStack_' + event;
    var eventProp = 'on' + event;
    if (typeof object[handlersProp] == 'undefined') {
      object[handlersProp] = [];
      if (typeof object[eventProp] != 'undefined')
        object[handlersProp].push(object[eventProp]);
      object[eventProp] = function(e) {
        var ret = true;
        for (var i = 0; ret != false && i < object[handlersProp].length; i++)
          ret = object[handlersProp][i](e);
        return ret;
    } }
    object[handlersProp].push(handler);
} }
function removeHandler(object, event, handler) { // Thanks xpoint.ru!
  if (typeof object.removeEventListener != 'undefined')
    object.removeEventListener(event, handler, false);
  else if (typeof object.detachEvent != 'undefined')
    object.detachEvent('on' + event, handler);
  else {
    var handlersProp = '_handlerStack_' + event;
    if (typeof object[handlersProp] != 'undefined') {
      for (var i = 0; i < object[handlersProp].length; i++) {
        if (object[handlersProp][i] == handler) {
          object[handlersProp].splice(i, 1);
          return;
} } } } }

var x, y;
var loadingImage = new Image();
var okImage = new Image();
loadingImage.src = "images/loading.gif";
okImage.src = "images/ok.gif";

if (window.opera || (navigator.userAgent.indexOf('MSIE') > -1)) { //IE + Opera
  getM_x = function () { return event.pageX || (event.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft)) || 0;}
  getM_y = function () { return event.pageY || (event.clientY + (document.documentElement.scrollTop || document.body.scrollTop)) || 0;}
} else { // Mozilla
  addHandler(document, 'mousemove', function(e) {
    x = e.pageX;
    y = e.pageY;
  });
  getM_x = function () { return x; }
  getM_y = function () { return y; }
}

function showOk() {
  var imgLoading = document.getElementById("_loading_");
  with (imgLoading) {
    src = okImage.src;
    style.visibility = "visible";
} }


function showOk2(txt) {

	try { 
		var dt=txt.match(/info=;([^;]*);([^;]*);([^;]*);([^;]*)/)[0].split(';');
	}	catch (exception) {
		return;
	};

	
	var imgLoading = document.getElementById("_loading2_");

	with (imgLoading) {

	ttt='<div class="product-notification-container cm-auto-hide" id="notification_d807fc4a494c3e351b95b9ba4259451b__1"><div class="w-shadow"></div><div class="e-shadow"></div><div class="nw-shadow"></div><div class="ne-shadow"></div><div class="sw-shadow"></div><div class="se-shadow"></div><div class="n-shadow"></div><div class="popupbox-closer" onClick="hideOk2()"><img src="templates/vamshop/images/close_popupbox.png" class="cm-notification-close" title="Закрыть" alt="Закрыть" /></div><div class="product-notification"><h1>Добавлено в Вашу корзину</h1>'+
'<div class="notification-body"><ul><li><a href="'+dt[4]+'">'+dt[3]+'</a></li><li><strong class="valign">'+dt[1]+'</strong>&nbsp;x&nbsp;<span class="none">руб.</span><span id="sec_price_327820714" class="none">'+dt[2]+'</span></li>'+
'</ul></div><div class="clear"><div class="float-left">'+
'	<span class="button" ><a class="cm-notification-close" onClick="hideOk2()">Продолжить покупки</a></span>'+
'</div><div class="float-right">'+
'	<span class="button-action" ><a href="checkout_shipping.php">Оформить заказ</a></span>'+
'</div></div>'+
'</div><div class="s-shadow"></div></div>';

	imgLoading.innerHTML=ttt
		
    style.visibility = "visible";
    setTimeout( "hideOk2()", 100000000);
	setOpacity();

} }

function hideOk() {
  if(document.getElementById("_loading_")) document.getElementById('_loading_').style.visibility = "hidden";
  removeHandler(document, 'mousemove', hideOk);
}

function hideOk2() {
  if(document.getElementById("_loading2_")) document.getElementById('_loading2_').style.visibility = "hidden";
//  removeHandler(document, 'mousemove', hideOk);
	removeOpacity()
}

function showLoading() {
  var imgLoading = document.getElementById("_loading_");
  if(!imgLoading) {
    imgLoading = document.createElement("img");
    with(imgLoading) {
      id = "_loading_";
      style.position = "absolute";
      style.visibility = "hidden";
    }
    document.body.appendChild(imgLoading);
  }
  with(imgLoading) {
    src = loadingImage.src;
    style.left = (getM_x() + Offset_X) + "px";
    style.top = (getM_y() + Offset_Y) + "px";
    style.visibility = "visible";
} }



function showLoading2() {

  var ms = document.getElementById("mainshadow");
  if(!ms) {

	ms = document.createElement("div");
	with(ms) {
      id = "mainshadow";
       style.width = "1px";
       style.height = "1px";
    }
    document.body.appendChild(ms);
 }


  var imgLoading = document.getElementById("_loading2_");
  if(!imgLoading) {

    imgLoading = document.createElement("div");
	with(imgLoading) {
      id = "_loading2_";
      style.position = "absolute";
      style.visibility = "hidden";
    }
    document.body.appendChild(imgLoading);
  }
  with(imgLoading) {
//    src = loadingImage.src;
//    style.left = (getM_x() + Offset_X) + "px";
//    style.top = (getM_y() + Offset_Y) + "px";
	sz=getPageSize()
	style.left =(getBodyScrollLeft()+ sz[2]/2) +"px";
	style.top =(getBodyScrollTop()+ sz[3]/2) +"px";
    style.visibility = "visible";
} }

function hideLoading() {
  if(document.getElementById("_loading_")) document.getElementById("_loading_").style.visibility = "hidden";
}

function hideLoading2() {
  if(document.getElementById("_loading2_")) document.getElementById("_loading2_").style.visibility = "hidden";
}

function doBuyNowGet( link ) {
  showLoading2();
  var reqAddCart = new JsHttpRequest();
  reqAddCart.onreadystatechange = function() {
    if (reqAddCart.readyState == 4) {

      if (reqAddCart.responseJS) {
        document.location.href = reqAddCart.responseJS.ajax_redirect;
        return;
      }
      else {
        document.getElementById('divShoppingCart').innerHTML = ''+(reqAddCart.responseText||'')+'';
		if ( SHOW_ADDED ) {
          showOk2(reqAddCart.responseText);
//          timerID = setTimeout( "addHandler(document, \'mousemove\', hideOk)", 500);
        }
      }
    }
  }

  reqAddCart.caching = false;
  reqAddCart.open('GET', link, true);
  reqAddCart.send(null);
  return false;
}

function doAddProduct(form) {

  showLoading2();
  var reqAddCart = new JsHttpRequest();
  reqAddCart.onreadystatechange = function() {
    if (reqAddCart.readyState == 4) {
      if (reqAddCart.responseJS) {
		document.location.href = reqAddCart.responseJS.ajax_redirect;
        return;
      }
      else {
        document.getElementById('divShoppingCart').innerHTML = ''+(reqAddCart.responseText||'')+''
        if ( SHOW_ADDED ) {
          showOk2(reqAddCart.responseText);
//          timerID = setTimeout( "addHandler(document, \'mousemove\', hideOk)", 5000);
        }
      }
    }
  }
// собираем все элементы формы:
  var senddata = new Object();
  var fe = form.elements;
  for(var i=0 ; i<fe.length ; i++) {
    if ( fe[i].type=="radio" || fe[i].type=="checkbox" ) {
      if ( fe[i].checked ) senddata[fe[i].name] = fe[i].value;
    } else {
      senddata[fe[i].name] = fe[i].value;
    }
  }
  var url = 'ajax_shopping_cart.php?' + ( senddata.products_id ? 'products_id='+senddata.products_id+'&' : "" ) + ( senddata.products_qty ? '&products_qty='+senddata.products_qty+'&' : "" ) + ( senddata.id ? '&id='+senddata.id+'&' : "" ) + 'action=add_product';
  url += '&report=1';

  reqAddCart.caching = false;
  reqAddCart.open( 'GET', url, true);
  reqAddCart.send( senddata );
  return false;
}


function doDelProduct(form) {
  showLoading();
  var reqAddCart = new JsHttpRequest();
  reqAddCart.onreadystatechange = function() {
    if (reqAddCart.readyState == 4) {
      if (reqAddCart.responseJS) {
        document.location.href = reqAddCart.responseJS.ajax_redirect;
        return;
      }
      else {
        document.getElementById('divShoppingCart').innerHTML = ''+(reqAddCart.responseText||'')+''
        if ( SHOW_ADDED ) {
          showOk();
          timerID = setTimeout( "addHandler(document, \'mousemove\', hideOk)", 500);
        }
      }
    }
  }

  var senddata = new Object();
  var fe = form.elements;
  for(var i=0 ; i<fe.length ; i++) {

    if ( fe[i].type=="hidden" ) {
      senddata[fe[i].name] = fe[i].value;
    }
  }
  var url = 'ajax_shopping_cart.php?' + ( senddata["products_id[]"] ? 'products_id[]='+senddata["products_id[]"]+'&' : "" ) + ( senddata["old_qty[]"] ? '&old_qty[]='+senddata["old_qty[]"]+'&' : "" ) + ( senddata["cart_quantity[]"] ? '&cart_quantity[]='+senddata["cart_quantity[]"]+'&' : "" ) + ( senddata["cart_delete[]"] ? '&cart_delete[]='+senddata["cart_delete[]"]+'&' : "" ) + 'action=update_product';

  reqAddCart.caching = false;
  reqAddCart.open( 'GET', url, true);
  reqAddCart.send( senddata );
  return false;
}



function ReviewsPage(page,products_id) {

  showLoading();
  var reqAddCart = new JsHttpRequest();
  reqAddCart.onreadystatechange = function() {
    if (reqAddCart.readyState == 4) {
      if (reqAddCart.responseJS) {
        document.location.href = reqAddCart.responseJS.ajax_redirect;
        return;
      }
      else {
        document.getElementById('divReviews').innerHTML = ''+(reqAddCart.responseText||'')+''
		
        if ( SHOW_ADDED ) {
          showOk();
          timerID = setTimeout( "addHandler(document, \'mousemove\', hideOk)", 500);
        }
      }
    }
  }

  var senddata = new Object();
  var url = 'ajax_reviews_page.php?page=' + page + '&products_id=' + products_id + '&type=ajax';

  reqAddCart.caching = false;
  reqAddCart.open( 'GET', url, true);
  reqAddCart.send( senddata );
  return false;
}

/* --------     */

function getBodyScrollTop()
{
  return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}

function getBodyScrollLeft()
{
  return self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
}

function getClientWidth()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

function getClientHeight()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}


    function setOpacity() {

	   var testObj = document.getElementById('mainshadow');

		var sz=getPageSize()
	   testObj.style.width = sz[0] + "px" 
	   testObj.style.height = sz[1] + "px" 

       testObj.style.display = "block";

	   testObj.style.opacity = 0.4;
       testObj.style.filter = 'alpha(opacity=40)';
    }


    function removeOpacity() {

       var testObj = document.getElementById('mainshadow');

	   testObj.style.opacity = 0;
       testObj.style.filter = 'alpha(opacity=0)';
          testObj.style.display = "none";
    }



function  getPageSize(){
       var xScroll, yScroll;

       if (window.innerHeight && window.scrollMaxY) {
               xScroll = document.body.scrollWidth;
               yScroll = window.innerHeight + window.scrollMaxY;
       } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
               xScroll = document.body.scrollWidth;
               yScroll = document.body.scrollHeight;
       } else if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight){ // Explorer 6 strict mode
               xScroll = document.documentElement.scrollWidth;
               yScroll = document.documentElement.scrollHeight;
       } else { // Explorer Mac...would also work in Mozilla and Safari
               xScroll = document.body.offsetWidth;
               yScroll = document.body.offsetHeight;
       }

       var windowWidth, windowHeight;
       if (self.innerHeight) { // all except Explorer
               windowWidth = self.innerWidth;
               windowHeight = self.innerHeight;
       } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
               windowWidth = document.documentElement.clientWidth;
               windowHeight = document.documentElement.clientHeight;
       } else if (document.body) { // other Explorers
               windowWidth = document.body.clientWidth;
               windowHeight = document.body.clientHeight;
       }


       // for small pages with total height less then height of the viewport
       if(yScroll < windowHeight){
               pageHeight = windowHeight;
       } else {
               pageHeight = yScroll;
       }

       // for small pages with total width less then width of the viewport
       if(xScroll < windowWidth){
               pageWidth = windowWidth;
       } else {
               pageWidth = xScroll;
       }

       return [pageWidth,pageHeight,windowWidth,windowHeight];
}

