
var totalOptions = 0;
var optionNames  = '';
var optionPrices = '';
var optionsRow   = '';
var totalCap     = 0; //{if:newModel[cap_price]}{newModel[cap_price]}{else:}0{end:};
var totalOur     = 0; //{if:newModel[price]}{newModel[ourPriceMinusOptions]}{else:}0{end:};


$(document).ready(function() {

	// Create tabs and start them working
    //$("#tabNewVehicleSpec").tabs();
	
	// Choose to animate the tabs
	$("#tabNewVehicleSpec").tabs({ fx: { opacity: 'toggle', duration: 'fast' } });
	
	$("#testdrive_preferred_date").datepicker();
	$('#testdrive_preferred_date').datepicker('option', {showAnim: 'fadeIn',dateFormat: 'dd/mm/yy'});

	// For unique styling of dialog buttons
	$('.ui-dialog-buttonpane button').each( function () {
	
		var html = $(this).html();
		$(this).addClass('btn' + html);
		$(this).html('<span class="ui-button-text">' + html + '</span');
	}); 
	
	var buttons = $('.ui-dialog-buttonpane').children('button'); 
	buttons.removeClass('ui-button-text-only').addClass('ui-button-text-icon').addClass('ui-button'); 
	 
});


/**
* @ desc This will opend the dialog and show the correct form
*/
function showForm( formType ){
	
	var dialogName = '';
	var formName   = '';
	
	// Using switch will stop JS errors from passing incorrect dialog names
	switch( formType ){
		case 'valuemyvehicle':
			dialogName   = 'dialogCapNewVehiclesValueMyVehicle';
			formName     = 'formCapNewVehiclesValueMyVehicle';
			alertBoxName = 'alertBoxValueMyVehicle';
		break;	
	}
	
	if( dialogName != '' ){
		
		// Clear the form values
		clearFormElements('#' + formName); 

		// Removes validation messages
		var validator = $('#' + formName).validate();
		validator.resetForm();

		// Clear Alert Box Text
		resetTips( alertBoxName );
	
		// Open the dialog box
		$('#' + dialogName ).dialog('open');
		
		// highlight first input
		$('#' + dialogName + ' :input:text:first').focus();
	}
}


/**
* @ desc This will save the selected areas for this franchise via Ajax
*/
function submitForm( formType, isUsingDialogBox ){

	if( formType != '' ){
	
		// Check if form is valid before proceeding
		if( $( "#formCapNewVehicles" + formType ).valid() ){

			$('body').css('cursor', 'progress'); 

			$.ajax({
			  url: '/frontend-operations/submit-form/',
			  dataType: 'json',
			  data: $( '#formCapNewVehicles' + formType ).serialize(),
			  success: function(data){
			  
					if( data != 0 ){
						
						if( isUsingDialogBox == true ){
							// Submitted ok.
							updateTips('Enquiry sent','Thank you for your enquiry. We will respond as soon as possible','highlight','','alertBox' + formType);
							clearFormElements("#formCapNewVehicles" + formType); 
							setTimeout( "autoCloseDialog('dialogCapNewVehicles" + formType + "');",4000);
						}else{
							updateTips('Thank You','You details have been submitted successfully','highlight','','alertBox' + formType);
						}
					}else{

						// Show generic message
						updateTips('Request Failed','The form submit request failed, please try again.','error','','alertBox' + formType);
					}
				},
			error: function( objRequest ){
					updateTips('Request Failed','The request to submit failed, please try again.','error','','alertBox' + formType);
				}
			});
			$('body').css('cursor', 'default'); 
		}
	}
}


