/*
 * An SCC (selection, contact, confirmation) page is a page that contains a set of three form pages
 * that collect information from a user. These forms include a selection page, contact information
 * page, and a confirmation page. Currently, this page structure is used by the Test Drive page.
 *
 * The required functions that must be implemented by a delegate of the SCC page are:
 *   submitData()
 *   validateSelectionPage()
 *   validateContactPage()
 *
 * The functions that can optionally be implemented by a delegate of the SCC page are:
 *   aboutToShowConfirmationPage()
 *   aboutToShowContactPage()
 *   aboutToShowSelectionPage()
 *   didShowConfirmationPage()
 *   didShowContactPage()
 *   didShowSelectionPage()
 */

$.extend($.ford.pages, {scc: {}});
$.extend($.ford.pages.scc, {delegate: null});
$.extend($.ford.pages.scc, {onSubmitSelectionPage: function() {
	if($.isFunction($.ford.pages.scc.delegate.validateSelectionPage)) {
		if(!$.ford.pages.scc.delegate.validateSelectionPage()) {
			return;
		}
	}
	$.ford.pages.scc.showContactPage();
}});
$.extend($.ford.pages.scc, {onSubmitContactPage: function(event) {
	if(!$.ford.pages.scc.delegate.validateContactPage()) {
		return;
	}
	if(!$.ford.pages.scc.delegate.submitData()) {
		return;
	}
	$.ford.pages.scc.showConfirmationPage();
}});
$.extend($.ford.pages.scc, {showConfirmationPage: function() {
	if($.isFunction($.ford.pages.scc.delegate.aboutToShowConfirmationPage)) {
		$.ford.pages.scc.delegate.aboutToShowConfirmationPage();
	}

	if($('#1_div').is(':visible')) {
		$('#1_div').hide();
	}
	if($('#2_div').is(':visible')) {
		$('#2_div').hide();
	}
	if(!$('#3_div').is(':visible')) {
		$('#3_div').show();
		$('html, body').animate({scrollTop: 0}, 0);
	}
	
	if($.isFunction($.ford.pages.scc.delegate.didShowConfirmationPage)) {
		$.ford.pages.scc.delegate.didShowConfirmationPage();
	}
}});
$.extend($.ford.pages.scc, {showContactPage: function() {
	if($.isFunction($.ford.pages.scc.delegate.aboutToShowContactPage)) {
		$.ford.pages.scc.delegate.aboutToShowContactPage();
	}
	try { $.ford.pages.scc.delegate.aboutToShowContactPage(); } catch(e) {}
	
	if($('#1_div').is(':visible')) {
		$('#1_div').hide();
	}
	if($('#3_div').is(':visible')) {
		$('#3_div').hide();
	}
	if(!$('#2_div').is(':visible')) {
		$('#2_div').show();
		$('html, body').animate({scrollTop: 0}, 0);
	}

	if($.isFunction($.ford.pages.scc.delegate.didShowContactPage)) {
		$.ford.pages.scc.delegate.didShowContactPage();
	}
}});
$.extend($.ford.pages.scc, {showSelectionPage: function() {
	if($.isFunction($.ford.pages.scc.delegate.aboutToShowSelectionPage)) {
		$.ford.pages.scc.delegate.aboutToShowSelectionPage();
	}

	if($('#2_div').is(':visible')) {
		$('#2_div').hide();
	}
	if($('#3_div').is(':visible')) {
		$('#3_div').hide();
	}
	if(!$('#1_div').is(':visible')) {
		$('#1_div').show();
		$('html, body').animate({scrollTop: 0}, 0);
	}

	if($.isFunction($.ford.pages.scc.delegate.didShowSelectionPage)) {
		$.ford.pages.scc.delegate.didShowSelectionPage();
	}
}});
$.extend($.ford.pages.scc, {_init: function() {
	$('#selectionSubmitButton').bind('click', function() {
		$.ford.pages.scc.onSubmitSelectionPage();
	});
	
	$('#contactSubmitButton').bind('click', function() {
		$.ford.pages.scc.onSubmitContactPage();
	});
	
	// Set each select box to disabled the 'Select One' item once an item has been selected.
	$('#2_div select').change(function() {
		$(this).children('option:first').attr("disabled", "disabled");
	});

	// If contact information for the user has been stored, use this data to populate the contact
	// information on the contact page.
	if($.ford.vars.get('formsContact')) {
		var formsContact = $.ford.vars.get('formsContact');
		$('form#contact input:text#fname').val(formsContact.firstName);
		$('form#contact input:text#lname').val(formsContact.lastName);
		$('form#contact input:text#apt').val(formsContact.apt);
		$('form#contact input:text#address').val(formsContact.address);
		$('form#contact input:text#city').val(formsContact.city);
		$('form#contact select#province').val(formsContact.province);
		$('form#contact input:text#postal').val(formsContact.postal);
		$('form#contact input:text#phone').val(formsContact.phone);
		$('form#contact input:text#email').val(formsContact.email);
	}

	// Validate the form data of the contact page when it is changed.
	$('form#contact').live('keyup', function(e) {
		if($.ford.validation.validate.form(this)) {
			$('.all-required').removeClass('red-text');
		}
	});	
}});

