// suckerfish menus for IE 6
sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("li");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);


function calculateCost()
{
	var total=0.00;
	var qty = parseFloat($('#quantity').val());
	var price = parseFloat($('#productmodels li.sel .price').text().replace(/^\$/g,''));
	total = qty*price;
	//optional extras
	$('#productextras .sel').each(function(i,row){
		total += ($(':text',row).val() * parseFloat($('.price',row).text().replace(/^.*\$/g,'')));
	});
	
	$('#productcostbottom').text('$'+(total).toFixed(2));
}

function validate(name,message)
{
	var f = $("input[name*='"+ name +"']");
	
	if (f.val().replace(/\s+/,'') == '')
	{
		var pDiv = $(f.parent('div').get(0));
		!pDiv.hasClass('error') && pDiv.addClass('error').append(
			$('<div>').text(message)
			.css({display:'none'}).show()
		);
		f.addClass('error');
		f.focus();
		return false;
	} else {
		// remove existing errors
		f.removeClass('error');
		$(f.parent('div').get(0)).find('div').remove();
	}
	
	return true;
}

$(document).ready(function() { 
	// make the whole td clickable in the nav
	$('#nav').css('z-index',10000);
	
	// clean up the product sorting
	$('#productsortlabel :submit').remove();
	
	// make sort submit on change
	$('#productsortlabel :select').change(function(event){
		this.form.submit();
	});
	
	var calcCost = !$('#rightprodcol .nocost').length;
	$('#quantity').blur(function(event) {
		calcCost && calculateCost();
	});
	
	$('.extraquantity').blur(function(event) {
		calcCost && calculateCost();
	});
	
	$('#productmodels :radio').click(function (event) {
		$('#productmodels li').removeClass('sel');
		$(this).parent().addClass('sel');
		
		calcCost && calculateCost();
	});
	
	$('#productextras :text').each(function(i,row){
		row.disabled=true;
	});
	
	$('#productextras :checkbox').click(function (event) {
		var p = $(this).parent();
		var t = $(':text',p).get(0);
		t.disabled = !t.disabled;
		p.toggleClass('sel');
		
		calcCost && calculateCost();
	});
	
	// copy addresses in the order form
	$('#sameaddress input').click(function() {
            
        // If checked
        if ($(this).is(":checked")) {
            
            //for each input field
            $('#billingaddress input', ':visible').each(function(i) { 
                	 $('#shippingaddress input').not(':checkbox').eq(i).val($(this).val());
                });
            $("#shippingaddress select:first").get(0).selectedIndex = $('#billingaddress select:first').get(0).selectedIndex;                    
        
        } else {
            $('#shippingaddress input', ':visible').each(function(i) { 
                $(this).val(""); 
            });
            $("#shippingaddress select:first").get(0).selectedIndex=0;
        }
    });

	// validate the quote details
	$('#quoteform').submit(function(event){
		return (
			validate('[firstName]','Please enter your first name') &&
			validate('[lastName]','Please enter your last name') &&
			validate('[email]','Please enter your email address') &&
			validate('[phoneWork]','Please enter your phone number') &&
			validate('[shippingSuburb]','Please enter your suburb')
			|| false
		);
	});
	
	// validate the order form details
	$('#orderform').submit(function(event){
		return (
			validate('[firstName]','Please enter your first name') &&
			validate('[lastName]','Please enter your last name') &&
			validate('[email]','Please enter your email address') &&
			validate('[phoneWork]','Please enter your phone number') &&
			validate('[billingAddressLine1]','Please enter your address') &&
			validate('[billingSuburb]','Please enter your suburb') &&
			validate('[billingPostcode]','Please enter your postcode') &&
			validate('[shippingAddressLine1]','Please enter your address') &&
			validate('[shippingSuburb]','Please enter your suburb') &&
			validate('[shippingPostcode]','Please enter your postcode')
			|| false
		);
	});
	
	// validate the request form details
	$('#catalogueform').submit(function(event){
		return (
			validate('[firstName]','Please enter your first name') &&
			validate('[lastName]','Please enter your last name') &&
			validate('[companyName]','Please enter your company name') &&
			validate('[companyPosition]','Please enter your company position') &&
			validate('[email]','Please enter your email address') &&
			validate('[phoneWork]','Please enter your phone number') &&
			validate('[shippingAddressLine1]','Please enter your address') &&
			validate('[shippingSuburb]','Please enter your suburb') &&
			validate('[shippingPostcode]','Please enter your postcode')
			|| false
		);
	});
	
	// hide the success message
	if ($('#addedtocart').length)
	{
		setTimeout(function() { $('#addedtocart').slideUp('slow',function(){$(this).remove();});},5000);
	}
	
	// disable right click on images
	$('img').bind("contextmenu",function(e){ return false; });
	
});



