var fparts = ["refstep1","refstep2","refstep3","refstep4","refstep5","refstep6","refstep7","refstep8"];

var pathC;

function imgPath(path){
	var aP = path.split("/");
	var newp = "";
	var lim = aP.length -1;
	for(i=0;i<lim;i++){
		newp += aP[i] + "/";	
	}
	return newp;
}

function Next(idname,hidebot){
	var errmsg = " ";
 	if(idname == "refstep8"){
 		errmsg += validar8();
		if(errmsg.length > 5){
			alert(errmsg);
		}else{
			document.images.bt8.src = pathC + "step-04-off.gif";
			document.images.bt9.src = pathC + "step-05-on.gif";
	 		document.getElementById("refstep8").style.height = "335px";
	 		NextStage(hidebot,idname);
		}
	 }	
	 if(idname == "refstep7"){
	 	errmsg += validar7();
		if(errmsg.length > 5){
			alert(errmsg);
		}else{
			document.images.bt7.src = pathC + "step-03-off.gif";
			document.images.bt8.src = pathC + "step-04-on.gif";
	 		document.getElementById("refstep7").style.height = "245px";
	 		NextStage(hidebot,idname);
		}
	 }
	  if(idname == "refstep6"){
	  	errmsg += validar6();
		if(errmsg.length > 5){
			alert(errmsg);
		}else{
			document.images.bt6.src = pathC + "step-02-off.gif";
			document.images.bt7.src = pathC + "step-03-on.gif";
	 		document.getElementById("refstep6").style.height = "255px";
			Title5();
			NextStage(hidebot,idname);
		}
	 } 
	 if(idname == "refstep4"){
	 	errmsg += validar4();
		if(errmsg.length > 5){
			alert(errmsg);
		}else{
			document.images.bt4.src = pathC + "step-04-off.gif";
			document.images.bt5.src = pathC + "step-05-on.gif";
	 		document.getElementById("refstep4").style.height = "335px";
	 		NextStage(hidebot,idname);
		}
	 } 
	  if(idname == "refstep3"){
	  	errmsg += validar3();
		if(errmsg.length > 5){
			alert(errmsg);
		}else{
			document.images.bt3.src = pathC + "step-03-off.gif";
			document.images.bt4.src = pathC + "step-04-on.gif";
	 		document.getElementById("refstep3").style.height = "240px";
			Title3();
			NextStage(hidebot,idname);
		}
	 } 
	 if(idname == "refstep2"){ 
		errmsg += validar2();
		if(errmsg.length > 5){
			alert(errmsg);
		}else{
	 		Title2();
			document.images.bt2.src = pathC + "step-02-off.gif";
			document.images.bt3.src = pathC + "step-03-on.gif";
	 		document.getElementById("refstep2").style.height = "220px";
	 		NextStage(hidebot,idname);
		}
	 }
	 if(idname == "refstep1"){	
	 //document.write("hello");
	 	document.getElementById("refstep1").style.height = "200px";
		if(document.myform.TYPE_DESIREDb[document.myform.TYPE_DESIREDb.selectedIndex].value.length < 2){
			errmsg += 'Please Select Loan Purpose. \n';
			SubErr($("TYPE_DESIREDb"));
		}
		if(document.myform.zip.value.length < 3){
			errmsg += 'Please Type Zip Code. \n';
			SubErr($("zip")); 
		}
		if(isZip(document.myform.zip.value)==true){
			errmsg += 'Zip Code must be 5 digit number. \n';
			SubErr($("zip"));
		}
		if(document.myform.state[document.myform.state.selectedIndex].value.length < 2){
			errmsg += 'Please Choose Your State. \n';
			SubErr($("state"));
		}
		if(errmsg.length > 5){
			alert(errmsg);
		}else{ 
				Title1(); 		
				document.myform.TYPE_DESIRED.value = document.myform.TYPE_DESIREDb[document.myform.TYPE_DESIREDb.selectedIndex].value;	 		
				document.myform.TYPE_DESIREDb.disabled = true;
				
				if(document.myform.TYPE_DESIREDb[document.myform.TYPE_DESIREDb.selectedIndex].value == "DebtConsolidation"){
					document.location = "/apply_online.php?t=Debt Management&zip=" + document.myform.zip.value + "&state=" + document.myform.state[document.myform.state.selectedIndex].value;
				}else if(document.myform.TYPE_DESIREDb[document.myform.TYPE_DESIREDb.selectedIndex].value == "DebtSettlement"){
					document.location = "/apply_online.php?t=Debt Settlement&zip=" + document.myform.zip.value + "&state=" + document.myform.state[document.myform.state.selectedIndex].value;
				}else if(document.myform.TYPE_DESIREDb[document.myform.TYPE_DESIREDb.selectedIndex].value == "Payday"){
					//alert("payday");
					document.location = "/apply_online.php?t=Payday Loans&zip=" + document.myform.zip.value + "&state=" + document.myform.state[document.myform.state.selectedIndex].value;
				}else if(document.myform.TYPE_DESIREDb[document.myform.TYPE_DESIREDb.selectedIndex].value == "NewPurchase"){
					for(i=0;i<4;i++){
					Element.remove(fparts[i]);
					}
					document.myform.TYPE_DESIRED.value = document.myform.TYPE_DESIREDb[document.myform.TYPE_DESIREDb.selectedIndex].value;	 
					var state = document.myform.state[document.myform.state.selectedIndex].text;
					var heading = state + " Lenders are Standing By to Serve You With All Your Home Purchase Needs.";
	 				Element.update('fsteptit5',heading);
					idname = "refstep5";
					document.getElementById("refstep5").style.height = "200px";
					pathC = imgPath(document.images.bt1.src);
					document.images.bt1.src = pathC + "step-01-off.gif";
					document.images.bt6.src = pathC + "step-02-on.gif";
					Effect.Fade(hidebot,{duration:2,transition: Effect.Transitions.wobble });
					Effect.SlideDown(idname, {duration:2});
					//document.getElementById("step0").style.display = "none";
				}else{
					for(i=4;i<fparts.length;i++){
						Element.remove(fparts[i]);
					}
					pathC = imgPath(document.images.bt1.src);
					document.images.bt1.src = pathC + "step-01-off.gif";
					document.images.bt2.src = pathC + "step-02-on.gif";
					document.myform.TYPE_DESIRED.value = document.myform.TYPE_DESIREDb[document.myform.TYPE_DESIREDb.selectedIndex].value;	 
					Effect.Fade(hidebot,{duration:2,transition: Effect.Transitions.wobble });
					Effect.SlideDown(idname, {duration:2});
					//document.getElementById("step0").style.display = "none";
				}
		}	
	 }
	 /*else{
	 	Effect.Fade(hidebot,{transition: Effect.Transitions.wobble });
		Effect.SlideDown(idname, {duration:2});
	}*/
}

function Title5(){
	var rate = document.myform.credit_rating[document.myform.credit_rating.selectedIndex].text;
	var heading = "HotLoan Lenders Have Zero Down Purchase Loans for " + rate + " Credit Homeowners.";
	 Element.update('fsteptit6',heading);
}

function Title3(){
	var rate = document.myform.rate_1[document.myform.rate_1.selectedIndex].text;
	var ltype = document.myform.TYPE_DESIREDb[document.myform.TYPE_DESIREDb.selectedIndex].text;
	var heading = "HotLoan Lenders can lower your current rate from " + rate + " to a new 5.88% Fixed on " + ltype + " loans.";
	 Element.update('fsteptit3',heading);
	//document.getElementById("fsteptit1").innerHTML = table;
}

function Title2(){
	var state = document.myform.state[document.myform.state.selectedIndex].text;
	var city = document.myform.City.value;
	var heading = city + " Homeowners have a minimum of $50.000 in cash out funds with " + state + " Sponsored Loan Programs.";
	 Element.update('fsteptit2',heading);
	//document.getElementById("fsteptit1").innerHTML = table;
}

function Title1(){
	var state = document.myform.state[document.myform.state.selectedIndex].text;
	var heading = state + " Lenders are Standing By to Serve You With All Your Refinance Needs.";
	 Element.update('fsteptit1',heading);
	//document.getElementById("fsteptit1").innerHTML = table;
}

function checkAgree(){
	if(document.myform.agreetoterms.checked == false){
		document.myform.envia.disabled=true;
		alert('You must agree to the terms and conditions to send your request');
	}else{
		document.myform.envia.disabled=false;
	}
}

function ElementHider(){
	for(i=0;i< fparts.length;i++){
		Element.hide(fparts[i]);
	}
}

function isZip(s) {
     // Check for correct zip code
     reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
     if (!reZip.test(s)) {
       	//alert("Zip Code Is Not Valid");
        return true;
     }else{
		return false;
     }
}

//chek if email is valid
function echeck(str) {
		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false;
		 }
 		 return true;					
}

function Cargador(){
	ElementHider();
	if(TYPEDES.length > 3){
		var i = indexToSelect('TYPE_DESIREDb',TYPEDES);
		var textil = "";
		switch(TYPEDES){
				case "NewPurchase":
					textil = "Get up to 5 New Home Purchase offers instantly!";
					break
				case "SecondMortgage":
					textil = "Compare and get up to 5 Second Mortgage offers instantly!";
					break
				case "Refinance":
					textil = "Compare and get up to 5 Refinance offers instantly!";
					break
				case "Refinance-CashOut":
					textil = "Get up to 5 Refinance Cash Out offers instantly!";
					break
				case "DebtConsolidation":
					textil = "Compare and get up to 5 Debt Consolidation offers instantly!";
					break
				case "HomeEquity":
					textil = "Compare and get up to 5 Home Equity offers instantly!";
					break	
			}		
		document.myform.TYPE_DESIREDb.selectedIndex = i;
		document.getElementById("ltypename").innerHTML = textil;
	}
}

//returns the index that should be selected
function indexToSelect(selectname,value){
	eval("var aOptions = document.myform."+selectname);
	for (i=0;i<aOptions.length;i++){
			if(aOptions[i].value == value){
				var y = i;
				break;
			}
	}
	return y;
}

//Shows next stage
function NextStage(hidebot,idname){
	Effect.Fade(hidebot,{transition: Effect.Transitions.wobble });
	Effect.SlideDown(idname, {duration:2});
}

//hightlight en campos con error
function SubErr(field){
	new Effect.Highlight(field, {startcolor:'#b5ea5e', endcolor:'#ff6600', duration:10});
}

//validates tab2
function validar2(){
	var err = "";
	if($("City").value.length < 2){
		err += 'You should type a City \n';
		SubErr($("City"))
	}
	if($("Address1").value.length < 7){
		err += 'You should type a valid Address \n';
		SubErr($("Address1"))
	}
	if(document.myform.property_value[document.myform.property_value.selectedIndex].value.length < 2){
			err += 'Please Select Your Home Value. \n';
			SubErr($("property_value"));
		}
	return err;
}

//validates tab3 
function validar3(){
	var err = "";
	if(document.myform.loan_desired[document.myform.loan_desired.selectedIndex].value.length < 2){
		err += 'Please Select Loan Amount Desired. \n';
		SubErr($("loan_desired"));
	}
	if(document.myform.mortgage_balance_1[document.myform.mortgage_balance_1.selectedIndex].value.length < 2){
		err += 'Please Select Total Mortgage Balance. \n';
		SubErr($("mortgage_balance_1"));
	}
	if(document.myform.rate_1[document.myform.rate_1.selectedIndex].value.length < 1){
		err += 'Please Select Current Rate. \n';
		SubErr($("rate_1"));
	}
	return err;
}

//validates tab4
function validar4(){
	var err = "";
	if(isNaN($("Income").value) || $("Income").value.length < 1){
		err += 'You should type Your Monthly Income. \n';
		SubErr($("Income"));
	}
	if(isNaN($("Debt2").value) || $("Debt2").value.length < 1){
		err += 'You should type Your Monthly Debt. \n';
		SubErr($("Debt2"));
	}
	return err;
}

//validates tab6
function validar6(){
	var err = "";
	if(document.myform.PROPERTY_owned[document.myform.PROPERTY_owned.selectedIndex].value.length < 1){
		err += 'Please Select Property Type. \n';
		SubErr($("PROPERTY_owned"));
	}
	if(document.myform.credit_rating[document.myform.credit_rating.selectedIndex].value.length < 1){
		err += 'Please Select Your Credit Rating. \n';
		SubErr($("credit_rating"));
	}
	var ownersel = 1;
	for (i=0;i<document.myform.home_owner.length;i++){
		if (document.myform.home_owner[i].checked==true){
			ownersel=2;
			break; //exist for loop, as target acquired.
		}
	}
	if(ownersel == 1){
		err += 'Please choose if you are a home owner. \n'; 
		SubErr($("home_owner"));
	}
	return err;
}

//validates tab7
function validar7(){
	var err="";
	if(document.myform.loan_desired[document.myform.loan_desired.selectedIndex].value.length < 2){
		err += 'Please Select Loan Amount Desired. \n';
		SubErr($("loan_desired"));
	}
	if(document.myform.property_value[document.myform.property_value.selectedIndex].value.length < 2){
		err += 'Please Select Your Home Value. \n';
		SubErr($("property_value"));
	}
	if(document.myform.down_payment[document.myform.down_payment.selectedIndex].value.length < 2){
		err += 'Please Select Your Down Payment. \n';
		SubErr($("down_payment"));
	}
	if(document.myform.purchase_time[document.myform.purchase_time.selectedIndex].value.length < 2){
		err += 'Please Select Your Purchase Time. \n';
		SubErr($("purchase_time"));
	}
	return err;
}

//validates tab8
function validar8(){
	var err = "";
	
	var found_home = 1;
	for (i=0;i<document.myform.found_home.length;i++){
		if (document.myform.found_home[i].checked==true){
			found_home=2;
			break; //exist for loop, as target acquired.
		}
	}
	if(found_home == 1){
		err += 'Please choose if found home. \n'; 
		SubErr($("found_home"));
	}
	
	var realtor_need = 1;
	for (i=0;i<document.myform.realtor_need.length;i++){
		if (document.myform.realtor_need[i].checked==true){
			realtor_need=2 ;
			break; //exist for loop, as target acquired.
		}
	}
	if(realtor_need == 1){
		err += 'Please choose if need a realtor. \n'; 
		SubErr($("realtor_need"));
	}
	
	var with_realtor = 1;
	for (i=0;i<document.myform.with_realtor.length;i++){
		if (document.myform.with_realtor[i].checked==true){
			with_realtor=2 ;
			break; //exist for loop, as target acquired.
		}
	}
	if(with_realtor == 1){
		err += 'Please choose if you are working with a realtor. \n'; 
		SubErr($("with_realtor"));
	}
	
	var bankrupcy = 1;
	for (i=0;i<document.myform.bankrupcy.length;i++){
		if (document.myform.bankrupcy[i].checked==true){
			bankrupcy=2 ;
			break; //exist for loop, as target acquired.
		}
	}
	if(bankrupcy == 1){
		err += 'Please choose if filled bankrupcy in the past 10 years. \n'; 
		SubErr($("bankrupcy"));
	}
	
	
	
	return err;
}

//does final validation
function valfinal(){
	var err = "";
	
	if(document.myform.TYPE_DESIREDb[document.myform.TYPE_DESIREDb.selectedIndex].value == "NewPurchase"){
		var jj = 1;
	}else{
		if(isZip(document.myform.zip.value)==true){
			err += 'Zip Code must be 5 digit number. \n';
			SubErr($("zip"));
			//new Effect.Highlight(document.myform.zip, {startcolor:'#b5ea5e', endcolor:'#ff6600', duration:10});
		}
		err += validar2();	
		err += validar3();
		err += validar4();
	}
	
	
	
	if($("first_name").value.length < 2){
		err += 'You should type Your First Name. \n';
		SubErr($("first_name"));
	}
	if(document.myform.last_name.value.length < 2){
		err += 'You should type Your Last Name. \n';
		new Effect.Highlight(document.myform.last_name, {startcolor:'#b5ea5e', endcolor:'#ff6600', duration:10});
	}
	var stremail = document.myform.email.value;
	if(stremail.length < 2){
		err += "You must type a valid Email. \n";
		new Effect.Highlight(document.myform.email, {startcolor:'#b5ea5e', endcolor:'#ff6600', duration:10});
		
	}else{
		if (echeck(stremail)==false){
			err += "You must type a valid Email. \n";
			new Effect.Highlight(document.myform.email, {startcolor:'#b5ea5e', endcolor:'#ff6600', duration:10});
		}
	}
	if(document.myform.home_area_code.value.length < 3){
		err += "Home Area Code should be 3 numberic digits. \n";
		new Effect.Highlight(document.myform.home_area_code, {startcolor:'#b5ea5e', endcolor:'#ff6600', duration:10});
	}else{
		if(isNaN(document.myform.home_area_code.value)==true){
			err += "Home Area Code should be numeric. \n";
			new Effect.Highlight(document.myform.home_area_code, {startcolor:'#b5ea5e', endcolor:'#ff6600', duration:10});
		}
	}
	
	if(document.myform.home_phone_prefix.value.length < 3){
		err += "Home Phone Prefix should be 3 numberic digits. \n";
		new Effect.Highlight(document.myform.home_phone_prefix, {startcolor:'#b5ea5e', endcolor:'#ff6600', duration:10});
	}else{
		if(isNaN(document.myform.home_phone_prefix.value)==true){
			err += "Home Phone should be 3 numberic digits. \n";
			new Effect.Highlight(document.myform.home_phone_prefix, {startcolor:'#b5ea5e', endcolor:'#ff6600', duration:10});
		}
	}
	
	if(document.myform.home_phone_suffix.value.length < 4){
		err += "Home Phone suffix should be 4 numberic digits. \n";
		new Effect.Highlight(document.myform.home_phone_suffix, {startcolor:'#b5ea5e', endcolor:'#ff6600', duration:10});
	}else{
		if(isNaN(document.myform.home_phone_suffix.value)==true){
			err += "Home Phone suffix should be 4 numberic digits. \n";
			new Effect.Highlight(document.myform.home_phone_suffix, {startcolor:'#b5ea5e', endcolor:'#ff6600', duration:10});
		}
	}	
	if(isNaN(document.myform.work_area_code.value)==true){
		err += "Work Phone should be numeric. \n";
		new Effect.Highlight(document.myform.work_area_code, {startcolor:'#b5ea5e', endcolor:'#ff6600', duration:10});
	}
	if(isNaN(document.myform.work_phone_prefix.value)==true){
		err += "Work Phone should be numeric. \n";
		new Effect.Highlight(document.myform.work_phone_prefix, {startcolor:'#b5ea5e', endcolor:'#ff6600', duration:10});
	}
	if(isNaN(document.myform.work_phone_suffix.value)==true){
		err += "Work Phone should be numeric. \n";
		new Effect.Highlight(document.myform.work_phone_suffix, {startcolor:'#b5ea5e', endcolor:'#ff6600', duration:10});
	}
	if(document.myform.calltime[document.myform.calltime.selectedIndex].value.length < 2){
		err += "Please Select Best Time to call. \n";
		new Effect.Highlight(document.myform.calltime, {startcolor:'#b5ea5e', endcolor:'#ff6600', duration:10});
	}	
	if(err.length > 5){
		alert(err);
	}else{
		document.myform.submit();
	}
}