


var country_bak = 1;
var operator_bak = 2;
var style_true = 0;
var reg_h_s_timer = null;
var film_url = null;
var film_type = null;



function checklogin(e, field)
{

	if(!e) e = window.event;

	if (e.type=="keyup" || e.type=="blur")
	{
		$("#login_error").removeClass('ok');
		
		if(field.value.match(/[^a-z0-9\-]+/i))
		{
			
			$("#login_error").html(errorstr[0]);

		} else if (field.value.length < 3) {
			
			$("#login_error").html(errorstr[1]);
			
		} else if (field.value.length > 25) {
			
			$("#login_error").html(errorstr[2]);

		} else {
			$("#login_error").html('');
			
			clearTimeout(logintimer);
			logintimer = setTimeout(function() {
				checkpost("login", field.value);
			}, 500);
		}
	
//		if (e.type=="blur")
//			alert(e.type);
		
	} else if (e.type=="keydown") {
		
		clearTimeout(logintimer);
		
	}
	
	return ;
	
}


function checkemail(e, field)
{

	if(!e) e = window.event;
	
	if (e.type=="keyup" || e.type=="blur")
	{
		
		$("#email_error").removeClass('ok');
		
		if(field.value.match(/[^a-z0-9\_\.\-\@]+/i))
		{
			
			$("#email_error").html(errorstr[0]);

		} else if (!field.value.match(/^[a-z0-9][a-z0-9_.-]*@[a-z0-9_.-]+\.[a-z]{2,7}$/i)) {
			
			$("#email_error").html(errorstr[3]);

		} else {
			$("#email_error").html('');
			
			clearTimeout(emailtimer);
			emailtimer = setTimeout(function(){
				checkpost("email", field.value);
			}, 500);
		}
		
//		alert(e.type);
		
	} else if (e.type=="keydown") {
		
		clearTimeout(emailtimer);
		
	}
	
	return ;
	
}






function checkl(field)
{
	
		$("#login_error").removeClass('ok');
		
		if(field.match(/[^a-z0-9\-]+/i))
		{
			if (!loginfocus)
				$("#login_error").html(errorstr[0]);

		} else if (field.length < 3) {
			
			if (!loginfocus)
				$("#login_error").html(errorstr[1]);
			
		} else if (field.length > 25) {
			
			$("#login_error").html(errorstr[2]);

		} else {
			$("#login_error").html('');
			
			clearTimeout(logintimer);
			logintimer = setTimeout(function() {
				checkpost("login", field);
			}, 500);
		}
	
	return ;
	
}

$(document).bind("ready", function() {
	
	
	$("input[name='name']").bind("change keyup", function(){
		checkl($("input[name='name']").attr("value"));
	});
	
	$("input[name='name']").bind("keydown", function(){
		clearTimeout(logintimer);
	});
	
	

	
	$("input[name='email']").bind("change keyup", function(){
		checke($("input[name='email']").attr("value"));		
	});
	$("input[name='email']").bind("keydown", function(){
		clearTimeout(emailtimer);	
	});
	
	
	
	$("input[name='password1']").bind("change keyup", function(){
		field=$("input[name='password1']").attr("value");
		
		if (field.length < 6) {
			
			$("#password1_error").removeClass('ok');
			$("#password1_error").html(errorstr[4]);
			
		} else {
			
			$("#password1_error").addClass('ok');
			$("#password1_error").html('V');
	
		}
	});	
	
	
	$("input[name='password2']").bind("change keyup", function(){
		field=$("input[name='password2']").attr("value");
		
		if (field.length < 6 || field!=$("input[name='password1']").attr("value")) {
			
			$("#password2_error").removeClass('ok');
			$("#password2_error").html(errorstr[5]);
			
		} else {
			
			$("#password2_error").addClass('ok');
			$("#password2_error").html('V');
	
		}
	});	
	
	
	$("#glasss input[name='sec_code']").bind("change keyup", function(){
		
		field=$("#glasss input[name='sec_code']").attr("value");
		
		glasss="#glasss #sec_code_error, #glasss #sec_code_error2, #glasss #sec_code_error3";
		
		if (field.length != 6) {
			
			$(glasss).removeClass('ok');
			$(glasss).html(errorstr[6]);
			
		} else {
			
			$(glasss).addClass('ok');
			$(glasss).html('V');
	
		}
	});	
	
	$("#reminding-pop-up input[name='sec_code']").bind("change keyup", function(){
		
		field=$("#reminding-pop-up input[name='sec_code']").attr("value");
		
		if (field.length != 6) {
			
			$("#sec_code_error3").removeClass('ok');
			$("#sec_code_error3").html(errorstr[6]);
			
		} else {
			
			$("#sec_code_error3").addClass('ok');
			$("#sec_code_error3").html('V');
	
		}
	});	

	$("#registration-pop-up input[name='sec_code']").bind("change keyup", function(){
		
		field=$("#registration-pop-up input[name='sec_code']").attr("value");
		
		if (field.length != 6) {
			
			$("#sec_code_error2").removeClass('ok');
			$("#sec_code_error2").html(errorstr[6]);
			
		} else {
			
			$("#sec_code_error2").addClass('ok');
			$("#sec_code_error2").html('V');
	
		}
	});	
	
	$("#registration-form input[name='sec_code']").bind("change keyup", function(){
		
		field=$("#registration-form input[name='sec_code']").attr("value");
		
		if (field.length != 6) {
			
			$("#sec_code_error4").removeClass('ok');
			$("#sec_code_error4").html(errorstr[6]);
			
		} else {
			
			$("#sec_code_error4").addClass('ok');
			$("#sec_code_error4").html('V');
	
		}
	});	

	
//	$('.b-silver-button-registration').bind('click', function () {
//		showregistrationform();
//		return false;
//	});


	$("form#paycheck").live("submit", function(){
		
		function onAjaxSuccess(data)
		{
			if (data[0] == "/")
			{
				
				location.href = data;

			} else {

				alert(data);

			}

		}

		$.post(
			'/engine/ajax/pay.php', $(this).serialize() ,onAjaxSuccess
		);
			
		return false;
		
	});
	
	

	$("a#showpayform1, a#showpayform2, a#showpayform3, a#showregform1").bind("click",function() {

		clearTimeout(reg_h_s_timer);
	
		reg_h_s_timer = setInterval(reg_h_s_2, 500);
		
		var sid = $(this).attr('id');
		
		if (sid == 'showregform1')
		{
			spanclick('registration');
			
		} else {
			
			spanclick('without');
			
			if (sid == 'showpayform1')
			{
				film_type = 'watch';
				$('span#paytype').html('посмотреть');
			}
			else if (sid == 'showpayform2')
			{
				film_type = 'save';
				$('span#paytype').html('скачать');
			}
			else if (sid == 'showpayform3')
			{
				film_type = 'mp4';
				$('span#paytype').html('скачать (MP4)');
			}
			
			$("form#paycheck").find('input#save').val(film_type);

		}

		$("#payform").modal();

		return false;
	});


	$("span#click").live("click",function() {

		var id = $(this).attr('class');

		spanclick(id);

		return false;
	});
	
	
	function spanclick(id)
	{
		if ($('#' + id + '-click').hasClass('active'))
		{
			return false;
		} else {
			$('div.b-popup-bookmarks div').removeClass();
			$('#' + id + '-click').addClass('active');
			$('div.b-popup-bookmarks div[class!=active]').addClass('passive');
		}

		$('#without-form').css('display', 'none');
		$('#registration-form').css('display', 'none');

		$('#' + id + '-form').css('display', 'block');
		$('#' + id + '-form').css('display', 'block');
		
	}
	
	
	
//	$("#sms-pay a").bind("click",function() {
//		
//		number=$(this).text();
//		number_country=$(this).attr("rel");
//		
//		$("#cur_tel").text(number);
//		
//		$("#sms-pay td").removeClass("current");
//		
//		
//		$("div#country"+number_country+"-"+operator_bak+" table#sms-pay td#sms"+number_country+"-"+operator_bak+"-"+number+"-1").addClass("current");
//		$("div#country"+number_country+"-"+operator_bak+" table#sms-pay td#sms"+number_country+"-"+operator_bak+"-"+number+"-2").addClass("current");
//		$("div#country"+number_country+"-"+operator_bak+" table#sms-pay td#sms"+number_country+"-"+operator_bak+"-"+number+"-3").addClass("current");
//		
//		return false;
//	});
//
//	var country_bak=0;
	

});


function sms_country(country)
{
		
		if (country_bak!=country)
		{
//			$("#country"+country_bak+"-"+operator_bak).css({"display":"none"});
			
			$("#country"+country_bak).css({"display":"none"});
								
			$("#country"+country).css({"display":"block"});
						
			country_bak=country;
			
			var sel_val=0;
			$("#country"+country+" select option:selected").each(function () {
				sel_val=$(this).val();
            });
            
//          alert(sel_val);

			sms_operator(sel_val, country);
		}

}



function sms_operator(op, country)
{
	
	
	
	prefix = $("#prefix-"+country+"-"+op).text();
	$("#cur_prefix").html(prefix);
	
	if (country == country_bak)
	{
		
		var price = $("#country"+country_bak+"-"+op+" table#sms-pay tr#sms-2 td:last").text();
		
		$("#cur_price").html(price);
		
		var number = $("#country"+country_bak+"-"+op+" table#sms-pay tr#sms-1 td:last").text();
		
		$("#cur_tel").html(number);
				
		$("#nds").html( $("#country-nds-"+country_bak).text() );
			
		operator_bak=op;
	}
	
//	alert(op + ' ' + country);
}



var reg_h_s_timer = null;


function reg_h_s()
{
	var reg_i = 0;
	
	$('#registration-pop-up ins').each( function () {
		if ($(this).text() == 'V')
		{
			reg_i++;
		} else {
			reg_i--;
		}
	});
	
	if (reg_i == 4 && $('#registration-pop-up input:checkbox').attr('checked'))
	{
		$('#registration-pop-up input:submit').removeAttr('disabled');
	} else {
		$('#registration-pop-up input:submit').attr('disabled','true');
	}	
	
//	alert(reg_i);
}


function reg_h_s_2()
{
	var reg_i = 0;
	
	$('#registration-form ins').each( function () {
		if ($(this).text() == 'V')
		{
			reg_i++;
		} else {
			reg_i--;
		}
	});
		
	if (reg_i == 5 && $('#registration-form input[type=checkbox]').attr('checked'))
	{
		$('#registration-form input[type=submit]').removeAttr('disabled');
	} else {
		$('#registration-form input[type=submit]').attr('disabled','true');
	}	
	
//	alert(reg_i);
}




function paycheck()
{
	
	my_ajax.onShow ('');

	function onAjaxSuccess(data)
	{
		if (data != "")
		{
			$("#comment-box-"+id+" textarea").html(data);
			
		} else {
			
			location.href = '/';
			
		}
		
		my_ajax.onHide();

	}
	
	$.get(
		'/engine/ajax/pay.php',{id: id},onAjaxSuccess
	);
		
	return false;
}