// Gets any table cells with the menu_item class, and adds events to use them as menu items.
// Set the href attribute to the page the item should go to - <td class="menu_item" href="index.asp">
function menu_actions() {
	$$('table#menu td.menu_item').each(function(el) {
		el.onmouseover = function() {
			Element.addClassName(el, 'hover');
		}
		
		el.onmouseout = function() {
			Element.removeClassName(el, 'hover');
		}
		
		el.onclick = function() {
			document.location.href = el.getAttribute('href');
		}
	})
}

// Updates the price lists
function update_prices(id) {
	var size = $$('select#size_' + id + ' option[value=' + String($F('size_' + id)) + ']')[0];
	var frame = $$('select#frame_' + id + ' option[value=' + String($F('frame_' + id)) + ']')[0];
	var annexe = $$('select#annexe_' + id + ' option[value=' + String($F('annexe_' + id)) + ']')[0];
	
	var awning_price = 0;
	var annexe_price = 0;
	var total_price = 0;
	
	if (size.value > 0 && frame.value > 0) {
		awning_price = parseInt(size.getAttribute('price')) + parseInt(frame.getAttribute('price'));
	} else {
		awning_price = 0;
	}
	
	annexe_price = parseInt(annexe.getAttribute('price'));
	total_price = awning_price + annexe_price;
	
	$('awning_price_' + id).innerHTML = awning_price;
	$('annexe_price_' + id).innerHTML = annexe_price;
	$('total_price_' + id).innerHTML = total_price;
}

function update_porch_prices(id) {
	var frame = $$('select#frame_' + id + ' option[value=' + String($F('frame_' + id)) + ']')[0];
	var base_price = $F('base_price_' + id);
	var awning_price = 0;
	
	if (parseInt(frame.value) > 0) {
		awning_price = parseInt(frame.getAttribute('price')) + parseInt(base_price);
	} else {
		awning_price = base_price;
	}
	
	$('total_' + id).innerHTML = awning_price;
}

function update_canopy_prices(id) {
	var size = $$('select#size_' + id + ' option[value=' + String($F('size_' + id)) + ']')[0];
	var frame = $$('select#frame_' + id + ' option[value=' + String($F('frame_' + id)) + ']')[0];
	
	var awning_price = 0;
	var total_price = 0;
	
	if (size.value > 0 && frame.value > 0) {
		awning_price = parseInt(size.getAttribute('price')) + parseInt(frame.getAttribute('price'));
	} else {
		awning_price = 0;
	}
	
	total_price = awning_price;
	
	$('awning_price_' + id).innerHTML = awning_price;
}

// Toggles a block on and off with an effect.
function toggle_block(id, show) {
	var el = $(id);
	
	if (show) {
		Effect.Appear(el);
	} else {
		Effect.Fade(el);
	}
}

// Top level validation function.
function validate(errors) {
	if (errors.length > 0) {
		var errorList = '';
		for (var i = 0; i < errors.length; i++) {
			errorList += "- " + errors[i] + "\n";
		}
		alert('One more problems were found with the information you provided:\n\n' + errorList);
		return false;
	} else {
		return true;
	}
}

function validate_full_awning_order(id) {
	var errors = new Array();
	
	if ($F('size_' + id) == 0) errors.push('Select a size');
	if ($F('frame_' + id) == 0) errors.push('Select a frame');
	if ($F('colour_' + id) == 0) errors.push('Select a colour');
	
	return validate(errors);
}

function validate_porch_order(id) {
	var errors = new Array();
	
	if ($F('frame_' + id) == 0) errors.push('Select a frame');
	if ($F('colour_' + id) == 0) errors.push('Select a colour');
	
	return validate(errors);
}

function validate_order() {
	var errors = new Array();
	
	if ($F('name').length == 0) errors.push('Name is required');
	if ($F('email').length == 0) errors.push('E-Mail address is required');
	if ($F('phone').length == 0) errors.push('Phone number is required');
	if ($F('address').length == 0) errors.push('Address is required');
	
	if ($F('cc_no').length == 0) errors.push('Card number is required');
	if ($F('cc_ccv').length < 3) errors.push('CCV must be a three digit number');
	
	return validate(errors);
}

function validate_user() {
	var errors = new Array();
	
	if ($F('username').length == 0) errors.push('Username is required');
	if ($F('password').length == 0) errors.push('Password is required');
	if ($F('password') !== $F('password_repeat')) errors.push('Password and Repeat Password must be the same');
	
	return validate(errors);
}

function validate_manufacturer() {
	var errors = new Array();
	
	if ($F('Manufacturer').length == 0) errors.push('Manufacturer is required');
	
	return validate(errors);
}

// Validates a newly created awning, not requiring any of the extra bits.
function validate_basic_awning() {
	var errors = new Array();
	
	if ($F('name').length == 0) errors.push('Model is required');
	if ($F('projection').length == 0) {
		errors.push('Projection is required');
	} else {
		if (isNaN(Number($F('projection')))) errors.push('Projection must be a number larger then 0');
	}
	if ($F('description').length == 0) errors.push('Description is required');
	if ($F('frame').length == 0) errors.push('Frame Options is reqired');
	
	// Optionals that need validation
	if ($F('doors').length > 0 && (isNaN(Number($F('doors'))) || Number($F('doors')) == 0)) errors.push('Doors must be a number larger then 0');
	if ($F('panels').length > 0 && (isNaN(Number($F('panels'))) || Number($F('panels')) == 0)) errors.push('Panels must be a number larger then 0');
	
	if ($('low_annexe_available').checked) {
		if ($F('low_annexe_price').length == 0) {
			errors.push('Low Annexe Price is required');
		} else {
			if (isNaN(Number($F('low_annexe_price'))) || Number($F('low_annexe_price')) == 0) errors.push('Low Annexe Price must be a number larger then 0');
		}
	}
	
	if ($('tall_annexe_available').checked) {
		if ($F('tall_annexe_price').length == 0) {
			errors.push('Tall Annexe Price is required');
		} else {
			if (isNaN(Number($F('tall_annexe_price'))) || Number($F('tall_annexe_price')) == 0) errors.push('Tall Annexe Price must be a number larger then 0');
		}
	}
	
	return validate(errors);
}

function validate_canopy() {
	var errors = new Array();
	
	if ($F('name').length == 0) errors.push('Name is required');
	if ($F('description').length == 0) errors.push('Description is required');
	if ($F('frame').length == 0) errors.push('Frame options are required');
	if ($F('materials').length == 0) errors.push('Materials are required');
	if ($F('panels').length > 0 && (isNaN($F('panels')) || Number($F('panels')) == 0)) errors.push('Panels must be a number larger then 0');
	
	return validate(errors);
}

function validate_porch() {
	var errors = new Array();
	
	if ($F('model').length == 0) errors.push('Name is required');
	if ($F('description').length == 0) errors.push('Description is required');
	if ($F('frame').length == 0) errors.push('Frame Options are reqired');
	if ($F('materials').length == 0) errors.push('Materials are required');
	if ($F('size').length == 0) {
		errors.push('Size is required');
	} else {
		var valid = $F('size').match(/^\d+x\d+$/);
		if (valid === null) errors.push('Size should be in the format 120x532');
	}
	if ($F('price').length == 0) errors.push('Price is required');
	
	// Optionals that need validation
	if ($F('panels').length > 0 && (isNaN(Number($F('panels'))) || Number($F('panels')) == 0)) errors.push('Panels must be a number larger then 0');
		
	return validate(errors);
}

function validate_annexe() {
	var errors = new Array();
	
	if ($F('name').length == 0) errors.push('Name is required')
	if ($F('description').length == 0) errors.push('Description is required')
	if ($F('frame').length == 0) errors.push('Frame options are required')
	if ($F('materials').length == 0) errors.push('Materials are required')
	if ($F('doors').length > 0 && (isNaN(Number($F('doors'))) || Number($F('doors')) == 0)) errors.push('Doors must be a number larger then 0');
	if ($F('panels').length > 0 && (isNaN(Number($F('panels'))) || Number($F('panels')) == 0)) errors.push('Panels must be a number larger then 0');
	if ($F('price').length == 0) {
		errors.push('Price is required');
	} else {
		if (isNaN(Number($F('price'))) || Number($F('price')) == 0) errors.push('Price must be a number larger then 0, without the £ sign');
	}
	
	return validate(errors);
}

function validate_caravan() {
	var errors = new Array();
	
	if ($F('model').length == 0) errors.push('Model is required');
	if ($F('price').length == 0) {
		errors.push('Price is required');
	} else {
		if (isNaN(Number($F('price'))) || Number($F('price')) == 0) errors.push('Price must be a number larger then 0, without the £ sign');
	}
	if ($F('interior_length').length == 0) errors.push('Interior length is required');
	if ($F('unladen_weight').length == 0) errors.push('Unladen weight is required');
	
	return validate(errors);
}

function validate_frame() {
	var errors = new Array();
	
	if ($F('frame_price').length == 0) {
		errors.push('Price is required')
	} else {
		if (isNaN(Number($F('frame_price')))) errors.push('Price is required. If this is the standard frame enter a price of 0.')
	}
	
	return validate(errors);
}

function validate_accessory() {
	var errors = new Array();
	
	if ($F('name').length == 0) errors.push('Name is required');
	if ($F('price').length == 0) {
		errors.push('Price is required');
	} else {
		if (isNaN(Number($F('price'))) || Number($F('price')) == 0) errors.push('Price must be a number larger then 0, without the £ sign');
	}
	
	return validate(errors);
}