var null_element;
function spsGetElementById(id) {
	if (document.getElementById(id)) {
		return document.getElementById(id);
	} else if (document.all) {
		return document.all[id];
	} else if (document.layers && document.layers[id]) {
		return (document.layers[id]);
	} else {
		return null_element;
	}
}

function spsSetElementProp(name, prop, val) {
	var elt=spsGetElementById(name);
	if (elt) elt[prop]=val;
}

function spsSetElementStyle(name, prop, val) {
	var elt=spsGetElementById(name);
	if (elt && elt.style) elt.style[prop]=val;
}

function spsGetFormElement(fname, ctlname) {
	var frm=document.forms[fname];
	return frm?frm.elements[ctlname]:null;
}

function justReturn() {
	return;
}

function openWithSelfMain(url,name,width,height) {
	var options = "width=" + width + ",height=" + height + "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no";

	new_window = window.open(url, name, options);
	window.self.name = "main";
	new_window.focus();
}

function openCentered(url,name,width,height){
	var WW = width;
	var HH = height;
	var TT = screen.height / 2 - HH / 2;
	var LL = screen.width / 2 - WW / 2;
	var opts = 'width='+WW+',height='+HH+',top='+TT+',left='+LL+',innerWidth='+WW+',innerHeight='+HH+',status=no,scrollbars=yes,resizable=no,menubar=no,copyhistory=no,toolbar=no,location=no,directories=no';
	var new_window = window.open(url,name,opts);
	new_window.focus();
	
	if( typeof( new_window.innerWidth ) == 'number' ) {
		//Non-IE
		new_window.innerWidth = WW;
		new_window.innerHeight = HH;
	} else if( new_window.document.documentElement && ( new_window.document.documentElement.clientWidth || new_window.document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		new_window.document.documentElement.clientWidth = WW;
		new_window.document.documentElement.clientHeight = HH;
	} else if( new_window.document.body && ( new_window.document.body.clientWidth || new_window.document.body.clientHeight ) ) {
		//IE 4 compatible
		new_window.document.body.clientWidth = WW;
		new_window.document.body.clientHeight = HH;
	}
}

function setElementColor(id, color){
	spsGetElementById(id).style.color = "#" + color;
}

function setElementFont(id, font){
	spsGetElementById(id).style.fontFamily = font;
}

function setElementSize(id, size){
	spsGetElementById(id).style.fontSize = size;
}

function changeDisplay(id){
	var elestyle = spsGetElementById(id).style;
	if (elestyle.display == "") {
		elestyle.display = "none";
	} else {
		elestyle.display = "block";
	}
}

function setVisible(id){
	spsGetElementById(id).style.visibility = "visible";
}

function setHidden(id){
	spsGetElementById(id).style.visibility = "hidden";
}

function makeBold(id){
	var eleStyle = spsGetElementById(id).style;
	if (eleStyle.fontWeight != "bold" && eleStyle.fontWeight != "700") {
		eleStyle.fontWeight = "bold";
	} else {
		eleStyle.fontWeight = "normal";
	}
}

function makeItalic(id){
	var eleStyle = spsGetElementById(id).style;
	if (eleStyle.fontStyle != "italic") {
		eleStyle.fontStyle = "italic";
	} else {
		eleStyle.fontStyle = "normal";
	}
}

function makeUnderline(id){
	var eleStyle = spsGetElementById(id).style;
	if (eleStyle.textDecoration != "underline") {
		eleStyle.textDecoration = "underline";
	} else {
		eleStyle.textDecoration = "none";
	}
}

function makeLineThrough(id){
	var eleStyle = spsGetElementById(id).style;
	if (eleStyle.textDecoration != "line-through") {
		eleStyle.textDecoration = "line-through";
	} else {
		eleStyle.textDecoration = "none";
	}
}

function appendSelectOption(selectMenuId, optionName, optionValue){
	var selectMenu = spsGetElementById(selectMenuId);
	var newoption = new Option(optionName, optionValue);
	selectMenu.options[selectMenu.length] = newoption;
	selectMenu.options[selectMenu.length].selected = true;
}

function disableElement(target){
	var targetDom = spsGetElementById(target);
	if (targetDom.disabled != true) {
		targetDom.disabled = true;
	} else {
		targetDom.disabled = false;
	}
}
function spsCheckAll(formname, switchid) {
	var ele = document.forms[formname].elements;
	var switch_cbox = spsGetElementById(switchid);
	for (var i = 0; i < ele.length; i++) {
		var e = ele[i];
		if ( (e.name != switch_cbox.name) && (e.type == 'checkbox') ) {
			e.checked = switch_cbox.checked;
		}
	}
}

function spsCheckGroup(formname, switchid, groupid) {
	var ele = document.forms[formname].elements;
	var switch_cbox = spsGetElementById(switchid);
	for (var i = 0; i < ele.length; i++) {
		var e = ele[i];
		if ( (e.type == 'checkbox') && (e.id == groupid) ) {
			e.checked = switch_cbox.checked;
			e.click(); e.click();  // Click to activate subgroups
									// Twice so we don't reverse effect
		}
	}
}

function spsCheckAllElements(elementIds, switchId) {
	var switch_cbox = spsGetElementById(switchId);
	for (var i = 0; i < elementIds.length; i++) {
		var e = spsGetElementById(elementIds[i]);
		if ((e.name != switch_cbox.name) && (e.type == 'checkbox')) {
			e.checked = switch_cbox.checked;
		}
	}
}

function spsSavePosition(id)
{
	var textareaDom = spsGetElementById(id);
	if (textareaDom.createTextRange) {
		textareaDom.caretPos = document.selection.createRange().duplicate();
	}
}

function spsInsertText(domobj, text)
{
	if (domobj.createTextRange && domobj.caretPos){
  		var caretPos = domobj.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) 
== ' ' ? text + ' ' : text;  
	} else if (domobj.getSelection && domobj.caretPos){
		var caretPos = domobj.caretPos;
		caretPos.text = caretPos.text.charat(caretPos.text.length - 1)  
== ' ' ? text + ' ' : text;
	} else {
		domobj.value = domobj.value + text;
  	}
}

function spsCodeSmilie(id, smilieCode) {
	var revisedMessage;
	var textareaDom = spsGetElementById(id);
	spsInsertText(textareaDom, smilieCode);
        //textareaDom.focus();
	return;
}

function showImgSelected(imgId, selectId, imgDir, extra, spsUrl) {
	if (spsUrl == null) {
		spsUrl = "./";
	}
	imgDom = spsGetElementById(imgId);
	selectDom = spsGetElementById(selectId);
	imgDom.src = spsUrl + "/"+ imgDir + "/" + selectDom.options[selectDom.selectedIndex].value + extra;
}

function spsTextAlign(id, align) {
	var result;
	var domobj = spsGetElementById(id);
	switch(align) {
	default:
	case 0:
		result = "[left][/left]";
		break;
	case 1:
		result = "[center][/center]";
		break;
	case 2:
		result = "[right][/right]";
		break;
	}
	spsInsertText(domobj, result);
        //domobj.focus();
}

function spsCodeUrl(id, fSiteUrl, enterUrlPhrase, enterWebsitePhrase){
	if (enterUrlPhrase == null) {
		enterUrlPhrase = "Enter the URL of the link you want to add:";
	}
	var text = prompt(enterUrlPhrase, "");
	var domobj = spsGetElementById(id);
	if ( text != null && text != "" ) {
		if (enterWebsitePhrase == null) {
			enterWebsitePhrase = "Enter the web site title:";
		}
		var text2 = prompt(enterWebsitePhrase, "");
		if ( text2 != null ) {
			if ( text2 == "" ) {
				var result = (fSiteUrl)?"[siteurl=" + text + "]" + text + "[/siteurl]":"[url=" + text + "]" + text + "[/url]";
			} else {
				var pos = text2.indexOf(unescape('%00'));
				if(0 < pos){
					text2 = text2.substr(0,pos);
				}
				var result = (fSiteUrl)?"[siteurl=" + text + "]" + text2 + "[/siteurl]":"[url=" + text + "]" + text2 + "[/url]";
			}
			spsInsertText(domobj, result);
		}
	}
        //domobj.focus();
}

function spsCodeImg(id, enterImgUrlPhrase, enterImgPosPhrase, imgPosRorLPhrase, errorImgPosPhrase){
	if (enterImgUrlPhrase == null) {
		enterImgUrlPhrase = "Enter the URL of the image you want to add:";
	}
	var text = prompt(enterImgUrlPhrase, "");
	var domobj = spsGetElementById(id);
	if ( text != null && text != "" ) {
		if (enterImgPosPhrase == null) {
			enterImgPosPhrase = "Now, enter the position of the image.";
		}
		if (imgPosRorLPhrase == null) {
			imgPosRorLPhrase = "'R' or 'r' for right, 'L' or 'l' for left, or leave it blank.";
		}
		if (errorImgPosPhrase == null) {
			errorImgPosPhrase = "ERROR! Enter the position of the image:";
		}
		var text2 = prompt(enterImgPosPhrase + "\n" + imgPosRorLPhrase, "");
		while ( ( text2 != "" ) && ( text2 != "r" ) && ( text2 != "R" ) && ( text2 != "l" ) && ( text2 != "L" ) && ( text2 != null ) ) {
			text2 = prompt(errorImgPosPhrase + "\n" + imgPosRorLPhrase,"");
		}
		if ( text2 == "l" || text2 == "L" ) {
			text2 = " align=left";
		} else if ( text2 == "r" || text2 == "R" ) {
			text2 = " align=right";
		} else {
			text2 = "";
		}
		var result = "[img" + text2 + "]" + text + "[/img]";
		spsInsertText(domobj, result);
	}
        //domobj.focus();
}

function spsCodeEmail(id, enterEmailPhrase){
	if (enterEmailPhrase == null) {
		enterEmailPhrase = "Enter the email address you want to add:";
	}
	var text = prompt(enterEmailPhrase, "");
	var domobj = spsGetElementById(id);
	if ( text != null && text != "" ) {
		var result = "[email]" + text + "[/email]";
		spsInsertText(domobj, result);
	}
        //domobj.focus();
}

function spsCodeQuote(id, enterQuotePhrase){
	if (enterQuotePhrase == null) {
		enterQuotePhrase = "Enter the text that you want to be quoted:";
	}
	var text = prompt(enterQuotePhrase, "");
	var domobj = spsGetElementById(id);
	if ( text != null && text != "" ) {
		var pos = text.indexOf(unescape('%00'));
		if(0 < pos){
			text = text.substr(0,pos);
		}
		var result = "[quote]" + text + "[/quote]";
		spsInsertText(domobj, result);
	}
        //domobj.focus();
}

function spsCodeCode(id, enterCodePhrase){
	if (enterCodePhrase == null) {
		enterCodePhrase = "Enter the codes that you want to add.";
	}
	var text = prompt(enterCodePhrase, "");
	var domobj = spsGetElementById(id);
	if ( text != null && text != "" ) {
		var result = "[code]" + text + "[/code]";
		spsInsertText(domobj, result);
	}
        //domobj.focus();
}

function spsCodeText(id, hiddentext, enterTextboxPhrase){
	var textareaDom = spsGetElementById(id);
	var textDom = spsGetElementById(id + "Addtext");
	var fontDom = spsGetElementById(id + "Font");
	var colorDom = spsGetElementById(id + "Color");
	var sizeDom = spsGetElementById(id + "Size");
	var spsHiddenTextDomStyle = spsGetElementById(hiddentext).style;
	var textDomValue = textDom.value;
	var fontDomValue = fontDom.options[fontDom.options.selectedIndex].value;
	var colorDomValue = colorDom.options[colorDom.options.selectedIndex].value;
	var sizeDomValue = sizeDom.options[sizeDom.options.selectedIndex].value;
	if ( textDomValue == "" ) {
		if (enterTextboxPhrase == null) {
			enterTextboxPhrase = "Please input text into the textbox.";
		}
		alert(enterTextboxPhrase);
                //textDom.focus();
	} else {
		if ( fontDomValue != "FONT") {
			textDomValue = "[font=" + fontDomValue + "]" + textDomValue + "[/font]";
			fontDom.options[0].selected = true;
		}
		if ( colorDomValue != "COLOR") {
			textDomValue = "[color=" + colorDomValue + "]" + textDomValue + "[/color]";
			colorDom.options[0].selected = true;
		}
		if ( sizeDomValue != "SIZE") {
			textDomValue = "[size=" + sizeDomValue + "]" + textDomValue + "[/size]";
			sizeDom.options[0].selected = true;
		}
		if (spsHiddenTextDomStyle.fontWeight == "bold" || spsHiddenTextDomStyle.fontWeight == "700") {
			textDomValue = "[b]" + textDomValue + "[/b]";
			spsHiddenTextDomStyle.fontWeight = "normal";
		}
		if (spsHiddenTextDomStyle.fontStyle == "italic") {
			textDomValue = "[i]" + textDomValue + "[/i]";
			spsHiddenTextDomStyle.fontStyle = "normal";
		}
		if (spsHiddenTextDomStyle.textDecoration == "underline") {
			textDomValue = "[u]" + textDomValue + "[/u]";
			spsHiddenTextDomStyle.textDecoration = "none";
		}
		if (spsHiddenTextDomStyle.textDecoration == "line-through") {
			textDomValue = "[d]" + textDomValue + "[/d]";
			spsHiddenTextDomStyle.textDecoration = "none";
		}
		spsInsertText(textareaDom, textDomValue);
		textDom.value = "";
		spsHiddenTextDomStyle.color = "#000000";
		spsHiddenTextDomStyle.fontFamily = "";
		spsHiddenTextDomStyle.fontSize = "12px";
		spsHiddenTextDomStyle.visibility = "hidden";
                //textareaDom.focus();
	}
}

function spsValidate(subjectId, textareaId, submitId, plzCompletePhrase, msgTooLongPhrase, allowedCharPhrase, currCharPhrase) {
	var maxchars = 65535;
	var subjectDom = spsGetElementById(subjectId);
	var textareaDom = spsGetElementById(textareaId);
	var submitDom = spsGetElementById(submitId);
	if (textareaDom.value == "" || subjectDom.value == "") {
		if (plzCompletePhrase == null) {
			plzCompletePhrase = "Please complete the subject and message fields.";
		}
		alert(plzCompletePhrase);
		return false;
	}
	if (maxchars != 0) {
		if (textareaDom.value.length > maxchars) {
			if (msgTooLongPhrase == null) {
				msgTooLongPhrase = "Your message is too long.";
			}
			if (allowedCharPhrase == null) {
				allowedCharPhrase = "Allowed max chars length: ";
			}
			if (currCharPhrase == null) {
				currCharPhrase = "Current chars length: ";
			}
			alert(msgTooLongPhrase + "\n\n" + allowedCharPhrase + maxchars + "\n" + currCharPhrase + textareaDom.value.length + "");
			textareaDom.focus();
			return false;
		} else {
			submitDom.disabled = true;
			return true;
		}
	} else {
		submitDom.disabled = true;
		return true;
	}
}
/*
//---- Progress Bar
var gPbInterval;
var gPbWidth;
var gCursorWidth;
var gCursorPos;
var gCursorSpeed;

function pbstart(imgurl,width,cwidth,speed,fgcol,bgcol,bgcolc,brd,brdcol) {
	var clientW;
	var clientH;
	var objProBar;
	var objProCur;
	var objShadow;
	var pbheight;
	var pbie;

	gCursorSpeed = speed;

	pbie = false;
	if(navigator.appName == "Microsoft Internet Explorer") pbie = true;

	if ( pbie ) 
		document.write("<DIV style=\"position: absolute;overflow: hidden;visibility: hidden; z-index: 100;left: 0px; top: 0px;background-color: #000000;width: "+(width+12)+"px;height: 60px;filter:alpha(opacity=30);\" NAME=\"pbshd\" ID=\"pbshd\"></DIV>");
	else
		document.write("<DIV style=\"position: absolute;overflow: hidden;visibility: hidden; z-index: 100;left: 0px; top: 0px;background-color: #666666;width: "+(width+12)+"px;height: 60px;\" NAME=\"pbshd\" ID=\"pbshd\"></DIV>");

	
	document.write("<DIV style=\"position: absolute;overflow: hidden;visibility: hidden; z-index: 101;left: 0px; top: 0px;width: "+(width+12)+"px;height:60px;\" NAME=\"pbdlg\" ID=\"pbdlg\">");
	document.write("<TABLE CELLSPACING=\"3\" CELLPADDING=\"0\" ALIGN=\"center\" STYLE=\"color: "+fgcol+";background-color: "+bgcol+";border: "+brd+"px solid "+brdcol+";\">");
	document.write("<TR>");
	document.write("<TD STYLE=\"font-family: Verdana, Arial, Helvetica, sans-serif;font-size: xx-small;font-weight: bold;text-align: center;\">");
	document.write("Caricamento in corso ...");
	document.write("</TD>");
	document.write("</TR>");
	document.write("<TR>");
	document.write("<TD>");
	document.write("<DIV style=\"position: relative;overflow: hidden;border: 1px solid #000000;width: "+width+";background-color: "+bgcolc+";\">");
	document.write("<DIV ID=\"pbcur\" NAME=\"pbcur\" STYLE=\"position: relative;left: 0px;width: "+width+";\">");
	document.write("<IMG SRC=\""+imgurl+"\" WIDTH=\""+cwidth+"\" BORDER=\"0\" ALIGN=\"middle\">");
	document.write("</DIV>");
	document.write("</DIV>");
	document.write("</TD>");
	document.write("</TR>");
	document.write("<TR>");
	document.write("<TD STYLE=\"font-family: Verdana, Arial, Helvetica, sans-serif;font-size: xx-small;font-weight: bold;text-align: center;\">");
	document.write("Attendere prego");
	document.write("</TD>");
	document.write("</TR>");
	document.write("</TABLE>");
	document.write("</DIV>");

	objProBar = spsGetElementById('pbdlg');
	objProCur = spsGetElementById('pbcur');
	objShadow = spsGetElementById('pbshd');

	if ( pbie ) {
		clientW = document.body.clientWidth;
		clientH = document.body.clientHeight;
		gPbWidth = objProBar.offsetWidth;
		pbheight = objProBar.offsetHeight;
		objShadow.style.width = gPbWidth+'px';//objProBar.offsetWidth+'px';
		objShadow.style.height = pbheight+'px';//objProBar.offsetHeight+'px';
	} else {
		clientW = window.innerWidth;
		clientH = window.innerHeight;
		if(objProCur.clip){
			gPbWidth = objProBar.clip.width;
			pbheight = objProBar.clip.height;
			objShadow.style.width = gPbWidth+'px';//objProBar.clip.width+'px';
			objShadow.style.height = pbheight+'px';//objProBar.clip.height+'px';
		}else{
			//gPbWidth = objProBar.offsetWidth;
			gPbWidth = parseInt(objProBar.style.width);//objProCur.offsetHeight;
			pbheight = parseInt(objProBar.style.height);//objProCur.offsetHeight;
			objShadow.style.width = gPbWidth+'px';
			objShadow.style.height = pbheight+'px';//objProBar.offsetHeight+'px';
		}
	}
	gCursorWidth = cwidth;
	gCursorPos = 0;

	objProBar.style.left = ((clientW-gPbWidth) / 2)+'px';
	objProBar.style.top = ((clientH-pbheight) / 2)+'px';
	objShadow.style.left = (((clientW-gPbWidth) / 2) + 5)+'px';
	objShadow.style.top = (((clientH-pbheight) / 2) + 5)+'px';
	objShadow.style.visibility = 'visible';
	objProBar.style.visibility = 'visible';

	gPbInterval = setInterval('pbscroll()', 100 );
}

function pbfinish() {
	clearInterval(gPbInterval);
	spsGetElementById('pbdlg').style.visibility = 'hidden';
	spsGetElementById('pbshd').style.visibility = 'hidden';
}

function pbscroll() {
	gCursorPos = gCursorPos + gCursorSpeed;
	if ( gCursorPos > gPbWidth ) gCursorPos = -(gCursorWidth - 1);
	spsGetElementById('pbcur').style.left = parseInt(gCursorPos);
}
*/
function externalLinks() {
	if (!document.getElementsByTagName) return;

	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		var relvalue = anchor.getAttribute("rel");

		if (anchor.getAttribute("href")) {
			var external = /external/;
			var relvalue = anchor.getAttribute("rel");
			if (external.test(relvalue)) { anchor.target = "_blank"; }
		}
	}
}

