/*
* @version $Id: javascript.js 123 2004-08-18 15:52:58Z jirka $
*/


  function countPackagePrice(product_id){
  
  //var product_id = 1239999;
  
  var idsObj = document.getElementById("packages_products_string");
    
  var pps = idsObj.value.split("*");
  var exists = false;
  if(pps.length > 0){
    for (i = 0; i < pps.length; i++) {
      if( parseInt(product_id) == parseInt(pps[i]) ){
        exists = true; 
        break;
      }      
    }
  }
  
  if( exists == false ){
    if(idsObj.value.length > 0){
      idsObj.value = idsObj.value + "*";
    }
    idsObj.value = idsObj.value+product_id;
  }else{  
    var newValue = new Array(); 
    var p = 0;
    for (i = 0; i < pps.length; i++) {
      if( parseInt(product_id) != parseInt(pps[i]) ){
        newValue[p] = parseInt(pps[i]);
        p++;
      }      
    }
    idsObj.value = newValue.join("*");
   }
  
  var discount = 0;
  
  var totalPrice = parseFloat(document.getElementById("totalpriceproduct").value);
  var prices = new Array();
  
    var arr = document.getElementsByTagName("input");
    
     for (i = 0; i < arr.length; i++) {
       if( /packageinp(?=\d+)/.test(arr[i].id) ){
        var chkboxid = parseInt(arr[i].id.substring(10));        
        document.getElementById("packageinp"+chkboxid).value=0;
        setText("package"+chkboxid,"0");
        }
      }
    
      for (i = 0; i < arr.length; i++) {
       if( /package(?=\d+)/.test(arr[i].id) ){
        var chkboxid = parseInt(arr[i].id.substring(7));        
          if( document.getElementById("package"+chkboxid).checked ){
            var packageprices = document.getElementById("packageprices"+chkboxid);
            if(packageprices){            
              var parsed = packageprices.value.split("*");
              totalPrice += parseFloat(parsed[1]); 
              document.getElementById("packageinp"+parsed[2]).value = parseFloat(document.getElementById("packageinp"+parsed[2]).value) + (parseFloat(parsed[0]) - parseFloat(parsed[1]));   
              setText("package"+parsed[2],document.getElementById("packageinp"+parsed[2]).value);
            }
          }
       }
      }
      
      setText("totalprice",totalPrice);
            
  }
  
  function setText(element,text){
    var obj = document.getElementById(element);
    obj.innerText = obj.textContent = text;
  }


function checkCount(form){
  var count = parseInt(form.count.value);
  if(count > parseInt(form.skladem.value)){
    alert("Můžete objednat od 1 do "+form.skladem.value+" ks této položky.");
    return false;
  }
  
  return true;
}

var showLabels = false;

function otevriOkno(url, jmeno, w, h) 
{  
  popupWin = window.open(url, jmeno, 'resizable=1,menubar=0,scrollbars=no,width=' + w + ',height=' + h);
	popupWin.moveTo( ((screen.availWidth-w)/2),((screen.availHeight-h)/2) );
	popupWin.focus();		
}

function openWindow(url){
	var objWnd = window.open(url,"window","scrollbars=no, width=400, height=150,left=10,top=10");
	objWnd.moveTo( ((screen.availWidth-400)/2),((screen.availHeight-150)/2) );
	objWnd.focus();
}

function chckInfoForm(formular){
	
	var od_jmeno = formular.od_jmeno;
	var od_email = formular.od_email;	
	var pro_email = formular.pro_email;	
	
	if(isEmpty(od_jmeno.value)){
		alert("Vyplňte, prosím, Vaše jméno a příjmení");
		od_jmeno.focus();
		return false;
	}else if(!isEmail(od_email.value)){
		alert("Vyplňte, prosím, Vaši emailovou adresu.");
		od_email.focus();
		return false;
	}else if(!isEmail(pro_email.value)){
		alert("Vyplňte, prosím, emailovou adresu příjemce.");
		pro_email.focus();
		return false;
	}
	
	return true;
}

function isEmail(email) {
    if(isEmpty(email))
	{
	  return false;
	}
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null) return false;
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) 
      {
	    var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
	    if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
return false;
}	

function ukazMenu(id, cesta){
var ikona = document.getElementById("iko_"+id);
var menu = document.getElementById("mnu_"+id);

var zdroj = ikona.src;

var ikona_soubor = zdroj.substr(zdroj.lastIndexOf("/")+1);

switch(ikona_soubor){
	
	case "menu_otevreno.png":
	case "menu_zavreno.png":	
		switch(menu.className){
			case "zavreno":
				menu.className = "otevreno";
				ikona.src = cesta+"/img/menu_otevreno.png";
			break;
			
			default:
				menu.className = "zavreno";	
				ikona.src = cesta+"/img/menu_zavreno.png";							
		}			
	break;
}
}

function checkForm35(){

	var jmeno = document.getElementById("data_name");
	var prijmeni = document.getElementById("data_prijmeni");
	var email = document.getElementById("data_email");
	
	if(isEmpty(jmeno.value) ){
		alert("Vyplňte, prosím, Vaše jméno");
		jmeno.focus();
		return false;
  }else if(isEmpty(prijmeni.value)){  		
		alert("Vyplňte, prosím, Vaše příjmení");
		prijmeni.focus();
		return false;
	}else if(!isEmail(email.value)){
		alert("Vyplňte, prosím, Vaši emailovou adresu.");
		email.focus();
		return false;
	}
	
	document.getElementById("form35").action = "./php/exec.php";
  	
	return true;
  
}

function checkForm31(){

	var jmeno = document.getElementById("data_name");
	var prijmeni = document.getElementById("data_prijmeni");
	var email = document.getElementById("data_email");
	
	if(isEmpty(jmeno.value) ){
		alert("Vyplňte, prosím, Vaše jméno");
		jmeno.focus();
		return false;
  }else if(isEmpty(prijmeni.value)){  		
		alert("Vyplňte, prosím, Vaše příjmení");
		prijmeni.focus();
		return false;
	}else if(!isEmail(email.value)){
		alert("Vyplňte, prosím, Vaši emailovou adresu.");
		email.focus();
		return false;
	}
		
	document.getElementById("form31").action = "./php/exec.php";
  	
	return true;
  
}
