$j(document).ready(function(){

  $j('#advanced-search-show').click(function(){
  	$j('#advanced-search').fadeIn('fast');
  })
  
  $j('#advanced-search-hide').click(function(){
  	$j('#advanced-search').fadeOut('fast');
  })
  
  $j("a[rel^='prettyPhoto']").prettyPhoto();
	  
});


function addToCart(obj,product_id,variant_id,boxes,event) {

    if( !NumericFieldsTest(event) ) return false;
    
    if (CheckKeyPress(event,13) || CheckKeyPress(event,9) || CheckKeyPress(event,38) || CheckKeyPress(event,40))
    {
		obj.style.background = '#FFCB8F';
		
		if(!CheckKeyPress(event,9) && (CheckKeyPress(event,13) || CheckKeyPress(event,40))) doNext(obj);
		if(CheckKeyPress(event,38)) doPrev(obj);
    } 
    else {
        limitText(event,obj,5);
		obj.style.background = '#FFFFFF';
    }
}

function addToCart2(obj,product_id,variant_id,boxes) {

    var real_value = '';
    if(obj.value) {
	    real_value = parseInt(obj.value);
	    if(real_value%boxes != 0) {
	      var modulo = real_value%boxes;
	      real_value = real_value + (boxes-modulo);
	    }
	    
	    obj.value = real_value;
    }
    
    xajax_addToCart(real_value,product_id,variant_id);
	obj.style.background = '#FFCB8F';
}


function updateCart(obj,product_id,variant_id,boxes,event) {

    if( !NumericFieldsTest(event) ) return false;

    if (CheckKeyPress(event,13) || CheckKeyPress(event,9) || CheckKeyPress(event,38) || CheckKeyPress(event,40))
    {
		obj.style.background = '#FFCB8F';
		
		if(!CheckKeyPress(event,9) && (CheckKeyPress(event,13) || CheckKeyPress(event,40))) { doNext(obj,2); }
		if(CheckKeyPress(event,38)) { doPrev(obj,2); }
		if(CheckKeyPress(event,9)) doNext(obj);
    } 
    else {
        limitText(event,obj,5);
		obj.style.background = '#FFFFFF';
    }
}

function updateCart2(obj,product_id,variant_id,boxes) {

    var real_value = '';
    if(obj.value) {
	    real_value = parseInt(obj.value);
	    if(real_value%boxes != 0) {
	      var modulo = real_value%boxes;
	      real_value = real_value + (boxes-modulo);
	    }
	    
	    obj.value = real_value;
    }
    
	xajax_updateCart1(real_value,product_id,variant_id);
	obj.style.background = '#FFCB8F';
  
}

function updateDirect(obj,iter,event) {

    if( !NumericFieldsTest(event) ) return false;

    if (CheckKeyPress(event,13) || CheckKeyPress(event,9) || CheckKeyPress(event,39) || CheckKeyPress(event,37))
    {
		xajax_updateDirect(iter, obj.value);
		obj.style.background = '#FFCB8F';
		
        if(!CheckKeyPress(event,9) && (CheckKeyPress(event,13) || CheckKeyPress(event,39))) doNext(obj);
		if(CheckKeyPress(event,37)) doPrev(obj);
    } 
    else {
		obj.style.background = '#FFFFFF';
    }
}


function addToCartDirect(obj,iter,event) {

if (CheckKeyPress(event,13) || CheckKeyPress(event,9) || CheckKeyPress(event,39) || CheckKeyPress(event,37) || CheckKeyPress(event,38) || CheckKeyPress(event,40))
    {
		obj.style.background = '#FFCB8F';
		
        if(!CheckKeyPress(event,9) && (CheckKeyPress(event,13) || CheckKeyPress(event,40) || CheckKeyPress(event,39))) {
          
          addNewRow(iter);
          doNext(obj);
        }
		if(!CheckKeyPress(event,9) && (CheckKeyPress(event,38) || CheckKeyPress(event,37))) doPrev(obj);
		
		if(CheckKeyPress(event,9)) {
		  addNewRow(iter);
		}
    } 
    else {
        limitText(event,obj,5);
		obj.style.background = '#FFFFFF';
    }
}

function addNewRow(iter) {
  if(MSIE) var displayvalue = "block";
  else var displayvalue = "table-row";
  if($('row'+(iter+1)) && $('row'+(iter+1)).style.display=="none")  $('row'+(iter+1)).style.display=displayvalue;
  if($('row'+(iter+2)) && $('row'+(iter+2)).style.display=="none")  $('row'+(iter+2)).style.display=displayvalue;
}

function addToCartDirect2(obj,iter) {

    var boxes = $('product_boxes_'+iter).innerHTML;
        
    var real_value = '';
    if(obj.value) {
	    real_value = parseInt(obj.value);
	    if(real_value%boxes != 0) {
	      var modulo = real_value%boxes;
	      real_value = real_value + (boxes-modulo);
	    }
	    
	    obj.value = real_value;
    }
	    
	xajax_addToCart(real_value,0,0,$('product_name_'+iter).value,iter);
	obj.style.background = '#FFCB8F';
}



function NumericFieldsTest(event) {

  if(CheckKeyPress(event,13) || CheckKeyPress(event,9) || CheckKeyPress(event,39) || CheckKeyPress(event,37) 
      || CheckKeyPress(event,8) || CheckKeyPress(event,38) || CheckKeyPress(event,40) || CheckKeyNumericPress(event) ) {
      
      return true;
  }
  else return false;

}

function limitText(event, limitField, limitNum) {
	if (limitField.value.length >= limitNum && !CheckKeyPress(event,8)) {
		limitField.value = limitField.value.substring(0, limitNum-1);
	} 
}


function doNext(el,jump)
{
    if(!jump) jump = 1;
	var f = el.form;
	var els = f.elements;
	var x, nextEl;
	for (var i=0, len=els.length; i<len; i++){
		x = els[i];
		if (el == x && (nextEl = els[i+jump])){
		   if (nextEl.focus) nextEl.focus();
		}
	}
}

function doPrev(el,jump)
{
    if(!jump) jump = 1;
	var f = el.form;
	var els = f.elements;
	var x, prevEl;
	for (var i=0, len=els.length; i<len; i++){
		x = els[i];
		if (el == x && (prevEl = els[i-jump])){
		   if (prevEl.focus) prevEl.focus();
		}
	}
}


function showInvoice(element_id) {

	$(element_id).style.display = 'block';
    $('customer_invoice_name').className = '';
    $('customer_invoice_address').className = '';
    $('customer_invoice_postal').className = '';
    $('customer_invoice_city').className = '';
    $('customer_invoice_country').className = '';
    $('customer_invoice_nip').className = '';
}

function hideInvoice(element_id) {

	$(element_id).style.display = 'none';
    $('customer_invoice_name').className = 'vno';
    $('customer_invoice_address').className = 'vno';
    $('customer_invoice_postal').className = 'vno';
    $('customer_invoice_city').className = 'vno';
    $('customer_invoice_country').className = 'vno';
    $('customer_invoice_nip').className = 'vno';
}

function setCartButton(product_id,variant_id,cart_page_id) {
  $("cartbutton_"+product_id).onclick = function() { xajax_addToCart(product_id,variant_id,cart_page_id); };
}

function changeTab(element_id) {
  $j('.tabs li').removeClass('active');
  $j('.info .content').css('display','none');
  
  $j('#link_'+element_id).addClass('active');
  $j('#div_'+element_id).css('display','block');
}
