function selectNodes(list) {
	for (var i = 0; i < list.length; i++) {
		if (list.options[i].value != "")
			list.options[i].selected = true;
	}
	return true;
}



function selectAll(id) {
    var e = document.getElementById(id);
    
    for (var i=0; i < e.length; i++) {
        e.options[i].selected = true;
    }
}

function checkSession()
{
    Ajax("/ajax/checkSession.asp", "");
    if (xmlHTTP.responseText == "False") { // note:xmlHTTP defined in /js/ajax.js
        return false;
    }
    else
    {
        return true;
    }

}

function checkSessionNet() {
    Ajax("/ajax/checkSession.aspx", "");
    if (xmlHTTP.responseText == "False") { // note:xmlHTTP defined in /js/ajax.js
        return false;
    }
    else {
        return true;
    }

}

function showWindoid(address, height, width, currentaddress)
{
    // session is still alive
    if (checkSession() == true)
    {
        // display windoid
        showWindow(address, height, width); // this function is defined in /template/globalscripts.js
    }
    else // session is lost
    {
        // redirect to lost session page
        window.location = currentaddress;
    }
    
}


function toggleSelect(id) {
    var e = document.getElementById(id);
    var notSelectedFlag = false;
    var i = 0;
    for (i = 0; i < e.length; i++)
    {
        if (e.options[i].selected == false)
        {    
            notSelectedFlag = true;
        }
    }
    if (notSelectedFlag) // at least one option not selected, so select all
    {
        for (i = 0; i < e.length; i++) {
            e.options[i].selected = true;
        }
    }
    else // all options selected, so deselect all
    {
        for (i = 0; i < e.length; i++) {
            e.options[i].selected = false;
        }
    }
   
}
 
function selectLists() {
	for (i=0; i < document.theForm.elements.length; i++) {
		if ((document.theForm.elements[i].type == "select-one" ||
		    document.theForm.elements[i].type == "select-multiple") && 
			
			(document.theForm.elements[i].name == "list1" ||
			 document.theForm.elements[i].name == "list2" ||
			 document.theForm.elements[i].name == "list4" ||
			 document.theForm.elements[i].name.indexOf("peerList2") != -1)) { 
				selectNodes(document.theForm.elements[i]);
		}
	}
}
	
function checkSel() {
	for (var i = 0; i < document.theForm.elements.length; i++) {
		if (document.theForm.elements[i].type == "checkbox" &&
			document.theForm.elements[i].checked) {
				return true;
		}		
	}		 
	
	// No checkbox selected
	WarningWindow("noSelection");
	return false;
}
			
function check(on, section) {
	// Remember the value of this checkbox if it exists
	if (document.theForm.useDefaults) {
		var defaultChecked = document.theForm.useDefaults.checked;
	}
	
	for (var i = 0; i < document.theForm.elements.length; i++) {
		if (document.theForm.elements[i].type == "checkbox") {
			if (section == null || document.theForm.elements[i].id.indexOf(section) != -1)
				document.theForm.elements[i].checked = on;
		}		
	}		
	
	// If user clicked the <clear> button then select proper radio button
	if (!on) document.theForm.settings[0].checked = true;
	
	// The loop above may have reset this checkbox.. put back to original value if it exists.
	if (document.theForm.useDefaults) {
		document.theForm.useDefaults.checked = defaultChecked;
	}
}

var SPECIAL_CHAR_ERR_MSG_CONST = "names may only contain alpha-numeric characters, space or - . / : _"; 
var SPECIAL_CHAR_ERR_MSG = "names may only contain alpha-numeric characters, space or - . / : _";

function specialCharExists(name, mType) {
	var ascCode;
	var charFound = false;
	
	SPECIAL_CHAR_ERR_MSG = "";
	
	if (mType == null) {mType = "";}  
	mType = mType.toUpperCase();

	for (var i=0; i < name.length; i++) {
		ascCode = name.substr(i,1).charCodeAt(0);
		
		// Valid Character ascii values
		// ----------------------------
		// (32) space - Note: not allowed in URLs
		// (33) Exclamation - Note: URL only
		// (36) dollar sign - Note: only for Alert Notification Subject strings.
		// (37) percent - Note: only allowed for search strings
		// (38) ampersand - Note: we do not advertise this as a valid character. Allowed for URL and Population names.
		// (44) comma - Note: only allowed for Backbone test names.
		// (45) hyphen
		// (46) period
		// (47) slash
		// (48 - 57) numeric 0 through 9
		// (58) colon
		// (61) equal - Note: URL only and Nofification Subject strings.
		// (63) qestion mark - Note: only allowed for search strings and URL strings
		// (65 - 90) uppercase chars
		// (91) left square bracket - Note: only for Alert Notification Subject strings.
		// (93) right square bracket - Note: only for Alert Notification Subject strings.
		// (95) underscore
		// (97 - 122) lowercase chars
		// 
		if ( (mType != "URL" && ascCode == 32) || 
		     (mType == "URL" && ascCode == 33) ||      						 
	  	     (mType == "NOTIFICATION" && ascCode == 36) ||
	  	     ((mType == "SEARCH") && ascCode == 37) ||
		     ((mType == "POPULATION" || mType == "URL") && ascCode == 38) ||
		     ((mType == "DC") || (mType == "BB") && ascCode == 44) ||
		     ascCode == 45 ||  
		     ascCode == 46 ||
		     ascCode == 47 ||   					 
		     (ascCode > 47 && ascCode < 58) ||		 
		     ascCode == 58 ||
		     ((mType == "URL" || mType == "NOTIFICATION") && ascCode == 61) ||
    	     ((mType == "SEARCH" || mType == "URL") && ascCode == 63) ||
		     (ascCode > 64 && ascCode < 91) || 
		     (mType == "NOTIFICATION" && ascCode == 91) ||
		   	 (mType == "NOTIFICATION" && ascCode == 93) ||	
		   	  ascCode == 95 || 	 
		     (ascCode > 96 && ascCode < 123) ) {	 
				charFound = false;
		}	
		else {
			charFound = true;

			if (mType == "DC")  
				SPECIAL_CHAR_ERR_MSG = SPECIAL_CHAR_ERR_MSG_CONST + " ,";
			else if (mType == "URL") 
				SPECIAL_CHAR_ERR_MSG = SPECIAL_CHAR_ERR_MSG_CONST.replace(/, space/ig,"") + " ! & = ?";
			else if (mType == "NOTIFICATION")
			    SPECIAL_CHAR_ERR_MSG = SPECIAL_CHAR_ERR_MSG_CONST.replace(/names /ig,"Subject lines ") + " [ ] $ =";
			else
			    SPECIAL_CHAR_ERR_MSG = SPECIAL_CHAR_ERR_MSG_CONST;
			
			break;	
		}	
	} // for		

	return charFound;
} // specialCharExists		

function trimLR(sString) {
	while (sString.substring(0,1) == ' ') {
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ') {
		sString = sString.substring(0,sString.length-1);
	}

	return sString;
}

function wrapText(blob, length) {
	var temp = "";
	var s;
	
	if (blob != null) {
		if (blob.length <= length)
			temp = blob;
		else {	
			for (var i = 0; i < blob.length/length; i++) { // to len(blob) / length
				s = (i * length);
				if (temp != "")
					temp += "<br>";
				
				temp += blob.substr(s, length);
			}
		}
	}
	
	return temp;
}

function selectItem(listName, selectValue) {
    var list = document.getElementById(listName);
    
    if (list != null) {
        for (var i=0; i < list.length; i++) {
            if (list.options[i].value == selectValue) {
                list.options[i].selected = true;
                break;
            }
        }
    }
}

function hide(id,hl,hlText) {
    var section = document.getElementById(id);
    //var hl = document.getElementById(hl);

    if (section != null) {
        if (section.className == "lyrOn" || section.className == "") {
            section.className = "lyrOff";
        }
        else {
            section.className = "lyrOn";
        }
        
        if (hl != null) {
            hl.innerHTML = hlText;
        }
    }
}

function add2list(list, optText, optValue) {
	if (list != null) {
    	list.options[list.length] = new Option(optText, optValue);
        
        // Move option (aphabetically)
		var tempText;
		var tempValue;
		
		for (var i = list.length-1; i > 0; i--) {
			if (list.options[i].text.toUpperCase() < list.options[i-1].text.toUpperCase()) {
				tempText = list.options[i-1].text;
				tempValue = list.options[i-1].value;	
		
				list.options[i-1].text = list.options[i].text;
				list.options[i-1].value = list.options[i].value;
			
				list.options[i].text = tempText;
				list.options[i].value = tempValue;
			}
			else
				break;  // exit 'for'
		}
	}
} // addOption

function expander(id, hlName) {
	var section = document.getElementById(id);
	var hlOne = document.getElementById(hlName + "1");
	var hlTwo = document.getElementById(hlName + "2");
	
	if (section.style.overflow == "auto") {
		section.style.overflow = "visible";
		if (hlOne) {hlOne.innerHTML = "collapse";}
	    if (hlTwo) {hlTwo.innerHTML = "collapse";}
	}
	else {
		section.style.overflow = "auto";
		if (hlOne) {hlOne.innerHTML = "expand";}
		if (hlTwo) {hlTwo.innerHTML = "expand";}
	}
}

function options(hl, id, onText, offText) {
    var section = document.getElementById(id);
    var link = document.getElementById(hl);
   
    if (onText == "") {onText = "fewer options";}
    if (offText == "") {offText = "more options";}
       
    if (section.className == "lyrOff") {
        section.className = "lyrOn";
        link.innerHTML = offText;    
    }
    else {
        section.className = "lyrOff";
        link.innerHTML = onText;
    }
        
}

function validateDelete(id, name) {
    return confirm("Are you sure you want to delete\n" + name + "?");
}

function reloadDays(dayList, monthList, yearList) {
    var savedDay = dayList.options[dayList.selectedIndex].value;
    var month = monthList.options[monthList.selectedIndex].value;
    var year = yearList.options[yearList.selectedIndex].value;
    var text = "";
    
    if (month == -1)  // defaulted "mo" value
        return;
        
    var days = daysinmonth(month, year);

    dayList.length = 0;  // clear out existing options
     
    for (var i=1; i<=days; i++) {
        var option = document.createElement("option");
        
		if (i < 10)
		    text = "0" + i;
		else
		    text = i;
		    
		option.value = i;
		option.text = text;
		 
		if (i == savedDay) {
		    option.selected = true;
		}

		dayList.options.add(option);
    }
}

function daysinmonth(month, year) {
    var days;
    
    switch (month) {
        case "1":
        case "3":
        case "5":
        case "7":
        case "8":
        case "10":
        case "12":
            days = "31";
            break;
            
        case "4":
        case "6":
        case "9":
        case "11":
            days = "30";
            break;
            
        case "2":
            if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) ) 
                days = "29";
            else
                days = "28";
            break;
    }
    
    return days;
}   

function resizeIframe(frmname, newheight) {
    document.getElementById(frmname).style.height = newheight + "px";
}

function resizeIframeXY(iframe_id, x, y) {
    var iframe = document.getElementById(iframe_id);
    
    iframe.style.height = y + "px"; 
    iframe.style.width = x + "px"; 

}

function ResizeIFrameHeight(iframe_id) {
    var h;

    if (document.all) 
	    var	h = document.frames(iframe_id).document.body.scrollHeight;
    else
	    var h = document.getElementById(iframe_id).contentDocument.body.scrollHeight;

    document.getElementById(iframe_id).style.height = h; 
}
		
function setDocumentDomain() {
    var dDomain = document.domain;
    var position = dDomain.indexOf(".");
    
    if (position != -1) {
        dDomain = dDomain.substr(position + 1); 
        if (dDomain.length > 0) {
            document.domain = dDomain;
        }
    }
}

function isNumeric(value) {
    var regexp = new RegExp(); 

    // Test for Non-Numeric characters
    regexp = /\D+/

	if (regexp.test(trimLR(value))) 
        return false;
    else
        return true;
}

function decimalPlaces(n_value) {
	var dPlaces = 0;
	
	pos = n_value.indexOf(".");

	if (pos != -1)
	    dPlaces = n_value.substr(pos).length;  

    return dPlaces;
}


function accept09(e) {
    var key;
    
    if (e.which) // firefox etc
        key = e.which;
    else
        key = e.keyCode;
        
    return (key < 33 || (key > 47 && key < 58) || key > 127);
}

function rsWindow(iframe) {
    var minHeight = 150;
    var h;
    
    //if (document.getElementById(iframe).className == "lyrOn") {
        if (eval(iframe) != null) {
            h = eval(iframe).getPageHeight();
            document.getElementById(iframe).style.height = h < minHeight ? minHeight : h + "px";
        }
    //}
}

function validateEmailList(id, errorId) {
    var list = document.getElementById(id).value;
    var errMsg = document.getElementById(errorId);
    var result = true;

    errMsg.className = "lyrOff";
    
    if (trimLR(list).length > 0) {
        Ajax("/Ajax/EmailValidation.aspx","emailList=" + list);
        
        if (xmlHTTP.responseText != "success") {
            errMsg.innerHTML = "Invalid Email Address (" + xmlHTTP.responseText + ")<br>";
            errMsg.className = "lyrOn";
            result = false;
        }
    }
    
    return result;
}

// Duplicate name - see /chartfx/popup.js
//function showPopup(id, e) {
//    var ex = e.clientX;
//    var ey = e.clientY;
//    var popup = document.getElementById(id);
//
//    popup.style.top = (ey + document.documentElement.scrollTop)-5 + "px";
//    popup.style.left = ex-50 + "px";
//    popup.className = "lyrOn";
//}
//
// via JS Cookbook
function getElementStyle(elemID, IEStyleProp, CSSStyleProp) {
    var elem = document.getElementById(elemID);
    if (elem.currentStyle) {
        return elem.currentStyle[IEStyleProp];
    } else if (window.getComputedStyle) {
        var compStyle = window.getComputedStyle(elem, "");
        return compStyle.getPropertyValue(CSSStyleProp);
    }
    return "";
}

function ShowHelp(id, linkId) {
    var link = document.getElementById(linkId);
    var pos = getElementPosition(linkId);
    var popup = document.getElementById(id);
    var posRight = pos.left + link.offsetWidth;
    
    popup.className = "lyrOn";
    popup.style.top =  pos.top + (link.offsetHeight -1) + "px";
    popup.style.left = (posRight - popup.offsetWidth) + "px";
    
    var HelpLeft = document.getElementById("HelpLeft");
    var HelpMiddle = document.getElementById("HelpMiddle");
    var HelpRight = document.getElementById("HelpRight");

    HelpLeft.style.background = "url(/images/theme/platform_theme.png) repeat 0 -288px";
    HelpMiddle.style.background = "url(/images/theme/platform_theme.png) repeat 0 -312px";
    HelpRight.style.background = "url(/images/theme/platform_theme.png) repeat -130px -360px";
    
    HelpMiddle.className = "list-item-small";
}

function ShowList(list_id, parent_id, align_right) {
    var parent = document.getElementById(parent_id);
    var pos = getElementPosition(parent_id);
    var posRight = pos.left + parent.offsetWidth;
    var list = document.getElementById(list_id);
    var paddingright = parseInt(getElementStyle(parent_id, "paddingRight", "padding-right"));

    list.className = "lyrOn";
    list.style.top = pos.top + parent.offsetHeight + "px";
    
    list.style.left =  pos.left + "px";
    list.style.width = (parent.offsetWidth - paddingright) + "px";
}

function ResetLink(id, use_arrow_img) {
    var LinkLeft = document.getElementById(id + "Left");
    var LinkMiddle = document.getElementById(id + "Middle");
    var LinkRight = document.getElementById(id + "Right");
    
    var HelpDiv = document.getElementById('help');
    HelpDiv.className = "lyrOff";
    
    LinkLeft.style.background = "url(/images/theme/platform_theme.png) repeat 0 -192px";
    LinkMiddle.style.background = "url(/images/theme/platform_theme.png) repeat scroll 0 -216px";
    
    if (use_arrow_img)
        LinkRight.style.background = "url(/images/theme/platform_theme.png) repeat -129px -264px";
    else
        LinkRight.style.background = "url(/images/theme/platform_theme.png) repeat -129px -264px";
    
    LinkMiddle.className = "button-text-small";
}

  
function getElementPosition(elemID) {// via JavaScript & DHTML Cookbook; retrieves top-left coordinates
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 && 
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return {left:offsetLeft, top:offsetTop};
}    

function validateURL(url) {
    var result = true;
    var regexp = new RegExp(); 
    
    regexp = /^(http|https|mms|rtsp|rtmp):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
    //regexp = /^(http|https):\/\/([\w-]+\.)+(\/[\w- .\/?%&=]*)?/
    if (!regexp.test(trimLR(url))) {
        result = false;
    }
    
    return result;
}

function RolloverTab(idx, action, template) {
    var tabSeparatorL = document.getElementById(idx.length == 0 ? template + "tab_separator" : "tab-separator-" + idx);
    var next_template;
    var ctl_idx;
    
    if (template != null) {
        ctl_idx = parseInt(template.substr((template.lastIndexOf("l") + 1)));
        ctl_idx++;

        if (ctl_idx < 10)
            ctl_idx = "0" + ctl_idx;

        next_template = "ctl00_Header_ctl" + (ctl_idx) + "_";
    }
    
    var tabSeparatorR = document.getElementById(idx.length == 0 ? next_template + "tab_separator" : "tab-separator-" +(Number(idx) + 1));
    
    action = (action == "on") ? "over" : action;
    
    document.getElementById(idx.length == 0 ? template + "tab_left" : "tab-left-" + idx).className = "tab-left-" + action;
    document.getElementById(idx.length == 0 ? template + "tab_middle" : "tab-middle-" + idx).className = "tab-middle-" + action;
    document.getElementById(idx.length == 0 ? template + "tab_right" : "tab-right-" + idx).className = "tab-right-" + action;

    document.getElementById(idx.length == 0 ? template + "tab_title" : "tab-title-" + idx).className = "tab-title-" + action;
    document.getElementById(idx.length == 0 ? template + "tab_subtitle" : "tab-subtitle-" + idx).className = "tab-subtitle-" + action;
        
    if (tabSeparatorL) {
        tabSeparatorL.className = "tab-separator-" + action;
    }
    if (tabSeparatorR) {
        tabSeparatorR.className = "tab-separator-" + action;
    }
}


function RolloverButton(idx, action, template) {
    document.getElementById(idx.length == 0 ? template + "nav_btn_left" : "nav-btn-left-" + idx).className = "nav-btn-left-" + action;
    document.getElementById(idx.length == 0 ? template + "nav_btn_middle" : "nav-btn-middle-" + idx).className = "nav-btn-middle-" + action;
    document.getElementById(idx.length == 0 ? template + "nav_btn_right" : "nav-btn-right-" + idx).className = "nav-btn-right-" + action;
    document.getElementById(idx.length == 0 ? template + "nav_btn_title" : "nav-btn-title-" + idx).className = "nav-btn-title-" + action;
    document.getElementById(idx.length == 0 ? template + "nav_btn_subtitle" : "nav-btn-subtitle-" + idx).className = "nav-btn-subtitle-" + action;

    var beta_tag = document.getElementById("betatag");
    if (beta_tag != null) {
        beta_tag.style.color = (action == "on") ? "9bb742" : "4a6200";
    }
}

function ValidatePwd(un_id, pwd_id, pwd2_id, err_msg_id) {
    var err_msg = document.getElementById(err_msg_id);
    var pwd = document.getElementById(pwd_id).value;
    var pwd2 = document.getElementById(pwd2_id).value;
    var username = document.getElementById(un_id).value;
    var err_text = "";
    var regexp = new RegExp();
   
    err_msg.innerHTML = "";
    err_msg.style.display = "none";
        
    // 8 char min, 1 letter, 1 number
 //   regexp = /(?=^.{8,}$)(?=.*[A-Za-z])(?=.*[\d]).*/
 //   result = (regexp.test(pwd));
 //   
 //   if (result) {
 //       // do not allow 4+ consecutive characters 
 //       regexp = /(\S)\1{3,}/   
 //       result = (!regexp.test(pwd));
 //   }

    
    regexp = /(?=^.{8,}$).*/
    if (!regexp.test(pwd)) {
        err_text += (err_text.length > 0 ? "<br/>" : "") + "Passwords must be a minimum of 8 characters.";
    }

    regexp = /(?=.*[A-Za-z]).*/
    if (!regexp.test(pwd)) {
        err_text += (err_text.length > 0 ? "<br/>" : "") + "Passwords must contain at least 1 letter.";
    }

    regexp = /(?=.*[\d]).*/
    if (!regexp.test(pwd)) {
        err_text += (err_text.length>0?"<br/>":"")+ "Passwords must contain at least 1 number.";
    }
       
    regexp = /(\S)\1{3,}/
    if (regexp.test(pwd)) {
        err_text += (err_text.length>0?"<br/>":"") + "Password cannot contain 4 or more consecutive letters or numbers.";
    }
    
    if (pwd.toLowerCase() == username.toLowerCase()) {
        err_text += (err_text.length > 0 ? "<br/>" : "") + "Password must be different from your user ID.";
    }
    
    if (pwd.toLowerCase() != pwd2.toLowerCase()) {
        err_text += (err_text.length > 0 ? "<br/>" : "") + "Passwords do not match.";
    }
  
    if (err_text.length > 0) {
        err_msg.innerHTML = err_text;
        err_msg.style.display = "";
        return false;
    }
    
    return true;
}

function calc_footer_height(id, min_height) {
    var e = getElementPosition(id);
    var top = e.top;
    var h = new Number();
    var availHeight = screen.height - min_height;
    
    //if (window.innerHeight)  
    //    availHeight = window.innerHeight - 16;
    //else
    //    availHeight = document.body.clientHeight - 20;

    if ((top > availHeight) || ((availHeight - top) < min_height))
        h = min_height;
    else
        h = (availHeight - top);
    
    return h;
}

function setFooterHeight(id, min_height) {
    document.getElementById(id).style.height = calc_footer_height(id, min_height) + "px";
}

function GetPageCount(id) {
    Ajax("/Ajax/UniversalConfiguration.aspx", "action=pgcount&mid=" + id);
    var uc = eval('(' + xmlHTTP.responseText + ')');

    return Number(uc[0].pagecount);
}

function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}