// CONSTANTS
ADD_FOR_CONDO = 0;
TYPICAL_FOR_MORTGAGE = 15;

function start() {
	
	// ATTACH FIELD LISTENERS
	bind_field_listeners();
	
	// HIDE ALL UNNECESSARY FIELDS AT THE START
	hide_hidden_fields();
	
	// INITIALIZE ANY NECESSARY FIELD VALUES TO THEIR DEFAULTS
	initialize_fields();
}

function bind_field_listeners() {
	// do field bindings - for submit form
	$("#loan_type").bind("change", loan_type_listener);
	$("#mortgage_pages").bind("change", mortgage_listener);
	$("#subordination_agreement_needed").bind("change", subordination_agreement_listener);
	$("#power_of_attorney_needed").bind("change", power_of_attorney_listener);
	$("#reissue_rate_credit_requested").bind("change", reissue_rate_credit_listener);
	$("#assignment_needed").bind("change", assignment_listener);
	$("#property_type").bind("change", property_type_listener);
	$("#survey").bind("change", survey_listener);
	$("#reverse_mortgage").bind("change", reverse_mortgage_listener);
	
	$("#loan_amount").bind("change", loan_amount_listener);
	
	$("#state").bind("change", state_listener );
	
	$("#new_lender_same_as_old").bind("change", new_lender_same_as_old_listener ); //old_lender_same_as_new_listener );
	
	
	// LINE LISTENERS FOR CALCULATED FIELDS
	$("#gfe4").bind("click", gfe4_listener );
	
	// BUTTON LISTENERS
	//$("#dc_goBack").bind("click", dc_go_back_listener );
	//$("#va_goBack").bind("click", va_go_back_listener );
	$("#goBack").bind("click", go_back_listener );
	$("#submit").click( submit_listener );
	$("#print").click( print_listener );
	
	
}


////////////////////////////////////////////////////////
// Special or New Listeners
////////////////////////////////////////////////////////
function test_listener() { 
	alert("TEST LISTENER");
}

function state_listener() {
	var state = $(this).attr("value");
	
	if( state == "dc" ) {
		//alert("displaying dc!");
		$(".va").css("display", "none");
		//$(".old_lender_is_same").css("display", "none");
		$("#old_lender_same_as_new").attr("value", "no");
	}
	else if( state == "va" ) {
		//alert("displaying va!");
		$(".va").css("display", "");
		$(".old_lender_is_same").css("display", "none");
	}
	else if( state == "md" ) {
		//alert("displaying md!");
	}
	
}

function gfe4_listener() {
	//alert("this is it!");
	//var display = $("#gfe4_breakdown").css("display");
	
	if( !$("#gfe4_breakdown").hasClass("hidden") ) {//if(display == "block") {
		/*$('#gfe4_breakdown').slideUp( function() { 
			$('.expand_icon').attr({src: "css/expandV.gif"});
		});		
		//alert( $('#gfe4_breakdown img').attr("class") );
		*/
		$("#gfe4_breakdown").addClass("hidden");
		$('.expand_icon').attr({src: "css/expandV.gif"});
	}
	else {
		$("#gfe4_breakdown").removeClass("hidden");
		$('.expand_icon').attr({src: "css/contractV.gif"});
		/*
		$('#gfe4_breakdown').slideDown( function() {
			$('.expand_icon').attr({src: "css/contractV.gif"});
		});
		//alert( $('#gfe4_breakdown img') );
		*/
	}
}

////////////////////////////////////////////////////////
// Form Configuration Functions
////////////////////////////////////////////////////////
function hide_hidden_fields() {
	$(".refinance").css("display", "none");
	$(".subordination").css("display", "none");
	$(".assignment").css("display", "none");
	$(".power_of_attorney").css("display", "none");
	$(".reissue_rate_credit").css("display", "none");
	
	$(".survey").css("display", "none");
	$(".survey_required").css("display", "none");
	$(".survey_not_required").css("display", "none");
	
	// VA Specific
	$(".va").css("display", "none");
	$(".old_lender_is_same").css("display", "none");
	
	$(".reverse_mortgage").css("display", "none");
	
	// $("#gfe4_breakdown").css("display", "none");
	$("#gfe4_breakdown").addClass("hidden");
	
	$(".loading").hide();
}

function initialize_fields() {
	$("#state").attr("value", "dc");
	
	// Initialize fields to default values if needed (ensures default values are maintained even if user 
	// hits the Back button)
	$("#loan_type").attr("value", "purchase");
	$("#power_of_attorney_needed").attr("value", "no"); // Make sure that Power of Attorney defaults to "No"
	$("#subordination_agreement_needed").attr("value", "no"); // Make sure that Power of Attorney defaults to "No"
	$("#reverse_mortgage").attr("value", "no"); 
	$("#property_type").attr("value", ""); 

	$("#survey").attr("value", "no");
	
	$("#old_lender_same_as_new").attr("value", "no");
	
	$("#reissue_rate_credit_requested").attr("value", "no");
	
	$("#mortgage_pages").attr("value", 15);
	$("#hud_mortgage_pages").attr("value", 8);
}

//////////////////////////////////////////////////////////
// AJAX FUNCTIONS
//////////////////////////////////////////////////////////
function content_callback(responseText, statusText) {
	//alert("clicked!");
	$("#content").html( responseText );
	
	start();
	
	$(".loading").hide();
}

//////////////////////////////////////////////////////////
// BUTTON LISTENER FUNCTIONS
//////////////////////////////////////////////////////////
function submit_listener() {
	$(".loading").show();
	
	var form_data = $('#loan_values_form').formSerialize();
	
	var state = $("#state").attr("value");
	$.get( state + "_loan_calculated_headless.php", form_data, content_callback );
	
	return false;
}

function print_listener() {
	window.print();
	
	return false;
}

function dc_go_back_listener() {
	$(".loading").toggle();
	
	$.get( "dc_loan_headless.php", {}, content_callback );
}

function va_go_back_listener() {
	$(".loading").toggle();
	
	$.get( "dc_loan_headless.php", {}, content_callback );
}

function go_back_listener() {
	$(".loading").toggle();
	
	$.get( "dc_loan_headless.php", {}, content_callback );
}

//////////////////////////////////////////////////////////
// FIELD LISTENER FUNCTIONS
//////////////////////////////////////////////////////////
function new_lender_same_as_old_listener() {
	//alert("ASDFASDF");
	var old_lender_is_same = $("#new_lender_same_as_old").attr("value");
	
	if( old_lender_is_same == "yes" ) {
		$(".old_lender_is_same").css("display", "");
	}
	else {		
		$(".old_lender_is_same").css("display", "none");
	}
}

function loan_type_listener() {
	var loan_type = $(this).attr("value");
	if( loan_type == "purchase") {
		$(".purchase").css("display", "");
		$(".refinance").css("display", "none");
	}
	else if( loan_type == "refinance" ) {
		$(".purchase").css("display", "none");
		$(".refinance").css("display", "");
		$(".subordination").css("display", "none");
	}
}

function mortgage_listener() {
	var needed = $(this).attr("value");
	if( needed == "none" ) {
		$(".assignment").css("display", "none");
		//$("#assignment_pages").attr("value", "0");
		
		//alert("assignment pages now set to " + $("#assignment_pages").attr("value") );
	}
	else {
		$(".assignment").css("display", "");
		//$("#assignment_pages").attr("value", "2");
	}
}

function power_of_attorney_listener() {
	var needed = $(this).attr("value");
	if( needed == "yes" ) {
		$(".power_of_attorney").css("display", "");
		$("#power_of_attorney_borrowers").attr("value", "1");
	}
	else {
		$(".power_of_attorney").css("display", "none");
		$("#power_of_attorney_borrowers").attr("value", "0");
		//alert("power_of_attorney_borrowers now set to " + $("#power_of_attorney_borrowers").attr("value") );
	}
}

function reissue_rate_credit_listener() {
	var needed = $(this).attr("value");
	if( needed == "yes" ) {
		$(".reissue_rate_credit").css("display", "");
	}
	else {
		$(".reissue_rate_credit").css("display", "none");
		$("#old_title_policy").attr("value", "");
	}
}

function subordination_agreement_listener() {
	var needed = $(this).attr("value");
	// alert("ASDFASF");
	if( needed == "yes" ) {
		$(".subordination").css("display", "");
		//$("#power_of_attorney_borrowers").attr("value", "1");
	}
	else {
		$(".subordination").css("display", "none");
		//$("#power_of_attorney_borrowers").attr("value", "0");
		
	}
}

function reissue_rate_credit_listener() {
	var needed = $(this).attr("value");
	if( needed == "yes" ) {
		$(".reissue_rate_credit").css("display", "");
	}
	else {
		$(".reissue_rate_credit").css("display", "none");
		$("#old_title_policy").attr("value", "");
	}
}

function assignment_listener() {
	var needed = $(this).attr("value");
	if( needed == "yes" ) {
		$(".assignment").css("display", "");
		//$("#assignment_pages").attr("value", "1");
	}
	else {
		$(".assignment").css("display", "none");
		//$("#power_of_attorney_borrowers").attr("value", "0");
		//alert("power_of_attorney_borrowers now set to " + $("#power_of_attorney_borrowers").attr("value") );
	}
}

function loan_amount_listener() {
	var text = $(this).attr("value");
		//alert("changing loan amount" + text );
	if( text == "" ) {
		$(".mortgage").css("display", "none");
	}
	else
		$(".mortgage").css("display", "");
}

function property_type_listener() {
	var text = $(this).attr("value");
	
	if( text == "condo" ) {
		ADD_FOR_CONDO = 4;
		
		// common value to be used below
		var typical_plus_condo = (TYPICAL_FOR_MORTGAGE + ADD_FOR_CONDO);
		
		// change tooltips
		$(".mortgage").qtip("api").updateContent( "<span>" + typical_plus_condo + "</span> pages is typical, but <span>" + typical_plus_condo*2 + "</span> is a safe estimate", false );
		$(".reverse_mortgage").qtip("api").updateContent( "<span>" + typical_plus_condo + "</span> pages is typical, but <span>" + typical_plus_condo*2 + "</span> is a safe estimate", false );
		
		// change labels
		$(".mortgage label").html( "# of Pages in Deed of Trust / Mortgage:" );
		$("#mortgage_pages").attr("value", typical_plus_condo );
		$(".reverse_mortgage label").html( "# of Pages in HUD Deed of Trust / Mortgage:" );
		$("#hud_mortgage_pages").attr("value", typical_plus_condo );
		
		$(".survey").css("display", "none");
		// $("#survey").attr("value", "yes");
		$("#survey").attr("value", "no");
		$(".survey_required").css("display", "none");
		$(".survey_not_required").css("display", "none");
	}
	else if( text == "house" ) {
		ADD_FOR_CONDO = 0;
		
		// common value to be used below
		var typical_plus_condo = (TYPICAL_FOR_MORTGAGE + ADD_FOR_CONDO);
		
		// change tooltips
		$(".mortgage").qtip("api").updateContent( "<span>" + typical_plus_condo + "</span> pages is typical, but <span>" + typical_plus_condo*2 + "</span> is a safe estimate", false );
		$(".reverse_mortgage").qtip("api").updateContent( "<span>" + typical_plus_condo + "</span> pages is typical, but <span>" + typical_plus_condo*2 + "</span> is a safe estimate", false );
		
		// change labels
		$(".mortgage label").html( "# of Pages in Deed of Trust / Mortgage:" );
		$("#mortgage_pages").attr("value", typical_plus_condo );
		$(".reverse_mortgage label").html( "# of Pages in HUD Deed of Trust / Mortgage:" );
		$("#hud_mortgage_pages").attr("value", typical_plus_condo );
		
		$(".survey").css("display", "");
		$("#survey").attr("value", "yes");
		$(".survey_required").css("display", "");
		$(".survey_not_required").css("display", "none");
	}
}

function survey_listener() {
	var text = $(this).attr("value");
		//alert("changing loan amount" + text );
	if( text == "yes" ) {
		$(".survey_required").css("display", "");
		$(".survey_not_required").css("display", "none");
	}
	else {
		$(".survey_required").css("display", "none");
		$(".survey_not_required").css("display", "");
	}
}

function reverse_mortgage_listener() {
	var text = $(this).attr("value");
		//alert("changing loan amount" + text );
		
	if( text == "no" ) {
		TYPICAL_FOR_MORTGAGE = 15;
		
		$(".reverse_mortgage").css("display", "none");
	}
	else {
		TYPICAL_FOR_MORTGAGE = 8;
		//alert("add for condo: " + ADD_FOR_CONDO);
		
		$(".reverse_mortgage").css("display", "");
		
	}
	
	// common value to be used below
	var typical_plus_condo = (TYPICAL_FOR_MORTGAGE + ADD_FOR_CONDO);
	
	// change tooltip values
	$(".mortgage").qtip("api").updateContent( "<span>" + typical_plus_condo + "</span> pages is typical, but <span>" + typical_plus_condo*2 + "</span> is a safe estimate", false );
	// i don't know why, but this required a bit of a hack.. needed to get into the actual property rather than use the modifier method
	$(".reverse_mortgage").qtip("api").options.content.text = "<span>" + typical_plus_condo + "</span> pages is typical, but <span>" + typical_plus_condo*2 + "</span> is a safe estimate";
	
	// change html of label
	$(".mortgage label").html( "# of Pages in Deed of Trust / Mortgage: " );
	$("#mortgage_pages").attr("value", typical_plus_condo );
	$(".reverse_mortgage label").html( "# of Pages in HUD Deed of Trust / Mortgage:" );
	$("#hud_mortgage_pages").attr("value", typical_plus_condo );

}

// test methods
