function supGetElementById(){
	if (document.getElementById) {
		return true;
	}
	 if (document.all) {
	 	return true;
	 }
	 if(document.layers){
	 	return true;
	 }
	 return false;
}

function xoopsGetElementById(id){
	if (document.getElementById) {
		return (document.getElementById(id));
	} else if (document.all) {
		return (document.all[id]);
	} else {
		if ((navigator.appname.indexOf("Netscape") != -1) && parseInt(navigator.appversion == 4)) {
			return (document.layers[id]);
		}
	}
}

function xoopsSetElementProp(name, prop, val) {
	var elt=xoopsGetElementById(name);
	if (elt) elt[prop]=val;
}

function xoopsSetElementStyle(name, prop, val) {
	var elt=xoopsGetElementById(name);
	if (elt && elt.style) elt.style[prop]=val;
}

function xoopsGetFormElement(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 setElementColor(id, color){
	xoopsGetElementById(id).style.color = "#" + color;
}

function setElementFont(id, font){
	xoopsGetElementById(id).style.fontFamily = font;
}

function setElementSize(id, size){
	if(size.indexOf("%")==-1){
		xoopsGetElementById(id).style.fontSize = "";
		xoopsGetElementById(id).className = size;
	}
	else{
		xoopsGetElementById(id).style.fontSize = size;
		xoopsGetElementById(id).className = "";
	}
}

function changeDisplay(id){
	var elestyle = xoopsGetElementById(id).style;
	if (elestyle.display == "") {
		elestyle.display = "none";
	} else {
		elestyle.display = "block";
	}
}

function setVisibleDisplay(id){
	xoopsGetElementById(id).style.display = "";
}

function setHiddenDisplay(id){
	xoopsGetElementById(id).style.display = "none";
}

function setVisible(id){
	xoopsGetElementById(id).style.visibility = "visible";
}

function setHidden(id){
	xoopsGetElementById(id).style.visibility = "hidden";
}

function makeBold(id){
	var eleStyle = xoopsGetElementById(id).style;
	if (eleStyle.fontWeight != "bold" && eleStyle.fontWeight != "700") {
		eleStyle.fontWeight = "bold";
	} else {
		eleStyle.fontWeight = "normal";
	}
}

function makeItalic(id){
	var eleStyle = xoopsGetElementById(id).style;
	if (eleStyle.fontStyle != "italic") {
		eleStyle.fontStyle = "italic";
	} else {
		eleStyle.fontStyle = "normal";
	}
}

function makeUnderline(id){
	var eleStyle = xoopsGetElementById(id).style;
	if (eleStyle.textDecoration != "underline") {
		eleStyle.textDecoration = "underline";
	} else {
		eleStyle.textDecoration = "none";
	}
}

function makeLineThrough(id){
	var eleStyle = xoopsGetElementById(id).style;
	if (eleStyle.textDecoration != "line-through") {
		eleStyle.textDecoration = "line-through";
	} else {
		eleStyle.textDecoration = "none";
	}
}

function makeAlign(id,align){
	var eleStyle = xoopsGetElementById(id).style;
	if (eleStyle.textAlign != align) {
		eleStyle.textAlign = align;
	} else {
		eleStyle.textAlign = "justify";
	}
}

function appendSelectOption(selectMenuId, optionName, optionValue){
	var selectMenu = xoopsGetElementById(selectMenuId);
	var newoption = new Option(optionName, optionValue);
	selectMenu.options[selectMenu.length] = newoption;
	selectMenu.options[selectMenu.length].selected = true;
}

function getSelectedOptionText(select){
	var id=select.value;
	var options=select['options'];
	var text="";
	for(var i=0;i<options.length;i++){
		if(options[i].value==id){
			text=trim(options[i].text);
			break;
		}
	}
	return text;
}

function disableElement(target){
	var targetDom = xoopsGetElementById(target);
	if (targetDom.disabled != true) {
		targetDom.disabled = true;
	} else {
		targetDom.disabled = false;
	}
}
function xoopsCheckAll(formname, switchid) {
	var ele = document.forms[formname].elements;
	var switch_cbox = xoopsGetElementById(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 xoopsCheckGroup(formname, switchid, groupid) {
	var ele = document.forms[formname].elements;
	var switch_cbox = xoopsGetElementById(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 xoopsCheckAllElements(elementIds, switchId) {
	var switch_cbox = xoopsGetElementById(switchId);
	for (var i = 0; i < elementIds.length; i++) {
		var e = xoopsGetElementById(elementIds[i]);
		if ((e.name != switch_cbox.name) && (e.type == 'checkbox')) {
			e.checked = switch_cbox.checked;
		}
	}
}

function xoopsSavePosition(id)
{
	var textareaDom = xoopsGetElementById(id);
	if (textareaDom.createTextRange) {
		textareaDom.caretPos = document.selection.createRange().duplicate();
	}
}

function xoopsInsertText(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 xoopsPageBreak(id){
	var domobj;
	if(domobj=xoopsGetElementById(id)){
		xoopsInsertText(domobj,'\n[pagebreak]\n');
	}
}

function xoopsNewPart(id){
	var domobj;
	if(domobj=xoopsGetElementById(id)){
		xoopsInsertText(domobj,'\n[newpart]\n');
	}
}

function xoopsCodeSmilie(id, smilieCode) {
	var revisedMessage;
	var textareaDom = xoopsGetElementById(id);
	xoopsInsertText(textareaDom, smilieCode);
	textareaDom.focus();
	return;
}

function showImgSelected(imgId, selectId, imgDir, extra, xoopsUrl) {
	if (xoopsUrl == null) {
		xoopsUrl = "./";
	}
	imgDom = xoopsGetElementById(imgId);
	selectDom = xoopsGetElementById(selectId);
	imgDom.src = xoopsUrl + "/"+ imgDir + "/" + selectDom.options[selectDom.selectedIndex].value + extra;
}

function xoopsCodeUrl(id, enterUrlPhrase, enterWebsitePhrase){
	if (enterUrlPhrase == null) {
		enterUrlPhrase = "Enter the URL of the link you want to add:";
	}
	var text = prompt(enterUrlPhrase, "");
	var domobj = xoopsGetElementById(id);
	if ( text != null && text != "" ) {
		if (enterWebsitePhrase == null) {
			enterWebsitePhrase = "Enter the web site title:";
		}
				
		var text2 = prompt(enterWebsitePhrase, "");
		if ( text2 != null ) {
			var url, balise;
			
			var tab=text.split(epopUrl);
			if(tab.length>1){
				url=tab[1];
				balise="siteurl";
			}
			else{
				url=tab[0];
				balise="url";
			}
			if ( text2 == "" ) {
				var result = "["+balise+"=" + url + "]" + text + "[/"+balise+"]";
			} else {
				var pos = text2.indexOf(unescape('%00'));
				if(0 < pos){
					text2 = text2.substr(0,pos);
				}
				var result = "["+balise+"=" + url + "]" + text2 + "[/"+balise+"]";
			}
			xoopsInsertText(domobj, result);
		}
	}
	domobj.focus();
}

function xoopsCodeUrlImage(id, enterUrlPhrase){
	if (enterUrlPhrase == null) {
		enterUrlPhrase = "Enter the URL of the link you want to add:";
	}
	var text = prompt(enterUrlPhrase, "");
	var domobj = xoopsGetElementById(id);
	if ( text != null && text != "" ) {
		
		var url, balise;
			
		var tab=text.split(epopUrl);
		if(tab.length>1){
			url=tab[1];
			balise="siteurl";
		}
		else{
			url=tab[0];
			balise="url";
		}
		var result = balise+"="+url;
		domobj.value=result;
	}
	domobj.focus();
}

function xoopsCodeImg(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 = xoopsGetElementById(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]";
		xoopsInsertText(domobj, result);
	}
	domobj.focus();
}

function xoopsCodeEmail(id,msg_text,msg_to,msg_subject,msg_body){
	var domobj = xoopsGetElementById(id);
	var text=prompt(msg_text,"");
	if(text!=null && text!=""){
		var to=prompt(msg_to,"");
		if(to!=null){
			var subject=prompt(msg_subject,"");
			if(subject!=null){
				var body=prompt(msg_body,"");
				if(body!=null){
					var result = "[email";
					if(to!=""){
						result+=" to="+to;
					}
					if(subject!=""){
						result+=" subject="+subject;
					}
					if(body!=""){
						result+=" body="+body;
					}
					result+="]" + text + "[/email]";
					xoopsInsertText(domobj, result);
				}
			}
		}
	}
	domobj.focus();
}

function xoopsCodeQuote(id, enterQuotePhrase){
	if (enterQuotePhrase == null) {
		enterQuotePhrase = "Enter the text that you want to be quoted:";
	}
	var text = prompt(enterQuotePhrase, "");
	var domobj = xoopsGetElementById(id);
	if ( text != null && text != "" ) {
		var pos = text.indexOf(unescape('%00'));
		if(0 < pos){
			text = text.substr(0,pos);
		}
		var result = "[quote]" + text + "[/quote]";
		xoopsInsertText(domobj, result);
	}
	domobj.focus();
}

function xoopsCodeCode(id, enterCodePhrase){
	if (enterCodePhrase == null) {
		enterCodePhrase = "Enter the codes that you want to add.";
	}
	var text = prompt(enterCodePhrase, "");
	var domobj = xoopsGetElementById(id);
	if ( text != null && text != "" ) {
		var result = "[code]" + text + "[/code]";
		xoopsInsertText(domobj, result);
	}
	domobj.focus();
}

function xoopsCodeText(id, hiddentext, enterTextboxPhrase){
	var textareaDom = xoopsGetElementById(id);
	var textDom = xoopsGetElementById(id + "Addtext");
	var fontDom = xoopsGetElementById(id + "Font");
	var colorDom = xoopsGetElementById(id + "Color");
	var sizeDom = xoopsGetElementById(id + "Size");
	var xoopsHiddenTextDomStyle = xoopsGetElementById(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 (xoopsHiddenTextDomStyle.textDecoration == "underline") {
			textDomValue = "[u]" + textDomValue + "[/u]";
			xoopsHiddenTextDomStyle.textDecoration = "none";
		}
		if (xoopsHiddenTextDomStyle.textDecoration == "line-through") {
			textDomValue = "[d]" + textDomValue + "[/d]";
			xoopsHiddenTextDomStyle.textDecoration = "none";
		}
		if (xoopsHiddenTextDomStyle.fontWeight == "bold" || xoopsHiddenTextDomStyle.fontWeight == "700") {
			textDomValue = "[b]" + textDomValue + "[/b]";
			xoopsHiddenTextDomStyle.fontWeight = "normal";
		}
		if (xoopsHiddenTextDomStyle.fontStyle == "italic") {
			textDomValue = "[i]" + textDomValue + "[/i]";
			xoopsHiddenTextDomStyle.fontStyle = "normal";
		}
		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 ((xoopsHiddenTextDomStyle.textAlign != "justify") && (xoopsHiddenTextDomStyle.textAlign != "")) {
			var align=xoopsHiddenTextDomStyle.textAlign;
			textDomValue = "\n["+align+"]" + textDomValue + "[/"+align+"]\n";
			xoopsHiddenTextDomStyle.textAlign = "justify";
		}
		xoopsInsertText(textareaDom, textDomValue);
		textDom.value = "";
		//xoopsHiddenTextDomStyle.color = "#000000";
		xoopsHiddenTextDomStyle.fontFamily = "";
		xoopsHiddenTextDomStyle.fontSize = "100%";
		xoopsGetElementById(hiddentext).className = "medium";
		//xoopsHiddenTextDomStyle.visibility = "hidden";
		textareaDom.focus();
	}
}

function appendCode(addCode) {
	if (targetDom.createTextRange && targetDom.caretPos){
  		var caretPos = targetDom.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) 
== ' ' ? addCode + ' ' : addCode;  
	} else if (targetDom.getSelection && targetDom.caretPos){
		var caretPos = targetDom.caretPos;
		caretPos.text = caretPos.text.charat(caretPos.text.length - 1)  
== ' ' ? addCode + ' ' : addCode;
	} else {
		targetDom.value = targetDom.value + addCode;
  	}
  	if(window.opener){
  		window.opener.focus();
  	}
	window.close();
	return;
}

function xoopsValidate(subjectId, textareaId, submitId, plzCompletePhrase, msgTooLongPhrase, allowedCharPhrase, currCharPhrase) {
	var maxchars = 65535;
	var subjectDom = xoopsGetElementById(subjectId);
	var textareaDom = xoopsGetElementById(textareaId);
	var submitDom = xoopsGetElementById(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;
	}
}

function confirmation(name,text1,text2){
	// on regarde s'il y a zero, une ou plusieurs cases cochées
	var nb=0;
	var nbCoches=0;
	var elt;
	
	while((nb<nbLines) && (nbCoches<2)){
		if(elt=xoopsGetElementById(name+"_"+nb)){
			if(elt.checked){
				nbCoches++;	
			}
		}
		nb++;
	}
	
	if(nbCoches==0){
		return true;	
	}
	else if(nbCoches==1){
		return confirm(text1);
	}
	else{
		return confirm(text2);
	}
}



function popup(	id, lien, widthPopup, heightPopup, xPos, yPos, noPopup) {
			
	if(xPos==null){
		xPos=20;
	}
	
	if(yPos==null){
		yPos=xPos;
	}
	
	
	if(heightPopup>(screen.height-yPos-80)){
		heightPopup=screen.height-yPos-80;					
	}
	
	if(widthPopup>(screen.width-xPos-50)){
		widthPopup=screen.width-xPos-50;					
	}
		
	var string = "height="+heightPopup+", width="+widthPopup+
				", scrollbars=yes, resizable=yes, status=no, menubar=no,location=no,top="+yPos+",left="+xPos;
	
	if(!noPopup){
		lien+="&popup=1";
	}
	var pop = window.open (	lien, 
							id,
							string
						);
	pop.focus();
}

function popup_img(url,id,width,height) {
	
	xPos=100;
	yPos=100;
	
	width=width+18;
	height=height+4;
	
	popup("img"+id,url, width, height, xPos, yPos, true,false,false);

}

function affecteLabel (id,text,label,hidden_id,hidden_label){
	if(opener && opener.location){
		if(label_span=opener.xoopsGetElementById(label)){
			if(label_span.innerHTML=text){
				opener.xoopsGetElementById(hidden_id).value=id;
				opener.xoopsGetElementById(hidden_label).value=text;
				opener.focus();
				self.close();
				return false;
			}
		}
	}
	return true;
}

function setOppenerFieldValue(field_id,field_value){
	if(opener && opener.location){
		opener.xoopsGetElementById(field_id).value=field_value;
		opener.focus();
		self.close();
		return false;
	}
	return true;
}

function setOppenerFieldValues(fields){
	if(opener && opener.location){
		for (var i=0; i<fields.length; i++){
			opener.xoopsGetElementById(fields[0]).value=fields[1];
		}
		opener.focus();
		self.close();
		return false;
	}
	return true;
}

function noSubmitEnter(myform){
	var form=xoopsGetElementById(myform);
	var elt;
	for(var i=0;i<form.elements.length;i++){
		elt=form.elements[i];
		
		if((elt.type=="text") || (elt.type=="password")){
			elt.onkeypress=noEnter;
		}
	}
}

function noEnter(e) {
	var mykeyCode=0;
	if(window.event && (mykeyCode=window.event.keyCode)){
      	mykeyCode=window.event.keyCode;
	}
   	else if(e){
      	mykeyCode=e.which;
   	} 
  
   	if(mykeyCode==13){
		return false;
	}
   	return true;
}


function openerReload(url,not_force_close){
	var ok=false;
	if(opener && opener.location){
		if(url==null){
			var url=opener.location.href;
			// remplacement du # pour forcer le reload
			url=url.replace(/#/,"&#",url);
		}
		opener.location.href=url;
		opener.focus();
		ok=true;
	}
	if(!not_force_close || ok){
		self.close();
		return true;
	}
	return false;
}

function popupClose(){
	if(opener&&opener.location){
		opener.focus();
	};
	self.close();
	return true;
}

function check_all(bool,form_id,ids_name){
	var form;
	var elt;
	if(form=xoopsGetElementById(form_id)){
		for(var i=0;i<form.elements.length;i++){
			elt=form.elements[i];
			if(elt.type=="checkbox"){
				for(var j=0;j<ids_name.length;j++){
					if(elt.id.indexOf(ids_name[j])!=-1){
						elt.checked=bool;
						break;
					}
				}
			}
		}
	}
}

function add_favorites () {
	var siteURL = document.location;
   	var siteNOM = document.title;
       
    if (window.external) {
    	window.external.AddFavorite(siteURL,siteNOM);
  	}
    else if (window.sidebar) {
      	window.sidebar.addPanel(siteNOM,siteURL,"");
  	}
}


// Removes leading whitespaces
function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

// Removes leading and ending whitespaces
function trim( value ) {
	return LTrim(RTrim(value));	
}


