/* JavaScript Document ==============================================
	hrf.js: contains the javascript functions required by the client actions of Hotel Reservation Form.
	Developer: Ezzat A. Helal
	email: eahelal@helalsoftware.net - eahelal@gmail.com
	web site: www.helalsoftware.net
	Date: May 14, 2005
	Cairo
	===========================================================
	Function List:
		OnLeaveAction(actCode): for validation of user input.
		makeValidDate(id) Validate Date when month, day, or year changed.
		formatprice(price,is_decimal) to format the total price displayed with two decimal places.
		is_integer(numb) to check that the number of rooms is integer (does not contain decimal point)
		price_change(): recomputes the total price when the guest changes the room type or the number of nights.
========================================================================== */
function OnLeaveAction(actCode) {
	
	var room_price = document.mainForm.room_type;
	for (i=0; i<room_price.length; i++) {
		if (document.mainForm.room_type[i].checked) {
			var str_room_price = document.mainForm.room_type[i].value;
			//alert('aaaa'+str_room_price+'llllllll');
			break;
		}
	}

	var IsOK = true;

	if (actCode.substr(0,7) == "RESERVE") { // reservation
		if (document.mainForm.guest_name.value.length < 3) {
			alert("invalid guest name, it must be of length greater than 2");
        	document.mainForm.guest_name.focus();
        	document.mainForm.guest_name.select();
			IsOK = false;
		}
		if (document.mainForm.guest_email.value.length < 3) {
			alert("invalid guest email, it must be of length greater than 2");
        	document.mainForm.guest_email.focus();
        	document.mainForm.guest_email.select();
			IsOK = false;
		} else if ((p1 = document.mainForm.guest_email.value.indexOf('@')) < 0) {
			alert("invalid email format (No AT found), try again");
        	document.mainForm.guest_email.focus();
        	document.mainForm.guest_email.select();
			IsOK = false;
		} else if (p1 == 0) {
			alert("invalid email format (No id before AT), try again");
        	document.mainForm.guest_email.focus();
        	document.mainForm.guest_email.select();
			IsOK = false;
		} else if ((p2 = document.mainForm.guest_email.value.indexOf('.')) < 0) {
			alert("invalid email format (No dot found), try again");
        	document.mainForm.guest_email.focus();
        	document.mainForm.guest_email.select();
			IsOK = false;
		} else if ((p2 - p1) < 2) {
			alert("invalid email format (nothing between dot and AT, try again");
        	document.mainForm.guest_email.focus();
        	document.mainForm.guest_email.select();
			IsOK = false;
		} else if (p2+1 >= document.mainForm.guest_email.value.length) {
			alert("invalid email format (nothing after dot), try again");
        	document.mainForm.guest_email.focus();
        	document.mainForm.guest_email.select();
			IsOK = false;
		}
		if (document.mainForm.NumNights.value.length < 1) {
			alert("invalid Number of nights, It must be entered");
        	document.mainForm.NumNights.focus();
        	document.mainForm.NumNights.select();
			IsOK = false;
		} else if (isNaN(document.mainForm.NumNights.value)) {
			alert("invalid Number of nights, It must be numeric");
        	document.mainForm.NumNights.focus();
        	document.mainForm.NumNights.select();
			IsOK = false;
		} else if (parseInt(document.mainForm.NumNights.value) < 1) {
			alert("invalid Number of nights, It must be greater than zero");
        	document.mainForm.NumNights.focus();
        	document.mainForm.NumNights.select();
			IsOK = false;
		}if (parseInt(document.mainForm.NumNights.value) < 3 && str_room_price == 20) {
			alert("Minimum number of nights for standard package must at least 3 nights.");
        	document.mainForm.NumNights.focus();
        	document.mainForm.NumNights.select();
			IsOK = false;
		}
		
	}
	
	if (IsOK == true) {
		document.mainForm.actCode.value = actCode;
		document.mainForm.submit();
	}
}

function OnLeaveAction2(actCode) {

	var IsOK = true;
	
	

	if (actCode.substr(0,7) == "RESERVE") { // reservation
		if (document.mainForm.guest_name.value.length < 3) {
			alert("invalid guest name, it must be of length greater than 2");
        	document.mainForm.guest_name.focus();
        	document.mainForm.guest_name.select();
			IsOK = false;
		}
		if (document.mainForm.guest_email.value.length < 3) {
			alert("invalid guest email, it must be of length greater than 2");
        	document.mainForm.guest_email.focus();
        	document.mainForm.guest_email.select();
			IsOK = false;
		} else if ((p1 = document.mainForm.guest_email.value.indexOf('@')) < 0) {
			alert("invalid email format (No AT found), try again");
        	document.mainForm.guest_email.focus();
        	document.mainForm.guest_email.select();
			IsOK = false;
		} else if (p1 == 0) {
			alert("invalid email format (No id before AT), try again");
        	document.mainForm.guest_email.focus();
        	document.mainForm.guest_email.select();
			IsOK = false;
		} else if ((p2 = document.mainForm.guest_email.value.indexOf('.')) < 0) {
			alert("invalid email format (No dot found), try again");
        	document.mainForm.guest_email.focus();
        	document.mainForm.guest_email.select();
			IsOK = false;
		} else if ((p2 - p1) < 2) {
			alert("invalid email format (nothing between dot and AT, try again");
        	document.mainForm.guest_email.focus();
        	document.mainForm.guest_email.select();
			IsOK = false;
		} else if (p2+1 >= document.mainForm.guest_email.value.length) {
			alert("invalid email format (nothing after dot), try again");
        	document.mainForm.guest_email.focus();
        	document.mainForm.guest_email.select();
			IsOK = false;
		}
		if (document.mainForm.NumNights.value.length < 1) {
			alert("invalid Number of nights, It must be entered");
        	document.mainForm.NumNights.focus();
        	document.mainForm.NumNights.select();
			IsOK = false;
		} else if (isNaN(document.mainForm.NumNights.value)) {
			alert("invalid Number of nights, It must be numeric");
        	document.mainForm.NumNights.focus();
        	document.mainForm.NumNights.select();
			IsOK = false;
		} else if (parseInt(document.mainForm.NumNights.value) < 1) {
			alert("invalid Number of nights, It must be greater than zero");
        	document.mainForm.NumNights.focus();
        	document.mainForm.NumNights.select();
			IsOK = false;
		}if (isNaN(document.mainForm.total_price.value)) {
			alert("invalid Number of total price, It must be numeric");
        	document.mainForm.total_price.focus();
        	document.mainForm.total_price.select();
			IsOK = false;
		}
	}
	
	if (IsOK == true) {
		document.mainForm.actCode.value = actCode;
		document.mainForm.submit();
	}
}
// ===========================================================================
function makeValidDate(id) {
  year = document.mainForm.selYear.options[ document.mainForm.selYear.selectedIndex ].value;
  month = document.mainForm.selMonth.options[ document.mainForm.selMonth.selectedIndex ].value;
  day = document.mainForm.selDay.options[ document.mainForm.selDay.selectedIndex ].value;
  maxDay = 31;
  if ( month == 4 || month == 6 || month == 9 || month == 11 ) {
    maxDay = 30;
  } else if ( month == 2 ) {
    if ( year%100 != 0 && year%4 == 0 ) {
      maxDay = 29;
    } else {
      maxDay = 28;
    }
  }
	sel_day = eval("document.mainForm.selDay");
	if (id == "M") {
		sel_day.options.length = 0;
		for (i=1; i<=maxDay; i++) {
			sel_day.options[i-1] = new Option(i,i);
		}
	}
	sel_day.selectedIndex = Math.min(day, maxDay)-1;
}

/* 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);
}
// =========================================================================
function is_integer(numb) {
	if (numb.indexOf(".") == -1) return (true);
	else return (false);
}
// ========================================================================
function price_change() {
	
	var room_price = document.mainForm.room_type.value;
	var str_room_price = room_price;
	/*for (i=0; i<room_price.length; i++) {
		if (document.mainForm.room_type[i].checked) {
			var str_room_price = document.mainForm.room_type[i].value;
			//alert('aaaa'+str_room_price+'llllllll');
			break;
		}
	}*/
	
	var flt_unit_price = parseFloat(str_room_price);
	//var flt_unit_price = 30;
	var str_nights = document.mainForm.NumNights.value;
	var int_nights = parseInt(str_nights);
	if (isNaN(int_nights)) {
		alert("Number of nights must be whole numbers - the default value is set to 1");
		document.mainForm.NumNights.value = "1";
       	document.mainForm.NumNights.focus();
       	document.mainForm.NumNights.select();
	} else if (!is_integer(str_nights)) {
		alert("Number of nights must be whole numbers - the default value is set to 1");
		document.mainForm.NumNights.value = "1";
       	document.mainForm.NumNights.focus();
       	document.mainForm.NumNights.select();
	} 
					
		var total_price = flt_unit_price * document.mainForm.NumNights.value;
		var total_price_element = document.getElementById("total_price");
		total_price_element.firstChild.data = "$" + formatprice(total_price, "Y");
	

}