var cart_id = "esm-basket"; // id области в которую будет встроена корзина.


// Тип браузера
function browse_check() {
   this.ver     = navigator.appVersion;
   this.agent   = navigator.userAgent;
   this.dom     = document.getElementById?1:0;
   this.opera5  = this.agent.indexOf( "Opera 5" )>-1;
   this.opera   = ( this.agent.indexOf( "Opera" )>-1 ) ? 1 : 0;
   this.firefox = ( this.agent.indexOf( "Firefox" )>-1 ) ? 1 : 0;
   this.ie5     = ( this.ver.indexOf( "MSIE 5" ) >-1 && this.dom && !this.opera5 ) ? 1 : 0;
   this.ie6     = ( this.ver.indexOf( "MSIE 6" ) >- 1 && this.dom && !this.opera5 ) ? 1 : 0;
   this.ie7     = ( this.ver.indexOf( "MSIE" ) >- 1 && this.dom && !this.opera5 ) ? 1 : 0;
   this.ie4     = ( document.all && !this.dom && !this.opera5 ) ? 1 : 0;
   this.ie      = this.ie4 || this.ie5 || this.ie6 || this.ie7;
   this.ns6     = ( this.dom && parseInt( this.ver ) >= 5 ) ? 1 : 0;
   this.ns4     = ( document.layers && !this.dom ) ? 1 : 0;
   this.safari  = ( this.ver.indexOf( "Safari" ) > -1 ) ? 1 : 0;
   this.ok      = this.ie || this.opera || this.firefox || this.safari;
   
   return this; 

} // End function browse_check

 
bwc = new browse_check(); 
document.cookie = " check_esm=on; path=/";

var esmDiv = document.getElementById( cart_id );


// Первая загрузка корзины
function loadCart() { 
   getFile( "/cgi-bin/esm/add_to_cart.pl?cid=" + cid + "&" + parseInt( ( Math.random() * 1000000 + 1 ) ) ); 
} // End function loadCart


// Добавление в корзину
function toCart( price_id, front_id ) {
   var check_esm = "";

   var re = new RegExp( "check_esm=([^;]*);" );
   if( re.exec( document.cookie + ";" ) ) { check_esm = RegExp.$1; }

   re = new RegExp( "esm_cid=([^;]*);" );
   if( re.exec( document.cookie + ";" ) ) { cid = RegExp.$1; }

   if( !( bwc.ok && check_esm == "on" ) || 
       !getFile( "/cgi-bin/esm/add_to_cart.pl?" + parseInt( ( Math.random() * 1000000 + 1 ) ) + 
                                      "&cid=" + cid + "&price_id=" + price_id + "&front_id=" + front_id ) 
     ) orderGoods( cid );

   show_text( "Товар добавлен в корзину." );
   
} // End function toCart


function orderGoods(cid) { 
   if (!cid){ return; } page = new bwSize(); 
   wleft=(page.sw-(page.sw-100))/2; 
   wtop=(page.sh-(page.sh-50))/2;
   property="left="+wleft+",top="+wtop+",width="+(page.sw-300)+
            ",height="+(page.sh-200)+",toolbar=0,menubar=0,scrollbars=1,resizable=1"
   esm_win=window.open("/cgi-bin/esm/cart.pl?cid="+cid,"esm_order",property); 
   
} // End function orderGoods


function setCid(cid) { 
   if(cid) { document.cookie=" esm_cid="+escape(cid)+"; path=/"; }
} // End function setCid


function getFile( filename ) {
   var obj = get_http_obj();
   if( obj ){
      obj.open( "GET", filename, true );
      obj.onreadystatechange = function(){
         if( obj.readyState== 4 && obj.responseText ) {
            var re = /(\d)cid=/;
            
            if( re.test( obj.responseText ) ) {
               esmDiv.innerHTML = obj.responseText.substr( 35 );
               
               var element = document.createElement( "script" );
               element.text = 'setCid(\'' + obj.responseText.substr( 5, 30 ) + '\');';

               esmDiv.appendChild( element );
               
            } else {
               alert ( 'Ошибка при отображении корзины' );
               return false;

            } // End if

         } // End if
         
      } // End function
      
      obj.send( null );

      return true;
     
   } else {
      return false;
   } // End if
   
} // End function getFile


function get_http_obj() {
   var obj = null;
   try{
      obj = new ActiveXObject( "Msxml2.XMLHTTP" ); 
   } catch( err ){
      try{
         obj = new ActiveXObject( "Microsoft.XMLHTTP" ); 
      } catch( err ){
         obj = null;
      } // End try
      
   } // End try
         
   if( !obj && typeof XMLHttpRequest != "undefined" ) {
      obj = new XMLHttpRequest(); 
   } // End if
   return obj;
   
} // End function get_http_obj


// Высвечивание текста
function show_text( text ) {
   var obj = document.getElementById('esm_text')
   
   obj.innerHTML = text;
   obj.style.visibility = "visible";

   if( bwc.ie5 ) {
      var bodyElement = document.body;
   } else {
      var bodyElement = document.documentElement || document.body;
   }

   var clientH = bodyElement.clientHeight;
   var clientW = bodyElement.clientWidth;
   var scrollT;
   var scrollL;
   
   if( bwc.safari ) {
      scrollT = self.pageYOffset;
      scrollL = self.pageXOffset;
   } else {
      scrollT = bodyElement.scrollTop;
      scrollL = bodyElement.scrollLeft;
   }

   obj.style.top  = parseInt( clientH / 2 ) - 20 + scrollT + "px";
   obj.style.left = parseInt( ( clientW - 200 ) / 2 ) + scrollL + "px";
   
   setTimeout("hide_text()", 3000); 

} // End function show_text

// Гасим текст
function hide_text() {
   var obj = document.getElementById('esm_text')
   
   obj.style.visibility = "hidden";

} // End function show_text

function bwSize() {
   this.w=(bwc.ie)?document.body.offsetWidth-20:innerWidth; 
   this.h=(bwc.ie)?document.body.offsetHeight-5:innerHeight;
   if (self.screen) { 
      this.sw=screen.width; 
      this.sh=screen.height;
      
   } // End if
else if(self.java) { 
   jkit=java.awt.Toolkit.getDefaultToolkit(); 
   scrsize=jkit.getScreenSize(); 
   this.sw=scrsize.width; 
   this.sh=scrsize.height; 
   
} // End if

return this; 

} // End function bwSize   

cid = check_esm = "";
esm_cookie = document.cookie + ";";
 
re = new RegExp( "check_esm=([^;]*);" );
if( re.exec( esm_cookie ) ) { check_esm = RegExp.$1; }
 
re = new RegExp( "esm_cid=([^;]*);" );
if( re.exec( esm_cookie ) ) { cid = RegExp.$1; }

if( bwc.ok ) { 
   loadCart();
   
} // End if
