var main_url="http://www.mortgagesum.com/mortgagewidget/amortization/";


function writetext(){
	document.getElementById('mg_box').innerHTML = '<div  id="mg_top" class="mg_top">Amortization Calculator</div><form name="MORTGAGE"><input name="FREQUENCY" value="Monthly" type="hidden"><div class="mg_clear"></div><div class="mg_clear"></div><div class="mg_clear"></div><div class="mg_clear"></div><div class="mg_clear"></div><div class="mg_row"><div class="mg_left">First Payment Year </div><div class="mg_right"><select size="1" name="START" class="mg_details" id="mg_fpy"><option value="2007" selected="selected">2007</option><option value="2008">2008</option><option value="2009">2009</option><option value="2010">2010</option><option value="2011">2011</option></select></div><div class="mg_clear"></div></div><div class="mg_row"><div class="mg_left">Mortgage Amount: </div><div class="mg_right"><input onchange="MyCheckEnteredValue(document.MORTGAGE.AMOUNT);" class="mg_details" id="mg_mortAm" size="10" value="300000" name="AMOUNT"></div><div class="mg_clear"></div></div><div class="mg_row"><div class="mg_left">Interest Rate</div><div class="mg_right"><input onchange="MyCheckEnteredValue(document.MORTGAGE.RATE);" class="mg_details" id= "mg_intRa" size="4" value="6.0" name="RATE"><label style="font-size:12px; font-family:Arial">%</label></div><div class="mg_clear"></div></div><div class="mg_row"><div class="mg_left">Mortgage Length</div><div class="mg_right"><select size="1" name="YEARS" class="mg_details" id="mg_mortLen"><option value="30" selected="selected">30</option><option value="25" selected="selected">25</option><option value="15">15</option><option value="10">10</option><option value="7">7</option>< <option value="5">5</option><option value="4">4</option><option value="3">3</option></select> <label style="font-size:12px; font-family:Arial">Years</label></div><div class="mg_clear"></div><p></p><div align="center"><input onclick="dosum(this.form, this)" class="mg_send" value="  Calculate  " name="cmdCalc" type="button" id="btn_calc"> <input value="Reset" id="btn_res" class="mg_send" name="cmdReset" type="reset"> <input onclick="dosum(this.form, this)" id="btn_amort" class="mg_send" value="Amortize" name="cmdAmoritize" type="button"></div><div class="mg_clear"></div><div class="mg_clear"></div><div class="mg_clear"></div><div class="mg_clear"></div><div class="mg_clear"></div><div class="mg_clear"></div><div class="mg_clear"></div><p style="font-size:12px; font-family:Arial"><b>Amortization Calculation Results</b></p><div class="mg_clear"></div></div><div class="mg_row"><div class="mg_left">Payment Amount</div><div class="mg_right"><input size="10" id="mg_paym" name="PAYMENT"></div><div class="mg_clear"></div></div><div class="mg_row"><div class="mg_left">Total Interest</div><div class="mg_right"><input size="10" id="mg_int" name="INTEREST"></div></form><p>&nbsp;</p><div class="for_reference_only">&nbsp;&nbsp;For Reference Only&nbsp;&nbsp;&nbsp;&nbsp;<a class="mg_text" href="'+main_url+'" target="_blank">Amortization Calculator</a><div class="mg_clear"></div><div class="mg_clear"></div><div class="mg_clear"></div><div class="mg_clear"></div></div>';
}

		  
		  
function update1stColors(){
	document.getElementById("mg_fpy").style.backgroundColor = mg_input;
	document.getElementById("mg_mortAm").style.backgroundColor = mg_input;
	document.getElementById("mg_intRa").style.backgroundColor = mg_input;
	document.getElementById("mg_mortLen").style.backgroundColor = mg_input;	
	document.getElementById("mg_paym").style.backgroundColor = mg_input;
	document.getElementById("mg_int").style.backgroundColor = mg_input;	
	
	document.getElementById("mg_box").style.backgroundColor = mg_box_bg;
	document.getElementById("mg_top").style.backgroundColor = mg_top_bg;
	
	document.getElementById("btn_calc").style.backgroundColor = mg_but;
	document.getElementById("btn_res").style.backgroundColor = mg_but;
	document.getElementById("btn_amort").style.backgroundColor = mg_but;
	
	document.getElementById("btn_calc").style.color = mg_mess_but;
	document.getElementById("btn_res").style.color = mg_mess_but;
	document.getElementById("btn_amort").style.color = mg_mess_but;

	document.getElementById("mg_fpy").style.borderColor = mg_border;
	document.getElementById("mg_mortAm").style.borderColor = mg_border;
	document.getElementById("mg_intRa").style.borderColor = mg_border;
	document.getElementById("mg_mortLen").style.borderColor = mg_border;
	document.getElementById("mg_paym").style.borderColor = mg_border;
	document.getElementById("mg_int").style.borderColor = mg_border;
	document.getElementById("mg_box").style.borderColor = mg_box_border;
	document.getElementById("mg_top").style.color = mg_top_text;

}

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.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_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><script type='text/javascript' src='"+main_url+"amortization-calculator.js'>";
	}
}

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);
	mg_box_size_h = (document.getElementById('mg_manual_height').value*1 + 10);
	
	if(!document.all)
	{
		mg_box_size_w += "px";
		mg_box_size_h += "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();
	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_fpy").style.backgroundColor = document.getElementById("mg_color").value;
		document.getElementById("mg_mortAm").style.backgroundColor = document.getElementById("mg_color").value;
		document.getElementById("mg_intRa").style.backgroundColor = document.getElementById("mg_color").value;
		document.getElementById("mg_mortLen").style.backgroundColor = document.getElementById("mg_color").value;		
		document.getElementById("mg_paym").style.backgroundColor = document.getElementById("mg_color").value;
		document.getElementById("mg_int").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 == "Buttons Background"){
		document.getElementById("btn_calc").style.backgroundColor = document.getElementById("mg_color").value;
		document.getElementById("btn_res").style.backgroundColor = document.getElementById("mg_color").value;
		document.getElementById("btn_amort").style.backgroundColor = document.getElementById("mg_color").value;
		mg_but = document.getElementById("mg_color").value;
	}			
	else if(element == "Input Border"){
		document.getElementById("mg_fpy").style.borderColor = document.getElementById("mg_color").value;
		document.getElementById("mg_mortAm").style.borderColor = document.getElementById("mg_color").value;
		document.getElementById("mg_intRa").style.borderColor = document.getElementById("mg_color").value;
		document.getElementById("mg_mortLen").style.borderColor = document.getElementById("mg_color").value;
		document.getElementById("mg_paym").style.borderColor = document.getElementById("mg_color").value;
		document.getElementById("mg_int").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 == "Buttons Text"){
		document.getElementById("btn_calc").style.color = document.getElementById("mg_color").value;	
		document.getElementById("btn_res").style.color = document.getElementById("mg_color").value;	
		document.getElementById("btn_amort").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;
	}		
	generate_code();
}


function resetColor(){
	element = document.getElementById('mg_fieldToChange')[document.getElementById('mg_fieldToChange').selectedIndex].value;
	if(element == "Input/Output Background"){
		document.getElementById("mg_fpy").style.backgroundColor = mg_input_i;
		document.getElementById("mg_mortAm").style.backgroundColor = mg_input_i;
		document.getElementById("mg_intRa").style.backgroundColor = mg_input_i;
		document.getElementById("mg_mortLen").style.backgroundColor = mg_input_i;	
		document.getElementById("mg_paym").style.backgroundColor = mg_input_i;	
		document.getElementById("mg_int").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 == "Buttons Background"){
		document.getElementById("btn_calc").style.backgroundColor = mg_but_i;
		document.getElementById("btn_res").style.backgroundColor = mg_but_i;
		document.getElementById("btn_amort").style.backgroundColor = mg_but_i;
		mg_but = mg_but_i;
	}			
	else if(element == "Input Border"){
		document.getElementById("mg_fpy").style.borderColor = mg_border_i;
		document.getElementById("mg_mortAm").style.borderColor = mg_border_i;
		document.getElementById("mg_intRa").style.borderColor = mg_border_i;
		document.getElementById("mg_mortLen").style.borderColor = mg_border_i;
		document.getElementById("mg_paym").style.borderColor = mg_border_i;
		document.getElementById("mg_int").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 == "Buttons Text"){
		document.getElementById("btn_res").style.color = mg_but_text_i;
		document.getElementById("btn_calc").style.color = mg_but_text_i;
		document.getElementById("btn_amort").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;
	}		
	generate_code();
}


window.onLoad=loadfunctions();