
// NetOffice JS Utils - Browser
// (c) 2008 unix-world.org
// v.1.7.1
// r.081125


//======================================= Convert special characters to HTML entities v.810.114
//=======================================

function htmlspecialchars(string, quote_style) {
	string = string.toString();  
	string = string.replace(/&/g, '&amp;');  
	string = string.replace(/</g, '&lt;');  
	string = string.replace(/>/g, '&gt;');  
	// Encode depending on quote_style  
	if (quote_style == 'ENT_QUOTES') {  
		string = string.replace(/"/g, '&quot;');  
		string = string.replace(/'/g, '&#039;');  
	} else if (quote_style != 'ENT_NOQUOTES') {  
		// All other cases (ENT_COMPAT, default, but not ENT_NOQUOTES)  
		string = string.replace(/"/g, '&quot;');  
	} //end if else
	return string;
} //END FUNCTION

//======================================= Upload Multiple Files v.1.1
//=======================================

/* Example:
<!-- 1st element :: ID is required :: element NAME="up_file{#}" -->
<input id="multifile_uploader" type="file">
<!-- The File List DIV -->
<div id="multifile_list"></div>
<script>
	var multifile_selector = new MultiSelector(document.getElementById('multifile_list'), 10);
	multifile_selector.addElement(document.getElementById('multifile_uploader'));
</script>
*/

function MultiSelector(list_target, max){
	this.list_target = list_target; // Where to write the list
	this.count = 0; // How many elements ?
	this.id = 1; // The 1st counter
	if(max) { // find the max
		this.max = max;
	} else {
		this.max = -1;
	} //end if
	this.addElement = function(element) { // Add a new file input element
		// Make sure it's a file input element
		if(element.tagName == 'INPUT' && element.type == 'file') {
			element.name = 'up_file' + this.id++; // Element name + number
			element.multi_selector = this; // Add reference to this object
			element.onchange = function() { // What to do when a file is selected
				var new_element = document.createElement('input'); // New file input
				new_element.type = 'file';
				this.parentNode.insertBefore(new_element, this); // Add new element
				this.multi_selector.addElement(new_element); // Apply 'update' to element
				this.multi_selector.addListRow(this); // Update list
				this.style.position = 'absolute'; // Hide this: we can't use display:none because Safari doesn't like it
				this.style.left = '-1000px';
			};
			if( this.max != -1 && this.count >= this.max ) { // If we've reached maximum number, disable input element
				element.disabled = true;
			} //end if
			this.count++; // File element counter
			this.current_element = element; // Most recent element
		} else {
			alert( 'Error: MultiFile Uploader :: Not a file input element' ); // This can only be applied to file input elements!
		} //end if else
	};
	this.addListRow = function(element){ // Add a new row to the list of files
		var new_row = document.createElement('div'); // Row div
		new_row.element = element; // References
		var new_row_button = document.createElement('input'); // Delete button
		new_row_button.type = 'button';
		new_row_button.value = '[ X ]';
		new_row_button.onclick= function() { // Delete function
			this.parentNode.element.parentNode.removeChild(this.parentNode.element); // Remove element from form
			this.parentNode.parentNode.removeChild(this.parentNode); // Remove this row from the list
			this.parentNode.element.multi_selector.count--; // Decrement counter
			this.parentNode.element.multi_selector.current_element.disabled = false; // Re-enable input element (if it's disabled)
			return false; // prevent some browsers to reload the window
		};
		new_row.appendChild(new_row_button); // Add button
		var new_row_text = document.createElement('span'); // Text
		new_row_text.innerHTML = '<font face="verdana,tahoma,arial" size="1">&nbsp;&nbsp;' + htmlspecialchars(element.value) + '&nbsp;&nbsp;</font>'; // Set row value
		new_row.appendChild(new_row_text); // Add button
		this.list_target.appendChild(new_row); // Add it to the list
	};
};

//======================================= JS Util :: Smart Focus
//=======================================

// Allow webpages to Raise or lower windows :: GET | POST
//-- links :: GET
var WindowObjectReferenceOfRequestedPopup;
function OpenRequestedPopup(strUrl, strTarget, windowWidth, windowHeight) {
	var windowLeft, windowTop;
	//--
	if(typeof windowWidth == 'undefined') {
		windowWidth = 900;
	} //end if
	if(typeof windowHeight == 'undefined') {
		windowHeight = 625;
	} //end if
	//--
	windowLeft = 15;
	windowTop = 5;
	//--
	WindowObjectReferenceOfRequestedPopup = window.open(strUrl, strTarget, "top=" + windowTop + ",left=" + windowLeft + ",width=" + windowWidth + ",height=" + windowHeight + ",menubar,resizable,scrollbars,status");
	WindowObjectReferenceOfRequestedPopup.focus();
	//-- events
	// WindowObjectReferenceOfRequestedPopup == null
	// WindowObjectReferenceOfRequestedPopup.closed
	//--
} //end function
//-- forms :: POST
// this function must be called by a form button
function OpenRequestedPostForm(objForm, strTarget, windowWidth, windowHeight) {
	objForm.target = strTarget;
	OpenRequestedPopup('lib/core/img/busy.gif', strTarget, windowWidth, windowHeight);
} //end function
//--

//=======================================
//======================================= iFrames

// Resize iFrames Dinamically on Height
function resizeFrame(f) {
	f.style.height = "1px";
    f.style.height = f.contentWindow.document.body.scrollHeight + "px";
} //end function


//=======================================
//======================================= Cookies

function readCookie(name) {
	var cookieValue = "";
	var search = name + "=";
	if(document.cookie.length > 0) {
		offset = document.cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = document.cookie.indexOf(";", offset);
			if (end == -1) {
				end = document.cookie.length;
			} //end if
			cookieValue = unescape(document.cookie.substring(offset, end))
		} //end if
	} //end if
	return cookieValue;
} //END FUNCTION

//=======================================
//======================================= Replace Selection

//-- set selection
function setSelectionRange(input, selectionStart, selectionEnd) {
	if (input.setSelectionRange) {
		input.focus();
		input.setSelectionRange(selectionStart, selectionEnd);
	} else if (input.createTextRange) {
		var range = input.createTextRange();
		range.collapse(true);
		range.moveEnd('character', selectionEnd);
		range.moveStart('character', selectionStart);
		range.select();
	} //end if else
} //END FUNCTION
//-- replace selection
function replaceSelection (input, replaceString) {
	if (input.setSelectionRange) {
			var selectionStart = input.selectionStart;
			var selectionEnd = input.selectionEnd;
			input.value = input.value.substring(0, selectionStart)+ replaceString + input.value.substring(selectionEnd);
			if (selectionStart != selectionEnd){
				setSelectionRange(input, selectionStart, selectionStart + 	replaceString.length);
			} else {
				setSelectionRange(input, selectionStart + replaceString.length, selectionStart + replaceString.length);
			} //end if else
	} else if (document.selection) {
			var range = document.selection.createRange();
			if (range.parentElement() == input) {
				var isCollapsed = range.text == '';
				range.text = replaceString;
				if (!isCollapsed)  {
					range.moveStart('character', -replaceString.length);
					range.select();
				} //end if
			} //end if
	} //end if else
} //END FUNCTION

//======================================= Catch TAB
//=======================================

// catch the TAB key
function catchTab(item,e){
	if(navigator.userAgent.match("Gecko")){
		c=e.which;
	} else {
		c=e.keyCode;
	} //end if else
	if(c==9){
		replaceSelection(item,String.fromCharCode(9));
		setTimeout("document.getElementById('"+item.id+"').focus();",0);
		return false;
	} //end if
} //END FUNCTION


//=======================================
//======================================= Show / Hide Element


function switchdiv(id) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		if(document.getElementById(id).style.display == 'none') {
			showdiv(id);
		} else {
			hidediv(id);
		} //end if else
	} else {
		if (document.layers) { // Netscape 4
			if(document.id.display == 'none') {
				showdiv(id);
			} else {
				hidediv(id);
			} //end if else
		} else { // IE 4
			if(document.all.id.style.display == 'none') {
				showdiv(id);
			} else {
				hidediv(id);
			} //end if else
		} //end if else
	} //end if else

} //END FUNCTION

function hidediv(id) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	} else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		} else { // IE 4
			document.all.id.style.display = 'none';
		} //end if else
	} //end if else
} //END FUNCTION


function showdiv(id) {
	//safe function to show an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	} else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		} else { // IE 4
			document.all.id.style.display = 'block';
		} //end if else
	} //end if else
} //END FUNCTION

//=======================================
//=======================================

// END SCRIPT

