function isValidEmail(email)  
{
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
if(reg.test(email) == false) 
	{
		return false;
	}
return true;
}
function isValidStr(srt) 
{
var rep = /^([+()0-9_-])+([0-9_-])$/;
if(rep.test(srt) == false) 
	{
		return false;
	}
return true;
}
jQuery(document).ready(function(){

    jQuery('a.lightbox').lightBox();
	
	$('.catalog_menu, .cat_name').cornerz({radius: 10});
	$('#menu a:first').css('border-top', 'none');
	$('#menu a:last').css('border-bottom', 'none');
	$('.price_table tr').each(function(){
		$('th:odd, td:odd', this).not(':last').css('border-right-width', '5px');
	});
	$('.price_table3 tr').each(function(){
		$('th:nth-child(3n), td:nth-child(3n)', this).not(':last').css('border-right-width', '5px');
	});
	// bottom meu
	// $('#bottom_menu > li').hover(function(){
		// $(this).children('ul').show();
	// },function(){
		// $(this).children('ul').hide();
	// });

	var ident    = 1;
	var height   = 0;
	var num_cols = 3;
	var a = new Array();
	$(".catalog_group, .index_descr").each(function(index){
	 if($(this).hasClass("index_descr")) num_cols = 2;
	 var h = $(this).height();
		if(ident <= num_cols){
			if(height <= h) height = h; 
				a[index] = index;
					if(ident == num_cols){
						for (var key in a) $('.catalog_group, .index_descr').eq(key).height(height); 
					a = new Array();
					height = 0;
				}
			ident++;
		}
		if(ident > num_cols)ident = 1;
	});

	// popup
	$('.open').live('click',function(){
		showPopup();
	});
	$('.close, #opaco').live('click',function(){
		closePopup();
	});
	$('#reset_b').click(function(){
		$('#ajax_submit_form').find('input:text,textarea').each(function(){
			$(this).val("");
		});
	});
	$('#submit_b').click(function(){
		var error = "";
		var index = "";
		var ara = new Array();
		$('#ajax_submit_form').find('input:text,textarea').each(function(e){
			switch($(this).attr("name")){
			case 'fio':if($(this).val().length < 4) error = error + "Вы не ввели Ваше ФИО \n";break;
			case 'phone':if(isValidStr($(this).val()) == false) error = error + "Вы некоректно ввели Ваш телефон \n";break;
			case 'email':if(isValidEmail($(this).val()) == false) error = error + "Неверный формат e-mail \n";break;
			case 'captcha':if($(this).val().length < 4) error = error + "Вы не ввели Код на картинке \n";break;
			}
		});	
		//alert($('#maplongtitude').val()+" "+$('#maplatitude').val()+" "+$('#mapzoom').val());
		if(error == ""){
			$.post('/ru/msg/',{
			msg:'true',
			fio:		$('#fio').val(),
			phone:		$('#phone').val(),
			email:		$('#email').val(),
			org:		$('#org').val(),
			priduct:	$('#priduct').val(),
			quant:		$('#quant').val(),
			term:		$('#term').val(),
			user_city:	$('#user_city').val(),
			user_street:	$('user_street').val(),
			user_house:	$('#user_house').val(),
			user_korp:	$('#user_korp').val(),
			user_room:	$('#user_room').val(),			
			add_info:	$('#add_info').val(),			
			captcha:	$('#captcha').val(),
			maplongtitude: $('#maplongtitude').val(),
			maplatitude: $('#maplatitude').val(),
			mapzoom: $('#mapzoom').val()
			},
		function(data){
			if (data!=0) {
			if(data =='ff00OK'){
				$('#ajax_submit_form').before('<strong>Вашe сообщение принято к рассмотрению, в ближайшее время мы вышлем Вам ответ по E-Mail!</strong>');
							$('#ajax_submit_form').remove();
					}else alert(data);
			}  
		});
		}else alert(error);
		return false;
	});
	
	$('#content .price_table tr, #content .price_table2 tr, #content .price_table3 tr').filter(":odd").each(function(){
		$('td', this).css('background-color', '#fff');
	});
	
});

function closePopup(){
	$('#opaco').toggleClass('hidden').removeAttr('style');
	$('#open').toggleClass('hidden');
	return false;
}
function showPopup(){
	if($.browser.msie){
		$('#opaco').height($(document).height()).toggleClass('hidden');
	} else {
		$('#opaco').height($(document).height()).toggleClass('hidden').fadeTo('slow', 0.7);
	};
	$('#open').alignCenter().toggleClass('hidden');
	return false;
}
$.fn.alignCenter = function() {
	var marginLeft = Math.max(40, parseInt($(window).width()/2 - $(this).width()/2)) + 'px';
	var marginTop = Math.max(40, parseInt($(window).height()/2 - $(this).height()/2)) + 'px';
	return $(this).css({'margin-left':marginLeft, 'margin-top':marginTop});
};	

