var reEmail = new RegExp("^[\.a-zA-Z0-9_-]{1,}@[a-zA-Z0-9_-]{1,}[\.]{1}([a-zA-Z0-9_-]{1,}[\.]{1})*[a-zA-Z]{2,}$");
//var reWebsiteAddress = new RegExp("^[a-zA-Z0-9_-]{1,}[\.]{1}([a-zA-Z0-9_-]{1,}[\.]{1})*[a-zA-Z]{2,}/.{0,}$");
var reWebsiteAddress = new RegExp("^[a-zA-Z0-9_-]{1,}[\.]{1}([a-zA-Z0-9_-]{1,}[\.]{1})*[a-zA-Z]{2,}$");
var password = new RegExp("^[a-zA-Z0-9 ]{0,10}$");
var password1 = new RegExp("^[a-zA-Z0-9 ]{1,10}$");
var oldpassword = new RegExp("^[a-z0-9]{10}$");

var strange_domains = new Array(
    new RegExp(".*\.prv\.pl"),
    new RegExp(".*\.w\.pl"),
    new RegExp(".*\.z\.pl"),
    new RegExp(".*\.glt\.pl")
)

function doExchange() {

    if (confirm('Na pewno chcesz wymienić dostępne punkty?'))
        document.location.href = '/twojeplatnosci/?ss=1';

}

function doPayment() {

    if (confirm('Na pewno chcesz dokonać wypłaty?'))
        document.location.href = '/admin/?s=225&ss=6&a=sent';

}

function doDiscardUser(uid) {
    if (confirm('Na pewno chcesz odrzucić partnera?'))
        document.location.href = '/admin/odrzucenie/?uid='+uid+'&ss=7';
}

function onColor(co, jaki) {
    co.style.backgroundColor=jaki;
}

function offColor(co, jaki) {
    co.style.backgroundColor=jaki;
}

function ion(element) {
  element.style.backgroundColor="";
  element.style.color="#000000";
}

function ioff(element,kolor) {
  element.style.backgroundColor="#F3D2D2";
  element.style.color="#000000";
}

function popup(section, getstring, x, y) {
    
    var sx = screen.width;
    var sy = screen.height; 
    var l = sx/2-x/2;
    var t = sy/2-y/2;
    
    switch (section) {
        
        case 'help':
            
            w = window.open('/p/help.php?subject='+getstring, 'help_window',  'width='+x+',height='+y+',toolbar=no, location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no, resizable=yes');
            w.moveTo(l, t);
            break;
        
        case 'show_ad':
                w = window.open('/exampleadviewer'+getstring, 'ad_window',  'width='+x+',height='+y+',toolbar=no, location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no, resizable=yes');
            w.moveTo(l, t); 
            break;
        
        default:
            break;
    }
    
}

function trimAll(sString) {
    while (sString.substring(0,1) == ' ') {
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' '){
        sString = sString.substring(0,sString.length-1);
    }
    return sString;
}

function checkMultiAddressBox (ab) {

    var adresy = ab.value.split(',');
    var tmp = '';
    var addOk = true;
    for (var i=0; i<adresy.length; i++) {
        //tmp += adresy[i]+'\n';
        var curAdd = trimAll(adresy[i]);
        if (! curAdd.match(reWebsiteAddress) && ab.value.length > 0) addOk = false;
    }
    return addOk;
}

function isHex(entry) {
    validChar='0123456789ABCDEF'; 
    strlen=entry.length;          
    if (strlen < 6) return false;
    entry=entry.toUpperCase();          // in case of lowercase characters
    for (i=0; i < strlen; i++ ) {
        if (validChar.indexOf(entry.charAt(i)) < 0) {
           return false;
        }
    }
    return true;
}

function wyslijSortujFormStatystykKierunek(f,ordval,dir) {
    f.field.value = ordval;
    f.formsortdir.value = dir;
    f.submit();
}

function wyslijSortujFormStatystyk(f,ordval) {
    f.field.value = ordval;
    if (f.keepsettings.value == 'false') f.formsortdir.value = '';
    f.submit();
}

function wyslijSortujFormUserow(f, ordval) {
    f.field.value = ordval;
    if (f.keepsettings.value == 'false') f.formsortdir.value = '';
    f.submit();
}

function wyslijSortujFormReklam(f, ordval) {
    f.field.value = ordval;
    if (f.keepsettings.value == 'false') {
        f.formsortdir.value = '';
    }
    f.offset.value = '';
    f.submit();
}

function sprawdzFormGenerowaniaReklamy(f) {
    var evOk = true;
    /*
    var komunikat = '';
    var pola = Array(f.site_url);
    for (var i=0; i<pola.length; i++) {
        pola[i].style.background = 'white';
    }
    if (! f.site_url.value.match(reWebsiteAddress)) {
        f.site_url.style.background = '#FFF2F2';
        evOk = false;
        komunikat += 'Błędnie wypełnione lub puste pole Adres strony;\n';
    }
    */
    if (evOk) f.submit();
    else alert(komunikat);
}

function sprawdzAkceptacjeForma(f) {
    var komunikat = 'Nie można przejść dalej bez akceptacji regulaminu.';
    if (f.akceptacja.checked) {
        f.submit();
        //alert ('ok');
    }
    else alert(komunikat);
}

function sprawdzFormPomocyTechnicznej(f) {
    var evOk = true;
    var komunikat = '';
    var pola = Array(f.description);
    for (var i=0; i<pola.length; i++) {
        pola[i].style.background = 'white';
        pola[i].style.border = '1px solid #BDC1C1';
    }
    if (f.description.value == '') {
        f.description.style.background = '#FFF2F2';
        f.description.style.border = '1px solid #C42785';
        evOk = false;
        komunikat += 'Błędnie wypełnione lub puste pole Opis problemu.\n';
    }
    if (evOk) f.submit();
    else alert(komunikat);
}
    
function sprawdzFormZglaszaniaNaduzyc(f) {
    var evOk = true;
    var komunikat = '';
    var pola = Array(f.name, f.sname, f.www, f.user_id);
    for (var i=0; i<pola.length; i++) {
        pola[i].style.background = 'white';
        pola[i].style.border = '1px solid #BDC1C1';
    }
    if (isNaN(f.user_id.value)) {
        f.user_id.style.background = '#FFF2F2';
        f.user_id.style.border = '1px solid #C42785';
        evOk = false;
        komunikat += 'Błędnie wypełnione lub puste pole m2m ID.\n';
    }
    if (f.name.value == '') {
        f.name.style.background = '#FFF2F2';
        f.name.style.border = '1px solid #C42785';
        evOk = false;
        komunikat += 'Błędnie wypełnione lub puste pole Imię.\n';
    }
    if (f.sname.value == '') {
        f.sname.style.background = '#FFF2F2';
        f.sname.style.border = '1px solid #C42785';
        evOk = false;
        komunikat += 'Błędnie wypełnione lub puste pole Nazwisko.\n';
    }
    if (! f.www.value.match(reWebsiteAddress)) {
        f.www.style.background = '#FFF2F2';
        f.www.style.border = '1px solid #C42785';
        evOk = false;
        komunikat += 'Błędnie wypełnione lub puste pole Strona www.\n';
    }
    if (evOk) f.submit();
    else alert(komunikat);
}
    
function sprawdzFormZweryfikowanegoPartnera(f) {
    var evOk = true, evOk2 = true;
    var komunikat = '';
    //ustawienie ktore pola sa sprawdzane
    //var red = typeof f.reduced != 'undefined';
    //if (red)
    //  var pola = Array(f.email, f.street, f.number, f.city, f.province, f.account_no, f.nip, f.regon);
    //else 
    var pola = Array(f.imie, f.nazwisko, f.email, f.www, f.webcategory, f.street, f.number, f.city, f.province, f.nip, f.regon, f.recommended_by, f.oldpassword, f.newpassword1, f.newpassword2);

    var pola2 = document.getElementsByClassName('recoverable');
    //alert(pola2)
            
    for (var i=0; i<pola.length; i++) {
        if (typeof pola[i] != 'undefined') {
            pola[i].style.background = 'white';
            pola[i].style.border = '1px solid #BDC1C1';
        }
    }
    
    for (var i=0; i<pola2.length; i++) {
        if (typeof pola2[i] != 'undefined') {
            pola2[i].style.background = 'white';
            pola2[i].style.border = '1px solid #BDC1C1';
        }
    }
    
    if (typeof f.prywatnosc != 'undefined')
        if (!f.prywatnosc.checked) {
            komunikat += 'Aby wysłać formularz, należy zaakceptować klauzulę prywatności.\n';
            evOk = false;
        }
    if (typeof f.pelnoletnosc != 'undefined')
        if (!f.pelnoletnosc.checked) {
            komunikat += 'Aby wysłać formularz, należy byc osobą pełnoletnią.';
            evOk = false;
        }   
    if (evOk) { 
        if (typeof f.imie != 'undefined') {
            if (f.imie.value == '') {
                f.imie.style.background = '#FFF2F2';
                f.imie.style.border = '1px solid #C42785';
                evOk = false;
                komunikat += 'Błędnie wypełnione lub puste pole Imię.\n';
            }
        }
        if (typeof f.nazwisko != 'undefined') {
            if (f.nazwisko.value == '') {
                f.nazwisko.style.background = '#FFF2F2';
                f.nazwisko.style.border = '1px solid #C42785';
                evOk = false;
                komunikat += 'Błędnie wypełnione lub puste pole Nazwisko.\n';
            }
        }

        if (typeof f.oldpassword != 'undefined' && typeof f.newpassword1 != 'undefined' && typeof f.newpassword2 != 'undefined') {
        if (f.oldpassword.value != '' || f.newpassword1.value != '' || f.newpassword2.value != '') {    
            if (typeof f.oldpassword != 'undefined') {
                //if (f.oldpassword.value != '') {  
                    if (!f.oldpassword.value.match(password)) {
                        f.oldpassword.style.background = '#FFF2F2';
                        f.oldpassword.style.border = '1px solid #C42785';
                        evOk = false;
                        komunikat += 'Błędnie wprowadzone Stare hasło.\n';
                    }
                //}
            }
            
            if (typeof f.newpassword1 != 'undefined' && typeof f.newpassword2 != 'undefined' && typeof f.oldpassword != 'undefined') {
                    
                    if (((f.newpassword1.value != f.newpassword2.value) || !f.newpassword1.value.match(password1) || !f.newpassword2.value.match(password1))) {
                        
                        f.newpassword1.style.background = '#FFF2F2';
                        f.newpassword1.style.border = '1px solid #C42785';
                        f.newpassword2.style.background = '#FFF2F2';
                        f.newpassword2.style.border = '1px solid #C42785';
                        evOk = false;
                        komunikat += 'Błędnie wprowadzone pola Nowe hasło.\n';
                    }
                
            }
        }
        }
        
        if (typeof f.email != 'undefined') {        
            if (! f.email.value.match(reEmail)) {
                //f.email.style.background = '#FFF2F2';
                f.email.style.background = '#FFF2F2';
                f.email.style.border = '1px solid #C42785';         
                evOk = false;
                komunikat += 'Błędnie wypełnione lub puste pole Twój e-mail.\n';
            }
        }
        if (typeof f.www != 'undefined') {
            if (! f.www.value.match(reWebsiteAddress)) {
                f.www.style.background = '#FFF2F2';
                f.www.style.border = '1px solid #C42785';
                evOk = false;
                komunikat += 'Błędnie wypełnione lub puste pole Strona www.\n';
            }
        }
        if (typeof f.webcategory != 'undefined') {
            if (f.webcategory.options[f.webcategory.selectedIndex].value == 0) {
                f.webcategory.style.background = '#FFF2F2';
                f.webcategory.style.border = '1px solid #C42785';
                evOk = false;
                komunikat += 'Błędnie wypełnione lub puste pole Kategoria strony www.\n';
            }
        }
        if (f.street.value == '') {
            f.street.style.background = '#FFF2F2';
            f.street.style.border = '1px solid #C42785';
            evOk = false;
            komunikat += 'Błędnie wypełnione lub puste pole Ulica.\n';
        }
        if (f.number.value == '') {
            f.number.style.background = '#FFF2F2';
            f.number.style.border = '1px solid #C42785';
            evOk = false;
            komunikat += 'Błędnie wypełnione lub puste pole Numer.\n';
        }
        if (f.city.value == '') {
            f.city.style.background = '#FFF2F2';
            f.city.style.border = '1px solid #C42785';
            evOk = false;
            komunikat += 'Błędnie wypełnione lub puste pole Miasto.\n';
        }
        if (f.province.selectedIndex == 0) {
            f.province.style.background = '#FFF2F2';
            f.province.style.border = '1px solid #C42785';
            evOk = false;
            komunikat += 'Nie wybrano z pola Województwo.\n';
        }
        if (typeof f.country != 'undefined') {
            if (f.country.selectedIndex == 0) {
                f.country.style.background = '#FFF2F2';
                f.country.style.border = '1px solid #C42785';
                evOk = false;
                komunikat += 'Nie wybrano z pola Kraj.\n';
            }
        }
        /*
        if (f.account_no.value == '') {
            f.account_no.style.background = '#FFF2F2';
            f.account_no.style.border = '1px solid #C42785';
            evOk = false;
            komunikat += 'Błędnie wypełnione lub puste pole Numer konta bankowego.\n';
        }
        */
        if (f.kind.checked) {
            if (f.nip.value == '') {
                f.nip.style.background = '#FFF2F2';
                f.nip.style.border = '1px solid #C42785';
                evOk = false;
                komunikat += 'Błędnie wypełnione lub puste pole Nip.\n';
            }
            if (f.regon.value == '') {
                f.regon.style.background = '#FFF2F2';
                f.regon.style.border = '1px solid #C42785';
                evOk = false;
                komunikat += 'Błędnie wypełnione lub puste pole Regon.\n';
            }
        }
        if (typeof f.recommended_by != 'undefined') {
            if (isNaN(f.recommended_by.value)) {
                f.recommended_by.style.background = '#FFF2F2';
                f.recommended_by.style.border = '1px solid #C42785';
                evOk = false;
                komunikat += 'Błędnie wypełnione lub puste pole Osoba polecająca.\n';               
            }
        }
        var skipArray = new Array();
        for (var i=0; i<pola2.length; i++) {
            skipArray[i] = false;
            if (typeof pola2[i] != 'undefined') {
                if (pola2[i].type == 'text') {
                    if (! pola2[i].value.match(reWebsiteAddress)) {
                        if (pola2[i].value == '') {//pomin sprawdzanie kombo jesli textfield pusty
                            skipArray[i] = true;        
                        }
                        else {          
                            evOk2 = false;
                            pola2[i].style.background = '#FFF2F2';
                            pola2[i].style.border = '1px solid #C42785';
                        }
                    }
                }
                else {
                    if (pola2[i].options[pola2[i].selectedIndex].value == 0 && skipArray[i-1] != true) {
                        evOk2 = false;
                        pola2[i].style.background = '#FFF2F2';
                        pola2[i].style.border = '1px solid #C42785';
                    }
                }
            }           
        }
        if (evOk2 == false) komunikat += 'Błędnie wypełniona lista Proponowane nowe strony.\n';
        
    }
        
    if (evOk && evOk2) {
        f.submit();
        //alert ('ok');
    }
    else alert(komunikat);
    
}
    
function sprawdzFormPotencjalnegoPartnera(f) {
    var evOk = true;
    var evOk2 = true;
    
    var komunikat = '';
    var pola = Array(f.imie, f.nazwisko, f.email, f.www, f.webcategory, f.recommended_by);
    for (var i=0; i<pola.length; i++) {
        pola[i].style.background = 'white';
        pola[i].style.border = '1px solid #BDC1C1';
    }
    document.getElementById('al').style.display='none';
                
    
    if (!f.prywatnosc.checked || !f.regulamin.checked) {
        komunikat += 'Aby wysłać formularz, należy zaakceptować klauzulę prywatności oraz zapoznać się z regulaminem Programu Parnterskiego Easy Money.';
        evOk = false;
    }
    else {  
        if (f.imie.value == '') {
            f.imie.style.background = '#FFF2F2';
            f.imie.style.border = '1px solid #C42785';
            evOk = false;
            komunikat += 'Błędnie wypełnione lub puste pole Imię.\n';
        }
        if (f.nazwisko.value == '') {
            f.nazwisko.style.background = '#FFF2F2';
            f.nazwisko.style.border = '1px solid #C42785';
            evOk = false;
            komunikat += 'Błędnie wypełnione lub puste pole Nazwisko.\n';
        }
        if (! f.email.value.match(reEmail)) {
            f.email.style.background = '#FFF2F2';
            f.email.style.border = '1px solid #C42785';
            evOk = false;
            komunikat += 'Błędnie wypełnione lub puste pole Twój e-mail.\n';
        }
        if (! f.www.value.match(reWebsiteAddress)) {
            f.www.style.background = '#FFF2F2';
            f.www.style.border = '1px solid #C42785';
            evOk = false;
            komunikat += 'Błędnie wypełnione lub puste pole Adres Twojej strony www.\n';
        }
        if (f.webcategory.options[f.webcategory.selectedIndex].value == 0) {
            f.webcategory.style.background = '#FFF2F2';
            f.webcategory.style.border = '1px solid #C42785';
            evOk = false;
            komunikat += 'Błędnie wypełnione lub puste pole Kategoria strony www.\n';
        }
        if (isNaN(f.recommended_by.value)) {
            f.recommended_by.style.background = '#FFF2F2';
            f.recommended_by.style.border = '1px solid #C42785';
            evOk = false;
            komunikat += 'Błędnie wypełnione pole Easy ID osoby polecającej.\n';                
        }
        
        if (!f.alc.checked) {
            for (i in strange_domains) {
            
                if (f.www.value.match(strange_domains[i])) {
                
                    evOk2 = false;  
                
                }   
            
            }
        }
        
    }
        
    if (evOk && evOk2) {
        return true;
        //alert ('ok');
    }
    else {
        if (!evOk2) {
            document.getElementById('al').style.display='block';    
        }
        if (!evOk) {
    
            alert(komunikat);
        
        }
        
        
        return false;
    }
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function showHideElement(pathOfElement) {
    if (typeof pathOfElement != 'undefined') {
        pathOfElement.style.display = (pathOfElement.style.display == 'none') ? 'block' : 'none';
    }
    else {
        //alert('nothing to do :(');
    }
}

function pokazLubUkryj(id) {
    if (document.getElementById(id).style.display == 'none')
        pokaz(id);
    else
        ukryj(id);
}

function pokaz(id) {
    //detekcja przegladarki
    var browser_type = navigator.appName;
    var browser_version = parseInt(navigator.appVersion);
    var opera_onboard = navigator.userAgent.indexOf('Opera') != -1 ? 1 : 0;
    //przypisanie wartosci display dla roznych przegladarek
    if (browser_type == "Microsoft Internet Explorer" && browser_version >= 4 && !opera_onboard)
        var display_value = 'inline';
    else
        var display_value = 'table-row';    
    document.getElementById(id).style.display = display_value;
    document.getElementById('rozwin'+id).style.display = 'none';
    document.getElementById('zwin'+id).style.display = 'inline';
}

function ukryj(id) {
    document.getElementById(id).style.display = 'none';
    document.getElementById('zwin'+id).style.display = 'none';
    document.getElementById('rozwin'+id).style.display = 'inline';
}
