
/**
* @version  0.0.4-rc2
* @require  fix()
*/

var cfBorderColor     = '#000000';

var cfLangNoWord      = 'Wypełnij poprawnie wymagane pola';
var cfLangTel         = 'Podaj poprawny nr telefonu';
var cfLangMail        = 'Podaj poprawny e-mail';
var cfLangUrl         = 'Podaj poprawny adres url';
var cfLangPostCode    = 'Podaj poprawny kod pocztowy';
var cfLangNip         = 'Podaj poprawny NIP';
var cfLangPesel       = 'Podaj poprawny PESEL';
var cfWrongValue      = 'Podaj poprawną wartość';
var cfToSmallValue    = 'Zbyt mała wartość';
var cfWrongDate       = 'Niepoprawne określenie daty';
var cfTxtToShort      = 'Zbyt mało tekstu';
var cfMin             = 'Minimum';
var cfFieldsChecked   = 'musi zostać zaznaczone';

var sAllWarnings = '';
var oFirstWrong;
var bIsWarnings = false;
var bAllGood = true;

function checkText( obj, komunikat ) {
    re = /\s/gi;
    checkT = obj.value.replace(re, "");

    if( checkT == '' ) {
        if( komunikat )
            sAllWarnings += komunikat +'\n';
        if( obj.type != 'hidden' ){
            obj.style.borderColor = 'red';
            if( bIsWarnings == false )
                oFirstWrong = obj;
        }
        bIsWarnings = true;
        return false;
    } else if( obj.type != 'hidden' )
        obj.style.borderColor = cfBorderColor;
    return true;
} // end function checkText

function checkTel( obj ) {
    poprawnyTelefon = /^[0-9() ]{7,}$/i;

    if( obj.value.search( poprawnyTelefon ) == -1 ) {
        sAllWarnings += cfLangTel +'\n';
        if( obj.type != 'hidden' ){
            obj.style.borderColor = 'red';
            if( bIsWarnings == false )
                oFirstWrong = obj;
        }
        bIsWarnings = true;
        return false;
    } else if( obj.type != 'hidden' )
        obj.style.borderColor = cfBorderColor;
    return true;
} // end function checkTel

function checkEmail( obj ) {
    //re = /\s/gi;
    //check = obj.value.replace( re, "" );
    // ^[a-z0-9]+([_\\.-][a-z0-9]+)*.@([a-z0-9]+([\.-][a-z0-9]{2,4}))*$
    // ^[^@]+@[^@]+.[a-z]{2,}$
    // ^[a-z0-9]+([_\\.-][a-z0-9]+)*@([^\._])([a-z0-9_\.-]+([\.][a-z0-9]{2,4}))*$

    var poprawnyEmail = /^[a-z0-9_\.\-]+@[a-z0-9\-]+\.[a-z0-9\-\.]+$/i;
    if ( !poprawnyEmail.test( obj.value ) ) {
        sAllWarnings += cfLangMail +'\n';
        if( obj.type != 'hidden' ){
            obj.style.borderColor = 'red';
            if( bIsWarnings == false )
            oFirstWrong = obj;
        }
        bIsWarnings = true;
        return false;
    } else if( obj.type != 'hidden' )
        obj.style.borderColor = cfBorderColor;
    return true;
} // end function checkEmail

function emailCheck(obj) {
    re = /\s/gi;
    txt=obj.value.replace( re, "" );

    if (txt.indexOf("@")<3){
        alert("Ten adres jest chyba niepoprawny. Proszę, sprawdĄ początek przed znakiem '@'.");
        obj.style.borderColor = 'red';
        return false;
    } else if ( (txt.indexOf(".com")<5) && (txt.indexOf(".org")<5) && (txt.indexOf(".gov")<5) && (txt.indexOf(".net")<5 ) && (txt.indexOf(".mil")<5) && (txt.indexOf(".edu")<5) && (txt.indexOf(".pl")<5) ){
        alert("Ten adres jest chyba niepoprawny.\nProszę, sprawdx końcówkę. (powinien zawierać .com, .edu, .net, .org, .gov, .mil lub .pl)");
        obj.style.borderColor = 'red';
        return false;
    } else {
        return true;
    }
}
// End -->

function checkWww( obj ) {
    re = /\s/gi;
    check = obj.value.replace( re, "" );
    poprawnyUrl = /^[a-z\d.\\\/\:]{1,}.[a-z]{2,}$/i;

    if( check.search( poprawnyUrl ) == -1 || check == 'http://' ) {
        sAllWarnings += cfLangUrl +'\n';
        if( obj.type != 'hidden' ){
            obj.style.borderColor = 'red';
            if( bIsWarnings == false )
                oFirstWrong = obj;
        }
        bIsWarnings = true;
        return false;
    } else if( obj.type != 'hidden' )
        obj.style.borderColor = cfBorderColor;
    return true;
} // end function checkWww

function checkKod( obj ) {
    poprawnyKod = /^[0-9]{2}-[0-9]{3}$/i;

    if ( obj.value.search( poprawnyKod ) == -1 ) {
        sAllWarnings += cfLangPostCode +'\n';
        if( obj.type != 'hidden' ){
            obj.style.borderColor = 'red';
            if( bIsWarnings == false )
                oFirstWrong = obj;
        }
        bIsWarnings = true;
        return false;
    } else if( obj.type != 'hidden' )
        obj.style.borderColor = cfBorderColor;
    return true;
} // end function checkKod

function checkNip( obj ) {
    poprawnyNip1 = /^[0-9]{3}-[0-9]{3}-[0-9]{2}-[0-9]{2}$/i;
    poprawnyNip2 = /^[0-9]{3}-[0-9]{2}-[0-9]{2}-[0-9]{3}$/i;
    poprawnyNip3 = /^[a-zA-Z]{2}[ ]{1}/i;

    if( obj.value.search( poprawnyNip1 ) == -1 && obj.value.search( poprawnyNip2 ) == -1 && obj.value.search( poprawnyNip3 ) ) {
        sAllWarnings += cfLangNip +'\n';
        if( obj.type != 'hidden' ){
            obj.style.borderColor = 'red';
            if( bIsWarnings == false )
                oFirstWrong = obj;
        }
        bIsWarnings = true;
        return false;
    } else if( obj.type != 'hidden' )
        obj.style.borderColor = cfBorderColor;
    return true;
} // end function checkNip

function checkPesel( obj ) {
    poprawnyPesel = /^[0-9]{11}$/i;

    if( obj.value.search( poprawnyPesel ) == -1 ) {
        sAllWarnings += cfLangPesel +'\n';
        if( obj.type != 'hidden' ){
            obj.style.borderColor = 'red';
            if( bIsWarnings == false )
                oFirstWrong = obj;
        }
        bIsWarnings = true;
        return false;
    } else if( obj.type != 'hidden' )
        obj.style.borderColor = cfBorderColor;
    return true;
} // end function checkPesel

function checkFloat( obj, komunikat ) {
    poprawnyFloat = /^-?[0-9]{1,}[.]{1}[0-9]{1,}$/i;
    poprawnyInt = /^-?[0-9]{1,}$/i;

    if( obj.value.search( poprawnyFloat ) == -1 && obj.value.search( poprawnyInt ) == -1 ) {
        if( komunikat)
            sAllWarnings += komunikat +'\n';
        else
            sAllWarnings += cfWrongValue +'\n';
        if( obj.type != 'hidden' ){
            obj.style.borderColor = 'red';
            if( bIsWarnings == false )
                oFirstWrong = obj;
        }
        bIsWarnings = true;
        return false;
    } else if( obj.type != 'hidden' )
        obj.style.borderColor = cfBorderColor;
return true;
} // end function checkFloat

function checkInt( obj, komunikat ) {
    poprawnyInt = /^-?[0-9]{1,}$/i;

    if( obj.value.search( poprawnyInt ) == -1 ) {
        if( komunikat )
            sAllWarnings += komunikat +'\n';
        else
            sAllWarnings += cfWrongValue +'\n';
        if( obj.type != 'hidden' ){
            obj.style.borderColor = 'red';
            if( bIsWarnings == false )
                oFirstWrong = obj;
        }
        bIsWarnings = true;
        return false;
    } else if( obj.type != 'hidden' )
        obj.style.borderColor = cfBorderColor;
    return true;
} // end function checkInt

function checkFloatValue( obj, minFloat, komunikat ) {
    if( +obj.value <= +minFloat ) {
        if( komunikat )
            sAllWarnings += komunikat +'\n';
        else
            sAllWarnings += cfToSmallValue +'\n';
        if( obj.type != 'hidden' ){
            obj.style.borderColor = 'red';
            if( bIsWarnings == false )
                oFirstWrong = obj;
        }
        bIsWarnings = true;
        return false;
    } else if( obj.type != 'hidden' )
        obj.style.borderColor = cfBorderColor;
    return true;
} // end function checkFloatValue

function checkIntValue( obj, minInt, znak, komunikat ) {
    if( !minInt )
        minInt = 0;
    if( !znak )
        znak = '==';
    eval ( 'good = ( '+ +obj.value +' '+ znak +' '+ +minInt +' );' );

    if( good == true ){
        if( obj.type != 'hidden' )
            obj.style.borderColor = cfBorderColor;
    } else {
        if( komunikat )
            sAllWarnings += komunikat +'\n';
        else
            sAllWarnings += cfWrongValue +'\n';
        if( obj.type != 'hidden' ){
            obj.style.borderColor = 'red';
            if( bIsWarnings == false )
                oFirstWrong = obj;
        }
        bIsWarnings = true;
        return false;
    }
    return true;
} // end function checkIntValue

function checkDate( obj, format, separator ) {
    if( !format )
        format = "ymd";
    if( !separator )
        separator = "-";

    var d = 2;
    var m = 2;
    var y = 4;

    eval( 'var I = '+ format.charAt( 0 ) +';' );
    eval( 'var II = '+ format.charAt( 1 ) +';' );
    eval( 'var III = '+ format.charAt( 2 ) +';' );

    eval( 'var er = /^[0-9]{'+ I +'}['+ separator +']{1}[0-9]{'+ II +'}['+ separator +']{1}[0-9]{'+ III +'}$/i;' );

    if( obj.value.search( er ) == -1 ) {
        sAllWarnings += cfWrongDate +'\n';
        if( obj.type != 'hidden' ){
            obj.style.borderColor = 'red';
            if( bIsWarnings == false )
                oFirstWrong = obj;
        }
        bIsWarnings = true;
        return false;
    } else if( obj.type != 'hidden' )
        obj.style.borderColor = cfBorderColor;
    return true;
} // end function checkDate

function checkTxt( obj, min, komunikat ) {
    if( !min )
        min = 6;

    var re = /\s/gi;
    alert (obj);
    var check = obj.value.replace( re, "" );

    if( check.length < min ) {
        if( komunikat )
            sAllWarnings += komunikat +'\n';
        else
            sAllWarnings += cfTxtToShort +'\n';
        if( obj.type != 'hidden' ){
            obj.style.borderColor = 'red';
            if( bIsWarnings == false )
                oFirstWrong = obj;
        }
        bIsWarnings = true;
        return false;
    } else if( obj.type != 'hidden' )
        obj.style.borderColor = cfBorderColor;
    return true;
} // end function checkTxt

function checkCheck( obj, check, znak, komunikat, option ) {
    if( znak == '=' )
        znak = '==';

    if( option == 'string' )
    // dla danych typu string
        eval ( 'good = ( "'+ obj.value +'" '+ znak +' "'+ check.value +'" );' );
    else
    // dla danych liczbowych
        eval ( 'good = ( '+ +obj.value +' '+ znak +' '+ +check.value +' );' );

    if( good == true ) {
        if( obj.type != 'hidden' )
            obj.style.borderColor = cfBorderColor;
    } else{
        if( komunikat )
            sAllWarnings += komunikat +'\n';
        else
            sAllWarnings += cfWrongValue +'\n';
        if( obj.type != 'hidden' ){
            obj.style.borderColor = 'red';
            if( bIsWarnings == false )
                oFirstWrong = obj;
        }
        bIsWarnings = true;
        return false;
    }
    return true;
} // end function checkTxt

function compareDates( obj, check, znak, komunikat ) {
    return checkCheck( obj, check, znak, komunikat, 'string' );
} // end function compareDates

function checkCheckbox( inputName, formName, min, komunikat ) {
    // sprawdza czy zostala zaznaczona wymagana ilosc pol
    var formularz   = document.getElementById( formName );
    var inputy      = formularz.getElementsByTagName( 'input' );
    var n           = '0';

    for( var lp in inputy ) {
        if( inputy[lp].type == 'checkbox' && inputy[lp].name.search( inputName ) != -1 ) {
            if( inputy[lp].checked )
                n++;
        }
    }

    if ( n < +min ) {
        if( komunikat )
            sAllWarnings += komunikat +'\n';
        else
            sAllWarnings += cfMin + " " + min + " " + cfFieldsChecked +'\n';

        for( var lp in inputy ) {
            if( inputy[lp].type == 'checkbox' && inputy[lp].name.search( inputName ) != -1 )
                inputy[lp].style.borderColor = 'red';
        }
        bIsWarnings = true;
        return false;
    } else{
        for( var lp in inputy ) {
            if( inputy[lp].type == 'checkbox' && inputy[lp].name.search( inputName ) != -1 )
                inputy[lp].style.borderColor = cfBorderColor;
        }
    }
    return true;
} // end function checkCheckbox

function kropka( pole ){
    re = /\,/gi;
    zKropka = pole.value.replace(re, "\.");
    return zKropka;
}  // end function kropka

function checkForm( form, tablica ) {
    sAllWarnings    = '';
    bIsWarnings     = false;
    bAllGood        = true;
    oFirstWrong     = '';
    for( i in tablica ) {
        if( !tablica[i][1] || tablica[i][1] == 'simple' ) {
            bAllGood = checkText( form[tablica[i][0]], tablica[i][2] );
        } else if( ( tablica[i][1] == 'tel' ) && ( ( tablica[i][2] == 'false' && form[tablica[i][0]].value ) || !tablica[i][2] ) ) {
            bAllGood = checkTel( form[tablica[i][0]] );
        } else if( ( tablica[i][1] == 'email' ) && ( ( tablica[i][2] == 'false' && form[tablica[i][0]].value ) || !tablica[i][2] ) ) {
            bAllGood = checkEmail( form[tablica[i][0]] );
        } else if( ( tablica[i][1] == 'email1' ) && ( ( tablica[i][2] == 'false' && form[tablica[i][0]].value ) || !tablica[i][2] ) ) {
            bAllGood = emailCheck( form[tablica[i][0]] );
        } else if( ( tablica[i][1] == 'www' ) && ( ( tablica[i][2] == 'false' && form[tablica[i][0]].value ) || !tablica[i][2] ) ) {
            bAllGood = checkWww( form[tablica[i][0]] );
        } else if( ( tablica[i][1] == 'kod' ) && ( ( tablica[i][2] == 'false' && form[tablica[i][0]].value ) || !tablica[i][2] ) ) {
            bAllGood = checkKod( form[tablica[i][0]] );
        } else if( ( tablica[i][1] == 'nip' ) && ( ( tablica[i][2] == 'false' && form[tablica[i][0]].value ) || !tablica[i][2] ) ) {
            bAllGood = checkNip( form[tablica[i][0]] );
        } else if( ( tablica[i][1] == 'pesel' ) && ( ( tablica[i][2] == 'false' && form[tablica[i][0]].value ) || !tablica[i][2] ) ) {
            bAllGood = checkPesel( form[tablica[i][0]] );
        } else if( ( tablica[i][1] == 'float' ) && ( ( tablica[i][3] == 'false' && form[tablica[i][0]].value ) || !tablica[i][3] ) ) {
            form[tablica[i][0]].value = kropka( form[tablica[i][0]] );
            bAllGood = checkFloat( form[tablica[i][0]] );
            if( bAllGood ){
                form[tablica[i][0]].value = fix( form[tablica[i][0]].value );
                if( tablica[i][2] != '' )
                    bAllGood = checkFloatValue( form[tablica[i][0]], tablica[i][2] );
            }
        } else if( tablica[i][1] == 'checkbox' ) {
            bAllGood = checkCheckbox( tablica[i][0], tablica[i][3], tablica[i][2], tablica[i][4] );
        } else if( tablica[i][1] == 'date' && ( ( tablica[i][2] == 'false' && form[tablica[i][0]].value) || !tablica[i][2] ) ) { // data
            bAllGood = checkDate( form[tablica[i][0]], tablica[i][3], tablica[i][4] );
        } else if( ( tablica[i][1] == 'txt' ) && ( ( tablica[i][4] == 'false' && form[tablica[i][0]].value ) || !tablica[i][4] ) ) { // opis
            bAllGood = checkTxt( form[tablica[i][0]], tablica[i][2], tablica[i][3] );
        } else if( ( tablica[i][1] == 'check' ) && ( ( tablica[i][4] == 'false' && ( form[tablica[i][0][0]].value && form[tablica[i][0][1]].value ) ) || !tablica[i][4] ) ) { // sprawdzanie rownosci itp
            bAllGood = checkCheck( form[tablica[i][0][0]], form[tablica[i][0][1]], tablica[i][2], tablica[i][3] );
        } else if( ( tablica[i][1] == 'compareDates' ) && ( ( tablica[i][4] == 'false' && ( form[tablica[i][0][0]].value && form[tablica[i][0][1]].value ) ) || !tablica[i][4] ) ) {
            bAllGood = compareDates( form[tablica[i][0][0]], form[tablica[i][0][1]], tablica[i][2], tablica[i][3] );
        } else if( ( tablica[i][1] == 'int' ) && ( ( tablica[i][5] == 'false' && form[tablica[i][0]].value ) || !tablica[i][5] ) ) { // int - np. ilosc
            bAllGood = checkInt( form[tablica[i][0]], tablica[i][3] );
            if( tablica[i][2] && bAllGood ) {
                if( tablica[i][4] ) {
                    bAllGood = checkIntValue( form[tablica[i][0]], tablica[i][2], tablica[i][4], tablica[i][3] );
                } else
                    bAllGood = checkFloatValue( form[tablica[i][0]], tablica[i][2], tablica[i][3] );
            }
        }
    } // end for

    if( bIsWarnings == true ) {
        sAllWarnings = cfLangNoWord + '\n' + sAllWarnings;
        alert ( sAllWarnings );
        if( oFirstWrong )
            oFirstWrong.focus();
        return false;
    }
    return true;
}

function checkLength( obj ){
    linie = obj.value.split( '\n' );
    if( linie.length > 20 ){
        alert( 'Treść może zawierać maksymalnie 20 linii' );
        linie.length = 15;
        obj.value = linie.join( '\n' );
    }
    if( obj.value.length > 1000 ){
        alert( 'Treść może zawierać maksymalnie 1000 znaków' );
        obj.value = obj.value.substring( 0, 1000 );
    }
    return false;
}
// end function checkForm
