var notRequired = Array("address", "city", "zip", "country");
var error_missing = "Selle välja täitmine on kohustuslik.";
var error_mail = "See ei ole kehtiv meiliaadress.";

$(document).ready(function(){

	$("#come_ok").click(function(){
		var v =	($(this).attr("checked")) ? "1" : "0";
		if (v == 1) {
			$("#present_ok").removeAttr("checked");
			$("#present li").removeClass("required");
			for (i = 0; i < notRequired.length; i++) {
				$("#buy ." + notRequired[i]).removeClass("required");
			}	
		} else {
			for (i = 0; i < notRequired.length; i++) {
				$("#buy ." + notRequired[i]).addClass("required");
			}	
		}
	});

	$("#present_ok").click(function(){
		var v =	($(this).attr("checked")) ? "1" : "0";
		if (v == 1) {
			$("#come_ok").removeAttr("checked");
			$("#present li").addClass("required");
			for (i = 0; i < notRequired.length; i++) {
				$("#buy ." + notRequired[i]).removeClass("required");
			}	
		} else {
			$("#present li").removeClass("required");
			for (i = 0; i < notRequired.length; i++) {
				$("#buy ." + notRequired[i]).addClass("required");
			}	
		}
	});

	$("#customer_info").submit(function(e) {

		var ok = 1;
		
		$("strong.error").remove();
		$(".error").removeClass("error");
		
		$("li.required input").each(function() {
			if ($(this).val() == "") {
				$(this).addClass("error").after("<strong class='error'>" + error_missing + "</strong>");
				ok = 0;
			}
		});

		$("input[name='email']").each(function() {
			if (!checkEmail($(this).val())) {
				$(this).addClass("error").after("<strong class='error'>" + error_mail + "</strong>");
				ok = 0;
			}							
		});

		if (ok == 0) e.preventDefault();
	});


	$("#text").append("<div class='clear'></div>");
	$(".box").append("<div class='clear'></div>");
	$("#samalt dt dd ul").append("<div class='clear'></div>");
	$("#samast_sarjast dl dd ul").append("<div class='clear'></div>");
	$("#samast_sarjast dl dd ul").append("<div class='clear'></div>");
	$("#jobb").append("<div class='clear'></div>");
	$("#justIlmunud").append("<div class='clear'></div>");

	$("#comment_foot li").children("input, div").prev("label").append(":");
	$("#comment_foot li").children("textarea").prev("label").append(":");
	$("#comment_foot li .balra").prev("input").addClass("inputBalra");
	$(".kohustuslik").prepend("<em class='tarnike'>*</em>&nbsp;");
	$(".tarnike").parent("label").css({ paddingTop: "0" });
	
	$("#comment_foot legend").prepend("(").append(")");
	
	$("#ostukorv .kokku").append(":");
	$("#ostukorv input[type=submit]").addClass("ostukorvSubmit"); 
	$("input[type=submit]").addClass("submit");
	$("input[type=text]").addClass("textInput");
	$("input[type=checkbox]").addClass("checkbox");
	$(".checkbox").add(":radio").next("label").addClass("balra"); 
	$("#tellimus label").not(".balra").append(":");
	$(".autor_raamatust").prepend("(").append(")");
	
	$(".character").next(".stage").before(" ");
	
	$(".author").append(":");
	$("h3 .stitle").prev(".title").append(" ");
	
	$("#arvustused a").addClass("out");
	$(".meedia #text dd a").addClass("out");
	
	$("dt.year").next("dd").children(":first-child").css({ marginTop: "-1em" });
	
	$("#footer li:first").addClass("dekaart");

	$("#arvustused").prev("h4").append("&nbsp;(" + $("#arvustused ul li").size() + ")");

 	var nr = $("#kommentaarid #comment_body li dl").size();
	if (nr == "") { nr = 0; }
	$("#kommentaarid").prev("h4").append("&nbsp;(" + nr + ")");

	$("h4").next("div").addClass("peida");
	$("#uudised div").removeClass("peida");
	$(".peida").hide();
	$(".peida").prev("h4").prepend("<span class='nyil'></span>").wrap("<a href='' class='loe' title='loe/peida'></a>");
	$(".loe").click(function(event)
	{
		$(this).next(".peida").toggle("fast");
		$(this).toggleClass("loe");		$(this).toggleClass("smallPadding");
		$(this).parents(".block").toggleClass("bigpadding");
		return false;	});

	$(".kiri").each(function() {
		var c = ($(this)).attr("class");
		c = c.split(" ");
		var n = (c[1] != undefined) ? c[1] : "info";
		$(this).html("<a href='mailto:" + n + "@petroneprint.ee' title='" + n + "@petroneprint.ee'>" + n + "@petroneprint.ee</a>");			
	});

	$("#kontakt dd").next("dt").addClass("dt");
	
	$("#pp, #uudised").equalizeCols();
//	$("#myyk, #kontakt").equalizeCols();
	
	$("#control .textInput").each(function() {
		if ($(this).val() != "") {
			$(this).addClass("focus");
		}
	}).focus(function() {
		$(this).addClass("focus");
	}).blur(function() {
		$(this).removeClass("focus");		
	});
	
	$("#control form").submit(function() {
		if ($("#control .textInput").val() == "") {
			return false;
		}
	});

		
});

function checkEmail(email) {
	var email = email.replace(/^\s+|\s+$/g, ''); 
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) {
		return false;
	} else {
		return true;	
	}
}
