//dynamic top navigation
initNav = function() {
	var navRoot = document.getElementById("main-navigation");
	var lis = navRoot.getElementsByTagName("li");
	for (var i=0; i<lis.length; i++)
	{
		lis[i].onmouseover = function()
		{
			this.className += " hover";
		}
		lis[i].onmouseout = function()
		{
			this.className = this.className.replace(" hover", "");
		}
	}
}
if (document.all && window.attachEvent)
	attachEvent("onload", initNav);

function initTextBox()
{
	var divs = document.getElementsByTagName("div");
	for (var i=0; i<divs.length; i++)
	{
		if (divs[i].className.indexOf("text-box") != -1 )
		{
			var links = divs[i].getElementsByTagName("a");
			if (links.length != 0)
			{
				divs[i]._link = links[0];
			}
			if (divs[i].className != "text-box-list" )
			{
				divs[i].onmouseover = function()
				{
					this.className += " hover";
				}
				divs[i].onmouseout = function()
				{
					this.className = this.className.replace("hover", "");
				}
				divs[i].onclick = function()
				{
					if (this._link) {
						window.location = this._link.href;
//						alert();
//						this._link.click();
					}
				}
			}
		}
	}
}
if (window.addEventListener){
	window.addEventListener("load", initTextBox, false);
}
else if (window.attachEvent){
	window.attachEvent("onload", initTextBox);
}

function initPage()
{
	var divs = document.getElementsByTagName("div");
	for (var i=0; i<divs.length; i++)
	{
		if (divs[i].className.indexOf("text-box-list") != -1 )
		{
			var subDivs = divs[i].getElementsByTagName("div");
			var maxHeigth = divs[i].offsetHeight -18;
			if (subDivs)
			{
				for (var j=0; j<subDivs.length; j++)
				{
					if (subDivs[j].className.indexOf("text-content") != -1)
					{
						subDivs[j].style.height =  maxHeigth + "px";
					}
				}
			}
		}
	}
}

if (window.addEventListener){
	window.addEventListener("load", initPage, false);
	window.addEventListener("resize", initPage, false);
}
else if (window.attachEvent){
	window.attachEvent("onload", initPage);
	window.attachEvent("onresize", initPage);
}	

function popupPage(page, width, height) {
	if (width == undefined){width =450;}
	if(height==undefined){height =500;}
	
	msgWindow=window.open('','','toolbar=no,width=' + width + ',status=no,scrollbars=yes,height=' + height +'');
	msgWindow.location.href =page;
	if (msgWindow.opener == null) msgWindow.opener =self;
}
function scrollToTop()
{
	speed = 5 //the higher the integer the faster the scroll.
	startPos = (document.all)?document.documentElement.scrollTop:window.pageYOffset;
	while (startPos>400)
	{
		window.scrollTo(0,startPos);
		startPos = startPos - speed;
	}
}
//calculator code
function Intcal()	{
	form = document.getElementById('calc');
	n=form.LType.selectedIndex;
	if (n==0) {
		AnnualInterestRate=5.74; //Mortgage
	}
	else if (n==1) {
		AnnualInterestRate=7.65; //HELOC
	}
	else	{
		AnnualInterestRate=7.00; //New Auto
	}
	LoanAmount= form.LAmount.value;
	AvgIR=AnnualInterestRate*121/100;
	PoorIR=AnnualInterestRate*158/100;
	Years= form.Term.value;
	MonthRate=AnnualInterestRate/100/12;
	AvgMR=AvgIR/100/12;
	PoorMR=PoorIR/100/12;
	NumPayments=Years*12;
	MonthPayment=Math.floor((LoanAmount*MonthRate)/(1-Math.pow((1+MonthRate),(-1*NumPayments)))*100)/100;
	AvgMP=Math.floor((LoanAmount*AvgMR)/(1-Math.pow((1+AvgMR),(-1*NumPayments)))*100)/100;
	PoorMP=Math.floor((LoanAmount*PoorMR)/(1-Math.pow((1+PoorMR),(-1*NumPayments)))*100)/100;
	TotalPayment=NumPayments*MonthPayment;
	AvgTP=NumPayments*AvgMP
	PoorTP=NumPayments*PoorMP
	TotalInterest=TotalPayment-LoanAmount;
	AvgTI=AvgTP-LoanAmount;
	PoorTI=PoorTP-LoanAmount;
	form.APR3.value=AnnualInterestRate.toFixed(2);
	form.PAmount3.value=TotalPayment.toFixed(2);
	form.IAmount3.value=TotalInterest.toFixed(2);
	form.APR2.value=AvgIR.toFixed(2);
	form.PAmount2.value=AvgTP.toFixed(2);
	form.IAmount2.value=AvgTI.toFixed(2);
	form.APR1.value=PoorIR.toFixed(2);
	form.PAmount1.value=PoorTP.toFixed(2);
	form.IAmount1.value=PoorTI.toFixed(2);
}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();
if (BrowserDetect.browser == "Safari")
{
	document.write("<link href=\"\/css\/safari.css\" rel=\"stylesheet\" type=\"text/css\" />");
}

var isIE = BrowserDetect.browser == "Explorer" ? true : false;	

var myInterval =setInterval("moveIt()",100);

function moveIt(){
	var overlay = document.getElementById("overlay");
	if( overlay && overlay.style.visibility == "visible")
	{
		//var tellafriend = document.getElementById("tellafriend");
		//var askaquestion = document.getElementById("askaquestion");
		//if (tellafriend.style.visibility =="visible")
		//{
			//ShowPopupForm("tellafriend");	
		//}
		//else if(askaquestion.style.visibility =="visible")
		//{
			//ShowPopupForm("askaquestion");
		//}
		ShowOverlay()
	}
}
function ShowOverlay()
{
	var overlay = document.getElementById("overlay");
	var height = isIE ? document.body.clientHeight  + document.body.scrollTop : window.innerHeight  + window.pageYOffset;
	var width = isIE ? document.body.clientWidth + document.body.scrollLeft :  window.innerWidth  + window.pageXOffset ;
	overlay.style.height = height  + "px";
	overlay.style.width = width  + "px";
	overlay.style.left = 0 + "px";
	overlay.style.top = 0 + "px";
	overlay.style.visibility = "visible";
}
function ShowPopupForm(thepopup) 
{
	ShowOverlay()
	//var overlay = document.getElementById("overlay");
	var popup = document.getElementById(thepopup);

	var height = isIE ? document.body.clientHeight  + document.body.scrollTop : window.innerHeight  + window.pageYOffset;
	var width = isIE ? document.body.clientWidth + document.body.scrollLeft :  window.innerWidth  + window.pageXOffset ;
	
	/*overlay.style.height = height  + "px";
	overlay.style.width = width  + "px";
	overlay.style.left = 0 + "px";
	overlay.style.top = 0 + "px";
	overlay.style.visibility = "visible";*/
		
	popup.style.visibility = "visible";
	popup.style.top = isIE ? document.documentElement.scrollTop + 20  + "px": (window.pageYOffset + height)/2 - (popup.offsetHeight/2) + "px"; 
	popup.style.left = (width/2) - (popup.offsetWidth/2) + "px";
	popup.style.visibility = "visible";
	
}
function HidePopupForm(thepopup,message, theform) 
{
	var overlay = document.getElementById("overlay");
	var popup = document.getElementById(thepopup);
	
	overlay.style.visibility ="hidden";
	overlay.style.left ="-1000px";
	
	popup.style.visibility ="hidden";
	popup.style.left ="-1000px";
	
	document.getElementById(theform).reset();
	document.getElementById(message).innerHTML="";
}
function isNumberKey(evt)
{
 	var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
    	return false;

    return true;

}
function isAlphaKey(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode < 97 || charCode > 122) {
		if (charCode < 70 || charCode > 90) {
        	return false;
		}
	}
    return true;
}
function ajaxFunction(theform, popup, message)
{
	loadProcessingDiv(popup);
	var ajaxRequest = isIE ?  new ActiveXObject("Msxml2.XMLHTTP"): new XMLHttpRequest();
	ajaxRequest.onreadystatechange = function()
	{
		if(ajaxRequest.readyState==4)
		{
			if(ajaxRequest.status == 200)
			{
				var mydiv =document.getElementById(message);
				mydiv.innerHTML= ajaxRequest.responseText.length > 0 ? ajaxRequest.responseText: "Error processing form.";
				document.getElementById("randImg").src = document.getElementById("randImg").src + "#";
				document.getElementById("randImg2").src = document.getElementById("randImg2").src + "#";
				removeProcessingDiv(popup);
			}
		}
		else
		{
			removeProcessingDiv(popup);
		}
	}
	var myForm = document.getElementById(theform);
	var myData = getData(myForm);
	ajaxRequest.open("POST", myForm.action, true);
	ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajaxRequest.send(myData);
}
function getData(myForm)
{
	var params =new Array();
	for (var i=0; i< myForm.elements.length; i++)
	{
		var param = encodeURIComponent(myForm.elements[i].name);
		param += "=";
		param += encodeURIComponent(myForm.elements[i].value);
		params.push(param);
	}
	return params.join("&");
}
function loadProcessingDiv(thepopup)
{
	var popup = document.getElementById(thepopup);
	var height = isIE ? document.body.clientHeight  + document.body.scrollTop : window.innerHeight  + window.pageYOffset;
	var width = isIE ? document.body.clientWidth + document.body.scrollLeft :  window.innerWidth  + window.pageXOffset ;
	
	var processing = document.createElement("div");
	processing.id = "processing";
	processing.className ="processing";

	processing.innerHTML = "<img src='images/processing.gif' alt='PROCESSING' />";
	popup.appendChild(processing);
	processing.style.left= popup.offsetWidth/2 - processing.offsetWidth/2 + "px";
	processing.style.top = popup.offsetHeight/2  + "px";
}
function removeProcessingDiv(thepopup)
{
	var popup = document.getElementById(thepopup);
	var processing = document.getElementById("processing")
	if (processing) {popup.removeChild(processing);}
}
