function switchimage(newimg, oldimg) {
	var img = document.getElementById(oldimg);
	img.setAttribute('src', 'assets/images/' + newimg);
}

function showhide(divname, doit){
	document.getElementById(divname).style.display=doit;
}

/**********************************************************
SLIDER
**********************************************************/
var intervalID		= false;
var sliderSpeed		= 30;			// How fast the interval is.
var sliderJump		= 20;			// How many pixels jumped each interval.
var sliderOffset	= 0;			// How far the slider can slide back.
var sliderOut		= 28;			// Where the slider stops.

function slidedown() {
	slide = document.getElementById('slider');
	if(slide.offsetTop <= sliderOut) {
		clearInterval(intervalID);
	} else {
		slide.style.marginTop = (slide.offsetTop - sliderJump) + 'px';
	}
}

function slideup() {
	slide = document.getElementById('slider');
	if(slide.offsetTop >= sliderOffset) {
		clearInterval(intervalID);
		document.getElementById('slidersmama').style.display = 'none';
	} else {
		slide.style.marginTop = (parseInt(slide.style.marginTop) + sliderJump) + 'px';
	}

}


function checktheor()
{
	mama 					= document.getElementById('slidersmama');
	slide					= document.getElementById('slider');
	mama.style.display 		= 'block';
	sliderOffset			= mama.offsetHeight;
	//sliderOut				= 28;

	if(slide.offsetTop > sliderOut) {
		intervalID	= setInterval(slidedown, sliderSpeed);
	} else {
		mama.style.display 		= 'block';
		intervalID = setInterval(slideup, sliderSpeed);
	}
}

function setupSlider() {
//window.onload = function() {
	//alert('Onload');
	mama 	= document.getElementById('slidersmama');
	slide	= document.getElementById('slider');
	mama.style.visibility	= 'hidden';
	mama.style.display 		= 'block';
	mama.style.height		= mama.offsetHeight + 28 + 'px';
	slide.style.marginTop 	= mama.offsetHeight + 'px';
	sliderOffset			= mama.offsetHeight;
	mama.style.display 		= 'none';
	mama.style.visibility	= 'visible';

}

var min=12;
var max=18;
function increaseFontSize(divname) {
	var p = document.getElementById(divname);

	if(p.style.fontSize) {
		var s = parseInt(p.style.fontSize.replace("px",""));
	} else {
		var s = 14;
	}
	if(s!=max) {
		s += 1;
	}
	p.style.fontSize = s+"px"

}
function decreaseFontSize(divname) {
	var p = document.getElementById(divname);
	if(p.style.fontSize) {
		var s = parseInt(p.style.fontSize.replace("px",""));
	} else {
		var s = 14;
	}
	if(s!=min) {
		s -= 1;
	}
	p.style.fontSize = s+"px"

}

function mailto(el, end, front, middle)
{
	el.href= 'mailto:' + front + middle + end;
	document.status = el.href;
}


/****************************************
Mortgage Calculator
****************************************/
function setPrice(){
	if(window.opener){
		var tp=opener.price;
		if(tp){
			document.form.listPrice.value=tp;
			document.form.dwnPymnt.focus();
			return;}
	}
	document.form.listPrice.focus();
}

function setCMHC(){
	var Dpercent = document.form.dwnPymnt.value/document.form.listPrice.value;
	var mtotal = document.form.listPrice.value - document.form.dwnPymnt.value;
	if(document.form.useCMHC.checked==false){
		document.form.amountCMHC.value = 0;
		document.getElementById('CMHCamount').innerHTML = '';
		return;
	}
	var Dpremium = 0;
	if (Dpercent <=.35){
		if (Dpercent <=.05){Dpremium = mtotal*.0275;}
		if (Dpercent<=.10 && Dpercent > .05){Dpremium = mtotal*.02;}
		if (Dpercent<=.15 && Dpercent > .10){Dpremium = mtotal*.0175;}
		if (Dpercent<=.20 && Dpercent > .15){Dpremium = mtotal*.01;}
		if (Dpercent<=.25 && Dpercent > .20){Dpremium = mtotal*.0065;}
		if (Dpercent<=.35 && Dpercent > .25){Dpremium = mtotal*.005;}
		Dpremium= Dpremium.toFixed(0);
	}
	if (Dpercent >.35){Dpremium = 0;}
	document.form.amountCMHC.value = Dpremium;
	document.getElementById('CMHCamount').innerHTML = '$'+Dpremium;
}

function calcMortAmount(){
	var mtotal = document.form.listPrice.value - document.form.dwnPymnt.value;
}

function calcDownPmnt(rate){
	rate= rate/100;
	var down = document.form.listPrice.value*rate;
	document.form.dwnPymnt.value = down.toFixed(0);
}


function calcPayments(){
	//check for required fields
	if(document.form.listPrice.value=='' || document.form.listPrice.value==0){alert('Please Enter A Price!');document.form.listPrice.focus();return false;}
	if(document.form.i.value=='' ){alert('Please Enter An Interest Rate!');document.form.i.focus();return false;}

	setCMHC();
	var subTotal = (document.form.listPrice.value - document.form.dwnPymnt.value)*1; //Princioal Required
	var P = subTotal + (document.form.amountCMHC.value*1);  //Total Mortgage Amount
	var F= document.form.paymentFreq.options[document.form.paymentFreq.selectedIndex].value;  //Payment Frequenct
	var term = document.form.amort.options[document.form.amort.selectedIndex].value; //Loan Term
	var I = document.form.i.value/100; // interest rate
	var r = I/F;  // effective rate
	var n = F*term;  // number of payments
	var a = [P*Math.pow((1+r),n)*r]/[Math.pow((1+r),n)-1]; //calc monthly payment

	// display results
	a= a.toFixed(0);
	P= P.toFixed(0);
	subTotal= subTotal.toFixed(0);

	document.getElementById('principal').innerHTML='$'+subTotal;
	document.getElementById('mortgageAmount').innerHTML='$'+P;
	document.getElementById('paymentAmount').innerHTML='$'+a;
	return;
}

function mortCalc(value)
{
	calcy						= document.getElementById('mortcalculator');
	calcy.style.display			= 'block';
	document.forms['mortForm'].elements['listPrice'].value = value;
}

function closeCalc()
{
	document.getElementById('mortcalculator').style.display = 'none';
	
}


/**************************************
 Miscellaneous Functions
 **************************************/

// Check all the checkboxes with the same name <attr> in the same form <frm>.
function selectAll(el, frm, attr)
{
	fObject = document.forms[frm];
	elements = fObject.elements[attr + '[]'];
	for(i = 0; i < elements.length; i++)
	{
		if(el.checked) { elements[i].checked = true; }
		else { elements[i].checked = false; }
	}
}

function realtorLogin()
{
	if(document.getElementById('loginmenu').style.display == 'block') {
		showhide('loginmenu', 'none');
	} else {
		showhide('loginmenu', 'block');
	}
}

function addLoadEvent(func) { var oldonload = window.onload;  if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() {oldonload(); func(); } } } 

addLoadEvent(setupSlider);

function newWindow(a_str_windowURL, a_str_windowName, a_int_windowWidth, a_int_windowHeight, a_bool_scrollbars, a_bool_resizable, a_bool_menubar, a_bool_toolbar, a_bool_addressbar, a_bool_statusbar, a_bool_fullscreen) 
{
	var int_windowLeft = (screen.width - a_int_windowWidth) / 2;
	var int_windowTop = (screen.height - a_int_windowHeight) / 2;
	var str_windowProperties = 'height=' + a_int_windowHeight + ',width=' + a_int_windowWidth + ',top=' + int_windowTop + ',left=' + int_windowLeft + ',scrollbars=yes,resizable=' + a_bool_resizable + ',menubar=' + a_bool_menubar + ',toolbar=' + a_bool_toolbar + ',location=' + a_bool_addressbar + ',statusbar=' + a_bool_statusbar + ',fullscreen=' + a_bool_fullscreen + '';
	//alert(a_str_windowURL + ',' + a_str_windowName + ',' + str_windowProperties);
	var obj_window = window.open(a_str_windowURL, a_str_windowName, str_windowProperties);
	//alert(window.open);
	//window.open('/pages/for_sale/index.php', 'This is a test', 'width=800,top=100,left=100,scrollbars=no,status=no');
	//window.open('/pages/for_sale/index.php', a_str_windowName, 'width=200,height=200');
	//if (parseInt(navigator.appVersion) >= 4) {obj_window.window.focus(); }
}
	