var d = document;

function MM_findObj(n, d){
    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_showHideLayers(){
    var i, p, v, obj, args = MM_showHideLayers.arguments;
    for(i = 0; i < (args.length-2); i += 3){
        if((obj = MM_findObj(args[i])) != null){
            v = args[i + 2];
            if(obj.style){
                obj = obj.style;
                v = (v == 'show') ? 'visible' : (v == 'hide') ? 'hidden' : v;
            }
            obj.visibility = v;
        }
    }
}

function MM_swapImage(){
    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 MM_swapImgRestore(){
    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(){
    if(d.images){ 
        if(!d.imgs) d.imgs = new Array();
        var i, j = d.imgs.length, a = MM_preloadImages.arguments;
        for(i = 0; i < a.length; i++){
            if (a[i].indexOf("#") != 0){ 
                d.imgs[j] = new Image;
                d.imgs[j++].src = a[i];
            }
        }
    }
}

function VB_innerHTML(div, txt){
    if((obj = fObj(div)) != null){
        with(navigator){
            if((appName == "Netscape") && (parseInt(appVersion) == 4)){
                with(obj.document){
                    write(txt);
                    close();
                }
            }
            else{
                obj.innerHTML = txt;
            }
        }
    }
}

function VB_focus() {
    var amax = document.links.length;
    for(var a = 0; a < amax; a++) {
        d.links[a].onfocus = d.links[a].blur;
    }
}

function VB_favorite(){
    window.external.AddFavorite(window.document.location.href, window.document.title);
}

function VB_top() {
    window.scrollTo(0,0);
}

function VB_back() {
    window.history.back();
}

function VB_setX(winW) {
    var scrW = screen.width;
    return Math.floor((scrW - winW) / 2);
}

function VB_setY(winH) {
    var scrH = screen.height;
    return Math.floor((scrH - winH) / 2);
}

function VB_newWin(url,W,H) {
    var w = (W || 800);
    var h = (H || 600);
    var l = VB_setX(w);
    var t = VB_setY(h);
    var op = '';
    op += "toolbar=0, menubar=0, scrollbars=0, resizable=0, status=0, location=0, directories=0, ";
    op += "height=" + h + ", width=" + w + ", left=" + l + ", top=" + t;
    s=window.open('',"",op);
    s.document.write('<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"><img src='+url+' alt=""></body>');
}

function VB_skok(url){ window.location.replace(url); }

function VB_close(){
    window.close();
}

function VB_close_refresh(){
    window.opener.location.reload();
    window.close();
}

function VB_set_what_find(){
    var lw = document.frmFindClient.lstwhat; // lista
    var hw = document.frmFindClient.what; // pole ukryte
    var val = lw.value;
    // var txt = lw.options[lw.options.selectedIndex].text
    // alert(val + "\n" + txt);
    hw.value = val;
}

var upimg=1;
var zi = 1;
function pokaz(upimg) {
//alert(upimg);
 var x = document.createElement('input');
 x.setAttribute('name','question['+upimg+',1]');
 x.setAttribute('type','text');
 x.setAttribute('value','');
 x.setAttribute('size',80);

var tr = document.createElement('tr');
td1 = document.createElement('td');
	td = document.createElement('td');
	td.appendChild(x);
	td1.style.textAlign = "right";
	td1.appendChild(document.createTextNode("Pytanie:"));
	tr.appendChild(td1);
	tr.appendChild(td);
	
 document.getElementById("add").appendChild(tr);
 
 var tr1 = document.createElement('tr');
td3 = document.createElement('td');
	td2 = document.createElement('td');
	td3.style.textAlign = "right";
	td3.appendChild(document.createTextNode("Odpowiedzi:"));
	tr1.appendChild(td3);
	
ie=document.body.filters//rozpoznaj IE
for(i=1; i<5; i++) {
z = ((upimg-1) * 4) + i;
input=ie?'<input name=answer_['+upimg+',1]>':"input"
  var x2 = document.createElement(input);
  var x1 = document.createElement('input');
  x3 = document.createElement('br');
  
 x1.setAttribute('name','answer['+upimg+',1]['+ z +']');
 x1.setAttribute('type','text');
 x1.setAttribute('value','');
 x2.type = 'radio';
 x2.name = 'answer_['+upimg+',1]';
 x2.value = z;
	td2.appendChild(x2);
	td2.appendChild(x1);
	td2.appendChild(x3);
	
	tr1.appendChild(td2);
	 document.getElementById("add").appendChild(tr1);
}
	
 //z = upimg++;
 zi++;
 //alert(zi);
 return zi;
}

var reWhitespace = /^\s+$/
var reDate = /^\d{4}\-\d{2}\-\d{2}$/
//var reTelefon = /^\+[0-9]{1,3}\.[0-9]{1,14}$/
//var reNip = /^\d{10}$/
//var reEmail = /(^(\S+@)([a-zA-Z0-9\-]+\.)+(com|net|edu|mil|gov|org|int|([a-zA-Z]{2}))$)/i
//var reHost = /^([a-zA-Z0-9\-]+\.)+(com|net|edu|mil|gov|org|int|info|([a-zA-Z]{2}))$/

function isEmpty(s) {return ((s == null) || (s.length == 0))}
function isWhitespace(s) {return (isEmpty(s) || reWhitespace.test(s));}
function isDate(s) {return reDate.test(s);}
//function isTelefon(s) {return reTelefon.test(s);}
//function isNip(s) {return reNip.test(s);}
//function isEmail(s) {return reEmail.test(s);}
//function isHost(s) {return reHost.test(s);}

function isChecked(obj) {
	multi = true;
	if (obj.type == 'radio' || obj.type == 'checkbox') multi = false;
	
	var checked = false;
	if (multi) {
	   	for (var i = 0; i < obj.length; i++)
	   		if (obj[i].checked) {checked = true; break}
	}
	else checked = obj.checked;
	return checked;
}

function check_login() {
  var error_message="";
  var error_found = false;
  var login = document.form_login.login.value;
  var passwd = document.form_login.pass.value;

  if( isWhitespace(login) || isWhitespace(passwd)){
	error_message = "Wprowadź login i hasło!\n";
    error_found = true;
  }
  if (error_found) {
    alert(error_message);
    document.location.href='index.php';
    return false;
  } else {
    return true;
  }
}

function check_admin() {
  var error_message="";
  var error_found = false;
  var i=1;
  var name = document.form_admin.name.value;
  var surname = document.form_admin.surname.value;
  var login = document.form_admin.login.value;
  var passwd = document.form_admin.passwd.value;
  var passwdRetype = document.form_admin.passwdRetype.value;
  var pass = document.form_admin.pass.value;
  var law = document.form_admin.law;
//alert(law);
/*
  if( isWhitespace(name) || isWhitespace(surname) || isWhitespace(login)){
	error_message = i++ + ". " + "Wypelnij wymagane pola formularza:\n";
    error_found = true;
  }
*/
  if (isWhitespace(name)) {
    error_message = error_message + i++ + ". " + "Wypełnij pole imię.\n";
    error_found = true;	
  }
  if (isWhitespace(surname)) {
    error_message = error_message + i++ + ". " + "Wypełnij pole nazwisko.\n";
    error_found = true;	
  }
  if (isWhitespace(login)) {
    error_message = error_message + i++ + ". " + "Wypełnij pole login.\n";
    error_found = true;	
  }
  if (isWhitespace(pass)) {
  	if (isWhitespace(passwd)) {
  		error_message = error_message + i++ + ". " + "Wypełnij pole nowe hasło.\n";
  		error_found = true;	
  	}
  	if (isWhitespace(passwdRetype)) {
  		error_message = error_message + i++ + ". " + "Wypełnij pole powtórz hasło.\n";
  		error_found = true;	
  	}
  }
  if (passwd!=passwdRetype) {
  	error_message = error_message + i++ + ". " + "Hasła są różne.\n";
    error_found = true;
  }
  if (!isChecked(law)) {
    error_message = error_message + i++ + ". " + "Wypełnij pole prawa.\n";
    error_found = true;	
  }
  if (error_found) {
    alert(error_message);
    return false;
  } else {
    return true;
  }
}

function checkAll() {
	var law = document.form_admin.law;
	for( var i=1; i<law.length; i++) {
		law[i].checked = law[0].checked;
	}
}
