function ShowImage1(path_to_nahled, image_name, id, width, height)
{
  if(width == 0)
  {
  	width = 100;
  }
  if(height == 0)  
  {
  	height = 100;
  }
  window.open(encodeURI(path_to_nahled+"nahled.php?img=" + image_name + "&id=" + id), "_blank", "toolbar=no,scrollbars=no,location=no,status=no,width="+width+",height="+height+",resizable=1,screenX=150,screenY=150", false);
}

function ShowEmailWindow(path_to_form, id, width, height)
{
  window.open(encodeURI(path_to_form+"formular/index.php?id="+id+""), "_blank", "toolbar=no,scrollbars=no,location=no,status=no,width="+width+",height="+height+", resizable=0,screenX=150,screenY=150", false);
}

function VymazText()
{
	if( document.getElementById('searchtext').value == 'hledané slovo, kód výrobku') {
		document.getElementById('searchtext').value='';
	}
}


function ShowImage2(path_to_nahled, image_name, width, height)
{
  window.open(encodeURI(path_to_nahled+"nahled2.php?img=" + image_name), "_blank", "toolbar=no,scrollbars=no,location=no,status=no,width="+width+",height="+height+",resizable=0,screenX=150,screenY=150", false);
}

function _recalc()
{
    var theForm = document.getElementById("order");
    theForm.__EVENTARGUMENT.value = "recalc";
    theForm.submit();
}



function validate_order(formular) {
  if (!formular.jmeno.value) {
	alert_me("jméno");
	formular.jmeno.focus();
	return false;
  }
  if (!formular.prijmeni.value) {
	alert_me("příjmení");
	formular.prijmeni.focus();
	return false;
  }

  if (!formular.ulice.value) {
	alert_me("ulice");
	formular.ulice.focus();
	return false;
  }


  if (!formular.obec.value) {
	alert_me("obec");
	formular.obec.focus();
	return false;
  }

  if (!formular.PSC.value) {
	alert_me("PSČ");
	formular.PSC.focus();
	return false;
  }
  
  if(formular.email.value.length==0 || !validate_email(formular.email.value))
  {
	alert_me('email. Emailová adresa nemá platný formát!');
	formular.email.focus();
	return false;
  }

  return validate_shipping_payment(formular);
}

function validate_shipping_payment(formular)
{
  // test je-li zvolen nějaký způsob dopravy a platby
  var strName = "ship";
  var select = false;
  arrFull = fullShipping.split("|");
  for(i=0;i<arrFull.length;i++)
  {
    if(document.getElementById(strName + arrFull[i].toString()).checked == true)
    {
        select = true;
    }
  }
  if(select == false)
  {
    alert_me("způsob dopravy");
    return false;
  }



  strName = "pay";
  arrFull = fullPayment.split("|");
  select = false;
  for(i = 0; i < arrFull.length; i++)
  {
    if(document.getElementById(strName + arrFull[i].toString()).checked == true)
    {
        select = true;
    }
  }

  if(select == false)
  {
    alert_me("způsob platby");
    return false;
  }

  return true;

}

// validate email
function validate_email(email) {
   var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
   if (filter.test(email))
      return true;
   else
      return false;
}


function oznac($form_name,$switcher) {
   //$form_handle  = document[$form_name];
   $form_handle = $form_name;
   $number_items = $form_handle.elements.length;

   for (var $i = 0; $i < $number_items; $i++) {
      if ($form_handle.elements[$i].name == $switcher) {
         $form_handle.elements[$i].checked = !$form_handle.elements[$i].checked;
         //($switcher) ? true : false;
      }
   }
}


function alert_me(co) {
	window.alert("Prosíme, vyplňte položku: " + co);
}


function __doPostBack(parentForm, eventTarget, eventArgument) {
	var theForm = document.forms[parentForm];
	if (!theForm) {
	    theForm = document.getElementById(parentForm);
	}

	theForm.__EVENTTARGET.value = eventTarget;
    theForm.__EVENTARGUMENT.value = eventArgument;

    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.submit();
    }
}

function submit_order()
{  
    var theForm = document.getElementById("order");    
    if(theForm.__EVENTARGUMENT.value == "recalc")
    {
        // return validate_shipping_payment(theForm);
        return true;
    }
    else
    {        
        return validate_order(theForm);    
    }
}




function previous_page(formname) {
  formik = document.getElementById(formname);
  formik.event.value = "previous";
  formik.submit();
}

function next_page(formname) {
  formik = document.getElementById(formname);
  formik.event.value = "next";
  formik.submit();
}

function Check_Key(e) {
   var $key = 0;

   if (document.all) {
      $key = window.event.keyCode;
   } else {
      $key = e.which;
   }

   if ($key == 13) {
      document.forms.log.submit();
	}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function open_compare_window(){
	var f = document.forms.produkty;
	var first = true;
	var list = "";
	var id;
	var count = 0;

	for(i = 0; i < f.elements.length; i++){
		if(f.elements[i].tagName == "INPUT" && f.elements[i].type == "checkbox"){
			if(f.elements[i].checked == true){
				if(first == true){
					first = false;
				}else{
					list += ",";
				}
				id = f.elements[i].name.substring(f.elements[i].name.lastIndexOf('[')+1, f.elements[i].name.lastIndexOf(']')) ;
				list += id;
				count++;
			}
		}
	}

	if(count == 0){
		alert("Musíte zaškrtnout výrobky, které chcete porovnat.");
	}else{
		window.open('../compare/index.php?id='+list,'srov','scrollbars=yes,resizable=yes,width=800,height=600');
	}
}

// povolí k označení všechny možnosti platby a přepravy
function unsel_all()
{
    var strName = "ship";

    arrFull = fullShipping.split("|");
    for(i = 0; i < arrFull.length; i++)
    {
        document.getElementById(strName + arrFull[i].toString()).disabled = false;
    }

    strName = "pay";
    arrFull = fullPayment.split("|");
    for(i = 0; i < arrFull.length; i++)
    {
        document.getElementById(strName + arrFull[i].toString()).disabled = false;
    }
}


// obstarává aktivaci a deaktivaci voleb platby a dopravy v potvrzování objednávky
function update_payment(shipping)
{
    var strString;
    var strName = "pay";
    var strShipName = "ship";
    var i;
    var te;
    var nosel = true;
    var before_sel = null;

    // odznačení všech ostatních shippings (způsobů dopravy) - aby se chovali jako radio buttons
    arrFull = fullShipping.split("|");
    for(i = 0; i < arrFull.length; i++)
    {
        if(arrFull[i] != shipping)
        {
            idn = strShipName + arrFull[i].toString();
            document.getElementById(idn).checked = false;
        }
        else
        {
            if(document.getElementById(strShipName + arrFull[i].toString()).checked == true)
            {
                nosel = false;
            }
        }
    }

    if(nosel == true)
    {
        unsel_all();
        return;
    }

    strString = arrShipping[shipping];

    // označení všech možností plateb jako zakázaných
    arrFull = fullPayment.split("|");
    for(i = 0; i < arrFull.length; i++)
    {
        element = document.getElementById(strName + arrFull[i].toString());
        element.disabled = true;
        if(element.checked == true)
        {
            before_sel = element;
        }
    }

    // explicitní povolení plateb pro daný způsob dopravy
    arrItem = strString.split("|");
    for(i = 0; i < arrItem.length; i++)
    {
        te = strName + arrItem[i].toString()
        document.getElementById(te).disabled = false;
    }

    if(before_sel != null)
    {
        if(before_sel.disabled == true)
        {
            before_sel.checked = false;
        }
    }
}

function update_shipping(payment)
{
    var strString;
    var strName = "ship";
    var strPayName = "pay";
    var i;
    var te;
    var nosel = true;
    var before_sel = null;
    var defShipping = 0;

    // odznačení všech ostatních payments
    arrFull = fullPayment.split("|");
    for(i = 0; i < arrFull.length; i++)
    {
        if(arrFull[i] != payment)
        {
            document.getElementById(strPayName + arrFull[i].toString()).checked = false;
        }
        else
        {
            if(document.getElementById(strPayName + arrFull[i].toString()).checked == true)
            {
                nosel = false;
            }
        }
    }

    if(nosel == true)
    {
        unsel_all();
        return;
    }

    strString = arrPayment[payment];
    defShipping = arrPreffShipping[payment];

    // označení všech možností plateb jako zakázaných
    arrFull = fullShipping.split("|");
    for(i = 0; i < arrFull.length; i++)
    {
        element = document.getElementById(strName + arrFull[i].toString());
        element.disabled = true;
        if(element.checked == true)
        {
            before_sel = element;
        }
    }

    // explicitní povolení plateb pro daný způsob dopravy
    arrItem = strString.split("|");
    for(i = 0; i < arrItem.length; i++)
    {
        te = strName + arrItem[i].toString()
        document.getElementById(te).disabled = false;
            // objGet(strName + 'Sub' + arrItem[i].toString()).disabled = false;
    }

    if(before_sel != null)
    {
        if(before_sel.disabled == true)
        {
            before_sel.checked = false;
        }
    }
    
    // zakrtnutí implicitního způsobu dopravy (pouze není-li zakrtunta ádná jiná)
    if(before_sel == null)
    {
        element = document.getElementById(strName + defShipping.toString());
        if(element.disabled == false)
        {
            element.checked = true;
        }
    }
}


/*
function change_sum() is DEPRECATED - use recalc() instead  

function change_sum(id, v_id) {
  var formik = document.getElementById("order");
  var sum_element = document.getElementById("number" + id + "_" + v_id);
  formik.product_id.value = id;
  formik.product_var_id.value = v_id;
  formik.product_sum.value = sum_element.value;
  formik.event.value = "change_number";
  formik.__EVENTARGUMENT.value = "recalc";
  formik.submit();
}*/
