$(document).bind('ready', function() {
  list_func();
  item_func();
  cart_func();
});
function reload_containers(par) 
{
  if (par && $('div.container[@rel=pay]').length) {
    while ($('div.container:last').attr('rel') != 'pay') page_close();
    page_close();
  }
  $('div.container[@rel]').each(function() {
    if ($(this).attr('rel') == 'profile') {
      $(this).load(modal_link('/profile/'), function() {
        set_win_title();
        page_func();
        profile_func();        
        Custom.init();
      });
    } else if ($(this).attr('rel') == 'history') {
      $(this).load(modal_link('/history/'), function() {
        set_win_title();
        page_func();
        cart_func_history();
        page_func_history();
      });
    } else if ($(this).attr('rel') == 'pay') {
      $(this).load(modal_link('/pay/'), function() {
        set_win_title();
        page_func();
        pay_func();        
        cart_func_pay();
        Custom.init();
      });
    }
  })
}
carttm = new Object();
function cart_func()
{
  $('#dv_cart .address').bind('click', function() {
    var last_href = modal_link('/profile/');
    $('div.selectedItem[@rel=c]').hide();
    $('div.container:last').hide().each(function() {
      var el = document.createElement('div');
      $(el).attr('id', 'container').attr('class', 'container').attr('rel', 'profile').insertAfter($(this)).load(last_href, function() {
        set_win_title();
        page_func();
        profile_func();        
        Custom.init();
      });
    }); 
    set_href('/profile/');
    return false;
  })
  $('#dv_cart a.history').bind('click', function() {
    var last_href = modal_link('/history/');
    $('div.selectedItem[@rel=c]').hide();
    $('div.container:last').hide().each(function() {
      var el = document.createElement('div');
      fpar = 'fr=2';
      if ($('div.container:last #mainList li.active').length) {
        fpar = 'fr=6';
      }
      else if ($('div.container:last #mainList').length) {
        fpar = 'fr=1';
      }
      $(el).attr('id', 'container').attr('class', 'container').attr('rel', 'history').insertAfter($(this)).load(add_params(last_href, fpar), function() {
        set_win_title();
        page_func();
        cart_func_history();
        page_func_history();
      });
    });    
    set_href('/history/');
    return false;
  });
  $('#dv_cart a.info').bind('click', function() {
     $('div.selectedItem[@rel=c]').hide();
     $(document.body).append($('#info' + $(this).attr('rel')).show().css({left:($(this).offset().left - 5) + 'px', top:($(this).offset().top - 50) + 'px'}));
    return false;
  });
  $('#dv_cart .currency a').bind('click', function() {
    if ($(this).is('.active')) return false;
    $('#dv_cart .currency a').toggleClass('active');
    //$(this).addClass('active');
    $('#dv_cart').load('/cart_pre.php?set_curr=' + $(this).attr('rel') + '&rand=' + Math.random(), function() {
      $('#dv_cart .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
      cart_func();
    });
    if ($('#filter').length) {
      //$('#filter ul.clist').toggleClass('cinvis');
      $(document.body).toggleClass('cr1').toggleClass('cr2');
      //$('#itemList td.price span').toggleClass('cinvis');
      $('#filter ul.clist a.inactive').removeClass('inactive');
      do_show();
    }
    return false;
  });
	$('#dv_cart a.delete').bind('click', function() {
    if (carttm[$(this).attr('rel')]) window.clearTimeout(carttm[$(this).attr('rel')]);
    $('#itemList a.dec[@rel=' + $(this).attr('rel') + ']').next('input').attr('value', 1).parent('div').addClass('hide').prev('a').removeClass('hide');
    $('#itemList a.preadd[@rel=' + $(this).attr('rel') + ']').removeClass('hide').next('a').addClass('hide');
    $('#dv_cart').load('/cart_pre.php?delete_product=' + $(this).attr('rel') + '&rand=' + Math.random(), function() {
      $('#dv_cart .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
      cart_func();
    });
    $('div.selectedItem[@rel=c]').remove();
    return false;
  });
  $('#dv_cart a.dec').bind('click', function() {
    if($(this).next('input').attr('value') > 1){
      if (carttm[$(this).attr('rel')]) window.clearTimeout(carttm[$(this).attr('rel')]);
      $('#itemList').add('div.addThis').add('div.selectedItem[@rel=c]').add('#dv_orderedItems').add('div.selectedItem[@rel=p]').find('input.text[@rel=' + $(this).attr('rel') + ']').attr('value', $(this).next('input').attr('value')/1 - 1);
      updatetm($(this).attr('rel'));
    } else {
      /*
      $('#itemList a.dec[@rel=' + $(this).attr('rel') + ']').parent('div').addClass('hide').prev('a').removeClass('hide');
      $('#dv_cart').load('/cart_pre.php?delete_product=' + $(this).attr('rel') + '&rand=' + Math.random(), function() {
        $('#dv_cart .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
        cart_func();
      });
      $('div.selectedItem[@rel=c]').remove();
      */
    }
    return false;
  });		
		 
 $('#dv_cart a.inc').bind('click', function() {
	  if($(this).prev('input').attr('value') < 100){
      if (carttm[$(this).attr('rel')]) window.clearTimeout(carttm[$(this).attr('rel')]);
      $('#itemList').add('div.addThis').add('div.selectedItem[@rel=c]').add('#dv_orderedItems').add('div.selectedItem[@rel=p]').find('input.text[@rel=' + $(this).attr('rel') + ']').attr('value', $(this).prev('input').attr('value')/1 + 1);
      updatetm($(this).attr('rel'));
      //$(this).prev('input').attr('value', $(this).prev('input').attr('value')/1 + 1);    
    }
    return false;
  });

 $('#dv_cart input.text[@rel]').bind('keyup', function() {
    if (carttm[$(this).attr('rel')]) window.clearTimeout(carttm[$(this).attr('rel')]);
    if (!/^[1-9][01-9]*$/.test($(this).attr('value'))) {
      if (/[1-9][01-9]*$/.test($(this).attr('value'))) $(this).attr('value', $(this).attr('value').replace(/[^01-9]+$/, ''));
      else $(this).attr('value', 1);
    }
    if ($(this).attr('value') > 100) $(this).attr('value', 100);
    //$(this).prev('input').attr('value', $(this).prev('input').attr('value')/1 + 1);
    $('#itemList').add('div.addThis').add('div.selectedItem[@rel=c]').add('#dv_orderedItems').add('div.selectedItem[@rel=p]').find('input.text[@rel=' + $(this).attr('rel') + ']').attr('value', $(this).attr('value'));
    updatetm($(this).attr('rel'));
    return false;
  }).bind('focus', function() { this.select(); }).bind('keydown', function(event) {
    var key = event.charCode ? event.charCode : event.keyCode;
    if (key == 38) {
      $(this).next('a').trigger('click');
    } else if (key == 40) {
      $(this).prev('a').trigger('click');
    }
  });
  $('#dv_cart .order a').bind('click', function() {
    if (!$(this).is('.disabled')) {
      var last_href = modal_link('/pay/');
      $('div.selectedItem[@rel=c]').hide();
      $('div.container:last').hide().each(function() {
        var el = document.createElement('div');
        $(el).attr('id', 'container').attr('class', 'container').attr('rel', 'pay').insertAfter($(this)).load(last_href, function() {
          set_win_title();
          page_func();
          pay_func();    
          cart_func_pay();
          Custom.init();
        });
      });    
      set_href('/pay/');
    }
    return false;
  })    
  $('#dv_cart a.shipinfo').bind('click', function() {
    var last_href = modal_link($(this).attr('href'));
    $('div.selectedItem[@rel=c]').hide();
    $('div.container:last').hide().each(function() {
      var el = document.createElement('div');
      fpar = 'fr=2';
      if ($('div.container:last #mainList li.active').length) {
        fpar = 'fr=6';
      }
      else if ($('div.container:last #mainList').length) {
        fpar = 'fr=1';
      }
      $(el).attr('id', 'container').attr('class', 'container').insertAfter($(this)).load(add_params(last_href, fpar), function() {
        set_win_title();
        page_func();
        $('div.container:last .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
      });
    });  
    set_href('/shipment/');  
    return false;
  });
  $('#dv_cart a.det').bind('click', function() {
    var last_href = modal_link($(this).attr('href'));
    $('div.selectedItem[@rel=c]').hide();
    $('div.container:last').hide().each(function() {
      var el = document.createElement('div');
      fpar = 'fr=2';
      fpar2 = 'fr2=1';
      if ($('div.container:last #mainList li.active').length) {
        fpar = 'fr=6';
      }
      else if ($('div.container:last #mainList').length) {
        fpar = 'fr=1';
      }
      $(el).attr('id', 'container').attr('class', 'container').insertAfter($(this)).load(add_params(last_href, fpar+'&' + fpar2), function() {
        set_win_title();
        item_func();
        $('#dv_detailsSide .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
      });
    });    
    set_href($(this).attr('href'));
    return false;
  });
}
window.waitamoment = 0;
function conflict_close()
{
  if ($('div.container:last').attr('rel') == 'conflict') {
    if ($('div.container').length > 1) {
      $('div.container:last').remove();
      $('div.container:last').show();
    } else {
      document.location.href = '/';
    }
  }
}
function page_close()
{
  if (window.waitamoment > 0) {
    window.setTimeout(page_close, 100);
    return false;
  }
  if ($('#cities').is(':visible')) {
    $('#cities').hide().removeClass('citpos1').removeClass('citpos2').removeClass('citpos3').prev().hide();
  }
  if ($('div.container').length > 1) {
    if ($('div.container:last').attr('rel') == 'conflict') {
      $('div.container:last').remove();
      $('div.container:last').show();  
      set_win_title();
      reload_cart();
      reload_paycart();
      $('div.container:last .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});    
    }
    else {
      if ($('div.container:last').attr('rel') == 'pay') $('div.selectedItem[@rel=p]').remove();
      $('div.container:last').remove();
      $('div.container:last').show();
      set_win_title();
      $('div.container:last .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
    }
    restore_href();
  } else {
    document.location.href = '/';
  }
  return false;
}
function page_close_nohistory()
{
  if (window.waitamoment > 0) {
    window.setTimeout(page_close_nohistory, 100);
    return false;
  }
  if ($('#cities').is(':visible')) {
    $('#cities').hide().removeClass('citpos1').removeClass('citpos2').removeClass('citpos3').prev().hide();
  }
  if ($('div.container').length > 1) {
    if ($('div.container:last').attr('rel') == 'conflict') {
      $('div.container:last').remove();
      $('div.container:last').show();
      reload_cart();
      reload_paycart();
      $('div.container:last .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
    }
    else {
      $('div.container:last').remove();
      $('div.container:last').show();
      $('div.container:last .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
    }
  } else {
    document.location.href = '/';
  }
  return false;
}
function page_func()
{
  $('div.container:last .content .buttons a.close').bind('click', function() {
    if (!$(this).is('.disabled')) { 
      $(this).addClass('disabled');
      page_close();
    }
    return false;
  });
}
window.waitamoment2 = 0;
function reload_cart()
{
  if ($('#dv_cart').length == 0) return;
  window.waitamoment2++;
  $('#dv_cart').load('/cart_pre.php?just_update=1&rand=' + Math.random(), function() {
    $('div.container:last #dv_cart .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
    cart_func();
    list_func_update();
    window.waitamoment2 = 0;
  });
}
function reload_paycart()
{
  if ($('#dv_orderedItems').length == 0) return;
  $('#dv_orderedItems').load('/cart_pre.php?just_update=1&fpay=1&rand=' + Math.random(), function() {
    $('div.container:last #dv_orderedItems .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
    cart_func_pay();
  });
}
function list_func_update()
{
  $('#itemList a.add').removeClass('hide').next('div').addClass('hide').find('input.text').attr('value', 1);
  $('#itemList a.preadd').removeClass('hide').next('a').addClass('hide');
  $('#dv_cart input.text[@rel]').each(function() {
    $('#itemList input.text[@rel=' + $(this).attr('rel') + ']').attr('value', $(this).attr('value')).parent().removeClass('hide').prev('a.add').addClass('hide');
  })
  $('#dv_cart a.delete').each(function() {
    $('#itemList a.preadd[@rel=' + $(this).attr('rel') + ']').addClass('hide').next('a').removeClass('hide');
  })
}
function _lf1() 
{
  var t = $(this);
  if (carttm[t.attr('rel')]) window.clearTimeout(carttm[t.attr('rel')]);
  t.addClass('hide');
  t.next('div').removeClass('hide');
  $('div.selectedItem[@rel=c]').remove();
  $('#dv_cart').load('/cart_pre.php?add_product=' + t.attr('rel') + '&rand=' + Math.random(), function() {
    $('#dv_cart .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
    cart_func();
  });
  return false;
}
function _lf2() 
{
  var t = $(this);
  t.addClass('hide');
  t.next('a').removeClass('hide');
  $('div.selectedItem[@rel=c]').remove();
  $('#dv_cart').load('/cart_pre.php?add_product=' + t.attr('rel') + '&rand=' + Math.random(), function() {
    $('#dv_cart .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
    cart_func();
  });
  return false;
}
function _lf3() 
{
  var t = $(this);
  t.addClass('hide');
  t.prev('a').removeClass('hide');
  $('div.selectedItem[@rel=c]').remove();
  $('#dv_cart').load('/cart_pre.php?delete_product=' + t.attr('rel') + '&rand=' + Math.random(), function() {
    $('#dv_cart .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
    cart_func();
  });
  return false;
}
function _lf4() 
{
  var t = $(this);
  if (carttm[t.attr('rel')]) window.clearTimeout(carttm[t.attr('rel')]);
  if(t.next('input').attr('value') > 1){
    //t.next('input').attr('value', t.next('input').attr('value')/1 - 1);
    $('#itemList').add('div.addThis').add('div.selectedItem[@rel=c]').add('#dv_orderedItems').add('div.selectedItem[@rel=p]').find('input.text[@rel=' + t.attr('rel') + ']').attr('value', t.next('input').attr('value')/1 - 1);
    updatetm(t.attr('rel'));
  } else {
    t.parent('div').addClass('hide').prev('a').removeClass('hide');
    $('#dv_cart').load('/cart_pre.php?delete_product=' + t.attr('rel') + '&rand=' + Math.random(), function() {
      $('#dv_cart .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
      cart_func();
    });
    $('div.selectedItem[@rel=c]').remove();
  }
  return false;
}
function _lf5() 
{
  var t = $(this);
  if(t.prev('input').attr('value') < 100){
    if (carttm[t.attr('rel')]) window.clearTimeout(carttm[t.attr('rel')]);
    //t.prev('input').attr('value', t.prev('input').attr('value')/1 + 1);
    $('#itemList').add('div.addThis').add('div.selectedItem[@rel=c]').add('#dv_orderedItems').add('div.selectedItem[@rel=p]').find('input.text[@rel=' + t.attr('rel') + ']').attr('value', t.prev('input').attr('value')/1 + 1);
    updatetm(t.attr('rel'));
  }
  return false;
}
function _lf6() 
{
  var t = $(this);
  if (carttm[t.attr('rel')]) window.clearTimeout(carttm[t.attr('rel')]);
  if (!/^[1-9][01-9]*$/.test(t.attr('value'))) {
    if (/[1-9][01-9]*$/.test(t.attr('value'))) t.attr('value', t.attr('value').replace(/[^01-9]+$/ig, ''));
    else t.attr('value', 1);
  }
  if (t.attr('value') > 100) t.attr('value', 100);
  //t.prev('input').attr('value', t.prev('input').attr('value')/1 + 1);
  $('#itemList').add('div.addThis').add('div.selectedItem[@rel=c]').add('#dv_orderedItems').add('div.selectedItem[@rel=p]').find('input.text[@rel=' + t.attr('rel') + ']').attr('value', t.attr('value'));
  updatetm(t.attr('rel'));
  return false;
}
function _lf7(event) 
{
  var t = $(this);
  var key = event.charCode ? event.charCode : event.keyCode;
  if (key == 38) {
    t.next('a').trigger('click');
  } else if (key == 40) {
    t.prev('a').trigger('click');
  }
}
function _lf8() 
{
    var t = $(this);
    var last_href = modal_link(t.attr('href'));
    $('div.selectedItem[@rel=c]').hide();
    $('div.container:last').hide().each(function() {
      var el = document.createElement('div');
      fpar = 'fr=2';
      fpar2 = 'fr2=2';
      $(el).attr('id', 'container').attr('class', 'container').insertAfter($(this)).load(add_params(last_href, fpar+'&' + fpar2), function() {
        set_win_title();
        item_func();
        $('#dv_detailsSide .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
      });
    });    
    set_href(t.attr('href'));
    return false;
}
function _lf0() 
{
   $('#' + this.id).toggleClass('inactive');
   do_show();
  return false;
}
function continue_loading()
{
  if (!document.getElementById('extend_load')) return;
  $.ajax({
      url: '/load.php?code='+document.getElementById('extend_load').value,
      type: 'GET',
      dataType: 'html',
      timeout: 2000,
      error: function(){
        window.loading = window.setTimeout(continue_loading, 50);
      },
      success: function(xml){
        if (xml != 'END' && xml != '') {
          var arr2 = xml.split("\n");
          for(var i=0; i<arr2.length; i++)
          {
            var arr = arr2[i].split("\t");
            var t = $('#tr' + arr[0]);
            t.find('td:first').html(arr[1]).next().html(arr[2]).next().html(arr[3]).next().html(arr[4]);
            t.find('a.dos').bind('click', _lf0).each(function(){this.className=document.getElementById(this.id).className.replace('inactive', 'dact')});
            t.find('a.add').bind('click', _lf1);
            t.find('a.preadd').bind('click', _lf2);
            t.find('a.predel').bind('click', _lf3);
            t.find('a.dec').bind('click', _lf4);		
            t.find('a.inc').bind('click', _lf5);
            t.find('input.text[@rel]').bind('keyup', _lf6).bind('focus', function() { this.select(); }).bind('keydown', _lf7);
            t.find('a.detail').bind('click', _lf8);
          }          
          //var arr = xml.split("\t");
          //$('#tr' + arr[0]).addClass(arr[1]).find('td:first').html(arr[2]).next().html(arr[3]).next().html(arr[4]).next().html(arr[5]);
          window.loading = window.setTimeout(continue_loading, 100);
        }
      }
  });

  //window.loading = window.setTimeout(continue_loading, 50);
}
function _lf9() 
{
  var t = $(this);
  if (!t.is('.sortact')) {
    $('#itemList thead a.sort').toggleClass('sortact');
    $('#itemList thead a.sortact').each(function() {
      var l = $(this).prev().attr('value').split(',');      
      t = $(this).parent().parent().parent().next();
      for(var i=0; i<l.length; i++)
      {
      	$('#tr' + l[i]).appendTo(t);
      }
      var t2 = $('#itemList table');
      //$(t2).find('tr.in').removeClass('alt');
      t2.each(function() {
        var acti = 0;
        var t = $(this);
        t.find('tr.in').not('.invis').each(function() {
          if (++acti%2 == 1) $(this).addClass('alt'); else $(this).removeClass('alt');
        });
      });
    });
  }
  return false;
}
function list_func()
{
  $('#itemList').add('#filter').find('a.dos').bind('click', _lf0);
	$('#itemList a.add').bind('click', _lf1);
	$('#itemList a.preadd').bind('click', _lf2);
	$('#itemList a.predel').bind('click', _lf3);
  $('#itemList a.dec').bind('click', _lf4);		
  $('#itemList a.inc').bind('click', _lf5);
  $('#itemList input.text[@rel]').bind('keyup', _lf6).bind('focus', function() { this.select(); }).bind('keydown', _lf7);
  $('#itemList a.detail').bind('click', _lf8);
  $('#itemList thead a.sort').bind('click', _lf9);
  window.loading = window.setTimeout(continue_loading, 100);
}
function item_func()
{
	$('div.addThis a.add').bind('click', function() {
    if (carttm[$(this).attr('rel')]) window.clearTimeout(carttm[$(this).attr('rel')]);
    $(this).add('#itemList a.add[@rel=' + $(this).attr('rel') + ']').addClass('hide').next('div').removeClass('hide');
    $('div.selectedItem[@rel=c]').remove();
    window.waitamoment2++;
    $('#dv_cart').load('/cart_pre.php?add_product=' + $(this).attr('rel') + '&rand=' + Math.random(), function() {
      //$('#dv_cart .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
      cart_func();
      if ($('#dv_orderedItems').length > 0) {
        $('div.selectedItem[@rel=p]').remove();
        $('#dv_orderedItems').load('/cart_pre.php?just_update=1&fpay=1&rand=' + Math.random(), function() {
          cart_func_pay();
        });
      }
      window.waitamoment2--;
    });
    return false;
  });
	$('div.addThis a.buy').bind('click', function() {
    if ($(this).is('.disabled')) return false;
    $(this).addClass('disabled');
    if ($(this).prev().prev().is(':visible')) {
      $(this).prev().prev().trigger('click');
    }
    close_n_buy();
    return false;
  });
	$('div.addThis a.preadd').bind('click', function() {
    $(this).add('#itemList a.preadd[@rel=' + $(this).attr('rel') + ']').addClass('hide').next('a').removeClass('hide');
    $('div.selectedItem[@rel=c]').remove();
    $('#dv_cart').load('/cart_pre.php?add_product=' + $(this).attr('rel') + '&rand=' + Math.random(), function() {
      //$('#dv_cart .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
      cart_func();
      if ($('#dv_orderedItems').length > 0) {
        $('div.selectedItem[@rel=p]').remove();
        $('#dv_orderedItems').load('/cart_pre.php?just_update=1&fpay=1&rand=' + Math.random(), function() {
          cart_func_pay();
        });
      }
    });
    return false;
  });
	$('div.addThis a.predel').bind('click', function() {
			$(this).add('#itemList a.predel[@rel=' + $(this).attr('rel') + ']').addClass('hide').prev('a').removeClass('hide');
      $('div.selectedItem[@rel=c]').remove();
      $('#dv_cart').load('/cart_pre.php?delete_product=' + $(this).attr('rel') + '&rand=' + Math.random(), function() {
        //$('#dv_cart .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
        cart_func();
        if ($('#dv_orderedItems').length > 0) {
          $('div.selectedItem[@rel=p]').remove();
          $('#dv_orderedItems').load('/cart_pre.php?just_update=1&fpay=1&rand=' + Math.random(), function() {
            cart_func_pay();
          });
        }
      });
      return false;
		});
  $('div.addThis a.dec').bind('click', function() {
			if($(this).next('input').attr('value') > 1){
        if (carttm[$(this).attr('rel')]) window.clearTimeout(carttm[$(this).attr('rel')]);
				//$(this).next('input').attr('value', $(this).next('input').attr('value')/1 - 1);
        $('#itemList').add('div.addThis').add('div.selectedItem[@rel=c]').add('#dv_orderedItems').add('div.selectedItem[@rel=p]').find('input.text[@rel=' + $(this).attr('rel') + ']').attr('value', $(this).next('input').attr('value')/1 - 1);
        updatetm($(this).attr('rel'));
			} else {
        $(this).add('#itemList a.dec[@rel=' + $(this).attr('rel') + ']').parent('div').addClass('hide').prev('a').removeClass('hide');
        $('#dv_cart').load('/cart_pre.php?delete_product=' + $(this).attr('rel') + '&rand=' + Math.random(), function() {
          //$('#dv_cart .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
          cart_func();
          if ($('#dv_orderedItems').length > 0) {
            $('div.selectedItem[@rel=p]').remove();
            $('#dv_orderedItems').load('/cart_pre.php?just_update=1&fpay=1&rand=' + Math.random(), function() {
              cart_func_pay();
            });
          }
        });
        $('div.selectedItem[@rel=c]').remove();
      }
      return false;
		});		
		
 $('div.addThis a.inc').bind('click', function() {
	    if($(this).prev('input').attr('value') < 100){
        if (carttm[$(this).attr('rel')]) window.clearTimeout(carttm[$(this).attr('rel')]);
        //$(this).prev('input').attr('value', $(this).prev('input').attr('value')/1 + 1);
        $('#itemList').add('div.addThis').add('div.selectedItem[@rel=c]').add('#dv_orderedItems').add('div.selectedItem[@rel=p]').find('input.text[@rel=' + $(this).attr('rel') + ']').attr('value', $(this).prev('input').attr('value')/1 + 1);
        updatetm($(this).attr('rel'));
      }
      return false;
		});

 $('div.addThis input.text[@rel]').bind('keyup', function() {
    if (carttm[$(this).attr('rel')]) window.clearTimeout(carttm[$(this).attr('rel')]);
    if (!/^[1-9][01-9]*$/.test($(this).attr('value'))) {
      if (/[1-9][01-9]*$/.test($(this).attr('value'))) $(this).attr('value', $(this).attr('value').replace(/[^01-9]+$/ig, ''));
      else $(this).attr('value', 1);
    }
    if ($(this).attr('value') > 100) $(this).attr('value', 100);
    //$(this).prev('input').attr('value', $(this).prev('input').attr('value')/1 + 1);
    $('#itemList').add('div.addThis').add('div.selectedItem[@rel=c]').add('#dv_orderedItems').add('div.selectedItem[@rel=p]').find('input.text[@rel=' + $(this).attr('rel') + ']').attr('value', $(this).attr('value'));
    updatetm($(this).attr('rel'));
    return false;
  }).bind('focus', function() { this.select(); }).bind('keydown', function(event) {
    var key = event.charCode ? event.charCode : event.keyCode;
    if (key == 38) {
      $(this).next('a').trigger('click');
    } else if (key == 40) {
      $(this).prev('a').trigger('click');
    }
  });
  $('#dv_detailsSide li.category a').bind('click', function() {
    if ($('div.container[@rel=home]').length)
    {
      var this_href = $(this).attr('href');
      var this_rel = $(this).attr('rel');
      var this_em = $(this).parent().is('em');
      while ($('div.container:last').attr('rel') != 'home') page_close();
      if ($('#searchField').attr('value') != '' && $('#searchField').attr('value') != 'undefined') {
        $('#close').trigger('click');
      }
      if (this_em) {
        $('#mainList li').removeClass('active');
        $('#mainList li[@rel=' + this_rel + ']').addClass('active');
        if ($('#categorySelect').length) {
          $('#categorySelect').load(modal_link(this_href), function() {
            set_win_title();
            subcat_domodal();
            $('#service #breabcrumbs').replaceWith($('div.container:last #breabcrumbs'));
            search_tree();
          });
        } else {
          $('#in_container').load(modal_link2(this_href), function() {
            set_win_title();
            index_domodal();
            subcat_domodal();
            $('#service #breabcrumbs').replaceWith($('div.container:last #breabcrumbs'));
            search_tree();
          });
        }
        set_href(this_href);
        $('#searchRoot').attr('value', this_href);
      } else {
        set_href(this_href);
        $('#searchRoot').attr('value', this_href);
        $('#in_container').html(empty_list).load(modal_link(this_href), function() {
          set_win_title();
          items_domodal();
          list_func();
          $('#in_container .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
          $('#service #breabcrumbs').replaceWith($('div.container:last #breabcrumbs'));
          search_tree();
        })
      }
      return false;
    }
  })
  $('div.container:last .content .buttons a.close').bind('click', function() {
     page_close();
     if ($('#breabcrumbs a').length == 3 && !$('#itemList').length) {
       $('#breabcrumbs').html('');
       search_width();
     }
     return false
  });
  $('div.container:last .content .buttons a.prev').each(function(){
    var a = $(this).attr('rel').split('_');
    if (a[0] != 0) { // List
      if (a[0] == 2) {
        var t = $('#itemList tr.in').not('.invis');
        var tid = 'tr';
        var tid2 = '';
        var tcl = 'a.detail';
      } else if (a[0] == 1) {
        var t = $('#dv_cart tr');
        var tid = 'ctr';
        var tid2 = 'info';
        var tcl = 'a.det';
      } else if (a[0] == 5) {
        var t = $('#dv_orderedItems tr');
        var tid = 'otr';
        var tid2 = 'infop';
        var tcl = 'a.det'; 
      } else if (a[0] == 3) {
        var t = $('#dv_orderedItemsConf tr').not('.hide');
        var tid = 'ftr';
        var tid2 = 'infof';
        var tcl = 'a.det';
      } else if (a[0] == 4) {
        var t = $('#dv_orderedItemsSmall tr');
        var tid = 'htr';
        var tid2 = '_infoh';
        var tcl = 'a.det';
      } 
      var i = 0;
      var l = t.length;
      var f1 = 0;
      var f2 = 0;
      t.each(function(){
        if (this.id == tid + a[1]) {
          if (i!=0) {
            f1 = 1;
            $('div.container:last .content .buttons a.prev').removeClass('disabled').bind('click', function() {
              if (!$(this).is('.disabled')) {
                $(this).addClass('disabled');
                var a = $(this).attr('rel').split('_');
                if (a[0] == 2) {
                  var t = $('#itemList tr.in').not('.invis');
                  var tid = 'tr';
                  var tid2 = '';
                  var tcl = 'a.detail';
                } else if (a[0] == 1) {
                  var t = $('#dv_cart tr');
                  var tid = 'ctr';
                  var tid2 = 'info';
                  var tcl = 'a.det';
                } else if (a[0] == 5) {
                  var t = $('#dv_orderedItems tr');
                  var tid = 'otr';
                  var tid2 = 'infop';
                  var tcl = 'a.det'; 
                } else if (a[0] == 3) {
                  var t = $('#dv_orderedItemsConf tr').not('.hide');
                  var tid = 'ftr';
                  var tid2 = 'infof';
                  var tcl = 'a.det';
                } else if (a[0] == 4) {
                  var t = $('#dv_orderedItemsSmall tr');
                  var tid = 'htr';
                  var tid2 = '_infoh';
                  var tcl = 'a.det';
                } 
                var i = 0;
                var prev = null;
                t.each(function(){
                  if (this.id == tid + a[1]) {
                    var t = $(prev).find(tcl);
                    if (t.length == 0) t = $('#' + prev.id.replace(tid, tid2)).find(tcl);
                    var last_href = modal_link(t.attr('href'));
                    fpar = 'fr=' + a[2];
                    fpar2 = 'fr2=' + a[0];
                    $('div.container:last').load(add_params(last_href, fpar+'&' + fpar2), function() {
                      set_win_title();
                      item_func();
                      $('#dv_detailsSide .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
                    });
                    set_href_over(t.attr('href'));
                    return false;
                  } else {
                    prev = this;
                  }
                });
              }
              return false;
            });
          }
          if (i!=l-1) {
            f2= 1;
            $('div.container:last .content .buttons a.next').removeClass('disabled').bind('click', function() {
              if (!$(this).is('.disabled')) {
                $(this).addClass('disabled');
                var a = $(this).attr('rel').split('_');
                if (a[0] == 2) {
                  var t = $('#itemList tr.in').not('.invis');
                  var tid = 'tr';
                  var tid2 = '';
                  var tcl = 'a.detail';
                } else if (a[0] == 1) {
                  var t = $('#dv_cart tr');
                  var tid = 'ctr';
                  var tid2 = 'info';
                  var tcl = 'a.det';
                } else if (a[0] == 5) {
                  var t = $('#dv_orderedItems tr');
                  var tid = 'otr';
                  var tid2 = 'infop';
                  var tcl = 'a.det'; 
                } else if (a[0] == 3) {
                  var t = $('#dv_orderedItemsConf tr').not('.hide');
                  var tid = 'ftr';
                  var tid2 = 'infof';
                  var tcl = 'a.det';
                } else if (a[0] == 4) {
                  var t = $('#dv_orderedItemsSmall tr');
                  var tid = 'htr';
                  var tid2 = '_infoh';
                  var tcl = 'a.det';
                } 
                var i = 0;
                var next = 0
                t.each(function(){
                  if (this.id == tid + a[1]) {
                    next = 1;
                  } else if (next) {
                    var t = $(this).find(tcl);
                    if (t.length == 0) t = $('#' + this.id.replace(tid, tid2)).find(tcl);
                    var last_href = modal_link(t.attr('href'));
                    fpar = 'fr=' + a[2];
                    fpar2 = 'fr2=' + a[0];
                    $('div.container:last').load(add_params(last_href, fpar+'&' + fpar2), function() {
                      set_win_title();
                      item_func();
                      $('#dv_detailsSide .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
                    });
                    set_href_over(t.attr('href'));
                    return false;
                  } else {
                    next = 0;
                  }
                });
              }
              return false;
            });
          }
        }
        i++;
      });
      if (!f1) $('div.container:last .content .buttons a.prev').bind('click', function() { return false; });
      if (!f2) $('div.container:last .content .buttons a.next').bind('click', function() { return false; });
      //if (t.attr('id') != tid + a[1]) $(this).removeClass('disabled')
    }
  });
}
function close_n_buy()
{
  if (window.waitamoment > 0) {
    window.setTimeout(close_n_buy, 100);
  } else {
     $('div.container:last .content .buttons a.close').trigger('click');
     close_n_buy2();
  }
}
function close_n_buy2()
{
  if (window.waitamoment2 > 0) {
    window.setTimeout(close_n_buy2, 100);
  } else {
     $('#dv_cart .order a').trigger('click');
  }
}
function updatetm (id)
{
  recalccart();
  recalccart_pay();
  window.waitamoment++;
  window.setTimeout('window.waitamoment--', 2000);
  carttm[id] = window.setTimeout('_updatetm(' + id + ')', 2000);
}
function _updatetm(id)
{
  $('#invis').load('/cart_pre.php?update_product=' + id + '&qty=' + $('#itemList').add('div.addThis').add('div.selectedItem[@rel=c]').find('input.text[@rel=' + id + ']').attr('value') + '&rand=' + Math.random(), function() {
    recalccart();
    recalccart_pay();
    $(this).html('');
  });
}
function recalccart()
{
  if ($('#dv_cart .forPayment strong').length == 0) return;
  $('div.selectedItem[@rel=c] a.dec').each(function() {
    if ($(this).next('input').attr('value') == 1) {
      $(this).addClass('disabled');
    } else {
      $(this).removeClass('disabled');
    }
  });
  $('#itemList').add('div.addThis').add('div.selectedItem[@rel=c]').find('a.inc').each(function() {
    if ($(this).prev('input').attr('value') == 100) {
      $(this).addClass('disabled');
    } else {
      $(this).removeClass('disabled');
    }
  });
  var total = 0;
  
  var this_total = 0;
  var this_qty = 0;
  var total_ship = 0;
  var total_cour = 0;
  $('#dv_cart input[@type=hidden][@rel]').each(function() {
    this_qty = $('#itemList').add('div.addThis').add('div.selectedItem[@rel=c]').find('input.text[@rel=' + $(this).attr('rel') + ']').attr('value');
    total_ship += this_qty*$(this).next().attr('value');
    this_total = parseInt($(this).attr('value')*this_qty);
    total += this_total;
    $('#dv_cart').add('div.selectedItem[@rel=c]').find('.price[@rel='+$(this).attr('rel')+']').each(function(){
      $(this).html($(this).html().replace(/[1-9][01-9]*/, this_total))
    });
    if (this_qty > 1) {
      $('#dv_cart').add('div.selectedItem[@rel=c]').find('.quantity2[@rel='+$(this).attr('rel')+']').each(function(){
        $(this).html($(this).html().replace(/[01-9]+/, this_qty)).removeClass('hide');
      });
    } else {
      $('#dv_cart').add('div.selectedItem[@rel=c]').find('.quantity2[@rel='+$(this).attr('rel')+']').each(function(){
        $(this).html($(this).html().replace(/[01-9]+/, this_qty)).addClass('hide');
      });
    }
  });
  var total_grn = total;
  if ($('#dv_cart input[@name=has_curr]').length == 0) {
    total_grn = total*$('#dv_cart input[@name=exchange]').attr('value');
  }
  var csh = $('#dv_cart input[@name=city_ship]').attr('value').split('|');
  total_ship = Math.round(1*csh[4] + 0.01*csh[1]*total + 0.01*csh[2]*total_ship);
  total_cour = (1*csh[3] && 1*csh[3] < total_grn) ? 0 : 1*csh[0];
  var def_percent = $('#dv_cart input[@name=def_percent]').attr('value').split('|');
  $('#dv_cart input[@name=total_ship]').attr('value', 0.01*Math.round(100*total_ship/$('#dv_cart input[@name=exchange]').attr('value')));
  
  if ($('#dv_cart input[@name=has_curr]').length > 0) {
    total_ship = 1*def_percent[0] + 0.01*def_percent[1]*total_grn + 0.01*def_percent[3]*total_cour + 0.01*def_percent[2]*$('#dv_cart input[@name=total_ship]').attr('value')/1 * $('input[@name=exchange]').attr('value');
    $('#dv_cart .delivery span:first').html($('#dv_cart .delivery span:first').html().replace(/[01-9]+/, Math.round(total_ship)));
    if (total_ship) {
      total += Math.round(total_ship);
      $('#dv_cart .delivery span:last').addClass('invis').prev().removeClass('invis');
    } else {
      $('#dv_cart .delivery span:last').removeClass('invis').prev().addClass('invis');
    }
  } else {
    total_ship = (1*def_percent[0] + 0.01*def_percent[1]*total_grn + 0.01*def_percent[3]*total_cour) / $('input[@name=exchange]').attr('value') + 0.01*def_percent[2]*$('#dv_cart input[@name=total_ship]').attr('value')/1;
    $('#dv_cart .delivery span:first').html($('#dv_cart .delivery span:first').html().replace(/[01-9]+/, Math.round(total_ship)));
    if (total_ship) {
      total += Math.round(total_ship);
      $('#dv_cart .delivery span:last').addClass('invis').prev().removeClass('invis');
    } else {
      $('#dv_cart .delivery span:last').removeClass('invis').prev().addClass('invis');
    }
  }
  $('#dv_cart .forPayment strong').html($('#dv_cart .forPayment strong').html().replace(/[1-9][01-9]*/, total));
}
function dis_addresses()
{
  $('#payinfo .payIn input.styled').each(function() {
    if ($(this).attr('checked')) {
      $(this).parent().find('input.text').attr('disabled', false);
    } else {
      $(this).parent().find('input.text').removeClass('error').attr('disabled', true);
    }
  })
}
function check_orderform_() {
  var found = false;
  var el = this;
  if (el && el.name) $(this).removeClass('error');
  $('#payinfo .clientInfo input[@type=text]').not('.nocheck').each(function() {
    if (el && el.name && $(this).attr('name') == el.name) found = true;
    if (!found) {
      if ((!$(this).attr('value') || ($(this).attr('name') == 'phone' && !/^[01-9 \+\-\)\(]+$/.test($(this).attr('value')))) && !$(this).attr('disabled')) $(this).addClass('error'); else $(this).removeClass('error');
    }
  })
}
function check_orderform__() {
  var found = false;
  var el = this;
  $('#payinfo .clientInfo input[@type=text]').not('.nocheck').each(function() {
    if (!found) {
      if ((!$(this).attr('value') || ($(this).attr('name') == 'phone' && !/^[01-9 \+\-\)\(]+$/.test($(this).attr('value')))) && !$(this).attr('disabled')) $(this).addClass('error'); else $(this).removeClass('error');
    }
    if (el && el.name && $(this).attr('name') == el.name) found = true;
  })
}
function check_orderform() {
  var st = true;
  $('#payinfo .clientInfo input[@type=text]').not('.nocheck').each(function() {
    if (!$(this).attr('value') && !$(this).attr('disabled')) st = false;
  })
  if (!/^[01-9 \+\-\)\(]+$/.test($('#payinfo .clientInfo input[@name=phone]').attr('value'))) st = false;
  if (st) {
    $('#payinfo .buttons a.primary span').removeClass('disabled').parent().removeClass('disabled');
  } else {
    $('#payinfo .buttons a.primary span').addClass('disabled').parent().addClass('disabled');
  }
}
window.ship_address = false;
function pay_func()
{
  if ($('#payinfo').length == 0) return;
  check_orderform();
  $('#payinfo .datePicker a').bind('click', function() {
    if ($(this).is('.disabled')) return false;
    if ($(this).attr('rel')) $(this).parent().parent().next('span.day').html($(this).attr('rel'));
    $(this).parent().parent().find('a').removeClass('active').parent().find('input').attr('disabled', true);
    $(this).addClass('active').parent().find('input').attr('disabled', false);
    return false;
  });
  $('#payinfo a.cityname').bind('click', function() {
    if ($('#cities').hasClass('citpos3')) {
      $('#cities').hide().removeClass('citpos3').prev().hide();
    } else {
      $('#cities_wrap').show().next().show().removeClass('citpos2').removeClass('citpos1').addClass('citpos3').find('.flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
    }
    return false;
  });
  $('#payinfo #phone-data').bind('blur', function() {
    if (/^[01-9 \+\-\)\(]+$/.test($(this).attr('value')) && $(this).attr('value') != $(this).attr('rel') && !$('#signOut').length) {
      $(this).attr('rel', $(this).attr('value'));
      $.ajax({
          url: '/login.php?action=preload&regid='+$(this).attr('value'),
          type: 'GET',
          dataType: 'json',
          timeout: 2000,
          error: function(){
          },
          success: function(obj){
            if (obj.id) {
              if (!$('#payinfo #fio-data').attr('value')) $('#payinfo #fio-data').attr('value', obj.fio);
              /*if (!$('#payinfo #street').attr('value')) {
                $('#payinfo #street').attr('value', obj.street);
                $('#payinfo #house').attr('value', obj.house);
                $('#payinfo #flat').attr('value', obj.office);
              }*/
              $('#payinfo #addr_place').html(obj.address);
              addr_place();
              window.ship_address = true;
              $('#cities a.q'+obj.id_city).not('.act').trigger('click');
              //clickcity(obj.id_city);
              check_orderform();
            }
          }
      });
    }
  });
  $('#payinfo .payIn input.styled').bind('change', dis_addresses);  
  $('#payinfo .payIn input.styled').bind('change', recalccart_pay);
  $('#payinfo .clientInfo input[@type=text]').bind('keyup', check_orderform).bind('keyup', check_orderform_).bind('blur', check_orderform__).bind('focus', function() {$(this).removeClass('error')});
  $('#payinfo .clientInfo input[@type=radio]').bind('change', check_orderform).bind('keyup', check_orderform_).bind('blur', check_orderform__).bind('focus', function() {$(this).removeClass('error')});
  $('#payinfo .buttons a.primary').bind('click', function() {
    if (!$(this).is('.disabled')) {
      if (1)  {   
        if ($('div.container').length > 1) {
          $('#invis').load(modal_link('/pay/?modal=1&postmodal=1&' + fserialize(document.payform)), function() {
            set_win_title();
            var s = $('#invis').html().split('|');
            // ----------------  autologin  -----------------
            if (s[0] == 5) {
              window.loginblock = s[1];
              window.logincity = s[2];
              window.logincurr = s[3];
              $('#signInNow a.secondary').trigger('click');
              if (window._popup) {
                try { window._popup.close(); } catch(e) {}
              }              
              if ($('div.container[@rel=conflict]').length) {
                while ($('div.container:last').attr('rel') != 'conflict') page_close();               
               page_close();
              }
              //conflict_close();
              //$('#login').html(s[1]);
              //logout_functions();
              //reload_cart();
              //reload_containers();
              var last_href = modal_link('/conflict/');
              $('div.container:last').hide().each(function() {
                var el = document.createElement('div');
                $(el).attr('id', 'container').attr('class', 'container').attr('rel', 'conflict').insertAfter($(this)).load(last_href, function() {
                  set_win_title();
                  //page_func();
                  conflict_func();      
                  Custom.init();
                });
              });    
              set_href('/conflict/');
              return false;
            } else if (s[0] == 4) {
              $('#login').html(s[1]);
              clickcity(s[2]);
              clickcurr(s[3]);
              logout_functions();
              conflict_close();
            } else if (s[0] == 3 || s[0] == 2) {
              $('#regid_err').show().html(s[1]).bind('mouseover', function() {$(this).hide()});
              return false;
            } else if (s[0] == 1) {
            }
            $(this).html('');
            // ----------------  autologin  -----------------
            $('#login a.address2').html($('#payinfo #email-data').attr('value'));
            if ($('#payinfo input[@type=hidden]').filter('[@name=id_payment]').not('[@disabled]').attr('value') == 3) {
              if (window._popup && window._popup.document && window._popup.document.forms[0]) {
                window._popup.document.forms[0].action = '/history_pdf.php';
                window._popup.document.forms[0].submit();
              } else if (!window._popup) {
                window._popup = window.open('/history_pdf.php', '_popup', 'width=800,height=600,scrollbars=1,resizable=1');
              } else {
                window._popup.document.location.href='/history_pdf.php';
              }
            } 
            if ($('#payinfo input[@type=hidden]').filter('[@name=id_payment]').not('[@disabled]').attr('value') == 2) {
              if (window._popup && window._popup.document && window._popup.document.forms[0]) {
                window._popup.document.forms[0].action = '/history_pay.php';
                window._popup.document.forms[0].submit();
              } else if (!window._popup) {
                window._popup = window.open('/history_pay.php', '_popup', 'width=800,height=600,scrollbars=1,resizable=1');
              } else {
                window._popup.document.location.href='/history_pay.php';
              }
            }
            page_close();
            reload_cart();
            var last_href = modal_link('/history/?retmsg=add_order');
            $('div.selectedItem[@rel=c]').hide();
            $('div.container:last').hide().each(function() {
              var el = document.createElement('div');
              $(el).attr('id', 'container').attr('class', 'container').attr('rel', 'history').insertAfter($(this)).load(last_href, function() {
                set_win_title();
                page_func();
                cart_func_history();
                page_func_history();
              });
            });    
            set_href('/history/');
          });    	
        } else {
          document.payform.submit();
        }
      }
    } else {
      check_orderform_();
    }
    return false
  });
  /*    .bind('mouseover', function() {
    if ($(this).is('.disabled')) {
      check_orderform_();
    }
  });*/
  $('#payinfo .payIn label').add('#payinfo .payIn input').bind('click', function() {
    getClassParent($(this), 'payIn').find('.styled').attr('checked', true);
    Custom.clear();
    dis_addresses();
    check_orderform();
    recalccart_pay();
  }); 
  $('#payinfo #address a.addr').bind('click', function() {
    $(this).parent().parent().addClass('hide').next().removeClass('hide');
    return false
  })
  $('#payinfo ul.paytype a').bind('click', function() {
    if (!$(this).hasClass('disabled')) { 
      $('#payinfo .buttons a.primary span').html($(this).attr('rel'));
      if ($(this).parent().hasClass('end') || !$('#cities a.act').hasClass('office')) {
        $('#payinfo ul.time li a').removeClass('control').addClass('disabled').removeClass('active').next().attr('disabled', true);
      } else {
        $('#payinfo ul.time li a').removeClass('disabled').addClass('control');
        $('#payinfo ul.time li.start a').addClass('active').next().attr('disabled', false);
      }
      if ($(this).parent().hasClass('start')) {
        $('#terminal').removeClass('termdis1');
      } else {
        $('#terminal').addClass('termdis1');
      }
      if ($(this).parent().hasClass('end')) {
        $('#payinfo span.day').hide();
        $('#payinfo ul.date li a').removeClass('control').addClass('disabled').removeClass('active').next().attr('disabled', true);
      } else {        
        $('#payinfo span.day').show();
        if (!$('#cities a.act').hasClass('main')) {
          $('#payinfo ul.date li a').not('.dis_nk').removeClass('disabled').addClass('control').removeClass('active').filter('a:first').trigger('click');
            //.addClass('active').next().attr('disabled', false);
        } else {
          $('#payinfo ul.date li a').removeClass('disabled').addClass('control').removeClass('active').filter('a:first').trigger('click'); //.addClass('active').next().attr('disabled', false);
        }
      }
    }
  })
}
$(document).bind('ready', function() {
  conflict_func();
})
function recalcconf()
{
  if ($('#dv_orderedItemsConf .forPayment strong').length == 0) return;
  var total = 0;
  var this_total = 0;
  var this_qty = 0;
  var total_ship = 0;
  var total_cour = 0;
  $('#dv_orderedItemsConf input[@name=price][@rel]').each(function() {
    if ($('#curentBasket').attr('checked')) {
      this_qty = $(this).next().next().attr('value');
    } else if ($('#basket').attr('checked')) {
      this_qty = $(this).next().next().next().attr('value');
    } else if ($('#combineBasket').attr('checked')) {
      if ($('#sum').attr('checked')) {
        this_qty = ($(this).next().next().attr('value')/1 + $(this).next().next().next().attr('value')/1);
      } else {
        this_qty = $(this).next().next().attr('value')/1 ? $(this).next().next().attr('value')/1 : $(this).next().next().next().attr('value')/1; //Math.max($(this).next().attr('value')/1, $(this).next().next().attr('value')/1);
      }
    }
    total_ship += this_qty*$(this).next().attr('value');
    this_total = parseInt($(this).attr('value')*this_qty);
    total += this_total;
    $('#dv_orderedItemsConf').add('div.selectedItem[@rel=f]').find('.price[@rel='+$(this).attr('rel')+']').each(function(){
      $(this).html($(this).html().replace(/[01-9]+/, this_total))
    });
    $('#dv_orderedItemsConf').add('div.selectedItem[@rel=f]').find('.quantity[@rel='+$(this).attr('rel')+']').each(function(){
      $(this).html($(this).html().replace(/[01-9]+/, this_qty))
    });
    if (this_qty > 1) {
      $('#dv_orderedItemsConf').add('div.selectedItem[@rel=f]').find('.quantity2[@rel='+$(this).attr('rel')+']').each(function(){
        $(this).html($(this).html().replace(/[01-9]+/, this_qty)).removeClass('hide');
      });
    } else {
      $('#dv_orderedItemsConf').add('div.selectedItem[@rel=f]').find('.quantity2[@rel='+$(this).attr('rel')+']').each(function(){
        $(this).html($(this).html().replace(/[01-9]+/, this_qty)).addClass('hide');
      });
    }
  });
  if ($('#curentBasket').attr('checked')) {
    $('#dv_orderedItemsConf .orderedTable .q2').addClass('hide');
    $('#dv_orderedItemsConf .orderedTable .q1').removeClass('hide');
  } else if ($('#basket').attr('checked')) {
    $('#dv_orderedItemsConf .orderedTable .q1').addClass('hide');
    $('#dv_orderedItemsConf .orderedTable .q2').removeClass('hide');
  } else if ($('#combineBasket').attr('checked')) {
    $('#dv_orderedItemsConf .orderedTable .q1').removeClass('hide');
    $('#dv_orderedItemsConf .orderedTable .q2').removeClass('hide');
  }
  var acti = 0;
  $('#dv_orderedItemsConf .orderedTable tr').not('.hide').each(function() {
    if (++acti%2 == 1) $(this).addClass('alt'); else $(this).removeClass('alt'); 
  });
  $(this).find('th span').html(acti);
  
  var total_grn = total;
  if ($('#dv_orderedItemsConf input[@name=has_curr]').length == 0) {
    total_grn = total*$('#dv_orderedItemsConf input[@name=exchange]').attr('value');
  }
  var csh = $('#dv_orderedItemsConf input[@name=city_ship]').attr('value').split('|');
  total_ship = Math.round(1*csh[4] + 0.01*csh[1]*total + 0.01*csh[2]*total_ship);
  total_cour = (1*csh[3] && 1*csh[3] < total_grn) ? 0 : 1*csh[0];
  var def_percent = $('#dv_orderedItemsConf input[@name=def_percent]').attr('value').split('|');

  
  if ($('#dv_orderedItemsConf input[@name=has_curr]').length > 0) {
    total_ship = 1*def_percent[0] + 0.01*def_percent[1]*total_grn + 0.01*def_percent[3]*total_cour + 0.01*def_percent[2]*$('#dv_orderedItemsConf input[@name=total_ship]').attr('value')/1 * $('input[@name=exchange]').attr('value');
    $('#dv_orderedItemsConf .delivery span:first').html($('#dv_orderedItemsConf .delivery span:first').html().replace(/[01-9]+/, Math.round(total_ship)));
    if (total_ship) {
      total += Math.round(total_ship);
      $('#dv_orderedItemsConf .delivery span:last').addClass('invis').prev().removeClass('invis');
    } else {
      $('#dv_orderedItemsConf .delivery span:last').removeClass('invis').prev().addClass('invis');
    }
  } else {
    total_ship = (1*def_percent[0] + 0.01*def_percent[1]*total_grn + 0.01*def_percent[3]*total_cour) / $('input[@name=exchange]').attr('value') + 0.01*def_percent[2]*$('#dv_orderedItemsConf input[@name=total_ship]').attr('value')/1;
    $('#dv_orderedItemsConf .delivery span:first').html($('#dv_orderedItemsConf .delivery span:first').html().replace(/[01-9]+/, Math.round(total_ship)));
    if (total_ship) {
      total += Math.round(total_ship);
      $('#dv_orderedItemsConf .delivery span:last').addClass('invis').prev().removeClass('invis');
    } else {
      $('#dv_orderedItemsConf .delivery span:last').removeClass('invis').prev().addClass('invis');
    }
  }
  $('#dv_orderedItemsConf .forPayment strong').html($('#dv_orderedItemsConf .forPayment strong').html().replace(/[1-9][01-9]*/, total));
  $('#dv_orderedItemsConf .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
}
function anti_conflict_func()
{
  window.show_conflict = false;
  $('#service').add('#login').show();
}
function conflict_func()
{
  if ($('#cartconflict').length == 0) return;
  //window.show_conflict = true;
  //$('#service').add('#login').hide();
  $('#dv_orderedItemsConf a.info').bind('click', function() {
     $('div.selectedItem[@rel=f]').hide();
     $(document.body).append($('#infof' + $(this).attr('rel')).show().css({left:($(this).offset().left - 5) + 'px', top:($(this).offset().top - 50) + 'px'}));
    return false;
  });
  $('.conflictSide input.styled').bind('change', function() {
    recalcconf()
    return false;
  })
  $('#cartconflict .buttons a.primary').bind('click', function() {
    if (!$(this).is('.disabled')) {
      if ($('div.container').length > 1) {
        //anti_conflict_func();
        $('#invis').load(modal_link('/conflict/?modal=1&postmodal=1&' + fserialize(document.conf_frm)), function() {
          set_win_title();
          $('#login').html(window.loginblock);
          clickcity(window.logincity);
          clickcurr(window.logincurr);
          $(this).html('');
          logout_functions();
          reload_cart();
          reload_containers();
          page_close();
          Custom.init();
        });    	
      } else {
        document.conf_frm.submit();
      }
    }
    return false
  });
  $('#cartconflict .buttons a.close').bind('click', function() {
    if (!$(this).is('.disabled')) {
      /*anti_conflict_func();
      $('#login').load('/logout.php?modal=1', function() {
        login_functions(); 
        page_close();
        Custom.init();
        reload_containers();
      });*/
      page_close();
    }
    return false
  });
  
  $('#dv_orderedItemsConf a.shipinfo').bind('click', function() {
    var last_href = modal_link($(this).attr('href'));
    $('div.selectedItem[@rel=f]').hide();
    $('div.container:last').hide().each(function() {
      var el = document.createElement('div');
      fpar = 'fr=5';
      $(el).attr('id', 'container').attr('class', 'container').insertAfter($(this)).load(add_params(last_href, fpar), function() {
        set_win_title();
        page_func();
        $('div.container:last .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
      });
    });  
    set_href('/shipment/');  
    return false;
  });
  $('#dv_orderedItemsConf a.det').bind('click', function() {
    var last_href = modal_link($(this).attr('href'));
    $('div.selectedItem[@rel=f]').hide();
    $('div.container:last').hide().each(function() {
      var el = document.createElement('div');
      fpar = 'fr=5';
      fpar2 = 'fr2=3';
      $(el).attr('id', 'container').attr('class', 'container').insertAfter($(this)).load(add_params(last_href, fpar+'&' + fpar2), function() {
        set_win_title();
        item_func();
        $('#dv_detailsSide .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
      });
    });    
    set_href($(this).attr('href'));
    return false;
  });
  
  $('#dv_orderedItemsConf .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
}
