function cent(amount) {
// returns the amount in the .99 format
    amount -= 0;
    return (amount == Math.floor(amount)) ? amount + '.00' : (  (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}


function init() {

	init_total();
	init_misc();	// server generated javascript, in hotel_form.php
}

function init_total() {
	var frm = document.TheForm

	// Calculate total fee
	if (sdate!='') frm.txtStartDate.value = sdate
	if (edate!='') {
		frm.txtEndDate.disabled = false
		frm.txtEndDate.value = edate
	}

	setHotelCost()

}

function setHotelCost() {
	var frm = document.TheForm;

	var rate = 0
	id1 = frm.hotel1.selectedIndex
	if (id1>0) {
		if ( frm.room1[0].checked ) {
			rate = hotel_s[id1]
		}
		else if ( frm.room1[1].checked ) {
			rate = hotel_t[id1]
		}
		frm.cost1.value = cent(rate * frm.night1.value*1)
	}
	else {
		frm.cost1.value = 0
	}
	frm.cc_gtotal.value = cent(rate*1)
	frm.authorised_amt_sgd.value = cent(rate*1)

	id2 = frm.hotel2.selectedIndex
	if ( frm.room2[0].checked )
		frm.cost2.value = cent(hotel_s[id2] * frm.night2.value)
	else if ( frm.room2[1].checked )
		frm.cost2.value = cent(hotel_t[id2] * frm.night2.value)

	id3 = frm.hotel3.selectedIndex
	if ( frm.room3[0].checked )
		frm.cost3.value = cent(hotel_s[id3] * frm.night3.value)
	else if ( frm.room3[1].checked )
		frm.cost3.value = cent(hotel_t[id3] * frm.night3.value)

	/*
	var total = cent(frm.cost1.value*1 + frm.cost2.value*1 + frm.cost3.value*1)
	frm.cc_gtotal.value = cent(total)
	frm.authorised_amt_sgd.value = cent(total)
	*/
}


// triggered by hotel onchange event
function chkHotel(h) {

	frm = document.TheForm
	id1 = frm.hotel1.selectedIndex
	id2 = frm.hotel2.selectedIndex
	id3 = frm.hotel3.selectedIndex

	if (h==1)	{// hotel 1
		if (id1>0 && (id1==id2||id1==id3)) {
			alert ("Please do not select the same hotel more than once.")
			frm.hotel1.selectedIndex = 0
			frm.hotel1.focus()
			return false
		}
	}
	else if (h==2) {
		if (id2>0 && (id2==id1||id2==id3)) {
			alert ("Please do not select the same hotel more than once.")
			frm.hotel2.selectedIndex = 0
			frm.hotel2.focus()
			return false
		}
	}
	else if (h==3) {
		if (id3>0 && (id3==id1||id3==id2)) {
			alert ("Please do not select the same hotel more than once.")
			frm.hotel3.selectedIndex = 0
			frm.hotel3.focus()
			return false
		}
	}

	setHotelCost()
}



// check credit card number length
function check_cc() {
	var frm = document.TheForm
	var cclen = frm.cc_no1.value.length + frm.cc_no2.value.length + frm.cc_no3.value.length + frm.cc_no4.value.length
	if (frm.cc_type[0].checked || frm.cc_type[1].checked) {	// VISA, MASTER
		if (cclen<16) {
			alert ("VISA/Master credit card number must have 16 digits")
			frm.cc_no1.focus()
			return false
		}
	}
	//else if (frm.cc_type[2].checked) {	//DINERS
	//	if (cclen<14||cclen>14) {
	//		alert ("Diners Club credit card number must have 14 digits")
	//		frm.cc_no1.focus()
	//		return false;
	//	}
	//}
	else if (frm.cc_type[2].checked) {	//AMEX
		if (cclen<15||cclen>15) {
			alert ("American Express credit card number must have 15 digits")
			frm.cc_no1.focus()
			return false;
		}
	}
	//else if (frm.cc_type[4].checked) {	//DINERS
	//	if (cclen<16) {
	//		alert ("JCB credit card number must have 14 digits")
	//		frm.cc_no1.focus()
	//		return false;
	//	}
	//}
}




function verifyRegForm()
{
  if (document.TheForm.salutation.value.length == 0) {
	alert("Please select the title.");
	document.TheForm.salutation.focus();
	return false;
  }

  if (document.TheForm.firstname.value.length == 0) {
	alert("Please enter your first name.");
	document.TheForm.firstname.focus();
	return false;
  }

  if (document.TheForm.lastname.value.length == 0) {
	alert("Please enter your last name.");
	document.TheForm.lastname.focus();
	return false;
  }

  if (document.TheForm.company.value.length == 0) {
	alert("Please enter your company name.");
	document.TheForm.company.focus();
	return false;
  }

  if (document.TheForm.designation.value.length == 0) {
	alert("Please enter your designation.");
	document.TheForm.designation.focus();
	return false;
  }

  if (document.TheForm.address.value.length == 0) {
	alert("Please enter your mailing address.");
	document.TheForm.address.focus();
	return false;
  }

  if (document.TheForm.city.value.length == 0) {
	alert("Please enter the city name.");
	document.TheForm.city.focus();
	return false;
  }

  if (document.TheForm.zip.value.length == 0) {
	alert("Please enter the zip code.");
	document.TheForm.zip.focus();
	return false;
  }

  if (document.TheForm.country.value.length == 0) {
	alert("Please select your country.");
	document.TheForm.country.focus();
	return false;
  }

  if (document.TheForm.tel_cc.value.length == 0) {
	alert("Please select your telephone number country code.");
	document.TheForm.tel_cc.focus();
	return false;
  }
  fldValue = TheForm.tel_cc.value
  if ( fldValue != parseInt(fldValue) )
  {
	alert('Please enter the country code in numeric values only.')
	document.TheForm.tel_cc.focus();
	return false;
  }

  if (document.TheForm.tel_ac.value.length != 0) {
  fldValue = TheForm.tel_ac.value
  if ( fldValue != parseInt(fldValue) )
  {
	alert('Please enter the area code in numeric values only.')
	document.TheForm.tel_ac.focus();
	return false;
  }
  }

  if (document.TheForm.tel_no.value.length == 0) {
	alert("Please enter your telephone number.");
	document.TheForm.tel_no.focus();
	return false;
  }
  fldValue = TheForm.tel_no.value
  if ( fldValue != parseInt(fldValue) )
  {
	alert('Please enter the telephone number in numeric values only.');
	document.TheForm.tel_no.focus();
	return false;
  }

  if (document.TheForm.fax_cc.value.length == 0) {
	alert("Please select your fax number country code.");
	document.TheForm.fax_cc.focus();
	return false;
  }
  fldValue = TheForm.fax_cc.value
  if ( fldValue != parseInt(fldValue) )
  {
	alert('Please enter the fax country code in numeric values only.')
	document.TheForm.fax_cc.focus();
	return false;
  }

  if (document.TheForm.fax_ac.value.length != 0) {
  fldValue = TheForm.fax_ac.value
  if ( fldValue != parseInt(fldValue) )
  {
	alert('Please enter the fax area code in numeric values only.')
	document.TheForm.fax_ac.focus();
	return false;
  }
  }

  if (document.TheForm.fax_no.value.length == 0) {
	alert("Please enter your fax number.");
	document.TheForm.fax_no.focus();
	return false;
  }
  fldValue = TheForm.fax_no.value
  if ( fldValue != parseInt(fldValue) )
  {
	alert('Please enter the fax number in numeric values only.')
	document.TheForm.fax_no.focus();
	return false;
  }

  if (document.TheForm.email.value.length == 0) {
	alert("Please enter your email address.");
	document.TheForm.email.focus();
	return false;
  } else
  	{
  		if (document.TheForm.email.value.charAt(0) == " ")
  		{
  		  alert("The first character of Email field cannot be space.");
		  document.TheForm.email.focus();
   		  return false;
   		} else
   		    {
   		     	var email_v = document.TheForm.email.value;
		    	if (email_v.search('@') == -1) {
		     	   alert("Invalid Email Address without '@'.");
		           document.TheForm.email.focus();
		           return false;
			}
   		}
  	}


  var frm = document.TheForm


  if ((frm.hotel1.value.length == 0) && (frm.hotel2.value.length == 0) && (frm.hotel3.value.length == 0))
  {
   	alert("Please select at least one hotel room for booking.");
   	frm.hotel1.focus();
   	return false;
  }

  if (frm.hotel1.value.length != 0)
  {
  	if (frm.night1.value == "")
  	{
  	  alert("Please enter the number of nights for 1st choice.");
  	  frm.night1.focus();
  	  return false;
  	}

  	if (frm.room1[0].checked == false && frm.room1[1].checked == false)
  	{
  	  alert("Please select the room type for 1st choice hotel.");
  	  return false;
  	}
  }

  if (frm.hotel2.value.length != 0)
  {
  	if (frm.night2.value == "")
  	{
  	  alert("Please enter the number of nights for 2nd choice.");
  	  frm.night2.focus();
  	  return false;
  	}

  	if (frm.room2[0].checked == false && frm.room2[1].checked == false)
  	{
  	  alert("Please select the room type for 2nd choice hotel.");
  	  return false;
  	}

  }

  if (frm.hotel3.value.length != 0)
  {
  	if (frm.night3.value == "")
  	{
  	  alert("Please enter the number of nights for 3rd choice.");
  	  frm.night3.focus();
  	  return false;
  	}
  	if (frm.room3[0].checked == false && frm.room3[1].checked == false)
  	{
  	  alert("Please select the room type for 3rd choice hotel.");
  	  return false;
  	}
  }

  if (frm.checkin_date_dd.value.length == 0)
  {
  	alert("Please select the check-in date.");
  	frm.checkin_date_dd.focus();
  	return false;
  }
  if (frm.checkin_date_mm.value.length == 0)
  {
  	alert("Please select the check-in date.");
  	frm.checkin_date_mm.focus();
  	return false;
  }
  if (frm.checkin_date_yyyy.value.length == 0)
  {
  	alert("Please select the check-in date.");
  	frm.checkin_date_yyyy.focus();
  	return false;
  }
  if (frm.checkin_time.value.length == 0)
  {
  	alert("Please select the check-in time.");
  	frm.checkin_time.focus();
  	return false;
  }


  if (frm.checkout_date_dd.value.length == 0)
  {
  	alert("Please select the check-out date.");
  	frm.checkout_date_dd.focus();
  	return false;
  }
  if (frm.checkout_date_mm.value.length == 0)
  {
  	alert("Please select the check-out date.");
  	frm.checkout_date_mm.focus();
  	return false;
  }
  if (frm.checkout_date_yyyy.value.length == 0)
  {
  	alert("Please select the check-out date.");
  	frm.checkout_date_yyyy.focus();
  	return false;
  }
  if (frm.checkout_time.value.length == 0)
  {
  	alert("Please select the check-out time.");
  	frm.checkout_time.focus();
  	return false;
  }


  if (document.TheForm.cc_type[0].checked == false &&
  	document.TheForm.cc_type[1].checked == false &&
    document.TheForm.cc_type[2].checked == false)
  {
   	alert("Please select the type of credit card that you are using.");
   	document.TheForm.cc_type[0].focus();
   	return false;
  }

  if (document.TheForm.cc_name.value.length == 0)
  {
   	alert("Please enter the credit card holder's name.");
   	document.TheForm.cc_name.focus();
   	return false;
  }

  if (check_cc() == false)
  {
	  return false;
  }

  if (document.TheForm.cc_mm.value.length == 0) {
	alert("Please select the month of the expiry date.");
	document.TheForm.cc_mm.focus();
	return false;
  }

  if (document.TheForm.cc_yy.value.length == 0) {
	alert("Please select the year of the expiry date.");
	document.TheForm.cc_yy.focus();
	return false;
  }

  if (document.TheForm.cc_id.value.length == 0) {
	alert("Please enter the Last Three Digit Number Appearing on the Signature Panel.\nFor AMEX Card, enter only the last 3 digits located on the front of your card.");
	document.TheForm.cc_id.focus();
	return false;
  }
  return true;

}
