// JavaScript Document
var sDirLib = '/homevaluesofamerica/lib/';
var sDirImg = '/homevaluesofamerica/images/';
var bGoCalc=false;
var iMaxWidth=300;

/*window.addEvent('load',function(){
	new Element('div',{'id':'slimboxbg'}).injectInside($$('body'));
});*/

window.addEvent('domready',function(){
	new Element('div',{'id':'slimboxbg'}).injectInside($E('body'));
	var oFxBg = new Fx.Styles($('slimboxbg'),{duration:500, transition:Fx.Transitions.Quad.easeOut});
	var showLoading = function(){
		oFxBg.start({'background-color':['#000000','#000000'],'opacity':[0 ,.7]});		
	}
	var hideLoading = function(){
		oFxBg.start({'background-color':['#000000','#000000'],'opacity':[.7 ,0]});
	}
	var checkForm=function(o,s){
		if(bGoCalc){
			if(o.value==''){
				alert(s);
				o.focus();
				bGoCalc=false;
				return;
			}
		}
	};
	
	// calc.php
	if($defined($('a_calc'))){
		$('a_calc').addEvent('click',function(){
			bGoCalc=true;
			checkForm($('street'),'Address Required.');
			checkForm($('zip'),'Zip Required.');
			checkForm($('city'),'City Required.');
			checkForm($('state'),'State Required.');
			checkForm($('email'),'Email Required.');
			checkForm($('fname'),'First Name Required.');
			checkForm($('lname'),'Last Name Required.');
			checkForm($('esthomevalue'),'Estimated Home Value Required.');
			checkForm($('mortgage1'),'1st Mortgage Amount Required.');
			checkForm($('mortgage2'),'2nd Mortgage Amount Required.');
			checkForm($('mortgage2'),'2nd Mortgage Amount Required.');
			checkForm($('curmopay'),'Current Monthly Payment Required.');
			checkForm($('annualincome'),'Annual Income Required.');
			checkForm($('curintrate'),'Current Interst Rate Required.');
			checkForm($('calltime'),'Best Time To Call Required.');
			//checkForm($('phonenum'),'Phone Number Required.');
			if(bGoCalc){
				$('form_home').send({
					onRequest:function(){
						showLoading();
					},
					onFailure:function(){
						hideLoading();
					},
					onSuccess:function(sResponse){
						hideLoading();
						var sResult='';
						if(sResponse.length>1){
							var oVal=Json.evaluate(sResponse);
							var aLenders=oVal.lenders;
							//var sFname=(oVal.fname.trim()!='')? ' '+oVal.fname+' ' : ' ';
							var sFname=oVal.fname;
							sResult=oVal.low+' - '+oVal.high;
							sResult=oVal.high;
							
							sResult ='<p>Well '+sFname+' looks like our Electronic Appraiser says your house is going to be somewhere around...<p>';
							sResult+='<p align="center">Value:<span class="style21"> <b>'+oVal.high+'</b></span></p>';
							sResult+='<p>That\'s based on a quick evaluation of your home your neighborhood, and accumulated appreciation since the last sale price. </p>';
							sResult+='<p>Good news '+sFname+', we found a quality lender in your area that specializes in this type of transaction. Click on the the company name to go directly to their website.</p>';
							
							if(aLenders.length>0){
								$('div_logo').empty();
								/*aLenders.each(function(aLender){
									var sUrl=aLender['url'],sLogo=aLender['logo'];
									new Element('p').adopt(
										new Element('a',{'href':sUrl}).adopt(
											new Element('img',{'src':sDirLib+'thumbw.php?q='+sLogo+'&w='+iMaxWidth,'border':0})
										)
									).injectInside($('div_logo'));
								});*/
							}
						}else{
							sResult='<center>No value available.</center>';
							$('div_logo').empty();
						}
						$('div_calc').addClass('style35').setHTML( sResult );
					}
				});
			}
			
		});
	}
	
	// index.php
	if($defined($('div_homeequity'))){
		var oSlide1=new Fx.Slide('div_homeequity');
		oSlide1.hide();
		var oImg1=$('img_ref');
		$('a_homeequity').addEvent('click',function(e){
			e=new Event(e);
			oSlide1.toggle();
			e.stop();
			//alert(oImg.getProperty('src')==sDirImg+'arrow_right.gif');
			if(oImg1.getProperty('src')==sDirImg+'arrow_right.gif'){
				oImg1.setProperty('src',sDirImg+'arrow_down.gif');
			}else{
				oImg1.setProperty('src',sDirImg+'arrow_right.gif');
			}
		});
		
		var oSlide2=new Fx.Slide('div_homeimpr');
		oSlide2.hide();
		var oImg2=$('img_fnd');
		$('a_homeimpr').addEvent('click',function(e){
			e=new Event(e);
			oSlide2.toggle();
			e.stop();
			
			if(oImg2.getProperty('src')==sDirImg+'arrow_right.gif'){
				oImg2.setProperty('src',sDirImg+'arrow_down.gif');
			}else{
				oImg2.setProperty('src',sDirImg+'arrow_right.gif');
			}
		});
	}
	
	// apply.php
	if($chk($('a_apply'))){
		$('a_apply').addEvent('click',function(e){
			//alert('testtest');
			$('form_apply').send({
				onRequest:function(){
					showLoading();
				},
				onFailure:function(){
					hideLoading();
				},
				onComplete:function(sRes){
					hideLoading();
					if(sRes.length>1){
						var aUrl=Json.evaluate(sRes);
						/*var oTbl=new Element('table',{'align':'center','cellspacing':'10'}).injectInside($('test'));
						var oTbdy=new Element('tbody').injectInside(oTbl);
						var oTr=new Element('tr').injectInside(oTbdy);*/
						$('test').empty();
						$('div_msg').empty();
						$('div_msg').setText('Good News we found a Lender(s) in your area who will be able to assist in any loan program you are interested in. Please scroll down to view Lender(s) and product(s) available.');
						
						if(aUrl.length>0){
							aUrl.each(function(aTmp){
								var sUrl=aTmp['url'];
								//new Element('td').adopt(new Element('iframe',{'src':sUrl,'width':'600','height':'600','frameborder':0})).injectInside(oTr);
								new Element('div',{'styles':{'padding-bottom':'20px'}}).adopt(new Element('iframe',{'src':sUrl,'width':'1024','height':'800','frameborder':0,'scrolling':'auto'})).injectInside($('test'));
							});
						}
						
							
					}
				}
			});
		});
			
	}
});

function sTrim( sInputString ) { 
	return sInputString.replace(/^\s*|\s*$/g,""); 
}
function bCheckEmail( sFieldVal ){  
	var sExpression = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	var sFieldVal = sTrim( sFieldVal ); 
			  
	return ( sExpression.test( sFieldVal ) ) ? true : false; 
}
function checkInt(e) {
	if (window.event) oKey = e.keyCode;
	else if (e.which) oKey = e.which;
	
	if ( (oKey >= 48 && oKey <= 57) || oKey == 8 ) return true;
	else return false;
}