
// NetVision JS - Browser Utils
// (c) 2006-2010 unix-world.org
// v.8.1
// r.20100202

// DEPENDS: MooTools

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

var WindowObjectuseAJAXOpener = 0;
var WindowObjectuseAJAXProtect = 0;
var WindowObjectReferenceOfRequestedPopup;

var NetVision_JSUtil = new function() { // START CLASS

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

this.htmlspecialchars = function(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

//======================================= forms :: GET/POST (this function must be called by a form button)

this.PopUpForm = function(objForm, strTarget, windowWidth, windowHeight, forcePopUp, forceDims) {
	//--
	if(((WindowObjectuseAJAXOpener) && (forcePopUp != 1)) || (forcePopUp == -1)) {
		objForm.target = 'netVision__iFrmBox__iFrame'; // ifrmbox use
	} else {
		objForm.target = strTarget; // normal popUp use
	} //end if else
	//--
	init_PopUp('lib/js/jsutil/img/busy.gif', objForm.target, windowWidth, windowHeight, forcePopUp, forceDims);
	//--
	return false;
	//--
} //END FUNCTION

//======================================= links :: GET (this function must be called by a form button)

this.PopUpLink = function(strUrl, strTarget, windowWidth, windowHeight, forcePopUp, forceDims) {
	//--
	init_PopUp(strUrl, strTarget, windowWidth, windowHeight, forcePopUp, forceDims);
	//--
	return false;
	//--
} //END FUNCTION

//======================================= Resize iFrames Dinamically on Height

this.resize_iFrame = function(f) {
	f.style.height = "1px";
    f.style.height = f.contentWindow.document.body.scrollHeight + "px";
} //END FUNCTION

//======================================= Limit Text Area v.1.2

this.textArea_Limit = function(y_field, y_countfield, y_maxlimit) {
	//--
	var maxlimit = 0 + y_maxlimit;
	if(maxlimit <= 0) {
		alert('TextArea (with Counter) :: Invalid Text Limit');
	} //end if
	//--
	var field = document.getElementById(y_field);
	var countfield = document.getElementById(y_countfield);
	//--
	if (field.value.length > maxlimit) { // if too long then trim it!
		field.value = field.value.substring(0, maxlimit);
	} else { // otherwise update the counter
		countfield.value = maxlimit - field.value.length;
	} //end if else
} //END FUNCTION

//======================================= Confirm Form Submit

this.confirm_Form_Submit = function(y_confirm) {
	var agree=confirm(y_confirm);
	if (agree) {
		return true ;
	} else {
		return false ;
	} //end if else
} //END FUNCTION

//======================================= Check All Checkbox

this.checkAll_CkBoxes = function(y_form_name) {
	var i;
	for(i=0; i<document.forms[y_form_name].elements.length; i++) {
		if(document.forms[y_form_name].elements[i].type == "checkbox") {
			document.forms[y_form_name].elements[i].checked = !document.forms[y_form_name].elements[i].checked;
		} //end if
	} //end for
} //END FUNCTION

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

// Example: <textarea id="txt" onKeyDown="NetVision_JSUtil.catch_TABKey(this, event);">
this.catch_TABKey = function(item,e) {
	var c;
	if(navigator.userAgent.match("Gecko")) {
		c = e.which;
	} else {
		c = e.keyCode;
	} //end if else
	if(c == 9) {
		replace_Selection(item,String.fromCharCode(9));
		setTimeout("document.getElementById('"+item.id+"').focus();",0);
		return false;
	} //end if
} //END FUNCTION

//======================================= Upload Multiple Files v.1.2.1

this.Multi_File_Upload = function(list_target, max, varname, vartype){
	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
	if((typeof varname == 'undefined') || (varname == '')) {
		varname = 'up_file';
	} //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')) {
			if(vartype == '[]') {
				element.name = varname + '[]'; // Element name is Array
			} else {
				element.name = varname + this.id++; // Element name + number
			} //end if else
			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;' + NetVision_JSUtil.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
	};
} //END FUNCTION

// ###################################### PRIVATES

//======================================= Open Req. PopUp

var init_PopUp = function(strUrl, strTarget, windowWidth, windowHeight, forcePopUp, forceDims) {
	//--
	var windowLeft, windowTop;
	//--
	if(((WindowObjectuseAJAXOpener) && (forcePopUp != 1)) || (forcePopUp == -1)) {
		//-- iFrmBox use
		if(forceDims != 1) {
			NetVision_iFrmBox.go_Load(strUrl, WindowObjectuseAJAXProtect); // we do not use here custom size
		} else {
			NetVision_iFrmBox.go_Load(strUrl, WindowObjectuseAJAXProtect, windowWidth, windowHeight); // we use here custom size
		} //end if else
		//--
	} else {
		//--
		if((typeof windowWidth == 'undefined') || (windowWidth == 0)) {
			windowWidth = 960;
		} //end if
		if((typeof windowHeight == 'undefined') || (windowHeight == 0)) {
			windowHeight = 620;
		} //end if
		//--
		windowLeft = 15;
		windowTop = 5;
		//-- normal use :: events (normal use): WindowObjectReferenceOfRequestedPopup == null ; WindowObjectReferenceOfRequestedPopup.closed
		WindowObjectReferenceOfRequestedPopup = window.open(strUrl, strTarget, "top=" + windowTop + ",left=" + windowLeft + ",width=" + windowWidth + ",height=" + windowHeight + ",menubar,resizable,scrollbars,status");
		WindowObjectReferenceOfRequestedPopup.focus();
		//--
	} //end if else
	//--
} //end function

//======================================= Replace Selection in a Field

//-- set selection
var set_Selection_Range = function(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
var replace_Selection = function(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){
				set_Selection_Range(input, selectionStart, selectionStart + 	replaceString.length);
			} else {
				set_Selection_Range(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
//--

} //END CLASS

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


// #END
