﻿var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var completeStep;

function validateForm() {
	var errorMessage = 'The following fields are empty or contain errors:<br />';
	var errors = false;
	
	if (document.getElementById('contact_name').value.length == 0) {
		errorMessage += '<b style="color:#FF0000;">Name</b><br />';
		errors = true;
	}
	
	if (document.getElementById('contact_phone').value.length == 0) {
		errorMessage += '<b style="color:#FF0000;">Phone</b><br />';
		errors = true;
	}
	
	if (reg.test(document.getElementById('contact_email').value) == false) {
		errorMessage += '<b style="color:#FF0000;">Email</b><br />';
		errors = true;
	}
	
	if (errors) {
		document.getElementById('errorMessage').innerHTML = errorMessage;
		return false;
	} else {
		return true
	}
}

function validateComplete(o) {
	if ($('#leech_length_2_'+completeStep).val() > 0)
		switchStep(o);
	else {
		alert('Please fill in a valid Leech Length.');
	}
}

function calculateLineLength() {
	if (completeStep == 'complete2' || (completeStep == 'complete3' && $('#spinnakerSock_complete3').val() == 1))
		var modifier = 2;
	else
		var modifier = 1;
	
	if (completeStep == 'complete5')
		var addedLineLength = 40;
	else
		var addedLineLength = 30;
	
	if ($('#leech_length_2_'+completeStep).val() > 0)
		$('#line_length_2_'+completeStep).val(modifier * $('#leech_length_2_'+completeStep).val() + addedLineLength);
	else
		$('#line_length_2_'+completeStep).val('');
}

function clothWeightChange(o) {
	if (o.value == 1)
		$('#head_strop_block_1_complete3').val('Dyneema Loop & Block - for socks with rope pocket');
	else
		$('#head_strop_block_1_complete3').val('Dyneema Loop - for socks with hoops & used with a separate halyard');
	
	calculateLineLength();
}

function switchStep(o) {
	$('#'+o.id).parents().filter('fieldset').hide();
	$('#'+o.name).show();
}

function changeTarget(id, name) {
	$('#'+id).name = name;
}

function increase(id) {
	var value = parseInt($('#'+id+'_out').html());
	$('#'+id+'_out').html(value+1);
	$('#'+id+'_hidden').val(value+1);
}

function decrease(id) {
	var value = $('#'+id+'_out').html();
	if (value > 0) {
		$('#'+id+'_out').html(value-1);
		$('#'+id+'_hidden').val(value-1);
	}
}

function snuffairSize(value, extra) {
	var snuffSize = 0;
	
	switch (true) {
		case (value == ''):
			break;
		case (value < 600):
			snuffSize = 2;
			break;
		case (value < 1100):
			snuffSize = 3;
			break;
		case (value < 2400):
			snuffSize = 4;
			break;
		case (value > 2400):
			snuffSize = 5;
			break;
	}
	
	$("#snuffair-size-1"+extra+" option[value='"+snuffSize+"']").attr('selected', 'selected');
	
	//if (snuffSize == 0)
	//	$("#snuffair-size-1"+extra).removeAttr('disabled');
	//else
	//	$("#snuffair-size-1"+extra).attr('disabled', 'disabled');
	
	if (extra == '_retrofit')
		changeCircumference();
}

function changeCircumference() {
	var value = '';
	switch ($('#snuffair-size-1_retrofit').val()) {
		case "2":
			value = '1880m';
			break;
		case "3":
			value = '2340m';
			break;
		case "4":
			value = '2750m';
			break;
		case "5":
			value = '3240m';
			break;
			
	}
	$('#snuffairCircumference').val(value);
}

jQuery(document).ready(function() {
	jQuery("#step1").hide();
	jQuery("#step2").hide();
	jQuery("#step3").hide();
	jQuery("#complete1").hide();
	jQuery("#complete2").hide();
	jQuery("#complete3").hide();
	jQuery("#complete5").hide();
	jQuery("#parts1").hide();
	jQuery("#retrofit1").hide();
	$('#hideEverything').show();
	
	jQuery("#print").click(function() {
		window.print();
	});
	
	$("#next0").click(function() {
		if ($("input[name='enquiryType']:checked").val() == 'order') {
			document.getElementById('orderButton').value = 'Submit Order';
		} else if ($("input[name='enquiryType']:checked").val() == 'quote') {
			document.getElementById('orderButton').value = 'Get a Quote';
		}
		
		switchStep(this);
	});
	
	$("#next1").click(function() {
		var option = $("input[name='orderType']:checked").val();
		this.name = option + '1';
		$('#back3').attr('name', option + '1');
		switchStep(this);
	});

	
	jQuery("#nextC1").click(function() {
		var val = $('#snuffair-size-1').val();
		switch (val) {
			case "2":
				this.name = 'complete2';
				break;
			case "3":
			case "4":
				this.name = 'complete3';
				break;
			case "5":
				this.name = 'complete5';
				break;
		}
		completeStep = this.name;
		$('#back3').attr('name', this.name);
		
		if (val == "0")
			alert('Please choose a valid spinnaker size.');
		else
			switchStep(this);
	});	
});