
// cvs id: $Id: script.js,v 1.7 2009/08/03 00:16:54 sean Exp $
// ---------------------------------------------------------
//  bookingQueryForm.js
//  validation checks for booking query form
// ---------------------------------------------------------

// ---------------------------------------------------------
function indexOfFieldObjectKnownAs(fieldName) { 
	for (var i=0;i<document.criteriaForm.elements.length;i++) 
		if (fieldName == document.criteriaForm.elements[i].name) 
    	return i; 
	return -1; 
} 
    

// ---------------------------------------------------------
function RemoveOptions(id) {
  var field = document.getElementById(id);

  field.options.length = 0;
}

// ---------------------------------------------------------
function flipVis(hide, show) {
  if (hide) {
    var previousDiv = document.getElementById(hide);
    if (previousDiv)
      previousDiv.disabled = true;
  }
  
  if (show) {
    var nextDiv = document.getElementById(show);
    if (nextDiv)
      nextDiv.disabled = false;

  }
}


// ---------------------------------------------------------
function clearSelectDisable(name, dummyMessage) {

  var theElement = document.getElementById(name);

  if (theElement) {

    theElement.options.length = 0;

    // Selected a country that seems real -> get a state
    var option = document.createElement('option');
    option.text = dummyMessage;
    option.value = 'dummy';
    theElement.options[theElement.options.length] = option;

    theElement.disabled = true;

  } else {
    alert('cnf');
  }

  return true;
}


// ---------------------------------------------------------
function setLoading(id) {
 
  var select = document.getElementById(id);

  if (!select)
    return;

  select.disabled = true;

  RemoveOptions(id);


  var newOption = document.createElement('option');
  newOption.text='[Loading...]';
  newOption.value='';

  select.options[select.options.length] = newOption;

  //var div = document.getElementById('div_' + id);
  //if (div)
 //  div.style.display = "";
}

// ---------------------------------------------------------
function callServer(id) {
  var theValue = 0;

  if (id != 'lastKnownSearch') {
    var selekta = document.getElementById(id);

    if(id == 'countryId') {
      setLoading('stateId');

      //var lc = document.getElementById('locations');
      //if (lc)
      //  lc.disabled = true;

    } else if (id == 'stateId') {
      setLoading('locations');
    }

    theValue = selekta.options[selekta.selectedIndex].value;
  }



  var head = document.getElementsByTagName('head').item(0);
  var old  = document.getElementById('lastLoadedCmds');
  if (old)
    head.removeChild(old);

  var script = document.createElement('script');
  script.src = g_remoteServer + '?portalId=' + g_portalId + '&element=' + id + '&value=' + theValue;
  script.type = 'text/javascript';
  script.defer = true;
  script.id = 'lastLoadedCmds';
  void(head.appendChild(script));
}


function initGridSearch() {

  callServer('lastKnownSearch');
}




  // ---------------------------------------------------------
  function checkGrid() {
    var rv = true;
    var nullOptions = new Array("0", "dummy", "");
    var errorMessage = '';
    var formFieldsToCheck;


    var selectionByName  = document.getElementById('mainform').selectionByName;

    if (selectionByName.value == 'y') {

      formFieldsToCheck = new Array("property");
      errorMessage = "Please select a Property.";

    } else {

      formFieldsToCheck = new Array("countryId","stateId","locations");
      errorMessage = "Please select a Country, State and Region.";

    }

    for (var x = 0; x < formFieldsToCheck.length; x++) {

      var fieldRef = document.getElementById(formFieldsToCheck[x]);
      var currentValue = fieldRef.options[fieldRef.selectedIndex].value;

      for (var y = 0; y < nullOptions.length; y++) {

        if (currentValue == nullOptions[y]) {
          rv = false;
          break;
        }
      }

      if (rv == false)
        break;

    }

    if (rv == false) {
      alert(errorMessage);
    }

    return rv;
  }

/*
  // ---------------------------------------------------------
  function rangeCheck (nowDate) {
    var fy = document.criteriaForm.fromDateYear.value;
    var fm = document.criteriaForm.fromDateMonth.value;
    var fd = document.criteriaForm.fromDateDay.value;
    var fromDate = new Date(fy,fm,fd);

    var ty = document.criteriaForm.toDateYear.value;
    var tm = document.criteriaForm.toDateMonth.value;
    var td = document.criteriaForm.toDateDay.value;
    var toDate = new Date(ty,tm,td);

    var rs = document.criteriaForm.rangeStatus;
    if (fromDate >= toDate) {
      //rs.value = 'Bad date range !';
      //rs.className = 'rangeInvalid';
      //return false;
      
      var newToDate = new Date(fromDate.getTime() + 86400000);
      document.criteriaForm.toDateYear.value = newToDate.getFullYear();
      document.criteriaForm.toDateMonth.value = newToDate.getMonth();
      document.criteriaForm.toDateDay.value = newToDate.getDate();

      rs.value = '';
      rs.className = 'rangeValid';
      
      return true;
      
    } else if (nowDate > fromDate) {
      rs.value = 'Date before today !';
      rs.className = 'rangeInvalid';
      return false;
    } else {
      rs.value = '';
      rs.className = 'rangeValid';
      return true;
    }
  }

  function roomsCheck () {
    var rooms = document.criteriaForm.rooms.value;
    var roomsOk;
    var reg = new RegExp('^[0-9]+$');

    //	Check that all characters in rooms are digits
    if (rooms=='0' || rooms=='00')
      roomsOk = false; //	Special case: 0 is not allowed
    else if (reg.test(rooms))
      roomsOk = true;
    else
      roomsOk = false;

    var rs = document.criteriaForm.roomsStatus;
    if (!roomsOk) {
      rs.value = 'Must be a number !';
      rs.className = 'rangeInvalid';
      return false;
    } else {
      rs.value = '';
      rs.className = 'rangeValid';
      return true;
    }
  }
*/


/*
	Div calendar
*/
  function rangeCheck (nowDate, groupName) {
	var fd = document.getElementById('fromDateDay').value;

	var fmy = document.getElementById('fromDateMonthYear').value;
	var fmyarr=fmy.split('-');
	var fm = fmyarr[0];
    var fy = fmyarr[1];
    var fromDate = new Date(fy,fm,fd);

	var td = document.getElementById('toDateDay').value;
	var tmy = document.getElementById('toDateMonthYear').value;
	var tmyarr=tmy.split('-');
	var tm = tmyarr[0];
    var ty = tmyarr[1];
    var toDate = new Date(ty,tm,td);

    var rs = document.getElementById('rangeStatus');

	var dateToUpdate = '';

	// fromDate is after toDate - reset the toDate
	if (fromDate >= toDate) {
      var newToDate = new Date(fromDate.getTime() + 86400000);
	  var newYear = newToDate.getFullYear();
	  var newMonth = newToDate.getMonth();

	  document.getElementById('toDateMonthYear').value = newMonth+'-'+newYear;
	  var test = newYear+'-'+newMonth;
      document.getElementById('toDateDay').value = newToDate.getDate();

	  if (groupName == 'toDate') {
		dateToUpdate = newToDate;
	  } else {
		dateToUpdate = fromDate; 
	  }
	
	  // updating the calendar div
	  document.getElementById('embedded-calendar').innerHTML = '';
	  setupCalendar(dateToUpdate);
    } 
	
	// invalid date range
	else if (nowDate > fromDate) {
		setTimeout("closeInvalidDate()",3000);
		document.getElementById('embedded-calendar').style.display='none';
		document.getElementById('invalid-date').style.display='block';
      return false;
    }
	
	// valid range
	else {
	  if (groupName == 'toDate') {
		dateToUpdate = toDate;
	  } else {
		dateToUpdate = fromDate; 
	  }

	  // updating the calendar div
	  document.getElementById('embedded-calendar').innerHTML = '';
	  setupCalendar(dateToUpdate);

      return true;
    }
  }


  function updateCalendar(formName, groupName) {
	document.getElementById('embedded-calendar').style.display = 'block';
	document.criteriaForm.selectedCal.value = groupName;
	setTimeout("closeCal()",10000);
  }

  // auto close calendar after x seconds
  function closeCal() {
	document.getElementById('embedded-calendar').style.display = 'none';
  }

  // close invalid date box
  function closeInvalidDate() {
	document.getElementById('invalid-date').style.display='none';
  }
  
  function roomsCheck () {
    var rooms = document.criteriaForm.rooms.value;
    var roomsOk;
    var reg = new RegExp('^[0-9]+$');

    //	Check that all characters in rooms are digits
    if (rooms=='0' || rooms=='00') roomsOk=false; //	Special case: 0 is not allowed
	else if (reg.test(rooms)) roomsOk=true;
    else roomsOk=false;

    if (!roomsOk) {
		alert('You must select at least one room');
      return false;
    } else {
      return true;
    }
  }

  function switchToPropertyView() {
    // change the background

    var selectionByName  = document.getElementById('mainform').selectionByName;

    //if (selectionByName.value == 'y')
    //  return;

    selectionByName.value = 'y';



    var portalFormSearchGrid  = document.getElementById('portalFormSearchGrid');
    portalFormSearchGrid.style.background = 'url(/siteSpecific/page/images/searchPanel.property.gif) no-repeat 0 0';


    var divCountry  = document.getElementById('div_country');
    var selectCountry  = document.getElementById('countryId');


    divCountry.style.visibility  = 'hidden';
    selectCountry.style.zIndex = -1;
    selectCountry.disabled = true;
    selectCountry.style.visibility  = 'hidden';

    var divState  = document.getElementById('div_stateId');
    var selectState  = document.getElementById('stateId');


    divState.style.visibility  = 'hidden';
    selectState.style.zIndex = -1;
    selectState.disabled = true;
    selectState.style.visibility  = 'hidden';


    var divRegion  = document.getElementById('div_locations');
    var selectRegion  = document.getElementById('locations');


    divRegion.style.visibility  = 'hidden';
    selectRegion.style.zIndex = -1;
    selectRegion.disabled = true;
    selectRegion.style.visibility  = 'hidden';


    var divProperty  = document.getElementById('div_property');
    var selectProperty  = document.getElementById('property');

    selectProperty.style.visibility  = 'visible';
    selectProperty.disabled = false;
    selectProperty.style.zIndex = 50;
    divProperty.style.visibility  = 'visible';



    return;
  }

  function switchToRegionView() {

    var selectionByName  = document.getElementById('mainform').selectionByName;

    //if (selectionByName.value == 'n')
    //  return;

    selectionByName.value = 'n';

    var portalFormSearchGrid  = document.getElementById('portalFormSearchGrid');
    portalFormSearchGrid.style.background = 'url(/siteSpecific/page/images/searchPanel.gif) no-repeat 0 0';




    var divProperty  = document.getElementById('div_property');
    var selectProperty  = document.getElementById('property');


    divProperty.style.visibility  = 'hidden';
    selectProperty.style.zIndex = -1;
    selectProperty.disabled = true;
    selectProperty.style.visibility  = 'hidden';


    var divCountry  = document.getElementById('div_country');
    var selectCountry  = document.getElementById('countryId');

    selectCountry.style.visibility  = 'visible';
    selectCountry.disabled = false;
    selectCountry.style.zIndex = 50;
    divCountry.style.visibility  = 'visible';

    var divState  = document.getElementById('div_stateId');
    var selectState  = document.getElementById('stateId');

    selectState.style.visibility  = 'visible';
    selectState.disabled = false;
    selectState.style.zIndex = 50;
    divState.style.visibility  = 'visible';


    var divRegion  = document.getElementById('div_locations');
    var selectRegion  = document.getElementById('locations');

    selectRegion.style.visibility  = 'visible';
    selectRegion.disabled = false;
    selectRegion.style.zIndex = 50;
    divRegion.style.visibility  = 'visible';



    return;
  }




