//////////////////////////////////////////////////////////////////////////////////////////////////
//INC FUNCTIONS.JS VERSIONE 1.1///////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////////////////////////
//FUNZIONI STRINGA////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////
//Formattazione html di un campo di testo/////////////////////////////////////////////////////////
//Ultima modifica: ven 17 nov 2006////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
function formatText(textElem, startTag, endTag) {
	textElem.focus();
	
	if (navigator.appName == "Microsoft Internet Explorer") {
		theSelection = document.selection.createRange().text;
		
		if (startTag.toLowerCase() == "<br>" || startTag.toLowerCase() == "<br/>") {
			document.selection.createRange().text = "<br>" + theSelection;
		}
		if (theSelection && startTag.toLowerCase() == "<a>") {
			var href = prompt("Inserire url", "http://");
			startTag = "<a href='"+href+"' target='_blank'>";
		}
		if (theSelection) {
			document.selection.createRange().text = startTag + theSelection + endTag;
			textElem.focus();
			theSelection = "";
		}	
	}
	else {
		var selLength = textElem.textLength;
		var selStart = textElem.selectionStart;
		var selEnd = textElem.selectionEnd;
	
		if (selEnd == 1 || selEnd == 2) selEnd = selLength;
	
		var s1 = (textElem.value).substring(0, selStart);
		var s2 = (textElem.value).substring(selStart, selEnd);
		var s3 = (textElem.value).substring(selEnd, selLength);

		if (startTag.toLowerCase() == "<br>" || startTag.toLowerCase() == "<br/>") {
			textElem.value = s1 + startTag + s2 + s3;			
		} 
		if ((selEnd - selStart) > 0 &&  startTag.toLowerCase() == "<a>"){
			var href = prompt("Inserire url", "http://");
			startTag = "<a href='"+href+"' target='_blank'>";
		}
		if ((selEnd - selStart) > 0){
			textElem.value = s1 + startTag + s2 + endTag + s3;
		}
	}	
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//Verifica formato CAP////////////////////////////////////////////////////////////////////////////
//Ultima modifica: lun 6 nov 2006/////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
function isCAP(string) {
	string = trim(string);
	
	var re = new RegExp("\\d\\d\\d\\d\\d");
	
	if (string.search(re) != 0) return false;
	return true;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//Verifica formato email//////////////////////////////////////////////////////////////////////////
//Ultima modifica: mer 25 ott 2006////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
function isEmail(string) {
	string = trim(string);

	if (string.indexOf("@") == -1 || string.indexOf(".") == -1 || string.length < 8)
    	return false;

	var username = string.substring(0, string.indexOf("@"));
	var domain = string.substring(string.indexOf("@")+1);
	var suffix = string.substring(string.lastIndexOf(".")+1);

	if (username.length < 2 || domain.length < 2 || suffix.length < 2 || suffix.length > 4)
    	return false;
		
	var re = new RegExp("\\s|[^A-Za-z0-9._-]|\\.{2,}|^\\.|\\.$|_{2,}|^_|_$|-{2,}|^-|-$");
	if (username.search(re) != -1 || domain.search(re) != -1 || suffix.search(/[^A-Za-z]/) != -1) return false;

	return true;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//Verifica formato euro///////////////////////////////////////////////////////////////////////////
//Ultima modifica: mer 25 ott 2006////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
function isEuro(strValue) {
	var numValue = trim(strValue.substring(strValue.indexOf("€")+1, strValue.length));
	var intValue = numValue.indexOf(",") != -1 ? numValue.substring(0, numValue.indexOf(",")) : numValue;
	var decValue = numValue.substring(numValue.indexOf(",")+1, numValue.length);
	var arrIntValue = intValue.split(".");
	var re = new RegExp("[^\\d\\.,]");

	if (numValue.search(re) != -1) return false;
	if (decValue.indexOf(",") != -1 || decValue.indexOf(".") != -1) return false;

	if (arrIntValue.length > 1) {
		if (arrIntValue[0].length == 0 || arrIntValue[0].length > 3 || arrIntValue[0] == 0) return false;
		for (i = arrIntValue.length-1; i >= 1 ; i--) {
			if (arrIntValue[i].length != 3) return false;
		}
	}
	return true;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//Rimuove spazi iniziali e finali da una stringa//////////////////////////////////////////////////
//Ultima modifica: mer 25 ott 2006////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
function trim(string) {
	var removeChar = " ";
	var returnString = string;

	while (returnString.charAt(0) == removeChar) {
	  	returnString = returnString.substring(1, returnString.length);
	}
	while (returnString.charAt(returnString.length-1) == removeChar) {
		returnString = returnString.substring(0, returnString.length-1);
  	}

	return returnString;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//Sostituisce tutte le occorrenze trovate in una stringa//////////////////////////////////////////
//Ultima modifica: mer 25 ott 2006////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
function replace(string, oldTxt, newTxt) {
	var returnString;

	returnString = string.split(oldTxt);
	returnString = returnString.join(newTxt);

	return returnString;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//Ritorna valore di un parametro passato nella querytring/////////////////////////////////////////
//Ultima modifica: mer 25 ott 2006////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
function getQueryString(strParamName) {
	var intQueryLength = location.search.length;
	var strQueryString = location.search.substring(1, intQueryLength);

	if (strParamName == null) return strQueryString;

	var arrValues = strQueryString.split("&");

	for (i=0; i<arrValues.length; i++) {
		arrValues[i] = arrValues[i].split("=");
	}

	for (i=0; i<arrValues.length; i++) {
		if (arrValues[i][0] == strParamName) {
			return unescape(replace(arrValues[i][1], "+", "%20"));
		}
	}

	return "";
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//Ritorna nome file da un percorso completo///////////////////////////////////////////////////////
//Ultima modifica: mer 25 ott 2006////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
function getFileName(path) {
	var i = path.length;

	for (i; i>=0; i--) {
		if (path.charAt(i) == "/") break
	}

	if (i != -1)
		return path.substring(i+1);
	else
		return "";
}
//////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////
//FUNZIONI FORM///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////
//Ritorna tipo di input///////////////////////////////////////////////////////////////////////////
//Ultima modifica: mer 25 ott 2006////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
function getType(input) {
	return typeof input.type == "undefined" ? input[0].type : input.type;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//Verifica se si tratta di un array di unput//////////////////////////////////////////////////////
//Ultima modifica: gio 9 nov 2006/////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
function isArray(input) {
	return typeof input.type == "undefined" ? true : false;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//Verifica input vuoti////////////////////////////////////////////////////////////////////////////
//Ultima modifica: mar 16 gen 2007////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
function isEmpty(input) {
	var strType = getType(input);
	if ((strType == "text" || strType == "textarea" || strType == "file" || strType == "password") && trim(input.value).length < 1) {
		return true;
	}
	if ((strType == "select-one" && (input.selectedIndex == 0 && input.options.length == 0) || input.value == "") || (strType == "select-multiple" && input.selectedIndex == -1)) {
		return true;
	}
	if (strType == "radio" || strType == "checkbox") {
		var isChecked = false;

		if (isArray(input)) {
			for (var i = 0; i < input.length; i++) {
				if (input[i].checked == true) {
					isChecked = true;
					break;
				}
			}
		}
		else {
			if (input.checked == true) isChecked = true
		}

		if (isChecked == false) {
			return true;
		}
	}
	return false;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//Svuota input////////////////////////////////////////////////////////////////////////////////////
//Ultima modifica: mer 25 ott 2006////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
function empty(input) {
	var strType = getType(input);

	if (strType == "text" || strType == "textarea" || strType == "password" || strType == "hidden") {
		input.value = "";
	}
	if (strType == "file") {
		return false;
	}
	if (strType == "select-one") {
		input.selectedIndex = 0;
	}
	if (strType == "select-multiple") {
		input.selectedIndex == -1;
	}
	if (strType == "radio" || strType == "checkbox") {
		if (isArray(input)) {
			for (var i = 0; i < input.length; i++) {
				input[i].checked = false;
			}
		}
		else {
			input.checked = false;
		}
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//Invia form ad un url specificato////////////////////////////////////////////////////////////////
//Ultima modifica: ven 10 nov 2006////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
function submitForm(frmForm, strHref) {
	frmForm.action = strHref;
	frmForm.submit();
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//Svuota tutti gli input di un form///////////////////////////////////////////////////////////////
//Ultima modifica: ven 10 nov 2006////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
function resetForm(frmForm) {
	for (i = 0; i < frmForm.length; i++) {
		empty(frmForm.elements[i]);
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//Seleziona-deseleziona tutti gli elementi di una checkbok, radio, select/////////////////////////
//Ultima modifica: mar 16 gen 2007////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
function selectAll(input, bselect) {
	var strType = getType(input);
	
	if (strType == "select-multiple") {
		for (var i=0; i<input.options.length; i++) {
			input.options[i].selected = bselect;
		}	
	}
	
	if (strType == "radio" || strType == "checkbox") {
		if (isArray(input)) {
			for (var i=0; i<input.length; i++) {
				input[i].checked = bselect;
			}
		}
		else {
			input.checked = bselect;
		}
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//Invia alert utilizzando l'attributo title di un input///////////////////////////////////////////
//Ultima modifica: mer 8 nov 2006/////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
function msgAlert(input, msg) {
	input = isArray(input) ? input[0] : input;  
	
	if (msg == null) msg = "Il campo \""  + input.title + "\" non è valido.";
	alert(msg);
	input.focus();

	return false;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//Disabilita l'invio del form con il tasto "INVIA"////////////////////////////////////////////////
//Ultima modifica: mer 16 lug 2008////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
function disableEnterKey(e)
{
	if(window.event){
		// Microsoft Internet Explorer
		key = window.event.keyCode;
	}
	else{
		// Firefox
		key = e.which;
	}
	
	if(key == 13){
		return false;
	}
	else{
		return true;
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////
//ALTRE FUNZIONI//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////
//Apri nuova finestra del browser/////////////////////////////////////////////////////////////////
//Parametri :copyHistory, directories, location, menubar, resizable, scrollbars,//////////////////
//////////// status, toolbar, fullscreen, channelMode, dependent, hotkeys.////////////////////////
//Ultima modifica: mer 25 ott 2006////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
function openWin(winUrl, winName, winFeatures, winWidth, winHeight, winLeft, winTop) {
	var win;

	winFeatures += (winFeatures != "" && typeof winFeatures != "undefined") ? "," : "";
	winFeatures += (winWidth != "" && typeof winWidth != "undefined") ? "width=" + winWidth : "";
	winFeatures += (winHeight != "" && typeof winHeight != "undefined") ? ",height=" + winHeight : "";
	winFeatures += (winLeft != "" && typeof winLeft != "undefined") ? ",left=" + winLeft : ",left=" + (screen.width-winWidth)/2;
	winFeatures += (winTop != "" && typeof winTop != "undefined") ? ",top=" + winTop : ",top=" + (screen.height-winHeight)/2;

	win = window.open(winUrl, winName, winFeatures);
	win.focus();

	return false;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//Ritorna l'elemento che ha provocato un evento///////////////////////////////////////////////////
//Ultima modifica: mer 25 ott 2006////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
function getTargetElement(event, strReturn) {
	var browser = new BrowserInfo();

	var targetElement = browser.ie ? event.srcElement : event.target;

	if (strReturn == "object" || strReturn == null || typeof strReturn == "undefined" || strReturn == "") return targetElement;
	if (strReturn == "tagname") return targetElement.tagName;
	if (strReturn == "name") return typeof targetElement.name != "undefined" ? targetElement.name : targetElement.id;
	if (strReturn == "id") return typeof targetElement.id != "undefined" ? targetElement.id : targetElement.name;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//Cambia il colore di un elemento/////////////////////////////////////////////////////////////////
//Ultima modifica: mer 25 ott 2006////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
function changeColor(target, newBkgColor, newTxtColor) {
	var oldBkgColor = target.style.backgroundColor;
	var oldTxtColor = target.style.color;

	target.style.backgroundColor = (newBkgColor != null && newBkgColor != "") ? newBkgColor : oldBkgColor;
	target.style.color = (newTxtColor != null && newTxtColor != "") ? newTxtColor : oldTxtColor;
	target.style.cursor = "default";
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//Oggetto per informazioni browser////////////////////////////////////////////////////////////////
//Ultima modifica: mer 25 ott 2006////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
function BrowserInfo() {
 	this.appname = navigator.appName;
	this.appcodename = navigator.appCodeName;
	this.appversion = navigator.appVersion.substring(0,4);
	this.appminorversion = navigator.appMinorVersion;
	this.useragent = navigator.userAgent;
	this.product = navigator.product;
	this.productsub = navigator.productSub;
	this.vendor = navigator.vendor;
	this.vendorsub = navigator.vendorSub;
	this.platform = navigator.platform;
	this.cpuclass = navigator.cpuClass
	this.oscpu = navigator.oscpu;
	this.language = navigator.language;
	this.browserlanguage = navigator.browserLanguage;
	this.userlanguage = navigator.userLanguage;
	this.systemlanguage = navigator.systemLanguage
	this.javaenabled = navigator.javaEnabled();
	this.cookienabled = navigator.cookieEnabled;
	this.taintenabled = navigator.taintEnabled()
	this.width = screen.width;
	this.height = screen.height;
	this.colors = screen.colorDepth;
	this.online = navigator.onLine;
	this.securitypolicy = navigator.securityPolicy;
	this.mimetypes = navigator.mimeTypes;
	this.plugins = navigator.plugins;

	if (this.useragent.indexOf("MSIE") != -1 && this.useragent.indexOf("Opera") == -1) {
		this.browsername = "Microsoft Internet Explorer";
		srt = this.useragent.indexOf("MSIE") + 4;
		stp = this.useragent.indexOf(";", srt);
		this.browserversion = this.useragent.substring(srt, stp);
	}
	if (this.useragent.indexOf("Netscape") != -1) {
		this.browsername = "Netscape";
		srt = this.useragent.indexOf("Netscape/") + 9;
		stp = this.useragent.indexOf(" ", srt);
		this.browserversion = this.useragent.substring(srt, stp);
	}
	if (this.useragent.indexOf("Firefox") != -1) {
		this.browsername = "Firefox";
		srt = this.useragent.indexOf("Firefox/") + 8;
		this.browserversion = this.useragent.substring(srt);
	}
	if (this.useragent.indexOf("konqueror") != -1) {
		this.browsername = "Konqueror";
	}
	if (this.useragent.indexOf("Opera") != -1) {
		this.browsername = "Opera";
	}
	if (this.useragent.indexOf("Safari") != -1) {
		this.browsername = "Safari";
	}

	this.ie = typeof document.all != "undefined" ? true : false;
	this.ie4 = this.ie && parseInt(this.browserversion) == 4 ? true : false;
	this.ie6 = this.ie && parseInt(this.browserversion) == 6 ? true : false;
	this.ns = this.browsername == "Netscape" ? true : false;
	this.ns4 = typeof document.layers != "undefined" ? true : false ;
	this.ns6 = !this.ie && typeof document.getElementById != "undefined" ? true : false;
	this.opera = typeof window.opera != "undefined" ? true : false;

	this.dom = typeof document.getElementsByTagName != "undefined" && typeof document.createElement != "undefined" ? true : false;
	this.halfdom = (this.opera && this.useragent.indexOf("Konqueror 7") == 0) || (this.useragent.indexOf("Konqueror") > 0 && this.useragent.indexOf("konqueror/3") == 0) ? true : false;
	this.fulldom = this.dom || this.ie4 || this.ns4;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
