function count_thumb(url) {
	$.get('count_thumb.php?url=' + encodeURIComponent(url), function(data) {});
}

function count_thumb_3(obj) {
	url = $(obj).find('img').attr('src');
	$.get('count_thumb.php?url=' + encodeURIComponent(url), function(data) {});
}

function count_thumb2(url) {
	return true;
}

String.prototype.replaceAll = function(search, replace){
  return this.split(search).join(replace);
}

function ajax_login() {
	email = document.getElementById('ajax_login_email').value;
	password = document.getElementById('ajax_login_password').value;
	document.getElementById('ajax_login_button').disabled = true;
	
	$.get("/user/ajax_login/" + email + "/" + password + "/", function(data) {
			if (data == "OK") {
				$('div.downloadbox-frame').remove();
				purchase_file(chosen_file_id);
			} else {
				document.getElementById('ajax_login_errors').innerHTML = data;
				document.getElementById('ajax_login_button').disabled = false;
			}
			return false;
		});
}

function ajax_coupon(id) {
	if (id == 13) {
		return ajax_triple(id);
	}
	var code = document.getElementById('ajax_coupon_code' + id).value;
	var return_url = document.getElementById('ajax_coupon_return' + id).value;
	
	document.getElementById('ajax_coupon_button' + id).disabled = true;
	
	$.get("/user/ajax_login/" + code + "/" + code + "/1/", function(data) {
			if (data == "OK") {
				$('div.downloadbox-frame').remove();
				//purchase_file(chosen_file_id);
				document.location = return_url;
			} else {
				//document.getElementById('ajax_coupon_errors').innerHTML = data;
				data = data.replaceAll("<br>", "");
				alert(data);
				document.getElementById('ajax_coupon_button' + id).disabled = false;
			}
			return false;
		});
	return false;
}

$(function() {
		$('a.downloadbox').click(function() {
				show_box('$("#downloadbox-forms-div").load("/user/ajax_forms_login/' + film_id + '");');
				return false;
		});
		$('a.downloadbox_no_reg').click(function() {
				film_id = document.getElementById('film_id').value;
				free = document.getElementById('film_is_free').value;
				show_box('$("#downloadbox-forms-div").load("/user/ajax_themed/" + film_id + "/" + free);');
				return false;
		});
		$('a.downloadbox_list').click(function() {
				film_id = $(this).attr('film_id');
				free = $(this).attr('film_is_free');
				show_box('$("#downloadbox-forms-div").load("/user/ajax_themed/" + film_id + "/" + free);');
				return false;
		});
		$('a.downloadbox_addmoney').click(function() {
				if (document.getElementById('film_id')) {
					film_id = document.getElementById('film_id').value;
				} else {
					film_id = 0;
				}
				if (document.getElementById('film_is_free')) {
					free = document.getElementById('film_is_free').value;
				} else {
					free = 0;
				}
				show_box('$("#downloadbox-forms-div").load("/user/ajax_themed_addmoney/" + film_id + "/" + free);');
				return false;
		});
});

function subscription(step) {
	if (step == 0) {
		document.getElementById('subscription_instructions').innerHTML = '<img src="/images/loading_bar.gif" style="margin: 30px 0px 30px 100px;" />';
		$("#subscription_instructions").load("/user/ajax_subscription/0");
	} else if (step == 1) {
		var user_provider = document.getElementById('subscription_provider').value;
		var user_phone = document.getElementById('subscription_user_phone').value;
		document.getElementById('subscription_instructions').innerHTML = '<img src="/images/loading_bar.gif" style="margin: 30px 0px 30px 100px;" />';
		$.get("/user/ajax_subscription/1" + "?user_phone=" + user_phone + "&user_provider=" + user_provider, function(data) {
			if (data == "NO") {
				simple_pmethods();
			} else {
				document.getElementById('subscription_instructions').innerHTML = data;
			}
			return false;
		});
	} else if (step == 2) {
		var user_provider = document.getElementById('subscription_provider').value;
		var user_phone = document.getElementById('subscription_user_phone').value;
		var user_code = document.getElementById('subscription_user_code').value;
		if (!document.getElementById('subscription_agree') || document.getElementById('subscription_agree').checked) {
			document.getElementById('subscription_instructions').innerHTML = '<img src="/images/loading_bar.gif" style="margin: 10px 0px 10px 30px;" />';
			$("#subscription_instructions").load("/user/ajax_subscription/2" + "?user_phone=" + user_phone + "&user_provider=" + user_provider + "&user_code=" + user_code);
		} else {
			alert("Вы должны согласиться с правилами!");
		}
	}
}

function simple_pmethods() {
	film_id = document.getElementById('subscription_film_id').value;
	free = 0;
	hideDiv();
	show_box('$("#downloadbox-forms-div").load("/user/ajax_themed/" + film_id + "/" + free + "?simple=1");');
	return false;
}
