// JavaScript Document
// Common functions

function externalLinks() { 
if (!document.getElementsByTagName) return; 
var anchors = document.getElementsByTagName("a"); 
for (var i=0; i<anchors.length; i++) { 
	var anchor = anchors[i]; 
	if (anchor.getAttribute("href") && 
		anchor.getAttribute("rel") == "_blank") 
		anchor.target = "_blank"; 
	} 
} 

// Preload Images
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];}}
}

// Pop Up Windows
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

// Set Cookie
function setCookie() {
  var cookiexists = getCookie("Referer");
  var referer 	  = escape(document.referrer);  
  var referer     = referer.replace("http%3A//","");
  var referer2    = referer.replace("www.","");
  var myDomain    = document.domain.replace("www.","");
  var adWords	  = getURLParam("PPC");
  if (referer2.indexOf(myDomain) == 0) { referer = ""; } 
  if (referer != "" && !cookiexists) {
	  var exp = new Date()
	  var expires = exp.getTime() + (275 * 24 * 60 * 60 * 1000)  // set cookie to expire in 9 months
	  exp.setTime(expires);
	  document.cookie = "Referer=" + referer + "; expires=" + exp.toGMTString() + "; path=/";
	  var searchfor = /ppc%253dadwords/;
	  var searchstring = referer.toLowerCase();
	  var matchPos1 = searchstring.search(searchfor);
	  if (matchPos1 != -1 || adWords.toLowerCase() == "adwords") {
		  document.cookie = "Landed=AdWords" + "; expires=" + exp.toGMTString() + "; path=/";
	  } 
  }  
}

function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if ( 
		aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return unescape(strReturn);
}

function getCookie(c_name) {
  if (document.cookie.length>0) {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
  return "";
}

// Expand/Collapse text divs
function toggle(id) {
  var e = document.getElementById(id); 
  if (e.style.display == '')
  {
    e.style.display = 'none';
  } else {
    e.style.display = '';
  }
}

function toggleDiv(divid){
   if(document.getElementById(divid).style.display == 'none'){
      document.getElementById(divid).style.display = 'block';
   } else {
      document.getElementById(divid).style.display = 'none';
   }
}

function toggleDiv2(divid){
   if(document.getElementById(divid).style.display == 'none'){
      document.getElementById(divid).style.display = 'block';
      document.getElementById("F01").focus();
   } else {
      document.getElementById(divid).style.display = 'none';
   }
}


function NotifyForm_Validator(Form)
{
  if (Form.F01.value == "")
  {
    alert("Please enter your Name");
    Form.F01.focus();
    return (false);
  }
  
  if (Form.F02.value == "")
  {
    alert("Please enter your Email Address");
    Form.F02.focus();
    return (false);
  }

  if (!check_email(Form.F02.value))
  {
    alert("You appear to have entered an invalid email address");
    Form.F02.focus();
    return (false);
  }
  
  if (Form.Turing.value == "")
  {
    alert("Please enter the anti-spam validation letters in the box to the left");
    Form.Turing.focus();
    return (false);
  }
     
  return (true);
} 

function ContactForm_Validator(Form)
{
  if (Form.F01.value == "")
  {
    alert("Please enter your Name");
    Form.F01.focus();
    return (false);
  }
  
  if (Form.F02.value == "")
  {
    alert("Please enter your Email Address");
    Form.F02.focus();
    return (false);
  }

  if (!check_email(Form.F02.value))
  {
    alert("You appear to have entered an invalid email address");
    Form.F02.focus();
    return (false);
  }
  
  if (Form.F15.value == "")
  {
    alert("Please enter details of your enquiry");
    Form.F15.focus();
    return (false);
  }
    
  if (Form.Turing.value == "")
  {
    alert("Please enter the anti-spam validation letters shown in the box to the left");
    Form.Turing.focus();
    return (false);
  }
     
  return (true);
} 

function NewsletterForm_Validator(Form)
{
  if (Form.F01.value == "")
  {
    alert("Please enter your Name");
    Form.F01.focus();
    return (false);
  }
  
  if (Form.F02.value == "")
  {
    alert("Please enter your Email Address");
    Form.F02.focus();
    return (false);
  }

  if (!check_email(Form.F02.value))
  {
    alert("You appear to have entered an invalid email address");
    Form.F02.focus();
    return (false);
  }
  
  if (Form.Turing.value == "")
  {
    alert("Please enter the anti-spam validation letters shown in the box to the left");
    Form.Turing.focus();
    return (false);
  }
     
  return (true);
} 

function UnsubscribeForm_Validator(Form)
{
  if (Form.F02.value == "")
  {
    alert("Please enter your Email Address");
    Form.F02.focus();
    return (false);
  }

  if (!check_email(Form.F02.value))
  {
    alert("You appear to have entered an invalid email address");
    Form.F02.focus();
    return (false);
  }
  
  if (Form.Turing.value == "")
  {
    alert("Please enter the letters shown in the box to the left");
    Form.Turing.focus();
    return (false);
  }
     
  return (true);
} 

function check_email(e) {
	ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";

	for(i=0; i < e.length ;i++){
		if(ok.indexOf(e.charAt(i))<0){
			return (false);
		}
	}

	if (document.images) {
		re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
		re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		if (!e.match(re) && e.match(re_two)) {
			return (-1);
		}
	}
}

function addOrder(Form)
{
   alert("Thank you! This item has been added to your shopping basket. \n\nClick on 'View Shopping Basket' to complete your purchase when you have finished shopping.");	     
   document.forms[0].submit()
} 
