//public_html/js/common.js
function getSelectedTourName()
{
  sel=parent.frames[0].document.tourSelect.touID;
  return sel.options[sel.selectedIndex].text;
}

function getSelectedTourIndex()
{ 
  sel=parent.frames[0].document.tourSelect.touID;
  return sel.options[sel.selectedIndex].value;
} 

function addDays(startDate, days)
{
  return new Date(startDate.getTime() + (days*24*60*60*1000));
}

function formatDate(d)
{
  var m = d.getMonth() + 1;
  if (m < 10)
    m = "0" + m;
  var day = d.getDate();
  if (day < 10)
    day = "0" + day;
  var year = d.getYear();
  return (m + '/' + day + '/' + ((year < 10) ? '0' : '') + year);
}

function getCheckedValue(radioGroup)
{
	for (var i = 0; i < radioGroup.length; i++)
  {
    if (radioGroup[i].checked)
      return radioGroup[i].value;
  }
  return -1;
}

function inArray(theArray, theElement)
{
  for (var i = 0; i < theArray.length; i++)
    if (theArray[i] == theElement) 
      return true;
  return false;
}

defaultEmptyOK = false;

// Returns true if character c is a digit 
// (0 .. 9).
function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

// Check whether string s is empty.
function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

// isInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if all characters in string s are numbers.
//
// Accepts non-signed integers only. Does not accept floating 
// point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// By default, returns defaultEmptyOK if s is empty.
// There is an optional second argument called emptyOK.
// emptyOK is used to override for a single function call
//      the default behavior which is specified globally by
//      defaultEmptyOK.
// If emptyOK is false (or any value other than true), 
//      the function will return false if s is empty.
// If emptyOK is true, the function will return true if s is empty.
//
// EXAMPLE FUNCTION CALL:     RESULT:
// isInteger ("5")            true 
// isInteger ("")             defaultEmptyOK
// isInteger ("-5")           false
// isInteger ("", true)       true
// isInteger ("", false)      false
// isInteger ("5", false)     true
function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if ((i==0) && ((c == '-') || (c == '+')))
          continue;
        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

// isIntegerInRange (STRING s, INTEGER a, INTEGER b [, BOOLEAN emptyOK])
// 
// isIntegerInRange returns true if string s is an integer 
// within the range of integer arguments a and b, inclusive.
// 
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isIntegerInRange (s, a, b)
{   
    if (isEmpty(s)) 
       if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerInRange.arguments[1] == true);

    // Catch non-integer strings to avoid creating a NaN below,
    // which isn't available on JavaScript 1.0 for Windows.
    if (!isInteger(s, false))
    {
      return false;
    }

    // Now, explicitly change the type to integer via parseInt
    // so that the comparison code below will work both on 
    // JavaScript 1.2 (which typechecks in equality comparisons)
    // and JavaScript 1.1 and before (which doesn't).
    var num = parseInt (s);
    return ((num >= a) && (num <= b));
}

function moveSelectedOptions(from,to)
{
  for (var i=(from.options.length-1); i>=0; i--)
  {
    var o = from.options[i];
    if (o.selected)
    {
      to.options[to.options.length] = new Option( o.text, o.value, false, false);
      from.options[i] = null;
    }
  }
  from.selectedIndex = -1;
  to.selectedIndex = -1;
  sortSelect(from);
  sortSelect(to);
}

function printListBoxValues(theBox)
{
  for (var i=0;i<theBox.options.length;i++)
  {
    alert("name: " + theBox.options[i].text + "\nvalue: " + theBox.options[i].value);
  }
}

//select all items in a select box
function selectAllOptions(obj)
{
  for (var i=0; i<obj.options.length; i++)
  {
    obj.options[i].selected = true;
  }
}
  
function setCookie(name, value, expires, path, domain, secure) {
  document.cookie = name + "=" + escape(value) + 
  ((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
  ((path == null) ? "" : "; path=" + path) +
  ((domain == null) ? "" : "; domain=" + domain) +
  ((secure == null) ? "" : "; secure");
}

function setSelectedValue(sel, val)
{
  for (var i=0; i < sel.options.length; i++)
  {
    if (sel.options[i].value == val)
    {
      sel.options[i].selected=true;
      return;
    }
  }
}
  
function sortSelect(obj)
{
  var o = new Array();
  for (var i=0; i<obj.options.length; i++)
  {
    o[o.length] = new Option( obj.options[i].text, obj.options[i].value,
    obj.options[i].defaultSelected, obj.options[i].selected) ;
  }
  o.sort(
    function(a,b)
    { 
      if ((a.text+"") < (b.text+"")) { return -1; }
      if ((a.text+"") > (b.text+"")) { return 1; }
      return 0;
    } 
  );
  for (var i=0; i<o.length; i++)
  {
    obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
  }
}

//assumes format mm/dd/yyyy; or adds '20' to yy
function validateDate(d)
{
  var dateArray=d.split('/');
  if (dateArray.length != 3)
    return false;
  var m = dateArray[0];
  var day = dateArray[1];
  var y = dateArray[2];
  if (dateArray[2].length == 2)
    y = '20' + y;
  try
  {
    if ((m.length == 0) || (m.length > 2))
      return false;
    m = parseInt(m, 10);
    if ((m < 1) || (m > 12))
      return false;
    if ((day.length == 0) || (day.length > 2))
      return false;
    day1 = parseInt(day, 10);
    if ((day1 < 1) || (day1 > 31))
      return false;
    if (y.length != 4)
      return false;
    y = parseInt(y, 10);
    if ((y < 2001) || (y > 2012))
       return false;
  }
  catch (e)
  {
    return false;
  }
  return true;
}

//returns false if d1 is after d2
//assumes dates already checked for validity
function checkDateOrder(d1, d2)
{
  if (d1 == d2)
    return true;
  
  var dateArray1=d1.split('/');
  var m1 = parseInt(dateArray1[0], 10);
  var day1 = parseInt(dateArray1[1], 10);
  var y1 = parseInt(dateArray1[2], 10);

  var dateArray2=d2.split('/');
  var m2 = parseInt(dateArray2[0], 10);
  var day2= parseInt(dateArray2[1], 10);
  var y2 = parseInt(dateArray2[2], 10);
  
  if (y1 != y2)
    return (y1 < y2);
  if (m1 != m2)
    return (m1 < m2)
  return (day1 < day2);
}

//assumes format HH:MM
function validateTime(t)
{
  var timeArray = t.split(':');
  if (timeArray.length != 2)
     return false;
  var h = timeArray[0];
  var m = timeArray[1];
  try
  {
    if ((h.length < 1) || (h.length > 2))
      return false;
    h = parseInt(h, 10);
    if ((h < 0) || (h > 23))
       return false;
    if ((m.length < 1) || (m.length > 2))
      return false;
    m = parseInt(m, 10);
    if ((m < 0) || (m > 59))
       return false;
  }
  catch (e)
  {
    return false;
  }
  return true;
}

