function showFields(opt) {
	argv = showFields.arguments;
	a = 1;
	while (argv[a]) {
		if (document.getElementById('dbformF'+argv[a])) {
			tr = document.getElementById('dbformF'+argv[a]).parentNode;
			while (tr.tagName.toUpperCase() != 'TR') tr = tr.parentNode;
//			tr.style.display = (opt)?'':'none';
//			alert(tr.className);
			tr.className = (opt)?'formRow':'hidden';
		
		}
		++a;
	}
	footReset();	
}

function footReset() {
	f = document.getElementById('footer').style.position="relative";
	f = document.getElementById('footer').style.position="absolute";
}

function openGmapSelector(gmapIID,gmapKey) {
	var addr = (document.getElementById('form')['ulica'] && (document.getElementById('form')['miasto'] || document.getElementById('form')['miejscowosc']))?document.getElementById('form')['ulica'].value+','+(document.getElementById('form')['miasto']?document.getElementById('form')['miasto'].value:document.getElementById('form')['miejscowosc'].value):'szukaj ulicy...'; 
	var gmW=window.open('gmap.html','gmap','left=100,top=100,width=400,height=344');
	gmW.document.writeln('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN">'
	+ '<html><head><title></title>'
	+ '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2">'
	+ '<style type="text/css">'
	+ 'html, body { margin: 0; padding: 0; overflow: hidden; }'
	+ 'form { display: inline;}'
	+ '</style></head><body><form name="ne">'
	+ '<input type="text" name="gmap" id="gmapll" value="'+document.getElementById(gmapIID).value+'" style="margin:0;width:400px;border:0;background:#666;color:#fff;text-align:center;">'
	+ '<div id="gmapDivgmap" style="width:400px; height: 300px;"></div>'
	+ '<input id="gmapGCgmap" type="text" onclick="if(this.value==\'szukaj ulicy...\')this.value=\'\'" value="'+addr+'" style="width:290px;border:1px #666 solid;background:#fff;color:#000;text-align:left;">'
	+ '<input style="width:56px;border:1px #666 solid;background:#666;color:#fff;text-align:center;" type="button" value="szukaj" onclick="gmapshowAddressgmap(document.getElementById(\'gmapGCgmap\').value)" />'
	+ '<input style="width:48px;border:1px #063 solid;background:#063;color:#fff;text-align:center;font-weight:bold;" type="button" value="OK" onclick="window,opener.document.getElementById(\''+gmapIID+'\').value=document.getElementById(\'gmapll\').value;window.close();" />'
	+ '<scr'+'ipt src="http://maps.google.com/maps?file=api&v=2&key='+gmapKey+'" type="text/javascript"></scr'+'ipt>'
	+ '<scr'+'ipt src="/js/dbform_gmap.js" type="text/javascript"></scr'+'ipt>'
	+ '</form></body></html>');
	gmW.document.close();
	gmW.focus();

}

function dbfromValid(f) {
	
	var m = document.getElementById('dbformF'+mmm);
	var tr = document.getElementById('dbformI'+mmm);


	for (a = 0; a < dbformFields.length; ++a) {
		t = dbformFields[a];
		//alert (t.req);
		if (t.req == '1' || t.js) {
			i = document.getElementById('dbformF'+t.field);
			al = document.getElementById('dbformI'+t.field);
			if (t.req == '1' && t.type == 'checkboxes') {
				cl = 1;
				tf = 0;
				$('input',i).each(function(){
					tf += this.checked?1:0;
					++cl; 
				});
				//alert (tf + '>' + t.size);
				if (tf == 0) { 
					i.parentNode.parentNode.className = 'alertRow';
					al.innerHTML = t.alert1;
					return false;
				}
				else if (tf > t.size) {
					i.parentNode.parentNode.className = 'alertRow';
					al.innerHTML = t.alert2;
					return false;
				}
				else {
					i.parentNode.parentNode.className = 'formRow';
					al.innerHTML = '';
				}	
			}
			else if (t.req  == '1' && i.tagName.toUpperCase() == "SELECT" && i.selectedIndex == 0) {
				al.innerHTML = t.alert1;
				i.focus(); 
				i.parentNode.parentNode.className = 'alertRow';
				return false;
				
			}
			else if (t.req  == '1' && t.type == 'checkbox' && !i.checked) {
				al.innerHTML = t.alert1;
				i.focus(); 
				i.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.className = 'alertRow';
				return false;
				
			}
			else if (t.req  == '1' && i == m) {
				try{
					if (i.value.indexOf('@',0) == -1 || i.value.indexOf('.',0) == -1){
						al.innerHTML = '"Nieprawidłowy email"';
						i.focus(); 
						i.parentNode.parentNode.className = 'alertRow';
						return false;
					}
					else {
						al.innerHTML = '';
						i.parentNode.parentNode.className = 'formRow';
					}
				}
				catch(e){}
			}

			else if (t.req == '1' && t.type == 'foto'  && i.value == '' && document.getElementById('__http__dbformF'+t.field).value != '' ) {
				al.innerHTML = ''; //t.info;
				if  (i.parentNode.parentNode.className == 'alertRow') i.parentNode.parentNode.className = 'formRow';	
			}
			else if (t.req == '1' && i.tagName.toUpperCase() != "SELECT" && i.value == '') {
				al.innerHTML = t.alert1;
				i.focus(); 
				i.parentNode.parentNode.className = 'alertRow';
				return false;
			}
			else if (t.js != '' && !eval(t.js)) {
				//alert (t.field);
				al.innerHTML = t.alert2; //t.js + ':' + eval(t.js); ;
				i.focus(); 
				i.parentNode.parentNode.className = 'alertRow';
				return false;		
			}
			else {
				al.innerHTML = ''; //t.info;
				if  (i.parentNode.parentNode.className == 'alertRow') i.parentNode.parentNode.className = 'formRow';	
			}
		}
		
	}
	if (document.getElementById('alertmsg') && document.getElementById('alertwin')) {
		document.getElementById('alertmsg').innerHTML = 'Trwa wysyłanie formularza...';
		document.getElementById('alertwin').style.display = 'block';
		//alert(document.body.scrollTop);
		if (document.documentElement.scrollTop) document.getElementById('alertwin').style.top = (document.documentElement.scrollTop + 200) + 'px'; 
		else document.getElementById('alertwin').style.top = (document.body.scrollTop + 200) + 'px';
		
	}
	return true;
}

function addTLine(tableID) {
	//trs = document.getElementById(slineID);
	tbl = document.getElementById(tableID).firstChild;
	dupli = false;
	kids = tbl.childNodes;
	//alert(kids.length+kids[0].tagName);
 	for (var i = 0; i < kids.length; i++) {
		t = kids[i].tagName;
		//alert(t+':'+kids[i].id);
		if (t == 'TR' && dupli && kids[i].className == 'eline') {
			frename(dupli);
			dupli.style.display = '';
			tbl.insertBefore(dupli,kids[i]);
			break;		
		}

 		if (t == 'TR') {
			dupli = kids[i].cloneNode(true); 
		}
		
		
	}
	//reLp(tableID);
	void(0);
}
function delrow(timg,ob) {
	timg.parentNode.firstChild.value = 1;
	timg.parentNode.parentNode.style.display = 'none';
	timg.parentNode.parentNode.firstChild.innerHTML = '1;';
	reLp(ob);
	void(0);
}

function frename(ob) {
	var d = ob.childNodes;
	//alert (d.length);
	for (var i = 0; i < d.length; i++) {
		//alert (i + d[i].tagName);
		if (d[i].nodeType == 3) continue;
		

		if (d[i].tagName == 'TD' && d[i].innerHTML.match(/\d+[:;]/)) {
			no = d[i].innerHTML.match(/\d+[:;]/);
			no = parseInt(no) + 1;
			d[i].innerHTML = no+':';
		}
		if (d[i].tagName == 'INPUT') {
			no = d[i].name.match(/\d+/);
			no = parseInt(no) + 1;
			d[i].name = d[i].name.replace(/\[\d+\]/,'['+no+']');
			d[i].id = d[i].id.replace(/\d+$/,no);
			d[i].value = '';
		}
		else if (d[i].tagName == 'SELECT') {
			no = d[i].name.match(/\d+/);
			no = parseInt(no) + 1;
			d[i].name = d[i].name.replace(/\[\d+\]/,'['+no+']');
			d[i].id = d[i].id.replace(/\d+$/,no);
			d[i].selectedIndex = 0;
			d[i].onfocus = function () {}
			d[i].onchange = function () {}
		}
		else if (d[i].firstChild) frename(d[i]);
	}
}

function testCheckboxes(f) { 
				cl = 1;
				tf = 0;
				while (ic = document.getElementById('dbformF'+f+cl)) {
					tf += ic.checked?1:0;
					++cl; 
				}
				return tf;
}

function dbfield(a) {
	return document.getElementById('form')[a];
}


function ismaxlength(obj){
var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
if (obj.getAttribute && obj.value.length>mlength)
obj.value=obj.value.substring(0,mlength)
}


function addTLine2(tableID) {
	//trs = document.getElementById(slineID);
	tbl = document.getElementById(tableID).firstChild;
	dupli = false;
	kids = tbl.childNodes;
	//alert(kids.length+kids[0].tagName);
 	for (var i = 0; i < kids.length; i++) {
		t = kids[i].tagName;
		//alert(t+':'+kids[i].id);
		if (t == 'TR' && dupli && kids[i].className == 'eline') {
			frename2(dupli);
			dupli.style.display = '';
			tbl.insertBefore(dupli,kids[i]);
			$('a.dp-choose-date',tbl.childNodes[i]).remove();
			$('.date-pick-tpl',tbl.childNodes[i]).datePicker({startDate:'2009-01-01'});
			break;		
		}

 		if (t == 'TR') {
			dupli = kids[i].cloneNode(true); 
		}
		
		
	}
	reLp(tableID);
	void(0);
}
function delrow2(timg,ob) {
	timg.parentNode.firstChild.value = 1;
	timg.parentNode.parentNode.style.display = 'none';
	//timg.parentNode.parentNode.firstChild.innerHTML = '1;';
	reLp(ob);
	void(0);
}

function frename2(ob) {
	var d = ob.childNodes;
	//alert (d.length);
	for (var i = 0; i < d.length; i++) {
		//alert (i + d[i].tagName);
		if (d[i].nodeType == 3) continue;
		

		if (d[i].tagName == 'TD' && d[i].innerHTML.match(/\d+[:;]/)) {
			no = d[i].innerHTML.match(/\d+[:;]/);
			no = parseInt(no) + 1;
			d[i].innerHTML = no+':';
		}
		if (d[i].tagName == 'INPUT') {
			no = d[i].name.match(/\[\d+\]/);
			no = no.toString();
			no = parseInt(no.replace(/[\[\]]/g,'')) + 1;
			d[i].name = d[i].name.replace(/\[\d+\]/,'['+no+']');
			d[i].id = d[i].id.replace(/\d+$/,no);
			d[i].value = '';
		}
		else if (d[i].tagName == 'SELECT') {
			//alert (d[i].name + ',' + d[i].name.match(/\[\d+/) + ',' + (parseInt(d[i].name.match(/\d+/))+1));
			no = d[i].name.match(/\[\d+/).toString().substring(1);
			no = parseInt(no) + 1;
			d[i].name = d[i].name.replace(/\[\d+\]/,'['+no+']');
			d[i].id = d[i].id.replace(/\d+$/,no);
			d[i].selectedIndex = 0;
			d[i].onfocus = function () {}
			d[i].onchange = function () {}
		}
		else if (d[i].firstChild) frename2(d[i]);
	}
}


$(document).ready(function(){

	$('.node-1 input').before('<img src="/img/om_plus.gif" class="node-plus" />');
	$('.node-0 input').before('<img src="/img/om_plus.gif" class="node-none" />');
	
	$('img.node-plus').live('click',function(){
		var l = this.parentNode;
		var lv = parseInt(l.className.match(/indent-(\d+)/)[1]) + 1;
		//alert (lv);
		while (l.nextSibling) {
			l = l.nextSibling;
			if (l.nodeType == 3) continue;
			if ($(l).hasClass('indent-'+lv)) {
				l.style.display = 'block';
			}
			else if ($(l).hasClass('indent-'+(lv-1)) || $(l).hasClass('indent-'+(lv-2)) || $(l).hasClass('indent-'+(lv-3))) {
				break;
			}
		}
		this.className = 'node-minus';
		this.src = this.src.replace('_plus','_minus');
		return false;
	});

	$('img.node-minus').live('click',function(){
		var t = this;
		var l = this.parentNode;
		var lv = parseInt(l.className.match(/indent-(\d+)/)[1]) + 1;
		//alert (lv);
		while (l.nextSibling) {
			l = l.nextSibling;
			if (l.nodeType == 3) continue;
			
			if ($(l).hasClass('indent-'+(lv-1)) || $(l).hasClass('indent-'+(lv-2)) || $(l).hasClass('indent-'+(lv-3))) {
				break;
			}			
			$('img.node-minus',l).each(function(){
				this.className = 'node-plus';
				this.src = this.src.replace('_minus','_plus');
			});			
			l.style.display = 'none';
			
		}
		t.className = 'node-plus';
		t.src = this.src.replace('_minus','_plus');
		return false;
	});


});
