function creditStepOne(){
	
	var sendForm = true;
	
	$('#credit1 input:not(#credit_resend,#cosign_credit_resend)').each(function() {
		if( $(this).val() == '' ){
			$(this).css('border-color','red');
			sendForm = false;
		}
	});
	
	if (sendForm){
		$('#credit1 input').css('border-color','#cccccc');
		
		//send info
		$.post("http://www.lowbooksales.com/RV/lead_form_ajax.asp",$('#credit1').serialize(),function(data){
			//alert(data);
			if( !$('#credit-form').data('cosign') ){
				$('#credit_resend').val(data);
			}
			
			if( isFinite($('#credit_resend').val()) ){
				$('#credit-step2').css('display','');
				$('#credit_b1').css('display','none');
			}
			
			var xCoordinate = ( typeof(window.pageXOffset) == 'number' ) ? window.pageXOffset : document.body.scrollLeft;
			window.scrollTo(xCoordinate,0);
		});
		
	} else {
		alert('Please enter a value for highlighted fields.');
	}
}

function creditStepTwo(){
	
	//open review div/populated
	$('#credit_button2').css('display','none');
	$('#credit-step3').css('display','');
	//$('#credit-review').css('display','');
}

function creditStepThree(){
	
	if( $('#credit_terms').attr('checked') ){
		var formString = $('#credit1').serialize() + "&" + $('#credit2').serialize();
		
		var sendForm = true;
		$('#credit2 input,#credit2 select').each(function() {
			if( $(this).val() == '' ){
				$(this).css('border-color','red');
				sendForm = false;
			}
		});
		
		if (sendForm){
			$('#credit2 input,#credit2 select').css('border-color','#cccccc');
			if( $('#credit_terms').attr('checked') && $('#credit-review').css('display') == 'none' )
				$.post("http://www.lowbooksales.com/RV/lead_form_ajax.asp",formString,function(data){
					
					if( data.toLowerCase() == "success" ){
						$('#credit-form').prepend('<div class="credit-message"></div>')
						.find('.credit-message').css({'font-size':'14px','margin-left':'160px','color':'blue'}).html('Primary applicant information has been successfully saved.')
						.end().hide();
						$('#credit-submitted').show();
						
						if( !$('#credit-form').data('cosign') )
							$('#credit-coapp-add').show();
					}
					
					var xCoordinate = ( typeof(window.pageXOffset) == 'number' ) ? window.pageXOffset : document.body.scrollLeft;
					window.scrollTo(xCoordinate,0);
				});
		} else {
			alert('Please enter a value for highlighted fields.');
		}
	} else {
		alert("You must accept the terms to continue.");
		$('#credit-review').css('display','');
	}
}

function hasCosign(){
	resetCreditForm();
	
	$('#credit-form').data('cosign',true);
}

function resetCreditForm(){
	var jango = $('#credit1 input, #credit2 input, #credit2 select');
	var boba = jango.clone();
	boba.wrapAll('<div id="original-credit-app"></div>').appendTo('#credit1').hide();
	$('#original-credit-app select').each(function(){
		var input = "<input type='hidden' name='"+ this.name +"' value='"+ $(this).val() +"'/>";
		
		$(this).after(input);
	}).remove();
	
	jango.each(function(){
		var id = 'cosign_' + this.id;
		var name = 'cosign_' + this.name;
		
		this.id = id;
		this.name = name;
		
		$(this).val('');
	});
	
	$('<input type="hidden" name="cosigner" value="1"/>').appendTo('#credit1');
	
	//open review div/populated
	$('#credit_b1').css('display','');
	$('#credit_button2').css('display','');
	$('#credit-step2').css('display','none');
	$('#credit-step3').css('display','none');
	$('#credit-review').css('display','none');
}

function showTerms(){
	$('#credit-review').css('display','');
	return false;
}

$(document).ready(function(){
	
	$("#credit-form input,#credit-form select").change(function(){
		$(this).css("border-color","#cccccc");
	});
	$("#credit-form .numbers input").change(function(){
		var self = $(this);
		if( isNaN(self.val()) ){
			self.val('').css("border-color","red");
			alert('Please enter a numeric value for this field.');
		}
	});
	
	$('#credit-form a.terms-link').click(function(){
		showTerms();
		return false;
	});
	
	$('#credit-coapp-add .credit-coapp-add-button').click(function(){
		var value = this.value;
		
		if( value == 'joint' ){
			$('#credit-submitted,#credit-coapp-add').hide();
			$('#credit-form').show();
			$('#credit-header').css('color','blue').html('Co-Applicant');
			
			hasCosign();
		} else {
			$('#credit-coapp-add').hide();
			$('#credit-submitted2').show();
			$('#credit-submitted').hide();
		}
	});
	
});
