//Functions used in the MC

// A simple javascript to pop-up windows...
function openWindow(newURL, newWidth, newHeight, resize, scrollbars, winName)  {
  // Declare and initialize top and left variables
  var calcLeft = 100;
  var calcTop = 100;
  if (scrollbars=="") {scrollbars="no"}
  if (winName == "") { winName = "remote";}
         
  // Update properties if comp. browser
  if (parseInt(navigator.appVersion) >= 4){
  calcTop = screen.availHeight /2 - newHeight / 2;
  calcLeft = screen.availWidth / 2 - newWidth / 2;}

  // Open the new window using top and left properties
  popup_win = window.open(newURL, winName, 'status=no,toolbar=no,menubar=no,location=no,scrollbars=' + scrollbars + ',width=' + newWidth + ',height=' + newHeight + ',left=' + calcLeft + ',top=' + calcTop + ',resizable=yes');
  popup_win.opener.name = "opener";
  popup_win.focus()
}   



function strltrim(x) 
{
	//trim trailing spaces from left
    return x.replace(/^\s+/,'');
}

function strrtrim(x) 
{
	//trim trailing spaces from right
    return x.replace(/\s+$/,'');
}
function strtrim(x) 
{
	//trim trailing spaces (right and left)
    return x.replace(/^\s+/,'').replace(/\s+$/,'');
}

function _el(el) {
	return document.getElementById(el);
}

function getASPSessionCookie() {
	var c = document.cookie.split(";");
	var mc = "";
	for (var t in c) {
		if (c[t].match(/ASPSESSIONID/i)) {
			mc += c[t] + ";"
		}
		if (c[t].match(/uid/i)) {
			mc += c[t] + ";"
		}
	}
	return URLEncode(encode64(mc));
}

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=;!@#$%^&*()_-?<>'\"[]{}|\\:~`";
function encode64(input) {
	var output = "";
	var chr1, chr2, chr3;
	var enc1, enc2, enc3, enc4;
	var i = 0;
	do {
		chr1 = input.charCodeAt(i++);
		chr2 = input.charCodeAt(i++);
		chr3 = input.charCodeAt(i++);
		enc1 = chr1 >> 2;
		enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
		enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
		enc4 = chr3 & 63;
		if (isNaN(chr2)) {
			enc3 = enc4 = 64;
		} else if (isNaN(chr3)) {
			enc4 = 64;
		}
		output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4);
	} while (i < input.length);
	return output;
}
function URLEncode(s)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = s;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
}
// ******************************************************************
// This function accepts a string variable and verifies if it is a
// proper date or not. It validates format matching either
// mm-dd-yyyy or mm/dd/yyyy. Then it checks to make sure the month
// has the proper number of days, based on which month it is.

// The function returns true if a valid date, false if not.
// ******************************************************************

function isDate(dateStr) {

	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?

	if (matchArray == null) 
	{
		alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
		return false;
	}

	month = matchArray[1]; // p@rse date into variables
	day = matchArray[3];
	year = matchArray[5];

	if (month < 1 || month > 12) { // check month range
	alert("Month must be between 1 and 12.");
	return false;
	}

	if (day < 1 || day > 31) {
	alert("Day must be between 1 and 31.");
	return false;
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("Month "+month+" doesn`t have 31 days!")
		return false;
	}

	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
		alert("February " + year + " doesn`t have " + day + " days!");
		return false;
		}
	}
	
	datNew= new Date();
	strYear = datNew.getFullYear() + 3; //allow up to 3 years in advance

	if (year < 1900 || year > strYear)
	{
		alert("Year "+year+" is out of range. ")
		return false;
	}
	
	return true; // date is valid
}
function isNumeric(x) {
	var RegExp = /^[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?$/;
	var result = x.match(RegExp);
	return result;
}
function isInteger(x) {
	var RegExp = /^[0-9]+$/;
	var result = x.match(RegExp);
	return result;
}

function verifyTextField(el,val,err) {
	if (val == "") {
		alert(err);
		el.focus();
		return false;
	}
	return true;
}
function verifyNumberField(el,val,err) {
	if (val == "" || (!isInteger(val)) ) {
		alert(err);
		el.focus();
		return false;
	}	
	return true;
}
function verifyDateField(el,val,err) {
	if (val == "") {
		alert(err);
		el.focus();
		return false;
	} else {
		var d = magicDate(val,el.id);
		if (d) {
			el.value = d
		} else {
			el.focus();
			return false;
		}
	}
	return true;
}

function addEvent(elm, evType, fn, useCapture)
// addEvent and removeEvent
// cross-browser event handling for IE5+,  NS6 and Mozilla
// By Scott Andrew
{
  if (elm.addEventListener){
    elm.addEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.attachEvent){
    var r = elm.attachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler for '" + evType + "' could not be added to '" + elm + "'");
  }
}

var helpshown=false;

function showHideHelp(action)
{
	var helpLabelDiv = document.getElementById("HelpLabel");
	var helpTextDiv = document.getElementById("HelpText");
	
	switch (action)
	{
		case "show":
			helpLabelDiv.innerHTML = "Hide Help";
			helpTextDiv.style.visibility = "visible";
			helpTextDiv.style.display = "block";  
			helpshown = true;
			break;
			
		case "hide":
			helpLabelDiv.innerHTML = "Show Help";
			helpTextDiv.style.visibility = "hidden";
			helpTextDiv.style.display = "none";  
			helpshown = false;
			break;
	}
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// the following functions are used for the dualListBox form element
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function delAttribute(strSelectedList,strAvailableList,strHiddenField){
	var availableList = document.getElementById(strAvailableList);
	var selectedList = document.getElementById(strSelectedList);
	var hiddenField = document.getElementById(strHiddenField);
	var selIndex = selectedList.selectedIndex;
	if(selIndex < 0)
		return;
	availableList.appendChild(selectedList.options.item(selIndex));
	selectNone(selectedList,availableList);
	updateSelections(hiddenField,selectedList);
}

function addAttribute(strSelectedList,strAvailableList,strHiddenField){
	var availableList = document.getElementById(strAvailableList);
	var selectedList = document.getElementById(strSelectedList);
	var hiddenField = document.getElementById(strHiddenField);
	var addIndex = availableList.selectedIndex;
	if(addIndex < 0)
		return;
	selectedList.appendChild(availableList.options.item(addIndex));
	selectNone(selectedList,availableList);
	updateSelections(hiddenField,selectedList);
}

function selectNone(list1,list2){
	list1.selectedIndex = -1;
	list2.selectedIndex = -1;
	addIndex = -1;
	selIndex = -1;
}

function updateSelections(hiddenField,selectedList) {
	var len = selectedList.options.length;
	var hiddenVal="";
	for (var i = 0; i < len; i++ ) {
		hiddenVal += (selectedList.options(i).value) + "" + (( i < (len - 1) )?",":"");
	}
	hiddenField.value = hiddenVal;
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ADMIN MENU FUNCITONS (start)
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function menuHover(obj, id, selected)
{
    cls_on = "AdminNavMenu_Hover"
    cls_off = "AdminNavMenu"
    cls_selected = "AdminNavMenu_Selected"
    if(obj.className == cls_off)
    {
        obj.className = cls_on     
    }
    else
    {
        obj.className = cls_off     
    }
    
}
var displayedSubMenuId;

function showSubMenu(menuId)
{
   if(displayedSubMenuId==undefined ||  menuId != displayedSubMenuId)
    {
        if (displayedSubMenuId!=undefined && displayedSubMenuId.length > 0)
        {
            //hide displayed menu 
            menu = document.getElementById(displayedSubMenuId)
            menu.className = "AdminNavMenu_SubMenu"
        }
        
        //show selected menu
         menu = document.getElementById(menuId)
         menu.className = "AdminNavMenu_SubMenu_Displayed"
        displayedSubMenuId = menuId
    }
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ADMIN MENU FUNCITONS (end)
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


// FILTER FUNCTIONS
function FilterList(pageURLVal, fieldElmId, valElmId, tagElmId)
{
    var fieldElm, valElm, tagElm
    fieldElm = document.getElementById(fieldElmId)
    valElm = document.getElementById(valElmId)
    tagElm = document.getElementById(tagElmId);
    
    if (fieldElm!=null && valElm!=null && pageURLVal!=null && pageURLVal.length>0)
    {
        //keyword filter is handled differently 
        var loc = pageURLVal 
        loc += ( pageURLVal.indexOf("?") < 0) ? "?" : "" ;
        loc += ( pageURLVal.indexOf("?") < pageURLVal.length-1) ? "&" : "" ;
        if (fieldElm[fieldElm.selectedIndex].value.toLowerCase() == "keyword")
            loc +=  "filterfield=" + fieldElm[fieldElm.selectedIndex].value + "&tag=" + tagElm[tagElm.selectedIndex].value;
        else
            loc +=  "filterfield=" + fieldElm[fieldElm.selectedIndex].value + "&filterval=" + valElm.value;
        window.location = loc;    
    }
}
function Filter_ShowHideTagList(fieldElmId, valElmId, tagElmId)
{
    var fieldElm, valElm, tagElm
    fieldElm = document.getElementById(fieldElmId)
    valElm = document.getElementById(valElmId);
    tagElm = document.getElementById(tagElmId);
    
    if (fieldElm[fieldElm.selectedIndex].value.toLowerCase() == "keyword")
    {
        valElm.style.visibility = "hidden"
        valElm.display = "none"
        valElm.style.position = "absolute"
        tagElm.style.visibility = "visible"
        tagElm.display = "inline"  
        tagElm.style.position = "relative"      
    }
    else
    {
        valElm.style.visibility = "visible"
        valElm.display = "inline"
        valElm.style.position = "relative"
        tagElm.style.visibility = "hidden"
        tagElm.display = "none"        
        tagElm.style.position = "absolute"      
    }
    
}

