function toggleDropMenuItem() {
	$(this).toggleClass('active');
}
function blurSearch() {
	if($(this).val()=='') {
		$(this).val('Etsi tuotteita'); // Empty box if default to allow user to enter their query
	}
	// change fieldset background and search button style
	$(this).parent().removeClass('active');
	var hakubtn = $(this).parent().find('input[type=submit]');
	hakubtn.removeClass('blueButton');
	hakubtn.addClass('grayButton');
}
function focusSearch() {
	if($(this).val()=='Etsi tuotteita') {
		$(this).val(''); // Empty box if default to allow user to enter their query
	}
	// change fieldset background and search button style
	$(this).parent().addClass('active');
	var hakubtn = $(this).parent().find('input[type=submit]');
	hakubtn.removeClass('grayButton');
	hakubtn.addClass('blueButton');
	
	// Attach event for blur
	$(this).blur(blurSearch);
}
function operateTabs() {
	if (!$(this).hasClass('selected')) {    
		// Remove the selected class from all LI    
		$('.boxTabs > ul > li').removeClass('selected');
		
		// After cleared all the LI, reassign the class to the selected tab
		$(this).addClass('selected');
		
		// Hide all the DIV in .boxBody
		$('.boxBody div').hide();
		
		// Look for the right DIV index based on tabs LI index
		var ind = $('.boxTabs > ul > li').index(this);

		// Show the selected boxBody div
		$('.boxBody div:eq('+ind+')').show();
	}
	return false;
}
function addProductQty() {
	var inp = $('#pqty');
	if(inp) {
		var valToSet = parseInt(inp.val())+1;
		inp.val(valToSet); 
	}
	return false;
}
function removeProductQty() {
	var inp = $('#pqty');
	if(inp) {
		var valToSet = parseInt(inp.val())-1;
		if(valToSet<1) {
			valToSet = 1;
		}
		inp.val(valToSet); 
	}
	return false;
}
function toggleDivVisibility(id) {
	var ele = $(id);
	if(ele) {
		ele.toggleClass('hidden');
	}
	return false;
}
function setDivVisibility(id, isVisible) {
	var ele = $(id);
	if(ele) {
		if(isVisible && ele.hasClass('hidden')) {
			ele.removeClass('hidden');			
		} else if(!isVisible && !ele.hasClass('hidden')) {
			ele.addClass('hidden');
		}
	}
	return false;
}
function toggleCheckbox(id) {
	var ele = $(id);
	if(ele) {
		if(ele.attr('checked'))
			ele.attr('checked', false)
		else
			ele.attr('checked', true);
	}
	return false;
}

//Validates ssl/submit_order.php form when submitted
function validateSubmitOrderForm() {

	// Validate and make sure one payment method is selected
	paymentSelected = false;
	for (var i=0; i<document.paymentform.payment_method.length; i++) {
		if (document.paymentform.payment_method[i].checked) {
			paymentSelected = true;
			break;
		}
	}
	
	if(paymentSelected)
		return true;
	else
		alert('Ole hyvä ja valitse jokin maksutapa tilaukselle!');
		
	return false;
}
function setCheckOutDeliveryWishFields() {
	if($("#pvmtypeval2").attr('checked')) {
		setDivVisibility('#additionalPvmSelectionFields', true);
	} else if($("#pvmtypeval1").attr('checked')) {
		setDivVisibility('#additionalPvmSelectionFields', false);
	}	
}
function openTermsWin(termsUrl) {
	var twin = window.open(termsUrl, 'Toimitusehdot', 'scrollbars=yes,menubar=no,height=700,width=800,resizable=yes,toolbar=no,location=no,status=no');	
	return false;
}

function focusSubscription() {
	if($(this).val()=='Sähköpostiosoitteesi') {
		$(this).val(''); // Empty box if default to allow user to enter their query
	}
	// Attach event for blur
	$(this).blur(blurSubscription);
}
function blurSubscription() {
	if($(this).val()=='') {
		$(this).val('Sähköpostiosoitteesi'); // set default if form input doesn't contain anything
	}
}

$(document).ready(function() {	
	
	// Add active class toggle to menu li items and make sure we can click anywhere
	$('#menu > li').hover(function() {$(this).addClass('active');}, function() { if(!$(this).hasClass('selected')) $(this).removeClass('active');});
	$('#menu > li').click(function() {window.location=$(this).find("a").attr("href"); return false;});
	
	// Add 'emptying' action to header search box
	$('#searchinputbox').focus(focusSearch);

	// feedback_orders.php
	if($('#feedback_orders').length) {
		$('form.rating').rating();
	}
	
	var acsUrl = window.configCat+'/ajax_search_products.php';
	
	// Add autocomplete to search field
	var ac = $('#searchinputbox').autocomplete({ 
	    serviceUrl:acsUrl,
	    minChars:2, 
	    maxHeight:600,
	    width:550,
	    deferRequestBy: 0,
	    noCache: false,
	    onSelect: function(value, data){ $('#searchinputbox').val(data); $('#topsearchform').submit(); }
	  });	
});

