//http://www.dithered.com/javascript/compression/index.html
function wmGetElementId (html_name_id) {
	if(document.getElementById){//browser="Netscape ~=6"
		return document.getElementById(html_name_id);
	}
	if (document.all) {	//browser="IE"
		return document.all[html_name_id];
	}
	return null;
}
function wmGetOrFindElementId (objDoc, html_name_id) {
	if(objDoc==null) objDoc = document.getElementById(html_name_id);
	return (objDoc);
}
function wmSetContent(html_name_id, html_code){
	document.getElementById(html_name_id).innerHTML = html_code;
}

function wmGetContent(html_name_id){
	var html_code = new String(""); 
	var doc = wmGetElementId(html_name_id);
	if(doc) {
		try{
			html_code = doc.innerHTML;
		}catch(Exception) {
			html_code = "";
		}
	}
	return html_code;
}
function wmGetContentText(html_name_id, bWithLineFeed){
	var doc = wmGetElementId(html_name_id);
	if(doc) return wmGetTextForElement(doc, bWithLineFeed);
	return new String("");
}
function wmAppendContent(html_name_id, html_code) {
	document.getElementById(html_name_id).innerHTML = wmGetContent(html_name_id) + html_code;
}
function wmGetTextForElement(obj , bWithLineFeed) {
	var strContent = new String("");
	if(!obj) return strContent;
	var child = obj.childNodes;
	for (var i=0;i < child.length;i++) {
		if (child[i].nodeType==1) {
			if(child[i].nodeName == 'OPTION') {
				if(child[i].getAttribute("selected")) {
					strContent += (child[i].getAttribute("text")) ? child[i].getAttribute("text") : "";
				}
			}else if(child[i].nodeName == 'INPUT') {
				switch(child[i].type) {
					case "hidden" : 
								strContent += "";break;
					default : 
								value = (child[i].getAttribute("value")) ? child[i].getAttribute("value") : "";
								if(value=="") value = ".....";
								strContent += value;
				}
			}else if(child[i].nodeName == 'BR') {
				strContent += (bWithLineFeed==true) ? "<BR>" : "\n";
			}else if(child[i].nodeName == 'P') {
				strContent += ( (bWithLineFeed==true) ? "<P>" : "\n") + wmGetTextForElement(child[i], bWithLineFeed);
			}else{
				strContent+= wmGetTextForElement(child[i], bWithLineFeed);
			}
		}else {
			if (child[i].nodeType==3) {
				strContent += child[i].data ;
			}
		}
	}
	return strContent;
}
function wmSearchByIdOrNameAt (objParent, strLookingFor) {
	var objDoc = null;
	var subDoc = null;
	var child = objParent.childNodes;
	var i , theName, theId, strNameId;
	for(i=0; i<child.length; i++) {
		theName = child[i].name;
		theId = child[i].id;
		if(theId) {
			if(theId == strLookingFor) return child[i];
		}
		if(theName) {
			if(theName == strLookingFor) return child[i];
		}
	}
	for(i=0; i<child.length; i++) {
		subDoc = wmSearchByIdOrNameAt (child[i] , strLookingFor);
		if(subDoc) return subDoc;
	}
	return null;
}
function wmSearchObjectAt (objParent, objSearchFor, bRecurse) {
	var objDoc = null;
	var subDoc = null;
	var child = objParent.childNodes;
	var i , theName, theId, strNameId;
	for(i=0; i<child.length; i++) {
		if(child[i].id == objSearchFor.id) return objSearchFor;
	}
	if(bRecurse==true) {
		for(i=0; i<child.length; i++) {
			subDoc = wmSearchObjectAt (child[i] , objSearchFor, true);
			if(subDoc) return subDoc;
		}
	}
	return null;
}

//belone
//fonction qui change une fiche d'edition en une fiche de visualisation
function wmFormView (objForm, bExcludeButton) {
	if(!objForm) return ;
	var oLayer = null;
	var value = "";
	var obj = null;
	var bChecked = false;
	for(var i = 0; i < objForm.elements.length; i++) {
		obj = objForm.elements[i];
		if(!obj) continue;
		if(obj.type=="hidden") continue;
		if(bExcludeButton && obj.type=="button") continue;
		if(bExcludeButton && obj.type=="submit") continue;
		oLayer = obj.parentNode;
		value = "";
		if(oLayer) {
			value = wmGetTextForElement(oLayer, true);
			if(obj.type == "radio") {
				for(var j = 0; j < oLayer.childNodes.length; j++) {
					if(oLayer.childNodes[j].type == "radio") {
						if(oLayer.childNodes[j].getAttribute("checked")) {
							bChecked = true;
							continue;
						}
					}
					if(bChecked) {
						value = (oLayer.childNodes[j].data) ? oLayer.childNodes[j].data : "&nbsp;";
						bChecked = false;
					}
				}
			}
			if(obj.type == "checkbox") {
				for(var j = 0; j < oLayer.childNodes.length; j++) {
					if(oLayer.childNodes[j].type == "checkbox") {
						if(oLayer.childNodes[j].getAttribute("checked")) {
							bChecked = true;
							continue;
						}
					}
					if(bChecked) {
						value = (oLayer.childNodes[j].data) ? oLayer.childNodes[j].data : "&nbsp;";
						bChecked = false;
					}
				}
			}
			value = replace(value, "\r", "<br>");
			value = replace(value, "\n", "<br>");
			value = replace(value, "\t", "&nbsp;");
			oLayer.removeChild(obj);
			wmReplaceContentInObject(oLayer, value);
			i--;
		}
	}
}

//function wmFormView2
function wmFormView2 (objForm, bExcludeButton, strOptionCaption) {
	if(!objForm) return ;
	var oLayer = null;
	var value = "";
	var obj = null;
	var bChecked = false;
	var strChkRadioCaption =  ((strOptionCaption==null) || (typeof strOptionCaption=='undefined')) ? "caption" : strOptionCaption;
	var j=0;
	while( j < objForm.elements.length) {
		obj = objForm.elements[j];
		if( (obj.type=="hidden") || (obj.type=="file") || (bExcludeButton && obj.type=="button") || (bExcludeButton && obj.type=="submit") ) {
			j=j+1;
			continue;
		}
		value = obj.value;
		var str_type = (new String(obj.type)).toLowerCase();
		if(str_type=="checkbox" || str_type=="radio") {
			value = "";
			if(!obj.checked) {
				var caption = wmGetElementId(strChkRadioCaption+"_"+obj.name+"_"+obj.value);
				if(caption!=null) {
					caption.parentNode.removeChild(caption);
				}
			}
		}else
		if(str_type == 'text' || str_type == 'textarea') {
			value = obj.value;
		}
		else {
			value = wmGetTextForElement(obj, true);
		}
		value = replace(value, "\r", "<br>");
		value = replace(value, "\n", "<br>");
		value = replace(value, "\t", "&nbsp;");
		var new_obj = document.createElement('SPAN');
		new_obj.innerHTML = value;
		obj.parentNode.insertBefore(new_obj, obj);
		obj.parentNode.removeChild(obj);
	}
}
//function wmReplaceContent
function wmReplaceContent(doc, target_html_id, strContent) {
	var e1, e2, e3;
	if( (target_html_id==null) || (target_html_id=="")) return null;
	var location = wmSearchByIdOrNameAt(doc, target_html_id);
	if(location) {
		try{
			location.innerHTML =  strContent;
		}catch(e1){
			try{
				location.innerText =  strContent;
			}catch(e2){
			}
		}
	}
	return location;
}
function wmReplaceContentInObject(objLocation, strContent) {
	var e1, e2, e3;
	if(!objLocation) return null;
	if(objLocation) {
		objLocation.textContent = strContent;
		try{
			objLocation.innerHTML =  strContent;
		}catch(e1){
			try{
				objLocation.innerText =  strContent;
			}catch(e2){
			}
		}
	}
	return objLocation;
}

function wmFormSetValue(form_id, value) {
	var objForm = wmGetElementId(form_id);
	if(!objForm) return false;
	objForm.value = value;
	return true;
}
function wmFormGetValue(form_id, defaultValue) {
	var objForm = wmGetElementId(form_id);
	if(!objForm) return defaultValue;
	return objForm.value;
}
//Not yet
function wmSetAttribute(doc, a_attr, value) {
	var i,j;
	if(doc==null) return;
	doc.innerHTML = value;
}

function wmTextToHtml (strTxt) {
	var res = strTxt;
	res = replace(res, "\n\n", "<P>");
	res = replace(res, "\n", "<BR>");
	res = replace(res, "  ", " &nbsp;");
	return res;
}
function wmHtmlToText (strHtml) {
	var res = strHtml;
	res = replace(res, "<P>","\n\n");
	res = replace(res, "<p>","\n\n");
	res = replace(res, "<BR>", "\n");
	res = replace(res, "<br>", "\n");
	res = replace(res, "&nbsp;", " ");
	return res;
}
function wmEraseComment (strText, strDcom, strEcom) {
	var strBeginComment = (strDcom && strDcom!="") ? strDcom : C_DCOM;
	var strEndComment = (strEcom && strEcom!="") ? strDcom : C_ECOM;
	a_endComment = strText.split(strEndComment);
	var nbEndComment = a_endComment.length;
	var strValid = "";
	var nbBeginComment;
	var a_beginComment;
	if(nbEndComment>0) {
		for(var i=0;i<nbEndComment; i++) {
			strValid = "";
			a_beginComment = a_endComment[i].split(strBeginComment);
			nbBeginComment = a_beginComment.length;
			if(nbBeginComment>1) {
				for(var j=0;j<nbBeginComment-1; j++) {
					strValid += a_beginComment[j];
					if(j<nbBeginComment-2) {
						strValid += strBeginComment;
					}
				}
			}else{
				strValid = a_beginComment[0];
			}
			if(i+1<nbEndComment) {
				a_endComment[i+1] = strValid + a_endComment[i+1];
			}
		}
	}
	return a_endComment[nbEndComment-1];
}
function wmPrompt (strTitle , strDefaultText, bBlankNotAccepted) {
	var res = prompt(strTitle , strDefaultText);
	if(res==null) return null;
	if( (bBlankNotAccepted==true) && (replace(res, " ","")=="") ) {
		return null;
	}
	return res;
}

function wmSetLayerState(html_id, vState) {
	var oLayer;
	var i;
	var strState = vState;
	if((vState==true) || (vState==1) || (vState=="1") ) strState = "block";
	if((vState==false) || (vState==0) || (vState=="0") ) strState = "none";
	//alert(strState);
	if(document.getElementById){//browser="Netscape ~=6"
		oLayer = document.getElementById(html_id);
	}
	if (document.all) {	//browser="IE"
		oLayer = document.all[html_id];
	}
	if (oLayer) {
		if(oLayer.length) {
			for(i=0;i<oLayer.length; i++) oLayer[i].style.display = strState;
		}else{
			oLayer.style.display = strState;
		}
		return oLayer;
	}
	return null;
}

function wmGetLayerState(html_id) {
	var oLayer;
	if(document.getElementById){//browser="Netscape ~=6"
		oLayer = document.getElementById(html_id);
	}
	if (document.all) {	//browser="IE"
		oLayer = document.all[html_id];
	}

	if (oLayer) {
		if(oLayer.length) return oLayer[0].style.display;
		return oLayer.style.display;
	}
	return "block";
}

function wmShowHideLayer(html_id) {
	if(document.getElementById){//browser="Netscape ~=6"
		oLayer = document.getElementById(html_id);
	}
	if (document.all) {	//browser="IE"
		oLayer = document.all[html_id];
	}

	if (oLayer) {
		if(oLayer.length) {
			var strState = (oLayer[0].style.display=="none") ? "block" : "none";
			for(i=0;i<oLayer.length; i++) oLayer[i].style.display = strState;
		}else{
			if(oLayer.style.display=="none") oLayer.style.display = "block";
			else oLayer.style.display = "none";				
		}
	}
}
function wmDoLink (strLink, form_id, strTarget) {
	window.location = strLink;
	return true;
	if(document.getElementById) {
		try {
			var form = wmGetElementId(form_id);
			if(form) {
				form.action = strLink;
				if(strTarget!=null && strTarget!="") form.target = strTarget;
				form.submit();
				return true;
			}
		}catch(error) {}
	}
	window.location.href = strLink;
	return true;
}

var strStatusBarText = '';
function wmPopup(strUrl , strWindowName, iWidth, iHeight, bMenubar, iLeft, iTop) {
	if(iLeft==-1) {
		iLeft = Math.round( (screen.width - iWidth)/2 );
	}
	if(iTop==-1) {
		iTop = Math.round( (screen.height - iHeight)/2 );
	}
	var w = window.open(strUrl, strWindowName, "width=" + iWidth +",height="+ iHeight + "menubar="+ bMenubar +",left=" + iLeft+ ",top="+iTop+",fullscreen=0,directories=0,location=0,resizable=0,scrollbars=1,status=0,toolbar=0");
	w.focus();
	return w;
}
function wmPopupCode(strCode , strWindowName, iWidth, iHeight, bMenubar, iLeft, iTop) {
	if(iLeft==-1) {
		iLeft = Math.round( (screen.width - iWidth)/2 );
	}
	if(iTop==-1) {
		iTop = Math.round( (screen.height - iHeight)/2 );
	}
	var w = window.open("", strWindowName, "width=" + iWidth +",height="+ iHeight + "menubar="+ bMenubar +",left=" + iLeft+ ",top="+iTop+",fullscreen=0,directories=0,location=0,resizable=0,scrollbars=1,status=0,toolbar=0");
	w.document.open();
	w.document.write(strCode);
	w.document.close();
	w.focus();
	return w;
}
function wmDisplayStatus() {
	self.status =strStatusBarText;
	//setTimeout("displayStatus()",1000);
}
function wmShowWindowPopUp(strUrl , strWindowName, iWidth, iHeight, bMenubar, iLeft, iTop) {
	if(iLeft==-1) {
		iLeft = Math.round( (screen.width - iWidth)/2 );
	}
	if(iTop==-1) {
		iTop = Math.round( (screen.height - iHeight)/2 );
	}
	var w = window.open(strUrl, strWindowName, "width=" + iWidth +",height="+ iHeight + "menubar="+ bMenubar +",left=" + iLeft+ ",top="+iTop+",fullscreen=0,directories=0,location=0,resizable=0,scrollbars=1,status=0,toolbar=0");
	w.focus();
}
function wmShowImage(strUrl) {
	var w = window.open(strUrl, 'ImageClick', "menubar=0,fullscreen=0,directories=0,location=0,resizable=0,scrollbars=0,status=0,toolbar=0");
	w.focus();
}
function wmTogglePane(objCurrentOnglet, currentIndex, nb_onglet, strOngletName) {
	var oLayer ;
	wmSetLayerState(strOngletName+"_pane_"+currentIndex, "block");
	if(objCurrentOnglet) {
		objCurrentOnglet.className = "onglet_on";
	}else{
		oLayer = wmGetElementId(strOngletName+"_"+currentIndex);
		if(oLayer) {
			oLayer.className = "onglet_on";
		}
	}
	for(var i=0; i<nb_onglet; i++) {
		if(i!=currentIndex) {
			wmSetLayerState(strOngletName+"_pane_"+i, "none");
			oLayer = wmGetElementId(strOngletName+"_"+i);
			if(oLayer) {
				oLayer.className = "onglet_off";
			}
		}
	}
}
