var d = window.document;

//////////////////////////////////////
//  GENERIC RETURN ELEMENT FUNCTION


function getE( v ) {
  var d = window.document;
  e = false;
  if ( d.getElementById ) {
    e = d.getElementById( v );
  }
  else if ( d.all ) {
    e = d.all[ v ];
  }
  return e;
}


	/* ------------------- Accordian Function ---------------------------- */
	
function popThisAccordian(e) {
	shrinkAllAccordians();
	
	var accordiansShrunk = checkAllAccordians();

	if(accordiansShrunk == true) {
		var targetP = e.nextSibling;
		if(targetP.nodeType != 1) {
			targetP = targetP.nextSibling;
		}
		
		if( targetP.style.display != "block" ) {
			targetP.style.display = "block";
		}
		
		var maxHeight = targetP.offsetHeight;
		// For some reason if I set it to 0px, the DOM didn't recognize the object
		targetP.style.height = "1px";
		timer = setTimeout(function() {expandAccordian(targetP, maxHeight)}, 32);
	}
}


function checkAllAccordians() {
	var pElements = document.getElementsByTagName("p");
	
	for(i=0; i<pElements.length; i++) {
		if( pElements[i].className == "initialHide" ) {
			if(pElements[i].style.diplay == "none") {
				return false;
			}
		}
	}
	
	return true;
}


function shrinkAllAccordians() {
	var pElements = document.getElementsByTagName("p");
	
	// Sinc Javascript doesn't know much info until it interacts with the page ... these 5 lines teach it.
	if(document.getElementById("initialShow") != null) {
		var intialShowingDiv = document.getElementById("initialShow");
		if(intialShowingDiv.style.display = "block") {
			intialShowingDiv.style.height = intialShowingDiv.offsetHeight + "px";
			intialShowingDiv.id = null;
		}
	}
	
	for(i=0; i<pElements.length; i++) {
		if( pElements[i].className == "initialHide" ) {
			var cellHeight = pElements[i].style.height;
			cellHeight = parseFloat(cellHeight.replace("px", ""));
			
			if(cellHeight > 0) {
				openTarget = pElements[i];
				shrinkTimer = setTimeout( function() { shrinkAccordian(openTarget) }, 32 );
			}
		}
	}
}

function expandAccordian(e, maxHeight) {
	var currentHeight = e.style.height;
	currentHeight = currentHeight.replace("px", "");
	var multiplier = 1.5;
	var newHeight = (parseFloat(currentHeight) * multiplier);
	
	if( newHeight >= maxHeight) {
		e.style.height = maxHeight + "px";
		clearTimeout(timer);
	} else {
		e.style.height = Math.round(newHeight) + "px";
		timer = setTimeout( function() { expandAccordian(e, maxHeight) }, 32 );
	}
}


function shrinkAccordian(e) {
	var currentHeight = e.style.height;
	currentHeight = currentHeight.replace("px", "");
	var multiplier = .7;
	var newHeight = (parseFloat(currentHeight) * multiplier);
	
	if( newHeight <= 2) {
		e.style.display = "none";
		e.style.height = null;
		clearTimeout(shrinkTimer);
	} else {
		e.style.height = newHeight + "px";
		shrinkTimer = setTimeout( function() { shrinkAccordian(e) }, 32 );
	}
}


function calcSharesToDollars(pricePerShare)
{
	ed = getE('equvDollars');
	ssVal = getSelectListVal('shareSelect');
	ssVal = ssVal*1;
	edVal = ssVal*pricePerShare;
	ed.value = formatAsMoney(edVal);
}

function calcDollarsToShares(pricePerShare)
{
	es = getE('equivShares');
	dv = getE('dollarVal');
	dvVal = dv.value * 1;
	decimals = 6;
	roundFactor = Math.pow(10,decimals);
	esVal = Math.round(dvVal/pricePerShare*roundFactor)/roundFactor;
	es.value = esVal;
}


function formatAsMoney(mnt) 
{
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);
}

///////////////////////////////
// RADIO CHECKED VALUES      //
///////////////////////////////

function setCheckedValue(radioObj, newValue) 
{
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) 
	{
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++)
	{
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) 
		{
			radioObj[i].checked = true;
		}
	}
}
function getCheckedValue(radioObj) 
{
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) 
	{
		if(radioObj[i].checked) 
		{
			return radioObj[i].value;
		}
	}
	return "";
}

//////////////////////////////
// SELECT LIST VALUES       //
//////////////////////////////

function setSelectListVal(selectID,setVal) 
{
	sel = getE(selectID);
	for (i=0; i<sel.options.length; i++) 
		if (sel.options[i].value == setVal)
			sel.selectedIndex = i;
}

function setTextValue(selectID, setVal) {
	sel = getE(selectID);
	sel.value = setVal;
}

function getSelectListVal(selectID)
{
	sel = getE(selectID);
	index = sel.selectedIndex;
	return sel[index].value;
}

function getSelectListText(selectID)
{
	sel = getE(selectID);
	index = sel.selectedIndex;
	return sel[index].text;
}



	/* ----------------------- TroubleTicket Validation ------------------------- */
	
function checkRequiredFields(e) {
	totalInputs = e.length;
	valid = true;
	
	for(i=0; i<totalInputs; i++) {
		if(e[i].getAttribute("required") != null) {
			if(e[i].value == "") {
				e[i].style.border = "2px solid red";
				valid = false;
			} else {
				e[i].style.border = "1px solid #111111";
			}
		}
	}
	
	return valid;
}
