// 20081003 MJS file to store all email addresses in that are used 
//on a website to protect address and also make one location to 
//update addresses
// 20081006 MJS sales address updated from sales01 to sales
// 20100225 MJS added antiSpam()

var support = 'support';
var sales = 'sales';

var domain = 'dispatching.co.nz';

var engineVersion = '4.3.0.0';

var supportEmail = support + '@' + domain;
var salesEmail = sales + '@' + domain;

function emailSupport(subject)
{
	 window.location.href=("mailto:" + supportEmail + "?subject=" + subject);
}

function emailSales(subject)
{
	 window.location.href=("mailto:" + salesEmail + "?subject=" + subject);
}

function showSupportEmail(subject)
{
	document.open();
	document.write('<a href="mailto:' + supportEmail + "?subject=" + subject + '">' + supportEmail + '</a>');
	document.close();
}

function showSalesEmail(subject)
{
	document.open();
	document.write('<a href="mailto:' + salesEmail + "?subject=" + subject + '">' + salesEmail + '</a>');
	document.close();
}

function getSupportEmail()
{
	return 'mailto:' + supportEmail
}

function getSalesEmail()
{
	return 'mailto:' + salesEmail
}

function justSupportEmail()
{
	return 	supportEmail
}

function justSalesEmail()
{
	return 	salesEmail
}

function getSubmittedRedirect()
{
	return 'http://' + document.domain + '/contact-submitted.html'	
}

function getContactSource()
{
	return document.domain + ' Contact Form'
}

function getDownloadPage()
{
	return 'http://' + document.domain + '/software-downloads.html'	
}

function getProductSource()
{
	return 'DG - ' + document.domain
}

function getDownloadSource()
{
	return document.domain + ' Download Form'
}

function queryString(parameter) { 
  var loc = location.search.substring(1, location.search.length);
  var param_value = false;
  var params = loc.split("&");
  for (i=0; i<params.length;i++) {
      param_name = params[i].substring(0,params[i].indexOf('='));
      if (param_name == parameter) {
          param_value = params[i].substring(params[i].indexOf('=')+1)
      }
  }
  if (param_value) {
      return param_value;
  }
  else {
      return " -- no parameters --"; //Here determine return if no parameter is found
  }
}

var requestsubmitted = false;
function validate_contactform(theForm) {

	if(requestsubmitted == true)
	{
	   alert("In progress. Please wait.");
	   return false;
	}
	if (theForm.first.value == "")
	{
		alert("Please enter a first name.");
		theForm.first.focus();
		return false;
	}

	if (theForm.last.value == "")
	{
		alert("Please enter a last name.");
		theForm.last.focus();
		return false;
	}
	
	if ((theForm.email.value==null)||(theForm.email.value==""))
	{
		alert("Please enter an email address.");
		theForm.email.focus();
		return false;
	}
	
	if (echeck(theForm.email.value)==false)
	{
		theForm.email.value=""
		theForm.email.focus();
		return false;
	}

	if (theForm.telephone.value == "")
	{
		alert("Please enter a telephone number.");
		theForm.telephone.focus();
		return false;
	}
	
	if (theForm.message.value == "")
	{
		alert("Please enter a question.");
		theForm.message.focus();
		return false;
	}
	
	//set redirect
	theForm.redirect.value=getSubmittedRedirect();
	
	//set source
	theForm.source.value=getContactSource();
	
	requestsubmitted = true;
	return true;	
}

/* Email Validation Script */

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Please enter a valid email address.")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Please enter a valid email address.")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Please enter a valid email address.")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Please enter a valid email address.")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Please enter a valid email address.")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Please enter a valid email address.")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Please enter a valid email address.")
		    return false
		 }

 		 return true					
	}
	
function validate_downloadform(theForm) {

	if(requestsubmitted == true)
	{
	   alert("In progress. Please wait.");
	   return false;
	}
	if (theForm.first.value == "")
	{
		alert("Please enter a first name.");
		theForm.first.focus();
		return false;
	}

	if (theForm.last.value == "")
	{
		alert("Please enter a last name.");
		theForm.last.focus();
		return false;
	}
	
	if ((theForm.email.value==null)||(theForm.email.value==""))
	{
		alert("Please enter an email address.");
		theForm.email.focus();
		return false;
	}
	
	if (echeck(theForm.email.value)==false)
	{
		theForm.email.value=""
		theForm.email.focus();
		return false;
	}

	if (theForm.telephone.value == "")
	{
		alert("Please enter a telephone number.");
		theForm.telephone.focus();
		return false;
	}
	
	//set redirect
	theForm.redirect.value=getSubmittedRedirect();
	
	//set source
	theForm.source.value=getDownloadSource();
	
	//set software version
	theForm.version.value=engineVersion;
	
	//set download page
	theForm.download.value=getDownloadPage();
	
	//set product
	theForm.product.value=getProductSource();
	
	requestsubmitted = true;
	return true;	
}

// allows backspace,space,(,),+,-,0-9
function isTelephoneNumber(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode
    if ((charCode == 8  || 
		 charCode == 32 || 
		 charCode == 40 || 
		 charCode == 41 || 
		 charCode == 43 || 
		 charCode == 45) || 
		(charCode >= 48 && charCode <= 57))
    	{
		return true;
		}
    return false;
}

// put a timer on the contacts to stop bot spamming
var antiSpam = function() {        
		if (document.getElementById("antiSpam")) { 
			a = document.getElementById("antiSpam");
			if (isNaN(a.value) == true) {
				a.value = 0;
			} else {
				a.value = parseInt(a.value) + 1;   
			}  
		}   
		setTimeout("antiSpam()", 1000);
	}
antiSpam();

