// JavaScript Document

function znajdzButton(){

    var inputy = window.document.getElementsByTagName('input');

    for(var i=0; i<inputy.length; i++){
        if(inputy[i].type == 'button'){
            return inputy[i];
        }
    }
    return false;
}


function jestPusty(wartosc){
    if(wartosc == "")
        return true;
    return false;
}

function waliduj(id,wartosc){
    if(jestPusty(wartosc))
        window.document.getElementById(id+'Failed').style.visibility = 'visible';
    else
        window.document.getElementById(id+'Failed').style.visibility = 'hidden';

    var selWartosc = new RegExp("sel.*")
    if(selWartosc.test(id) && wartosc == "0")
    window.document.getElementById(id+'Failed').style.visibility = 'visible';

    

    var submit = znajdzButton();
    //submit.disabled = "";

    submit.removeAttribute("disabled");
    //submitId = getElementById("loguj");
    //submitId.
    //submit.setAttribute('onclick',"walidujWszystkie();")

    
    var spany = window.document.getElementsByTagName('span');

    var spanRE = new RegExp(".*Failed");

    for(i=0; i<spany.length; i++){
        if(spany[i].style.visibility == 'visible' && spanRE.test(spany[i].id)){
            submit.disabled = "disabled";
            break;
        }
    }
}

function walidujWszystkie(){

    var submit = znajdzButton();
    //alert("dupa!");
    var spany = window.document.getElementsByTagName('span');

    var spanRE = new RegExp(".*Failed");

    for(var i=0; i<spany.length; i++){
        if(spanRE.test(spany[i].id)){
            var inputId = (spany[i].id).substring(0, (spany[i].id).length-6);
            var inputVal = window.document.getElementById(inputId);
            
            if(jestPusty(inputVal.value)){
                
                spany[i].style.visibility = 'visible';
                submit.disabled = "disabled";
                return false;
            }

            var selWartosc = new RegExp("sel.*")
            if(selWartosc.test(inputId) && inputVal.value == "0"){

                spany[i].style.visibility = 'visible';
                submit.disabled = "disabled";
                return false;
            }
            
        }
    }
    
    var formRE = new RegExp(".*Form");
    var formy = window.document.getElementsByTagName('form');
    for(i=0; i<formy.length; i++){
        if(formRE.test(formy[i].name)){
            formy[i].submit();
        }
    }
    return true;
}
