$(document).ready(function() {




	$("#lendingform").submit(function() {
		lendingemail = $('#lendinginput').val();
		var emailPattern = /^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/;
		
		if (!emailPattern.test(lendingemail)) {
			$('#lendinginput').focus();
			alert("Please fill in a valid e-mail address.");
		} else {
			
			$.post("/lending", {
				"EMAIL_REQUIRED" : "T",
				"EMAIL_DATATYPE" : "email",
				"SRC_REQUIRED" : "F",
				"SRC_DATATYPE" : "text",
				"SRC" : "fhaloanpros",
				"lending_lowdown" : "lending_lowdown",
				"EMAIL" : lendingemail
			}, function(result) {
				if (result == "email") {
					$('#lendinginput').focus();
					alert("Please fill in a valid e-mail address.");
				} else {

					var msgTitle = "Your comment has been submitted!";
					$("#lendingText").html(result);
					$("#lendingopt").html('');
					

				}

			});

		}
		return false;

	});

});

function clearText(obj)	{
	if(obj.value=='Enter Zip') {
		obj.value = '';
		return true;
	}
	else
	{
		obj.value=='Enter Zip';
		return true;
	}		
}
function setText(obj)	{
	if(obj.value=='') {
		obj.value = 'Enter Zip';
		return true;
	}
}


$(document).ready(function(){

  //form step one
    var letUsWork = $("#letUsWork");
    var yourChoice = $("#yourChoice");

	 $('input[type="submit"]').fadeTo("slow", 100);
	 $('input[type="submit"]').removeAttr("disabled");
	$('#gtlformhome').submit(function() {
	
   var err = ''; 
   if (!$('#rtn-state').attr('value')) err+="Please select a State!\n";
 if (!$('#rtn-pr_type').attr('value')) err+="Please select a Property Type!\n";
 if (!($('#rtn-radio1').attr('checked') || $('#rtn-radio2').attr('checked') || $('#rtn-radio3').attr('checked') || $('#rtn-radio4').attr('checked'))) err+="Please select a Credit Rating!\n";
 if(err.length>0){
 alert(err);
 return false;
 }  
 var yourChoice_checked = document.forms['gtlformhome'].elements['Choice'][1].checked;

  if(yourChoice_checked == true)
	{
		$('#gtlformhome').attr('action','fha-mortgage-rates/');
	}

	else 
		{
			var splitquad= $("#splitquad").attr('title');
		$('#gtlformhome').attr('action',splitquad);
		
		}
  return true;
});
	
	if(document.forms['gtlformhome']) {
		var letuswork_checked = document.forms['gtlformhome'].elements['Choice'][0].checked;
		var yourChoice_checked = document.forms['gtlformhome'].elements['Choice'][1].checked;	
		document.forms['gtlformhome'].elements['rtn-loan_type'][0].checked = true;	
		document.forms['gtlformhome'].elements['rtn-radio'][1].checked = true;	
		
		if( letuswork_checked == true && yourChoice_checked == false ) {
			letUsWork.addClass('Selected');
			yourChoice.removeClass('Selected');
			document.forms['gtlformhome'].elements['Choice'][1].checked = false;
			document.forms['gtlformhome'].elements['Choice'][0].checked = true;
				
		} else if(letuswork_checked == false && yourChoice_checked == true ) {
			yourChoice.addClass('Selected');
			letUsWork.removeClass('Selected');
			document.forms['gtlformhome'].elements['Choice'][0].checked = false;
			document.forms['gtlformhome'].elements['Choice'][1].checked = true;
		} else {
			letUsWork.addClass('Selected');
			yourChoice.removeClass('Selected');
			document.forms['gtlformhome'].elements['Choice'][1].checked = false;
			document.forms['gtlformhome'].elements['Choice'][0].checked = true;
		}			
	}	
	
    letUsWork.click(function() {
        if($(this).hasClass('Selected')) {
            $(this).removeClass('Selected');
			
		}
        else {
			document.forms['gtlformhome'].elements['Choice'][1].checked = false;
			document.forms['gtlformhome'].elements['Choice'][0].checked = true;
			
			$('#homeEquity,#debtConsolidation, #creditrating, .creditrating ,#propertyType').css('display', 'block');
			$(this).addClass('Selected');
			
			if(yourChoice.hasClass('Selected'))
            	yourChoice.removeClass('Selected');
		}
    });
    yourChoice.click(function() {
        if($(this).hasClass('Selected')) {
            $(this).removeClass('Selected');
			
		}
        else {
			document.forms['gtlformhome'].elements['Choice'][1].checked = true;
			document.forms['gtlformhome'].elements['Choice'][0].checked = false;
			$('#homeEquity,#debtConsolidation, #creditrating, .creditrating ,#propertyType').css('display', 'none');
			
            $(this).addClass('Selected');
			
			if(letUsWork.hasClass('Selected'))
            	letUsWork.removeClass('Selected');
		}
    });

	//form step two
	var loanType = new Array('refinance', 'homeLoan', 'homeEquity', 'debtConsolidation');
	var x;	
	var cardRating = new Array('excellent', 'good', 'fair', 'poor');	
		
	$('#refinance').click(function(){
		$(this).addClass('Selected');
		document.forms['gtlformhome'].elements['rtn-loan_type'][0].checked = true;
		removeAnother('refinance',loanType);
	});
	$('#homeLoan').click(function(){
		$(this).addClass('Selected');
		document.forms['gtlformhome'].elements['rtn-loan_type'][1].checked = true;
		removeAnother('homeLoan',loanType);
	});
	$('#homeEquity').click(function(){
		$(this).addClass('Selected');
		document.forms['gtlformhome'].elements['rtn-loan_type'][2].checked = true;
		removeAnother('homeEquity',loanType);
	});
	$('#debtConsolidation').click(function(){
		$(this).addClass('Selected');
		document.forms['gtlformhome'].elements['rtn-loan_type'][3].checked = true;
		removeAnother('debtConsolidation',loanType);
	});
	
	$('#excellent').click(function(){
	
		$(this).addClass('Selected');
		document.forms['gtlformhome'].elements['rtn-radio'][0].checked = true;
		removeAnother('excellent',cardRating);
	});
	$('#good').click(function(){
		$(this).addClass('Selected');
		document.forms['gtlformhome'].elements['rtn-radio'][1].checked = true;
		removeAnother('good',cardRating);
	});
	$('#fair').click(function(){
		$(this).addClass('Selected');
		document.forms['gtlformhome'].elements['rtn-radio'][2].checked = true;
		removeAnother('fair',cardRating);
	});
	$('#poor').click(function(){
		$(this).addClass('Selected');
		document.forms['gtlformhome'].elements['rtn-radio'][3].checked = true;
		removeAnother('poor',cardRating);
	});

	function removeAnother(string, element)
	{	
		for(x in element)
		{
			
			if(element[x] != string) {
				$("#"+element[x]).removeClass("Selected");
				$("#"+element[x]).checked = false;
			}
		}
	}	
	
	
	
	
	
});		





