var swatchLis,prodForm;

$(function(){
	swatchLis=$('#swatches.options ul li');
	var hidField=$('#swatches.options').children(':input')[0];
	prodForm=$('[name=productForm]')[0];
	
	swatchLis.click(function(){
		$('#swatches.options ul li.selected').removeClass('selected');
		$(this).addClass('selected');
		
		hidField.value=$(':input',this)[0].value;
		
		var intQuant=parseInt(prodForm.quantity.value);
		if(isNaN(intQuant)||intQuant<1)prodForm.quantity.value=1;
		
		updatePriceCust();
	});
	
	updatePriceCust();
});

function updatePers(fieldRef,fieldNum)
{
	$('#extraField-'+fieldNum).val(fieldRef.value);
}

function getSelectedOptionsCust()
{
	var options='';
	
	var fields=$(':input[name^=options]',prodForm);
	
	fields.each(function(){options+=','+this.value;});
	
	return options;
}

function updatePriceCust()
{
	var options=getSelectedOptionsCust();
	
	$('.price .right')[0].innerHTML='calculating price...';
	
	x_getLiveProductTotal(options, prodForm.productID.value, '', '1', prodForm.quantity.value, totalHandlerCust);
}

function totalHandlerCust(response)
{
	var parseResponse=response.split('|')[0];
	if(parseResponse)$('.price .right')[0].innerHTML=parseResponse;
}

function checkRequiredFieldsCust()
{
	var isValid=true;
	$('.isRequired',prodForm).each(
		function()
		{
			if(this.value=='')
			{
				$(this).css('background-color','#ff8888');
				isValid=false;
			}
		}
	);
	if(!isValid)alert('Please complete the fields marked in red');
	return isValid;
}

function switchImage(phID)
{
	var oldPhoto=$('.photoShow');
	var newPhoto=$('.phID'+phID);
	oldPhoto.removeClass('photoShow');
	oldPhoto.addClass('photoHide');
	newPhoto.removeClass('photoHide');
	newPhoto.addClass('photoShow');
}
