var root;


function IsEmpty(val) {
	for (i=0; i<val.length; i++) { 
		if (val.charAt(i) != ' ') return false
	}
	return true
}

function IsValidEmail(val) {
	var iLen = val.length;
	if 	((iLen < 6) || (val.indexOf('@') < 1) || ((val.charAt(iLen - 3) != '.') && (val.charAt(iLen - 4) != '.')) ) return false
	return true
}

function formVal(){
	var msg = "";
	var typeList = "text,password,select-one";
	//curForm = document.getElementById("form");
	curForm = document.form;
	for (var i=0; i<curForm.elements.length; i++){
		tag = curForm.elements[i];
		if (typeList.indexOf(tag.type) >= 0 && tag.getAttribute('required') == 1 && tag.value == "") {
			msg = msg + "<li>" + tag.title + " </li>";
		};
		if (tag.name.indexOf("email") >= 0  && tag.value.length != 0) {
			flag = IsValidEmail(tag.value);
			if (!flag) {
				msg = msg + "<li>" +  tag.title + " is invalid</li>";
			}
		};
		if (tag.type == "password" && tag.value.length > 0  && tag.value.length < 4) {
			msg = msg + "<li>" + "Password is too short</li>";
		}
		if (tag.name == "password2" && tag.value != "" && tag.value != curForm.password.value)
			msg = msg + "<li>" + "Password confirmation</li>";
	};
	
	document.getElementById('err').innerHTML = "";
	if (msg != "") {
		document.getElementById('err').innerHTML = "The following fields are required: <ul>" + msg + "</ul>";
		window.scrollTo(0,10);
		return false;
	} else {
		return true;
	}
}


function openActivity(arg) {
	var w = 890;
	var h = 660;
	total = arguments.length;
	if (total > 0) {
		urls = "resources/presentation/" + arguments[0];
		if (total >= 2) {
			w = arguments[1];
		}
		if (total >= 3) {
			h = arguments[2];
		}
		window.open(urls, '', 'toolbar=no,scrollbars=yes,status=no,resizable=no,width='+w+',height='+h)
	}
}

/*************************************************************
	Custom
*************************************************************/ 

function init(web_root) {
	root = web_root;
	var loadimg0 = new Image;
	var loadimg1 = new Image;
	var loadimg2 = new Image;
	var loadimg3 = new Image;
	var loadimg4 = new Image;
	var loadimg5 = new Image;
	var loadimg6 = new Image;
	var loadimg7 = new Image;

	loadimg0.src = root + 'images/gnav/ggnav_about_f2.gif';
	loadimg1.src = root + 'images/gnav/gnav_attorney_f2.gif';
	loadimg2.src = root + 'images/gnav/gnav_practice_f2.gif';
	loadimg3.src = root + 'images/gnav/gnav_media_f2.gif';
	loadimg4.src = root + 'images/gnav/gnav_careers_f2.gif';
	loadimg5.src = root + 'images/gnav/gnav_seminar_f2.gif';
	loadimg6.src = root + 'images/gnav/gnav_probono_f2.gif';
	loadimg7.src = root + 'images/gnav/gnav_office_f2.gif';
	
	var snav_overview_on = new Image;
	
	var snav_news_on = new Image;
	var snav_newsletter_on = new Image;
	var snav_overview_off = new Image;
	var snav_news_off = new Image;
	var snav_newsletter_off = new Image;	
}

function changeImage(id, image) {
	document.getElementById(id).src = root + 'images/gnav/' + image;
	//document.getElementById(id).src =  image;
}

function subPageTitle(str) {
	if (document.getElementById("subPageTitle")) {
		//document.getElementById("subPageTitle").style.display = "";
		document.getElementById("subPageTitle").innerHTML = str;
	}
}

// for search form;
function checkAll(id){
	var curForm = document.getElementById("form");
	var curList = curForm.elements["ids"];
 
	for (i = 1; i < curList.length; i++) {
		if (curList[i].checked) {
			curList[0].checked = false;
		} else if (curList[0].checked) {
			curList[i].checked = false;
		}
	}
}

/*************************************************************
	END OF CUSTOM FUNCTIONS
*************************************************************/ 





/*************************************************************
	DHTML Menu Functions
*************************************************************/ 
// DHTML dropdown menu functions;
function Browser() {
	var ua, s, i;
	this.version = 0;
	this.ok = false;
	
	ua = ua=navigator.userAgent.toLowerCase();
	
	this.isOpera = ua.indexOf('opera') != -1;
	this.isKonq = ua.indexOf('konqueror') != -1;
	this.isIE = ua.indexOf('msie') != -1;
	if (this.isIE) this.version = parseFloat(ua.substr(ua.indexOf('msie') + 4));
	this.isAol = ua.indexOf('aol') != -1;
	this.isNav = ua.indexOf('netscape6/') != -1;
	if (this.isNav) this.version = parseFloat(ua.substr(ua.indexOf('netscape6/') + 10));
	this.isGecko = ua.indexOf('gecko') != -1;
	if (this.isGecko){
		this.isNav = true;
		this.version = 6.1;
	}
	this.isSafari = ua.indexOf("safari") != -1;
	
	this.isMac = ua.indexOf('mac') != -1;
	this.isWin = ua.indexOf('win') != -1;
	this.isLin = !(this.bMac || this.bWin);
	
	this.isMacIE = this.isIE && this.isMac;
	
	this.ok = 	(this.isIE && this.isWin && this.version >= 4) || 
				(this.isIE && this.isMac && this.version >= 5) || 
				(this.isNavigator && this.version >= 5) || 
				(this.isGecko) || 
				(this.Safari);
	//alert(this.ok);
}

var browser = new Browser();

//----------------------------------------------------------------------------
// Code for handling the menu bar and active button.
//----------------------------------------------------------------------------
var activeButton = null;
function dmRootButtonGo(event, menuId, xOffset, yOffset) {
	var button;
	// Get the target button element.
	if (browser.isIE) button = window.event.srcElement;
	else button = event.currentTarget;
	
	button.blur();		// Blur focus from the link to remove that annoying outline.

	// Associate the named menu to this button if not already done.
	// Additionally, initialize menu display.
	if (button.menu == null) {
		button.menu = document.getElementById(menuId);
		if (button.menu.isInitialized == null) menuInit(button.menu);
	}

	// Set mouseout event handler for the button, if not already done.	
	if (button.onmouseout == null) button.onmouseout = buttonOrMenuMouseout;
	
	// Exit if this button is the currently active one.
	if (button == activeButton) return false;

	// Reset the currently active button, if any.
	if (activeButton != null) dmResetRootButton(activeButton);
	
	// Activate this button, unless it was the currently active one.
	if (button != activeButton) {
		dmDepressRootButton(button, xOffset, yOffset);
		activeButton = button;
	} else activeButton = null;
	
	return false;
}

function dmRootButtonOver(event, menuId, xOffset, yOffset) {
	var button;
	
	if (!browser.ok) return;
	bMouseOut = false;
	
	if (!xOffset) xOffset = 0;
	if (!yOffset) yOffset = 0;

	// Activates this button's menu if no other is currently active.
	if (activeButton == null) {
		dmRootButtonGo(event, menuId, xOffset, yOffset);
		return;
	}
	
	// Find the target button element.
	if (browser.isIE) button = window.event.srcElement;
	else button = event.currentTarget;
	
	// If any other button menu is active, make this one active instead.
	if (activeButton != null && activeButton != button) dmRootButtonGo(event, menuId, xOffset, yOffset);
}

function dmDepressRootButton(button, xOffset, yOffset) {
	var x, y;
	// Update the button's style class to make it look like it's depressed.
	// and opens the submenu
	
	//button.className += " dmRootButtonActive";
	
	// Set mouseout event handler for the button, if not already done.
	if (button.onmouseout == null) button.onmouseout = buttonOrMenuMouseout;
	if (button.menu.onmouseout == null) button.menu.onmouseout = buttonOrMenuMouseout;
	
	// Position the associated drop down menu under the button and show it.
	x = getPageOffsetLeft(button) + xOffset;
	y = getPageOffsetTop(button) + button.offsetHeight + yOffset;
	
	/*
	// For IE, adjust position.
	if (browser.isIE) {
		x += button.offsetParent.clientLeft;
		y += button.offsetParent.clientTop;
	}
	*/
	
	dmHideBleeders(button.menu.id);
	
	button.menu.style.left = x + "px";
	button.menu.style.top  = y + "px";
	button.menu.style.visibility = "visible";
}

function dmResetRootButton(button) {
	// Restore the button's style class.	
	//removeClassName(button, "dmRootButtonActive");

	// Hide the button's menu, first closing any sub menus.
	if (button.menu != null) {
		closeSubMenu(button.menu);
		dmUnHideBleeders(button.menu.id);
		button.menu.style.visibility = "hidden";
	}
}

//----------------------------------------------------------------------------
// Code to handle the menus and sub menus.
//----------------------------------------------------------------------------

function dmMenuMouseover(event) {
	var menu;
	
	bMouseOut = false;
	
	// Find the target menu element.
	if (browser.isIE) menu = getContainerWith(window.event.srcElement, "DIV", "dmMenu");
	else menu = event.currentTarget;
	
	// Close any active sub menu.
	if (menu.activeItem != null) closeSubMenu(menu);
}
function dmMenuMouseout(event) {
	var menu;
	
	bMouseOut = true;
	
	// Find the target menu element.
	if (browser.isIE) menu = getContainerWith(window.event.srcElement, "DIV", "dmMenu");
	else menu = event.currentTarget;
	
	// Close any active sub menu.
	closeSubMenu(menu);
}

function dmMenuItemMouseover(event, menuId) {
	var item, menu, x, y;
	// Find the target item element and its parent menu element.
	
	bMouseOut = false;
		
	if (browser.isIE) item = getContainerWith(window.event.srcElement, "A", "dmMenuItem");
	else item = event.currentTarget;
	menu = getContainerWith(item, "DIV", "dmMenu");
	
	// Close any active sub menu and mark this one as active.
	if (menu.activeItem != null) closeSubMenu(menu);
	menu.activeItem = item;
	
	// Highlight the item element.
	item.className += " dmMenuItemHighlight";
	
	// Initialize the sub menu, if not already done.
	if (item.subMenu == null) {
		item.subMenu = document.getElementById(menuId);
		if (item.subMenu.isInitialized == null) menuInit(item.subMenu);
	}

	// Set mouseout event handler for the sub menu, if not already done.
	if (item.subMenu.onmouseout == null) item.subMenu.onmouseout = buttonOrMenuMouseout;
		
	// Get position for submenu based on the menu item.
	x = getPageOffsetLeft(item) + item.offsetWidth;
	y = getPageOffsetTop(item);
	
	// Adjust position to fit in view.
	var maxX, maxY;
	
	if (browser.isNav) {
		maxX = window.scrollX + window.innerWidth;
		maxY = window.scrollY + window.innerHeight;
	} else if (browser.isMacIE) {
		maxX = document.body.scrollLeft + document.body.clientWidth;
		maxY = document.body.scrollTop + document.body.clientHeight;
	} else if (browser.isIE) {
		maxX = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft) +
		(document.documentElement.clientWidth != 0 ? document.documentElement.clientWidth : document.body.clientWidth);
		maxY = Math.max(document.documentElement.scrollTop, document.body.scrollTop) +
		(document.documentElement.clientHeight != 0 ? document.documentElement.clientHeight : document.body.clientHeight);
	}
	maxX -= item.subMenu.offsetWidth;
	maxY -= item.subMenu.offsetHeight;

	if (x > maxX) x = Math.max(0, x - item.offsetWidth - item.subMenu.offsetWidth + (menu.offsetWidth - item.offsetWidth));
	y = Math.max(0, Math.min(y, maxY));

	dmHideBleeders(item.subMenu.id);
	
	// Position and show the sub menu.
	item.subMenu.style.left = x + "px";
	item.subMenu.style.top  = y + "px";
	item.subMenu.style.visibility = "visible";

	// Stop the event from bubbling.
	if (browser.isIE) window.event.cancelBubble = true;
	else event.stopPropagation();
}

function closeSubMenu(menu) {
	if (menu == null || menu.activeItem == null) return;
	// Recursively close any sub menus.
	if (menu.activeItem.subMenu != null) {
		closeSubMenu(menu.activeItem.subMenu);
		dmUnHideBleeders(menu.activeItem.subMenu.id);
		menu.activeItem.subMenu.style.visibility = "hidden";
		menu.activeItem.subMenu = null;
	}
	removeClassName(menu.activeItem, "dmMenuItemHighlight");
	menu.activeItem = null;
}


var bMouseOut = false;
function buttonOrMenuMouseout(event) {
	var el;
	// If there is no active button, exit.
	if (activeButton == null) return;
	
	// Find the element the mouse is moving to.
	if (browser.isIE) el = window.event.toElement;
	else if (event.relatedTarget != null) el = (event.relatedTarget.tagName ? event.relatedTarget : event.relatedTarget.parentNode);
	
	// If the element is not part of a menu, reset the active button.
	if (getContainerWith(el, "DIV", "dmMenu") == null) {
		bMouseOut = true;
		setTimeout('buttonOrMenuMouseout_2();', 1500);
	}
}

function buttonOrMenuMouseout_2(){
	if (bMouseOut){
		bMouseOut = false;
		dmResetRootButton(activeButton);
		activeButton = null;
	}
}

//----------------------------------------------------------------------------
// Code to initialize menus.
//----------------------------------------------------------------------------
function menuInit(menu) {
	var itemList, spanList;
	var textEl, arrowEl;
	var itemWidth;
	var w, dw;
	var i, j;
	
	// Find the width of a menu item.
	itemList = menu.getElementsByTagName("A");
	if (itemList.length > 0) itemWidth = itemList[0].offsetWidth;
	else return;
	
	if (browser.isIE && browser.isMac){
		itemList[0].style.display = 'inline';
		iMaxItemWidth = itemList[0].offsetWidth;
		for (i=1; i < itemList.length; i++) {
			itemList[i].style.display = 'inline';
			iMaxItemWidth = Math.max(iMaxItemWidth, itemList[i].offsetWidth);
			itemList[i].style.display = 'block';
		}
		for (i=0; i < itemList.length; i++) itemList[i].style.width = iMaxItemWidth;
	} else if (browser.isIE) {
		// Fix IE hover problem by setting an explicit width on first item of the menu.	
		w = itemList[0].offsetWidth;
		itemList[0].style.width = w + "px";
		dw = itemList[0].offsetWidth - w;
		w -= dw;
		itemList[0].style.width = w + "px";
	}
	
	// Mark menu as initialized
	menu.isInitialized = true;
}


//----------------------------------------------------------------------------
// General utility functions.
//----------------------------------------------------------------------------

var colDmBleeders = new Object();
function dmHideBleeders(mnID){
	if (!colDmBleeders[mnID]) return;
	var arBleedIds = colDmBleeders[mnID];
	for (var i=0; i<arBleedIds.length; i++){
		document.getElementById(arBleedIds[i]).style.visibility = 'hidden';
	}
}

function dmUnHideBleeders(mnID){
	if (!colDmBleeders[mnID]) return;
	var arBleedIds = colDmBleeders[mnID];
	for (var i=0; i<arBleedIds.length; i++){
		document.getElementById(arBleedIds[i]).style.visibility = 'inherit';
	}
}

function getContainerWith(node, tagName, className) {
	// Starting with the given node, find the nearest containing element with the specified tag name and style class.
	while (node != null) {
		if (node.tagName != null && node.tagName == tagName && hasClassName(node, className)) return node;
		node = node.parentNode;
	}	
	return node;
}

function hasClassName(el, name) {
	var i, list;
	// Return true if the given element currently has the given class name.
	list = el.className.split(" ");
	for (i = 0; i < list.length; i++) if (list[i] == name) return true;
	return false;
}

function removeClassName(el, name) {
	var i, curList, newList;
	if (el.className == null) return;
	// Remove the given class name from the element's className property.
	newList = new Array();
	curList = el.className.split(" ");
	for (i = 0; i < curList.length; i++) if (curList[i] != name) newList[newList.length] = (curList[i]);
	el.className = newList.join(" ");
}

function getPageOffsetLeft(el) {
	var x;
	x = el.offsetLeft;
	if (browser.isSafari && getContainerWith(window.event.srcElement, "DIV", "dmMenu")){
		x -= document.getElementsByTagName('BODY').item(0).offsetLeft;
	} else if (browser.isMacIE && getContainerWith(window.event.srcElement, "DIV", "dmMenu")) {
		x += parseInt(document.body.currentStyle.marginLeft) - 8;
	}
	while (el.offsetParent != null){
		el = el.offsetParent;
		x += el.offsetLeft;
	}
	return x;
}

function getPageOffsetTop(el) {
	var y;
	y = el.offsetTop;
	//alert(el.nodeName+':'+el.offsetTop);
	if (browser.isSafari && getContainerWith(window.event.srcElement, "DIV", "dmMenu")){
		y -= document.getElementsByTagName('BODY').item(0).offsetTop;
	} else if (browser.isMacIE && getContainerWith(window.event.srcElement, "DIV", "dmMenu")) {
		y += parseInt(document.body.currentStyle.marginTop) - 6;
	}
	while (el.offsetParent != null){
		el = el.offsetParent;
		//alert(el.nodeName+':'+el.offsetTop);
		y += el.offsetTop;
	}
	return y;
}
// END OF DHTML DROPDOWN MENU;

/*************************************************************
	END OF DHTML MENU FUNCTIONS
*************************************************************/ 
 
// 2008.6.25, Ye Wang
// Toggle practice groups
function getNextSibling(obj) {	
	var nextNode = obj.nextSibling;
	while (nextNode.nodeType != 1) {
		nextNode = nextNode.nextSibling;
	}
	return nextNode;
} 	
function getPrevSibling(obj) {	
	var prevNode = obj.previousSibling;
	while (prevNode.nodeType != 1) {
		prevNode = prevNode.previousSibling;
	}
	return prevNode;
} 	
/*
function toggleList(obj) {  
	if (obj.tagName == "UL") {
		var targetObj = obj.children(1);
	} else {	
		var targetObj = getNextSibling(obj); 
	}   
 	if (obj.className == "close") {
		obj.className = "open";
		obj.title = "Collapse";
		targetObj.className =  "open";
	} else {
		obj.className = "close";
		obj.title = "Expand";
		targetObj.className = "close";
	}
} 
*/

function toggle(obj, id) {
	targetObj = document.getElementById(id);
	if (obj.className == "close") {
		obj.className = "open";
		obj.title = "Collapse Related Practices";
		targetObj.className = "open";
	} else {
		obj.className = "close";
		obj.title = "Expand Related Practices";
		targetObj.className = "close";
	}
}

function getElementsByClass(className) {
	var tagArray = new Array();
 	var targetTag = "*";
	if (arguments.length > 1) {
		targetTag = arguments[1];
	}  
	allTags = document.getElementsByTagName(targetTag); 
	for (i=0; i<allTags.length; i++) {
		curTag = allTags[i];
		if (curTag.className == className) {
			tagArray.push(curTag);
		}
	} 
	return tagArray;
} 


function toggleListAll(obj) {
	var newClass = "";
	var curClass = obj.className;
	var allTags = document.getElementsByTagName("UL");   
	var allTriggers = getElementsByClass(curClass, obj.tagName);	
	var allFlags = getElementsByClass(curClass.replace("All", ""), "A"); 
	
	if (curClass == "closeAll") {
		newState = "open";
		newTitle = "Collapse all";
		newClass = "openAll";
		newText = "Collapse All";
	} else {
		newState = "close";
		newTitle = "Expand all";
		newClass = "closeAll";
		newText = "Expand All"
	}	 
 
	for (i=0; i<allTriggers.length; i++) {
		curTag = allTriggers[i];
		curTag.title = newTitle;
		curTag.className = newClass;
		curTag.innerHTML = newText;
	}
	for (i=0; i<allFlags.length; i++) {
		curTag = allFlags[i];
		curTag.title = newTitle.replace(" all", "");
		curTag.className = newClass.replace("All", "");
		
	}
	for (i=0; i<allTags.length; i++) { 
		if (allTags[i].className == "close"
			|| allTags[i].className == "open") {
			allTags[i].className = newState;	
			getPrevSibling(allTags[i]).className = newState; 
		} 
	}	  
	window.scrollTo(0,10);
	
}