function gel(elo)
{
	if(typeof elo == 'string')
		elo = document.getElementById(elo);
	return elo;
}

function current_style(el)
{
	el = gel(el);
	var currstyle = el.currentStyle;
	if(!currstyle || typeof currstyle == 'undefined')
		currstyle = el.ownerDocument.defaultView.getComputedStyle(el, null);
	return currstyle;
}


function attr(el,name)
{
	//alert(el+" "+name);
	el = gel(el);
	var oattr = el.attributes[name];
	//alert(oattr+" "+typeof oattr+" "+oattr.value);
	if(oattr) {
		//alert(oattr);
		return oattr.value;
	}
	return el[name];
}


/*
function compare_obj(obj,cmp_obj)
{
	
}

function set_obj(obj,set_obj)
{

}

function getElements(startel,cmp_obj)
{

}

function get_pos_size()
{

}
*/

//--------------------------------------------------------------------------

//utile per captcha
function reload_img(img)
{
	if(typeof img == 'string')
		img = document.getElementById(img);
	
	if(!img.orig_url)
		img.orig_url = img.src;
	
	var now = new Date();
	if(img.orig_url.indexOf('?')>-1)
		img.src = img.orig_url + "&js_rand=" + now.getTime();
	else
		img.src = img.orig_url + "?js_rand=" + now.getTime();
}


function switch_icon(imgel,icons) {
	imgel = gel(imgel);

	var cur_src = imgel.src.toLowerCase();
	var val0 = icons[0].toLowerCase();

	if(cur_src.indexOf(val0) > 0)
		imgel.src = icons[1];
	else
		imgel.src = icons[0];
}

function toggle_display(el)
{
	if(typeof el == 'string')
		el = document.getElementById(el);
	
	var currstyle = el.currentStyle;
	if(!currstyle || typeof currstyle == 'undefined')
		currstyle = el.ownerDocument.defaultView.getComputedStyle(el, null);
	
	if(currstyle.display=='none')
			el.style.display='';
	else
		el.style.display='none';
}


function first_child(el,tagname) {
	if(typeof el == 'string')
		el = document.getElementById(el);
	var arr = el.getElementsByTagName(tagname);
	if(arr.length>0)
		return arr[0];
	return null;
}

function tm_toggle(img_el,icons,display_classes) {
	var curr_li = parent_tag(img_el,'LI');
	var subul = first_child(curr_li,'UL');
	if(subul) {
		toggle_display(subul);
		if(icons)
			switch_icon(img_el,icons);
		if(display_classes)
			switch_class(subul,display_classes);
	}
}


function parent_tag(startel,tagname)
{
	if(typeof startel == 'string')
		startel = document.getElementById(startel);
	while(startel)
	{
		if( typeof startel.nodeName!='undefined' && startel.nodeName==tagname.toUpperCase() )
			return startel;
		startel = startel.parentNode;
	}
}

function submit_parent(start_ele)
{
	var parent_form = parent_tag(start_ele,'FORM');
	if(parent_form)
		parent_form.submit();
}

function clear_inputs(startel)
{
	if(typeof startel == 'string')
		startel = document.getElementById(startel);
		
	var inputs = startel.getElementsByTagName('INPUT');
	for(var i=0;i<inputs.length;i++)
		inputs[i].value = '';
	
	var inputs = startel.getElementsByTagName('SELECT');
	for(var i=0;i<inputs.length;i++)
		inputs[i].value = '';
	
	var inputs = startel.getElementsByTagName('TEXTAREA');
	for(var i=0;i<inputs.length;i++)
		inputs[i].value = '';
}

//esempio: add_row('row_tpl','tabid',this)
function add_row(tpl_el,el_before)
{
	if(typeof tpl_el == 'string')
		tpl_el = document.getElementById(tpl_el);
	if(typeof tab_el == 'string')
		tab_el = document.getElementById(tab_el);
	if(typeof el_before == 'string')
		el_before = document.getElementById(el_before);

	var newrow = tpl_el.cloneNode(true);
	var tab_or_tabbody = el_before.parentNode;
	var insele = tab_or_tabbody.insertBefore(newrow,el_before);
	
	//force display and clear inputs
	insele.style.display = '';
	clear_inputs(insele);
}

function unify_checks(checkel,startel)
{
	if(typeof checkel == 'string')
		checkel = document.getElementById(startel);
		
	if(typeof startel == 'string')
		startel = document.getElementById(startel);
		
	var arrinput = startel.getElementsByTagName('INPUT');
	//alert(arrinput.length);
	for(var i=0;i<arrinput.length;i++)
	{
		if(arrinput[i].type=='checkbox' && arrinput[i]!=checkel)
			arrinput[i].checked = checkel.checked;
	}
}

function remove_element(el)
{
	if(typeof el == 'string')
		el = document.getElementById(el);
	el.parentNode.removeChild(el);
}

//abilita o disabilita gli input, textarea, select
//se newstate non č specificato verrā invertito lo stato attuale
function toggle_inputs(startel,newstate)
{
	var arr = startel.getElementsByTagName('INPUT');
	for(var i=0;i<arr.length;i++)
	{
		if(newstate)
			arr[i].disabled = newstate;
		else
			arr[i].disabled = !arr[i].disabled;
	}
	
	var arr = startel.getElementsByTagName('SELECT');
	for(var i=0;i<arr.length;i++)
	{
		if(newstate)
			arr[i].disabled = newstate;
		else
			arr[i].disabled = !arr[i].disabled;
	}
	
	var arr = startel.getElementsByTagName('TEXTAREA');
	for(var i=0;i<arr.length;i++)
	{
		if(newstate)
			arr[i].disabled = newstate;
		else
			arr[i].disabled = !arr[i].disabled;
	}
}


function clone(o) {
	//TODO: gestire clonaz arrays
	/*
	if( typeof o.length != 'undefined' )
		*/
	var r = {};
	for(k in o)
		r[k] = o[k];
	return r;
}


function addevent(obj, evType, fn)
{
	if(obj.addEventListener)
	{
		obj.addEventListener(evType,fn,false);
		return true;
	}
	else if(obj.attachEvent)
	{
		var r = obj.attachEvent("on"+evType,fn);
		return r;
	}
	else
		return false;
}

function fix_buttons() {
	var btns = document.getElementsByTagName('BUTTON');
	for(var i=0;i<btns.length;i++) {
		var cbtn = btns[i];
		var iname = cbtn.name;
		if(iname) {
			var bform = parent_tag(cbtn,'FORM');
			if(bform) {
				cbtn.name = '';
				var ni = document.createElement('INPUT');
				ni.type = 'hidden';
				ni = bform.appendChild(ni);
				ni.name = iname;
				ni.value = '';
				var vatt = cbtn.attributes['value'];
				if(vatt)
					ni.value = vatt.value;
			}
		}
	}
}


function add_child(model,box){
 if(typeof model == 'string')
 model = document.getElementById(model);
 if(typeof box == 'string')
 box = document.getElementById(box);

 var newel = model.cloneNode(true);
 box.appendChild(newel);
 clear_inputs(newel);
}

function add_hidden(form_or_sub_element,name_value_obj) {
	var form = parent_tag(form_or_sub_element,'FORM');
	for(k in name_value_obj) {
		var ni = document.createElement('INPUT');
		ni.type = 'hidden';
		ni.name = k;
		ni.value = name_value_obj[k];
		form.appendChild(ni);
	}
	return true;
}

//------------------------------------------------------------------------------


function escapeHTML(s)
{
	var div = document.createElement('div');
	var text = document.createTextNode(s);
	div.appendChild(text);
	return div.innerHTML;
}

function getElementsByAttribute(root_element,tag_name,attr_name)
{
	var elements = root_element.getElementsByTagName(tag_name);
	var ret = [];
	var c=elements.length;
	//alert(c);
	for (var i=0; i<c ; i++)
	{
		var e = elements[i];
		//alert(e);
		var at = attr(e,attr_name);
		//alert(e+" "+at);
		if(at) {
			//alert('getElementsByAttribute(): aggiunto '+e);
			ret.push(e);
		}
	}
	//alert(ret.length);
	return ret;
}

function getElementsByAttributeValue(root_element,tag_name,attr_name,attr_value)
{
	var elements = root_element.getElementsByTagName(tag_name);
	var ret = [];
	var c=elements.length;
	for (var i=0; i<c ; i++)
	{
		var e = elements[i];
		if(attr(e,attr_name)==attr_value)
			ret.push(e);
	}
	return ret;
}

function VerifyForm(form_element,invalid_classname)
{
	if(!invalid_classname)
		invalid_classname = 'invalid';

	var validators = {
		'email' : '^[a-zA-Z0-9._%+-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z]{2,4}$',
		'notempty' : '^.+$',
		'numeric': '^\d+$',
		'alphanumeric': '^[a-zA-Z0-9]+$',
		'chars': '^[a-zA-Z]+$',
		'captcha': '^[a-zA-Z0-9]{5}$',
		'password': function(el) {
			var rptel = attr(el,'repeatel');
			if(!rptel)
				rptel = 'password_repeat';
			rptel = gel(rptel);
			if(el.value && el.value===rptel.value) {
				remclass(el,'invalid');
				remclass(rptel,'invalid');
				return true;
			}
			addclass(el,'invalid');
			addclass(rptel,'invalid');
			return false;
		}
	}

	var els = getElementsByAttribute(form_element,"*","verify");
	var c = els.length;
	//alert("da verficare: "+c);
	var ninvalid = 0;
	for(var i=0; i<c; i++)
	{
		var e = els[i];
		var verify_val = attr(e,'verify');
		var verify_x = validators[verify_val];
		//alert(str_regexp);
		var verify_ret = false;
		if(typeof verify_x === 'string'){
			var rx = new RegExp(verify_x);
			verify_ret = rx.test(e.value);
		} else if(typeof verify_x === 'function') {
			verify_ret = verify_x(e);
		}
			
		if(!verify_ret) {
			addclass(e,invalid_classname);
			if(ninvalid===0) {
				var emsg = attr(e,'errmsg');
				if(emsg) {
					alert(emsg);
				}	else {
					//alert('Il campo "'+e.name+'" non č valido!');
					alert('Il campo "'+e.id+'" non č valido!');
				}
			}
			ninvalid++;
		} else {
			remclass(e,invalid_classname);
		}
	}
	
	//alert("invalidi: "+ninvalid);
	
	if(ninvalid===0)
		return true;
	
	return false;
	//return true;
}


//------------------------------------------------------------------------------



function hasclass(e,classname)
{
	if(typeof e == 'string')
		e = document.getElementById(e);
	
	var rx = new RegExp("(^|\\s)" + classname + "(\\s|$)");
	return rx.test(e.className);
}

function remclass(e,classname)
{
	if(typeof e == 'string')
		e = document.getElementById(e);
	
	//alert( "hasclass: "+classname+">"+this.hasclass(e,classname) );
	
	if(!this.hasclass(e,classname))
		return false;
	
	var rx = new RegExp("(^|\\s)" + classname + "(\\s|$)");
	var sc = e.className;
	//alert("class: "+sc);
	if(typeof sc == 'string')
	{
		sc = sc.replace(rx,'','gis');
		//alert("new class: "+sc);
		e.className = sc;
		return true;
	}			
	return false;
}

function addclass(e,classname)
{
	if(typeof e == 'string')
		e = document.getElementById(e);
	
	this.remclass(e,classname);
	var sc = e.className;
	if(typeof sc != 'string')
		sc = "";
	sc += " "+classname+" ";
	e.className = sc;
	return true;
}


function switch_class(e,arrclassnames)
{
	if(typeof e == 'string')
		e = document.getElementById(e);
	
	if(hasclass(e,arrclassnames[0])) {
		remclass(e,arrclassnames[0]);
		addclass(e,arrclassnames[1])
	}
	
	if(hasclass(e,arrclassnames[1])) {
		remclass(e,arrclassnames[1]);
		addclass(e,arrclassnames[0])
	}
}
























