// ORDER ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function order_book(struct_id) {
	var frm = document.getElementById("order_struct_" + struct_id);
	if (frm != null) frm.submit();
	return false;
}
var prev_order_select = "";
function order_select(struct_id) {
	var elm1 = document.getElementById("order_opened_" + struct_id);
	var elm2 = document.getElementById("order_closed_" + struct_id);
	if (elm1 != null && elm2 != null) {
		if (elm1.style.display == "none") {
			elm1.style.display = "block";
			elm2.style.display = "none";
			if (prev_order_select != struct_id) {
				elm1 = document.getElementById("order_opened_" + prev_order_select);
				elm2 = document.getElementById("order_closed_" + prev_order_select);
				if (elm1 != null && elm2 != null) {
					elm1.style.display = "none";
					elm2.style.display = "block";
				}
				prev_order_select = struct_id;
			}
		} else {
			elm1.style.display = "none";
			elm2.style.display = "block";
		}
	}
	return false;
}
function order_load(event_id, struct_id, seat_count_booked) {
	var cnt = document.getElementById("seat_layout_container_" + struct_id);
	var dat = new Date();
	if (cnt != null) {
		var myAjax = new Ajax.Updater (
			{
				success:"seat_layout_container_" + struct_id,
				failure:"seat_layout_container_" + struct_id	
			},
			"ajax.asp?ajax=seats&event_id=" + event_id + "&struct_id=" + struct_id + "&seat_count_booked=" + seat_count_booked + "&time=" + dat.getTime(), 
			{
				method: "get",
				evalScripts: true,
				asynchronous: true,
				onComplete: function () {
	
				}
			}
		)
		cnt.innerHTML = "<center><br /><br />Va rugam asteptati, se incarca locurile ...<br /><br /><br /></center>";
	}
	return false;
}

// ORDER SEATS ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function st(elm) {
	if (elm.className == "stF") {
		elm.className = "stB";
	} else if (elm.className == "stB") {
		elm.className = "stF";
	}
}
function st_seats(struct_id, elm_class) {
	var elm_count = 0;
	var elm_ids = "";
	var elm_col = document.getElementsByTagName("div");
	if (elm_col != null)
		for (var i = 0; i < elm_col.length; i++)
			if (elm_col[i].id != "" && elm_col[i].className == elm_class) {
				elm_count += 1;
				elm_ids += elm_col[i].id;
			}
	return 	{ids:elm_ids, count:elm_count};
}
function st_order(struct_id) {
	var elm_seats = st_seats(struct_id, "stB");
	var frm_elm1 = document.getElementById("seat_count_" + struct_id);
	var frm_elm2 = document.getElementById("seat_ids_" + struct_id);
	var frm = document.getElementById("order_struct_" + struct_id);
	if (frm_elm1 != null && frm_elm2 != null && frm != null) {
		frm_elm1.value = elm_seats.count;
		frm_elm2.value = elm_seats.ids.substr(1);
		frm.submit();
	}
	return false;
}
function st_reset(struct_id, st_booked_ids) {
	var elm = document.getElementById("order_opened_" + struct_id);
	if (elm != null) {
		var elm_col = elm.getElementsByTagName("div");
		for (var i = 0; i < elm_col.length; i++)
			if (elm_col[i].id != "") {
				if (st_booked_ids.indexOf(elm_col[i].id) >= 0) {
					if (elm_col[i].className == "stF") elm_col[i].className = "stB";
				} else {
					if (elm_col[i].className == "stB") elm_col[i].className = "stF";
				}
			}
	}
	return false;
}

// ORDER FIN ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function order_fin() {
	document.getElementById("fin_form").submit();
	return false;
}

function order_choose_pmt(pmt_option, dlv_option) {
	pmt_option = parseInt(pmt_option);
	dlv_option = parseInt(dlv_option);
	var pmt = document.getElementById("pmt_sel");
	for (var i = 0; i < pmt.options.length; i++) {
		pmt_val = parseInt(pmt.options[i].value);
		if (pmt_val == pmt_option)
			pmt.selectedIndex = i;
		var dlv = document.getElementById("dlv_" + pmt_val);
		if (dlv != null) 
			if (pmt_val == pmt_option) {
				for (var j = 0; j < dlv.options.length; j++)
					if (parseInt(dlv.options[j].value) == dlv_option) dlv.selectedIndex = j;
				dlv.style.display = "block";
			} else {
				dlv.style.display = "none";
				dlv.selectedIndex = 0;
			}
		var dsc = document.getElementById("pmt_desc_" + pmt_val);
		if (dsc != null) 
			if (pmt_val == pmt_option) {
				dsc.style.display = "block";
			} else {
				dsc.style.display = "none";
			}
	}
//	for (var j = 0; j < pmt.options.length; j++)
//		if (parseInt(pmt.options[j].value) == pmt_option) pmt.selectedIndex = j;
//	order_choose_dlv(dlv_option);
}

function order_choose_dlv(dlv_option) {
	dlv_option = parseInt(dlv_option);
	var dlv = document.getElementById("dlv_0");
	for (var i = 0; i < dlv.options.length; i++) {
		dlv_val = parseInt(dlv.options[i].value);
		var dsc = document.getElementById("dlv_desc_" + dlv_val);
		if (dsc != null) {
			if (dlv_val == dlv_option) {
				dsc.style.display = "block";
			} else {
				dsc.style.display = "none";
			}
		}
	}
	if (!order_check_delivery_country()) {
		// delivery was reset by this function
	}
}

function order_choose_country(elm) {
	var inpt = null;
	if (elm == null) return;
	if (elm.selectedIndex < 0) return;
	if (parseInt(elm.options[elm.selectedIndex].value) == 140) {
	
		// Romania

		inpt = document.getElementById("usr_city_select");
		if (inpt != null) inpt.disabled = false;
		if (inpt != null) inpt.style.display = "block";

		inpt = document.getElementById("usr_city_text");
		if (inpt != null) inpt.disabled = true;
		if (inpt != null) inpt.style.display = "none";

		usr_hide_fields("forename,surname,passport", false);
		usr_show_fields("name,county");
		
	} else {
	
		// other country

		inpt = document.getElementById("usr_city_select");
		if (inpt != null) inpt.disabled = true;
		if (inpt != null) inpt.style.display = "none";

		inpt = document.getElementById("usr_city_text");
		if (inpt != null) inpt.disabled = false;
		if (inpt != null) inpt.style.display = "block";

		usr_show_fields("forename,surname,passport");
		usr_hide_fields("name,county", true);

		if (!order_check_delivery_country()) {
			order_choose_pmt(0, 0); // reset both payment and delivery
		}

	}
}

function order_choose_county(elm, selected_city) {
	if (elm == null) return;
	if (elm.selectedIndex < 0) return;
	var county_id = parseInt(elm.options[elm.selectedIndex].value);
	if (county_id > 0) {
		if (selected_city === undefined) {
			elm = document.getElementById("usr_city_select");
			if (elm != null && elm.selectedIndex > -1)
				selected_city = elm.options[elm.selectedIndex].value;
		}
		var dat = new Date();
		var myAjax = new Ajax.Updater(
			{
				success: "usr_city_select_contaner",
				failure: "usr_city_select_contaner"
			},
			"ajax.asp?ajax=cities&country_id=140&county_id=" + county_id + "&selected=" + selected_city + "&time=" + dat.getTime(),
			{
				method: "get",
				evalScripts: true,
				asynchronous: true,
				onComplete: function() {
				}
			}
		)
		elm = document.getElementById("usr_city_select_contaner");
		if (elm != null) elm.innerHTML = "<em style=\"line-height:19px\"> se incarca lista ... </e>";
	} else {
		elm = document.getElementById("usr_city_select_contaner");
		if (elm != null) elm.innerHTML = "<em style=\"line-height:19px\">alegeti judetul mai intai</em>";
	}
}

function order_check_delivery_country() {
	var elm = document.getElementById("usr_country");
	if (elm != null && elm.selectedIndex > -1) {
		if (parseInt(elm.options[elm.selectedIndex].value) == 140) {
			// romania
		} else {
			var pmt = order_getpaymentmethod();
			var dlv = order_getdeliverymethod();
			// if dlv not external courier nor e-ticket
			if (dlv != 0 && dlv != 4 && dlv != 5) {
				if (globalLang == "_en") {
					alert("Tickets cannot be delivered in your country with the selected Delivery Method.\nPlease choose \"Deliver tickets by courier outside Romania\" or \"E-Ticket (virtual ticket)\" (if it is available).");
				} else {
					alert("Livrarea nu poate fi facuta in Tara aleasa.\nVa rugam selectati \"Livrare in afara Romaniei prin curier\" sau \"Livrare prin eBilet (bilet virtual)\" (daca este disponibila) sau selectati alta Tara.");
				}
				order_choose_pmt(pmt, 0);
				return false;
			}
		}
	}
	return true;
}

function order_getdeliverymethod() {
	var pmt = order_getpaymentmethod();
	if (pmt > 0) {
		var elm = document.getElementById("dlv_" + pmt);
		if (elm != null) {
			if (elm.selectedIndex > -1) {
				return parseInt(elm.options[elm.selectedIndex].value);
			}
		}		
	}
	return 0;
}

function order_getpaymentmethod() {
	var elm = document.getElementById("pmt_sel");
	if (elm != null) {
		if (elm.selectedIndex > -1) {
			return parseInt(elm.options[elm.selectedIndex].value);
		}
	}
	return 0;
}

function usr_show_fields(flds) {
	var arr = flds.split(",");
	var elm = null;
	for (var i = 0; i < arr.length; i++) {
		var fld = arr[i];
		elm = document.getElementById("usr_tr_" + fld);
		if (elm != null) elm.style.display = "";
		elm = document.getElementById("usr_" + fld);
		if (elm != null) elm.disabled = false;
	}
}

function usr_hide_fields(flds, force) {
	var arr = flds.split(",");
	var elm = null;
	for (var i = 0; i < arr.length; i++) {
		var fld = arr[i];
		if (usr_required_fields.indexOf("|" + fld + "|") == -1 || force) {
			elm = document.getElementById("usr_tr_" + fld);
			if (elm != null) elm.style.display = "none";
			elm = document.getElementById("usr_" + fld);
			if (elm != null) elm.disabled = true;
		}
	}
}

// ORDER CONFIRM ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function order_confirm() {
	document.getElementById("confirm_form").submit();
	return false;
}


