
var main_url="http://www.mortgagesum.com/mortgagewidget/custommortgagecalculator/";




function chh(){
	var aa=document.cont.amt.value;
	var bb=document.cont.interest.value;
	var cc=document.cont.yrs.value;


	if(aa == "") { aa="0"; }
	if(bb == "") { bb="0"; }
	if(cc == "") { cc="0"; }
	if(aa == "0") {  var show =  "<br>Please insert the values for the Principal field"; document.getElementById("mg_priceid").innerHTML=show;}
	if(bb == "0") {  var show =  "<br>Please insert the values for the Interst field"; document.getElementById("mg_priceid").innerHTML=show;}
	if(cc == "0") {  var show =  "<br>Please insert the values in Number of years field "; document.getElementById("mg_priceid").innerHTML=show;}

	if(aa != "0" && bb != "0" && cc != "0"  ){
		var str=aa
		var a=(str.replace(/,/, ""))
		var tt= eval(cc *12)
		var mi = eval(bb/1200);
		var temp= eval(1.0 /(Math.pow((1+mi),tt)));
		if (temp < 1){
			var pmt = eval((a*mi)/(1-temp)); 
		}
		else { 
			var pmt = eval(amt/$tt); 
		}
		var tot=pmt.toFixed(2);
		var show ="Your Monthly Payment for<b> "+cc+" Years</b> at an Interest Rate of <b>"+bb+"%</b> for a Loan Amount of <b>$"+aa+"</b> is <b>$"+tot+"</b> a Month.";
		document.getElementById("mg_priceid").innerHTML=show;
	}
}

function writetext(){
	document.getElementById('mg_box').innerHTML = '<div class="mg_top" id="mg_top">Mortgage Calculator</div><p class="mg_title">Calculate your monthly mortgage payments.</p><form name="cont"><div class="mg_row"><div class="mg_left">Principal: </div><div class="mg_right"><input type="text" class="mg_details" size="8" maxlength="20" name="amt" id="mg_amt" value="500,000" onChange="javascript:chh();"></div><div class="mg_clear"></div></div><div class="mg_row"><div class="mg_left">Interest: </div><div class="mg_right"><input type="text" class="mg_details" size="8" maxlength="20" name="interest" id="mg_interest" value="6.00" onChange="javascript:chh();"></div><div class="mg_clear"></div></div><div class="mg_row"><div class="mg_left">Number of Years: </div><div class="mg_right"><input type="text" class="mg_details" size="8" maxlength="4" name="yrs" id="mg_yrs" value="30" onChange="javascript:chh();"></div><div class="mg_clear"></div></div><div class="mg_row"><div class="mg_left">&nbsp;</div><div class="mg_right"><input type="button" class="mg_send" name="calc" id="mg_calc" value="Calculate" onclick="javascript:chh();"></div><div class="mg_clear"></div></div></form><div align="center"><p class="mg_priceid" id="mg_priceid"></p></div><div class="mg_text" align="right">Brought to you by <a class="widget_link" href="http://www.mortgagesum.com/" target="_blank">Mortgage Calculators</a></div>';
}


	
function update1stColors(){
	document.getElementById("mg_amt").style.backgroundColor = mg_input;
	document.getElementById("mg_interest").style.backgroundColor = mg_input;
	document.getElementById("mg_yrs").style.backgroundColor = mg_input;
	document.getElementById("mg_priceid").style.backgroundColor = mg_input;	
	
	document.getElementById("mg_box").style.backgroundColor = mg_box_bg;
	document.getElementById("mg_top").style.backgroundColor = mg_top_bg;
	document.getElementById("mg_calc").style.backgroundColor = mg_but;

	document.getElementById("mg_amt").style.borderColor = mg_border;
	document.getElementById("mg_interest").style.borderColor = mg_border;
	document.getElementById("mg_yrs").style.borderColor = mg_border;
	document.getElementById("mg_priceid").style.borderColor = mg_border;
	document.getElementById("mg_box").style.borderColor = mg_box_border;

	document.getElementById("mg_top").style.color = mg_top_text;
	document.getElementById("mg_calc").style.color = mg_mess_but;
	document.getElementById("mg_priceid").style.color = mg_results;	

}

function gethexacode(col){
	regex = /\d+/g;
	x=col.match(regex);
	var r = x[0];
	var g = x[1];
	var b = x[2];
	if(r/1>255)r=255;
	if(g/1>255)g=255;
	if(b/1>255)b=255;
	r = baseConverter(r,10,16) + '';
	g = baseConverter(g,10,16) + '';
	b = baseConverter(b,10,16) + '';
	if(r.length==1)r = '0' + r;
	if(g.length==1)g = '0' + g;
	if(b.length==1)b = '0' + b;
	return "#"+r+g+b;
}

function generate_code(){
	if(document.getElementById("mg_generated_code")){
		document.getElementById("mg_generated_code").value = "<div class='mg_box' id='mg_box'></div><link rel='stylesheet' href='"+main_url+"style_w.css' media='screen' /><script type='text/javascript'>var mg_box_border='"+mg_box_border+"';var mg_box_bg='"+mg_box_bg+"';var mg_border='"+mg_border+"';var mg_top_bg='"+mg_top_bg+"';var mg_top_text='"+mg_top_text+"';var mg_input='"+mg_input+"';var mg_but='"+mg_but+"';var mg_results='"+mg_results+"';var mg_mess_but='"+mg_mess_but+"';var mg_box_size_w='"+mg_box_size_w+"';var mg_box_size_h='"+mg_box_size_h+"'</script><script type='text/javascript' src='"+main_url+"functions.js'></script>";
	}
}



function updateBoxSize(x, y){
	mg_box_size_w = x + "px";
	mg_box_size_h = y+ "px";
	updateManualInput(x, y);
	generate_code();
}


function updateManualInput(x, y){
	if (document.getElementById("mg_manual_width")){
		document.getElementById("mg_manual_width").value = x;
	}
	if (document.getElementById("mg_manual_height")){
		document.getElementById("mg_manual_height").value = y;
	}
}

function resizeBox(){
	document.getElementById("mg_box").style.height = mg_box_size_h;
	document.getElementById("mg_box").style.width = mg_box_size_w;
}

function modifyBoxSize(){
	mg_box_size_w = (document.getElementById('mg_manual_width').value*1 + 10) + "px";
	mg_box_size_h = (document.getElementById('mg_manual_height').value*1 + 10) + "px";	

	YAHOO.dragndrop.container.panel1.cfg.setProperty("height", mg_box_size_h);
	YAHOO.dragndrop.container.panel1.cfg.setProperty("width", mg_box_size_w);
	resizeBoxM();	
}

function resizeBoxM(){
	document.getElementById("mg_container").style.height= mg_box_size_h - 25;
	document.getElementById("mg_container").style.width= mg_box_size_w - 10;
	resizeBox();
	resizeBoxRes();
}


function loadfunctions(){
	writetext();	
	generate_code();
	chh();
	update1stColors();	
	resizeBox();
}

function resizeBoxRes(){
	document.getElementById("mg_box").style.width = "99%";
	document.getElementById("mg_box").style.height = "99%";
	mg_box_size_w = document.getElementById('mg_manual_width').value + "px";
	mg_box_size_h = document.getElementById('mg_manual_height').value + "px";	
	generate_code();
}

function updateColors(){
	element = document.getElementById('mg_fieldToChange')[document.getElementById('mg_fieldToChange').selectedIndex].value;
	if(element == "Input/Output Background"){
		document.getElementById("mg_amt").style.backgroundColor = document.getElementById("mg_color").value;
		document.getElementById("mg_interest").style.backgroundColor = document.getElementById("mg_color").value;
		document.getElementById("mg_yrs").style.backgroundColor = document.getElementById("mg_color").value;
		document.getElementById("mg_priceid").style.backgroundColor = document.getElementById("mg_color").value;		
		mg_input = document.getElementById("mg_color").value;
	}
	else if(element == "Background"){
		document.getElementById("mg_box").style.backgroundColor = document.getElementById("mg_color").value;
		mg_box_bg = document.getElementById("mg_color").value;
	}			
	else if(element == "Header"){
		document.getElementById("mg_top").style.backgroundColor = document.getElementById("mg_color").value;
		mg_top_bg = document.getElementById("mg_color").value;
	}			
	else if(element == "Calculate Button"){
		document.getElementById("mg_calc").style.backgroundColor = document.getElementById("mg_color").value;
		mg_but = document.getElementById("mg_color").value;
	}			
	else if(element == "Input Border"){
		document.getElementById("mg_amt").style.borderColor = document.getElementById("mg_color").value;
		document.getElementById("mg_interest").style.borderColor = document.getElementById("mg_color").value;
		document.getElementById("mg_yrs").style.borderColor = document.getElementById("mg_color").value;
		document.getElementById("mg_priceid").style.borderColor = document.getElementById("mg_color").value;
		mg_border = document.getElementById("mg_color").value;
	}			
	else if(element == "Border"){
		document.getElementById("mg_box").style.borderColor = document.getElementById("mg_color").value;
		mg_box_border = document.getElementById("mg_color").value;
	}			
	else if(element == "Button Text"){
		document.getElementById("mg_calc").style.color = document.getElementById("mg_color").value;	
		mg_mess_but = document.getElementById("mg_color").value;
	}	
	else if(element == "Header Text"){
		document.getElementById("mg_top").style.color = document.getElementById("mg_color").value;
		mg_top_text = document.getElementById("mg_color").value;
	}	
	else if(element == "Results"){
		document.getElementById("mg_priceid").style.color = document.getElementById("mg_color").value;
		mg_results = document.getElementById("mg_color").value;
	}		
	generate_code();
}


function resetColor(){
	element = document.getElementById('mg_fieldToChange')[document.getElementById('mg_fieldToChange').selectedIndex].value;
	if(element == "Input/Output Background"){
		document.getElementById("mg_amt").style.backgroundColor = mg_input_i;
		document.getElementById("mg_interest").style.backgroundColor = mg_input_i;
		document.getElementById("mg_yrs").style.backgroundColor = mg_input_i;
		document.getElementById("mg_priceid").style.backgroundColor = mg_input_i;		
		mg_input = mg_input_i;
	}
	else if(element == "Background"){
		document.getElementById("mg_box").style.backgroundColor = mg_box_bg_i;
		mg_box_bg = mg_box_bg_i;
	}			
	else if(element == "Header"){
		document.getElementById("mg_top").style.backgroundColor = mg_top_bg_i;
		mg_top_bg = mg_top_bg_i;
	}			
	else if(element == "Calculate Button"){
		document.getElementById("mg_calc").style.backgroundColor = mg_but_i;
		mg_but = mg_but_i;
	}			
	else if(element == "Input Border"){
		document.getElementById("mg_amt").style.borderColor = mg_border_i;
		document.getElementById("mg_interest").style.borderColor = mg_border_i;
		document.getElementById("mg_yrs").style.borderColor = mg_border_i;
		document.getElementById("mg_priceid").style.borderColor = mg_border_i;
		mg_border = mg_border_i;
	}			
	else if(element == "Border"){
		document.getElementById("mg_box").style.borderColor = mg_box_border_i;
		mg_box_border = mg_box_border_i;
	}			
	else if(element == "Button Text"){
		document.getElementById("mg_calc").style.color = mg_but_text_i;
		mg_but_text = mg_but_text_i;
	}	
	else if(element == "Header Text"){
		document.getElementById("mg_top").style.color = mg_top_text_i;
		mg_top_text = mg_top_text_i;
	}	
	else if(element == "Results"){
		document.getElementById("mg_priceid").style.color = mg_results_i;
		mg_results = mg_results_i;
	}		
	generate_code();
}


window.onLoad=loadfunctions();