function informaWarning(id) {
	item = dojo.byId(id);
	hideTooltip(item);
	item.focus();
}

function focusWarning(item) {
	hideTooltip(item);
	testItem(item, 1)
}

function blurWarning(item) {
	testItem(item, -1)
}

function changeWarning(item) {
	testItem(item, 0)
}

function testItem(item, op) {
	type = item.type;
	opcion = "x";
	if(type=='text' || type=='textarea') {
		opcion = item.value;
	} else {
		opcion = item.options[item.selectedIndex].value;
	}
	
	mens = dojo.attr(item, "invalidMessage");

	if(opcion=="" && (op==0 || op==1) && mens!=null ) {
		dijit.showTooltip(dojo.attr(item, "invalidMessage"), item); 
		dojo.removeClass(item, "dijitError");
	} else if(opcion=="" && op==-1) {
		dijit.hideTooltip(item); 
		showTooltip(item);
		dojo.addClass(item, "dijitError");
	} else {
		dijit.hideTooltip(item); 
		hideTooltip(item);
	}
}


/* Mostrar el iconito de alerta para selects y textareas */
dojo.require('dojox.uuid.generateRandomUuid');

function showTooltip(who) {
	left = false;
	
	id = who.id;
	if(id=='') who.id = dojox.uuid.generateRandomUuid();
	
	if(dojo.byId("comments_id_"+who.id)==null) {
		var div = dojo.create("div", {
			"id": "comments_id_"+who.id,
			"class": "comments_id"
		}, dojo.body());
	}
	
	elemento = document.getElementById("comments_id_"+who.id);
	elemento.innerHTML = "<a href='javascript:informaWarning(\""+who.id+"\")'><img border='0' src='http://itemsweb.esade.edu/apps/img/dojo/error.png'></a>";
	
	lay = elemento;

	winH = document.body.clientHeight;
	topW = document.body.scrollTop;

	pos = findPos(who);
	posLayer = findPos(elemento);

	layerHeight = posLayer[2];
	layerWidth = posLayer[3];
	imageHeight = pos[2];
	factorCorreccionLeft = 35;
	
	if(who.type=="textarea") factorCorreccionLeft = 35;
	
	if(left) {
		factorCorreccionLeft = pos[0]*-1;
	}
	factorCorreccionHeight = -19;

	if(!left) {
		elemento.style.left= (pos[0] + pos[3] - factorCorreccionLeft)+"px";
	} else {
		elemento.style.left= (pos[0] - layerWidth - factorCorreccionLeft)+"px";
	}

	altura = imageHeight + pos[1];

	if( (altura + layerHeight + factorCorreccionHeight - topW) > winH ) {
		altura = altura - layerHeight - factorCorreccionHeight - imageHeight;
		if(altura < imageHeight + pos[1]) {
			altura = imageHeight + pos[1] +factorCorreccionHeight;
			altura -= layerHeight;
		}
	} else {
		altura += factorCorreccionHeight
	}
	altura +=1;
	elemento.style.top=altura+"px"
	elemento.style.visibility="visible";
}

// busca la posicion de un elemento en la pantalla
function findPos(obj) {
	var curleft = curtop = curheight = curwidth = 0 ;

	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		curheight += obj.offsetHeight
		curwidth += obj.offsetWidth

		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop,curheight,curwidth];
}

function hideTooltip(who) {
	if(dojo.byId("comments_id_"+who.id)!=null) {
		document.getElementById("comments_id_"+who.id).style.visibility="hidden";
		document.getElementById("comments_id_"+who.id).innerHTML = "";
	}
}

function gestion_filtro(mostrar, layer) {
	if(mostrar==1) {
		dojo.byId(layer).className='mostrar';
	} else {
		dojo.byId(layer).className='esconder';
	}
}

function goto_error(form, name) {
	el = document.forms[form].elements[name];
	if( !isArray(el) ) el.focus();
	else el[0].focus();
}

function isArray(obj){return(typeof(obj.length)=="undefined")?false:true;}

function purgaAlerts(id) {
  selects = dojo.query('#'+id+' select ');
  for(x=0;x<selects.length;x++) {
    if(selects[x].selectedIndex==0 && dojo.attr(selects[x],"required")==true) {
      selects[x].selectedIndex=1;
      selects[x].onchange();
    }
  }
  textareas = dojo.query('#'+id+' textarea ');
  for(x=0;x<textareas.length;x++) {
    if(textareas[x].value==''  && dojo.attr(textareas[x],"required")==true ) {
      textareas[x].value='x';
      textareas[x].onchange();
    }
  }
}
