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()
{

}

//--------------------------------------------------------------------------

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 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 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 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_child2(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);
	var image = newel.getElementsByClassName('filetype')[0];
  image.src = "";
  image.style.display = "none";
	clear_inputs(newel);
}

function add_icon(el,icone)
{
  var cont = el.parentNode;
  var filename = el.value;
  if(filename.lastIndexOf(".")>0)
  {
    var ext = filename.substring((filename.lastIndexOf(".")+1));
    var ico = "icon_"+ext+".gif";
  }
  else var ico = "icon_generic.gif";
  if(jQuery.inArray(ico,icone)<0) ico = "icon_generic.gif";
  var image = cont.getElementsByClassName('filetype')[0];
  image.src = "../img/doc_types/"+ico;
  var t = setTimeout(function () {image.style.display = "inline";},200);
}

/*
function add_before(tpl_el,el_before)
{
	if(typeof tpl_el == 'string')
		tpl_el = document.getElementById(tpl_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');
	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 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($("#azione").val()!="reg" && $("#azione").val()!="contatti") {var autoriz = 1;}
  else { if($("#aut").attr("checked")) var autoriz = 1; else var autoriz=0;}

  if($("#azione").val()=="reg")
  {
    var resp = $.ajax({
                url: "cntemail.php?email="+$("#email").val(),
                async: false
              }).responseText;
    if(resp == "notok")
    {
      $("#email").addClass(invalid_classname);
      alert($("#alertlang").val());
		  return false;
		}
  }

  if($("#azione").val()=="regadmin")
  {
  	var resp = $.ajax({
                url: "../cntemail.php?email="+$("#email").val(),
                async: false
              }).responseText;
    if(resp == "notok")
    {
      $("#email").addClass(invalid_classname);
      alert("Non è possibile effettuare l'inserimento di un utente con questo indirizzo email,\npoichè è già presente nel nostro database!");
		  return false;
		}
	}

  if(autoriz)
  {
	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;
	}
	else
	{
    alert("Bisogna dare il consenso al trattamento dei dati personali");
    return false;
  }
}

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])
	}
}

function conf_abil(el,email)
{
var r=confirm("Inviare comunicazione via email?\n\n(clicca su 'Cancel' per non inviarla!)");
if (r==true)
  {
	el.href += "&email="+email;
  }/*
else
  {

  }*/
}

