function setPopupMenu(){
  var licko = document.getElementById("li");  
  licko.onmouseover = function() { this.className += ' clover'; }
	licko.onmouseout = function() { this.className = this.className.replace(' clover', '') }
}

function loadThumbnail(id){
  document.getElementById("thumbnail").src=id.src;
  window.loadedThumbnail=id.src;
}

function popup(url,name,width,height){
	var left = (screen.availWidth-width)/2;
  var top = (screen.availHeight-height)/2;
  var popupWin = window.open(url,name,'scrollbars=no,location=no,directories=no,status=no,resizable=yes,menubar=no,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top);
	popupWin.focus();
}

function openParam(url){
  popup(url,"param",410,200);
}

function showThumbnail(){
  var imgObject = document.getElementById("thumbnail");
  var imgSrc = new String(imgObject.src);
  var newImg = new Image();
  newImg.src =imgSrc; 
  var path = imgSrc.split("/");    
  popup(MS_ESHOPROOT+"/catalog/imgbyname/?all=1&filename="+path[path.length-1],'img',630,755);        
}

function showImg(image_id,width,height){
  popup(MS_ESHOPROOT+"/catalog/img/?image_id="+image_id,'img',width,height);        
}

function cartAdd(product_id){
   var count = parseInt(document.getElementById("cart"+product_id).value);
   document.getElementById("cart"+product_id).value = count+1;        
}

function cartSubstract(product_id){
   var count = parseInt(document.getElementById("cart"+product_id).value);
   if(count > 0){
    document.getElementById("cart"+product_id).value = count-1;
   }        
}

function _ge(id){
  return document.getElementById(id);
}

function searchDisplayParameters(){
  //if(_ge("srchdepartment_id").indexOf("|") !== -1){
  	window.location = MS_ESHOPROOT+"/search/?q="+_ge("srchq").value+"&pricemin="+_ge("srchpricemin").value+"&pricemax="+_ge("srchpricemax").value+"&producer_id="+_ge("srchproducer_id").value+"&department_id="+_ge("srchdepartment_id").value;  
  //}
}

function defText(field,deftext){
  if (_ge(field).value == deftext) {
		_ge(field).value='';
	} else if (_ge(field).value == '') {
		_ge(field).value=deftext;
	}
}

function resetProducers(array,value){
  var inputs = _ge('filterform').elements;
  for(var i=0; i<inputs.length;i++){
    if(inputs[i].id.indexOf('producer_') == 0 && inputs[i].checked){
    inputs[i].checked=false;
    }           
  }
  resetParameter();
}

function resetParameter(loc){

  var url = new String(window.location);
  var inputs = _ge('filterform'+loc).elements;
  var query_string = new Array;
  var producers = new Array;
  var gotofirstpage = true;
  
  for(var i=0; i<inputs.length;i++){
    
    if(inputs[i].id=="dir"+loc && _ge("dir"+loc).value){
    query_string[query_string.length] = "dir="+_ge("dir"+loc).value;
    }else if(inputs[i].id=="par"+loc){
    query_string[query_string.length] = "par="+_ge("par"+loc).value;
    }else if(inputs[i].id.indexOf('list_') == 0){    
      if(_ge("listtype"+loc).value != inputs[i].value){
        gotofirstpage = true;
      }    
      query_string[query_string.length] = inputs[i].name+"="+_ge(inputs[i].id).value;
    }else if(inputs[i].id.indexOf('producer_') == 0 && inputs[i].checked){
      producers[producers.length] = _ge(inputs[i].id).value;
    }else if(inputs[i].id=="p"+loc && parseInt(_ge("p"+loc).value) > 1){
      //query_string[query_string.length] = "p="+_ge("p"+loc).value;
    }else if(inputs[i].id=="q"+loc && _ge("q"+loc).value){
    query_string[query_string.length] = "q="+_ge("q"+loc).value;
    }else if(inputs[i].id=="products_per_page"+loc){
      query_string[query_string.length] = "products_per_page="+_ge("products_per_page"+loc).value;
    }           
    
    
  }
  
  var pricefilter = _ge("pricefilter");
  
  if(pricefilter && pricefilter.value != "-1"){
    query_string[query_string.length] = "pricefilter="+_ge("pricefilter").value;
  }
  
  var producerfilter = _ge("producerfilter");
  
  if(producerfilter && parseInt(producerfilter.value) > 0){
    query_string[query_string.length] = "producerfilter="+_ge("producerfilter").value;
  }    
  
  var groupfilter = _ge("groupfilter");
  
  if(groupfilter && parseInt(groupfilter.value) > 0){
    query_string[query_string.length] = "group_id="+groupfilter.value;
  }
  
  if(producers.length > 0){
    query_string[query_string.length] = "producers="+producers.join(",");
  }
  
  if(query_string.length > 0){
    newlocation=_ge("category_url"+loc).value+"?"+query_string.join("&");
            
    if(producers.length > 0 || gotofirstpage){
  		newlocation = newlocation.replace(new RegExp('p=([0-9]+)','g'),'');
    }
        
    window.location = newlocation;
  }

  return true;
}

function setDirection(direction){
  _ge("dir").value=direction;
  resetParameter();
}

function recountCredits(){

  if(document.getElementById("usecredits").checked){
    var credits = "y";
  }else{
    var credits = "n";
  }
  
  var url = "" + window.location;  
  var url = url.substring(0, url.indexOf('?'));
  
  document.getElementById("usecredits").value=credits;  
  window.location.replace(url+"?crdts="+credits);
  
}

function goTo(url){
  location.replace(url);
}

function cetelem(product_id){
  popup(MS_ESHOPROOT+"/catalog/cetelem/?product_id="+product_id,'img',475,575);
}

function cetelemRecount(){
  
  
  var iframe = _ge("resultiframe");
  var vyseUveru = 0;
  var primaPlatba = 0;
  var pocetSplatek = 0;
  
  _ge("cetelemform").submit();
    
  /*iframe.src=MS_ESHOPROOT+"/catalog/cetelem/count/?kodProdejce="+_ge("kodProdejce").value+"&kodBaremu="+_ge("kodBaremu").value+"&kodPojisteni="+_ge("kodPojisteni").value+"&kodMaterialu="+_ge("kodMaterialu").value+"&cenaZbozi="+_ge("cenaZbozi").value+"&vyseUveru="+vyseUveru+"&primaPlatba="+primaPlatba+"&pocetSplatek="+pocetSplatek;*/
  
  
  
}

/***** BEINSTYLE.CZ MODIFICATION ****************************************************************************/

function selectProducer(){
  var select = _ge("srchproducer_id");
  if(select){
    var producer_id = parseInt(select.options[select.options.selectedIndex].value);   
    if(producer_id > 0){
      window.location = MS_ESHOPROOT+"/catalog/producer/"+producer_id+"/";
    }  
  }
}

function selectDepartment(){
  var select = _ge("department_id");
  if(select){
    var category_id = parseInt(select.options[select.options.selectedIndex].value);   
    if(category_id > 0){
      window.location = MS_ESHOPROOT+"/catalog/switcher/"+category_id+"/";
    }  
  }
}

// Switcher akce emailoveho zpravodaje dle vyberu radia na titulce.
function newsletterOnSubmit(theForm){

  if(_ge("newsletteremail")){

    var validator = new Validator();
  
    if(validator.isEmail(_ge("newsletteremail").value)){
      
      if(_ge("newslettersub") && _ge("newslettersub").checked){
        theForm.action = MS_ESHOPROOT+"/newsletter/add/";
        return true;        
      }
      
      if(_ge("newsletterunsub") && _ge("newsletterunsub").checked){
        theForm.action = MS_ESHOPROOT+"/newsletter/remove/";
        return true;
      }
    
    }else{
    
      alert(_ge("nwsemailmsg").value);
    
    }
  
  }else{
  
    alert(_ge("nwsemailmsg").value);
  
  }
  
  return false;
}
