﻿function ajax_triple(id) {
	var film_id = 0;
	if (document.getElementById('film_id')) {
		film_id = document.getElementById('film_id').value;
	}
	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;
	
	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();
				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;
}
