var select_text = '';
var select_type = '';

$(document).ready(function()
{
	$('.input input').focus(function(){$(this).parent().addClass('input-focus');});
	$('.input input').blur(function(){$(this).parent().removeClass('input-focus');});
	$('.carts .r span').hover(function(){$(this).toggleClass('ho')});
	$('.carts .r span').click(function()
	{
		$('.carts .r span').removeClass('sel');
		$(this).addClass('sel');
		var l = $(this).attr('title');
		$('.MTN').hide();
		$('.MCI').hide();
		$('.'+l).fadeIn();
	});
	$("input[name='card_no']").change(function()
	{
		if( $(this).parent().parent().hasClass('MCI') )
		{
			select_type = 'همراه اول';
		} else
		{
			select_type = 'ایرانسل';
		}
		$("input[name='card_no']").removeClass('selected_no');
		$(this).addClass('selected_no');
		select_text = $(this).parent().find('span').text();
		var p = $(this).attr('lang');
		var no = $("select[name='count']").val();
		$('.tot_price').text( p * no ).hide().fadeIn();
		$('.invoice .title').text( no + ' عدد ' + select_text + ' ' + select_type ).hide().fadeIn();
	});
	$("select[name='count']").change(function()
	{
		var p = $('.selected_no').attr('lang');
		if( ! p )
		{
			p = 0;
		}
		var no = $(this).val();
		$('.tot_price').text( p * no ).hide().fadeIn();
		if( select_type != '' && select_text != '' )
		{
			$('.invoice .title').text( no + ' عدد ' + select_text + ' ' + select_type ).hide().fadeIn();
		}
	});
	$('form.buy').submit(function()
	{
		var i = 1,message = '';
		if( $.trim( $("input[name='email']").val() ) != '' )
		{
			var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if( ! filter.test( $("input[name='email']").val() ) )
			{
				message += '<b>'+i+'</b> . ایمیل وارد شده معتبر نمی باشد .<br />';
				i++;
			}
		}
		if( $.trim( $("input[name='tell']").val() ) != '' )
		{
			var filter = /^09([0-9]{9})+$/;
			if( ! filter.test( $("input[name='tell']").val() ) )
			{
				message += '<b>'+i+'</b> . شماره وارد شده معتبر نمی باشد .<br />';
				i++;
			}
		}
		if( $("input[name='card_no'].selected_no").length == 0 )
		{
			message += '<b>'+i+'</b> . لطفا کارت مورد نظر خود را انتخاب کنید .<br />';
			i++;
		}
		if( i > 1 )
		{
			$('#message_place').empty().html(message);
			var l = $('.fade').width() / 2 - 150;
			var t = $('.fade').height() / 2 - 45;
			$('.fade').fadeIn('fast',function(){$('.new_win').css({ top : t+'px' , left : l+'px'}).fadeIn('fast');});
			return false;
		}
	});
	$('a.close').click(function()
	{
		$('.new_win').fadeOut('fast',function(){$('.fade').fadeOut('fast');});
	});
	$('.fade').click(function()
	{
		$('.new_win').fadeOut('fast',function(){$('.fade').fadeOut('fast');});
	});
	$(window).resize(function()
	{
		var l = $('.fade').width() / 2 - 150;
		var t = $('.fade').height() / 2 - 45;
		$('.new_win').animate({ top : t+'px' , left : l+'px'},'slow');
	});
});
