var iSingleNightCount = 1
var SingleNight = 0

var RuaRate=28
var RuaGuestRate=39
var NPRate=17
var NPGuestRate=30
// off peak
var RuaGuestOffPeakRate=25
var NPGuestOffPeakRate=17

function fWeekDay(){
document.write(' ')
}

function ValDates(iRow)
{
	var ValDteIn
	var ValDteOut = new Date()
	var datein
	var dateout
	var days
	var day = 24 * 60 * 60000
	var lodge=" "
	
	datein = eval("document.book.datein" + iRow)
	dateout = eval("document.book.dateout" + iRow)
	ValDteIn = ValidDate(datein)
	dayx = eval("document.book.days" + iRow)
	if (ValDteIn) 
	{
		ValDteOut = ValidDate(dateout)
		if (ValDteOut)
		{
			days = Math.round((ValDteOut - ValDteIn) / day)
			if (days < 0) {days = 0} // ignore negatives
			dayx.value = days
			if (days == 1) 
			{
				SingleNight = 1
			}
			else 
			{
				SingleNight = 0
			}
		}
		else 
		{
			dayx.value = 0
		}
	}
	else 
	{
		dayx.value = 0
	}

	lodge = eval("document.book.lodge"+iRow)
	if (SingleNight==1 && (lodge.value == "NP") && iSingleNightCount <= 3) 
	{
		alert("Don't forget to change the rate to the single night rate if \nonly staying one night. That is if not moving on to the other lodge.")
		iSingleNightCount++
	}
	if (days >= 1) 
	{
		ValRate(iRow)
	}
}

function ValidDate(datein)
{
	if(datein.value=="") { return 0 } // return if nothing
	var sDateIn = new String(datein.value)
	var dd
	var mm
	var yy
	var arDate = new Array()

	arDate = sDateIn.split("/")
	dd = parseInt(arDate[0],10)
	mm = (parseInt(arDate[1],10) - 1) // take one off to get correct month
	if(arDate.length == 3)
	{ // if year was specified
		yy = parseInt(arDate[2],10)
	}
	else 
	{
 		dDate = new Date() // get today
 		yy = parseInt(dDate.getFullYear(),10) // get this year
	}
	finalDate = new Date(yy,mm,dd)
	if(isNaN(finalDate)){
	alert("invalid date")
	return 0
	}


	return finalDate
}

function CalcTotal()
{
	days = document.book.days1.value
	rate = document.book.rate1.value
	// tot1 = Math.round(days * rate * 100)/100
	tot1 = days * rate
	document.book.total1.value = tot1

	days = document.book.days2.value
	rate = document.book.rate2.value
	tot2 = days * rate
	document.book.total2.value = tot2

	days = document.book.days3.value
	rate = document.book.rate3.value
	tot3 = days * rate
	document.book.total3.value = tot3

	days = document.book.days4.value
	rate = document.book.rate4.value
	tot4 = days * rate
	document.book.total4.value = tot4

	days = document.book.days5.value
	rate = document.book.rate5.value
	tot5 = days * rate
	document.book.total5.value = tot5

	days = document.book.days6.value
	rate = document.book.rate6.value
	tot6 = days * rate
	document.book.total6.value = tot6

	days = document.book.days7.value
	rate = document.book.rate7.value
	tot7 = days * rate
	document.book.total7.value = tot7

	days = document.book.days8.value
	rate = document.book.rate8.value
	tot8 = days * rate
	document.book.total8.value = tot8

	days = document.book.days9.value
	rate = document.book.rate9.value
	tot9 = days * rate
	document.book.total9.value = tot9

	days = document.book.days10.value
	rate = document.book.rate10.value
	tot10 = days * rate
	document.book.total10.value = tot10

	document.book.grand_total.value = tot1 + tot2 + tot3 + tot4 + tot5 + tot6 + tot7 + tot8 + tot9 + tot10
}

function SetRate(iRow)
{
	lodge = eval("document.book.lodge"+iRow)
	rate = eval("document.book.rate"+iRow)

	if (lodge.value == "R"){
		rate.value = RuaRate}
	else{
	if (lodge.value == "NP"){
		rate.value = NPRate}
	else{
	if (lodge.value == "RG"){
		rate.value = RuaGuestRate}
	else{
	if (lodge.value == "NPG"){
	   rate.value = NPGuestRate}
	else{
	if (lodge.value == "RGO"){
		rate.value = RuaGuestOffPeakRate}
	else{
	if (lodge.value == "NPGO"){
	   rate.value = NPGuestOffPeakRate}
	else{
		rate.value = 0}}}}}}

	CalcTotal()
}

function setfocus(field)
{
	//  document.book.memberemail.select()
	eval("document.book." + field + ".select()")
	eval("document.book." + field + ".focus()")
}

function myalert(i)
{
	switch(i)
	{
	case 1:alert("'Enter' will start a new line. It is good to break up your message with line breaks.")
	break
	case 2:alert("Enter the date in day slash month format e.g. 1/7. 01/07 is overkill. This is the date of the first night of your stay.")
	break
	case 3:alert("This is the date you leave. It is the day after the last night of your stay.")
	break
	case 4:alert("If you enter the dates as dd/mm this will be calculated for you. This is the number of days you are staying.")
	break
	case 5:alert("This is set on change of Lodge or Date. Change the rate for single night stays and kids under 5.")
	break
	case 6:alert("This will be calculated for you once the Days and Rate are known.")
	break
	case 7:alert("Choose the lodge & guest combination where you will stay. This will set the nightly rate.")
	break
	case 8:alert("Enter name and if a child the age also. Adults - you can enter your age if you want.")
	break
	case 9:alert("The total amount to pay for this booking. Click Calculate to have if added up for you.")
	break
	case 10:alert("The booking officer will tell you the door code for National Park after you pay.")
	break
	case 11:alert("total " + document.book.grand_total.value)
	break
	case (1==1):alert(i)
	break
   }
}

var iSubmitCount=0
function CheckSubmitted() {
CalcTotal()
if (document.book.thinksitisok.checked)
	{
	if (document.book.membername.value == "your name" || document.book.membername.value == "")
		{
		alert("Please enter your name and try again. (And you thought you had finished!)")
		setfocus("membername")
		return false
		}

	if (document.book.memberemail.value == "email address" || document.book.memberemail.value == "")
		{
		alert("Please enter your email address and try again. \nPut your contact details in the notes.")
		setfocus("memberemail")
		return false
		}

	if (checkEmail(document.book.memberemail)==false)
		{
		alert("You must enter a valid email address. \nPut your contact details in the notes.")
		setfocus("memberemail")
		return false
		}

	if (document.book.grand_total.value <= 0)
		{
		alert("Your booking in invalid. The total is $0\nTry clicking [Calculate]")
		return false
		}

	if (iSubmitCount == 0)
		{
		iSubmitCount++
		return true
		}
	else
		{
		alert("Your booking is being submitted as we speak.  Just click once only. Thanks!")
		return false
		}
	}
else
	{
	alert("If you are ready to book tick the Finished check box then click [Make Booking]")
	return false
	}
}

function ValRate(iRow)
{
// some rates are global (at top of page)
var SingleRuaRate=RuaRate
var SingleRuaGuestRate=RuaGuestRate

var under5NPRate=8.5
var SingleNPRate=20
var under5SingleNPRate=10

var SingleNPGuestRate=NPGuestRate
var under5NPGuestRate=15
var under5SingleNPGuestRate=15

// off peak
var SingleNPGuestOffPeakRate=15
var under5NPGuestOffPeakRate=7.5
var SingleRuaGuestOffPeakRate=25


	lodge = eval("document.book.lodge"+iRow).value
	rate = eval("document.book.rate"+iRow).value
	//days = eval("document.book.days"+iRow)
	days = parseInt(eval("document.book.days"+iRow).value)

	if (lodge == "R"){
		if (rate != RuaRate && rate != SingleRuaRate){
			//alert("This is not a valid rate for Ruapehu Lodge! Must be $"+RuaRate+" (or $"+SingleRuaRate+" single night). \nChoose Guest - Ruapehu from Lodge List to book a guest.")
			alert("This is not a valid rate for Ruapehu Lodge! Must be $"+RuaRate+".")
			SetRate(iRow)}}

	if (lodge == "NP" && days == 1){
		if (rate != SingleNPRate && rate != under5SingleNPRate && rate != NPRate && rate != under5NPRate){
			alert("This is not a valid rate for National Park Lodge! Must be $"+NPRate+" (or $"+SingleNPRate+" single night) ($"+under5NPRate+" or $"+under5SingleNPRate+" respectively if under 5). \nChoose Guest - Ruapehu from Lodge List to book a guest.")
			SetRate(iRow)}}

	if (lodge == "NP" && days > 1){
		if (rate != NPRate && rate != under5NPRate){
			alert("This is not a valid rate for National Park Lodge! Must be $"+NPRate+" (or $"+under5NPRate+" if under 5). \nChoose Guest - Natnl Park from Lodge List to book a guest.")
			SetRate(iRow)}}

// guests
	if (lodge == "RG"){
		if (rate != RuaGuestRate && rate != SingleRuaGuestRate){
			// alert("This is not a valid rate for Ruapehu Lodge! Must be $"+RuaGuestRate+" (or $"+SingleRuaGuestRate+" single night).")
			alert("This is not a valid rate for Ruapehu Lodge! Must be $"+RuaGuestRate+".")
			SetRate(iRow)}}

	if (lodge == "NPG" && days == 1){
		if (rate != SingleNPGuestRate && rate != under5SingleNPGuestRate && rate != NPGuestRate && rate != under5NPGuestRate){
			alert("This is not a valid rate for National Park Lodge! Must be $"+NPGuestRate+" (or $"+SingleNPGuestRate+" single night) ($"+under5NPGuestRate+" or $"+under5SingleNPGuestRate+" respectively if under 5).")
			SetRate(iRow)}}

	if (lodge == "NPG" && days > 1){
		if (rate != NPGuestRate && rate != under5NPGuestRate){
			alert("This is not a valid rate for National Park Lodge! Must be $"+NPGuestRate+" (or $"+under5NPGuestRate+" if under 5).")
			SetRate(iRow)}}

// guests off peak
	if (lodge == "RGO"){
		if (rate != RuaGuestOffPeakRate){
			alert("This is not a valid rate for Ruapehu Lodge, Guest, Off peak! Must be $"+RuaGuestOffPeakRate+".")
			SetRate(iRow)}}

	if (lodge == "NPGO"){
		if (rate != NPGuestOffPeakRate && rate != under5NPGuestOffPeakRate){
			alert("This is not a valid rate for National Park Lodge! Must be $"+NPGuestOffPeakRate+" ($"+under5NPGuestOffPeakRate+" if under 5).")
			SetRate(iRow)}}

	if (lodge == "XX"){
		alert("You must select a lodge and member/guest from the 'Lodge' list.")
		SetRate(iRow)}

	CalcTotal()
}

function CaluclateBtnClick()
{
	for(i=1;i<=10;i++)
	{
		ValDates(i)
	}
}

// check document.book.memberemail.value
function checkEmail(inputvalue)
{
    var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/
    if(pattern.test(inputvalue.value))
    {
		return true
	}
	else
	{
		return false
    }
}
