// Other functions

function clearSession (){
	$.ajax({
		async: false,
		type: 'GET',
		url: '/bininc/util/clearSession_index.a4p'
	});
}

function updateSession (){
	$.ajax({
		async: false,
		type: 'GET',
		url: '/bin_netadvance/updateSession_index.a4p',
		data: {'TCA_NetAdvance' : $("#Home_Amount").val(), 'TCA_CompanyZip' : $("#Home_Zip").val(), 'TCA_BrokerOfRecord' : $("#BrokerOfRecord").val(), 'TCA_TargetMarket' : $("#TargetMarket").val()},
		dataType: 'json',
		error: function(){
			// ERROR 
		},
		success: function(data){
			// SUCCESS
		}
	});
}

function ZipCheck(option){
	var errMsg = "We're sorry. <strong>NetAdvance</strong> is currently unavailable in your area.<br>We are adding new markets daily so please check back soon.<br>In the meantime, please visit <a href='http://www.ecommission.com'>eCommission</a>.<br>They may be able to process an advance for you.";
	var zip=$("#Home_Zip").val();

	if (zip.length === 5){
		$.ajax({
			async: false,
			type: 'GET',
			url: '/bininc/util/marketCheck.a4p',
			data: {'zip' : zip},
			dataType: 'json',
			error: function(){
				// ERROR 
			}, // error end
			success: function(data){
				var marketResult = data.result;
				switch (marketResult)
				{
				case "active":
					$("#BrokerOfRecord").val("No");
					$("#TargetMarket").val("No");
					$("#applyButtonContainer").fadeIn();
					$("#lookupInfo").html("");
					break;
				
				case "target":
				 if (option !== undefined){
					$( "#dialog-confirm" ).dialog({
						closeOnEscape: false, resizable: false, height:140, modal: true, 
						buttons: {
							"Yes": function() {
								$("#BrokerOfRecord").val("Yes");
								$("#TargetMarket").val("Yes");
								$( this ).dialog( "close" );
								$("#applyButtonContainer").fadeIn();
								$("#lookupInfo").html("");
							}, 
							"No": function() {
								clearSession();
								$("#BrokerOfRecord").val("No");
								$("#TargetMarket").val("Yes");
								$("#applyButtonContainer").hide();
								$( this ).dialog( "close" );
								$("#lookupInfo").html(errMsg);
							}
						},
						open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); } ,
						close: function(event, ui) { }
					});
				 }
					break;
				
				default:
					clearSession();
					$("#lookupInfo").html("");
					$("#applyButtonContainer").hide();
					$("#lookupInfo").html(errMsg);
				}
			} // success end
		});
	}else{
		//$("#lookupInfo").html("Please enter a Zip.");
		if (amount.length !== 0){
			$("#applyButtonContainer").hide();
		}else{
			$("#applyButtonContainer").hide();
		}
	}
}

//ON READY
$(function(){
		
	//If Continue Button is Clicked on Slider DIV
	$("#gotoStep2Button").click(function(){
		$("#Home_Amount").val( $( "#sliderControl" ).slider( "value" ) );
		$("#step1").fadeOut();
		$("#step2").fadeIn();
	});

	//If Login Button is Clicked on second step of Slider DIV
	$("#applyButton").click(function(){
		$("#Home_Amount").val( $( "#sliderControl" ).slider( "value" ) );
		updateSession();
		window.location.href = "/newapp.html";
	});
			
	//Zip Entry Field - second step of Slider DIV
	$("#Home_Zip").keyup(function(e) { // on keyup change 
		var code = (e.keyCode ? e.keyCode : e.which);
		if(code !== 9) { //dont check when tab is pressed
			ZipCheck("*");
		}
	});
	
//Some Other Inits	
	// Init hidden fields
	$("#Home_Amount").val("");	
	$("#Home_Zip").val("");	
	$("#BrokerOfRecord").val("");
	$("#TargetMarket").val("");
	
	// Clear A4D Sesssion if it exists
	clearSession();
	
	
	//Thanksgiving 2011
	$( "#holiday-message" ).dialog({
		autoOpen: false,
		modal: true,
		resizable: false,
		width: 450,
		buttons: {
			Close: function() {
				$( this ).dialog( "close" );
			}
		}
	});
	
	var x=new Date();
	x.setFullYear(2011,12-1,11);
	var today = new Date();
	if (x>today) {
		var showMsg = $.cookie('hol_msg');
		if (showMsg === null) {
			$.cookie('hol_msg', 'true');
			$('#holiday-message').dialog('open'); 
		}
	}

});


