// Keeps track of locked input fields to make CSS play nicely
var fields = new Array();

// Function: Create Cookie
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

// Function: Read Cookie
function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

// Activates a collapsible UI panel
function activatePanel(panel){
	// First ensure the panel isn't already open
	// The first line below is a way to check for the existence of an object without raising errors
	if(window.activePanel){
		if(activePanel.id != panel.id){		
			// Now we close any previously opened panels, if any
			closePanel(activePanel);
			// Open up the new panel
			openPanel(panel);
		}
	} else {
		// Open up the new panel
		openPanel(panel);
	}
}

function openPanel(panel){
	//mozilla & ie hack. IE ignores whitespace while Mozilla considers it a node
	if(document.all){
		panel.nextSibling.className = "region, open";		
	} else {
		panel.nextSibling.nextSibling.className = "region, open";
	}
	panel.className = "titlebar_expanded";
	createCookie(document.title + "_activePanel", panel.id, 365);
	activePanel = panel;
}

function closePanel(panel){
	//mozilla & ie hack. IE ignores whitespace while Mozilla considers it a node
	if(document.all){
		panel.nextSibling.className = "region, closed";
	} else {
		panel.nextSibling.nextSibling.className = "region, closed";
	}
	panel.className = "titlebar_collapsed";
}

// Sets the currently active panel (from a set of collapsible ones) after page load
function setInitPanel(){
	//check for the cookie value, if there is one.
	if(readCookie(document.title + "_activePanel") != null){
		//check to make sure that panel specified in the cookie still exists
		pnl = readCookie(document.title + "_activePanel");
		if(pnl){
			activePanel = document.getElementById(readCookie(document.title + "_activePanel"));
			openPanel(activePanel);
		}	
	} else {
		// As a default, we open the Help panel
		activePanel = document.getElementById("Netmon_Help");
		openPanel(activePanel);
	}
}

function toggleSiblingDIV(iconRef){
	divRef = iconRef.parentNode.parentNode;
	
	//alert(divRef.nodeName);
	if(divRef.nextSibling.className == "open"){
		divRef.nextSibling.className = "closed";
		iconRef.src = "assets/icons/icon_plus_collapsed.gif";
	} else {
		divRef.nextSibling.className = "open";
		iconRef.src = "assets/icons/icon_minus_expanded.gif";
	}
}

// This function prints the contents of an inline frame. Takes the ID attribute of a frame as input.
function printFrame(frameRef){
	eval(frameRef).print();
}

// Return true if the second parameter is part of the array in parameter 1
function in_array(array, val) {
        var i;
        for (i = 0; i < array.length; i++) {
                if (array[i] == val) {
                        return true;
                }
        }
        return false;
}

// Deletes the array element in argument 1 that has the value passed in argument 2
function array_del(array, val) {
        var i;
        for (i = 0; i < array.length; i++) {
                if (array[i] == val) {
                        array[i] = '';
                }
        }
}

// Pushes the value of argument 2 in array in argument 1
function array_push(array, val) {
        array[array.length] = val;
}

// Returns the human-readable representation of an array's content
function dump(array) {
        var temp = '';
        var i;

        for (i = 0; i < array.length; i++) {
                temp += i + " => " + array[i] + "\n";
        }

        return temp;
}

// Returns the last element of the array that has a value, then deletes it.
function array_pop(array) {
        var i;
        for (i = array.length; i > 0; i--) {
                if (array[i] != '') {
                        var temp = array[i];
                        array[i] = '';
                        return temp;
                }
        }
}

// Returns True if a field is in locked state, false otherwise
function is_locked(field) {
        return in_array(fields, field);
}

// Sets a field in lock state.
// This prevents onMouseOut to override onFocus and onBlur
function lock_field(field) {
        array_push(fields, field);
}

// Unlocks a field (use this on onBlur)
function unlock_field(field) {
        array_del(fields, field);
}

// Checks if the email address passed as argument is valid or not
function isValidEmail(str) {
        var email=/^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,4}$/i;
        return(email.test(str));
}