// xajax loader
if(typeof xajax != 'undefined')
{
	xajax.callback.global.onRequest = function() {
		xajax.$('cartLoading').style.display = 'inline';
	};
	xajax.callback.global.beforeResponseProcessing = function() {
		xajax.$('cartLoading').style.display = 'none';
	};
}

$(document).ready(function(){
	$('#message_div').show().delay(7000).fadeOut('slow');
	$(".required").blur(checkRequiredVisual);
	$(".valinta").change(calcSum);
	$('#view_delivery').click(function(){
		if($(this).is(':checked'))
		{
			$('#delivery_address').show();
		}
		else
			$('#delivery_address').hide();
	});

	$(".hilight").hover(
		function(){
			$(this).css("background-position", "0 -" + $(this).height() + "px");
		},
		function(){
			$(this).css("background-position", "0 0");
		}
		);

	$('#kaikki_tuotteet tr').hover(function(){
		$(this).css("background", "#efefef")
	}, function(){
		$(this).css("background", "#fff")
	});

	$('.themes').next('ul').hide();
	$('.themes').toggle(
		function(){
			$(this).next("ul").slideDown('fast');
		},
		function(){
			$(this).next("ul").slideUp('fast');
		});
});

function searchField()
{
	// on pageload we get default value out of field
	var def = $('#teksti').val();
	var lang = $('#lang').val();

	// when goes out of focus
	$('#teksti').blur(function(){
		if($(this).val() == '')
			$(this).val(def);
	});

	// when gets focus
	$('#teksti').focus(function(){
		if($(this).val() == def)
			$(this).val('');
	});

	$("#teksti").autocomplete("/index.php?do=getNames&lang=" + lang, {
		width: 220,
		selectFirst: false
	});
}

function calcSum()
{
	xajax_updateSum(xajax.getFormValues('orderForm'));
}

function quickAddToCart(id, _type, elm)
{
	xajax_updateCart(id, {
		order_amount: 1,
		type: _type,
		product_id:id
	});

	return false;
}

function addedToCart()
{
	$("#message_div").remove();
	$("<div />").attr("id", "message_div").addClass("ok").text(locStr.addedToBasket[loc]).appendTo("body").show().delay(4000).fadeOut("slow");
}

function removeFromCart(id)
{
	xajax.call( "removeCart",  {
		parameters: [ id ],
		mode: "synchronous"
	} );
	location.reload(true);
	return false;
}

function checkRequiredVisual()
{
	$(".required").each(function(){
		if(this.name == 'email' && !validateEmail($(this)))
		{
			$(this).removeClass("required_ok");
			$(this).addClass("required_wrong");

		}
		else if(this.value == undefined || this.value == ''){
			$(this).removeClass("required_ok");
			$(this).addClass("required_wrong");
		}
		else
		{
			$(this).removeClass("required_wrong");
			$(this).addClass("required_ok");
		}
	});
}

function checkRequired()
{
	var valid;
	valid = false;

	$(".required").each(function(){
		if(this.type == "checkbox")
		{
			if(!this.checked)
			{
				valid = false;
				alert(locStr.valueMissing[loc] + ": " + $(this).attr('rel'));
				return false;
			}
		}
		else
		{

			if(this.value == undefined || this.value == ''){
				valid = false;
				alert(locStr.valueMissing[loc] + ": " + $(this).attr('rel'));
				return false;
			}
			if(this.name == 'email'){
				if(!validateEmail($(this)))
				{
					valid = false;
					alert(locStr.malformedEmail[loc] + ": " + $(this).attr('rel'));
					return false;
				}
			}
		}

		valid = true;
		return true;
	});

	return valid;
}

function orderDone()
{
	addedToCart();
}

function validateEmail(e)
{
	var regex_email =  /^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i;

	if(!regex_email.test(e.val()))
	{
		return false;
	}

	return true;

}

/**
 * Comment
 */
function hideCountry() {
	$('.country_choice').hide();
}

function showCountry() {
	$('.country_choice').show();
}

function resetCountry(){
	$('.country_choice').val('Suomi');
	hideCountry();
}
