function hawaiifun_urlEncode(clearString)
{
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length)
  {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '')
    {
    	output += match[1];
      x += match[1].length;
    }
    else
    {
      if (clearString[x] == ' ')
        output += '+';
      else
      {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

function hawaiifun_trim(str)
{
  if (typeof(str) != 'string') return str;
  return str.replace(/^\s+|\s+$/g, '');
}

function checkpositivenumber(x)
{
	var anum=/^\d+$/;
	return anum.test(x);

}

function checkdate()
{
	if (document.getElementById('date').value.length == 0)
	{
		alert('Please select activity date');
		return false;
	}

	return true;
}

function checkextraitem(requested, limited, available, name)
{
  requested = hawaiifun_trim(requested);
	if (!checkpositivenumber(requested))
	{
		alert('Please enter valid extra amount(s)');
		return false;
	}
	if (limited && requested > available)
	{
		alert('Sorry, only ' + available + ' ' + name + '(s) available');
		return false;
	}
	return true;
}

function availability_popup(basePath, activityid, date, guestCountMap, frompage, fromvalue, hotelid, room, transportationrouteid)
{
  if (date == '')
  {
  	alert('Please select activity date');
  	return;
  }
  var guestsParameterString = '';
  var haveGuests = false;
  for (var guestTypeId in guestCountMap)
  {
    if (!parseInt(guestTypeId)) continue;
    if (!guestCountMap[guestTypeId]) continue;
    guestsParameterString += '&guests_t' + guestTypeId + '=' + guestCountMap[guestTypeId];
    haveGuests = true;
  }
  if (!haveGuests)
  {
    alert('Please select number of guests');
    return;
  }
  window.open(basePath + 'AvailabilityCheck.shtml?activityid=' + activityid + '&date=' + date + guestsParameterString + '&frompage=' + frompage + '&fromvalue=' + fromvalue + '&hotelid=' + hotelid + '&room=' + room + '&transportationrouteid=' + transportationrouteid, 'terms', 'width=550,height=180,scrollbars=yes,resizable=yes,top=100,left=100');
}

function purchase(activityid, date, guestCountMap, frompage, fromvalue, hotelid, room, transportationrouteid, extraitemids, extraitemamounts)
{
  var guestsParameterString = '';
  for (var guestTypeId in guestCountMap)
  {
    if (!parseInt(guestTypeId)) continue;
    if (!guestCountMap[guestTypeId]) continue;
    guestsParameterString += '&guests_t' + guestTypeId + '=' + hawaiifun_urlEncode(guestCountMap[guestTypeId]);
  }
  var q = 'AddToShoppingCart.shtml?activityid=' + activityid
          + '&date=' + date + guestsParameterString
          + '&frompage=' + hawaiifun_urlEncode(frompage) + '&fromvalue=' + hawaiifun_urlEncode(fromvalue)
          + '&hotelid=' + hotelid + '&room=' + hawaiifun_urlEncode(room) + '&transportationrouteid=' + transportationrouteid;
  q = q + '&returnurl=' + hawaiifun_urlEncode(window.opener.location.href);
  for (var i=0; i<extraitemids.length; i++)
  {
    q = q + '&extraitemid=' + extraitemids[i] + '&extraitemamount=' + hawaiifun_urlEncode(extraitemamounts[i]);
  }
  window.opener.location = q;
  window.close();
}

