//Java Script Document
var _d=document;
var isSummary = true;
var currentChar = null;
var sortByComp = true;
function gId(o) {
	return _d.getElementById(o);
}
function tabTo(n) {
	for(i=0;i<=4;i++) {
		tblnm=gId('tabl'+i);
		tbmnm=gId('tabm'+i);
		tbrnm=gId('tabr'+i);
		if(n==i) {
			tblnm.className="tab_left_on";
			tbmnm.className="tab_mid_on";
			tbrnm.className="tab_right_on";
			showDiv('dd'+n);
		} else {
			tblnm.className="tab_left";
			tbmnm.className="tab_mid";
			tbrnm.className="tab_right";
			hideDiv('dd'+i);
		}
	}
	if(n != 0)
		isSummary = false;
}
function showDiv(divnm) {
	gId(divnm).style["display"]="block";
	gId(divnm).style["visibility"]="visible";
}
function hideDiv(divnm) {
	gId(divnm).style["display"]="none";
	gId(divnm).style["visibility"]="hidden";
}
function showHideLayer(layerid, flag) {
	var tipPopup = document.getElementById(layerid);	
	tipPopup.style.display = (flag) ? 'block' : 'none';
}
function getSuggestions(query) {
	var ele = document.getElementById('executives');	
	var len = execs.length;
	var matches = '';
	var executive = null;
	var showExecutive = false;	
	query = query.trim();	
	for(var i=0; i<len; i++) {
		showExecutive = false;
		executive = execs[i].split('@');	
		if(query.length == 0) {
			showExecutive = true;
		} 
		else {			
			var exc = executive[0].toUpperCase().split(/\s/g);
			var comp = executive[1].toUpperCase();
			var excLen = exc.length;
			var que = query.toUpperCase().split(/\s/g);
			var ln = '';
			var fn = '';
			
			ln += exc[0];
			if(excLen == 2) {
				fn += exc[1];
			} else if(excLen > 2) {
				ln += " ";
				ln += exc[1];
				for(var k=2;k<excLen;k++) {
					if(k != 2) 
						fn += " ";
					fn += exc[k];					
				}				
			}
			
			if(comp.indexOf(query.toUpperCase()) == 0) {
				showExecutive = true;
			}
			
			if(que.length > 1) {
				if(((ln.indexOf(que[0]) == 0 || fn.indexOf(que[0]) == 0) &&
					(ln.indexOf(que[1]) == 0 || fn.indexOf(que[1]) == 0)) ||
					(ln.indexOf(que[0] + " " + que[1]) == 0) ||
					(fn.indexOf(que[0] + " " + que[1]) == 0)) {
					showExecutive = true;
				}
			} else {
				if(ln.indexOf(que[0]) == 0 || fn.indexOf(que[0]) == 0) {
					showExecutive = true;
				}
			}
		}
		if(showExecutive) {			
			matches += "<div class=\'exec-div\'  onClick=\"goToPage('" + executive[3] + "')\">";
			matches += "<a href=\"javascript:goToPage('" + executive[3] + "')\" class=\'execlink\'>";
			matches += executive[0] + "</a>";			
			matches += "<br/><span class=\'exec_info\'>" + executive[1] + "</span></div>";
		}
	}
		
	ele.innerHTML = matches;
}

function setTextBoxFocus() {
	document.getElementById('sugg_input').focus();
}
function setThisOn(obj, onFlag) {
	if(onFlag) {
		obj.value = '';
	} else if (obj.value.length == 0) {
		obj.value = 'Search for a CEO or Company';
		getSuggestions('');
	}
}
function getCEOSuggestions(obj) {
	getSuggestions(obj.value);	
}
function changeCompensation(type) {	
	var grantedFl = 'none';
	var takehomeFl = 'none';
	var expensedFl = 'none';
	if(type.value == 'granted')
		grantedFl = 'block';
	if(type.value == 'takehome')
		takehomeFl = 'block';
	if(type.value == 'expensed')
		expensedFl = 'block';
	
	if(!isSummary)
		tabTo('0');
	gId('total_granted').style.display = grantedFl;
	gId('stock_granted').style.display = grantedFl;
	gId('options_granted').style.display = grantedFl;
	gId('options_ti_granted').style.display = grantedFl;
	gId('stock_ti_granted').style.display = grantedFl;
	gId('graph_granted').style.display = grantedFl;
	
	gId('total_takehome').style.display = takehomeFl;
	gId('stock_takehome').style.display = takehomeFl;
	gId('options_takehome').style.display = takehomeFl;
	gId('options_ti_takehome').style.display = takehomeFl;
	gId('stock_ti_takehome').style.display = takehomeFl;
	gId('graph_takehome').style.display = takehomeFl;
	
	gId('total_expensed').style.display = expensedFl;
	gId('stock_expensed').style.display = expensedFl;
	gId('options_expensed').style.display = expensedFl;
	gId('options_ti_expensed').style.display = expensedFl;
	gId('stock_ti_expensed').style.display = expensedFl;
	gId('graph_expensed').style.display = expensedFl;
}
function resetPage() {
	gId('granted').checked="checked";
	var inp = gId('ceo_suggestion');
	inp.value = '';
	setThisOn(inp, false);
}
function goToPage(page) {
	window.location.href = page;
}
function showAllExecutives(bycomp) {
	for(i=0;i<=4;i++) {
		hideDiv('dd'+i);
	}
	gId("executive_positions").innerHTML = '';
	gId("executive_bio").innerHTML = '';
	gId("methodology_section").style.visibility = 'hidden';	
	gId("allExecutives").style.display = '';
	gId("AllExecsLink").style.display = 'none';
	currentChar = null;
	showExecs(bycomp); 
}
function showExecs(bycomp) {	
	var theText = '';
	var executive = null;
	var ele = document.getElementById('executives');	
	var sortedExecs = bycomp ? execsbycompany : execsbylastname;
	var len = sortedExecs.length;
	var matchFound = false;
	theText += "<table id='indextab' align='center' width='100%' cellpadding='0' cellspacing='0'>";
	theText += "<tr><td width='45%' align='center'><a href='javascript:showExecs(true)'><b>Company</b></a></td>"
	theText += "<td width='55%' align='center'><a href='javascript:showExecs(false)'><b>CEO</b></a></td></tr>";
	
	for (var i=0; i<len; i++) {
		matchFound=false;
		executive = sortedExecs[i].split('@');

		if (currentChar != null) {						
			if (currentChar == 'A') {
				if((/^[\d]/).test(executive[1]) || (currentChar) != executive[1].charAt(0)) {				
					continue;
				}
			}
			else if (currentChar == '0-9') {
				for(j=0; j<=9; j++) {
					if (j == executive[1].charAt(0)) {
						matchFound = true;
						break;
					} 
				}
				if (matchFound == false) {					
					continue;
				}
			}
			else if ((currentChar != executive[1].charAt(0))) {					
				continue;
			}
		}
		theText += "<tr onmouseover=\"this.bgColor='#C8D8F8'\" onmouseout=\"this.bgColor='#FFFFFF'\">";
		theText += "<td><a href=\"javascript:goToPage('" + executive[4] + "')\" class=\'index-exec\'>";
		theText += executive[1] + " (" + executive[2] + ")</a></td>";			
		theText += "<td><a href=\"javascript:goToPage('" + executive[4] + "')\" class=\'index-exec\'>";
		theText += executive[0] + "</a></td></tr>";
	}
	theText += "</table>";
	gId("exec_list").innerHTML = theText;
	sortByComp = bycomp;	
}
function findExecs(ch) {
	currentChar = ch;
	showExecs(sortByComp);
}
String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
function doNothing(){}//href requisite call
String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

