/* A few presentation-oriented functions will go here */
             
document.getElementsByClassName = function(name) {
  var results = new Array();
  var elems = document.getElementsByTagName("*");
  for (var i=0; i<elems.length; i++) {
    if (elems[i].className.indexOf(name) != -1) {
      results[results.length] = elems[i];
    }
  }
  return results;
}

sprint_addLoadEvent = function(func) { // allows multiple window.onload - http://simon.incutio.com/archive/2004/05/26/addLoadEvent
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
                                                                      
popUp = function(page, name, w, h, scroll) { 
	name = "foo"; page = page.split(" ").join("%20"); var winl = (screen.width - w) / 2; var wint = (screen.height - h) / 2; winprop  = 'height=' + h + ', width=' + w + ', top=' + wint + ', left=' + winl + ', scrollbars=' + scroll + ', resizable=yes';	win = window.open(page, name, winprop);	win.focus(); 
}

prepareSuperToolTip = function() {
	var concealeded = document.getElementsByClassName('concealed');
	var revealeded = document.getElementsByClassName('revealed');
	if (concealeded || revealeded) {
		for (var i = 0; i < concealeded.length; i++) {
			concealeded[i].onmousedown = function() { sprint_superToolTip(this); }
		}
		for (var i = 0; i < concealeded.length; i++) {
			revealeded[i].onmousedown = function() { sprint_superToolTip(this); }
		}
	} 
}

prepareCloserIcon = function() {
	var element = document.getElementsByClassName('closer');
	if (element) {
		for (var i = 0; i < element.length; i++) {
			element[i].onmousedown = function() { sprint_superToolTipHide() }
		}
	} 
}

function closer() {
	var revealeded = document.getElementsByClassName('revealed');
	var revealedSuperToolTips = document.getElementsByClassName('layerRevealed');
	if (revealeded) {
		for (var i = 0; i < revealeded.length; i++) {
			revealeded[i].className='concealed';
			revealeded[i].parentNode.parentNode.className = 'superToolTip';
			revealedSuperToolTips[i].className = 'superToolTip';
		}
    } 
}

sprint_superToolTipHide = function() {
	var revealeded = document.getElementsByClassName('revealed');
	var revealedSuperToolTips = document.getElementsByClassName('layerRevealed');
	if (revealeded) {
		for (var i = 0; i < revealeded.length; i++) {
			revealeded[i].className='concealed';
			revealeded[i].parentNode.parentNode.className = 'superToolTip';
			revealedSuperToolTips[i].className = 'superToolTip';
		}
    } 
}

sprint_superToolTip = function(what) {
	if (what.className == "concealed") { 
		sprint_superToolTipHide();
		what.className = "revealed";
		what.parentNode.parentNode.className += ' layerRevealed';
	} else {
		what.className='concealed';
		what.parentNode.parentNode.className = 'superToolTip';
	}
}

ubpAccountFlip = function() {
	var ubpAccountFlip = document.getElementById("ubpAccountFlip");
	if (ubpAccountFlip) {
		var accountIdentification = document.getElementById('accountIdentification');
		accountIdentification.appendChild(ubpAccountFlip);
	}
}

renderGasTank = function() {
	var tankWeight = document.getElementById("tankWeight");
	if (tankWeight) {
		var tankGauge = document.getElementById("tankGauge");
		var tankWeightValue = tankGauge.getAttribute("value");
		tankWeight.style.width = tankWeightValue + '%';
		var spendingLimitGraph = document.getElementById("spendingLimitGraph")
		if (tankWeightValue <= 30) spendingLimitGraph.className = "amountInGreen lefted";
		if (tankWeightValue >= 31) spendingLimitGraph.className = "amountInGreen middled";
		if (tankWeightValue >= 70) spendingLimitGraph.className = "amountInGreen righted";
		if (tankWeightValue >= 81) spendingLimitGraph.className = "amountInRed righted";
		if (tankWeightValue >= 100) { tankWeight.style.width = '101%'; }
		if (tankWeightValue < 1) { tankWeight.style.backgroundPosition = "-100px -100px"; }
	}
}

togglePassword = function(hidingWhat,showingWhat,convertingToText) {
	toggleVis = function(hidingWhat,showingWhat) {
		document.getElementById(showingWhat).style.display = "";
		document.getElementById(hidingWhat).style.display = "none";
	}
	toggleVis(hidingWhat,showingWhat);
	if (convertingToText) { document.getElementById(showingWhat).focus(); } 
	else { document.getElementById(showingWhat).value = document.getElementById(hidingWhat).value; }
}

toggleErrors = function(what,activated) {
	var allErrorMesages = document.getElementsByClassName('errorMessage');
	if (activated) { 
		//for (var i = 0; i < allErrorMesages.length; i++) {
		//	allErrorMesages[i].style.opacity = ".65";
		//	allErrorMesages[i].style.filter = "alpha(opacity=65)"; // IE-only
		//}
		what.style.zIndex = '2';
		//what.style.opacity = "";
		//what.style.filter = "alpha(opacity=100)"; // IE-only
	} else {
		//for (var i = 0; i < allErrorMesages.length; i++) {
		//	allErrorMesages[i].style.opacity = "1";              
		//	allError3Mesages[i].style.filter = "alpha(opacity=100)"; // IE-only
		//}
		what.style.zIndex = '1' 
	}
}

focusFormElement = function(where) {
	document.getElementById(where).select();
	document.getElementById(where).style.backgroundColor='#ffffcc';
}
                             
/* <BlindDownUp> */
    
	var sliderHeight = "";
	var divToSlide = "";
	var fullSlideHeight = "";
	var sliderSpeed=15;  // Time between frames.  10 is really fast. 100 is really slow.
	var sliderAccel=1.8; // Acceleration frames.  From "many" 1.1 to a "few" fast 3.	

	function blindingDown(height) {
		if (height<=fullSlideHeight) {
			document.getElementById(divToSlide).style.height = height+'px';
			if (height<(fullSlideHeight/2)) { sliderHeight=Math.round(height*sliderAccel)+1; }
			else { sliderHeight=fullSlideHeight-Math.round((fullSlideHeight-height)/sliderAccel)+1; }
			setTimeout("blindingDown(sliderHeight)",sliderSpeed);
		}
		else {
			document.getElementById(divToSlide).style.height = "auto";	
			sliderHeight="";			
			divToSlide="";
			fullSlideHeight="";
		}
	}
	function blindDown(objId) {
		if (document.getElementById(objId).style.display != 'none') { return; }
		document.getElementById(objId).style.overflow = 'hidden';	
		document.getElementById(objId).style.height = '1px';		
		document.getElementById(objId).style.display = '';		
		fullSlideHeight=document.getElementById(objId).scrollHeight;		
		divToSlide=objId;											
		blindingDown(1);												
		return;
	}
	function blindingUp(height) {	
		document.getElementById(divToSlide).style.height = height+'px';	
		if (height>0) {												
			if (height>(fullSlideHeight/2)) { sliderHeight=Math.round(fullSlideHeight-(fullSlideHeight*sliderAccel)+(height*sliderAccel))-1; if (sliderHeight<(fullSlideHeight/2)) { sliderHeight=Math.round(fullSlideHeight/2)-1; } }
			else { sliderHeight=Math.round(height/sliderAccel)-1; }
			setTimeout("blindingUp(sliderHeight)",sliderSpeed);			
		}
		else { document.getElementById(divToSlide).style.display = 'none'; sliderHeight=""; divToSlide=""; fullSlideHeight=""; }
	}
	function blindUp(objId) {
		if (document.getElementById(objId).style.display == 'none') { return; }
		document.getElementById(objId).style.overflow = 'hidden';	
		fullSlideHeight=document.getElementById(objId).scrollHeight;		
		if (!fullSlideHeight) { fullSlideHeight=document.getElementById(objId).offsetHeight; }
		fullSlideHeight-=10;												
		divToSlide=objId;											
		blindingUp(fullSlideHeight-1);									
		return;
	}
	function toggleBlind(objname){ if(document.getElementById(objname).style.display == "none") { blindDown(objname); } else{ blindUp(objname); } }

/* </BlindDownUp> */



/* <BookmarkletTools> */
	sprint_show10x7grid = function() {
		var uxdGridDiv = document.createElement('div');
		uxdGridDiv.setAttribute('id','uxdGrid');
		uxdGridDiv.setAttribute('onclick','this.style.display = "none"');
		document.body.appendChild(uxdGridDiv);
	}
	sprint_showMeasureGrid = function() {
		var uxdMeasureGrid = document.createElement('div');
		uxdMeasureGrid.setAttribute('id','uxdMeasureGrid');
		uxdMeasureGrid.setAttribute('onclick','this.style.display = "none"');
		document.body.appendChild(uxdMeasureGrid);
	}
/* </BookmarkletTools> */

function sprint_fixIEselectBug() {
	// Fixes text-selection bug in IE
	document.body.style.height = document.documentElement.scrollHeight + 'px';
}

sprint_addLoadEvent(ubpAccountFlip);
sprint_addLoadEvent(renderGasTank);
sprint_addLoadEvent(prepareSuperToolTip);
sprint_addLoadEvent(prepareCloserIcon);

/*@cc_on
	@if (@_win32)
		sprint_addLoadEvent(sprint_fixIEselectBug);
	@end
@*/

