function setElementInnerHTML(elementName, elementValue) {
    if (document.getElementById(elementName) != null) {
        document.getElementById(elementName).innerHTML = elementValue;
    }
}

function getElementInnerHTML(elementName) {
    if (document.getElementById(elementName) != null) {
        return document.getElementById(elementName).innerHTML;
    }
    return "";
}
function setElementValue(elementName, elementValue) {
    if (document.getElementById(elementName) != null) {
        document.getElementById(elementName).value = elementValue;
    }
}

function getElementValue(elementName) {
    if (document.getElementById(elementName) != null) {
        return document.getElementById(elementName).value;
    }
    return "";
}
function setElementChecked(elementName, elementValue) {
    if (document.getElementById(elementName) != null) {
        document.getElementById(elementName).checked = elementValue;
    }
}

function getElementChecked(elementName) {
    if (document.getElementById(elementName) != null) {
        return document.getElementById(elementName).checked;
    }
    return false;
}

function loadAjaxToDiv(ajaxFile, divElementName) {
  var xmlHttpAjax;
  if (window.ActiveXObject) {
    xmlHttpAjax = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else if (window.XMLHttpRequest) {
    xmlHttpAjax = new XMLHttpRequest();
  }
  xmlHttpAjax.onreadystatechange = function() {
      if(xmlHttpAjax.readyState == 4) {
      if(xmlHttpAjax.status == 200) {
          setElementInnerHTML(divElementName, xmlHttpAjax.responseText.replace(/^\s+|\s+$/g,""));
        }
      }
  }
  xmlHttpAjax.open("GET", ajaxFile, true);
  xmlHttpAjax.send(null);
}

function submitRegisterForm(formTitle, closeButtonTitle) {
  var ajaxFile = "ajax/shop/checkRegistrationForm.php?password1=" + getElementValue('login_pswd1') + "&password2=" + getElementValue('login_pswd2') +
      "&email=" + getElementValue('user_email') + "&name=" + getElementValue('user_name') + "&phone=" + getElementValue('user_phone');
  var divElementName = "ajaxform";
  var xmlHttpAjax;
  if (window.ActiveXObject) {
    xmlHttpAjax = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else if (window.XMLHttpRequest) {
    xmlHttpAjax = new XMLHttpRequest();
  }
  xmlHttpAjax.onreadystatechange = function() {
      if(xmlHttpAjax.readyState == 4) {
      if(xmlHttpAjax.status == 200) {
            setElementInnerHTML(divElementName, xmlHttpAjax.responseText.replace(/^\s+|\s+$/g,""));
            if (getElementInnerHTML(divElementName) != '') {
                $( "#dialog:ui-dialog" ).dialog( "destroy" );
                var buttons = {};
                buttons[ closeButtonTitle ] = function() {
                        $( this ).dialog( "close" );
                };

                $( "#ajaxform" ).dialog({
                        title: formTitle,
                        resizable: false,
                        height:200,
                        width: 400,
                        modal: true,
                        buttons: buttons


                });
            } else {
                document.registerForm.submit();
            }
        }
      }
  }
  xmlHttpAjax.open("GET", ajaxFile, true);
  xmlHttpAjax.send(null);    
}

function submitLogin(formTitle, closeButtonTitle) {
  var ajaxFile = "ajax/shop/checkLogin.php?login_name=" + getElementValue('login_name') + "&login_pswd=" + getElementValue('login_pswd');
  var divElementName = "ajaxform";
  var xmlHttpAjax;
  if (window.ActiveXObject) {
    xmlHttpAjax = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else if (window.XMLHttpRequest) {
    xmlHttpAjax = new XMLHttpRequest();
  }
  xmlHttpAjax.onreadystatechange = function() {
      if(xmlHttpAjax.readyState == 4) {
      if(xmlHttpAjax.status == 200) {
            setElementInnerHTML(divElementName, xmlHttpAjax.responseText.replace(/^\s+|\s+$/g,""));
            if (getElementInnerHTML(divElementName) != '') {
                $( "#dialog:ui-dialog" ).dialog( "destroy" );
                var buttons = {};
                buttons[ closeButtonTitle ] = function() {
                        $( this ).dialog( "close" );
                };

                $( "#ajaxform" ).dialog({
                        title: formTitle,
                        resizable: false,
                        height:200,
                        width: 400,
                        modal: true,
                        buttons: buttons


                });
            } else {
                document.loginForm.submit();
            }
        }
      }
  }
  xmlHttpAjax.open("GET", ajaxFile, true);
  xmlHttpAjax.send(null);
}

function recalculateBasket(uid, currency) {
var ajaxFile = "ajax/shop/updateBasket.php?uid=" + uid + "&amount=" + getElementValue('amount' + uid) + "&price=" + getElementValue('price' + uid) + "&currency=" + currency;
  var divElementName = "total" + uid;
  var xmlHttpAjax;
  if (window.ActiveXObject) {
    xmlHttpAjax = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else if (window.XMLHttpRequest) {
    xmlHttpAjax = new XMLHttpRequest();
  }
  xmlHttpAjax.onreadystatechange = function() {
      if(xmlHttpAjax.readyState == 4) {
      if(xmlHttpAjax.status == 200) {
            setElementInnerHTML(divElementName, xmlHttpAjax.responseText.replace(/^\s+|\s+$/g,""));
            getBasketSummary();
            getBasketSummaryVat();
            getPriceInclVat("totalvat" + uid, uid);
        }
      }
  }
  xmlHttpAjax.open("GET", ajaxFile, true);
  xmlHttpAjax.send(null);
}

function getBasketSummary() {
var ajaxFile = "ajax/shop/basketSummary.php";
  var divElementName = "summary";
  var xmlHttpAjax;
  if (window.ActiveXObject) {
    xmlHttpAjax = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else if (window.XMLHttpRequest) {
    xmlHttpAjax = new XMLHttpRequest();
  }
  xmlHttpAjax.onreadystatechange = function() {
      if(xmlHttpAjax.readyState == 4) {
      if(xmlHttpAjax.status == 200) {
            setElementInnerHTML(divElementName, xmlHttpAjax.responseText.replace(/^\s+|\s+$/g,""));
        }
      }
  }
  xmlHttpAjax.open("GET", ajaxFile, true);
  xmlHttpAjax.send(null);
}

function getBasketSummaryVat() {
var ajaxFile = "ajax/shop/basketSummaryVat.php";
  var divElementName = "summary_vat";
  var xmlHttpAjax;
  if (window.ActiveXObject) {
    xmlHttpAjax = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else if (window.XMLHttpRequest) {
    xmlHttpAjax = new XMLHttpRequest();
  }
  xmlHttpAjax.onreadystatechange = function() {
      if(xmlHttpAjax.readyState == 4) {
      if(xmlHttpAjax.status == 200) {
            setElementInnerHTML(divElementName, xmlHttpAjax.responseText.replace(/^\s+|\s+$/g,""));
        }
      }
  }
  xmlHttpAjax.open("GET", ajaxFile, true);
  xmlHttpAjax.send(null);
}

function getPriceInclVat(divElementName, uid) {
var ajaxFile = "ajax/shop/getPriceInclVat.php?uid=" + uid + "&price=" + getElementValue("price" + uid) + "&amount=" + getElementValue("amount" + uid);
  var xmlHttpAjax;
  if (window.ActiveXObject) {
    xmlHttpAjax = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else if (window.XMLHttpRequest) {
    xmlHttpAjax = new XMLHttpRequest();
  }
  xmlHttpAjax.onreadystatechange = function() {
      if(xmlHttpAjax.readyState == 4) {
      if(xmlHttpAjax.status == 200) {
            setElementInnerHTML(divElementName, xmlHttpAjax.responseText.replace(/^\s+|\s+$/g,""));
        }
      }
  }
  xmlHttpAjax.open("GET", ajaxFile, true);
  xmlHttpAjax.send(null);
}

function setOneTransport(idTransport, allTransports) {
    var transports_array=allTransports.split(";");

    for (cykl = 0; cykl < transports_array.length; cykl++) {
        setElementChecked("transport" + transports_array[cykl], false);
    }
    setElementChecked("transport" + idTransport, true);

    loadAjaxToDiv("ajax/shop/selectedTransport.php?transport_id=" + idTransport, "selected_transport");
}
function setOnePayment(idPayment, allPayments) {
    var payments_array=allPayments.split(";");

    for (cykl = 0; cykl < payments_array.length; cykl++) {
        setElementChecked("payment" + payments_array[cykl], false);
    }
    setElementChecked("payment" + idPayment, true);

    loadAjaxToDiv("ajax/shop/selectedpayment.php?payment_id=" + idPayment, "selected_payment");
}

function saveSupplyAddress() {
    var street2 = getElementValue("company_street2").replace("\n", "<br/>");
    var ajaxFile = "ajax/shop/saveSupplyAddress.php?company_street2=" + street2 + "&company_city2=" + getElementValue("company_city2") +
        "&company_zip2=" + getElementValue("company_zip2") + "&company_lcountry_id2=" + getElementValue("company_country2");
  var xmlHttpAjax;
  if (window.ActiveXObject) {
    xmlHttpAjax = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else if (window.XMLHttpRequest) {
    xmlHttpAjax = new XMLHttpRequest();
  }
  xmlHttpAjax.onreadystatechange = function() {
      if(xmlHttpAjax.readyState == 4) {
      if(xmlHttpAjax.status == 200) {
        }
      }
  }
  xmlHttpAjax.open("GET", ajaxFile, true);
  xmlHttpAjax.send(null);
}

function moreInfoForm(formTitle, closeButtonTitle, articleTitle, sendButtonTitle) {
  var ajaxFile = "/ajax/form/more_info.php?articleTitle=" + articleTitle;
  var divElementName = "ajaxform";
  var xmlHttpAjax;
  if (window.ActiveXObject) {
    xmlHttpAjax = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else if (window.XMLHttpRequest) {
    xmlHttpAjax = new XMLHttpRequest();
  }
  xmlHttpAjax.onreadystatechange = function() {
      if(xmlHttpAjax.readyState == 4) {
      if(xmlHttpAjax.status == 200) {
            setElementInnerHTML(divElementName, xmlHttpAjax.responseText.replace(/^\s+|\s+$/g,""));
            if (getElementInnerHTML(divElementName) != '') {
                $( "#dialog:ui-dialog" ).dialog( "destroy" );
                var buttons = {};
                buttons[ sendButtonTitle ] = function() {
                        $( this ).dialog( "close" );
                        moreInfoFormSend(getElementValue("more_info_msg"), getElementChecked("more_info_present"), 
                            getElementValue("more_info_email"), 
                            getElementValue("more_info_phone"), 
                            getElementValue("more_info_contact"), articleTitle, formTitle, closeButtonTitle);
                };
                buttons[ closeButtonTitle ] = function() {
                        $( this ).dialog( "close" );
                };

                $( "#ajaxform" ).dialog({
                        title: formTitle,
                        resizable: false,
                        height:340,
                        width: 450,
                        modal: true,
                        buttons: buttons


                });
            } else {
                document.registerForm.submit();
            }
        }
      }
  }
  xmlHttpAjax.open("GET", ajaxFile, true);
  xmlHttpAjax.send(null);    
}

function moreInfoFormSend(msg, present, email, phone, contact, articleTitle, formTitle, closeButtonTitle) {
  msg = msg.replace("\n", "<br/>");
  var ajaxFile = "/ajax/form/more_info_send.php?articleTitle=" + articleTitle + "&msg=" + msg + "&present=" + present + "&email=" + email + "&phone=" + phone + "&contact=" + contact;
  var divElementName = "ajaxform";
  var xmlHttpAjax;
  if (window.ActiveXObject) {
    xmlHttpAjax = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else if (window.XMLHttpRequest) {
    xmlHttpAjax = new XMLHttpRequest();
  }
  xmlHttpAjax.onreadystatechange = function() {
      if(xmlHttpAjax.readyState == 4) {
      if(xmlHttpAjax.status == 200) {
            setElementInnerHTML(divElementName, xmlHttpAjax.responseText.replace(/^\s+|\s+$/g,""));
            if (getElementInnerHTML(divElementName) != '') {
                $( "#dialog:ui-dialog" ).dialog( "destroy" );
                var buttons = {};
                buttons[ closeButtonTitle ] = function() {
                        $( this ).dialog( "close" );
                };

                $( "#ajaxform" ).dialog({
                        title: formTitle,
                        resizable: false,
                        height:170,
                        width: 450,
                        modal: true,
                        buttons: buttons


                });
            } else {
                document.registerForm.submit();
            }
        }
      }
  }
  xmlHttpAjax.open("GET", ajaxFile, true);
  xmlHttpAjax.send(null);  
}

function productWasSavedToBasket() {
    $( "#dialog:ui-dialog" ).dialog( "destroy" );
    var buttons = {};
    var closeButtonTitle = 'Zavři';
    var shopButtonTitle = 'Přejdi do nákupního košíka';
    var formTitle = 'Nákupní košík';
    buttons[ closeButtonTitle ] = function() {
            $( this ).dialog( "close" );
    };
    buttons[ shopButtonTitle ] = function() {
            $( this ).dialog( "close" );
    };

    $( "#ajaxform" ).dialog({
            title: formTitle,
            resizable: false,
            height:170,
            width: 450,
            modal: true,
            buttons: buttons
    });
}


$(function() {
        $('#gallery a').lightBox();
    });
    
function addToBasketDialog(product_id, product_type_id, product_accessory_id, catalogue_id) {
    
  var amount = getElementValue("basket_amount" + product_id + "_" + product_type_id);
    var xmlHttpAjax;
  if (window.ActiveXObject) {
    xmlHttpAjax = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else if (window.XMLHttpRequest) {
    xmlHttpAjax = new XMLHttpRequest();
  }
  xmlHttpAjax.onreadystatechange = function() {
      if(xmlHttpAjax.readyState == 4) {
      if(xmlHttpAjax.status == 200) {
            setElementInnerHTML("ajaxform", xmlHttpAjax.responseText.replace(/^\s+|\s+$/g,""));
            if (getElementInnerHTML("ajaxform") != '') {
                $( "#dialog:ui-dialog" ).dialog( "destroy" );
                var buttons = {};
                buttons[ "Pokračovat v nákupu" ] = function() {
                        $( this ).dialog( "close" );
                };
                buttons[ "Přejít do košíku" ] = function() {
                        window.location = '/shop_basket.htm';
                };

                $( "#ajaxform" ).dialog({
                        title: "Nákupní košík",
                        resizable: false,
                        height:170,
                        width: 450,
                        modal: true,
                        buttons: buttons


                });
            } else {
                document.registerForm.submit();
            }
        }
      }
  }
  xmlHttpAjax.open("GET", "/ajax/shop/addToBasketForm.php?product_id=" + product_id + "&product_type_id=" + product_type_id + "&product_accessory_id=" + product_accessory_id + "&catalogue_id=" + catalogue_id + "&basket_amount=" + amount, true);
  xmlHttpAjax.send(null);  
}    

function showProductDescription() {
        document.getElementById('product1style').style.display = 'block';
        document.getElementById('product2style').style.display = 'none';
        document.getElementById('product3style').style.display = 'none';
        if (document.getElementById('product1menu') != null) { document.getElementById('product1menu').style.background='url(/images/gastrorexcz/product_menu_active.jpg) repeat-x'; }
        if (document.getElementById('product2menu') != null) { document.getElementById('product2menu').style.background = ''; }
        if (document.getElementById('product3menu') != null) { document.getElementById('product3menu').style.background = ''; }
    }
    function showProductPrice() {
        document.getElementById('product1style').style.display = 'none';
        document.getElementById('product2style').style.display = 'block';
        document.getElementById('product3style').style.display = 'none';
        if (document.getElementById('product1menu') != null) { document.getElementById('product1menu').style.background = ''; }
        if (document.getElementById('product2menu') != null) { document.getElementById('product2menu').style.background='url(/images/gastrorexcz/product_menu_active.jpg) repeat-x'; }
        if (document.getElementById('product3menu') != null) { document.getElementById('product3menu').style.background = ''; }
    }
    function showProductPhotos() {
        document.getElementById('product1style').style.display = 'none';
        document.getElementById('product2style').style.display = 'none';
        document.getElementById('product3style').style.display = 'block';
        if (document.getElementById('product1menu') != null) { document.getElementById('product1menu').style.background = ''; }
        if (document.getElementById('product2menu') != null) { document.getElementById('product2menu').style.background = ''; }
        if (document.getElementById('product3menu') != null) { document.getElementById('product3menu').style.background='url(/images/gastrorexcz/product_menu_active.jpg) repeat-x'; }
    }
