var lm = {
    timer : 250,

    setLayout : function(layout) {
        var p = layout.split(':');
        lm.next_layout = p.shift();
        lm.layout_params = p;
        return lm.next_layout;
    },

    insert : function(targets, funkcja) {
        var ok = true;
        for(var target in targets) {
            var desthtml = $el(target);
            if (desthtml) {
                /* nie powinna usuwać wcześniej załadowanych stylów */
//                alert('Wdupczam do ' + target + ': ' + targets[target] );
                desthtml.innerHTML = targets[target];
            } else {
                ok = false;
                alert(
                    "Rozpacz!!! Problem z umieszczeniem w elemencie:\n\r" +
                        target);
            }
        }
        
        if ($el('akordeon'))
            $('#akordeon').accordion({active: false, clearStyle: true, collapsible: true});

        if (funkcja) funkcja();

        if (lm.layout_params) {
            var tabsId = lm.layout_params[0];
            var tabToSelect = lm.layout_params[1];
            $(tabsId).tabs('select', parseInt(tabToSelect));
            lm.layout_params = null;
        }
        return ok;

    },

    current_layout : null,
    next_layout : null,
    layout_params : null,
	
    init: function(targets, funkcja){
        
        $('#content').empty();
        lm.insert(targets, funkcja);

        if(lm.current_layout == 'init') {
            return true;
        }

        lm.current_layout = 'init';
        $('#content').animate({
            left: "210px",
            width: "554px"
        }, lm.timer );

        return true;
    },


    waski: function(targets, funkcja){

        lm.insert(targets, funkcja);
        $('#akordeon').accordion({active: false, clearStyle: true, collapsible: true});

        if(lm.current_layout == 'waski') {
        return true;
        }

       
//        $('#leftMenu').load(subdir + '/view/gigabyte/inserts/part_leftPannel.php',{},function(){
            lm.current_layout = 'waski';
            $('#content').animate({
                left: "210px",
                width: "554px"
            }, lm.timer, function(){lm.insert(targets, funkcja);});

//        });
        return true;
    },


    szeroki: function(targets, funkcja){

        if(lm.current_layout == 'szeroki') {
            return lm.insert(targets, funkcja);
        }

        $('#content').empty();
        $('#content').animate({
            left: "35px",
            width: "730px"
//            paddingLeft: "10px"
        }, lm.timer, function(){
            lm.current_layout = 'szeroki';
//            $('#leftPannel').load(subdir + '/view/gigabyte/inserts/part_lewy_slizyk.php');
            lm.insert(targets, funkcja);} );
        return true;
    },

    handel_Testowy: function(targets, funkcja){

        if(lm.current_layout == 'handel_OK') {
            return lm.insert(targets, funkcja);
        }

        $('#content').empty();
        $('#content').animate({
            left: "35px",
            width: "730px"
//            paddingLeft: "10px"
        }, lm.timer, function(){
            lm.current_layout = 'handel_OK';
//            $('#leftPannel').load(subdir + '/view/gigabyte/inserts/part_lewy_slizyk.php');
            lm.insert(targets, funkcja);} );

        return true;
    },

    handel: function(targets, funkcja){

        if(lm.current_layout == 'handel') {
            return lm.insert(targets, funkcja);
        }

        $('#content').empty();
        $('#content').load(subdir + '/view/gigabyte/inserts/part_materialyHandloweTabs.php',{},function(){
            lm.current_layout = 'handel';
            $('#content').animate({
                left: "35px",
                width: "730px"
//                paddingLeft: "10px"
            }, lm.timer, function(){
//                $('#leftPannel').load(subdir + '/view/gigabyte/inserts/part_lewy_slizyk.php');
                lm.insert(targets, funkcja);} );
            
        });
        return true;
    },

    marketing: function(targets, funkcja){

        if(lm.current_layout == 'marketing') {
            return lm.insert(targets, funkcja);
        }
        
        $('#content').empty();
        $('#content').load(subdir + '/view/gigabyte/inserts/part_materialyMarketingoweTabs.php',{},function(){
            lm.current_layout = 'marketing';
            $('#content').animate({
                left: "35px",
                width: "730px"
//                paddingLeft: "10px"
            }, lm.timer, function(){
//                $('#leftPannel').load(subdir + '/view/gigabyte/inserts/part_lewy_slizyk.php');
                lm.insert(targets, funkcja);} );
            
        });
        return true;
    }

}

ajaxAction.setLayoutManager(lm);

/* MENU LEWE */

function czytajExtraLiga(){
    ajaxAction.menuAction('extraliga_home');
}

function czytajZgloszenie(){
    ajaxAction.menuAction('userregister_home');
}


function czytajHandlowe(){
    ajaxAction.menuAction('handel_home');
}

function czytajMarketingowe(){
    ajaxAction.menuAction('marketing_home');
}

function czytajIndex(){
      ajaxAction.menuAction('news_home');
}


/* MENU GŁOWNE */

function czytajFaq(){
    ajaxAction.menuAction('faq_home');
}

function czytajRMA(){
    ajaxAction.menuAction('rma_home');
}


function czytajGdzieKupic(){
    ajaxAction.menuAction('sklepy');
}

function czytajKontakt(){
    ajaxAction.menuAction('kontakt_home');
}

function zamowienie(form){
    
    var szts = form.elements['szt[]'];
    var nazwas = form.elements['nazwa[]'];
    var typs = form.elements['typ[]'];

    var content = 'Wybrano:\n';

    var count = szts.length;
    if (count == undefined) count = 1;

    var isOne = false;
    var poz = 1;
    var szt;
    var pozycja;

    var element;
    var typ;
    var nazwa;

    for (var n = 0; n < count; n++) {
        if (count == 1) {
            element = szts;
            typ = typs;
            nazwa = nazwas;
        } else {
            element = szts.item(n);
            typ = typs.item(n);
            nazwa = nazwas.item(n);
        }
        
        szt = element.value;
        if (szt > 0) {
            isOne = true;
            pozycja = '' + poz + '. ';
            pozycja += nazwa.value;
            pozycja += ' (' + typ.value + '): ';
            pozycja += szt + ' szt. \n';
            content += pozycja;
            poz ++;
        }
    }

    if (!isOne) {
        alert('Aby zamówić wprowadź żądaną liczbę sztuk');
        return false;
    }
    else {
        content += '\nWybierz OK aby złożyć zamówienie!'
        return window.confirm(content);
    }
    
}

function rejestracja(form){

    var ok = form.elements.reg_accept.checked;
    if (!ok) {
        alert('Musisz zaakceptować regulamin rejestracji, aby się zarejestrować');
        return false;
    }

    var komunikat = '';
    var eNumber = 0;
    var eElement = null;

    if (!form.elements.nip) {
        alert('Musisz wybrać firmę.');
        return false;
    }


    var element;

    /* test firmowy */

    element = form.elements.nip;
    if (element && !formService.check_nip(form.nip.value)) {
        eNumber++;
        komunikat += eNumber + '. Wprowadź nip firmowy.\n';
        background  = element.style.background;
        element.style.background = "#FFDDDD";
        if (!eElement) eElement = element;
        ok = false;
    }


    element = form.firma;
    if (element && formService.isEmpty(element.value)) {
        eNumber++;
        komunikat += eNumber + '. Wprowadź nazwę firmy.\n';
        background  = element.style.background;
        element.style.background = "#FFDDDD";
        if (!eElement) eElement = element;
        ok = false;
    }

    element = form.ulica;
    if (element && formService.isEmpty(element.value)) {
        eNumber++;
        komunikat += eNumber + '. Wprowadź nazwę ulicy.\n';
        background  = element.style.background;
        element.style.background = "#FFDDDD";
        if (!eElement) eElement = element;
        ok = false;
    }

    element = form.kod;
    if (element && formService.isEmpty(element.value)) {
        eNumber++;
        komunikat += eNumber + '. Wprowadź kod pocztowy firmy.\n';
        background  = element.style.background;
        element.style.background = "#FFDDDD";
        if (!eElement) eElement = element;
        ok = false;
    }


    element = form.miasto;
    if (element && formService.isEmpty(element.value)) {
        eNumber++;
        komunikat += eNumber + '. Wprowadź miasto firmy.\n';
        background  = element.style.background;
        element.style.background = "#FFDDDD";
        if (!eElement) eElement = element;
        ok = false;
    }


    element = form.prowincja_id;
    if (element && element.value == 0) {
        eNumber++;
        komunikat += eNumber + '. Wybierz województwo.\n';
        background  = element.style.background;
        element.style.background = "#FFDDDD";
        if (!eElement) eElement = element;
        ok = false;
    }



    if (formService.isEmpty(form.login.value)) {
        eNumber++;
        komunikat += eNumber + '. Wprowadź login.\n';
        background  = form.login.style.background;
        form.login.style.background = "#FFDDDD";
        if (!eElement) eElement = form.login;
        ok = false;
//        return ok = false;
    }

    if (formService.isEmpty(form.password.value)) {
        eNumber++;
        komunikat += eNumber + '. Wprowadź hasło.\n';
        background  = form.password.style.background;
        form.password.style.background = "#FFDDDD";
        if (!eElement) eElement = form.password;
        ok = false;
//        return ok = false;
    }

    if (form.password2.value != form.password.value) {
        eNumber++;
        komunikat += eNumber + '. Wprowadzone hasła są niezgodne.\n';
        background  = form.password2.style.background;
        form.password2.style.background = "#FFDDDD";
        if (!eElement) eElement = form.password2;
        ok = false;
//        return ok = false;
    }


    if (formService.isEmpty(form.email.value)) {
        eNumber++;
        komunikat += eNumber + '. Koniecznie ustaw adres e-mail.\n';
        background  = form.email.style.background;
        form.email.style.background = "#FFDDDD";
        if (!eElement) eElement = form.email;
        ok = false;
//        return ok = false;
    } else

    if (!formService.check_email(form.email.value)) {
        eNumber++;
        komunikat += eNumber + '. Proszę wprowadzić  poprawny adres e-mail.\n';
        background  = form.email.style.background;
        form.email.style.background = "#FFDDDD";
        if (!eElement) eElement = form.email;
        ok = false;
//        return ok = false;
    }


    if (formService.isEmpty(form.imie.value)) {
        eNumber++;
        komunikat += eNumber + '. Wprowadź imię.\n';
        background  = form.imie.style.background;
        form.imie.style.background = "#FFDDDD";
        if (!eElement) eElement = form.imie;
        ok = false;
//        return ok = false;
    }

    if (formService.isEmpty(form.nazwisko.value)) {
        eNumber++;
        komunikat += eNumber + '. Wprowadź nazwisko.\n';
        background  = form.nazwisko.style.background;
        form.nazwisko.style.background = "#FFDDDD";
        if (!eElement) eElement = form.nazwisko;
        ok = false;
//        return ok = false;
    }

    if (formService.isEmpty(form.telefony.value)) {
        eNumber++;
        komunikat += eNumber + '. Wprowadź telefony.\n';
        background  = form.telefony.style.background;
        form.telefony.style.background = "#FFDDDD";
        if (!eElement) eElement = form.telefony;
        ok = false;
//        return ok = false;
    }


    if (formService.isEmpty(form.komunikator.value)) {
        eNumber++;
        komunikat += eNumber + '. Wprowadź komunikator.\n';
        background  = form.komunikator.style.background;
        form.komunikator.style.background = "#FFDDDD";
        if (!eElement) eElement = form.komunikator;
        ok = false;
//        return ok = false;
    }


    if (formService.isEmpty(form.komunikator.value)) {
        eNumber++;
        komunikat += eNumber + '. Wprowadź nazwisko.\n';
        background  = form.komunikator.style.background;
        form.komunikator.style.background = "#FFDDDD";
        if (!eElement) eElement = form.komunikator;
        ok = false;
//        return ok = false;
    }


    if (!ok) {
//        eElement.style.background = "#FFDDDD";
        eElement.focus();
        eElement.select();

        komunikat = 'Czerwonym kolorem zaznaczono niepoprawne wpisy:\n' +
            komunikat ;

        alert(komunikat);
    }

    return ok;
}

//var nipFirmy;
var elToInsert = 'firm_data';

function checkNip() {
    var tested = $el('tested_nip');
    var nip = tested.value;
//    if (nipFirmy == nip) {nipFirmy = '';return;}
    if (!formService.check_nip(nip)) {alert('Nieprawidłowy NIP'); tested.select();return;}
//    nipFirmy = nip;
    new DestSender(elToInsert).send(
        ajaxControler, {'cmd':'checkFirm', 'module':'users', 'nip':nip})
}


function registerFirm() {
    var nip = $el('nip').value;
    if (!formService.check_nip(nip)) {alert('Nieprawidłowy NIP');return;}
//    if (nipFirmy != nip) {checkNip();return;}
    var firma = $el('firma').value;
    new DestSender(elToInsert).send(
        ajaxControler, {'cmd':'registerFirm', 'module':'users', 'nip':nip, 'firma':firma})
}

