﻿var goout = 0;

function ajax_triple(id) {
	var film_id = 0;
	if (document.getElementById('film_id')) {
		film_id = document.getElementById('film_id').value;
	}
	if (document.getElementById('ajax_coupon_code' + id)) {
		code = document.getElementById('ajax_coupon_code' + id).value;
	} else {
		document.location.reload();
		return false;
	}
	var return_url = document.getElementById('ajax_coupon_return' + id).value;
	
	document.getElementById('ajax_coupon_button' + id).disabled = true;
	
	var frId = id;
	if (document.getElementById('fragment-' + frId) == null) {
		var el = $('#ajax_coupon_code' + id);
		var cnt = 0;
		while (!el.is("body")) {
			cnt++;
			el = el.parent();
			var tmpId = el.attr('id');
			if (tmpId != null) {
				tmpId = tmpId.replace('fragment-', '');
				if ((tmpId.length > 0) && (!isNaN(tmpId))) {
					frId = tmpId;
					break;
				}
			}
			if (cnt > 20) {
				break;
			}
		}
	}
	
	var tmp_content = $('#fragment-' + frId).html();
	$('#fragment-' + frId).html('<img style="margin: 100px 0 0 100px;" class="downloadbox-loading" src="/images/loading_bar.gif">');
	
	$.get("/user/ajax_triple/" + code + "/" + film_id + "/", function(data) {
			if (data == "OK") {
				$('div.downloadbox-frame').remove();
				if(document.location.toString().replace("player/iframe", "") != document.location.toString()) {
					if (goout != 0) {
						$('#fragment-' + frId).html('<h1 style="text-align: center; margin-top: 20px;"><a href="/" style="font-size: 25px; font-weight: bold;" target="_blank">смотреть фильм!</a></h1>');
						window.open("/");
						return false;
					}
					document.location.reload();
					return false;
				} else {
					document.location = return_url;
				}
				triple(code, film_id);
				return false;
			} else if (data.match(/^NOTOK/)) {
				//document.getElementById('ajax_coupon_errors').innerHTML = data.replace("NOTOK", "");
					data = data.replace("NOTOK", "").replaceAll("<br>", "");
					$('#fragment-' + frId).html(tmp_content);
					alert(data);
				document.getElementById('ajax_coupon_button' + id).disabled = false;
			} else {
				$('#fragment-' + frId).html(data);
			}
			return false;
		});
	return false;
}
function triple(code, film_id) {
	document.location = '/user/sms/?user_code=' + code + '&film_id=' + film_id;
}
function newcaptcha() {
	$('#newcaptcha_obj').html('<p style="text-align: center; margin: 30px;"><img src="/images/loading_bar.gif" /></p>');
	$.get("/newcaptcha", function(data) {
			$('#newcaptcha_obj').html(data);
			return false;
		});
}
