/**FUNZIONI DI SUPPORTO*/

function enableElement(toEnable){
	var elementToEnable = document.getElementById(toEnable);
	if(elementToEnable != null){
		elementToEnable.style.display = "";
	}
}

function disableElement(toDisable){
	var elementToDisable = document.getElementById(toDisable);
	if(elementToDisable != null){
		elementToDisable.style.display = "none";
	}
}

function enablePropertyById(toEnable){
	var propertyToEnable = document.getElementById(toEnable);
	if(propertyToEnable != null) {
		propertyToEnable.disabled = false;
	}
}

function enableProperty(formName, toEnable){
	var propertyToEnable = eval("document." + formName + "."+toEnable);
	var isArray = true;
	if(propertyToEnable != null){
		try{
			propertyToEnable.value.indexOf("a");
			isArray = false;
		}
		catch(e){
		}
		if(isArray){
			for(var i = 0; i < propertyToEnable.length; i++){
				propertyToEnable[i].disabled = false;
			}
		}
		else{
			propertyToEnable.disabled = false;
		}
	}
}

function disableProperty(formName, toDisable){
	var propertyToDisable = eval("document." + formName + "." + toDisable);
	var isArray = true;
	if(propertyToDisable != null){
		try{
			propertyToDisable.value.indexOf("a");
			isArray = false;	
		}
		catch(e){
		}
		if(isArray){
			for(var i = 0; i < propertyToDisable.length; i++){
				propertyToDisable[i].disabled = true;
			}
		}
		else{
			propertyToDisable.disabled = true;
		}
	}
}

function clearProperty(formName, toClear){
	var propertyToClear = eval("document." + formName + "." + toClear);
	if(propertyToClear != null){
		propertyToClear.value = "";
	}
}

function clearCheckedProperty(formName, toClear){
	var propertyToClear = eval("document." + formName+ "." + toClear);
	var isArray = true;
	if(propertyToClear != null){
		try{
			propertyToClear.value.indexOf("a");
			isArray = false;
		}
		catch(e){
		}
		if(isArray){
			for(var i = 0; i < propertyToClear.length; i++){
				propertyToClear[i].checked = false;
			}
		}
		else{
			propertyToClear.checked = false;
		}
	}
}

function trimString (toTrim) {
  //str = this != window? this : str;
  return toTrim.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function redirect(redirectPath){
	setTimeout("window.location='" + redirectPath +"'", 3000);
}

function goTo(path){
	document.location = path;
}

function submitAndClose(form){
	form.submit();
	window.close();
}

function newWindow(link, w_width, w_height, w_name) {
	/*link = escape(link);*/
	var w_left = "0";
	var w_status = "no";
	if(w_name == "riservata"){
		//w_left = (screen.width/2)-400;
		w_width = '1050';
		w_height = '600';
		w_status = "yes";
	}
	else if(link == 'inserimentoDescrizione.do'){
		w_width = '800';
		w_height = '600';
	}
	newWin = window.open(link,w_name,"height="+w_height+",width="+w_width+",toolbar=no,location=no,directories=no,status="+w_status+",menubar=no,scrollbars=yes,resizable=yes,top=0,left="+w_left+"");
}

function newWindowEscape(link, w_width, w_height, w_name) {
	link = escape(link);
	newWindow(link, w_width, w_height, w_name);
}

function resetSelect(formName, toReset){
	var selectToReset = eval("document." + formName + "." + toReset + "");
	if(selectToReset != null){
		var isArray = true;
		try{
			var test = selectToReset.options[0];
			isArray = false;
		}
		catch(e){
		}
		if(!isArray){
			selectToReset.options[0].selected = true;
		}
		else{
			for(var i=0; i<selectToReset.length; i++){
				selectToReset[i].options[0].selected = true;
			}
		}
	}
}

function setPropertyByElementNumber(form, elementNumber, propertyValue){
	var toSet = eval("document." + form + ".elements["+elementNumber+"]");
	toSet.value = propertyValue;
}


/**Email check*/
var done = false;
function checkEmail(emailProperty, alertMessage) {
	if (done){
		return false;
	}
	var emailPropertyValue = emailProperty.value;
	if (emailPropertyValue == ''){
		alert(alertMessage);
		return false;
	}
	if (emailPropertyValue.indexOf('@',0)==-1 || emailPropertyValue.indexOf('.',0) == -1 || ((emailPropertyValue.length-2) < emailPropertyValue.indexOf('.',0))){
		alert(alertMessage);
		return false;
	}
	var notAllowed = '()<>@;:\"[], ';
	var account= emailPropertyValue.substring(0,(emailPropertyValue).indexOf('@','0'));
	var domain = emailPropertyValue.substring((emailPropertyValue).indexOf('@','0')+1, emailPropertyValue.length);
	for(var i = 0; i < account.length; i++){
	   charToCheck = account.charAt(i);
	   if (notAllowed.indexOf(charToCheck) >= 0){
			alert(alertMessage);
			return false;
	   }
	}
	for (var j = 0; j < domain.length; j++){
	   charToCheck = domain.charAt(j);
	   if (notAllowed.indexOf(charToCheck) >= 0){
			alert(alertMessage);
			return false;
	   }
	}
	done = true;
	return true;
}

function trim(s) {
    return s.replace( /^\s*/, "" ).replace( /\s*$/, "" );
}