var currentGalleryImage = 0;



function fade(id, opacityStart, opacityEnd, millisec)
{ 
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacityStart > opacityEnd)
	{ 
      for(i = opacityStart; i >= opacityEnd; i--)
	  { 
        setTimeout("changeOpacity(" + i + ",'" + id + "')",(timer * speed)); 
        timer++; 
      } 
    }
	else if(opacityStart < opacityEnd)
	{ 
      for(i = opacityStart; i <= opacityEnd; i++) 
      { 
        setTimeout("changeOpacity(" + i + ",'" + id + "')",(timer * speed)); 
        timer++; 
      } 
    } 
} 

//change the opacity for different browsers 
function changeOpacity(newOpacity, id)
{ 
    var object = document.getElementById(id).style; 
    object.opacity = (newOpacity / 100); 
    object.MozOpacity = (newOpacity / 100); 
    object.KhtmlOpacity = (newOpacity / 100); 
    object.filter = "alpha(opacity=" + newOpacity + ")";
}

function fadeOut(divToFadeOut)
{
  fade(divToFadeOut,100,0,200);
}

function fadeIn(divToFadeIn)
{
  fade(divToFadeIn,0,100,200);
}

function changeGalleryImage(id)
{
  imageURL  = locationOfGalleryImages+id+".jpg";
  document.getElementById("galleryMainImage").src=imageURL;
  currentGalleryImage = id;
}

function showGalleryImage(id)
{
  if (currentGalleryImage != id)
  {
    fadeOut("galleryMainImage");
    setTimeout("changeGalleryImage("+id+")",200);
    setTimeout("fadeIn('galleryMainImage')",450);
  }
}

function initialiseGallery(firstImage)
{
  fadeOut('galleryLoader',400);
  document.getElementById("galleryLoader").style.display="none";
  setTimeout("fadeIn('galleryMainImage',400)",500);
  setTimeout("fadeIn('galleryThumbnailsContainer',400)",700);
}

function checkContactForm()
{
  if (document.contactForm.contactName.value == "")
  {
    alert ('Please enter your name.');
    document.contactForm.contactName.focus();
	return false;
  }
  if (document.contactForm.contactEmail.value == "")
  {
	if (document.contactForm.contactPhone.value == "")
	{
	  alert ('Please enter your contact number or your Email address.');
      //document.contactForm.contactEmail.focus();
	  return false;
	}
  }
  else if (checkEmailAddress(document.contactForm.contactEmail.value)==false)
  {
    alert ('Please enter a valid Email address, or leave blank if you wish to be contacted by phone.');
    document.contactForm.contactEmail.focus();
	return false;
  }
  if (document.contactForm.contactMessage.value == "")
  {
    alert ('Please tell us how we can help you.');
    document.contactForm.contactMessage.focus();
	return false;
  }
    return true;
}

function checkEmailAddress(emailAddress)
{
  var allowed = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.@-_";
  var emailLength = emailAddress.length;
  var atSignPosition = 0;
  var finalDotPosition = 0;
  var numberOfDots = 0;
  var numberOfAtSigns = 0;

  if (emailLength < 5)
  {
    return false;
  }

  for (i = 0; i < emailLength ;i ++)
  {
    if (allowed.indexOf(emailAddress.charAt(i))<0)
    {
      return (false);
    }
    if ((emailAddress.charAt(i)) == "@")
    {
      numberOfAtSigns ++;
      atSignPosition = i;
    }
    else
    {
      if ((emailAddress.charAt(i)) == ".")
      {
        numberOfDots ++;
        finalDotPosition = i;
      }
    }
  }
  if (numberOfDots == 0)
  {
    return (false);
  }
  if (numberOfAtSigns != 1)
  {
    return (false);
  }
  if (atSignPosition > finalDotPosition)
  {
    return (false);
  }
  if (finalDotPosition == (emailLength-1))
  {
    return (false);
  }
  return (true);
}