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

// DEPENDS: MooTools, iFrmBox

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

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

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

//======================================= Add Bookmark to Favorites

this.bookmark_url = function(title, url) {
	if (document.all) { // ie
		window.external.AddFavorite(url, title);
	} else if (window.sidebar) { // ffox
		window.sidebar.addPanel(title, url, '');
	} else if(window.opera && window.print){ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} else {
		alert('Your Browser does not support Add-To-Favorites / Bookmarks !');
	} //end if else
} //END FUNCTION

//======================================= 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

//======================================= AJAX Request (with Eval JScripts)

this.AJAX_Load_URL = function(y_div, y_url, y_method, y_img_loader, y_exec_jscripts) {
	//--
	if((y_url == null) || (y_url == '')) {
		return 0;
	} //end if
	if((y_method == null) || (y_method == '')) {
		y_method = 'GET';
	} //end if
	//--
	var ss = document.getElementById(y_div);
	//--
	var ajx = Browser.Request(); // MOO
	//--
	if((y_img_loader != null) && (y_img_loader != '')) {
		ss.innerHTML = '<div align="center"><img src="' + y_img_loader + '" title="Loading ..." alt="Loading ..."></div><br>' + ss.innerHTML;
	} //end if
	if (ajx.readyState == 4 || ajx.readyState == 0) {
		//--
		ajx.open(y_method, y_url, true);
		//--
		ajx.onreadystatechange = function() {
			if (ajx.readyState == 4) {
				//--
				ss.innerHTML = '' + ajx.responseText;
				//--
				if(y_exec_jscripts != false) {
					//--
					var xscripts = ss.getElementsByTagName("script"); 
					//--
					for(var i=0; i<xscripts.length; i++) {  
						eval(xscripts[i].text);  
					} //end for
					//--
				} //end if
				//--
			} //end if
		} //END FUNCTION
		//--
	} //end if
	//--
	ajx.send(null);
	//--
	return 1;
	//--
} //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

//======================================= Toggle DIV Visibility

this.ToggleVisibility = function(id) {
	//--
	var e = document.getElementById(id);
	//--
	if(e.style.display == 'none') {
		e.style.display = '';
		e.style.height='auto';
	} else {
		e.style.display = 'none';
		e.style.height='1px';
	} //end if else
	//--
} //END FUNCTION

//======================================= Limit TextArea 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

//======================================= Catch TAB Key in TextArea v.1.0

// Example: <textarea id="txt" onKeyDown="NetVision_JSUtil.catch_TABKey(event);">
this.catch_TABKey = function(evt) {
	//--
	var tab = "\t";
	var t = evt.target;
	var ss = t.selectionStart;
	var se = t.selectionEnd;
	var scrollTop = t.scrollTop;
	var scrollLeft = t.scrollLeft;
	//--
	if(evt.keyCode == 9) {
		//-- Tab key - insert tab expansion
		evt.preventDefault();
		//-- Special case of multi line selection
		if(ss != se && t.value.slice(ss,se).indexOf("\n") != -1) {
			//-- In case selection was not of entire lines (e.g. selection begins in the middle of a line) we have to tab at the beginning as well as at the start of every following line.
			var pre = t.value.slice(0,ss);
			var sel = t.value.slice(ss,se).replace(/\n/g,"\n"+tab);
			var post = t.value.slice(se,t.value.length);
			//--
			t.value = pre.concat(tab).concat(sel).concat(post);
			t.selectionStart = ss + tab.length;
			t.selectionEnd = se + tab.length;
		} else {
			//-- The Normal Case (no selection or selection on one line only)
			t.value = t.value.slice(0,ss).concat(tab).concat(t.value.slice(ss,t.value.length));
			if (ss == se) {
				t.selectionStart = t.selectionEnd = ss + tab.length;
			} else {
				t.selectionStart = ss + tab.length;
				t.selectionEnd = se + tab.length;
			} //end if
		} //end if else
		//--
		t.scrollTop = scrollTop;
		t.scrollLeft = scrollLeft;
		//--
	} //end if
	//--
} //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

//======================================= 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

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

} //END CLASS

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

//-- AUTOLOAD CODE BLOCK (CAN BE CHANGED)
var NetVision_Scan_iFrmBox = function() {
	var links = $$("a").filter(function(el) {
		if(el.rel && el.rel.test(/^slim_ifrmbox/i)) {
			el.onclick = function() {
				//--
				var aDim = this.rel.match(/[0-9]+/g);
				//--
				var winWidth = window.getWidth() * 0.9; // MOO
				var winHeight = window.getHeight() * 0.9; // MOO
				//--
				var w = (aDim && (aDim[0] > 0)) ? aDim[0] : winWidth; 
				var h = (aDim && (aDim[1] > 0)) ? aDim[1] : winHeight; 
				//--
				NetVision_JSUtil.PopUpLink(this.href, this.target, w, h, 0, 1);
				//--
				return false;
				//--
			} //end function
			return false;
		} //end if else
	});
} //END FUNCTION
//--
window.addEvent("domready", NetVision_Scan_iFrmBox);
//--

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


// #END
