/* scripted by: metascripter */
var galarate = 65;
var bustriprate = 25;
var hotelrate1 = 115;
var hotelrate2 = 55;
var buspassrate = 22;

function getTotal()
{
	// set tickcong default value to "0" if left blank
	// applicable discounted price "congrate" got off PHP date_test via $amt in hidden textbox id "amt"
	// congress total
	var congrateaa = document.form1.amt.value;
	var congratea = parseFloat(congrateaa);
	var congratebb = congratea/2;
	var congrateb = parseFloat(congratebb);

	if ( !isNaN(parseInt(document.form1.tickcong.value)) && (document.form1.tickcong.value!=0) && (document.form1.tickcong.value != null) )
	{
		if (parseInt(document.form1.tickcong.value) > 1)
		{
			document.form1.boxacongress.value = (congratea + ((parseInt(document.form1.tickcong.value) - 1) * congrateb)).toFixed(2);
		} else {
			document.form1.boxacongress.value = congratea.toFixed(2);
		}
	} else {
		document.form1.tickcong.value = 0;
		document.form1.boxacongress.value = 0;
	}

	// set tickgala default value to "0" if left blank
	// gala total
	if (!isNaN(parseInt(document.form1.tickgala.value)) && (document.form1.tickgala.value!="") && (document.form1.tickgala.value!=0))
	{
		document.form1.boxbgala.value = parseInt(document.form1.tickgala.value) * galarate;
	} else {
		document.form1.tickgala.value = 0;
		document.form1.boxbgala.value = 0;
	}

	// set tickbustrip default value to "0" if left blank
	// bus trip total
	if (!isNaN(parseInt(document.form1.tickbustrip.value)) && (document.form1.tickbustrip.value!="") && (document.form1.tickbustrip.value!=0))
	{
		document.form1.boxctrip.value = parseInt(document.form1.tickbustrip.value) * bustriprate;
	} else {
		document.form1.tickbustrip.value = 0;
		document.form1.boxctrip.value = 0;
	}

	// set tickhotel default value to "0" if left blank
	// hotel total
	if ( !isNaN(parseInt(document.form1.tickhotel.value)) && (document.form1.tickhotel.value != 0) && (document.form1.tickhotel.value != null) && (document.form1.room.value != ""))
	{
		// tickhotel is the separate textbox listing number of persons to book hotel rooms for
		var tickhotel = document.form1.tickhotel.value;

		if ( (document.form1.hotelname.value!="") && (document.form1.hotelname.value!=0) )
		{
			if ( ((document.form1.from.value!="") && (document.form1.from.value!=0)) && ((document.form1.to.value!="") && (document.form1.to.value!=0)) )
			{
				// extract date_parts directly from form data - conversion already done @ line 457-477
				var strDay1 = document.form1.from.value.substr(0, 2);
				var strMonth1 = document.form1.from.value.substr(2, 2);
				var strYear1 = document.form1.from.value.substr(4);

				var strDay2 = document.form1.to.value.substr(0, 2);
				var strMonth2 = document.form1.to.value.substr(2, 2);
				var strYear2 = document.form1.to.value.substr(4);

				var date_from = new Date(strYear1, strMonth1, strDay1);
				var date_to = new Date(strYear2, strMonth2, strDay2);
				var days_stay = days_diff(date_from, date_to);

				// alert("strDay1: " + strDay1 + " | strMonth1: " + strMonth1 + " | strYear1: " + strYear1 + " || strDay2: " + strDay2 + " | strMonth2: " + strMonth2 + " | strYear2: " + strYear2 + " || date_from: " + date_from + " | date_to: " + date_to + " | days_stay: " + days_stay);

				if (days_stay > 0)
				{
					if (Date.parse(document.form1.from.value) > Date.parse(document.form1.to.value))
					{
						alert("[Departure] date must occur after [Arrival] date!");
						document.form1.spell.value = 0;
						document.form1.boxdhotel.value = 0;
					} else {
						document.form1.spell.value = days_stay;

						if (document.form1.hotelname.value == "SOL MELIA AVENIDA AMERICA")
						{
							document.form1.boxdhotel.value = hotelrate1 * tickhotel * parseInt(document.form1.spell.value);
						}
						if (document.form1.hotelname.value == "MADRID TORRELAGUNA TRAVELODGE")
						{
							document.form1.boxdhotel.value = hotelrate2 * tickhotel * parseInt(document.form1.spell.value);
						}
					}
				} else {
					alert("Please select 2 dates such that you are at least staying for 1 day!");
					document.form1.spell.value = 0;
					document.form1.tickhotel.value = 0;
					document.form1.boxdhotel.value = 0;
				}
			} else {
				document.form1.spell.value = 0;
				document.form1.tickhotel.value = 0;
				document.form1.boxdhotel.value = 0;
			}
		} else {
			document.form1.spell.value = 0;
			document.form1.tickhotel.value = 0;
			document.form1.boxdhotel.value = 0;
		}
	} else {
		document.form1.spell.value = 0;
		document.form1.tickhotel.value = 0;
		document.form1.boxdhotel.value = 0;
	}

	// hotel buspass total
	var hname = document.form1.hotelname.selectedIndex;
	var hnametxt = document.form1.hotelname.options[hname].text;

	if (document.form1.buspass.checked == true)
	{
		if ( (document.form1.hotelname.selectedIndex != "") && (hnametxt == "MADRID TORRELAGUNA TRAVELODGE") )
		{
			if ( ((document.form1.tickhotel.value != 0) && (document.form1.tickhotel.value != null)) && (!isNaN(tickhotel * buspassrate)) )
			{
				document.form1.boxebuspass.value = tickhotel * buspassrate;
				// alert (document.form1.hotelname.selectedIndex);
			} else {
				document.form1.buspass.checked = false;
				document.form1.boxebuspass.value = 0;
				alert ("Please make sure to enter number of rooms under hotel!");
			}
		} else {
			document.form1.buspass.checked = false;
			document.form1.boxebuspass.value = 0;
			alert("Please select the MADRID TORRELAGUNA TRAVELODGE hotel first. \n\nNext choose the number of persons to book hotel room(s) \nfor and lastly mention your arrival and departure dates. \n\nOnly then you can book this bus service.\n");
		}
	} else {
		document.form1.buspass.checked = false;
		document.form1.boxebuspass.value = 0;
	}

	// adops total
	if (document.form1.ad1.checked == true) var a1 = 250; else var a1 = 0;
	if (document.form1.ad2.checked == true) var a2 = 125; else var a2 = 0;

	if (document.form1.ad3.checked == true) var a3 = 250; else var a3 = 0;
	if (document.form1.ad4.checked == true) var a4 = 250; else var a4 = 0;

	if (document.form1.ad5.checked == true) var a5 = 62.50; else var a5 = 0;
	if (document.form1.ad6.checked == true) var a6 = 31.25; else var a6 = 0;

	document.form1.boxfads.value = a1 + a2 + a3 + a4 + a5 + a6;

/*
	if ( ((document.form1.ad1.checked == true) && (document.form1.ad2.checked == true) && (document.form1.ad3.checked == true)) || ((document.form1.ad1.checked == true) && (document.form1.ad2.checked == true) && (document.form1.ad3.checked == false)) || ((document.form1.ad1.checked == true) && (document.form1.ad2.checked == false) && (document.form1.ad3.checked == false)) || ((document.form1.ad1.checked == false) && (document.form1.ad2.checked == true) && (document.form1.ad3.checked == true)) || ((document.form1.ad1.checked == false) && (document.form1.ad2.checked == false) && (document.form1.ad3.checked == true)) || ((document.form1.ad1.checked == false) && (document.form1.ad2.checked == true) && (document.form1.ad3.checked == false)) || ((document.form1.ad1.checked == true) && (document.form1.ad2.checked == false) && (document.form1.ad3.checked == true)) )
	{
		if ((document.form1.ad1.checked == true) && (document.form1.ad2.checked == true) && (document.form1.ad3.checked == true))
		{
			document.form1.boxfads.value = 750;
		}
		if ((document.form1.ad1.checked == true) && (document.form1.ad2.checked == true) && (document.form1.ad3.checked == false))
		{
			document.form1.boxfads.value = 500;
		}
		if ((document.form1.ad1.checked == true) && (document.form1.ad2.checked == false) && (document.form1.ad3.checked == false))
		{
			document.form1.boxfads.value = 250;
		}
		if ((document.form1.ad1.checked == false) && (document.form1.ad2.checked == true) && (document.form1.ad3.checked == true))
		{
			document.form1.boxfads.value = 500;
		}
		if ((document.form1.ad1.checked == false) && (document.form1.ad2.checked == false) && (document.form1.ad3.checked == true))
		{
			document.form1.boxfads.value = 250;
		}
		if(((document.form1.ad1.checked == false) && (document.form1.ad2.checked == true) && (document.form1.ad3.checked == false)))
		{
			document.form1.boxfads.value = 250;
		}
		if(((document.form1.ad1.checked == true) && (document.form1.ad2.checked == false) && (document.form1.ad3.checked == true)))
		{
			document.form1.boxfads.value = 500;
		}
	} else {
		document.form1.boxfads.value = 0;
	}
*/

	if (!isNaN(parseFloat(document.form1.boxacongress.value)))
		var aa = parseFloat(document.form1.boxacongress.value);
	else
		var aa = 0;

	if (!isNaN(parseFloat(document.form1.boxbgala.value)))
		var bb = parseFloat(document.form1.boxbgala.value);
	else
		var bb = 0;

	if (!isNaN(parseFloat(document.form1.boxctrip.value)))
		var cc = parseFloat(document.form1.boxctrip.value);
	else
		var cc = 0;

	if (!isNaN(parseFloat(document.form1.boxdhotel.value)))
		var dd = parseFloat(document.form1.boxdhotel.value);
	else
		var dd = 0;

	if (!isNaN(parseFloat(document.form1.boxebuspass.value)))
		var ee = parseFloat(document.form1.boxebuspass.value);
	else
		var ee = 0;

	if (!isNaN(parseFloat(document.form1.boxfads.value)))
		var ff = parseFloat(document.form1.boxfads.value);
	else
		var ff = 0;

	if (!isNaN(parseFloat(document.form1.boxgspain.value)))
	{
		var gg = parseFloat(document.form1.boxgspain.value);
	} else {
		document.form1.boxgspain.value = 0;
		var gg = 0;
	}

	if (!isNaN(parseFloat(document.form1.boxhperu.value)))
	{
		var hh = parseFloat(document.form1.boxhperu.value);
	} else {
		document.form1.boxhperu.value = 0;
		var hh = 0;
	}

	if (!isNaN(parseFloat(document.form1.boxiextras.value)))
	{
		var ii = parseFloat(document.form1.boxiextras.value);
	} else {
		document.form1.boxiextras.value = 0;
		var ii = 0;
	}

	var alltot = aa + bb + cc + dd + ee + ff + gg + hh + ii;
	document.form1.amount.value = alltot.toFixed(2);

}

function days_diff(date1, date2) {
    // number of milliseconds in a day
    var oneday = 1000 * 60 * 60 * 24

    // milliseconds
    var date1_ms = date1.getTime()
    var date2_ms = date2.getTime()

    var diff_ms = Math.abs(date1_ms - date2_ms)
    
    // convert back to days and return
    return Math.round(diff_ms/oneday)
}

function validate()
{
	// getGrandTotal();

	if ((document.form1.amount.value == "") || (document.form1.amount.value == 0))
	{
		// alert("You have not selected any booking yet!");
		return false;
	}
}

function checkdate(objName)
{
	var datefield = objName;
	if (chkdate(objName) == false)
	{
		datefield.select();
		alert("You entered an invalid date! Please try again.");
		datefield.focus();
		return false;
	} else {
		return true;
	}
}

function chkdate(objName)
{
	// US date style
	// var strDatestyle = "US";
	// EU date style
	var strDatestyle = "EU";
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var booFound = false;
	var datefield = objName;
	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	var err = 0;
	var strMonthArray = new Array(12);
	strMonthArray[0] = "Jan";
	strMonthArray[1] = "Feb";
	strMonthArray[2] = "Mar";
	strMonthArray[3] = "Apr";
	strMonthArray[4] = "May";
	strMonthArray[5] = "Jun";
	strMonthArray[6] = "Jul";
	strMonthArray[7] = "Aug";
	strMonthArray[8] = "Sep";
	strMonthArray[9] = "Oct";
	strMonthArray[10] = "Nov";
	strMonthArray[11] = "Dec";
	strDate = datefield.value;

	if (strDate.length < 1)
	{
		return true;
	}

	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++)
	{
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1)
		{
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);
			if (strDateArray.length != 3)
			{
				err = 1;
				return false;
			} else {
				strDay = strDateArray[0];
				strMonth = strDateArray[1];
				strYear = strDateArray[2];
			}
			booFound = true;
		}
	}

	if (booFound == false)
	{
		if (strDate.length>5)
		{
			strDay = strDate.substr(0, 2);
			strMonth = strDate.substr(2, 2);
			strYear = strDate.substr(4);
		}
	}

	if (strYear.length == 2)
	{
		strYear = '20' + strYear;
	}

	// US style
	if (strDatestyle == "US")
	{
		strTemp = strDay;
		strDay = strMonth;
		strMonth = strTemp;
	}

	intday = parseInt(strDay, 10);
	if (isNaN(intday))
	{
		err = 2;
		return false;
	}

	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth))
	{
		for (i = 0;i<12;i++)
		{
			if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase())
			{
				intMonth = i+1;
				strMonth = strMonthArray[i];
				i = 12;
		   }
		}
		if (isNaN(intMonth))
		{
			err = 3;
			return false;
		}
	}

	intYear = parseInt(strYear, 10);
	if (isNaN(intYear))
	{
		err = 4;
		return false;
	}

	if (intMonth>12 || intMonth<1)
	{
		err = 5;
		return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1))
	{
		err = 6;
		return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1))
	{
		err = 7;
		return false;
	}
	if (intMonth == 2)
	{
		if (intday < 1)
		{
			err = 8;
			return false;
		}
		if (LeapYear(intYear) == true)
		{
			if (intday > 29)
			{
				err = 9;
				return false;
			}
		} else {
			if (intday > 28)
			{
				err = 10;
				return false;
			}
		}
	}

	if (strDatestyle == "US")
	{
		// initial code: datefield.value = strMonthArray[intMonth-1] + "/" + intday+"/" + strYear;
		datefield.value = intd + "/" + intM + "/" + strYear;
	} else {
		// initial code: datefield.value = intday + "/" + strMonthArray[intMonth-1] + "/" + strYear;

		if(intday < 10)
		{
			var ntd = String(intday);
			intd = "0" + ntd;
		} else {
			var intd = intday;
		}
		if(intMonth < 10)
		{
			var ntM = String(intMonth);
			intM = "0" + ntM;
		} else {
			var intM = intMonth;
		}

		datefield.value = intd + "" + intM + "" + strYear;
	}
	return true;
}

function LeapYear(intYear)
{
	if (intYear % 100 == 0)
	{
		if (intYear % 400 == 0) { return true; }
	} else {
		if ((intYear % 4) == 0) { return true; }
	}
	return false;
}

function doDateCheck()
{
	var r = document.form1.tickhotel.value;
	var f = document.form1.from.value;
	var t = document.form1.to.value;

	// extract date_parts directly from form data - conversion already done @ line 457-477
	var strDay1 = f.substr(0, 2);
	var strMonth1 = f.substr(2, 2);
	var strYear1 = f.substr(4);

	var strDay2 = t.substr(0, 2);
	var strMonth2 = t.substr(2, 2);
	var strYear2 = t.substr(4);

	var date_from = new Date(strYear1, strMonth1, strDay1);
	var date_to = new Date(strYear2, strMonth2, strDay2);
	var days_stay = days_diff(date_from, date_to);

	if ( ((r != "") && (r > 0)) && (f != "") && (t != "") && (date_from <= date_to) )
	{
		// alert("The dates are valid.");
		getTotal();
	} else {
		if (f == "" || t == "") 
			alert("Both [Arrival] and [Departure] dates must be filled in.");
		else 
			alert("Incorrect dates: Please make sure your [Departure] date is after your [Arrival] date.");
	}
}

function doDateFormat(dateobj)
{
	dateField = dateobj;
	// extract date_parts from validated date from form and re-assign form date value in required format 
	var strDay = dateobj.substr(0, 2);
	var strMonth = dateobj.substr(2, 2);
	var strYear = dateobj.substr(4);
	var df = strYear + "-" + strMonth + "-" + strDay;
	return df;
}

/*  Selects from a single Select All checkbox an array of Checkboxes
	that all have an identical ID that is supplied to the script */
function selectAllCheckboxes(itemID, controlID)
{
	boxes = document.getElementsByName(itemID);
	count = boxes.length;
	flag = controlID.checked;
	for (i = 0; i < count; i++)
	{
		boxes[i].checked = flag;
	}
}

/* Clears and resets the Select All checkbox if any member checkbox is unchecked */
function clearSelectAll(controlID)
{
	control = document.getElementsByName(controlID);
	flag = control[0].checked;
	if (flag)
	{
		control[0].checked = 0;
	}
}

windowFlag = 0;
/* Opens a non-scrollable window with the specified width and height in the center of the screen */
function openWindow(URL, wide, high, windowID)
{
    scrnX = screen.availWidth;
    scrnY = screen.availHeight;
    leftPos = (scrnX - wide)/2;
    topPos = (scrnY - high)/2;
    str = "status=no,toolbar=no,scrollbars=no,resizable=no,width=" + wide + ",height=" + high + ",screenX=" + leftPos + ",screenY=" + topPos + ",left=" + leftPos + ",top=" + topPos;
	if (windowFlag && !(dialog.closed))
	{
		dialog.close();
	}
	dialog = window.open(URL,windowID,str);
	windowFlag = 1;
}

/* Opens a scrollable window with the specified width and height in the center of the screen */
function openScrollingWindow(URL, wide, high, windowID)
{
    scrnX = screen.availWidth;
    scrnY = screen.availHeight;
    leftPos = (scrnX - wide)/2;
    topPos = (scrnY - high)/2;
    str = "status=no,toolbar=no,scrollbars=yes,location=0,resizable=no,width=" + wide + ",height=" + high + ",screenX=" + leftPos + ",screenY=" + topPos + ",left=" + leftPos + ",top=" + topPos;
	if (windowFlag && !(dialog.closed))
	{
		dialog.close();
	}
	dialog = window.open(URL,windowID,str);
	windowFlag = 1;
}

/* Delete Object confirmation popup */
function confirmDelete(object, form)
{
	str = "Are you sure you want to delete this " + object + "?";
	if (confirm(str))
	{
		document.forms[form].submit();
	}
}

/* Generic confirmation popup */
function confirmMessage(msg, form)
{
	if (confirm(msg))
	{
		document.forms[form].submit();
	}
}

/* Prints the concatenated value of two passed form fields to a page object specified by ID */
function printFormText(sourceForm, sourceField0, sourceField1, targetID)
{
	formPointer = document.forms[sourceForm];
	document.getElementById(targetID).innerHTML = formPointer.elements[sourceField0].value + " " + formPointer.elements[sourceField1].value;
}

/* Show/Hide object functions */
function show(object)
{
	if (document.getElementById && document.getElementById(object) != null)
	{
		document.getElementById(object).style.visibility = 'visible';
	}
}

function hide(object) {
	if (document.getElementById && document.getElementById(object) != null)
	{
		document.getElementById(object).style.visibility = 'hidden';
	}
}

function showHide(obj, status)
{
	if (status == '1')
	{
		document.getElementById(obj).style.display = 'block';
	} else {
		document.getElementById(obj).style.display = 'none';
	}
}

/* quick search application */
function searchByKeyword(e, formElement)
{
	var code;

	if (!e) {
		e = window.event;
	}

	if (e.keyCode)
	{
		code = e.keyCode;
	} else if (e.which)
	{
		code = e.which;
	}

	if (code == 13)
	{
		return executeKeywordSearch(false);
	}

	return true;
}

function executeKeywordSearch(shouldSubmit)
{
	if (document.keywordSearchForm && document.keywordSearchForm.searchTerms)
	{
	    var keywordForm = document.keywordSearchForm;
		var termsElement = keywordForm.searchTerms;
		var searchTerms = termsElement.value;
		searchTerms = searchTerms.replace(/^\s*/, "").replace(/\s*$/, "");

		if (searchTerms == '')
		{
			alert('Please select a criterion to search.');
			termsElement.value = '';
			termsElement.focus();
			return false;
		} else {
			keywordForm.action = '/searchkey.php';
       		if ( shouldSubmit )
			{
				keywordForm.submit();
			}
		}

		return true;
	}

	return false;
}