/* Library */

String.prototype.trim = function()
{
  return this.replace(/^\s*|\s*$/g, '');
}

function Email()
{
  this.domain = '';
  this.name = '';

  this.check = function(address)
  {
    var validcharacters = 'abcdefghijklmnopqrstuvwxyz0123456789-_.@';
    var domain, i, position, success;
    success = false;
    if (address != null)
    {
      address = address.toLowerCase();
      position = address.indexOf('@');
      if (position != -1)
      {
        if (address.indexOf('@', position + 1) == -1)
        {
          if (address.substr(position + 1).indexOf('.') != -1)
          {
            for (i = 0; i < address.length; i++)
            {
              if (validcharacters.match(address.charAt(i)) == null)
                break;
            }
            if (i == address.length)
            {
              domain = address.substr(position + 1);
              if ((address.substring(0, position).length > 1) && (domain.length > 4))
              {
                if (domain.length > (domain.indexOf('.') + 2))
                {
                  this.domain = domain;
                  this.name = address.substring(0, position);
                  success = true;
                }
              }
            }
          }
        }
      }
    }
    return success;
  }
}

function changeImage(imageobj, image)
{
  imageobj.src = image;
  return true;
}

function preloadImage(image)
{
  var imageobj = new Image();
  imageobj.src = image;
}

function showPopup(html, left, top, width, height, resizable, scrollbars)
{
  var windowobj;
  if ((html.substr(html.length - 4, 4).toLowerCase() == '.htm') || (html.substr(html.length - 5, 5).toLowerCase() == '.html'))
    windowobj = window.open(html, '', 'left=' + left + ',top=' + top + ',width=' + width + ',height=' + height + ',resizable=' + ((resizable == true) ? 'yes' : 'no') + ',scrollbars=' + ((scrollbars == true) ? 'yes' : 'no'));
  else
  {
    windowobj = window.open('', '', 'left=' + left + ',top=' + top + ',width=' + width + ',height=' + height + ',resizable=' + ((resizable == true) ? 'yes' : 'no') + ',scrollbars=' + ((scrollbars == true) ? 'yes' : 'no'));
    windowobj.document.open();
    windowobj.document.write(html);
    windowobj.document.close();
  }	
  windowobj.focus();
}

function showPicture(title, css, picture, width, height)
{
  var html;
  html = '<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n';
  html += '<html>\n';
  html += '  <head>\n';
  html += '    <title>' + title + '</title>\n';
  html += '    <link rel=\"stylesheet\" href=\"' + css + '\" type=\"text/css\">\n';
  html += '  </head>\n';
  html += '  <body style=\"margin: 0px\">\n';
  html += '    <img src=\"' + picture + '\" alt=\"\" border=\"0\" width=\"' + width + '\" height=\"' + height + '\">\n';
  html += '  </body>\n';
  html += '</html>\n';
  if ((width > (screen.availWidth - 12)) || (height > (screen.availHeight - 31)))
    showPopup(html, 0, 0, width, height, true, true);
  else
    showPopup(html, (screen.availWidth - width) / 2, (screen.availHeight - height) / 2, width, height, false, false);
}

/* Routines */

var divobj, t;

function preloadImages()
{
  if (document.images != null)
  {
    preloadImage('images/home.gif');
    preloadImage('images/homed.gif');
    preloadImage('images/address.gif');
    preloadImage('images/addressd.gif');
    preloadImage('images/menu.gif');
    preloadImage('images/menud.gif');
    preloadImage('images/humor.gif');
    preloadImage('images/humord.gif');
    preloadImage('images/pictures.gif');
    preloadImage('images/picturesd.gif');
    preloadImage('images/reservation.gif');
    preloadImage('images/reservationd.gif');
  }
}

function setInputColor(inputobj, focus)
{
  if (focus == true)
    inputobj.style.background = '#ffaaaa';
  else
    inputobj.style.background = '#ffdddd';
}

function check(text)
{
  if (confirm(text) == true)
    window.location = "reservation.html";
  else
    window.location = "mailto:info@dewaterput.com";
}

function loadingMap()
{
  var divobj2;
  divobj2 = document.createElement('div');
  divobj2.innerHTML = t;
  divobj2.style.color = '#777777';
  divobj2.style.fontWeight = 'bold';
  divobj.insertBefore(divobj2, divobj.firstChild);
}

function loadMap(text, map, satellite, hybrid, browser)
{
  var glatlngobj, gmap2obj;
  divobj = document.getElementById('map');
  if (GBrowserIsCompatible() == true)
  {
    gmap2obj = new GMap2(divobj);
    t = text;
    GEvent.addListener(gmap2obj, 'load', loadingMap);
    gmap2obj.enableScrollWheelZoom();
    gmap2obj.setCenter(new GLatLng(51.26924, 3.26090), 13);
    gmap2obj.addOverlay(new GMarker(gmap2obj.getCenter()));
    gmap2obj.addControl(new GLargeMapControl());
    gmap2obj.addControl(new GMapTypeControl());
    gmap2obj.addControl(new GScaleControl());
    G_NORMAL_MAP.getName = function() { return map; }
    G_SATELLITE_MAP.getName = function() { return satellite; }
    G_HYBRID_MAP.getName = function() { return hybrid; }
  }
  else
    window.alert(browser);
}

function showMap(html, width, height)
{
  showPopup(html, (screen.availWidth - width) / 2, (screen.availHeight - height) / 2, width, height, false, false);
}

function isValidReservation(warning, emailwarning)
{
  var emailobj = new Email();
  var str;
  str = '';
  if (document.reservation.fullname.value.trim().length == 0)
  {
    document.reservation.fullname.focus();
    str = warning;
  }
  else if (document.reservation.phone.value.trim().length == 0)
  {
    document.reservation.phone.focus();
    str = warning;
  }
  else if (emailobj.check(document.reservation.email.value) == false)
  {
    document.reservation.email.focus();
    str = emailwarning;
  }
  else if (document.reservation.message.value.trim().length == 0)
  {
    document.reservation.message.focus();
    str = warning;
  }
  if (str.length > 0)
  {
    window.alert(str);
    return false;
  }
  else
    return true;
}

function submitReservation(warning, emailwarning)
{
  if (isValidReservation(warning, emailwarning) == true)
    document.reservation.submit();
}
