﻿// Remove leading and trailing spaces.
function Trim(string) 
{
    while (string.substring(0,1) == ' ')
    {
        string = string.substring(1, string.length);
    }
    while (string.substring(string.length-1, string.length) == ' ')
    {
        string = string.substring(0,string.length-1);
    }
    return string;
    
}// function Trim(string) 

// Determine if a number is an integer.
function IsInt(sText)
{
   var ValidChars = "0123456789-";
   var IsNumber = true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
      {
         IsNumber = false;
      }
   }
   return IsNumber;
   
}// function IsInt(sText)

// Determine if a number is an integer.
function IsIntPositive(sText)
{
   var ValidChars = "0123456789";
   var IsNumber = true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
      {
         IsNumber = false;
      }
   }
   return IsNumber;
   
}// function IsInt(sText)

// Validates the input fields on the contact form.
function ValidateFormContact()
{
    // Check name.
    var name = document.getElementById('txtName');
    if (Trim(name.value).length == 0)
    {
        alert('Please enter your name.');
        name.focus();
        return false;
    }

    // Get the preferred reply type.
    var byPhone = 0;
    var replyType = document.getElementById('radPhone');
    if (replyType.checked)
    {
        byPhone = 1;
    }
    
    if (byPhone == 1)
    {
        // Check phone number.
        var phoneNumber = document.getElementById('txtPhone');
            
        if (Trim(phoneNumber.value).length == 0)
        {
            alert('Please enter your telephone number.');
            phoneNumber.focus();
            return false;
        }
    }
    else
    {
        // Check email.
        var email = document.getElementById('txtEmail');
            
        if (Trim(email.value).length == 0)
        {
            alert('Please enter your email address.');
            email.focus();
            return false;
        }
        
    }        

    // Check email.
    var comment = document.getElementById('txtComment');
        
    if (Trim(comment.value).length == 0)
    {
        alert('Please enter your comment or enquiry.');
        comment.focus();
        return false;
    }
    
}// function ValidateFormContact()

// Validates the input fields on the product form.
function ValidateFormProduct()
{
    // Check name.
    var name = document.getElementById('txtName');
    if (Trim(name.value).length == 0)
    {
        alert('Please enter your name.');
        name.focus();
        return false;
    }

    // Get the preferred reply type.
    var byPhone = 0;
    var replyType = document.getElementById('radPhone');
    if (replyType.checked)
    {
        byPhone = 1;
    }
    
    if (byPhone == 1)
    {
        // Check phone number.
        var phoneNumber = document.getElementById('txtPhone');
            
        if (Trim(phoneNumber.value).length == 0)
        {
            alert('Please enter your telephone number.');
            phoneNumber.focus();
            return false;
        }
    }
    else
    {
        // Check email.
        var email = document.getElementById('txtEmail');
            
        if (Trim(email.value).length == 0)
        {
            alert('Please enter your email address.');
            email.focus();
            return false;
        }
        
    }        

    // Check no. of adults is either blank or numeric.
    var adults = document.getElementById('txtNoAdults');
    
    if (Trim(adults.value).length > 0)
    {
        if (!(IsIntPositive(adults.value)))
        {
            alert('Please enter a numeric value for number of adults or leave blank.');
            adults.focus();
            return false;
        }
    
    }// adults has a value in it

    // Check no. of children is either blank or numeric.
    var children = document.getElementById('txtNoChildren');
    
    if (Trim(children.value).length > 0)
    {
        if (!(IsIntPositive(children.value)))
        {
            alert('Please enter a numeric value for number of children or leave blank.');
            children.focus();
            return false;
        }
    
    }// children has a value in it

}// function ValidateFormProduct()

// Show the google map.
function ShowGoogleMap()
{
    var gmapDiv = document.getElementById("map");
    
    if (gmapDiv == null)
    {
        return false;
    }
    
    if (GBrowserIsCompatible()) 
    {
        var map = new GMap2(gmapDiv);
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(-17.898, 146.06), 11);
        var point = new GLatLng(-17.8978, 146.0922);
        map.addOverlay(new GMarker(point));
        return true;
    }
    else
    {
        alert('Web browser is not compatible with Google Maps.');
        return false;
    }        

}// function ShowGoogleMap()

function UnloadGoogleMap()
{
    var gmapDiv = document.getElementById("map");
    
    if (gmapDiv == null)
    {
        return true;
    }
    else
    {
        GUnload();
        return true;
    }

}

function OpenNewWindow(url) 
{ 

    link = window.open(url,"Link","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=600,height=500,left=40,top=50"); 

} 

// Checks for the enter button and automatically clicks a default button.
function CheckEnter(buttontoclick)
{
    if (window.event.keyCode == 13)
    {
        var button = document.getElementById(buttontoclick);
        if (button)
            button.click();
        return false;
    }
    return true;

}// function CheckEnter()
