$(document).ready(function(){ 
	$(document).pngFix();
	$("img.form_button").click(function(){
		checkForm(this);
	});
	$("input.text").keypress(function(e){
		if(e.which == 13){
			checkForm(this);
			return false;
		}
	});
	$("#search_form").find("div.delete_icon").click(function() {
		$("#search_form").find("input.text").attr({value:""});
		$("#search_form").find("input.text").focus();
	});
	$("#search_form").find("img.form_button").hover(function() {
		$(this).addClass("button_hover");
	}, function() {
		$(this).removeClass("button_hover");
	});
	$("#search_form").find("div.delete_icon").hover(function() {
		$(this).addClass("delete_icon_hover");
	}, function() {
		$(this).removeClass("delete_icon_hover");
	});
	$("div.pager").find("img").hover(function() {
		$(this).addClass("hover");
	}, function() {
		$(this).removeClass("hover");
	});
	$("#order_form input.send").hover(function() {
		$(this).addClass("button_hover");
	}, function() {
		$(this).removeClass("button_hover");
	});
	$("#order_form input.send").click(function() {
		checkForm(this);
		return false;
	});
});

function checkForm(form_element) {
	var formElement = form_element;
	var form = $(formElement).parents("form");
	if ($(form).attr("id") == "search_form") {
		if ($(form).find("input.text").attr("value") == "") {
			return false;
		}
		else {$(form).submit();
		}
	}
	else {
		var flag = 0;
		$(form).find("input.text").each(function() {
			if ($(this).attr("value") == "") {
				$(this).parent().addClass("attention");
				flag = 1;
			}
			else {
				if ($(this).attr("name").toLowerCase().search("mail") != -1) {
					var mail = $(this).attr("value");
					var mailRegex = /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i;
					if(!mail.match(mailRegex)){
						$(this).parent().addClass("attention");
						flag = 1;
					}
					else {$(this).parent().removeClass("attention");
					}
				}
				else {
					$(this).parent().removeClass("attention");
				}
			}
		});
		if (flag == 0) {$(form).submit();
		}
		else {return false;
		}
	}
}

//навигация с помощью стрелок
function NavigateThrough(event) {
	if (!document.getElementById) return;
	if (window.event) event = window.event;
	if (event.ctrlKey) {
		var link = null;
		var href = null;
		switch (event.keyCode ? event.keyCode : event.which ? event.which : null) {
			case 0x25: link = document.getElementById ('PrevLink'); break;
			case 0x27: link = document.getElementById ('NextLink'); break;
			case 0x24: href = '/'; break;
		}
		if (link && link.href) document.location = link.href;
		if (href) document.location = href;
	}			
}
