function bookMark(title, url) {
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if ( window.external ) { // IE Favorite
		window.external.AddFavorite(url, title); 
	}
}

function inputClick(id, defval) {
	val = $(id).value;
	if (val == defval) {
		$(id).value = '';
	}
}

function inputBlur(id, defval) {
	val = $(id).value;
	if (val == '') {
		$(id).value = defval;
	}
}

function cleanNum(num,def) {
	if (isNaN(num)) var num = new String(num); 
	num = num.replace(/[^0-9]/g, ''); 
	if (num.length == 0) {
		return def;
	} else {
		return num;
	}
}

function cleanDec(id) {	
	var num = $('#'+id).val();
	num = num.replace(/[^0-9^.]/g, ''); 
	$('#'+id).val(num);
}

function changeCat() {
	var url = "/inc/ajax/subcat.php?cid=" + $('cid').value;
	//new Ajax(url, {method: 'get', update: $('lmdl')}).request();
	new Request.HTML({url:url,update:$('subcat')}).send();
}

function prodSort(redir) {
	if ($('#sort').val().length > 0) document.location = "/searchbounce.php?redir=" + redir + "&srt=" + $('#sort').val();
}

function prodNpp(redir) {
	if ($('#npp').val().length > 0) document.location = "/searchbounce.php?redir=" + redir + "&npp=" + $('#npp').val();
}

function newsFilter(redir) {
	document.location = "/searchbounce.php?redir=" + redir + "&year=" + $('#year').val();
}

function searchFilter(redir) {
	document.location = "/searchbounce.php?redir=" + redir + "&filter=" + $('#filter').val();
}

function prodView(view,redir) {
	document.location = "/searchbounce.php?redir=" + redir + "&view=" + view;
}

function okBox(title,subtitle) {
	$("#dialog").html(subtitle);
	$("#dialog").dialog({
		bgiframe: true,
		resizable: false,
		width: 350,
		height: 160,
		modal: true,
		title: title,
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		}
	});
	$("#dialog").dialog('open');
}

function confirmBox(title,subtitle,action,jsact) {
	$("#dialog").html(subtitle);
	$("#dialog").dialog({
		bgiframe: true,
		resizable: false,
		width: 350,
		height:160,
		modal: true,
		title: 'Confirm ' + title + ' deletion',
		buttons: {
			Cancel: function() {
				$(this).dialog('close');
			},
			'Delete': function() {
				if (jsact) {
					eval(jsact);
				} else {
					document.location = action;
				}
				$(this).dialog('close');				
			}

		}
	});
	$("#dialog").dialog('open');
}

function showhidecon(id) {
	id = id.replace("#","");
	$('#prodtabs li').removeClass('sel');
	$('#prodtabcon div.tabdiv').hide();
	$("#tab_" + id).addClass('sel');
	if ($("#prod_" + id).is(':hidden')) {
		$("#prod_" + id).show();
	} else {
		$("#prod_" + id).hide();
	}
}

function starHover(star) {
	$('#stars a').removeClass('on');
	for (i = 1; i<= star; i++) {
		$('#star'+i).addClass('on');
	}
}

function starHide() {
	rating = $('#rating').val();
	for (i = 1; i <= 4; i++) {
		if (rating < i) {
			$('#star'+i).removeClass('on');
		} else {
			$('#star'+i).addClass('on');
		}
	}
}

function setRating(star) {
	$('#rating').val(star);
	starHide();
}

function addressLookup() {
	pcode = $('#pcodelu').val();
	if (pcode.length >= 6) {
		$.ajax({
			url: "/inc/ajax/address_lookup.php",
			global: false,
			type: "POST",
			dataType: "html",
			data: "pcode=" + pcode,
			success: function(msg){
				$('#addresslu').html(msg);
			}
		});
	} else {
		alert("Please enter your Postcode");
	}
}

function selectAddress(addressid) {
	cachename = 'cache_' + addressid.substr(0,(addressid.length-3));
	//console.log($('#'+cachename).html());
	if ($('#'+cachename).html().length == "") { // If address not loaded already

		$.ajax({
			url: "/inc/ajax/address_get.php",
			global: false,
			type: "POST",
			dataType: "html",
			data: "addressid=" + addressid,
			success: function(msg){				
				setAddressFields(addressid,msg);
			}
		});
	} else { // Cached
		setAddressFields(addressid, $('#' + cachename).html());
	}
}

function setAddressFields(addressid, fields) {
	cachename = 'cache_' + addressid.substr(0,(addressid.length-3));
	$('#'+cachename).html(fields); // Add data to cache div
	//console.log(fields);
	fieldarray = fields.split("|");
	
	$('#add1').val(fieldarray[0]);
	$('#add2').val(fieldarray[1]);
	$('#towncity').val(fieldarray[2]);
	$('#county').val(fieldarray[3]);
	$('#pcode').val(fieldarray[4]);
	
	$('#country').val('GB');
}


$(document).ready(function(){													 
	$("a[rel='lightbox']").colorbox({transition:"fade"});
	$('.cycle').cycle({
		fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});
	$('.brands_carousel').cycle({
		fx: 'scrollHorz' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});
	
	// supernav
	$('#supernav>li>a').hover(function() { // show
		id = $(this).attr('id').substr(3);
		$('#panel'+id).show();
	}, function() { // hide
		id = $(this).attr('id').substr(3);
		$('#panel'+id).hide();
	});
	$('div.dropdown').hover(function() { // show
		id = $(this).attr('id').substr(5);
		$('a#grp'+id).addClass('sel');
		$(this).show();
	}, function() { // hide
		id = $(this).attr('id').substr(5);
		$('a#grp'+id).removeClass('sel');
		$(this).hide();
	});
	
	if($.browser.msie) {
		var zIndexNumber = 1000;
		$('div').each(function() {
			$(this).css('zIndex', zIndexNumber);
			zIndexNumber -= 10;
		});
		$('div.dropdown').each(function() { $(this).css('zIndex',1000) } );
	}
	
	
});
