function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function setCookie(key, value, days, path, domain, secure) {
   value = encodeURIComponent(value);
   value+='; domain='+domain;
   value+='; path='+path;
   var date = new Date();
   date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
   value+='; expires=' + date.toGMTString();
   if (secure) value+='; secure';
   document.cookie = key + '=' + value;
}

function checkout_get_price(){
   
   if(obj = document.getElementById('usr_logged')) var usr_logged = obj.value;
   if(obj = document.getElementById('usr_id')) var usr_id = obj.value;
   if(obj = document.getElementById('no_logged_id')) var no_logged_id = obj.value;
   if(obj = document.getElementById('shipping_id')) var shipping_id = obj.value;
   if(obj = document.getElementById('payment_id')) var payment_id = obj.value;
   if(obj = document.getElementById('voucher_code')) var voucher_code = obj.value;
   
   var so1 = new sack();
	so1.requestFile = '/ajax/checkout_get_price.php';
	so1.method = 'GET';
	so1.element = 'checkout_price_updt';
	so1.runAJAX('usr_logged='+usr_logged+'&usr_id='+usr_id+'&no_logged_id='+no_logged_id+'&shipping_id='+shipping_id+'&payment_id='+payment_id+'&voucher_code='+voucher_code);
}

$(document).ready(function(){
	
	// KATEGORIE PRODUKTU
	
   //$('.acc_container').hide();
   //$('.acc_trigger:first').addClass('active').next().show();
   /*
   $('.acc_trigger').click(function(){
   	if( $(this).next().is(':hidden') ) {
   		$('.acc_trigger').removeClass('active').next().slideUp();
   		$(this).toggleClass('active').next().slideDown();
   	}
   	return false;
   });
   */
   // NOVINKY NA TITULCE
   
   $('.hp_news_p').hide();
   $('.hp_news_a:first').next().show();
   
   $('.hp_news_a').click(function(){
   	if( $(this).next().is(':hidden') ) {
   		$('.hp_news_a').next().slideUp();
   		$(this).next().slideDown();
   	}
   	return false;
   });

   // POPUP ODKAZY
   
   $("a.popup").click(function(){
      var adresa = $(this).attr('href');
      window.open(adresa, "nove_okno", "width=580,height=720,scrollbars=yes");
      return false;
   });

});

function show_map(id){
   
   if(id == '1'){
      $("#contact_map_2").css('z-index', '-1');
      $("#contact_map_1").css('z-index', '1');
      $("#contact_map_gps_2").css('display', 'none');
      $("#contact_map_gps_1").css('display', '');
      $("#contact_map_a_2").removeClass('now');
      $("#contact_map_a_1").addClass('now');
   }
   else if(id == '2'){
      $("#contact_map_1").css('z-index', '-1');
      $("#contact_map_2").css('z-index', '1');
      $("#contact_map_gps_1").css('display', 'none');
      $("#contact_map_gps_2").css('display', '');
      $("#contact_map_a_1").removeClass('now');
      $("#contact_map_a_2").addClass('now');
   }
   
}

function is_delivery_else_check(val){
   
   document.getElementById('d_adress_box').style.display = (val) ? '' : 'none';
   /*
   ids_source = new Array('f_name', 'f_surname', 'mail', 'tel', 'f_company', 'f_street', 'f_city', 'f_zipcode', 'f_state');
   ids_target = new Array('d_name', 'd_surname', 'd_mail', 'd_tel', 'd_company', 'd_street', 'd_city', 'd_zipcode', 'd_state');
   
   for(i = 0; i < ids_source.length; i++){
      if((obj_source = document.getElementById(ids_source[i])) && (obj_target = document.getElementById(ids_target[i]))){
         if(!val){
            obj_target.value = obj_source.value;
         }
         else{
            obj_target.value = '';
         }
      }
   }
   */
}

function checkout_submit(){
   
   var chyby = 0;
   
   ids = new Array('f_name', 'f_surname', 'f_mail', 'f_tel', 'f_street', 'f_city', 'f_zipcode', 'f_state');
   
   for(i = 0; i < ids.length; i++){
      if(obj = document.getElementById(ids[i])){
         if(trim(obj.value) == ''){chyby += 1; obj.className = 'text err';}else{obj.className = 'text';}
      }
   }
   
   if(document.getElementById('is_company').checked){
      
      ids = new Array('f_company', 'f_ico');
   
      for(i = 0; i < ids.length; i++){
         if(obj = document.getElementById(ids[i])){
            if(trim(obj.value) == ''){chyby += 1; obj.className = 'text err';}else{obj.className = 'text';}
         }
      }
      
   }
   
   if(document.getElementById('is_delivery_else').checked){
      
      ids = new Array('d_name', 'd_surname', 'd_mail', 'd_tel', 'd_street', 'd_city', 'd_zipcode', 'd_state');
   
      for(i = 0; i < ids.length; i++){
         if(obj = document.getElementById(ids[i])){
            if(trim(obj.value) == ''){chyby += 1; obj.className = 'text err';}else{obj.className = 'text';}
         }
      }
      
   }
   
   if(chyby == 0){document.forms.checkout.submit();}else{alert('Vyplňte všechna povinná pole označená hvězdičkou.')}
   
}

function contact_send(){
   
   var chyby = 0;
   
   ids = new Array('k_name', 'k_mail', 'k_text');
   
   for(i = 0; i < ids.length; i++){
      if(obj = document.getElementById(ids[i])){
         if(trim(obj.value) == ''){chyby += 1; obj.className = 'text err';}else{obj.className = 'text';}
      }
   }
   
   if(chyby == 0){document.forms.kontakt.submit();}else{alert('Vyplňte všechna povinná pole označená hvězdičkou.')}
   
}

function link_send(){
   
   var chyby = 0;
   
   ids = new Array('k_name', 'k_mail');
   
   for(i = 0; i < ids.length; i++){
      if(obj = document.getElementById(ids[i])){
         if(trim(obj.value) == ''){chyby += 1; obj.className = 'text err';}else{obj.className = 'text';}
      }
   }
   
   if(chyby == 0){document.forms.send_link.submit();}else{alert('Vyplňte všechna povinná pole označená hvězdičkou.')}
   
}

function cart_send(){
   
   var chyby = 0;
   
   if(!document.getElementById('krab').checked && !document.getElementById('stah').checked){
      
      chyby += 1;
      document.getElementById('cart_error').style.display = '';
      
   }
   
   if(chyby == 0){document.forms.cart.submit();}
   
}

function reg_submit() {
   
   var chyby = 0;
   
   ids = new Array('name', 'surname', 'mail', 'tel', 'pass', 'pass_2');
   
   if(document.getElementById('is_company').checked){
      ids[ids.length] = 'f_company';
      ids[ids.length] = 'f_ico';
   }
   
   for(i = 0; i < ids.length; i++){
      if(obj = document.getElementById(ids[i])){
         if(trim(obj.value) == ''){chyby += 1; obj.className = 'text err';}else{obj.className = 'text';}
      }
   }
   
   if(document.getElementById('pass_2').value != document.getElementById('pass').value){
      chyby += 1;
      document.getElementById('pass_2').className = 'text err';
   }
   else{
      document.getElementById('pass_2').className = 'text';
   }
   
   if(chyby == 0){document.forms.reg.submit();}else{alert('Vyplňte všechna povinná pole označená hvězdičkou.')}
}

function user_submit() {
   
   var chyby = 0;
   
   ids = new Array('name', 'surname', 'mail', 'tel');
   
   for(i = 0; i < ids.length; i++){
      if(obj = document.getElementById(ids[i])){
         if(trim(obj.value) == ''){chyby += 1; obj.className = 'text err';}else{obj.className = 'text';}
      }
   }
   
   if(document.getElementById('ch_pass').checked){
      
      pass = document.getElementById('pass');
      pass_2 = document.getElementById('pass_2');
      pass_old = document.getElementById('pass_old');
      
      if(trim(pass.value) == ''){chyby += 1; pass.className = 'text err';}else{pass.className = 'text';}
      if(trim(pass_2.value) == ''){chyby += 1; pass_2.className = 'text err';}else{pass_2.className = 'text';}
      if(trim(pass_old.value) == ''){chyby += 1; pass_old.className = 'text err';}else{pass_old.className = 'text';}
      if(pass_2.value != pass.value){chyby += 1; pass_2.className = 'text err';}else{pass_2.className = 'text';}
      
   }
   
   if(chyby == 0){document.forms.user.submit();}else{alert('Vyplňte všechna povinná pole označená hvězdičkou.')}
}

function toggle_order(id){
   
   obj = document.getElementById('order_'+id);
   obj.className = (obj.className == 'obj_box') ? 'obj_box obj_open' : 'obj_box';
   
}
