
/* Get Editor Text */

function getEditorText(editorInstance) {
	var editorDocument = FCKeditorAPI.GetInstance(editorInstance).EditorDocument;
	var editorText = "";
	if(navigator.userAgent.indexOf("MSIE") != -1) {
		var textRange = editorDocument.body.createTextRange();
		editorText = textRange.text;
	} else {
		var range = editorDocument.createRange();
		range.selectNode(editorDocument.body);
		editorText = range.toString();
	}
	return editorText;
}

/* Generate Object */

function AC_Generateobj(objAttrs, params, embedAttrs) {
	var str = "<object ";
	for(var i in objAttrs) {
		str += i + "=\"" + objAttrs[i] + "\" ";
	}
	str += ">";
	for(var i in params) {
		str += "<param name=\"" + i + "\" value=\"" + params[i] + "\" /> ";
	}
	str += "<embed ";
	for(var i in embedAttrs) {
		str += i + "=\"" + embedAttrs[i] + "\" ";
	}
	str += " ></embed></object>";
	document.write(str);
}

function AC_FL_RunContent() {
	var ret = AC_GetArgs(arguments, "movie", "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000", "application/x-shockwave-flash");
	AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

function AC_GetArgs(args, srcParamName, classid, mimeType) {
	var ret = new Object();
	ret.embedAttrs = new Object();
	ret.params = new Object();
	ret.objAttrs = new Object();
	for(var i = 0; i < args.length; i = (i + 2)) {
		var currArg = args[i].toLowerCase();
		switch(currArg) {
			case "classid":
				break;
			case "pluginspage":
				ret.embedAttrs[args[i]] = args[i+1];
				break;
			case "src":
			case "movie":	
				ret.embedAttrs["src"] = args[i+1];
				ret.params[srcParamName] = args[i+1];
				break;
			case "onafterupdate":
			case "onbeforeupdate":
			case "onblur":
			case "oncellchange":
			case "onclick":
			case "ondblClick":
			case "ondrag":
			case "ondragend":
			case "ondragenter":
			case "ondragleave":
			case "ondragover":
			case "ondrop":
			case "onfinish":
			case "onfocus":
			case "onhelp":
			case "onmousedown":
			case "onmouseup":
			case "onmouseover":
			case "onmousemove":
			case "onmouseout":
			case "onkeypress":
			case "onkeydown":
			case "onkeyup":
			case "onload":
			case "onlosecapture":
			case "onpropertychange":
			case "onreadystatechange":
			case "onrowsdelete":
			case "onrowenter":
			case "onrowexit":
			case "onrowsinserted":
			case "onstart":
			case "onscroll":
			case "onbeforeeditfocus":
			case "onactivate":
			case "onbeforedeactivate":
			case "ondeactivate":
			case "type":
			case "codebase":
				ret.objAttrs[args[i]] = args[i+1];
				break;
			case "width":
			case "height":
			case "align":
			case "vspace": 
			case "hspace":
			case "class":
			case "title":
			case "accesskey":
			case "name":
			case "id":
			case "tabindex":
				ret.embedAttrs[args[i]] = ret.objAttrs[args[i]] = args[i+1];
				break;
			default:
				ret.embedAttrs[args[i]] = ret.params[args[i]] = args[i+1];
		}
	}
	ret.objAttrs["classid"] = classid;
	if(mimeType) {
		ret.embedAttrs["type"] = mimeType;
	}
	return ret;
}

/* Find object */

function findObject(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 = findObject(n, d.layers[i].document);
	}
	if(!x && d.getElementById) {
		x = d.getElementById(n);
	}	
	return x;
}

/* Validate Form */

function validateForm() {
	var i, field, fieldLabel, fieldValue, test, index, number, min, max, errors = '', args = validateForm.arguments;
	for(i = 0; i < (args.length - 2); i += 3) {
		field = findObject(args[i]);
		fieldLabel = args[i + 1];
		test = args[i + 2];
		if(field) {
			fieldValue = field.value;
			if(fieldValue != "") {
				if(test.indexOf('isEmail') != -1) {
					index = fieldValue.indexOf('@');
					if(index < 1 || index == (fieldValue.length - 1)) {
						errors+='\r\n - Il valore del campo \"' + fieldLabel + '\" non corrisponde ad un indirizzo e-mail valido.';
					}
				} else if(test != 'R') {
					number = parseFloat(fieldValue);
					if(isNaN(fieldValue)) errors += '\r\n - Il valore del campo \"' + fieldLabel + '\" deve essere un numero.';
					if(test.indexOf('inRange') != -1) {
						index = test.indexOf(':');
						min = test.substring(8, index);
						max = test.substring(index + 1);
						if(number < min || max < number) {
							errors += '\r\n - Il valore del campo \"' + fieldLabel + '\" deve essere un numero compreso tra ' + min + ' e ' + max + '.';
						}
					}
					
				}
			} else if(test.charAt(0) == 'R') {
				errors += '\r\n - Compilare il campo \"' + fieldLabel + '\".';
			}
		}
	}
	if(errors) {
		alert('Impossibile proseguire, si sono verificati i seguenti problemi:' + errors);
	}
	document.returnValue = (errors == '');
}

/* Get window state */

function getWindowState() {

	var global = this;
	var lastSt, lastSl, lastIW, lastIH, bodyRank = null, docElRank = null;
	var twoTestCount = 0;

	var readScroll = {scrollLeft:NaN, scrollTop:NaN};
	var readSizeC = {clientWidth:NaN, clientHeight:NaN};
	var readSizeI = {innerWidth:NaN, innerHeight:NaN};

	var readScrollX = 'scrollLeft';
	var readScrollY = 'scrollTop';

	var theInterface = {
		getScrollX:getScrlXMain,
		getScrollY:getScrlYMain,
		getWidth:function() { return initWidthHeight('getWidth'); },
		getHeight:function() { return initWidthHeight('getHeight'); }
    };

	function getScrlXTest() {
		hasChangedScroll();
		return getScrlXMain();
	}

	function getScrlYTest() {
		hasChangedScroll();
		return lastSl;
	}

	function getScrlXMain() {
		return readScroll[readScrollX];
	}

	function getScrlYMain() {
		return readScroll[readScrollY];
	}

	function getWidthI() {
		return readSizeI.innerWidth;
	}

	function getHeightI() {
		return readSizeI.innerHeight;
	}

	function getWidthC() {
		return readSizeC.clientWidth;
	}

	function getHeightC() {
		return readSizeC.clientHeight;
	}

	function getHeightSmart() {
		return (hasChangedSize() ? theInterface.getHeight : getHeightC)();
	}

	function getWidthSmart() {
		return (hasChangedSize() ? theInterface.getWidth : getWidthC)();
    }

	function setInnerWH() {
		theInterface.getWidth = getWidthI;
		theInterface.getHeight = getHeightI;
	}

	function setMinimumScroll() {
		theInterface.getScrollX = getScrlXMain;
		theInterface.getScrollY = getScrlYMain;
	}

	function hasChangedSize() {
		if((lastIH != (lastIH = getHeightI())) || (lastIW != (lastIW = getWidthI()))) {
			threeObjectTest();
			return true;
		}
		return false;
    }

	function hasChangedScroll() {
		if((lastSl != (lastSl = getScrlYMain())) || (lastSt != (lastSt = getScrlXMain()))) {
			threeObjectTest();
		}
	}

	function rankObj_inner(testObj, rankObj) {
		var dv,dh;
		if(((dv = (global.innerHeight - testObj.clientHeight)) >= 0) && ((dh = (global.innerWidth - testObj.clientWidth)) >= 0) && (!(getScrlXMain() && !dv)) && (!(getScrlYMain() && !dh)) && (!((dh && (rankObj.hDiff || (rankObj.hDiff = dh)) && (dh != rankObj.hDiff)) || (dv && (rankObj.vDiff || (rankObj.vDiff = dv)) && (dv != rankObj.vDiff))))) {
			if(dh == dv) {
				rankObj.rank = Number(Boolean(dh));
			} else if((dh&&!dv)||(dv&&!dh)) {
				rankObj.rank = (dh+dv);
            } else {
				rankObj.rank = NaN;
			}
        } else {
			rankObj.rank = NaN;
		}
		return rankObj;
    }

	function propsAreNumbers(testObj) {
		for(var c = arguments.length; --c;) {
			if(typeof testObj[arguments[c]] != 'number') {
				return false;
			}
		}
		return true;
    }

	function rankObj(testObj, rankObj) {
		if(testObj && propsAreNumbers(testObj, 'clientWidth','clientHeight')) {
			rankObj_inner(testObj, rankObj);
		}
		return rankObj;
	}

	function initWidthHeight(callOn) {
		lastIW = getWidthI();
		lastIH = getHeightI();
		lastSt = getScrlYMain();
		lastSl = getScrlXMain();
		bodyRank = {vDiff:0, hDiff:0, rank:NaN};
		docElRank = {vDiff:0, hDiff:0, rank:NaN};
		theInterface.getWidth = getWidthSmart;
		theInterface.getHeight = getHeightSmart;
		theInterface.getScrollX = getScrlXTest;
		theInterface.getScrollY = getScrlYTest;
		threeObjectTest();
		rankObj = rankObj_inner;
		return theInterface[callOn]();
	} 

	function threeObjectTest() {
		var bodyNaN = isNaN(rankObj(document.body, bodyRank).rank);
		var docElNaN = isNaN(rankObj(document.documentElement, docElRank).rank);
		if(bodyNaN || docElNaN) {
			if(bodyNaN && docElNaN) {
				setMinimumScroll();
				setInnerWH();
			} else {
				readSizeC = ((docElNaN) ? document.body : document.documentElement);
				bodyRank = ((docElNaN) ? bodyRank : docElRank);
				docElRank = null;
				threeObjectTest = twoObjectTest;
            }
		} else {
			readSizeC = ((bodyRank.rank < docElRank.rank)? document.body : document.documentElement);
		}
	}

	function twoObjectTest() {
		if(isNaN(rankObj(readSizeC, bodyRank).rank)) {
			bodyRank = null;
			setMinimumScroll();
			setInnerWH();
		} else if((bodyRank.vDiff) && (bodyRank.hDiff) && (++twoTestCount > 2)) { 
			bodyRank = null;
			theInterface.getWidth = getWidthC;
			theInterface.getHeight = getHeightC;
			setMinimumScroll();
		}
	}

	if(!propsAreNumbers(global, 'innerHeight', 'innerWidth')) {
		readSizeC = compatModeTest(readSizeC);
		theInterface.getWidth = getWidthC;
		theInterface.getHeight = getHeightC;
    } else {
		readSizeI = global;
	}

	if(propsAreNumbers(global, 'pageYOffset', 'pageXOffset')) {
		readScroll = global;
		readScrollY = 'pageYOffset';
		readScrollX = 'pageXOffset';
	} else {
		readScroll = compatModeTest(readScroll);
	}

	return (getWindowState = function() {return theInterface;})();

} 

function compatModeTest(obj) {
	if((document.compatMode) && (document.compatMode.indexOf('CSS') != -1) && (document.documentElement)) {
		return (compatModeTest = function() { return document.documentElement; })((obj = null));
	} else if(document.body) {
		return (compatModeTest = function() { return document.body; })((obj = null));
	} else {
		return obj;
	}
}
