//     show.js
var active = 0;
var loadItem;
var loadTarget;
iLoad = function() {
    // spin();
    setTimeout("LoadData(loadTarget, loadItem);",500);
}
goBack = function() {
    if (loadItem  && loadTarget ) iLoad();
    else history.back();
}
rowHover = function(row, over) {
    row.className = (over) ? 'DataHover' : 'DataEven';
}
function setOpacity (obj, val) {
    obj.style.opacity = val;
    div.style.KhtmlOpacity = val;
    div.style.filter = 'alpha(opacity = '+val*100+')';
}
var div;
var val;
var stop = 0;
var i = 0;
var step = 20;
var ID;
var appearOn = Array();
var disappearOn = Array();
function appear(id) {
    if (!id) return false;
    formLay = $(id)
    Effect.Appear(formLay);
    return false;
}
function disappear(id) {
    if (!id) return false;
    formLay = $(id);
    //   Effect.DropOut(formLay);
    Effect.Fade(formLay);
    return false;
}
switchActive = function(event) {}
function Switch(o, dir) {}
function smoothSwitch(o) {}
function spin() {}
function ShowLogin() {}
function showInfo(mid,uid,box) {
    info = $('e');
    info.style.display = 'block';
    frame = $('inframe');
    frame.innerHTML = "Proszę czekać...";
    if (box.checked)
        LoadData('inframe','exec.php?mode=users&mid='+mid+'&oper=set&uid='+uid, true);
    else
        LoadData('inframe','exec.php?mode=users&mid='+mid+'&oper=unset&uid='+uid, true);
    setTimeout("info.style.display = 'none';", 1000);
}
userAlert = function(alertMsg, autoHide) {
    var eLayer = $('systemAlert');
    var _alertMsg = $('alertMsg');
    var _autoHide = autoHide;
    if (window.attachEvent && !window.opera)
        eLayer.top = curPos+'px';
    else    eLayer.style.top = curPos;
    if (alertMsg) {
        _alertMsg.innerHTML = alertMsg;
        eLayer.appear();
    }
    if (_autoHide) setInterval("Effect.Fade('systemAlert');", 1200);
}

saveForm = function(id, allFields) {
    var myForm = $(id);
    var alertMsg = '';
    if (allFields) {
        var inputs = myForm.getInputs('text');
        for (i in inputs) {
            // 			alert(/*inputs[i].id*/);
            if (inputs[i].value == '') {
                try {
                    $(inputs[i].id).style.border = '2px solid red';
                }
                catch (e) {
                    inputs[i].id = i+'_id';
                    $(inputs[i].id).style.border = '2px solid red';
                }//Element.setStyle(inputs[i].id ,'border: 1px solid red');
                // 			alert(inputs[i].name);
                if (!alertMsg) alertMsg = 'Uzupełnij proszę wszystkie pola.'
            }
        }
        if (alertMsg) {
            userAlert(alertMsg);
            alertMsg = '';
            return false;
        }
    }
    myForm.request( {
        onComplete: function(transport) {
            $('productData').innerHTML = transport.responseText;
            Effect.Fade('spinner');
            userAlert('Dane zostały zapisane.', 1);
        }
    });
    return false;
}
saveProductForm = function (id) {
    var myForm = $(id);
    var alertMsg = '';
    var params = myForm.serialize();
    new Ajax.Updater(
        'productData',
        myForm.action,
        {
            asynchronous:true,
            parameters:params,
            onComplete: function(transport) {
                Effect.Fade('spinner');
                userAlert('Dane zostały zapisane.', 1);
            }
        });
    iLoad();
}
saveMyForm = function (id, targetID) {
    var tID = $(targetID);
    if (!tID) targetID = 'main';
    var myForm = $(id);
    myForm.request( {
        onComplete: function(transport) {
            if (targetID) $(targetID).innerHTML = transport.responseText;
        }
    });
}
LoadSort = function (_Link ,Direction) {
    var Dir = (Direction) ? Direction+'=true' : '';
    LoadData('productData',_Link+'&'+Dir);
}
function goEdit(mode, id) {
    location.href='?mode='+mode+'&exec=Edit&item='+id;
}
function loadEdit(mode, id) {
    LoadData('productData','?mode='+mode+'&exec=Edit&item='+id);
}
function goDel(mode, id) {
    if (confirm('Pozyja zostanie usunięta. '))
        //LoadData('inframe','?mode='+mode+'&exec=Delete&item='+id);
        location.href='?mode='+mode+'&exec=Delete&item='+id;
}
function loadDel(mode, id) {
    if (confirm('Pozyja zostanie usunięta. '))
        LoadData('productData','?mode='+mode+'&exec=Delete&item='+id);
}
function goPermDel(mode, id) {
    if (confirm('Pozyja zostanie usunięta.  '))
        //LoadData('inframe','?mode='+mode+'&exec=Delete&item='+id);
        location.href='?mode='+mode+'&exec=PernamentDelete&item='+id;
}
function productDel(mode, id)
{
    if (confirm('Pozyja zostanie usunięta.  ')) {
        // 	spin();
        // 	LoadData('productData','?mode='+mode+'&exec=PernamentDelete&item='+id);
        LoadData('productData','?mode='+mode+'&exec=Delete&item='+id);
    }
//location.href='?mode='+mode+'&exec=PernamentDelete&item='+id;
}
function goOrder(mode, id)
{
    location.href='?mode='+mode+'&exec=Order&item='+id;
}
function goCat(BrandID, TypeID, ParentID)
{
    ParentID = (ParentID) ? 'kategoria'+ParentID : '';
    TypeID = (TypeID) ? TypeID : '';
    BrandID = (BrandID) ? '?BrandID='+BrandID : '';
    location.href = '/'+ParentID+TypeID+'.html'+BrandID;
}
function goProduct(id, BrandID, TypeID)
{
    if (TypeID) TypeID = '&TypeID='+TypeID; else TypeID = '';
    if (BrandID) BrandID = '&BranID='+BrandID; else BrandID = '';
    //LoadData('mainData','?mode=shop&amp;data=Product&amp;item='+id+'&amp;BrandID='+BrandID+'&amp;TypeID='+TypeID);
    location.href = '?mode=shop&data=Product&item='+id+BrandID+TypeID;
}
goBrand = function(BrandID) {
    if (!BrandID) BrandID = 0;
    location.href = '/?data=Product&ProducerID='+BrandID;
}
function goStatus(mode) {
    sel = $('status');
    //	location.href='?mode='+mode+'&StatusID='+sel.selectedIndex;
    LoadData('mainData','?mode='+mode+'&StatusID='+sel.selectedIndex);
}
function Go(_link) {
    if (_link) location.href= _link;
}
LoadData = function (o, _url, mod) {
    var url;
    url = (mod) ? _url : 'exec.php'+_url;
    new Ajax.Updater(o, url, {
        evalScripts:true
    } );
}
getProvider = function () {
    var Provider = '';
    var OrderID = '';
    var priceTo = '';
    var productName = '';
    Provider = '&Provider='+$('Provider').options[$('Provider').selectedIndex].value;
    OrderID = ($('OrderID').value != '') ? '&OrderID='+$('OrderID').value : '' ;
    Total = ($('Total').value != '') ? '&Total='+$('Total').value : '' ;

    loadItem = '?mode=shop&data=Payment'+Provider+OrderID+Total;
    LoadData('productData', '?mode=shop&data=Payment'+Provider+OrderID+Total);
}

function checkemail(str) {
    var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i ;
    if (filter.test(str))
        return true;
    else return false;
}
checkRegForm = function () {
    var stop = false;
    var email = $('email');
    var fname = $('first_name');
    var cname = $('name');
    var address1 = $('address1');
    // var address2 = $('address2');
    // var address3 = $('address3');
    var postal_code = $('postal_code');
    var city = $('Regcity');
    var companyName = $('companyName');
    var phone = $('phone');
    var pass1 = $('Cpassword');
    var pass2 = $('Cpassword2');
    var regionID = $('regionID');
    var passMissed = "Hasła są za krótkie lub różnią się.";
    res = '';
    if (pass1.value != pass2.value || pass1.value.length < 5) {
        res += '* '+passMissed+'<br>';
    // 	alert(pass1.value+" * "+pass2.value);
    }
    if (!checkemail(email.value)) res += "* Adres E-Mail jest nieprawidłowy<br>";
    if (fname.value == '') res += "* Nie podano imienia<br>";
    if (cname.value == '') res += "* Nie podano Nazwiska<br>";
    if (address1.value == '') res += "* Nie podano ulicy<br>";
    if (city.value == '') res += "* Nie podano miasta<br>";
    if (regionID.selectedIndex < 1) res += "* Nie wybrano województwa<br><br>";
    if (phone.value == '') res += "* Nie podano Numeru telefonu<br>";

    if (res) {
        res = "<b>Proszę uzupełnić wszystkie wymagane pola.</b> <br><br><blockquote style='text-align:left;'>"+res+"</blockquote>";
        userAlert(res);
        return false;
    }
    else
        $('registerForm').submit();
    return false;
}

checkNotifyForm = function () {
    var stop = false;
    var email = $('Nemail');
    var Nname = $('Nname');
    var Nmessage = $('Nmessage');
  
    res = '';
    if (!checkemail(email.value)) res += "* Adres E-Mail jest nieprawidłowy<br><br>";
    if (Nname.value == '') res += "* Podaj imię osoby polecającej<br><br>";
    if (Nmessage.value == '') res += "* Proszę wpisać wiadomość<br><br>";
  
    if (res) {
        res = "<b>Proszę uzupełnić wszystkie wymagane pola.</b> <br><br><blockquote style='text-align:left;'>"+res+"</blockquote>";
        userAlert(res);
        return false;
    } else return true;
// 	document.registerForm.submit();
}

checkQuestionForm = function () {
    var stop = false;
    var email = $('Nemail');
    var Nname = $('Nname');
    var Nmessage = $('Nmessage');
  
    res = '';
    if (!checkemail(email.value)) res += "* Proszę podać poprawny adres email<br><br>";
    if (Nname.value == '') res += "* Podaj swoje Imię<br><br>";
    if (Nmessage.value == '') res += "* Proszę wpisać pytanie<br><br>";
  
    if (res) {
        res = "<b>Proszę uzupełnić wszystkie wymagane pola.</b> <br><br><blockquote style='text-align:left;'>"+res+"</blockquote>";
        userAlert(res);
        return false;
    } else return true;
// 	document.registerForm.submit();
}

checkContactForm = function () {
    var stop = false;
    var email = $('Nemail');
    var Nname = $('Nname');
    var Nphone = $('Nphone');
    var Nmessage = $('Nmessage');
  
    res = '';
    if (!checkemail(email.value)) res += "* Adres E-Mail jest nieprawidłowy<br><br>";
    if (Nname.value == '') res += "* Podaj imię i nazwisko<br><br>";
    if (Nmessage.value == '') res += "* Proszę wpisać wiadomość<br><br>";
    if (Nphone.value == '') res += "* Proszę wpisać numer telefonu<br><br>";

    if (res) {
        res = "<b>Proszę uzupełnić wszystkie wymagane pola.</b> <br><br><blockquote style='text-align:left;'>"+res+"</blockquote>";
        userAlert(res);
        return false;
    } else return true;
// 	document.registerForm.submit();
}

checkNewsletter = function () {
    res = '';
    var email = $('newsletter_email');
    if (!checkemail(email.value)) res += "Prosze wpisać poprawny adres E-mail<br><br>";
    if (res) {
        userAlert(res);
        return false;
    }
    else
        return true;
}
  
//   productFunc
var id;
var info;
var basket;
var prod;
var code;
var p_name;
var quantity;
var sess_id;
var price;
var date = '';
var curPos;
var eLayer;
var color ='';
var size ='';
	
function setCur(evt) {
    if (evt.pageY) curPos = evt.pageY-10;
    else
    if (document.documentElement)
        if (document.documentElement.scrollTop)
            curPos = document.documentElement.scrollTop+200;
        else
            curPos = 200;
}
	
	
cancel = function(id) {
    if (!id) return false;
    formLay = $(id);
    //   Effect.DropOut(formLay);
    Effect.Fade(formLay);
    return false;
}	
	
showForm = function(id) {
    if (!id) return false;
    formLay = $(id)
    if (navigator.appName != "Microsoft Internet Explorer")
        formLay.style.top = curPos+'px';
    else
        formLay.style.top = curPos;
    //   Effect.SlideDown(formLay);
    Effect.Appear(formLay);
    return false;
}	
	
setProduct = function ( pid, pcode, pname, _sess_id, _price, _date) {
    if (document.documentElement)
        if (document.documentElement.scrollTop)
            curPos = document.documentElement.scrollTop+350;
        else
            curPos = 350;
    if (!curPos) curPos = 350;
    eLayer = $('productQuant');
    //   eLayer.style.display = 'block';

    if (navigator.appName != "Microsoft Internet Explorer")
        eLayer.style.top = curPos+'px';
    else
        eLayer.style.top = curPos;
    showForm('productQuant');
  
    prod = $('prod');
    quantity = $('quantity');
    selects = $('productSelects'+pid);
    if (selects) selects = $('productSelects'+pid).innerHTML;
    sess_id = _sess_id;
    price = _price;
    id = pid;
    p_name = pname;
    code = pcode;
    prod.innerHTML = "<span style='color:#808080;font-size:14px;'><b>Dodaj do koszyka "+p_name+"</b></span><br><br><br>"+selects;
    try {
        quantity.focus();
    }
    catch (e) {}
}

updatePrice = function (id) {
    if (!id) id ='';
    var selSize = $(id);
    if (selSize) {
        if (selSize.selectedIndex > 0) {
            size = '&arguments[size]='+selSize.options[selSize.selectedIndex].text;
            if (parseFloat(selSize.options[selSize.selectedIndex].value) > 0) $('itemPrice').innerHTML = parseFloat(selSize.options[selSize.selectedIndex].value)+' zł';
        }
    }
}
checkSelects = function (id) {
    if (!id) id ='';
    var selSize = $('selSize'+id);
    var selColor = $('selColor'+id);
    if (selSize) {
        if (selSize.selectedIndex == 0) {
            userAlert('Wybierz rozmiar');
            return false;
        }
        else {
            size = '&arguments[size]='+selSize.options[selSize.selectedIndex].text;
            if (parseFloat(selSize.options[selSize.selectedIndex].value) > 0) price = parseFloat(selSize.options[selSize.selectedIndex].value);
        }
    }
    if (selColor) {
        if (selColor.selectedIndex == 0) {
            userAlert('Wybierz kolor');
            return false;
        }
        else {
            color = '&arguments[color]='+selColor.options[selColor.selectedIndex].text;
        }
    }
    return true;
}
addProduct = function ( pid, pcode, pname, _sess_id, _price, _date) {
  
    result = $('productResult');
    quantity = $('quantity');
    //   productQuantity
  
    if (!checkSelects(pid)) return false;
  
    sess_id = _sess_id;
    price = _price;
    id = pid;
    p_name = pname;
    code = pcode;
    basketAdd();
    userAlert('Produkt dodany do koszyka.', 1);
    //   result.innerHTML = "Produkt dodany do koszyka.";
    return false;
}

basketAdd = function() {
    var q = quantity.value||1;
    if (!checkSelects(id)) return false;
    LoadData('basket','?mode=shop&exec=Add&arguments[sess_id]='+sess_id+'&data=Basket&arguments[product_id]='+id+'&arguments[quantity]='+q+'&arguments[product_name]='+p_name+'&arguments[code]='+code+'&arguments[price]='+price+color+size);
    cancel('productQuant');
    //   $('e').innerHTML = p_name+" został dodany do koszyka.";
    //   showForm('e');
    //   setTimeout("cancel('e');",1000);
    return false;
}

mShow = function (id) {
    try {
        $(id).style.display = 'block';
    } catch(e) {}
}

mHide = function (id) {
    try {
        $(id).style.display = 'none';
    } catch(e) {}
}

loadPhoto = function (image) {
    $('e').style.display = 'block';
    $('e').src = '../product_photo/photo/'+image;
}

getURLData = function () {
    var Loc = location.href.split("?");
    var _link = Loc[0]+'?';
    urlterms = (Loc[1]) ? Loc[1].split("&") : '';
    return Array(_link, urlterms);
}

goNextPage = function (direction, pageNo) {

    Loc = getURLData();

    var _link = Loc[0];
    var urlterms = Loc[1];

    var page = 0;
    var Direction = (direction) ? -1 : 1;
    for (i=0 ; i < urlterms.length;i++) {
        _var = urlterms[i].split('=');
        if (_var[0] == 'Page') {
            page = (pageNo) ? pageNo : parseInt(_var[1])+parseInt(Direction);
            urlterms[i] = '';
            continue;
        }
        else if (urlterms[i] != '') _link += urlterms[i]+'&';
    }
    if (page > 0) _link += 'Page='+page;
    else { 
        if (pageNo) _link += 'Page='+pageNo;
        else _link += 'Page=2';
    }
    Go(_link);
}

countOrder = function() {
    var shippingSelect = $('shippingSelect');
    var shippingPoczta = parseInt($('shippingPoczta').value);
    var shippingKurier = parseInt($('shippingKurier').value);
    var shippingCharge = parseInt($('shippingCharge').value);
    var basketTotal = $('basketTotal').innerHTML;
    basketTotal = parseInt(basketTotal);
    var orderTotal = $('orderTotal');
    var lukasMsg = $('lukas_msg');

    var shippingType = shippingSelect.options[shippingSelect.selectedIndex].value;
    orderTotal.innerHTML = basketTotal + parseInt( $('shipping'+shippingType).value );
    $('shippingCharge').value = parseInt( $('shipping'+shippingType).value );
    
    var total = parseInt(basketTotal + parseInt( $('shipping'+shippingType).value ));
    
    if (total < 500)
    {
      lukasMsg.innerHTML = 'Zakupy w systemie ratalnym LUKAS Raty dostępne są oo 500 zł wartości całego zamówienia.';
      $('show_lukas').setStyle({
        visibility: 'hidden'
      });
      $('simulator').setStyle({
        visibility: 'hidden'
      });
    }
    else if (total > 10000)
    {
      lukasMsg.innerHTML = 'Zakupy w systemie ratalnym LUKAS Raty dostępne są do 10000 zł wartości całego zamówienia.';
      $('show_lukas').setStyle({
        visibility: 'hidden'
      });
      $('simulator').setStyle({
        visibility: 'hidden'
      });
    }
    else if (($('shippingCharge').value != '35') && (total >= 500) && (total <= 10000))
    {
      $('show_lukas').setStyle({
        visibility: 'visible'
      });
      document.getElementById('simulator').onclick = function() 
                                                     {
                                                        window.open('http://www.fotookazja.pl/lukas/ewniosek.php?action=symulator&p='+total, 'LUKAS Raty', 'width=840,height=600,resizable=yes,scrollbars=yes,status=yes');
                                                        return false;
                                                     };

      document.getElementById('simulator').href = 'http://www.fotookazja.pl/lukas/ewniosek.php?action=symulator&p='+total;
      if (document.getElementById('lukas').checked)
      {
          $('simulator').setStyle({
            visibility: 'visible'
          });
      }
    }
    else
    {
      $('show_lukas').setStyle({
        visibility: 'hidden'
      });
      $('simulator').setStyle({
        visibility: 'hidden'
      });
    }   
}

showSimulator = function() {
  if (document.getElementById('lukas').checked)
  {
      $('simulator').setStyle({
        visibility: 'visible'
      });
  }
  else
  {
      $('simulator').setStyle({
        visibility: 'hidden'
      });  
  }
}

checkOrderData = function () {
    var stop = false;
    var fname = $('first_name');
    var cname = $('name');
    var address1 = $('address1');
    var postal_code = $('postal_code');
    var city = $('Regcity');
    var companyName = $('companyName');
    var phone = $('phone');
    res = '';
    if (fname.value == '') res += "* Nie podano imienia<br><br>";
    if (cname.value == '') res += "* Nie podano Nazwiska<br><br>";
    if (address1.value == '') res += "* Nie podano ulicy<br><br>";
    if (city.value == '') res += "* Nie podano miasta<br><br>";
    if (phone.value == '') res += "* Nie podano numeru Telefonu<br><br>";

    if (res) {
        res = "<b>Proszę uzupełnić wszystkie wymagane pola.</b> <br><br><blockquote style='text-align:left;'>"+res+"</blockquote>";
        userAlert(res);
        return false;
    }
    else
        saveMyForm('order', 'main');
    return false;
}
checkLoginData = function () {
    var stop = false;
    var lname = $('login_name');
    var lpass = $('login_password');
    res = '';
    if (lname.value == '') res += "* Nie podano nazwy Użytkownika<br><br>";
    if (lpass.value == '') res += "* Nie podano Hasła<br><br>";

    if (res) {
        res = "<b>Proszę uzupełnić wszystkie wymagane pola.</b> <br><br><blockquote style='text-align:left;'>"+res+"</blockquote>";
        userAlert(res);
        return false;
    }
    else
        saveMyForm('order', 'main');
    return false;
}

checkPNotifyForm = function () {
    var stop = false;
    var lname = $('friend_name');
    var lmail = $('friend_email');
    res = '';
    if (lname.value == '') res += "* Nie podano Imienia i nazwiska<br><br>";
    if (lmail.value == '')
        if (!checkemail(lmail.value)) res += "* Nieprawidłowy adres email<br><br>";

    if (res) {
        res = "<b>Proszę uzupełnić wszystkie wymagane pola.</b> <br><br><blockquote style='text-align:left;'>"+res+"</blockquote>";
        userAlert(res);
        return false;
    }
    else
        return true;
//         saveMyForm('order', 'text');

}

