/* open a url in new window */
function openMe(URL) {
    var wf = "";

    wf = wf + "width=" + "710";
    wf = wf + ",height=" + "510";
    wf = wf + ",resizable=" + "yes";
    wf = wf + ",scrollbars=" + "no";
    wf = wf + ",menubar=" + "no";
    wf = wf + ",toolbar=" + "no";
    wf = wf + ",directories=" + "no";
    wf = wf + ",location=" + "no";
    wf = wf + ",status=" + "no";

    window.open(URL,'_blank',wf);
}

/* open a url in new window */
function openContact(URL) {
    var wf = "";

    wf = wf + "width=" + "750";
    wf = wf + ",height=" + "450";
    wf = wf + ",resizable=" + "no";
    wf = wf + ",scrollbars=" + "no";
    wf = wf + ",menubar=" + "no";
    wf = wf + ",toolbar=" + "no";
    wf = wf + ",directories=" + "no";
    wf = wf + ",location=" + "no";
    wf = wf + ",status=" + "no";

    window.open(URL,'_blank',wf);
}

/* open a help page in new window */
function openHelp(URL) {
    var wf = "";

    wf = wf + "width=" + "720";
    wf = wf + ",height=" + "600";
    wf = wf + ",resizable=" + "no";
    wf = wf + ",scrollbars=" + "no";
    wf = wf + ",menubar=" + "no";
    wf = wf + ",toolbar=" + "yes";
    wf = wf + ",directories=" + "no";
    wf = wf + ",location=" + "no";
    wf = wf + ",status=" + "no";

    window.open(URL,'_blank',wf);
}

/* open order form in new window */
function showForm(URL) {
    var wf = "";

    wf = wf + "width=" + "600";
    wf = wf + ",height=" + "600";
    wf = wf + ",resizable=" + "yes";
    wf = wf + ",scrollbars=" + "yes";
    wf = wf + ",menubar=" + "yes";
    wf = wf + ",toolbar=" + "no";
    wf = wf + ",directories=" + "yes";
    wf = wf + ",location=" + "no";
    wf = wf + ",status=" + "no";

    window.open(URL,'_blank',wf);
}


function validate() {

    for (i = 0; i <= document.cartform.elements.length; i++) {

      if ((document.cartform.elements[i].type == 'text')) {
        if ((document.cartform.elements[i].value >= 1 && document.cartform.elements[i].value != '')) {

        } else {
          alert("Quantity must be integer");
          return false;
        }
      }
    } // end for(i)
    return true;
}


function checkqty(qty, minorder) {

  var matchArr = (document.getElementById(qty).getAttribute('value')).match(/^(\d+)$/);

  if (document.getElementById(qty).getAttribute('value') < 1 || document.getElementById(qty).getAttribute('value') == '' || matchArr == null) {
    alert("Quantity must be an integer");
    document.getElementById(qty).value = minorder;
  }

}


function validate_empty() {

    for (i = 0; i <= document.orderform.elements.length; i++) {

      if ((document.orderform.elements[i].name == 'empty')) {
        if ((document.orderform.elements[i].value == 'true')) {
          alert('Your cart is empty now');
	  return false;
        } else {
	  alert('Thank you for your order!');
          return ture;
        }
      }
    } // end for(i)

    return true;
}

// pre load images
  if (document.images) {

    update_on = new Image();
    update_on.src = "../images/up_cart1.jpg";
    update_off = new Image();
    update_off.src = "../images/up_cart2.jpg";

    submit_on = new Image();
    submit_on.src = "../images/s_order2.jpg";
    submit_off = new Image();
    submit_off.src = "../images/s_order1.jpg";

    add_on = new Image();
    add_on.src = "../images/add_cart1.jpg";
    add_off = new Image();
    add_off.src = "../images/add_cart.jpg";

  }


function updateOver() {

  if (document.images)
    document.submitimg.src=update_on.src;
}

function updateOut() {
  if (document.images)
    document.submitimg.src=update_off.src;
}


function submitOver() {

  if (document.images)
    document.submitimg.src=submit_on.src;
}

function submitOut() {
  if (document.images)
    document.submitimg.src=submit_off.src;
}

function addOver() {

  if (document.images)
    document.addcartimg.src=add_on.src;
}

function addOut() {
  if (document.images)
    document.addcartimg.src=add_off.src;
}

// notify user that product selected is being added to shopping cart
function notifyUser(STRING) {
    alert(STRING);
}


// used in admin section
function confirmSubmit()
{
var agree=confirm("Are you sure you wish to continue?");
if (agree)
	return true ;
else
	return false ;
}


// notify user
function tellUser(SSS) {
    alert(SSS);
}


function validateadd() {

  var matchPrice = (document.getElementById('price').getAttribute('value')).match(/^(\d{1,4})\.?(\d{2})?$/);
  if (document.getElementById('cotton') != null) {
    var matchCotton = (document.getElementById('cotton').getAttribute('value')).match(/^(\d{1,3})$/);
  }
  
  if (document.getElementById('subid') != null) {
    var matchSubid = (document.getElementById('subid').getAttribute('value')).match(/^(\d+)$/);
  }

  if (document.getElementById('pcode').getAttribute('value') == '') {
    alert('Tour Code must not be empty');
    return false;
  } else if (matchPrice == null || document.getElementById('price').getAttribute('value') < 1) {
    alert('invalid price');
    return false;
  } else if (document.getElementById('cotton').getAttribute('value') < 1 || document.getElementById('cotton').getAttribute('value') > 100 || matchCotton == null) {
    alert('cotton percentage must within the range of 1 -- 100');
    return false;
  } else if (document.getElementById('subid').getAttribute('value') == '' || document.getElementById('subid').getAttribute('value') < 1 || matchSubid == null) {
    alert('invalid sub category id!');
    return false;
  } 

  return true;
}

function validateuser() {

  if (document.getElementById('userid').getAttribute('value') == '' || document.getElementById('userpwd').getAttribute('value') == '') {
    alert('user id or password can not be empty');
    return false;
  }

  if (document.getElementById('user_status').getAttribute('value').match(/^(\d+)$/)== null) {
    alert('status must be 0 or 1');
    return false;
  }
return true;
}

function validatebooking() {
  if ( document.getElementById('family_name').getAttribute('value') == '' &&
  		document.getElementById('given_name').getAttribute('value') == '' ) {
    alert('Please fill in your name!');
    return false;
  } else if ( document.getElementById('phone').getAttribute('value') == '' && 
  		document.getElementById('mobile').getAttribute('value') == '' ) {
    alert('Please enter at least one of contact number for our consultant to contact you!');
    return false;
  } else if ( document.getElementById('depart_date').getAttribute('value') == '') {
    alert('Please specify the desired departure date!');
    return false;
  }
}

function myMove(el, x)
{
	while ( el.parentNode && 'tr' != el.nodeName.toLowerCase() )
	{
		el = el.parentNode;
	}

	var t = el.parentNode;
	var i = el.rowIndex + x;

	if ( i < 0 ) 
		i += t.rows.length;
	if ( i == t.rows.length ) i = 0;

	t.removeChild(el);
	var nRow = t.insertRow( i );
	t.replaceChild(el, nRow);
}

function saveData()
{
	//document.myform.process.value="save";
	savedString = "";
	//alert("total rows="+document.all.mytable.rows.length);

	for (i=0;i<document.getElementById("tourTable").rows.length;i++)
	{
	  //if (i > 0)
	    //savedString += "~";
	  //alert("index="+i);

	  var column = document.getElementById("tourTable").rows[i].cells[0];
	  
	  if (i == document.getElementById("tourTable").rows.length-1)
	    savedString += column.getAttribute("value");
	  else
	    savedString += column.getAttribute("value") + "@@";
	}
	document.tourForm.savedData.value = savedString;
	//alert(document.tourForm.savedData.value);
}

function saveTable()
{
	saveData();
	document.tourForm.submit();

}

function saveData2()
{
	//document.myform.process.value="save";
	savedString = "";
	//alert("total rows="+document.getElementById("regionTable").rows.length);

	for (i=0;i<document.getElementById("regionTable").rows.length;i++)
	{
	  //if (i > 0)
	    //savedString += "~";
	  //alert("index="+i);

	  var column = document.getElementById("regionTable").rows[i].cells[0];
	  
	  if (i == document.getElementById("regionTable").rows.length-1)
	    savedString += column.getAttribute("value");
	  else
	    savedString += column.getAttribute("value") + "@@";			
	}
	document.regionForm.savedData.value = savedString;
	//alert(document.regionForm.savedData.value);
}

function saveTable2()
{
	saveData2();
	document.regionForm.submit();

}

var obj;
var AppID = "10331003";

function LaunchMSN()
{
	
	eval ('try {obj = new ActiveXObject("MSNMessenger.P4QuickLaunch"); } catch (e) {obj = null;}');
	
	if (obj != null)
	{
		obj.LaunchApp(AppID, "ncttours@hotmail.com");
		return true;
	}

}


/*
 * Floating table related functions START
 */
// How many pixels from the top to display the content
var topOffset = 153

function keepAlive() {
	// Move the content into view
	document.all.navBar.style.pixelTop = document.body.scrollTop + topOffset
}

function smoothMove() {
	var Dif = parseInt((document.body.scrollTop+topOffset-document.all.navBar.offsetTop)*.1)
	// Work-around wierd Netscape NaN bug when Dif is 0
	if (isNaN(Dif)) Dif=0
	document.all.navBar.style.pixelTop+=Dif

}

function doLoad() {
	// Initialize DHTMLLib
	setup()

	// Ver1: When the page scrolls, move the content
	//window.onscroll = keepAlive;

	// Ver2:
	window.setInterval("smoothMove()",15)
	// Initialize the position
	keepAlive()
}

///////////////////////////////////////////////////
var topOffset2 = 0

function keepAlive2() {
	// Move the content into view
	document.all.onlineService.style.pixelTop = document.body.scrollTop + topOffset2
}

function smoothMove2() {
	var Dif2 = parseInt((document.body.scrollTop+topOffset2-document.all.onlineService.offsetTop)*.1)
	// Work-around wierd Netscape NaN bug when Dif is 0
	if (isNaN(Dif2)) Dif2=0
	document.all.onlineService.style.pixelTop+=Dif2

}

function doLoad2() {
	// Initialize DHTMLLib
	setup()

	// Ver1: When the page scrolls, move the content
	//window.onscroll = keepAlive;

	// Ver2:
	window.setInterval("smoothMove2()",15)
	// Initialize the position
	keepAlive2()
}
// Floating table related functions END



/////////////////// Agent Booking Functions //////////////

function validateBookRequestStep1(thisform) {
	var matchNumber;
	var validAdultNum=true;
	var validChildNum=true;
	var validInfantWSeatsNum=true;
	var validInfantWOSeatsNum=true;
	var emptyVal=true;

	if (document.getElementsByName('num_adult')[0].value != '') {
	  matchNumber = (document.getElementsByName('num_adult')[0].value).match(/^(\d+)$/);
	  if (!matchNumber)
	    validAdultNum=false;
	  emptyVal=false;
	}
	
	if (document.getElementsByName('num_child')[0].value != '') {
	  matchNumber = (document.getElementsByName('num_child')[0].value).match(/^(\d+)$/);
	  if (!matchNumber)
	    validChildNum=false;
	  if (document.getElementsByName('num_adult')[0].value == '' && validAdultNum) {
	    alert('At least one adult should accompany with children or infants!');
	    return false;
	  }
	  emptyVal=false;
	}
		
	if (document.getElementsByName('num_infants_with_seat')[0].value != '') {
	  matchNumber = (document.getElementsByName('num_infants_with_seat')[0].value).match(/^(\d+)$/);
	  if (!matchNumber)
	    validInfantWSeatsNum=false;
	  if (document.getElementsByName('num_adult')[0].value == '' && validAdultNum) {
	    alert('At least one adult should accompany with children or infants!');
	    return false;
	  }
	  emptyVal=false;
	}
	
	if (document.getElementsByName('num_infant_with_no_seat')[0].value != '') {
	  matchNumber = (document.getElementsByName('num_infant_with_noseat')[0].value).match(/^(\d+)$/);
	  if (!matchNumber)
	    validInfantWOSeatsNum=false;
	  if (document.getElementsByName('num_adult')[0].value == '' && validAdultNum) {
	    alert('At least one adult should accompany with children or infants!');
	    return false;
	  }
	  emptyVal=false;
	}

	if (emptyVal || validAdultNum == false || validChildNum == false || validInfantWSeatsNum == false || validInfantWOSeatsNum == false) {
	  alert('Please check the number of guests you specified!');
	  return false;
	}
}

function validateBookRequestStep2(thisform, total_guests, total_pickup_locs) {

	var n_pick_up_people=0;
	var matchNumber;
//alert(document.getElementsByName('pick_loc0')[0].value);


	if (document.getElementsByName('agent_staff')[0].value == '') {
	  alert('Please fill in the agent staff field!');
	  return false;
	}

	  if (document.getElementsByName('guest0') != null) {
		if (document.getElementsByName('guest0')[0].value == '') {
		  alert('Please enter the guest name!');
		  return false;
		}
	  }
/*
	  if (!document.getElementsByName('guest_sex0')[0].checked && !document.getElementsByName('guest_sex0')[1].checked) {
		// no radio button is selected
		alert('Please specify guest gender!');
		return false;
	  }

*/
/*
	for (i = 0; i < total_guests; i++) {
	  if (document.getElementsByName('guest'+i) != null) {
		if (document.getElementsByName('guest'+i)[0].value == '') {
		  alert('Please enter the guest name!');
		  return false;
		}
	  }

	  if (!document.getElementsByName('guest_sex'+i)[0].checked && !document.getElementsByName('guest_sex'+i)[1].checked) {
		// no radio button is selected
		alert('Please specify guest gender!');
		return false;
	  }
	 
    	} // end for(i)
*/
		
	for (i=0; i < total_pickup_locs; i++) {
	  if (document.getElementsByName('pick_loc'+i)[0].value) {
	    matchNumber = (document.getElementsByName('pick_loc'+i)[0].value).match(/^(\d+)$/);
	  	if (!matchNumber) {
	  	  alert('Please enter a vaild number!');
	  	  return false;
	  	}
	    n_pick_up_people += eval(document.getElementsByName('pick_loc'+i)[0].value);	    
	  }	  
	} // end for(i)

    if (n_pick_up_people != total_guests) {
	  alert('Total number of guests does not match with the sum of guests you specified in all pick up locations!');
	  return false;
	}


	if (document.getElementsByName('contact_num')[0].value == '') {
	  alert('Please tell us your contact number!');
	  return false;
	}
/*
	if (document.getElementsByName('email')[0].value == '' || !isValidEmail(document.getElementsByName('email')[0].value)) {
	  alert('Please fill in a valid e-mail address so that we can send you the confirmation e-mail!');
	  return false;
	}
*/
	if (document.getElementsByName('voucher')[0].value == '') {
	  alert('Please fill in the voucher!');
	  return false;
	}

}

function isValidEmail(str) {	
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}
