// JavaScript Document

function Submit_Action(act) {
	
	IsOK = true;
	if ((act == "SEARCH") || (act == "BACK_TO_RESULT")) {
		document.mainForm.action = "tag_search.php";
		document.mainForm.actCode.value = act;
	} else if ((act.substr(0,6) == "DETAIL") || (act == "BACK_TO_PACKAGE")) {
		document.mainForm.actCode.value = act;
		document.mainForm.action = "tag_product.php";
	} else if (act == "BACK_TO_SEARCH_FORM") {
		document.mainForm.actCode.value = act;
		document.mainForm.action = "tag.php";
	} else if (act == "REGISTER") {
		if (document.mainForm.user_id.value.length < 3) {
			alert("invalid user id. it must be of length greater than 2");
        	document.mainForm.user_id.focus();
        	document.mainForm.user_id.select();
			IsOK = false;
		} else if (document.mainForm.user_password.value.length < 3) {
			alert("invalid user password it must be of length greater than 2");
        	document.mainForm.user_password.focus();
        	document.mainForm.user_password.select();
			IsOK = false;
		} else if (document.mainForm.new_user_name.value.length < 3) {
			alert("invalid user password it must be of length greater than 2");
        	document.mainForm.new_user_name.focus();
        	document.mainForm.new_user_name.select();
			IsOK = false;
		} else if (document.mainForm.telephone.value.length < 3) {
			alert("invalid user telephone it must be of length greater than 2");
        	document.mainForm.telephone.focus();
        	document.mainForm.telephone.select();
			IsOK = false;
		} else if (document.mainForm.user_email.value.length < 3) {
			alert("invalid user email, it must be of length greater than 2");
			IsOK = false;
		} else if ((p1 = document.mainForm.user_email.value.indexOf('@')) < 0) {
			alert("invalid email format (No AT found), try again");
			IsOK = false;
		} else if (p1 == 0) {
			alert("invalid email format (No id before AT), try again");
			IsOK = false;
		} else if ((p2 = document.mainForm.user_email.value.indexOf('.')) < 0) {
			alert("invalid email format (No dot found), try again");
			IsOK = false;
		/******** DOT may come befor AT
		} else if ((p2 - p1) < 2) {
			alert("invalid email format (nothing between dot and AT, try again");
			IsOK = false;
		*/
		} else if (p2+1 >= document.mainForm.user_email.value.length) {
			alert("invalid email format (nothing after dot), try again");
			IsOK = false;
		} else {
			document.mainForm.actCode.value = act;
		}
	} else if (act == "LOG_IN") {
		if (document.mainForm.userid.value.length < 3) {
			alert("invalid user id. it must be of length greater than 2");
        	document.mainForm.userid.focus();
        	document.mainForm.userid.select();
			IsOK = false;
		} else if (document.mainForm.userpsw.value.length < 3) {
			alert("invalid user password it must be of length greater than 2");
        	document.mainForm.userpsw.focus();
        	document.mainForm.userpsw.select();
			IsOK = false;
		} else {
			document.mainForm.actCode.value = act;
		}
	} else if (act == "BOOK_NOW") {
		if (document.mainForm.booking_start_date.value == "NOT SELECTED") {
			alert("Start Date must be selected");
			IsOK = false;
		} else {
			document.mainForm.actCode.value = act;
		}
	} else if (act.substr(0,7) == "PAY_NOW") {
		document.mainForm.actCode.value = act;
	} else if (act.substr(0,10) == "PAY_ONLINE") {
		var reference = act.substr(11);
		open_bank_win(reference);
		IsOK = false;
	}
	if (IsOK) {
		document.mainForm.submit();
	}
}

function open_bank_win(reference) {
	alert("reference: " + reference);
	win_url = "tag_pay.php?reference="+reference;
	win_name = "online_payment";
	win_features = "toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=0,width=850,height=600";
	openWin(win_url, win_name, win_features);
}
//Function to open pop up window
function openWin(theURL,winName,features) {

  	window.open(theURL,winName,features);
}
function Set2val(V1,V2)	{	
	this.Id = V1;
	this.Description = V2;
}

function SelDrop(main_list,form_name,sub_list_name) {
<!-- Original Programmed by Ira Sterbakov - irasterb@erols.com 1/8/03 -->
<!-- May be freely used and modified - email notification would be nice -->
<!-- Added Ezzat A. Helal<eahelal@helalsoftware.net -->
<!-- main_list: Select Control; form_name: Form Name; sub_list_name: Sub Select Name -->
<!-- sP: array of start pointers/indexes; aDr: array of drop down options -->
	
	var sub_list = eval("document." + form_name + "." + sub_list_name);
	sub_list.options.length = 0;
	nP = main_list.selectedIndex;
	
	nsn = start_pointers_array[nP];
	if (nP == maxMain - 1)
		nen = maxSub;
	else
		nen = start_pointers_array[nP+1];
	
	for ( i1 = nsn; i1 < nen; i1++ ) {		
		sub_list.options[i1 - nsn] = new Option(sub_options_array[i1].Description,sub_options_array[i1].Id);
	}
}

function last_day(month_no) {
	var max_days = 31;
	if ( month_no == 4 || month_no == 6 || month_no == 9 || month_no == 11 ) {
		max_days = 30;
	} else if (month_no == 2 || month_no == 0) {
		max_days = 28;
	}
	return max_days;
}
// ===========================================================================
function makeValidDate(fld_id) {

	if ((s=fld_id.indexOf("Month", 0)) > 0) {
		var is_month = true;
		var month_id = fld_id ;
		var day_id = fld_id.substr(0,s) + "Day";
	} else if ((s=fld_id.indexOf("Day", 0)) > 0) {
		var is_month = false;
		var day_id = fld_id;
		var month_id = fld_id.substr(0,s) + "Month";
	}
	month_el = document.getElementById(month_id);
	day_el = document.getElementById(day_id);
	var selected_month = month_el.options[month_el.selectedIndex].value;
	var selected_day = day_el.options[day_el.selectedIndex].value;
	var max_days = last_day(selected_month);
	if (is_month) {
		day_el.options.length = 0;
		for (var i=1; i<=max_days; i++) {
			day_el.options[i-1] = new Option(i,i);
		}
		day_el.options[i-1] = new Option("ANY", 0);
	}
	if (selected_day == 0)
		day_el.selectedIndex = day_el.options.length - 1;
	else
		day_el.selectedIndex = Math.min(selected_day, max_days)-1;
		
}
var months_names = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
function check_date(a_day, a_month, a_year) {
	
	var selected_date = months_names[a_month - 1] + " " + a_day + ", " + a_year;
	var date_ctrl = document.getElementById("checkin_date").firstChild;
	date_ctrl.data = months_names[a_month - 1] + " " + a_day + ", " + a_year;
	document.mainForm.booking_start_date.value = a_year + "-" + a_month + "-" + a_day;
}
function rooms_changed(rooms, price_per_person, min_pax, pax) {

	if (rooms.value.length < 1) {
		alert("invalid number of rooms, it must be entered.");
       	rooms.focus();
       	rooms.select();
		IsOK = false;
	} else if (isNaN(rooms.value)) {
		alert("invalid number of rooms, it must be of numeric.");
       	rooms.focus();
       	rooms.select();
		IsOK = false;
	}
	if (IsOK) {
		var pax_num = parseInt(pax.value);
		var int_rooms = parseInt(rooms.value);
		var new_alloc = Math.ceil(pax_num/min_pax);
		if (int_rooms < new_alloc) {
			alert("number of rooms should not be less than " + new_alloc);
			int_rooms = new_alloc;
			rooms.value = int_rooms.toString();
		}
		
		var total_price = min_pax * price_per_person * int_rooms;
		total_price += total_checked_options();
		var date_ctrl = document.getElementById("total_price").firstChild;
		date_ctrl.data = "€ " + formatprice(total_price, "Y");
	}
}
function option_selected(package_option) {
	
	var i = parseInt(package_option.value);
	var option_price_ctrl = document.getElementById("total_option_price_"+i).firstChild;
	var str_price = option_price_ctrl.data
	var option_price = parseFloat(str_price);
	var total_price_ctrl = document.getElementById("total_price").firstChild;
	var str_price = total_price_ctrl.data
	str_price = str_price.substr(2);
	var total_price = parseFloat(str_price);
	if (package_option.checked) {
		total_price += option_price;
	} else {
		total_price -= option_price;
	}
	
	total_price_ctrl.data = "€ " + formatprice(total_price, "Y");
}
function total_checked_options() {
	var total_options_ctrl = document.getElementById("total_options");
	var total_options = parseInt(total_options_ctrl.value);
	var tot = 0;
	for (var i=0; i<total_options; i++) {
		var package_option = document.getElementById("selected_price_"+i);
		if (package_option.checked) {
			var option_price_ctrl = document.getElementById("total_option_price_"+i).firstChild;
			var str_price = option_price_ctrl.data
			var option_price = parseFloat(str_price);
			tot += option_price;
		}
	}
	return (tot);
}
function option_pax_changed(pax, price_per_person) {
	IsOK = true;
	if (pax.value.length < 1) {
		alert("invalid number of persons, it must be entered.");
       	pax.focus();
       	pax.select();
		IsOK = false;
	} else if (isNaN(pax.value)) {
		alert("invalid number of persons, it must be of numeric.");
       	pax.focus();
       	pax.select();
		IsOK = false;
	} else if (parseInt(pax.value) < 1) {
		alert("invalid number of persons, it must be of numeric value greater than or equal 1");
       	pax.focus();
       	pax.select();
		IsOK = false;
	}
	if (IsOK) {
		pax_num = parseInt(pax.value);
		var new_option_price = price_per_person * pax_num;
		// name = option_pax_n
		var idx = pax.name.substr(11);
		var option_price_ctrl = document.getElementById("total_option_price_"+idx).firstChild;
		var old_option_price = parseFloat(option_price_ctrl.data);
		var package_option = document.getElementById("selected_price_"+idx);
		var formated_price = formatprice(new_option_price, "Y");
		option_price_ctrl.data = formated_price;
		if (package_option.checked) {
			var total_price_ctrl = document.getElementById("total_price").firstChild;
			var str_price = total_price_ctrl.data
			str_price = str_price.substr(2);
			var total_price = parseFloat(str_price);
			total_price -= old_option_price;
			total_price += new_option_price;
			total_price_ctrl.data = "€ " + formatprice(total_price, "Y");
		}
	}

}
function pax_changed(pax, price_per_person, min_pax, rooms) {
	IsOK = true;
	if (pax.value.length < 1) {
		alert("invalid number of persons, it must be entered.");
       	pax.focus();
       	pax.select();
		IsOK = false;
	} else if (isNaN(pax.value)) {
		alert("invalid number of persons, it must be of numeric.");
       	pax.focus();
       	pax.select();
		IsOK = false;
	} else if (parseInt(pax.value) < 1) {
		alert("invalid number of persons, it must be of numeric value greater than or equal 1");
       	pax.focus();
       	pax.select();
		IsOK = false;
	}
	if (IsOK) {
		pax_num = parseInt(pax.value);
		int_rooms = parseInt(rooms.value);
		var new_alloc = Math.ceil(pax_num/min_pax);
		if (int_rooms < new_alloc) int_rooms = new_alloc;
		rooms.value = int_rooms.toString();
		var total_price = int_rooms * price_per_person * min_pax;
		total_price += total_checked_options();
		var date_ctrl = document.getElementById("total_price").firstChild;
		date_ctrl.data = "€ " + formatprice(total_price, "Y");
	}
}
/* formatprice ================================================ */
function formatprice(price,is_decimal) {

	var out_price = price.toString();
	var mainprice=  out_price.substring(0, out_price.indexOf("."));
	if (mainprice.length > 3) {
		mainprice = mainprice.substr(0, mainprice.length - 3) + "," + mainprice.substr(mainprice.length - 3, 3);
	}
	if(is_decimal=='Y') {
		if(out_price.indexOf('.')!=-1) {
			var decimalprice = out_price.substring((out_price.indexOf(".")+1),out_price.indexOf(".")+3);
			if(decimalprice.length==1) {
				decimalprice=decimalprice+"0";
			} else {
				decimalprice=decimalprice;
			}
			out_price = mainprice + "." +decimalprice;
		} else {
			out_price += "." + "00";
		}
	} else {
		out_price = mainprice;
	}
	return (out_price);
}
