function valid_email(email) {
	
	   at = email.indexOf("@");
	   dot = email.lastIndexOf(".");
	
	   if((at >= 1) && (dot > at)) {
	   	return(true);
	   } else {
	   	return(false);
	   }
	}

	function form_cookieset(form) {
   valid = 1;
      
      if((valid) && (form.title.value == '')) {
         valid = 0;
         alert("Please select your title.");
         form.title.focus();
      }
      
      if((valid) && (form.firstname.value == '')) {
         valid = 0;
         alert("Please enter your firstname.");
         form.firstname.focus();
      }

	   if((valid) && (form.surname.value == '')) {
         valid = 0;
         alert("Please enter your surname.");
         form.surname.focus();
      }

      if((valid) && (form.position.value == '')) {
         valid = 0;
         alert("Please enter your position.");
         form.position.focus();
      }
      
      if((valid) && (form.company.value == '')) {
         valid = 0;
         alert("Please enter your company name.");
         form.company.focus();
      }
      
      if((valid) && (form.address1.value == '')) {
         valid = 0;
         alert("Please enter the first line of your address.");
         form.address1.focus();
      }
      
      if((valid) && (form.town.value == '')) {
         valid = 0;
         alert("Please enter your town/city.");
         form.town.focus();
      }
      
      if((valid) && (form.postcode.value == '')) {
         valid = 0;
         alert("Please enter your postcode.");
         form.postcode.focus();
      }
      
      if((valid) && (form.country.value == '')) {
         valid = 0;
         alert("Please enter your county.");
         form.country.focus();
      }
      
      if((valid) && (form.telephone.value == '')) {
         valid = 0;
         alert("Please enter a contact telephone number.");
         form.telephone.focus();
      }
   	
   	if((valid) && (!valid_email(form.email.value))) {
         valid = 0;
         alert('Please enter a valid email address.');
         form.email.focus();
      }
      
      if(valid) {
      	form.cmd.value = "setcookie";
      	form.action = "contact.php";
         form.submit();
      }
	}
	
	function form_submit(form) {
      valid = 1;
      
      if((valid) && (form.title.value == '')) {
         valid = 0;
         alert("Please select your title.");
         form.title.focus();
      }
      
      if((valid) && (form.name.value == '')) {
         valid = 0;
         alert("Please enter your name.");
         form.name.focus();
      }
      
      if((valid) && (form.position.value == '')) {
         valid = 0;
         alert("Please enter your position.");
         form.position.focus();
      }
      
      if((valid) && (form.company.value == '')) {
         valid = 0;
         alert("Please enter your company name.");
         form.company.focus();
      }
      
      if((valid) && (form.address1.value == '')) {
         valid = 0;
         alert("Please enter the first line of your address.");
         form.address1.focus();
      }
      
      if((valid) && (form.town.value == '')) {
         valid = 0;
         alert("Please enter your town/city.");
         form.town.focus();
      }
      
      if((valid) && (form.postcode.value == '')) {
         valid = 0;
         alert("Please enter your postcode.");
         form.postcode.focus();
      }
      
      if((valid) && (form.country.value == '')) {
         valid = 0;
         alert("Please enter your county.");
         form.country.focus();
      }
      
      if((valid) && (form.telephone.value == '')) {
         valid = 0;
         alert("Please enter a contact telephone number.");
         form.telephone.focus();
      }
   	
   	if((valid) && (!valid_email(form.email.value))) {
         valid = 0;
         alert('Please enter a valid email address.');
         form.email.focus();
      }
      
      /*if((valid) && (form.distributor.checked || form.distributor2.checked || form.installer.checked || form.local.checked || form.special.checked || form.other2.checked)) {
		} else if (valid){
			valid = 0;
			alert('Please select your company type.');
		}*/
      
  
      if(valid) {
      	form.cmd.value = "send";
      	form.action = "contact_thanks.php";
         form.submit();
      }
	}

   function safeSplit(string, pattern) {
      var split_array = new Array();
      var split_x = 0;
      var start = 0;
      var end = 0;
   
      while (end < string.length) {
         end = string.indexOf(pattern, start);
         if (end == -1)
            end = string.length;
         split_array[split_x++] = string.substring(start, end);
         start = end + 1;
      }
      return(split_array);
   }
   
   function getCookieField(label) {
      var labelLen = label.length;
      var cookieLen = document.cookie.length;
      var x = 0;
      var cookieEnd;
   
      while(x < cookieLen) {
         var sub = x + labelLen;
         if (document.cookie.substring(x, sub) == label) {
            cookieEnd = document.cookie.indexOf(";", sub);
            if (cookieEnd == -1)
               cookieEnd = cookieLen;
            return(unescape(document.cookie.substring(sub + 1, cookieEnd)));
         }
         x++;
      }
      return("");
   }
   
	function cookieFind(cookie, id) {
		cookies = safeSplit(getCookieField(cookie), ",");
		found = false;
      for(x = 0; x < cookies.length; x++) {
         pair = safeSplit(cookies[x], ":");
         if(pair[0] == id)
            found = true;
      }
      
      return(found);
	}
	
   function basket(basket, id, number) {
      var cookies = safeSplit(getCookieField(basket), ",");
      var cookie = "";
      var found = false;
   
      id = "" + id;
      for(x = 0; x < cookies.length; x++) {
         pair = safeSplit(cookies[x], ":");
         if((pair[0]) && (pair[0] != ";") && (pair[0] != id))
            cookie = cookie + cookies[x] + ",";
      }
   
      if(number > 0)
         cookie += id + ":" + (number - 0);
   
      document.cookie = basket + "=" + cookie + "; path=/";
   }
   
	function remove(id) {
      basket('shop', id, 0);
    	document.forms['training'].action = "contact.php";
      document.forms['training'].submit();
	}
	
	function remove2(id) {
      basket('brochure', id, 0);
    	document.forms['training'].action = "contact.php";
      document.forms['training'].submit();
	}
	
	function remove3(id) {
      basket('download', id, 0);
    	document.forms['training'].action = "contact.php";
      document.forms['training'].submit();
	}