function ajaxUpdate(script,update,params,method,indicator){
    new Request({
      url:        script,
      method:     method,
      onRequest: function(request){
         if($(indicator)){
            $(indicator).setStyle('display','');
         }
      },
      evalScripts:  false,
      onComplete:   function(request){
         if($(indicator))
            $(indicator).setStyle('display','none');
      },
      onSuccess:  
         function(response) {
            if($(update)){
		            $(update).set('html', response);
		         }
        }
    }).send(params);
}



        		function aken(url,w,h){
        			var nam = url.substr(url.lastIndexOf("/") + 1);
        			nam = nam.substr(0, nam.indexOf("."));
        			nam = nam.replace(/\W+/g, '');
        			window.open(url, nam, 'scrollbars=no,status=no,resizable=no, width='+w+', height='+h+',screenX=10,screenY=10,top=10,left=10');
        		}
        		function aken2(url,w,h){
        			var nam = url.substr(url.lastIndexOf("/") + 1);
        			nam = nam.substr(0, nam.indexOf("."));
        			nam = nam.replace(/\W+/g, '');
        			window.open(url, nam ,'scrollbars=yes,status=no,resizable=no, width='+w+', height='+h+',screenX=10,screenY=10,top=10,left=10');
        		}

function clickclear(thisfield, defaulttext) {
if (thisfield.value == defaulttext) {
thisfield.value = "";
}
}
function clickrecall(thisfield, defaulttext) {
if (thisfield.value == "") {
thisfield.value = defaulttext;
}
}

function hide(div){
            $(div).setStyle('visibility','hidden');
         }

function getCursorCoords(event){
  /*
    gets the values of the current mouse position
    http://www.evolt.org/article/Mission_Impossible_mouse_position/17/23335/index.html
  */
  var result = new Array();
  var x = 0;
  var y = 0;
  var ev = event || window.event;
  var isOpera = (navigator.userAgent.indexOf('Opera') != -1);
  var isIE = (!isOpera && navigator.userAgent.indexOf('MSIE') != -1)
  
  if (ev.pageX || ev.pageY){
    x = ev.pageX;
    y = ev.pageY;
  } else if (ev.clientX || ev.clientY){
    x = ev.clientX;
    y = ev.clientY;
    if (isIE){
         x += document.documentElement.scrollLeft;
         y += document.documentElement.scrollTop;
    }
  }

  result[0] = x;
  result[1] = y;
  return result;
}

         function ajax(url, div, coords) {
            var doStuff = new Ajax(url, {
                   method: 'get',
                   update: $(div),
                   onRequest: function(){
                     $('spinner').setStyles({'top':coords[1], 'left':coords[0]+20, 'visibility':'visible'});
                   },
                   onComplete: function(){
                     $('spinner').setStyles({'visibility' : 'hidden'});
                     $('response').setStyles({'top': coords[1]+5, 'left': coords[0]+50, 'visibility' : 'visible'});
                     window.setTimeout("$('response').setStyle('visibility','hidden');", 1000);
                   },
                   evalScripts: true
            }).request();
         }

function mouseX(evt) {
if (evt.pageX) return evt.pageX;
else if (evt.clientX)
   return evt.clientX + (document.documentElement.scrollLeft ?
   document.documentElement.scrollLeft :
   document.body.scrollLeft);
else return null;
}


function mouseY(evt) {
if (evt.pageY) return evt.pageY;
else if (evt.clientY)
   return evt.clientY + (document.documentElement.scrollTop ?
   document.documentElement.scrollTop :
   document.body.scrollTop);
else return null;
}

function roundNumber(num, dec) {
	return Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
}


window.addEvent('domready', function() {



 if($('shoppingcart')){
   var page = parseInt($('cartpage').value);
 
   //handlers for the minus-plus buttons   
   
   //plus
   var elems = $('shoppingcart').getElements('input[id^=amountplus-]');
   elems.each(function(el){
      //getting the real product id
      var id = el.id.split('-')[1];
      el.addEvent('click', function(e) {
         var totprice = parseFloat($('totalprice').get('html'));
         var totprice2 = parseFloat($('totalprice2').get('html'));
         var otpprice = parseFloat($('totprice-'+id).get('html'));
         var otpprice2 = parseFloat($('totprice2-'+id).get('html'));
         var price = parseFloat($('price-'+id).get('html'));
         var price2 = parseFloat($('price2-'+id).get('html'));
         var count = $('count-'+id).value.length ? parseInt($('count-'+id).value) : 0;
         count = count + 1;
         var ntpprice = price.toFixed(2);
         var ntpprice2 = price2.toFixed(2);
         $('count-'+id).value = count;
         $('totprice-'+id).set('html',(price*count).toFixed(2));
         $('totalprice').set('html',(totprice-otpprice+(price*count)).toFixed(2));         
         $('totprice2-'+id).set('html',(price2*count).toFixed(2));
         $('totalprice2').set('html',(totprice2-otpprice2+(price2*count)).toFixed(2));         
         ajaxUpdate('index.php','','page='+page+'&ajax=1&id='+id+'&count='+count,'get','');
      });
   });

   //minus
   var elems = $('shoppingcart').getElements('input[id^=amountminus-]');
   elems.each(function(el){
      //getting the real product id
      var id = el.id.split('-')[1];
      var oldval = parseInt($('count-'+id).value);
      el.addEvent('click', function(e) {
         var totprice = parseFloat($('totalprice').get('html'));         
         var otpprice = parseFloat($('totprice-'+id).get('html'));
         var price = parseFloat($('price-'+id).get('html'));
         var totprice2 = parseFloat($('totalprice2').get('html'));         
         var otpprice2 = parseFloat($('totprice2-'+id).get('html'));
         var price2 = parseFloat($('price2-'+id).get('html'));
         var count = $('count-'+id).value.length ? parseInt($('count-'+id).value) : 0;
         count = count - 1;
         var ntpprice = price.toFixed(2);
         var ntpprice2 = price2.toFixed(2);
         $('count-'+id).value = count;
         $('totprice-'+id).set('html',(price*count).toFixed(2));
         $('totalprice').set('html',(totprice-otpprice+price*count).toFixed(2));         
         $('totprice2-'+id).set('html',(price2*count).toFixed(2));
         $('totalprice2').set('html',(totprice2-otpprice2+price2*count).toFixed(2));         
         if(parseInt($('count-'+id).value) > 0){
            ajaxUpdate('index.php','','page='+page+'&ajax=1&id='+id+'&count='+count,'get','');
         } else {
            $('totalprice').set('html',(totprice-otpprice));         
            $('totalprice2').set('html',(totprice2-otpprice2));         
            ajaxUpdate('index.php','','page='+page+'&ajax=1&del='+id,'get','');
            $('row-'+id).dispose();
            //also checking if no other elements are set
            if($('cart')){
               var elems = $('shoppingcart').getElements('input[id^=amountplus-]');
               if(!elems.length){
                  if($('errmsg')){
                     $('errmsg').dispose();
                  }
                  $('cart').dispose();
                  $('msg').set('html',$('empty_msg').value);
               }
            }
         }
      });
   });
   
   
      //delete
   var elems = $('shoppingcart').getElements('img[id^=delete-]');
   elems.each(function(el){
      //getting the real product id
      var id = el.id.split('-')[1];
      el.addEvent('click', function(e) {
         var count = parseInt($('count-'+id).value);
         var price = parseFloat($('price-'+id).get('html'));
         var totprice = parseFloat($('totalprice').get('html'));         
         $('totalprice').set('html',(totprice-(price*count)).toFixed(2));         
         var price2 = parseFloat($('price2-'+id).get('html'));
         var totprice2 = parseFloat($('totalprice2').get('html'));         
         $('totalprice2').set('html',(totprice2-(price2*count)).toFixed(2)); 
         ajaxUpdate('index.php','','page='+page+'&ajax=1&del='+id,'get','');
         $('row-'+id).dispose();
         //also checking if no other elements are set
         if($('cart')){
            var elems = $('shoppingcart').getElements('img[id^=delete-]');
            if(!elems.length){
               if($('errmsg')){
                  $('errmsg').dispose();
               }
               $('cart').dispose();
               $('msg').set('html',$('empty_msg').value);
            }
         }
         return false;
      });
   });

   
   
 }
 
});
