function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function updateProductList(action, offset) {
   if (action != null && action != '' && action.substring(0, 1) == '+') {
    eid = action.substring(1);
    if ($(eid).hasClassName('selected')) {
      action = '-' + action.substring(1);
    }
  }
  if($('kss-spinner'))
  {
    $('kss-spinner').style.display = 'block';
  }
  var ajax = new Ajax.Request(String(document.location), {
    method: 'post',
    parameters: 'mode=list&action=' + action + '&batch_start=' + (offset == null ? 0 : offset) + '&SearchableText' + (gup('SearchableText')),
    onFailure: function(request) {
      alert('Error occured while communicating with the AJAX server!');
      spinner = $('kss-spinner');
      spinner.hide();
    },
    onSuccess: function(request) {
      $('product-list').innerHTML = request.responseText;
      var f = $('product-list').select('script');
      for (var i=0; i<f.length; i++) {
          eval(f[i].innerHTML);
      }
      if($('kss-spinner'))
      {
        $('kss-spinner').style.display = 'none';
      }
      // highlighting
      if (action != null && action != '' && action.substring(0, 1) == '+') {
        eid = action.substring(1);
        if (!$(eid).hasClassName('selected')) {
          $(eid).addClassName('selected');
        }
      } else if (action == '-*') {
        a = $$('#category_list a.selected');
        for (i = 0; i < a.length; i++) {
          a[i].removeClassName('selected');
        }
      } else if (action != null && action != '' && action.substring(0, 1) == '-') {
        eid = action.substring(1);
        if ($(eid).hasClassName('selected')) {
          $(eid).removeClassName('selected');
        }
      }

    }
  });
  // + (search!='' ? '?SearchableText=' + search : '')
  // search = gup('SearchableText');
}

function updateEnabledCategories(categories) {
  var enabled_categories = (categories != '' ? categories.split(',') : null);
  a = $$('#category_list a');
  for (i = 0; i < a.length; i++) {
    if (categories != '' && enabled_categories.indexOf(a[i].id) != -1 || categories == '') {
      if (a[i].hasClassName('disabled')) {
        a[i].removeClassName('disabled');
      }
    } else if (categories != '') {
      if (!a[i].hasClassName('disabled')) {
        a[i].addClassName('disabled');
      }
    }
  }
}

function popupIn(rollover, back) {
  $(rollover).show();
  $(back).setStyle({
    opacity: 0
  });
  new Effect.Opacity(back, { from: 0, to: 0.7, duration: 0.3 });
}
  
function popupOut(rollover, back) {
  new Effect.Opacity(back, { from: 0.7, to: 0, duration: 0.3 });
  setTimeout('$("' + rollover + '").hide();', 200);
}

function infoIn(info, dummy) {
  $(dummy).hide();
  $(info).show();
}

function infoOut(info, dummy) {
  $(info).hide();
  $(dummy).show();
}

function button_over(n, t) {
  value = document.getElementById(n).src;
  document.getElementById(n).src = value.replace('.' + t, '_over.' + t);
}

function button_out(n, t) {
  value = document.getElementById(n).src;
  document.getElementById(n).src = value.replace('_over.' + t, '.' + t);
} 

function popUpProductSelect(url) {
  var qs = new Hash();
  var aj = new Ajax.Request(url,
      {method: 'GET', parameters: qs, asynchronous: false,
      onSuccess: function(res) {
        dboxOpen('visual-portal-wrapper', '', res.responseText, true);
      }
  });
}

/**
 * Message box
 */
function dboxOpen(container, title, contents)
{
  dboxCloseT(container);
  // style
  var stxt = '';
  if(arguments[5] != undefined)
  {
    var arg = arguments[5];
    for(var i in arg)
    {
      stxt += i + ':' + arg[i] + ';';
    }
  }
  // form
  var frm = '';
  var frmC = '';
  if(arguments[6] != undefined)
  {
    var arg = arguments[6];
    for(var i in arg)
    {
      frm += i + '="' + arg[i] + '" ';
    }
    frm = '<form ' + frm + '>';
    frmC = '</form>';
  }
  if(title != '') title = '<h1 id="dbox_title">' + title + '</h1>';
  var html ='\
<div id="dbox_modal"></div>\
<div id="dbox" style="' + stxt + '">\
' + frm + '\
  <div class="dbox_container">\
    ' + title +'\
    <div id="dbox_content"></div>\
    <div id="dbox_content_x"></div>\
    <div id="dbox_button_c" style="display:none" class="clearfix">\
      <span id="dbox_cc"><input id="dbox_cancel" class="submit" type="button"\
        value="Cancel" onclick="dboxClose(\'' + container + '\')"/></span>\
    </div>\
  </div>\
' + frmC + '\
</div>';
  $(container).insert({'bottom': html});
  $('dbox_content').innerHTML = contents;
   $('dbox').setStyle({
    left: '50%',
    marginLeft: '-' + parseInt($('dbox').getStyle('width'))/2 +  'px'
  });
  // modal
  var modal = true;
  if(arguments[3] != undefined)
    modal = arguments[3];
  if(!modal)
    $('dbox_modal').style.display = 'none';
  // buttons
  if(arguments[4] != undefined)
  {
    var arg = arguments[4];
    for(var i in arg)
    {
      if(i == 'cancel')
      {   
          if(arg[i].caption != undefined)
              $('dbox_cancel').value = arg[i].caption;
          if(arg[i].cls != undefined)
              $('dbox_cancel').className = arg[i].cls;
          if(arg[i].onclick != undefined)
              $('dbox_cancel').attributes['onclick'].value = 
                  "dboxClose('" + container + "');" + arg[i].onclick;
      }
      else
      {
        var cls = 'submit';
        if(arg[i].cls != undefined)
            cls = arg[i].cls;
        var ocl = '';
        if(arg[i].onclick != undefined)
          ocl = arg[i].onclick;
        var caption = i;
        if(arg[i].caption != undefined)
          caption = arg[i].caption;
        var type = 'button';
        if(arg[i].type != undefined)
          type = arg[i].type;
        var btn = '<input class="' + cls + '" value="' + caption +
            '" type="' + type + '" onclick="' + ocl + '"/>';
        $('dbox_button_c').insert({'bottom': btn});
      }
    }
  }
  new Effect.Opacity('dbox', { from: 0, to: .9, duration: 0.5 });
  $('dbox_content').setStyle({
    opacity: 1
  });
}

Event.observe(document, "keypress",
  function(event) {
      if(event.keyCode == 27)
      {
        dboxClose('');
      }
  });

function dboxClose(container)
{
  try
  {
    $('dbox_modal').remove();
    new Effect.Opacity('dbox', { from: .7, to: 0, duration: 0.5 });
    setTimeout("dboxCloseT('" + container + "')", 255);
  }
  catch(e){}
}

function dboxCloseT(container)
{
  try
  {
    $('dbox').remove();
    $('dbox_modal').remove();
  }
  catch(e){}
}

/* toggle product images */
function changeProductPhoto(iUrl)
{
  iUrl = iUrl==''? $('product_img1_url').value :iUrl;
  $('product_img1').src = iUrl;
}

function copyData() {
  if ($('samefields').checked == true) {
    $('shipping_fullname').value = $('fullname').value;
    $('shipping_address').value = $('address').value;
    $('shipping_zipcode').value = $('zipcode').value;
    $('shipping_city').value = $('city').value;
    $('shipping_province').value = $('province').value;
    $('shipping_country').value = $('country').value;
  }
  else {
    $('shipping_fullname').value = '';
    $('shipping_address').value = '';
    $('shipping_zipcode').value = '';
    $('shipping_city').value = '';
    $('shipping_province').value = '';
    $('shipping_country').value = '';    
  }
}

function play_pause(pause, play) {
  // TODO: fix play / pause
  if ( $(pause).hasClassName("paused") ) {
    $(play).hide();
    $(pause).show();
    $(pause).removeClassName("paused");
    px = startScroll();
  }
  else {
    $(pause).hide();
    $(play).show();
    $(pause).addClassName("paused");
    stopScroll();
  }
}
