if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i)) {
	var viewportmeta = document.querySelectorAll('meta[name="viewport"]')[0];
	if (viewportmeta) {
		viewportmeta.content = 'width=device-width, minimum-scale=1.0, maximum-scale=1.0';
		document.body.addEventListener('gesturestart', function() {
			viewportmeta.content = 'width=device-width, minimum-scale=0.25, maximum-scale=1.6';
		}, false);
	}
}

if($('html').children('.home').length == 0) { $('html').css('background', '#eee'); }

// Set up the slow-release form
$('form section').not('form section:first-child').hide();

$('#loanbalance legend').text("Is your current home loan balance $25,000 or more?");
$('#loanrate legend').text("Is your current home loan rate 8.00% or higher?");

$('.hero-button').click(function(event) {
	event.preventDefault();
	$('#hero').fadeOut(500, function() {
		$('.content').fadeIn(500);
		$('html').animate({
			'background-color': '#eee'
		}, 500);
	});
});

var validator = $('form').validate({
	errorElement: 'span',
	errorPlacement: function(error, element) {
		if(element.attr('type') == 'radio') {
			error.appendTo(element.parents("fieldset"));
		}
		else {
			error.appendTo(element.parent());
		}
	},
	highlight: function(element, errorClass) {
		if($(element).parents('fieldset').length > 0) {
			$(element).parents('fieldset').addClass(errorClass);
		} else {
			$(element).parents('label').addClass(errorClass);
		}
	},
	unhighlight: function(element, errorClass) {
		if($(element).parents('fieldset').length > 0) {
			if($(element).parents('fieldset').find('span.error:visible').length == 0) {
				$(element).parents('fieldset').removeClass(errorClass);
			}
		} else {
			$(element).parents('label').removeClass(errorClass);
		}
	}
});

var states = new Array('MA', 'ME', 'MI', 'RI', 'VT', 'NH', 'CT', 'NJ', 'AK', 'HI');
var credit = new Array('average', 'poor', 'horrible', 'none');

$.validator.addMethod('validState', function(value, element) {
	var valid = $.inArray(value, states);
	if(valid == -1) { return true; }
	else { return false; }
}, "We don't handle applications in this state.");

$.validator.addMethod('validSingleYear', function(value, element) {
	var width = $('[name="width"]:checked').val();
	if(width === 'single')
	{
		if(value < 1996) { return false; }
		else { return true; }
	} else { return true; }
}, "We don't work with single-wide homes that are older than 1996.");

$.validator.addMethod('validDoubleYear', function(value, element) {
	var width = $('[name="width"]:checked').val();
	if(width === 'double')
	{
		if(value < 1976) { return false; }
		else { return true; }
	} else { return true; }
}, "We don't work with multi-section homes that are older than 1976.");

$.validator.addMethod('validDown', function(value, element) {
	var down = $('[name="down"]:checked').val();
	if(down == 'no') { return false; }
	else { return true; }
}, "We cannot handle applications where the down payment is less than 5% of the sale price.");

$.validator.addMethod('validBankruptcy', function(value, element) {
	var bankruptcy = $('[name="bankruptcy"]:checked').val();
	if(bankruptcy == 'yes') { return false; }
	else { return true; }
}, "We can't pass along your information if you've had an undischarged bankruptcy within the last five years.");

$.validator.addMethod('validRepo', function(value, element) {
	var repo = $('[name="repo"]:checked').val();
	if(repo == 'yes') { return false; }
	else { return true; }
}, "We can't pass along your information if you've had a reposession in the last seven years.");

$.validator.addMethod('validRepay', function(value, element) {
	var repay = $('[name="repay"]:checked').val();
	if(repay == 'yes') { return false; }
	else { return true; }
}, "We can't pass along your information if you've been more than 30 days late on a credit card or loan payment in the last two years.");

$.validator.addMethod('validEmployment', function(value, element) {
	var employment = $('[name="employment"]:checked').val();
	if(employment != 'employed') { return false; }
	else { return true; }
}, "We can't pass along your information if you aren't currently employed.");

$.validator.addMethod('validLoanBalance', function(value, element) {
	var loanbalance = $('[name="loanbalance"]:checked').val();
	var type = $('[name="type"]:checked').val();
	if(type == 'refinancing')
	{
		if(loanbalance == 'no') { return false; }
		else { return true; }
	} else { return true; }
}, "We can't help you refinance if your current home loan balance is less than $25,000.");

$.validator.addMethod('validLoanRate', function(value, element) {
	var loanrate = $('[name="loanrate"]:checked').val();
	var type = $('[name="type"]:checked').val();
	if(type == 'refinancing')
	{
		if(loanrate == 'no') { return false; }
		else { return true; }
	} else { return true; }
}, "We can't help you refinance if your current home loan rate is less than 8.00%.");

$.validator.addMethod('validCredit', function(value, element) {
	var valid = $.inArray(value, credit);
	if(valid == -1) { return true; }
	else { return false; }
}, "We can only accept applicants whose credit score is Excellent or Good.");

$('[name="f_name"]').rules('add', {required: true});
$('[name="l_name"]').rules('add', {required: true});
$('[name="phone"]').rules('add', {required: true, phoneUS: true});
$('[name="state"]').rules('add', {required: true, validState: true});

$('[name="year"]').rules('add', {required: true, validDoubleYear: true, validSingleYear: true});
$('[name="type"]').rules('add', {required: true});
$('[name="width"]').rules('add', {required: true});
$('[name="down"]').rules('add', {required: true, validDown: true});

$('[name="bankruptcy"]').rules('add', {required: true, validBankruptcy: true});
$('[name="repo"]').rules('add', {required: true, validRepo: true});
$('[name="repay"]').rules('add', {required: true, validRepay: true});
$('[name="employment"]').rules('add', {required: true, validEmployment: true});
$('[name="credit"]').rules('add', {required: true, validCredit: true});

$('[name="loanbalance"]').rules('add', {validLoanBalance: true});
$('[name="loanrate"]').rules('add', {validLoanRate: true});

$('[name="loanbalance"]').rules('add', {required: function(element) {
	return $('[name="type"]').val() != 'refinancing';
}});
$('[name="loanrate"]').rules('add', {required: function(element) {
	return $('[name="type"]').val() != 'refinancing';
}});

var currentSection = 'personal_info';

var clicked = 0;
$('#next').click(function() {
	clicked = 1;
	var section = $('section:visible').length;
	var errors = 0;
	$('section:visible input, section:visible select, section:visible textarea').each(function() {
		if( ! $('form').validate().element($(this))) {
			errors++;
		}
	});
	if(errors == 0) {
		$('section:nth-child('+(section+1)+')').show();
		currentSection = $('section:nth-child('+(section+1)+')').attr('id');
		$.scrollTo('section:nth-child('+(section+1)+')', 500);
	}
	else
	{
		var element = $('span.error:visible:first').attr('for');
		$.scrollTo('#'+element, 500);
	}
	
	if(currentSection == 'refinance_info') {
		$('#next').hide();
		$('#submit_button').show();
	} else {
		if(currentSection == 'financial_info' && $('[name="type"]:checked').val() != 'refinancing') {
			$('#next').hide();
			$('#submit_button').show();
		} else {
			$('#next').show();
			$('#submit_button').hide();
		}
	}
});
$('#submit_button').click(function(event) {
	event.preventDefault();
	var errors = 0;
	$('section:visible input, section:visible select, section:visible textarea').each(function() {
		if( ! $('form').validate().element($(this))) {
			errors++;
		}
	});
	if(errors == 0) {
		$('form').submit();
	}
	else
	{
		var element = $('span.error:visible:first').attr('for');
		$.scrollTo('#'+element, 500);
	}
});

$('[name="type"]').change(function() {
	if(currentSection == 'financial_info') {
		if($('[name="type"]:checked').val() == 'refinancing') {
			$('#next').show();
			$('#submit').hide();
		} else {
			$('#next').hide();
			$('#submit').show();
		}
	}
	if(currentSection == 'refinancing_info') {
		if($('[name="type"]:checked').val() != 'refinancing') {
			$('#refinance_info').hide();
		}
	}
	
	if($('[name="type"]:checked').val() == 'refinancing') {
		$('.total').text('4');
	} else { $('.total').text('3'); }
})
