/**
 * @author Rebekah
 */
/**
 * Makes the div containing drop down links that is passed through visible
 * 
 * @param {Object} element
 */ 
function makeVisible(element) {

var links = document.getElementsByTagName("div");

for (var j = 0; j < links.length; j++) {
if (links[j].className == "Links") {
	links[j].style.display = "none";
	}
}

var linkDiv = document.getElementById(element);
linkDiv.style.display = "block";

}

/**
 * Makes drop down menus invisible
 */
function invisible() {
var divLinks = document.getElementsByTagName("div");

for (var i = 0; i < divLinks.length; i++) {
if (divLinks[i].className == "Links") {
	divLinks[i].style.display = "none";
	}
}
}

/**
 * Choose a form functions
 * 
 * @param {Object} ref
 * @param {Object} target
 */
function land(ref, target)
{
lowtarget=target.toLowerCase();
if (lowtarget=="_self") {window.location=loc;}
else {if (lowtarget=="_top") {top.location=loc;}
else {if (lowtarget=="_blank") {window.open(loc);}
else {if (lowtarget=="_parent") {parent.location=loc;}
else {parent.frames[target].location=loc;};
}}}
}
function jump(menu)
{
ref=menu.choice.options[menu.choice.selectedIndex].value;
splitc=ref.lastIndexOf("*");
target="";
if (splitc!=-1)
{loc=ref.substring(0,splitc);
target=ref.substring(splitc+1,1000);}
else {loc=ref; target="_self";};
if (ref != "") {land(loc,target);}
}
/**
 * Collapses info functions
 * @param {Object} section1 - button to turn on
 * @param {Object} section2 - button to turn off
 * @param {Object} element - what to toggle
 */
//See p407 of the Missing Manual

function ToggleVisibility(section1, section2, element){
	//Find the image.
	var section1 = document.getElementById(section1);
	var section2 = document.getElementById(section2);
	
	//Find the element to hide/unhide.
	var element = document.getElementById(element);
	
	//Check the element's current state.
	if (element.style.display != "block"){
		//If hidden, unhide it.
		element.style.display = "block";
		section1.style.display = "none";
		section2.style.display = "block";
	}
	else {
		//If not hidden, hide it.
		element.style.display = "none";
		section2.style.display = "none";
		section1.style.display = "block";
	}
}

/**
 * Mortgage calculator functions
 * 
 */
function floor(number)
{
  return Math.floor(number*Math.pow(10,2))/Math.pow(10,2);
}

function dosum()
{
  var mi = document.temps.IR.value / 1200;
  var base = 1;
  var mbase = 1 + mi;
  for (i=0; i<document.temps.YR.value * 12; i++)
  {
    base = base * mbase
  }
  document.temps.PI.value = floor(document.temps.LA.value * mi / ( 1 - (1/base)))
  document.temps.MT.value = floor(document.temps.AT.value / 12)
  document.temps.MI.value = floor(document.temps.AI.value / 12)
  var dasum = document.temps.LA.value * mi / ( 1 - (1/base)) +
	document.temps.AT.value / 12 + 
	document.temps.AI.value / 12;
  document.temps.MP.value = floor(dasum);
}

/*
*************************************************
********* JavaScript Implementation *************
*************************************************
Loan Calculator by Sidney Forcier
sf01@sidneyforcier.com
The formula for calculating loan payments is:
Pi / (q * (1 - (1 + (i / q))^(-nq)
Where the variables are as follows:
P - The amount of the loan (after down payment)
i - The interest expressed as a decimal (e.g. 8% is .08)
n - The number of term units (e.g. number of years)
q - The number of payments per term unit (e.g. 12 months)
You can enhance the formulas below by allowing the user to change the number of payments per year if you wish. I have hard-coded the payments to monthly.
*/
function CalculatePayments(principal, down_payment, interest, years)
{
var x = ((principal - down_payment) * interest / (12 * (1 - Math.pow(1 + (interest / 12), (-years * 12)))));
return Math.floor(x * 100) / 100
}
function ShowPayments()
{
var x = CalculatePayments(document.jsForm.jsPrincipal.value, document.jsForm.jsDownPayment.value, document.jsForm.jsInterest.value / 100, document.jsForm.jsYears.value);
if (isNaN(x))
document.jsForm.jsResult.value = 'Could not compute';
else
document.jsForm.jsResult.value = x;
}

 /**
  * Binds the MakeVisible function as the mouseover event to the element whose id is passed through
  * 
  * @param {Object} link The link that corresponds to the appearing div
  * @param {Object} element The element that will be passed through to the MakeVisible function
  */
 function bindMakeVisible(link, element){
 	if (!document.getElementById(link)){
		return false;
	}
	
	var chosenLink = document.getElementById(link);
	chosenLink.onmouseover = function(){
	makeVisible(element);	
	};
 }
 /**
  * Binds the Invisible function to the main-text element
  */
function bindInvisible(div){
	var area = document.getElementById(div);
	area.onmouseover=invisible;
	}

/**
 * Binds the jump function as the onclick event for "goButton"
 */
function bindJump(){
	if (!document.getElementById("goButton")) {
	return false;
	}
	var goButton = document.getElementById("goButton");
	goButton.onclick = function(){
		jump(this.form);
	};
}
/**
 * Binds the Toggle Visibility function to the "More or "Less" buttons
 */
function bindToggleVisibility(button){
	if(!document.getElementById(button)){
		return false;
	}
	var toggleButton = document.getElementById(button);
	toggleButton.onclick = function(){
		ToggleVisibility("more", "less", "info");
	}
}

/**
 * Binds the ShowPayments function to button1 on the calculator
 */
function bindShowPayments (){
	if(!document.getElementById("button1")){
		return false;
	}
	var calcButton = document.getElementById("button1");
	calcButton.onclick=ShowPayments;
}
/**
 * Binds the dosum function to button2 on the calculator
 */
function bindDoSum (){
	if(!document.getElementById("button2")){
		return false;
	}
	var calcButton = document.getElementById("button2");
	calcButton.onclick=dosum;
}

 /** 
 * Checks that the required methods, getElementById and
 * getElementsByTagName, are available.
 * 
 * Returns true if the methods are available, false if they aren't.
 */ 
function detectMethods() {
	if (!document.getElementById) {
        return false;
    }
    if (!document.getElementsByTagName) {
        return false;
    }
	return true;
}

/** 
 * Performs method detection, then binds the form 
 * validation event handler and loads the email address
 * cookie, if there is one.
 */ 
function bindCommonEventHandlers() {
    if (!detectMethods()) {
		return false;
	}
	//alert("bindingCommonEventHandlers");
	invisible();
	bindInvisible("main-text");
	bindInvisible("headLogo");
	
	bindMakeVisible("servicesMain", "Services");
	bindMakeVisible("locationsMain","Locations");
	
	bindToggleVisibility("more");
	bindToggleVisibility("less");
	
	bindShowPayments();
	bindDoSum();
	
	bindJump();
	
}

/**
 * Binds function bindEventHandlers to window's onload event:
 * when browser finishes loading the page, it will 
 * call bindEventHandlers
 */    

window.onload = bindCommonEventHandlers;
