// -------------------------------------------------------------------------- \\
// WDSCommon.js
// jsLibrary javascript file
// creation : 09/06/2004 by Charley FREDAIGUES
// update   : 13/10/2005
// -------------------------------------------------------------------------- \\
// Form fields managment
// Opening pop-ups
// -------------------------------------------------------------------------- \\


// -------------------------------------------------------------------------- \\
// WDSCommon class
// -------------------------------------------------------------------------- \\
// constructor...
// -------------------------------------------------------------------------- \\
function WDSCommon () {
	// attributes
	// none ...
	
	// methods
	this.getTag								= WDSCommon_getTag;
	this.updateTag 			  		= WDSCommon_updateTag;
	this.updateSelect 			  = WDSCommon_updateSelect;
	this.updateCheckBox 		  = WDSCommon_updateCheckBox;
	this.updateRadio          = WDSCommon_updateRadio;
	this.updateInputText      = WDSCommon_updateInputText;
	
	this.checkCheckBox        = WDSCommon_checkCheckBox;
	
	this.getTagValue          = WDSCommon_getTagValue;
	
	this.disableTag						= WDSCommon_disableTag;
	this.disableRadio 				= WDSCommon_disableRadio;
	
	this.popWin       				= WDSCommon_popWin;
	this.popSizedWin  				= WDSCommon_popSizedWin;
	this.popSizedWinNoScroll  = WDSCommon_popSizedWinNoScroll;
	
	this.formatNumber         = WDSCommon_formatNumber;
	
	this.unEscapeXml 					= WDSCommon_unEscapeXml;
	this.replaceStr						= WDSCommon_replaceStr;
}

// set as a singleton
var WDSCommon = new WDSCommon();

// -------------------------------------------------------------------------- \\
// WDSCommon_getTag
// 	  - tag        : the tag object of the form object
//    - name       : the name of the html tag select
// -------------------------------------------------------------------------- \\
// * get the tag
// -------------------------------------------------------------------------- \\
function WDSCommon_getTag (tag, name) {
	if (tag.tagName) {
		if (tag.tagName.toUpperCase() == "FORM") {
			var form = tag;
			tag = eval("form." + name);
		}
	}
	if (!tag) {
		WDSTrace.dump("WDSCommon.getTag return NULL !");
		return null;
	}
	return tag;
}


// -------------------------------------------------------------------------- \\
// WDSCommon_updateTag
// 	  - tag        : the tag object of the form object
//    - name       : the name of the html tag select (null if tag is a FORM)
//    - value      : the value to be set in the select tag
//    - jsonchange : onchange code to be executed ... (optional)
// -------------------------------------------------------------------------- \\
// * update the tag as describe
// -------------------------------------------------------------------------- \\
function WDSCommon_updateTag (tag, name, value, jsonchange) {
	tag = WDSCommon.getTag(tag, name);
	if (!tag) {
		return;
	}
	var type = tag.type;
	if (tag.tagName == "SELECT") {
		this.updateSelect(tag, value, jsonchange);
	} else if (tag.tagName == "INPUT") {
		if (type.toUpperCase() == "CHECKBOX") {
			this.updateCheckBox(tag, value);
		} else if (type.toUpperCase() == "TEXT") {
			this.updateInputText(tag, value);
		} else if (type.toUpperCase() == "RADIO") {
			this.updateRadio(tag, value, jsonchange);
		} else if (tag.length) {
			if (tag[0].type.toUpperCase() == "RADIO") {
				this.updateRadio(tag, value, jsonchange);
			}
		} else {
			this.updateInputText(tag, value);
		}
	} else if (tag.tagName == "TEXTAREA") {
		this.updateInputText(tag, value);
	} else {
		if (tag.length) {
			if (tag[0].type.toUpperCase() == "RADIO") {
				this.updateRadio(tag, value, jsonchange);
			}
		}
	}
}

// -------------------------------------------------------------------------- \\
// WDSCommon_disableTag
// 	  - tag        : the tag object
//    - [name]     : the name of the html tag input
//                   mandatory if tag is a form object 
//    - disable    : true / false
//    - [value]    : if radio please specify the value to disable the correct
// -------------------------------------------------------------------------- \\
// * disable the tag
// -------------------------------------------------------------------------- \\
function WDSCommon_disableTag (tag, name, disable, value) {
	tag = WDSCommon.getTag(tag, name);
	if (!tag) {
		return;
	}
	var type = tag.type;
	if (tag.tagName == "SELECT") {
		tag.disabled = disable;
	} else if (tag.tagName == "INPUT") {
		if (type.toUpperCase() == "TEXT") {
			tag.disabled = disable;
		} else if (type.toUpperCase() == "CHECKBOX") {
			tag.disabled = disable;
		} else if (type.toUpperCase() == "RADIO") {
			this.disableRadio(tag, disable, value);
		} else if (tag.length) {
			if (tag[0].type.toUpperCase() == "RADIO") {
				this.disableRadio(tag, disable, value);
			}
		} else {
			tag.disabled = disable;
		}
	} else if (tag.tagName == "TEXTAREA") {
		tag.disabled = disable;
	} else if (tag.length) {
		if (tag[0].type.toUpperCase() == "RADIO") {
			this.disableRadio(tag, disable, value);
		}
	}
}

// -------------------------------------------------------------------------- \\
// WDSCommon_disableRadio - PRIVATE
// 	  - tag        : the tag object
//    - disable    : true / false apply disable or not
//    - value      : the radio value to apply disable or not
// -------------------------------------------------------------------------- \\
// * disable a radio button
// -------------------------------------------------------------------------- \\
function WDSCommon_disableRadio (tag, disable, value) {
	if (tag.length) {
		for (i = 0; i < tag.length; i++) {
			var currElem = tag[i];
			if (!value || currElem.value == value || disable == false) {
				currElem.disabled = disable;
			}
		}
	} else {
		if (tag.value == value) {
			tag.disabled = disable;
		}
	}
}

// -------------------------------------------------------------------------- \\
// WDSCommon_updateInputText
// 	  - tag        : the tag object
//    - value      : the value to be set in the select tag
//    - jsonchange : onchange code to be executed ... (optional)
// -------------------------------------------------------------------------- \\
// * update the input text tag as describe
// -------------------------------------------------------------------------- \\
function WDSCommon_updateInputText (tag, value, jsonchange) {
	if (value == null) {
		value = "";
	}
	tag.value = value;
}

// -------------------------------------------------------------------------- \\
// WDSCommon_updateRadio
// 	  - tag        : the tag object
//    - value      : the value to be set in the select tag
//    - jsonchange : onchange code to be executed ... (optional)
// -------------------------------------------------------------------------- \\
// * update the input radio tag as describe
// -------------------------------------------------------------------------- \\
function WDSCommon_updateRadio (tag, value, jsonchange) {
	if (value == null) {
		return;
	}
	if (tag.length) {
		for (i = 0; i < tag.length; i++) {
			var currElem = tag[i];
			if (currElem.value == value) {
				currElem.checked = true;
				if (null != jsonchange) {
					eval(jsonchange);
				}
				return;
			}
		}
	} else {
		if (tag.value == value) {
			tag.checked = true;
			if (null != jsonchange) {
				eval(jsonchange);
			}
			return;
		}
	}
}

// -------------------------------------------------------------------------- \\
// WDSCommon_updateSelect
// 	  - tag        : the tag object
//    - value      : the value to be set in the select tag
//    - jsonchange : onchange code to be executed ... (optional)
// -------------------------------------------------------------------------- \\
// * update the select tag as describe
// * if value = null then select the first element
// -------------------------------------------------------------------------- \\
function WDSCommon_updateSelect (tag, value, jsonchange) {
	if (tag.tagName == "SELECT") {
		if (null == value) {
			if (tag.options) {
				if (tag.options.length > 0) {
					tag.options[0].selected = true;
					if (null != jsonchange) {
						eval(jsonchange);
					}
				}
			}
			return;
		}
		for (var i = 0; i < tag.options.length; i++) {
			var currvalue = tag.options[i].value;
			if (currvalue.toUpperCase() == value.toUpperCase()) {
				tag.options[i].selected = true;
				if (null != jsonchange) {
					eval(jsonchange);
				}
				/*
					Charley - 02/06/2004
					note : 
					keep that code ... when wanting to get onchange real value 
				  can't use that code when "this" is used in line code 
					that's why i do not use that code
					 
				var myonchange = tag.onchange;
				var mystring = new String(myonchange);
				mystring = mystring.substring(22, mystring.length - 1);
				eval(mystring);
				*/
			}
		}
	}
}

// -------------------------------------------------------------------------- \\
// WDSCommon_updateCheckBox
// 	  - tag        : the tag object
//    - value      : the value to be set in the select tag
// -------------------------------------------------------------------------- \\
// * update the checkbox tag as describe
// -------------------------------------------------------------------------- \\
function WDSCommon_updateCheckBox (tag, value) {
	var type = tag.type;
	if (type.toUpperCase() == "CHECKBOX") {
		if ( ((tag.value == value) && (!tag.checked)) || 
			   ((tag.value != value) && (tag.checked)) ) {
			tag.click();
		}
	}
}

// -------------------------------------------------------------------------- \\
// WDSCommon_checkCheckBox
// 	  - id        : the id of the html tag select
//    - value     : the value to check
// -------------------------------------------------------------------------- \\
// * update the checkbox tag as describe
// -------------------------------------------------------------------------- \\
function WDSCommon_checkCheckBox (id) {
	var tag = null;
	tag = document.getElementById(id);
	if (!tag) {
		return;
	}
	tag.checked = true;
}


// -------------------------------------------------------------------------- \\
// WDSCommon_getTagValue
// 	  - tag        : the tag object
//    - [name]     : the name of the html tag input
//                   mandatory if tag is a form object 
// -------------------------------------------------------------------------- \\
// * get the tag value
// -------------------------------------------------------------------------- \\
function WDSCommon_getTagValue (tag, name) {
	if (tag.tagName) {
		if (tag.tagName.toUpperCase() == "FORM") {
			var form = tag;
			tag = eval("form." + name);
		}
	}
	if (!tag) {
		WDSTrace.dump("WDSCommon_getTagValue : input " + name +" does not exist");
		return "";
	}

	var type = tag.type;
	if (tag.tagName == "SELECT") {
		WDSTrace.dump("WDSCommon_getTagValue : SELECT : " + tag.value);
		return tag.value;
	} else if (tag.tagName == "INPUT") {
		if (tag.length) {
			if (tag[0].type.toUpperCase() == "RADIO") {
				for (i = 0; i < tag.length; i++) {
					var currElem = tag[i];
					if (currElem.type.toUpperCase() == "RADIO" && currElem.checked) {
						WDSTrace.dump("WDSCommon_getTagValue : RADIO : " + currElem.value);
						return currElem.value;
					}
				}
			}
		} else {
			if (tag.type.toUpperCase() == "CHECKBOX") {
				if (tag.checked == true) {
					WDSTrace.dump("WDSCommon_getTagValue : CHECKBOX : " + tag.value);
					return tag.value;
				}
			} else {
				WDSTrace.dump("WDSCommon_getTagValue : ? : " + tag.value);
				return tag.value;
			}
		}
	} else if (tag.tagName == "TEXTAREA") {
		return tag.value;
	} else {
		if (tag.length) {
			for (i = 0; i < tag.length; i++) {
				var currElem = tag[i];
				if (currElem.type.toUpperCase() == "RADIO" && currElem.checked) {
					WDSTrace.dump("WDSCommon_getTagValue : RADIO : " + tag.value);
					return currElem.value;
				}
			}
		} else {
			if (tag.checked) {
				WDSTrace.dump("WDSCommon_getTagValue : ? : " + tag.value);
				return tag.value;
			}			
		}		
	}
	WDSTrace.dump("WDSCommon_getTagValue : bad");
	return "";
}



// -------------------------------------------------------------------------- \\
// WDSCommon_popWin
// 	  - uri      : url to be opened
// -------------------------------------------------------------------------- \\
// * open the given url in a pop up window with a fixed size (650x450)
// -------------------------------------------------------------------------- \\
function WDSCommon_popWin (uri) {
	var thePopwin = window.open(uri, '', 'scrollbars=yes,resizable=yes,width=650,height=450,left=50,top=50');
	return thePopwin;
}

// -------------------------------------------------------------------------- \\
// WDSCommon_popSizedWin
// 	  - uri       : url to be opened
//    - width     : width of the window
//    - height    : height of the window
// -------------------------------------------------------------------------- \\
// * open the given url in a pop up window with a fixed size (650x450)
// -------------------------------------------------------------------------- \\
function WDSCommon_popSizedWin (uri, width, height) {
	var thePopSizedWin = window.open(uri, '', 'scrollbars=yes,resizable=yes,width=' + width + ',height=' + height + ',left=50,top=50');

	if (navigator.appName == "Netscape") {
		thePopSizedWin.focus();
	}
	return thePopSizedWin;
}

// -------------------------------------------------------------------------- \\
// WDSCommon_popSizedWinNoScroll
// 	  - uri       : url to be opened
//    - width     : width of the window
//    - height    : height of the window
// -------------------------------------------------------------------------- \\
// * open the given url in a pop up window with a fixed size (650x450)
//   and no scroll
// -------------------------------------------------------------------------- \\
function WDSCommon_popSizedWinNoScroll (uri, width, height) {
	var thePopSizedWin = window.open(uri, '', 'scrollbars=no,resizable=no,width=' + width + ',height=' + height + ',left=50,top=50');

	if (navigator.appName == "Netscape") {
		thePopSizedWin.focus();
	}
	return thePopSizedWin;
}

// -------------------------------------------------------------------------- \\
// WDSCommon_formatNumber
// -------------------------------------------------------------------------- \\
// * format a number with d decimals
// -------------------------------------------------------------------------- \\
function WDSCommon_formatNumber (n, d) {
	n = n;
	d = d || 2;
	var f = Math.pow(10, d);
	n = Math.round(n * f) / f;
	n += Math.pow(10, - (d + 1));
	n += '';
	return d == 0 ? n.substring(0, n.indexOf('.')) : n.substring(0, n.indexOf('.') + d + 1);
}

// -------------------------------------------------------------------------- \\
// WDSCommon_replaceStr
// -------------------------------------------------------------------------- \\
// * Replaces text with by in string
// -------------------------------------------------------------------------- \\
function WDSCommon_replaceStr(string,text,by) {
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;
    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;
    var newstr = string.substring(0,i) + by;
    if (i+txtLength < strLength)
        newstr += WDSCommon.replaceStr(string.substring(i+txtLength,strLength),text,by);
    return newstr;
}

// -------------------------------------------------------------------------- \\
// WDSCommon_unEscapeXml
// -------------------------------------------------------------------------- \\
// * Convert escaped Xml entities to ascii (reverse the effect of the <c:out /> on xml entities &, <, >, ', ").
// * Ex: &#034;o&#039;donnel&034; -> "o'donnel"
// * Usefull when the result of <c:out /> may have quote and simple quote and that you
// * need to put it in a javascript string.
// * Ex: var myVar = WDSCommon.unEscapeXml("<c:out var='${var}' />");
// -------------------------------------------------------------------------- \\
function WDSCommon_unEscapeXml(text) {
		text = WDSCommon.replaceStr(text,'&#039;', "'");
		text = WDSCommon.replaceStr(text,'&#034;',"\"");
		text = WDSCommon.replaceStr(text,'&amp;',"&");
		text = WDSCommon.replaceStr(text,'&lt;',"<");
		text = WDSCommon.replaceStr(text,'&gt;',">");
		return text;
}


