
var Enquiry = Enquiry || {};
	
/* Enquiry templates */
Enquiry.tmpl = {
	/*
		Background
	*/
	formBackground : function () {
		var html = '<div class="form-background"></div>';
		return html;
	},
	/*
		Enquiry Form
	*/
	enquiryForm : function () {
		var html = '<div class="form-container clearfix">';
			html += '<form action="#" method="post" id="enquiry-form"><input type="hidden" name="model" id="modelValue" value="" />';
			html += '<a class="close" href="#">close</a><div class="form-inner">';
				html += '<div class="enquiry-heading clearfix">';
					html += '<h2 class="heading">General Enquiry</h2> <p class="required">* Required Fields</p>';
				html += '</div>';
				
					html += '<div class="request-type">';
						html += '<h3>Request Type*</h3>';
						html += '<div class="request-type-container clearfix">';
							html += '<div class="col">';
								html += '<label for="quote" class="quote radio"><input type="radio" name="request" id="quote" value="Quote" validate="required:true" /> <span>Quote</span></label>';
							html += '</div>';
							html += '<div class="col">';
								html += '<label for="test-drive" class="testdrive radio"><input type="radio" name="request" id="test-drive" value="Test Drive" /> <span>Test Drive</span></label>';
							html += '</div>';
							html += '<div class="col">';
								html += '<label for="brochure" class="brochure radio"><input type="radio" name="request" id="brochure" value="Brochure" /> <span>Brochure</span></label>';
							html += '</div>';
						html += '</div>';
						html += '<div class="your-details">';
							html += '<h3>Your Details*</h3>';
							html += '<div class="your-details-container clearfix">';
								html += '<div class="row clearfix">';
									html += '<div class="col">';
										html += '<input type="text" name="firstname" id="firstname" value="First Name*" />';
									html += '</div>';
									html += '<div class="col">';
										html += '<input type="text" name="lastname" id="lastname" value="Last Name*" />';
									html += '</div>';
									html += '<div class="col">';
										html += '<input type="text" name="email" id="email" value="Email address*" />';
									html += '</div>';
								html += '</div>';
								html += '<div class="row clearfix">';
									html += '<div class="wide">';
										html += '<input type="text" name="address" id="address" value="Address*" />';
									html += '</div>';
								html += '</div>';
								html += '<div class="row clearfix">';
									html += '<div class="col">';
										html += '<input type="text" name="suburb" id="suburb" value="Suburb*" />';
									html += '</div>';
									html += '<div class="col">';
										html += '<input type="text" name="postcode" id="postcode" value="Postcode*" />';
									html += '</div>';
									html += '<div class="col state">';
										html += '<select name="state" id="state" validate="required:true">';
											html += '<option value="">State*</option>';
											html += '<option value="NSW">NSW</option>';
											html += '<option value="VIC">VIC</option>';
											html += '<option value="NT">NT</option>';
											html += '<option value="QLD">QLD</option>';
											html += '<option value="TAS">TAS</option>';
											html += '<option value="WA">WA</option>';
											html += '<option value="SA">SA</option>';
											html += '<option value="A.C.T.">A.C.T.</option>';
										html += '</select>';
									html += '</div>';
								html += '</div>';
								html += '<div class="row clearfix">';
									html += '<div class="col">';
										html += '<input type="text" name="homephone" value="Home Phone" />';
									html += '</div>';
									html += '<div class="col">';
										html += '<input type="text" name="mobilephone" id="mobilephone" value="Mobile Phone*" />';
									html += '</div>';
									html += '<div class="col">';
										html += '<input type="text" name="workphone" value="Work Phone" />';
									html += '</div>';
								html += '</div>';
								html += '<div class="row clearfix">';
									html += '<div class="wide">';
										html += '<textarea name="comments" rows="6" cols="30">Comments</textarea>';
									html += '</div>';
								html += '</div>';
								html += '<div class="row clearfix">';
									html += '<div class="checkbox receive radio">';
										html += '<input class="checkbox" type="checkbox" name="receive" value="Yes" /> <span>I would like to receive marketing communications &amp; other offers from FPV</span>';
									html += '</div>';
								html += '</div>';
								html += '<div class="row clearfix">';
									html += '<div class="checkbox confirm radio">';
										html += '<input class="checkbox" type="checkbox" name="confirm" value="Yes" /> <span>Confirm dealer\'s response</span>';
									html += '</div>';
								html += '</div>';
								html += '<div class="row clearfix">';
									html += '<p>Every effort has been made to depict vehicle colours and trims as realistically as possible on this website. However some variation may occur. Please see your nearest FPV dealer to view actual colour and trims.</p>';
									html += '<p>Due to recent changes to legislation vehicle pricing is not available on the FPV website. Pricing details can be obtained by contacting your local FPV dealer. <a href="/dealer">To find the FPV dealer nearest you please click here.</a></p>';
								html += '</div>';
							html += '</div>';
						html += '</div>';
					html += '</div>';
				html += '</form>';
			html += '</div>';
			html += '<button type="submit" class="button">Send Enquiry</button><div class="loader"></div>';
			html += '</form>';
			html += '</div>';
		return html;
	},
	/*
		Test Drive Form
	*/
	testDriveForm : function () {
		var html = '<div class="form-container clearfix">';
			html += '<form action="#" method="post" id="test-drive-form"><input type="hidden" name="model" id="modelValue" value="" />';
			html += '<a class="close" href="#">close</a><div class="form-inner">';
				html += '<div class="enquiry-heading clearfix">';
					html += '<h2 class="heading">General Enquiry</h2> <p class="required">* Required Fields</p>';
				html += '</div>';
				
					html += '<div class="request-type">';
						html += '<h3>Request Type</h3>';
						html += '<div class="request-type-container clearfix">';
							html += '<div class="col">';
								html += '<label for="quote" class="quote radio"><input type="radio" name="request" id="quote" value="Quote" /> <span>Quote</span></label>';
							html += '</div>';
							html += '<div class="col">';
								html += '<label for="test-drive" class="testdrive radio selected"><input type="radio" name="request" id="test-drive" value="Test Drive" class="checked" checked="checked" /> <span>Test Drive</span></label>';
							html += '</div>';
							html += '<div class="col">';
								html += '<label for="brochure" class="brochure radio"><input type="radio" name="request" id="brochure" value="Brochure" /> <span>Brochure</span></label>';
							html += '</div>';
						html += '</div>';
						html += '<div class="your-details">';
							html += '<h3>Your Details*</h3>';
							html += '<div class="your-details-container clearfix">';
								html += '<div class="row clearfix">';
									html += '<div class="col">';
										html += '<input type="text" name="firstname" id="firstname" value="First Name*" />';
									html += '</div>';
									html += '<div class="col">';
										html += '<input type="text" name="lastname" id="lastname" value="Last Name*" />';
									html += '</div>';
									html += '<div class="col">';
										html += '<input type="text" name="email" id="email" value="Email address*" />';
									html += '</div>';
								html += '</div>';
								html += '<div class="row clearfix">';
									html += '<div class="wide">';
										html += '<input type="text" name="address" id="address" value="Address*" />';
									html += '</div>';
								html += '</div>';
								html += '<div class="row clearfix">';
									html += '<div class="col">';
										html += '<input type="text" name="suburb" id="suburb" value="Suburb*" />';
									html += '</div>';
									html += '<div class="col">';
										html += '<input type="text" name="postcode" id="postcode" value="Postcode*" />';
									html += '</div>';
									html += '<div class="col state">';
										html += '<select name="state" id="state" validate="required:true">';
											html += '<option value="">State*</option>';
											html += '<option value="NSW">NSW</option>';
											html += '<option value="VIC">VIC</option>';
											html += '<option value="NT">NT</option>';
											html += '<option value="QLD">QLD</option>';
											html += '<option value="TAS">TAS</option>';
											html += '<option value="WA">WA</option>';
											html += '<option value="SA">SA</option>';
											html += '<option value="A.C.T.">A.C.T.</option>';
										html += '</select>';
									html += '</div>';
								html += '</div>';
								html += '<div class="row clearfix">';
									html += '<div class="col">';
										html += '<input type="text" name="homephone" value="Home Phone" />';
									html += '</div>';
									html += '<div class="col">';
										html += '<input type="text" name="mobilephone" id="mobilephone" value="Mobile Phone*" />';
									html += '</div>';
									html += '<div class="col">';
										html += '<input type="text" name="workphone" value="Work Phone" />';
									html += '</div>';
								html += '</div>';
								html += '<div class="row clearfix">';
									html += '<div class="wide">';
										html += '<textarea name="comments" rows="6" cols="30">Comments</textarea>';
									html += '</div>';
								html += '</div>';
								html += '<div class="row clearfix">';
									html += '<div class="checkbox receive radio">';
										html += '<input class="checkbox" type="checkbox" name="receive" value="Yes" /> <span>I would like to receive marketing communications &amp; other offers from FPV</span>';
									html += '</div>';
								html += '</div>';
								html += '<div class="row clearfix">';
									html += '<div class="checkbox confirm radio">';
										html += '<input class="checkbox" type="checkbox" name="confirm" value="Yes" /> <span>Confirm dealer\'s response</span>';
									html += '</div>';
								html += '</div>';
								html += '<div class="row clearfix">';
									html += '<p>Every effort has been made to depict vehicle colours and trims as realistically as possible on this website. However some variation may occur. Please see your nearest FPV dealer to view actual colour and trims.</p>';
									html += '<p>Due to recent changes to legislation vehicle pricing is not available on the FPV website. Pricing details can be obtained by contacting your local FPV dealer. <a href="/dealer">To find the FPV dealer nearest you please click here.</a></p>';
								html += '</div>';
							html += '</div>';
						html += '</div>';
					html += '</div>';
				
			html += '</div>';
			html += '<button type="submit" class="button">Send Enquiry</button><div class="loader"></div>';
			html += '</form>';
			html += '</div>';
		return html;
	}
};

$( '.test-drive' ).click( function ( e ) {
	e.preventDefault();
	
	$( Enquiry.tmpl.formBackground() ).appendTo( 'body' );
	$( Enquiry.tmpl.testDriveForm() ).appendTo( 'body' );
	
	$('html, body').animate({ scrollTop: 0 }, 800);

	var width = $( '.form-container' ).width(),
		winWidth = $( window ).width(),
		leftPos = (winWidth/2-width/2)-30;
	
	$( '.form-container' ).css({ 'left': leftPos });
		
	$( window ).resize( function () {
		var width = $( '.form-container' ).width(),
			winWidth = $( window ).width(),
			leftPos = (winWidth/2-width/2)-30;
			
		$( '.form-container' ).css({ 'left': leftPos });
	} );
		
	var ebRand = Math.random()+'';
	ebRand = ebRand * 1000000;
	$('body').find('.end').remove();
	$('body').prepend('<scr'+'ipt src="HTTP://bs.serving-sys.com/BurstingPipe/ActivityServer.bs?cn=as&amp;ActivityID=149844&amp;rnd=' + ebRand + '" class="start"></scr' + 'ipt>');

	$.validator.addMethod("defaultValidate", function( value, element, param ) {
		if ( value === element.defaultValue ) {
			return false;
		} else {
			return true;
		}
	}, "This field is required" );
	$.metadata.setType("attr", "validate");
	
	var form = $('#test-drive-form');
	
	var attr = $(this).attr('rel');
	if (typeof attr !== 'undefined' && attr !== false) {
		var rel = $(this).attr('rel').split('|');
		form.find('#modelValue').val('' + rel[0] + '');
	}
	
	form.validate( {
		errorPlacement: function ( error, element ) {
			if ( element.parent().hasClass( 'state' ) ) {
				element.after(error);
			} else {
				element.after(error);
			}
			error.css({'display':'block'});
		},
		rules : {
			firstname : "defaultValidate",
			lastname : "defaultValidate",
			address : "defaultValidate",
			suburb : "defaultValidate",
			postcode : "defaultValidate",
			mobilephone : "defaultValidate",
			email : {
				required : true,
				email : true
			},
			request: {
				required: true
			}
		},
		messages : {
			firstname : "Please enter your first name",
			lastname : "Please enter your last name",
			email : "Please enter a valid email address",
			address : "Please enter your address",
			suburb : "Please enter your suburb",
			postcode : "Please enter a valid postcode",
			mobilephone : "Please enter your mobile phone",
			request: "Please select a request type"
		}
	} );
	
	$( '.quote' ).click( function () {
		$( '.request-type-container' ).find( 'input' ).removeAttr( 'checked' ).removeClass( 'checked' );
		$(this).find( 'input' ).attr( 'checked', 'checked' ).addClass( 'checked' );
		$( '.request-type-container' ).find( '.selected' ).removeClass( 'selected' );
		$(this).addClass( 'selected' );
	} );
	
	$( '.testdrive' ).click( function () {
		$( '.request-type-container' ).find( 'input' ).removeAttr( 'checked' ).removeClass( 'checked' );
		$(this).find( 'input' ).attr( 'checked', 'checked' ).addClass( 'checked' );
		$( '.request-type-container' ).find( '.selected' ).removeClass( 'selected' );
		$(this).addClass( 'selected' );
	} );
	
	$( '.brochure' ).click( function () {
		$( '.request-type-container' ).find( 'input' ).removeAttr( 'checked' ).removeClass( 'checked' );
		$(this).find( 'input' ).attr( 'checked', 'checked' ).addClass( 'checked' );
		$( '.request-type-container' ).find( '.selected' ).removeClass( 'selected' );
		$(this).addClass( 'selected' );
	} );
	
	$( '.receive' ).click( function () {
		if ( $( '.receive' ).hasClass( 'selected' ) ) {
			$( '.receive' ).removeClass( 'selected' );
			$( '.receive' ).find( 'input' ).removeAttr( 'checked' ).removeClass( 'checked' );
		} else {
			$(this).addClass( 'selected' );
			$(this).find( 'input' ).attr( 'checked', 'checked' ).addClass( 'checked' );
		}
	} );

	$( '.confirm' ).click( function () {
		if ( $( '.confirm' ).hasClass( 'selected' ) ) {
			$( '.confirm' ).removeClass( 'selected' );
			$( '.confirm' ).find( 'input' ).removeAttr( 'checked' ).removeClass( 'checked' );
		} else {
			$(this).addClass( 'selected' );
			$(this).find( 'input' ).attr( 'checked', 'checked' ).addClass( 'checked' );
		}
	} );
	
	$("input[type$='text']").focus(function() {
		if( this.value === this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
	
	$("textarea").focus(function() {
		if( this.value === this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
	
	$( '.button' ).unbind( 'click' ).click( function ( e ) {
		e.preventDefault();
	
		if ( form.valid() ) {
			$.ajax( {
				type : 'POST',
				url : '/js/dataconnect.php',
				data : form.serialize(),
				success : function ( data ) {
					
					var ebRand = Math.random()+'';
					ebRand = ebRand * 1000000;
					$('body').find('.start').remove();
					$('body').prepend('<scr'+'ipt src="HTTP://bs.serving-sys.com/BurstingPipe/ActivityServer.bs?cn=as&amp;ActivityID=149845&amp;rnd=' + ebRand + '" class="end"></scr' + 'ipt>');
				
					$( '.form-container .button' ).css({ 'background':'#6790a4','padding':'0 15px'}).text( 'Thank you for your request.' ).unbind( 'click' );
				},
				error : function () {
				
				}
			} );
		}
	} );

	$( '.close' ).click( function ( e ) {
		e.preventDefault();
		$( '.form-background, .form-container' ).remove();
	} );
	
	if ( $.browser.msie ) {
 		if ( $.browser.version === '8.0' ) {
 			Cufon.replace('h2.heading, .required, .request-type h3, .button', { fontFamily: 'GothicRegular' });
			Cufon.now();
 		}
	}
} );


$( '.get-a-quote' ).click( function ( e ) {
	e.preventDefault();
	
	$('html, body').animate({ scrollTop: 0 }, 800);
	
	$( Enquiry.tmpl.formBackground() ).appendTo( 'body' );
	$( Enquiry.tmpl.enquiryForm() ).appendTo( 'body' );
	
	var width = $( '.form-container' ).width(),
		winWidth = $( window ).width(),
		leftPos = (winWidth/2-width/2)-30,
		rel = $(this).attr('rel');
		
	$( '.form-container' ).css({ 'left': leftPos });
		
	$( window ).resize( function () {
		var width = $( '.form-container' ).width(),
			winWidth = $( window ).width(),
			leftPos = (winWidth/2-width/2)-30;
			
		$( '.form-container' ).css({ 'left': leftPos });
	} );
		
	var ebSession = '[SessionID]';
	var ebRand = Math.random()+'';
	ebRand = ebRand * 1000000;
	
	$('body').find('.end').remove();
	$('body').prepend('<scr'+'ipt src="HTTP://bs.serving-sys.com/BurstingPipe/ActivityServer.bs?cn=as&amp;ActivityID=149856&amp;rnd=' + ebRand + '&amp;Session='+ebSession+'" class="start"></scr' + 'ipt>');

	$.validator.addMethod("defaultValidate", function( value, element, param ) {
		if ( value === element.defaultValue ) {
			return false;
		} else {
			return true;
		}
	}, "This field is required" );
	$.metadata.setType("attr", "validate");
	
	var form = $('#enquiry-form');
	form.find('#modelValue').val('' + rel + '');
	form.validate( {
		errorPlacement: function ( error, element ) {
			if ( element.parent().hasClass( 'state' ) ) {
				element.after(error);
			} else {
				element.after(error);
			}
			error.css({'display':'block'});
		},
		rules : {
			firstname : "defaultValidate",
			lastname : "defaultValidate",
			address : "defaultValidate",
			suburb : "defaultValidate",
			postcode : "defaultValidate",
			mobilephone : "defaultValidate",
			email : {
				required : true,
				email : true
			},
			request: {
				required: true
			}
		},
		messages : {
			firstname : "Please enter your first name",
			lastname : "Please enter your last name",
			email : "Please enter a valid email address",
			address : "Please enter your address",
			suburb : "Please enter your suburb",
			postcode : "Please enter a valid postcode",
			mobilephone : "Please enter your mobile phone",
			request: "Please select a request type"
		}
	} );
	
	$( '.quote' ).click( function () {
		$( '.request-type-container' ).find( 'input' ).removeAttr( 'checked' ).removeClass( 'checked' );
		$(this).find( 'input' ).attr( 'checked', 'checked' ).addClass( 'checked' );
		$( '.request-type-container' ).find( '.selected' ).removeClass( 'selected' );
		$(this).addClass( 'selected' );
	} );
	
	$( '.testdrive' ).click( function () {
		$( '.request-type-container' ).find( 'input' ).removeAttr( 'checked' ).removeClass( 'checked' );
		$(this).find( 'input' ).attr( 'checked', 'checked' ).addClass( 'checked' );
		$( '.request-type-container' ).find( '.selected' ).removeClass( 'selected' );
		$(this).addClass( 'selected' );
	} );
	
	$( '.brochure' ).click( function () {
		$( '.request-type-container' ).find( 'input' ).removeAttr( 'checked' ).removeClass( 'checked' );
		$(this).find( 'input' ).attr( 'checked', 'checked' ).addClass( 'checked' );
		$( '.request-type-container' ).find( '.selected' ).removeClass( 'selected' );
		$(this).addClass( 'selected' );
	} );
	
	$( '.receive' ).click( function () {
		if ( $( '.receive' ).hasClass( 'selected' ) ) {
			$( '.receive' ).removeClass( 'selected' );
			$( '.receive' ).find( 'input' ).removeAttr( 'checked' ).removeClass( 'checked' );
		} else {
			$(this).addClass( 'selected' );
			$(this).find( 'input' ).attr( 'checked', 'checked' ).addClass( 'checked' );
		}
	} );

	$( '.confirm' ).click( function () {
		if ( $( '.confirm' ).hasClass( 'selected' ) ) {
			$( '.confirm' ).removeClass( 'selected' );
			$( '.confirm' ).find( 'input' ).removeAttr( 'checked' ).removeClass( 'checked' );
		} else {
			$(this).addClass( 'selected' );
			$(this).find( 'input' ).attr( 'checked', 'checked' ).addClass( 'checked' );
		}
	} );
	
	$("input[type$='text']").focus(function() {
		if( this.value === this.defaultValue ) {
			this.value = "";
		}
		
		var request_checked = $("input[name=request]:checked").length;
		if ( request_checked === 1 ) {
			$( '.request-type-container .request-error' ).remove();
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
	
	$("textarea").focus(function() {
		if( this.value === this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
	
	$( '.button' ).unbind( 'click' ).click( function ( e ) {
		e.preventDefault();
		
		var request_checked = $("input[name=request]:checked").length;
		if ( request_checked === 0 ) {
			$( '.request-type-container .request-error' ).remove();
			$( '.request-type-container' ).append( '<label class="request-error" style="float: left; line-height: 25px;">Please select a request type</label>' );
		} else {
			$( '.request-type-container .request-error' ).remove();
		}
		
		if ( form.valid() ) {
			if ( request_checked === 1 ) {
				$.ajax( {
					type : 'POST',
					url : '/js/dataconnect.php',
					data : form.serialize(),
					success : function ( data ) {
						
						var ebRev = '[Revenue]';
						var ebOrderID = '[OrderID]';
						var ebProductID = '[ProductID]';
						var ebProductInfo = '[ProductInfo]';
						var ebRand = Math.random()+'';
						ebRand = ebRand * 1000000;

						$('body').find('.start').remove();
						$('body').prepend('<scr'+'ipt src="HTTP://bs.serving‐sys.com/BurstingPipe/ActivityServer.bs?cn=as&amp;ActivityID=149857&amp;rnd=' + ebRand + '&amp;Value='+ebRev+'&amp;OrderID='+ebOrderID+'&amp;ProductID='+ebProductID +'&amp;ProductInfo='+ebProductInfo+'" class="end"></scr' + 'ipt>');
					
						$( '.form-container .button' ).css({ 'background':'#6790a4','padding':'0 15px'}).text( 'Thank you for your request.' ).unbind( 'click' );
					},
					error : function () {
					
					}
				} );
			}
		}
	} );

	$( '.close' ).click( function ( e ) {
		e.preventDefault();
		$( '.form-background, .form-container' ).remove();
	} );
	
	if ( $.browser.msie ) {
 		if ( $.browser.version === '8.0' ) {
 			Cufon.replace('h2.heading, .required, .request-type h3, .button', { fontFamily: 'GothicRegular' });
			Cufon.now();
 		}
	}
} );
