var IE = (document.all) ? true : false;
var DOM = (document.getElementById) ? true : false;
var NS4 = (document.layers) ? true : false;
var ie = IE;
var ns = NS4;

var shouldClose = false;
var currentMenu = null;
var menuTimer = null;

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function getElm(elmID) { return (IE) ? document.all[elmID] : (DOM) ? document.getElementById(elmID) : false; }

// setStyleByClass: given an element type and a class selector,
// style property and value, apply the style.
// args:
//  t - type of tag to check for (e.g., SPAN)
//  c - class name
//  p - CSS property
//  v - value
function setStyleByClass(t,c,p,v){
	var ie = (document.all) ? true : false;
	var elements;
	if(t == '*') {
		// '*' not supported by IE/Win 5.5 and below
		elements = (ie) ? document.all : document.getElementsByTagName('*');
	} else {
		elements = document.getElementsByTagName(t);
	}
	for(var i = 0; i < elements.length; i++){
		var node = elements.item(i);
		for(var j = 0; j < node.attributes.length; j++) {
			if(node.attributes.item(j).nodeName == 'class') {
				if(node.attributes.item(j).nodeValue == c) {
					eval('node.style.' + p + " = '" +v + "'");
				}
			}
		}
	}
}
function Show(what) {
	(IE)?eval(what + '.style.display="block";'):eval('document.getElementById("' + what + '").style.display="block"');
}
function Hide(what) {
	(IE)?eval(what + '.style.display="none";'):eval('document.getElementById("' + what + '").style.display="none"');
}
function ShowHide(what) {
	var I = (IE)?eval(what):document.getElementById(what);
	I.style.display = (I.style.display == '' || I.style.display == 'none')?"block":"none";
}

function action(elm,colOn,colOff){

  if (elm.checked) {
        colorRow(elm.value,colOn,'white');
        // use white as highlighted foreground color text
  } else {
        colorRow(elm.value,colOff,'black');
        // use black as default foreground color for text
  }
}
function colorRow(which,Bcolr,Fcolr){
	var whichRow = (IE) ? document.all[which] : (DOM) ? document.getElementById(which) : false;
	if (whichRow) {
	  if (IE){
	        for (var i=0;i<whichRow.children.length;i++){
	           whichRow.children[i].style.backgroundColor=Bcolr;
	           //whichRow.children[i].style.color = Fcolr;
	        }
	  } else if (DOM){
	        for (var i=0;i<whichRow.cells.length;i++){
	           whichRow.cells[i].style.backgroundColor=Bcolr;
	           //whichRow.cells[i].style.color = Fcolr;
	        }
	  }
	}
}
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}
function checkMaxLength(evt,txtbox,maxLength,isNumeric) {
	evt = (evt) ? evt : ((event) ? event : null);
	if (isNumeric) {
		if (evt.keyCode < 48 || evt.keyCode > 57) { evt.returnValue = false; } 
	}
	if (txtbox.value.length > maxLength) { evt.returnValue = false; } 
}
function trim(inputString) {
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") {
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") {
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) {
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length);
   }
   return retValue;
}
function isBlank(value) {
	if (value == null || value == "" || trim(value) == "") { return true; }
	else { return false; }
}
function IsNumeric(sText,extra) {
	var ValidChars = "0123456789" + extra;
	var IsNumber=true;
	var Char;
	for (i = 0; i < sText.length && IsNumber == true; i++) { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) { IsNumber = false; }
	}
	return IsNumber;
	
}

function IsTextual(sText,extra) {
	var ValidText = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-.@_#%+,:;<> /\\" + extra;
	var IsText = true;
	var Char;
	for (i = 0; i < sText.length && IsText == true; i++) { 
		Char = sText.charAt(i); 
		if (ValidText.indexOf(Char) == -1) { IsText = false; }
	}
	return IsText;
	
}
function isToLong(inputString,size) {
	if (!isBlank(inputString)) {
		if (inputString.length >= size) {
			return true;
		} else {
			return false;
		}		
	}
	return false;
}
function roundAmount(n) {
	var s = "" + Math.round(n * 100) / 100
	var i = s.indexOf('.')
	if (i < 0) return s + ".00"
	var t = s.substring(0, i + 1) + 
	s.substring(i + 1, i + 3)
	if (i + 2 == s.length) t += "0"
	return t
}
function setFocus(what) {
	var box = (IE) ? eval('document.all.' + what) : (DOM) ? document.getElementById(what) : false;
	if (box) {
		box.focus();
	}
}

function dispEmail(User,Domain,Subject,dispAs) {
	if (!Subject) Subject = "";
	if (!dispAs) dispAs = User + "@" + Domain;
	document.write("<a href='ma" + "il" + "to:" + User + "@" + Domain + "?subject=" + Subject + "'>" + dispAs + "</a>");
}

function getAsyncContent(content, page,callbackFunc,callbackSuccess,callbackFail){
	try {
		var xmlhttp;
		var useIE = false;
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
			useIE = true;
		} catch (E) {}
		if (useIE) { 
			xmlhttp.onreadystatechange = function() { 
				if (callbackFunc) {
					callbackFunc(xmlhttp);
				}
				if (xmlhttp && xmlhttp.readyState == 4 && xmlhttp.status == 200 && callbackSuccess) {
					callbackSuccess(xmlhttp);
				} else if (callbackFail && xmlhttp && xmlhttp.readyState == 4) {
					callbackFail(xmlhttp);
				}
			};
		} else { 
			xmlhttp = new XMLHttpRequest(); 
			xmlhttp.onload = function() { 
				if (callbackFunc) {
					callbackFunc(xmlhttp);
				}
				if (xmlhttp && xmlhttp.readyState == 4 && xmlhttp.status == 200 && callbackSuccess) {
					callbackSuccess(xmlhttp);
				} else if (xmlhttp && xmlhttp.readyState == 4 && callbackFail) {
					callbackFail(xmlhttp);
				}
			};
		}
		xmlhttp.open("POST",page,true);
		xmlhttp.send(content);
		
	} catch (e) { 
		alert('An error occured transmitting the request to the server:\n'+ e);
		return false;
	}
}

function readRSS(page,elmID,maxItems) {
	getElm(elmID).innerHTML = '<div class="loadingFeed">Loading feed...</div>';
	setTimeout('getAsyncContent(\''+page+'\', "rssFeedReader.aspx?n="+\''+maxItems+'\', null, function (xmlhttp) { getElm(\''+elmID+'\').innerHTML = xmlhttp.responseText; }, function (xmlhttp) { getElm(\''+elmID+'\').innerHTML = xmlhttp.responseText; } );',500);
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

var shouldClose = false;
var currentMenu = null;
var menuTimer = null;
function openMenu(which, atWhich) {
	shouldClose = false;
	if (currentMenu != null) { currentMenu.style.display='none'; }
	currentMenu = getElm(which);
	var tmp = findPos(atWhich);
	currentMenu.style.top = tmp[1];
	currentMenu.style.left = tmp[0];
	currentMenu.style.display='block';
}
function closeMenu() {
	shouldClose?currentMenu.style.display='none':false;
}
function forceCloseMenu() {
	currentMenu!=null?currentMenu.style.display='none':false;
}
function menuOut() {
	shouldClose = true;
	menuTimer = setTimeout("closeMenu()",500)
}
function menuOver() {
	shouldClose = false;
	if (menuTimer) clearTimeout(menuTimer);
}

function showAllManaged() {
    getElm('showHideAllManaged').innerHTML = '<a href="javascript:void(0);" onclick="hideAllManaged();">Hide All</a>';
    var x = 1;
    for (x=1;;x++) {
        try {
            Show('m'+x);
        } catch (E) {
            break;
        }
    }
}
function hideAllManaged() {
    getElm('showHideAllManaged').innerHTML = '<a href="javascript:void(0);" onclick="showAllManaged();">Show All</a>';
    var x = 1;
    for (x=1;;x++) {
        try {
            Hide('m'+x);
        } catch (E) {
            break;
        }
    }
}
function showAllBaseline() {
    getElm('showHideAllBaseline').innerHTML = '<a href="javascript:void(0);" onclick="hideAllBaseline();">Hide All</a>';
    var x = 1;
    for (x=1;;x++) {
        try {
            Show('b'+x);
        } catch (E) {
            break;
        }
    }
}
function hideAllBaseline() {
    getElm('showHideAllBaseline').innerHTML = '<a href="javascript:void(0);" onclick="showAllBaseline();">Show All</a>';
    var x = 1;
    for (x=1;;x++) {
        try {
            Hide('b'+x);
        } catch (E) {
            break;
        }
    }
}

function addToMailList() {
    if (typeof(Page_ClientValidate) == 'function') {
        if(Page_ClientValidate()) {
            mailList("add");
        }
    }
}
function mailList(action) {
    var c = trim(getElm('ctl00_bodyContent_mailinglistComment').value);
    var n = trim(getElm('ctl00_bodyContent_mailinglistName').value);
    var e = trim(getElm('ctl00_bodyContent_mailinglistEmail').value);
    getAsyncContent(c,'mailingList.aspx?action='+action+'&n='+n+'&e='+e,null,
        function(xmlhttp) {
            var resp = xmlhttp.responseText;
            if (resp == "true") {
                if (action == "add") {
                    getElm('mailingList').style.display = "none";
                    getElm('mailingListAddSuccess').style.display = "block";
                } else if (action == "remove") {
                    getElm('mailingList').style.display = "none";
                    getElm('mailingListRemoveSuccess').style.display = "block";
                }
            } else {
                if (action == "add") {
                    getElm('mailingList').style.display = "none";
                    getElm('mailingListAddFail').style.display = "block";
                    getElm('mailingListAddFailMessage').innerHTML = resp;
                } else if (action == "remove") {
                    getElm('mailingList').style.display = "none";
                    getElm('mailingListRemoveFail').style.display = "block";
                    getElm('mailingListRemoveFailMessage').innerHTML = resp;
                }
            }
        },
        function (xmlhttp) {
            alert("Error occured :(");
        }
    );
}
function removeFromMailList() {
    if (!isBlank(trim(getElm('ctl00_bodyContent_mailinglistEmail').value))) {
        mailList("remove");
    }
}
function showMailListForm() {
    getElm('mailingListAddFail').style.display = "none";
    getElm('mailingListRemoveFail').style.display = "none";
    getElm('mailingListRemoveSuccess').style.display = "none";
    getElm('mailingListAddSuccess').style.display = "none";
    getElm('mailingList').style.display = "block";
}