//
// Author: Bradley J Bristow-Stagg & Coral Johnson
// Copyright: 2000
// The Insect Company Web Site
//

// Function to do onMouseOver/Off event
function changeImg(imgName, imgSrc){
 imgName.src = imgSrc;  
}

// Change to a random image
function changeImgRand(imgName){
  document.images[imgName].src = "Graphics/insect" + generateRand(1,9) + ".gif";
}

// Generate a random number between x and y
function generateRand(x, y) {
  var range = y-x+1;
  var l = ("" + range).length;
  var num = (Math.floor(Math.random() * Math.pow(10,l)) % range) + parseInt(x);
  return num;
}

// Javascript functions from imageready
function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}



// This section deals with FORM validation for the Enquiries section of InsCoWeb
// This script and many more available at The JavaScript Source!!
// via the Internet  U R L :  http://www.compfund.com/javascript/
// Modified by: Bradley J Bristow-Stagg
// Date: 15th February 2000

// retrieve a cookie from users machine
function getCookie(name){
  var cname = name + "=";
  var dc = document.cookie;
  
  if (dc.length > 0) {              
    begin = dc.indexOf(cname);       
  
    if (begin != -1) {           
        begin += cname.length;       
  
        end = dc.indexOf(";", begin);
        if (end == -1) end = dc.length;

        return unescape(dc.substring(begin, end));
    } 
  }

  return null;
}


function setCookie(name, value, expires) {
  document.cookie = name + "=" + escape(value) + ((expires != null) ? ";expires=" + expires.toGMTString() : "") + "; path=/";
}


function setName() {
  var expdate = new Date ();
  expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000 * 365));
  var prompt=i = document.Mail.name.value;
  setCookie("name", i, expdate);
}

// Get the users name from the cookie
function getName() {
  if(getCookie("name") != null) {
    document.Mail.Name.value = getCookie("emailname");
  }
  else {
    document.Mail.name.value ="";
  }
}

// Get the Browser name and date that the form was sent
function getInfo() {
  var now= new Date();
  document.Mail.Info.value = "Browser: " + navigator.userAgent;
  document.Mail.Info.value += "Date:" + now;
}

// Check to see if they have sent a post before from cookie
function checkMultiple() {
  if (getCookie("emailsent") == 'true') return true;
  else return false;
}

// Process the form on completion
function process() {
  setCookie("emailsent", "true");
  with (document.Mail) {
  action = "mailto:enquiries@insectcompany.com?subject=InsCoWeb - Enquiries";}
}

// Check the form to make sure that all of the relevant info is there
function formCheck() {
  var passed = false;
  with (document.Mail) {
    if (Name.value == "") {
      alert("Please inlcude your name.");
      Name.focus();
    }
    else if (eMail.value == "") {
      alert("Please inlcude your eMail address.");
      eMail.focus();
    }
    else if (enquiry.value == "" && comments.value =="") {
      if (enquiry.value == ""){
        alert("Please inlcude your enquiry.");
        enquiry.focus();
      }
      else{
        alert("Please inlcude your Comments.");
        comments.focus();
      }
    }
    else if (checkMultiple()) {
      if (confirm("You just sent an e-mail from this form, are you sure you want to send another?")) {
        process(); passed = true;
      }
    }
    else {
      process();
      passed = true;
    }
  }
  return passed;
}
