// Core javascripts - useful functions and such

// Some global variables first
var Template_name = 'default';
var Template_path = 'tpl/' + Template_name + '/';

// Image pre-loader - from Dreamweaver hence it's ugly as hell
// -----------------------------------------------------------------------------
function MM_preloadImages() 
{ //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// Page preloader - put all page-specific stuff in here
// -----------------------------------------------------------------------------
function PageLoader(pagename) 
{
    
  if (pagename == 'p1') {
    
    MM_preloadImages( Template_path + 'gfx/p1-block-wd-bg.jpg',
                      Template_path + 'gfx/p1-block-ld-bg.jpg',
                      Template_path + 'gfx/p1-block-cs-bg.jpg',
                      Template_path + 'gfx/p1-block-wd-bg.jpg',
                      Template_path + 'gfx/p1-menu-wd-top.jpg',
                      Template_path + 'gfx/p1-menu-ld-top.jpg',
                      Template_path + 'gfx/p1-menu-cs-top.jpg',
                      Template_path + 'gfx/p1-menu-wd-bg-a.gif',
                      Template_path + 'gfx/p1-menu-ld-bg-a.gif',
                      Template_path + 'gfx/p1-menu-cs-bg-a.gif'
    );
    

    
  }
}

// #############################################################################
// CONTACT FORM 
// #############################################################################

  // Quick function to restored the buttons on the Contact form if there is an error
  function RestoreButtonsCForm()
  {
  var RestoreButtons = '<input type="submit" class="input-submit" value="Submit form &rarr;" name="submit">&nbsp;&nbsp;or&nbsp;&nbsp;<a href="#" onClick="javascript:document.ContactForm.reset();return false">Reset everything</a>';
  document.getElementById('SubmitTag').innerHTML=RestoreButtons;
  }

// -----------------------------------------------------------------------------   
  // Function to check if the e-mail address is valid
  function CheckContactEmail() {
    email = document.ContactForm.EmailAddress.value
    AtPos = email.indexOf("@")
    StopPos = email.lastIndexOf(".")
      
    if (email == "") {
    return false;
    }
    
    if (AtPos == -1 || StopPos == -1) {
    return false;
    }
    
    if (StopPos < AtPos) {
    return false;
    }
    
    if (StopPos - AtPos == 1) {
    return false;
    } 
    
    return true;
  }

// ----------------------------------------------------------------------------- 
  // Function to validate the contact form
  function validateContactForm()
  {
    
    document.getElementById('SubmitTag').innerHTML='Processing...';
    if (document.ContactForm.YourName.value == '')
  	{
  		// No name given
  		alert('ERROR: Please enter your name.');
      RestoreButtonsCForm();
      return false;
  	}
  	else if (CheckContactEmail() == false) {
      alert('ERROR: Please enter a valid e-mail address');
      RestoreButtonsCForm();
      return false;
    }
    else if ((document.ContactForm.Comments.value == '') || (document.ContactForm.Comments.value == 'Please provide some detail to help us respond to your enquiry.'))
  	{
      alert('ERROR: Please enter something into the comments box');
      RestoreButtonsCForm();
      return false;
    }
    
  	// If the script gets this far through all of your fields
  	// without problems, it's ok and you can submit the form
  
  	return true;
  }
  

// TESTING AND DEBUGGING AREA
// -----------------------------------------------------------------------------


