function 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] == ' ' || clearString.charCodeAt(x).toString(16).toUpperCase() == '20' || clearString.charCodeAt(x).toString(16).toUpperCase() == 'A0')
      {
        output += '+';
      }
      else
      {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

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 checkcancellation(cancellationpolicy)
{
    if (!cancellationpolicy.checked)
    {
        alert('You must check the box acknowledging our cancellation policy!');
        return false;
    }

	return true;
}

function checkextraitem(itemid, limited, available, name)
{
	var v = document.getElementById('extraitemamount_' + itemid).value;
	if (!checkpositivenumber(v))
	{
		alert('Please enter valid extra amount(s)');
		return false;
	}
	if (limited && v > available)
	{
		alert('Sorry, only ' + available + ' ' + name + '(s) available');
		return false;
	}
	return true;
}

var query = '';
var activitydate = '';
var seats1;
var seats2;
var seats3;
var seats4;
var seats5;

function reservation(referrerid, activityid, date, discountname, discountpercents)
{
  reservation_init(referrerid, activityid, date, discountname, discountpercents, 0.0, discountpercents, 0.0, discountpercents, 0.0, discountpercents, 0.0, discountpercents, 0.0, discountpercents, discountpercents, window.location.href);
}

function reservation2(referrerid, activityid, date, discountname, discountpercentage1, discountvalue1, discountpercentage2, discountvalue2, discountpercentage3, discountvalue3, discountpercentage4, discountvalue4, discountpercentage5, discountvalue5, discountpercentagetransportation, discountpercentageextras)
{
  reservation_init(referrerid, activityid, date, discountname, discountpercentage1, discountvalue1, discountpercentage2, discountvalue2, discountpercentage3, discountvalue3, discountpercentage4, discountvalue4, discountpercentage5, discountvalue5, discountpercentagetransportation, discountpercentageextras, window.location.href)
}

function reservation_init(referrerid, activityid, date, discountname, discountpercentage1, discountvalue1, discountpercentage2, discountvalue2, discountpercentage3, discountvalue3, discountpercentage4, discountvalue4, discountpercentage5, discountvalue5, discountpercentagetransportation, discountpercentageextras, referrerurl)
{
  query =
    'referrerid=' + URLEncode(referrerid) +
    '&referrerurl=' + URLEncode(referrerurl) +
    '&activityid=' + URLEncode(activityid) +
    (date != undefined ? '&date=' + URLEncode(date) : '') +
    '&discountname=' + URLEncode(discountname) +
    '&discountpercentage1=' + URLEncode(discountpercentage1) +
    '&discountvalue1=' + URLEncode(discountvalue1) +
    '&discountpercentage2=' + URLEncode(discountpercentage2) +
    '&discountvalue2=' + URLEncode(discountvalue2) +
    '&discountpercentage3=' + URLEncode(discountpercentage3) +
    '&discountvalue3=' + URLEncode(discountvalue3) +
    '&discountpercentage4=' + URLEncode(discountpercentage4) +
    '&discountvalue4=' + URLEncode(discountvalue4) +
    '&discountpercentage5=' + URLEncode(discountpercentage5) +
    '&discountvalue5=' + URLEncode(discountvalue5) +
    '&discountpercentagetransportation=' + URLEncode(discountpercentagetransportation) +
    '&discountpercentageextras=' + URLEncode(discountpercentageextras);
  activitydate = date;
  seats1 = 0;
  seats2 = 0;
  seats3 = 0;
  seats4 = 0;
  seats5 = 0;
}

function addseats1(seats, price, priceafterdiscount)
{
  query = query + '&seats1=' + URLEncode(seats);
  seats1 = seats;
}

function addseats2(seats, price, priceafterdiscount)
{
  query = query + '&seats2=' + URLEncode(seats);
  seats2 = seats;
}

function addseats3(seats, price, priceafterdiscount)
{
  query = query + '&seats3=' + URLEncode(seats);
  seats3 = seats;
}

function addseats4(seats, price, priceafterdiscount)
{
  query = query + '&seats4=' + URLEncode(seats);
  seats4 = seats;
}

function addseats5(seats, price, priceafterdiscount)
{
  query = query + '&seats5=' + URLEncode(seats);
  seats5 = seats;
}

function addseatsfromselect(select)
{
  var id = select.options[select.selectedIndex].value;
  query = query + '&seats' + id + '=1';
  if (id == '1') seats1 = 1;
  if (id == '2') seats2 = 1;
  if (id == '3') seats3 = 1;
  if (id == '4') seats4 = 1;
  if (id == '5') seats5 = 1;
}

function addextras(name, amount, price, priceafterdiscount)
{
  query = query + '&extra=' + URLEncode(name) + '&extraamount=' + URLEncode(amount);
}

function addextraitem(id, amount)
{
  query = query + '&extraitemid=' + id + '&extraitemamount=' + amount;
}

function setdiscount(discountcode)
{
  query = query + '&discountcode=' + discountcode;
}

var googleanalyticsaccount = '';
function setgoogleanalytics(account)
{
  googleanalyticsaccount = account;
  query = query + '&googleanalyticsaccount=' + account;
}

function getBaseUrl()
{
	var myName = /^(.*[\/\\])external\/functions\.js(\?|$)/;
	var scripts = document.getElementsByTagName("script");
	for (var i = 0; i < scripts.length; i++) {
		var src;
		if (src = scripts[i].getAttribute("src")) {
			if (src.match(myName)) {
				return src.replace(myName, "$1");
			}
		}
	}
}

var baseurl = 'https://www.hawaiifun.org/reservation/';
if(document.location.hostname.match(/^[a-z]+$/))
{
  baseurl = getBaseUrl();
}

function availability_popup()
{
  var mode = 'reservation';
  var modefield = document.getElementById('mode');
  if(modefield != undefined) mode = modefield.value;

  if (mode == 'reservation' && activitydate == '')
  {
  	alert('Please select activity date');
  	return;
  }
  if (!checkpositivenumber(seats1) || !checkpositivenumber(seats2) || !checkpositivenumber(seats3) || !checkpositivenumber(seats4) || !checkpositivenumber(seats5) ||
      (seats1 == 0 && seats2 == 0 && seats3 == 0 && seats4 == 0 && seats5 == 0))
  {
	  alert('Please enter valid guests number(s)');
	  return;
  }

  var action = 'AVAILABILITYCHECKPAGE';
  if(mode == 'giftcertificate')
  {
    action = 'GIFTCERTIFICATECHECKEXTRAITEMS';
    query += "&mode=giftcertificate";
  }

  var d=window.open('', '_blank', 'width=500,height=200,scrollbars=yes,resizable=yes,top=100,left=100').document;
  d.open("text/html", "replace");
  d.write("<html><head>"
    + "<script type='text/javascript'>var q='"+ query +"';</script>"
    + "<script type='text/javascript' src='"+ baseurl + "common/innertext.js'></script>"
    + "<script type='text/javascript' src='"+ baseurl + "external/functions.js'></script>"
    + "<script type='text/javascript' src='"+ baseurl + 'companyservlet?action=' + action + '&' + query +"'></script>"
    + (googleanalyticsaccount == '' ? '' : "<script src='https://ssl.google-analytics.com/ga.js' type='text/javascript'></script><script type='text/javascript'>try { pageTracker = _gat._getTracker('" + googleanalyticsaccount + "'); pageTracker._setAllowLinker(true); pageTracker._setAllowHash(false); pageTracker._trackPageview(); } catch(err) {}</script>")
    + "</head><body onload='javascript:showContent();'><table width='100%' height=170><tr><td width=100% valign=center align=center><b>C h e c k i n g . . .</b></td></tr></table></body></html>");
  d.close();
}

function purchase(hotelid, room, transportationrouteid)
{
  var url = baseurl + 'companyservlet?action=EXTERNALPURCHASEPAGE&' + query + '&hotelid=' + hotelid + '&room=' + room + '&transportationrouteid=' + transportationrouteid;
  if (typeof pageTracker == 'undefined')
  {
    window.opener.location = url;
  }
  else
  {
    window.opener.location = pageTracker._getLinkerUrl(url);
  }
  window.close();
}
