var smallimg_border = '';
var oldcolor = [];

function changeMainImg(small_url, big_url, smallid, productId, bigImgId) {
    document.getElementById('bigimg').style.background = "url('" + small_url + "') no-repeat center center";

    document.getElementById('bigimg').innerHTML = '<a href="'+big_url+'" rel="lightbox[big]"><img src="/images/transparent.gif" width="300" height="300"/></a>';

    smallimg_border = document.getElementById(smallid).style.border;
    document.getElementById(smallid).style.border = "1px solid #333333";

    var oXmlHttp = createXHR();

    oXmlHttp.open("get", "/ajax/GetImages.php?product=" + productId + "&no=" + bigImgId, true);
    oXmlHttp.onreadystatechange = function () {
        if (oXmlHttp.readyState == 4) {
            if (oXmlHttp.status == 200) {
                divTxt = oXmlHttp.responseText;

                document.getElementById("lightboxhidden").innerHTML = divTxt;
            }
            else {
                alert("Nastala chyba při komunikaci se serverem.");
            }
        }
    };

    oXmlHttp.send(null);
}

function borderBack(smallid) {
    document.getElementById(smallid).style.border = smallimg_border;
}


function createXHR() {
  if(typeof XMLHttpRequest != "undefined") {
    return new XMLHttpRequest();
  }
  else if(window.ActiveXObject) {
    var aVersions = ["MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.3.0"];
    for(var i = 0; i < aVersions.length; i++) {
      try {
        var oXHR = new ActiveXObject(aVersions[i]);
        return oXHR;
      } catch (oError) {
        //Do nothing
      }
    }
  }
  throw new Error("XMLHttp object could not be created.");
}


function CreateCarModelList() {
    document.getElementById("modeldiv").innerHTML = '<img src="/images/wait16trans.gif" alt="wait..."/>';

    rt = document.newproductf.elements['carbrand'];
    brandId = rt.options[rt.selectedIndex].value;

    var oXmlHttp = createXHR();

    oXmlHttp.open("get", "/ajax/GetCars.php?brand=" + brandId, true);
    oXmlHttp.onreadystatechange = function () {
        if (oXmlHttp.readyState == 4) {
            if (oXmlHttp.status == 200) {
                divTxt = oXmlHttp.responseText;

                document.getElementById("modeldiv").innerHTML = divTxt;
            }
            else {
                alert("Nastala chyba při komunikaci se serverem.");
            }
        }
    };

    oXmlHttp.send(null);
}

function CreateUserCarModelList(sel) {
    document.getElementById("modeldiv").innerHTML = '<img src="/images/wait16trans.gif" alt="wait..."/>';

    brandId = sel.options[sel.selectedIndex].value;

    var oXmlHttp = createXHR();

    oXmlHttp.open("get", "/ajax/GetCars.php?brand=" + brandId + "&allmodels=1", true);
    oXmlHttp.onreadystatechange = function () {
        if (oXmlHttp.readyState == 4) {
            if (oXmlHttp.status == 200) {
                divTxt = oXmlHttp.responseText;

                document.getElementById("modeldiv").innerHTML = divTxt;
            }
            else {
                alert("Nastala chyba pri komunikacii so serverom.");
            }
        }
    };

    oXmlHttp.send(null);
}

function CreateFilterCarModelList(category) {
    document.getElementById("modeldiv").innerHTML = '<img src="/images/wait16trans.gif" alt="wait..."/>';
    document.getElementById("yeardiv").innerHTML = '<select disabled="disabled"></select>';
    document.getElementById("enginediv").innerHTML = '<select disabled="disabled"></select>';
    
    rt = document.products.elements['carbrand'];
    brandId = rt.options[rt.selectedIndex].value;

    var oXmlHttp = createXHR();

    oXmlHttp.open("get", "/ajax/GetCars.php?brand=" + brandId + "&cat=" + category, true);
    oXmlHttp.onreadystatechange = function () {
        if (oXmlHttp.readyState == 4) {
            if (oXmlHttp.status == 200) {
                divTxt = oXmlHttp.responseText;

                document.getElementById("modeldiv").innerHTML = divTxt;
            }
            else {
                alert("Nastala chyba při komunikaci se serverem.");
            }
        }
    };

    oXmlHttp.send(null);
}

function CreateFilterYearList(category) {
    document.getElementById("yeardiv").innerHTML = '<img src="/images/wait16trans.gif" alt="wait..."/>';
    document.getElementById("enginediv").innerHTML = '<select disabled="disabled"></select>';

    rt = document.products.elements['carbrand'];
    brandId = rt.options[rt.selectedIndex].value;

    rt2 = document.products.elements['carmodel'];
    modelId = rt2.options[rt2.selectedIndex].value;

    var oXmlHttp = createXHR();

    oXmlHttp.open("get", "/ajax/GetYears.php?brand=" + brandId + "&model=" + modelId + "&cat=" + category, true);
    oXmlHttp.onreadystatechange = function () {
        if (oXmlHttp.readyState == 4) {
            if (oXmlHttp.status == 200) {
                divTxt = oXmlHttp.responseText;

                document.getElementById("yeardiv").innerHTML = divTxt;
            }
            else {
                alert("Nastala chyba při komunikaci se serverem.");
            }
        }
    };

    oXmlHttp.send(null);
}

function CreateFilterEngineList(category) {
    document.getElementById("enginediv").innerHTML = '<img src="/images/wait16trans.gif" alt="wait..."/>';

    rt = document.products.elements['carbrand'];
    brandId = rt.options[rt.selectedIndex].value;

    rt2 = document.products.elements['carmodel'];
    modelId = rt2.options[rt2.selectedIndex].value;

    rt3 = document.products.elements['manufyear'];
    year = rt3.options[rt3.selectedIndex].value;

    var oXmlHttp = createXHR();

    oXmlHttp.open("get", "/ajax/GetEngines.php?brand=" + brandId + "&model=" + modelId + "&year=" + year + "&cat=" + category, true);
    oXmlHttp.onreadystatechange = function () {
        if (oXmlHttp.readyState == 4) {
            if (oXmlHttp.status == 200) {
                divTxt = oXmlHttp.responseText;

                document.getElementById("enginediv").innerHTML = divTxt;
            }
            else {
                alert("Nastala chyba při komunikaci se serverem.");
            }
        }
    };

    oXmlHttp.send(null);
}

function countryChanged(sel) {
    country = sel.options[sel.selectedIndex].value;

    delivery_span = document.getElementById("deliveryfee");

    payment_cash_span = document.getElementById("paymentfee1");
    payment_bt_span = document.getElementById("paymentfee2");
    payment_mo_span = document.getElementById("paymentfee3");

    var oXmlHttp = createXHR();

    oXmlHttp.open("get", "/ajax/GetSettings.php?country=" + country, true);
    oXmlHttp.onreadystatechange = function () {
        if (oXmlHttp.readyState == 4) {
            if (oXmlHttp.status == 200) {
                arrInfo = oXmlHttp.responseText.split("||");

                delivery_span.innerHTML = arrInfo[0];
                payment_cash_span.innerHTML = arrInfo[1];
                payment_bt_span.innerHTML = arrInfo[2];
                payment_mo_span.innerHTML = arrInfo[3];
            }
            else {
                alert("Nastala chyba při komunikaci se serverem.");
            }
        }
    };

    oXmlHttp.send(null);
}

function ChangeColor(tableRow, highLight)  {
    var i;
    
    if(highLight) {        
        for (i in tableRow.cells) {
            oldcolor[i] = tableRow.cells[i].style.backgroundColor;
            tableRow.cells[i].style.backgroundColor = '#e0e0e0';
        }
    }
    else {
        for (i in tableRow.cells) {
            tableRow.cells[i].style.backgroundColor = oldcolor[i];

        }
    }
}

function DoNav(theUrl)
{
    document.location.href = theUrl;
}

function deliverySelected(num) {
    if(num==1) {
        document.buy.payment[0].disabled = false;
        document.buy.payment[2].disabled = true;

        if(document.buy.payment[2].checked == true)
            document.buy.payment[2].checked = false;
    }
    if(num==2) {
        document.buy.payment[0].disabled = true;
        document.buy.payment[2].disabled = false;

        if(document.buy.payment[0].checked == true)
            document.buy.payment[0].checked = false;
    }
}

function userOrderCancel() {
    cancelBt = document.getElementById('cancel');
    cancelBt.disabled = true;

    cancelDiv = document.getElementById('ordercancel');
    cancelDiv.style.display = 'block';
}

function userOrderCancelBack() {
    cancelBt = document.getElementById('cancel');
    cancelBt.disabled = false;

    cancelDiv = document.getElementById('ordercancel');
    cancelDiv.style.display = 'none';
}
