/*
	######################################################################################################
	########################################################################################### NAVIGATION
	######################################################################################################
*/

function openDoc (iURL) {
	document.location = iURL;
}

function openBoutiqueVR (filename) {
	
	var max_obj_w = 987;
	var max_obj_h = 740;
	var w = (screen.width > max_obj_w) ? max_obj_w :  screen.width;
	var h = (screen.height > max_obj_h) ? max_obj_h :  screen.height;
	
	var fName = filename;
	fName += "?obj_w=" + w;
	fName += "&obj_h=" + h;
	var windowName = "boutiqueVR";
	var NS6 = (document.getElementById && navigator.appName.indexOf("Netscape")>=0 )? true: false;
	var width = (NS6) ? w : w ;
	var height = (NS6) ? h : h ;
	var scrollbars = "0";
	openPopup(fName,windowName,width,height,scrollbars);
	
}

function openPrivacyPopUp (iFileName) {
	var fName = iFileName;
	var windowName = "privacy";
	var NS6 = (document.getElementById && navigator.appName.indexOf("Netscape")>=0 )? true: false;
	var width = (NS6) ? 600 : 617 ;
	var height = (NS6) ? 330 : 330 ;
	var scrollbars = "0";
	openPopup(fName,windowName,width,height,scrollbars);
}

function openVideoPopUp (iFileName) {
	var fName = iFileName;
	var windowName = "videos";
	var NS6 = (document.getElementById && navigator.appName.indexOf("Netscape")>=0 )? true: false;
	var width = (NS6) ? 600 : 617 ;
	var height = (NS6) ? 270 : 270 ;
	var height = (NS6) ? 300 : 300 ;
	openPopup(fName,windowName,width,height);
}

function openCreditsPopup (iFileName) {
	var fName = iFileName;
	var windowName = "credits";
	var NS6 = (document.getElementById && navigator.appName.indexOf("Netscape")>=0 )? true: false;
	var width = (NS6) ? 400 : 400 ;
	var height = (NS6) ? 400 : 400 ;
	var scrollbars = "no";
	openPopup(fName,windowName,width,height,scrollbars);
}

function openPopup (fileName,windowName,width,height,scrollbars) {
	ileft=(screen.width-width)/2;
	itop=(screen.height-height)/2;
	params = "toolbar=0,";
	params += "location=0,";
	params += "directories=0,";
	params += "status=0,";
	params += "menubar=0,";
	var scrollbars = (scrollbars) ? scrollbars : "yes" ;
	params += "scrollbars=" + scrollbars + ",";
	params += "resizable=yes,";
	params +="top=" + itop + ",";
	params +="left=" + ileft + ",";
	params += "width=" + width + ","; // Explorer, Navigator 2,3
	params += "height=" + height + ","; // Explorer, Navigator 2,3	
	params += "innerHeight=" + height + ","; // Navigator 4+
	params += "innerWidth=" + width; // Navigator 4+
	win = window.open(fileName, windowName, params);
	win.focus();
}

function openPopupAtXY (fileName,windowName,width,height,x,y,scrollbars) {
	ileft = x;
	itop = y;
	params = "toolbar=0,";
	params += "location=0,";
	params += "directories=0,";
	params += "status=0,";
	params += "menubar=0,";
	var scrollbars = (scrollbars) ? scrollbars : "yes" ;
	params += "scrollbars=" + scrollbars + ",";
	params += "resizable=yes,";
	params +="top=" + itop + ",";
	params +="left=" + ileft + ",";
	params += "width=" + width + ","; // Explorer, Navigator 2,3
	params += "height=" + height + ","; // Explorer, Navigator 2,3	
	params += "innerHeight=" + height + ","; // Navigator 4+
	params += "innerWidth=" + width; // Navigator 4+
	
	win = window.open(fileName, windowName, params);
	win.focus();
}

function fullscreen () {
		window.moveTo(0,0);
		if (document.all) {
			top.window.resizeTo(screen.availWidth,screen.availHeight);
		}
		else if (document.layers||document.getElementById) {
			if 
			(top.window.outerHeight<screen.availHeight||top.window.outerWidth<screen.availWidth){
				top.window.outerHeight = screen.availHeight;
				top.window.outerWidth = screen.availWidth;
			}
		}
	}

/*
	######################################################################################################
	######################################################################## OBJECT AND STYLES REFERENCING
	######################################################################################################
*/

var NS4 = (navigator.appName.indexOf("Netscape")>=0 && !document.getElementById)? true : false;
var IE4 = (document.all && !document.getElementById)? true : false;
var IE5 = (document.getElementById && document.all)? true : false;
var NS6 = (document.getElementById && navigator.appName.indexOf("Netscape")>=0 )? true: false;
var W3C = (document.getElementById)? true : false;

function getObj (layerName) {
	var layerRef = null;
	layerRef =  (NS4) ? document.layers[layerName] :
				(IE4) ? document.all[layerName] :
				(W3C) ? document.getElementById(layerName) :
				null;
	return layerRef;
}

function getStyle (objName,objRef) {
	var lyrRef = null;
	if (objName != null) {
		lyrRef = getObj(objName);			
	} else {
		lyrRef = objRef;
	}
	return lyrRef ? (NS4 ? lyrRef : lyrRef.style) : null;
}

function tellObj (obj,objName) {
	obj = (obj)  ? obj : getObj(objName);
	alert("object = " + obj);
	oStyle = getStyle('',obj);
	alert("style = " + oStyle);
}

function MM_findObj(n, d) {
	var p,i,x;
	if (!d) d = document;
	if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
		d = parent.frames[n.substring(p + 1)].document;
		n = n.substring(0,p);
	}
	if (!(x = d[n]) && d.all) x = d.all[n];
	for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
	for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n,d.layers[i].document);
  	if (!x && d.getElementById) x = d.getElementById(n);
	return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_showHideLayers() {
	var i,p,v,obj,args = MM_showHideLayers.arguments;
	for (i = 0; i < (args.length - 2); i += 3) {
		if ((obj = MM_findObj(args[i])) != null) {
			v=args[i+2];
			if (obj.style) {
				obj = obj.style;
				v = (v == 'show') ? 'visible' : (v == 'hide') ? 'hidden' : v;
			}
			obj.visibility=v;
		}
	}
}

function switchImage(imgName, imgSrc) {
	var imgObj = MM_findObj(imgName);
	imgObj.src = imgSrc;
}

function getElementsByClassName(classname, node)  {
	if (!node) node = document.getElementsByTagName("body")[0];
	var a = [];
	var re = new RegExp('\\b' + classname + '\\b');
	var els = node.getElementsByTagName("*");
	for(var i=0,j=els.length; i<j; i++)
		if(re.test(els[i].className))a.push(els[i]);
	return a;
}

/*
	######################################################################################################
	############################################################################### OBJECT CONTENT WRITING
	######################################################################################################
*/

function writeObject (obj,content) {	
	obj = getObject(obj);
	if (document.getElementById||document.all)
		obj.innerHTML = content
	else if (document.layers){
		obj.document.write(content)
		obj.document.close()
	}
}

/*
	######################################################################################################
	############################################################################## TABLE.ROWS DISPLAY/HIDE
	######################################################################################################
*/

/*
	## TASKS
		Visualizza/nasconde (secondo bShow) la riga numero rowNum della tabella tableID
	## INS
		bShow - if true show the row, else hide it [true|false]
		tableID - ID of table or tBody
		rowNum - zero-based row number
*/
function tableRowDisplay_byRowNum(bShow, tableID, rowNum) {
	var tbl = MM_findObj(tableID);
	if (tbl && rowNum < tbl.rows.length) {
		tbl.rows[rowNum].style.display = (bShow) ? '' : 'none';
	}
}

/*
	## TASKS
		Visualizza/nasconde (secondo bShow) la riga rowID in qualsiasi tabella (si presuppone ce ne sia una sola di ID = rowID in tutta la pagina) 
	## INS
		bShow - if true show the row, else hide it [true|false]
		rowID - ID of row
*/
function tableRowDisplay_byRowID(bShow, rowID) {
	var row = MM_findObj(rowID);	
	row.style.display = (bShow) ? '' : 'none';  
}


/*
	######################################################################################################
	########################################################################################## CHECK FORMS
	######################################################################################################
*/

function checkEmail (str) {
	var cond1 = (str.indexOf("@") > 0);
	var cond2 = (str.lastIndexOf(".") > str.indexOf("@"));
	return (cond1 && cond2);
}

function checkPassword (str) {
	if (str == undefined || str.length == 0) {
		return false;
	} else {
		return true;
	}
}

/*
	## INS
		friendlyDate
			EX: 23/12/2008 22:30
*/
function checkDate (strDate) {
	
	var isCorrect = true;
	var exitFor = false;
		
	// lunghezza della stringa
	if (strDate.length != 16) return false;
	
	for (i = 0; i < strDate.length; i++) {
	
		var char = strDate.charAt(i);
		
		switch (true) {
				
			// le posizioni con "/"
			case ((i == 2) || (i == 5)):
				var isSlash = (char == "/");
				if (! isSlash) {
					isCorrect = false;
					exitFor = true;
				}
				break;
				
			// la posizione con " "
			case (i == 10):
				var isSpace = (char == " ");
				if (! isSpace) {
					isCorrect = false;
					exitFor = true;
				}
				break;
				
			// la posizione con ":"
			case (i == 13):
				var isDuepunti = (char == ":");
				if (! isDuepunti) {
					isCorrect = false;
					exitFor = true;
				}
				break;
			
			// le posizioni con una cifra
			default:
				if (isNaN(char)) {
					isCorrect = false;
					exitFor = true;
				}
				
		}
		
		if (exitFor) break;
		
	}
	
	return isCorrect;
	
}

/*
	######################################################################################################
	################################################################################################# URLS
	######################################################################################################
*/

/*
	## TASKS
		1) Aggiunge una coppia nome=valore parametro GET ad una stringa URL
			a) Se l'URL non ha nessun GET, lo aggiunge
			b) Se l'URL ha già dei GET, ma non quello =$paramName, lo aggiunge
			c) Se l'URL ha già quel GET =$paramName, sostituisce $paramValue
			
*/
function addGetParamToUrl (iUrl,iParamName,iParamValue) {
	
	var newQueryString,newUrl;
	
	var queryString = document.location.search;
	
	if (queryString) {		
	
		var duplicateParam = (queryString.indexOf(iParamName) != -1);
		
		if (! duplicateParam) {
			
			newUrl = iUrl + "&" + iParamName + "=" + iParamValue;
			
		} else {
			
			newQueryString = "";
			var explode = String(document.location).split("?");
			var firstHalfUrl = explode[0];
			var sep = "&";
			var exploded = queryString.split(sep);
			for (var i = 0; i < exploded.length; i++) {
				var chunk = exploded[i];
				if (chunk.length == 0) {
					continue;
				}
				var s = chunk.indexOf(iParamName);
				if (s != -1) {
					var newChunk = iParamName + "=" + iParamValue;
					newQueryString += newChunk + "&";
				} else {
					newQueryString += chunk + "&";
				}
			}
			newUrl = firstHalfUrl + newQueryString;
		}
		
	} else {
		
		newUrl = iUrl + "?" + iParamName + "=" + iParamValue;
		
	}
	
	return newUrl;
}

/*
	######################################################################################################
	############################################################################################### IMAGES
	######################################################################################################
*/

function image_is_corrupted (imgName,imgObj) {	
	var img;		
	if (imgName != -1) {
		img = document.getElementById(imgName);
	} else {
		img = imgObj;
	}		
	if (img.fileSize == -1 && !img.complete) {
		return true;
	} else {
		return false;
	}
	
}

/*
	######################################################################################################
	############################################################################################## VISUALS
	######################################################################################################
*/

function screen_getObjMidPosX (objWidth) {
	return ((screen.width - objWidth)/2);
}

function screen_getObjMidPosY (objHeight) {
	return ((screen.height - objHeight)/2);
}

/*
	## INS
		objHeight
			L'altezza dell'oggetto (presumibilmente una tabella MAIN, cioè quella che contiene l'intero sito e che sivuole centrare anche sulle Y)
	## TASKS
		Stampa un TR con altezza calcolata in maniera tale da centrare l'oggetto di altezza objHeight sulle Y nello schermo
		NON TIENE CONTO dell'altezza degli strumenti del browser (barra indirizzi, etc)
*/
function visuals_printYcenteringRow (objHeight) {
	var midPosY = screen_getObjMidPosY (objHeight);
	var html = '<tr><td height="' + midPosY + '"></td></tr>';
	document.write(html);
}

/*
	######################################################################################################
	################################################################################################# DATE
	######################################################################################################
*/

function date_getNow () {
	
	var sepDay = "/";
	var sep = " ";
	var sepHour = ":";
	
	var d = new Date();
	var now = date_digitFormat(d.getDate()) + sepDay;
	now += date_digitFormat(d.getMonth()) + sepDay;
	now += d.getFullYear() + sep;
	now += date_digitFormat(d.getHours()) + sepHour;
	now += date_digitFormat(d.getMinutes());
	
	return now;
}

function date_digitFormat (digit) {
	var s = digit.toString();
	if (s.length == 1) s = "0" + s;
	return s;
}

/*
	######################################################################################################
	############################################################################################# SECURITY
	######################################################################################################
*/

function textHasSqlInjection (value) {
	
	var result = false;

	var blockSqlCommands = new Array();
	blockSqlCommands.push("DELETE");
	blockSqlCommands.push("INSERT");
	blockSqlCommands.push("SELECT");
	blockSqlCommands.push("UPDATE");
	blockSqlCommands.push("ALTER");
	blockSqlCommands.push("REVOKE");
	blockSqlCommands.push("CREATE");
	blockSqlCommands.push("DROP");
	blockSqlCommands.push("SHUTDOWN");
	blockSqlCommands.push("RELOAD");
	
	for (var i = 0; i < blockSqlCommands.length; i++) {
		if (value.indexOf(blockSqlCommands[i]) != -1) {
			return true;
		}
	}
	
}

/*
	######################################################################################################
	################################################################################################ FOCUS
	######################################################################################################
*/

function focusLogin (frmName, loginFieldName) {
	var f = MM_findObj(frmName);
	f[loginFieldName].focus();
}

/*
	######################################################################################################
	##################################################################################### PROJECT SPECIFIC
	######################################################################################################
*/

function homeHiliteNewsItem (itemID) {
	var yellowBar = MM_findObj("yn" + itemID);
	var greyBar = MM_findObj("gn" + itemID);
	var textBar = MM_findObj("tn" + itemID);
	var greyBar2 = MM_findObj("g2n" + itemID);
	yellowBar.style.backgroundColor = "#FFD75E";
	greyBar.style.backgroundColor = "#EFEFEF";
	textBar.style.backgroundColor = "#EFEFEF";
	greyBar2.style.backgroundColor = "#EFEFEF";
}

function homeDehiliteNewsItem (itemID) {
	var yellowBar = MM_findObj("yn" + itemID);
	var greyBar = MM_findObj("gn" + itemID);
	var textBar = MM_findObj("tn" + itemID);
	var greyBar2 = MM_findObj("g2n" + itemID);
	yellowBar.style.backgroundColor = "#FFCC33";
	greyBar.style.backgroundColor = "#D1D1D1";
	textBar.style.backgroundColor = "#D1D1D1";
	greyBar2.style.backgroundColor = "#D1D1D1";
}