/*
	Modified:	06/06/2006
	Function:	Display the contact form	+ Colours Selector
	Author: 	Dave Sniper
	Company: 	KDWEB Limited
*/


/* Validation check */
function submitContact(frm){
	if(isNull(frm.sName.value) || (frm.sName.value == "name") ){
		alert("Please enter your name");
		frm.sName.focus();
		return false;
	}
	
	if(isNull(frm.sAddress1.value) || (frm.sAddress1.value == "address 1") ){
		alert("Please enter your address");
		frm.sAddress1.focus();
		return false;
	}
	
	if(isNull(frm.sCity.value) || (frm.sCity.value == "city") ){
		alert("Please enter your city");
		frm.sCity.focus();
		return false;
	}
	
	if(isNull(frm.sPostcode.value) || (frm.sPostcode.value == "postcode") ){
		alert("Please enter your postcode");
		frm.sPostcode.focus();
		return false;
	}
	
	if(!checkEmail(frm.sEmail.value)){
		alert("Please enter valid email");
		frm.sEmail.focus();
		return false;
	}
	
	if(isNull(frm.colours.value)){
		alert("Please select one or more colours");		
		return false;
	}
	
	return true;
}


function createColourElements(){
	$('input[name=colour]').each(
		function(iterator, element){
			$('<span />')
				.attr('rel', $(element).val())
				.text(', ' + $(element).val())
				.css({'display': ($(element).is(':checked') ? 'inline' : 'none')})
				.appendTo('#selectedColour>b>span')
			;
		}
	)	
}

/*
	Function is to loop through all span "colour" element 
	and remove the stailing comma of the first colour selection (span element)
*/
function formatColourString(){
	$('#selectedColour>b>span span.showing').each(function(iterator, element){
		if (iterator == 0)
		{
			$(this).text($(this).attr('rel'));
		}
		else
		{
			$(this).text(', ' + $(this).attr('rel'));
		}
	});
}

function getSelectedColour(){

	$('input[name=colour]').each(
		function(iterator, element){
			if ($(element).is(':checked'))
			{
				$('#selectedColour span span[rel='+$(element).val()+']').addClass('showing');
			}
			else
			{
				$('#selectedColour span span[rel='+$(element).val()+']').removeClass('showing');
			}
		}
	)	
	
	formatColourString();

	$('#selectedColour>b>span span.showing').show();
	$('#selectedColour>b>span span:not(.showing)').hide();
	
	//Set selected colours to hidden field	
	$('input#coloursInput').attr('value',$('#selectedColour>b>span span.showing').text());
	
}
	
$(document).ready(function(){
	
	// Generate a list of colour elements
	createColourElements();		
	
	// Display selected colour from the sample request page	
	$('.colorList img').click(
		function(){
			var input = $(this).parent().find('input');
			if (input.is(':checked'))
			{
				input.removeAttr('checked')
			}
			else
			{
				input.attr('checked', 'checked');
			}
			getSelectedColour();
		}
	)
	
	getSelectedColour();
	$('input[name=colour]').click(getSelectedColour);	
});
