function CheckPostCode(test)
{
	size = test.length;
	test = test.toUpperCase();

	while (test.slice(0,1) == " ")
	{

		test = test.substr(1,size-1);
		size = test.length
	}

	while(test.slice(size-1,size)== " ")
	{

		test = test.substr(0,size-1);
		size = test.length
	}

	if (size < 6 || size > 8) 
	{ 
		return false; 
	}

	if (!(isNaN(test.charAt(0)))) 
	{

		return false; 
	}

	if (isNaN(test.charAt(size-3))) 
	{ 
		return false; 
	}

	if (!(isNaN(test.charAt(size-2)))) 
	{ 
		return false;
	}

	if (!(isNaN(test.charAt(size-1)))) 
	{ 
		return false; 
	}

	if (!(test.charAt(size-4) == " ")) 
	{

		return false; 
	}

	count1 = test.indexOf(" ");
	count2 = test.lastIndexOf(" ");

	if (count1 != count2) 
	{ 
		return false; 
	}

	return true;
}

function ValidEmail(field)
{
	var EmailOk  = true;
	var AtSym    = field.value.indexOf('@');
	var Period   = field.value.lastIndexOf('.');
	var Space    = field.value.indexOf(' ');
	var Length   = field.value.length - 1;   // Array is from 0 to length-1

	if ((AtSym < 1) ||                     // '@' cannot be in first position
	(Period <= AtSym+1) ||             // Must be atleast one valid char btwn '@' and '.'
	(Period == Length ) ||             // Must be atleast one valid char after '.'
	(Space  != -1))                    // No empty spaces permitted
	{
		EmailOk = false;
	}
	return EmailOk;
}

function numbersonly(myfield, e)
{
  var key;
  var keychar;

  if (window.event)
     key = window.event.keyCode;
  else if (e)
     key = e.which;
  else
     return true;

  keychar = String.fromCharCode(key);

  // control keys
  if ((key==null) || (key==0) || (key==8) ||
      (key==9) || (key==13) || (key==27) )
  {
    return true;
  }
  else if ((("0123456789").indexOf(keychar) > -1)) // numbers
  {
    return true;
  }
  else
  {
    return false;
  }
}

function textonly(myfield, e, dec)
{
  var key;
  var keychar;

  if (window.event)
     key = window.event.keyCode;
  else if (e)
     key = e.which;
  else
     return true;

  keychar = String.fromCharCode(key);

  // control keys
  if ((key==null) || (key==0) || (key==8) ||
      (key==9) || (key==13) || (key==27) )
  {
    return true;
  }
  else if ((("0123456789").indexOf(keychar) > -1)) // numbers
  {
    return false;
  }
  else
  {
    return true;
  }
}

function removeSpaces(obj)
{
	var tstring = "";		//string to hold object value
	string = obj.value;
	splitstring = string.split(" ");		//Split string where there is a space

	for(i = 0; i < splitstring.length; i++)	//loop through the string and remove all spaces
	{
		tstring += splitstring[i];
	}

	obj.value=tstring;

	return true;
}

function removeDots(obj)
{
	var tstring = "";		//string to hold object value
	string = obj.value;
	splitstring = string.split(".");		//Split string where there is a dot

	for(i = 0; i < splitstring.length; i++)	//loop through the string and remove all spaces
	{
		tstring += splitstring[i];
	}

	obj.value=tstring;

	return true;
}

function empty(field)
{
	if(field.value.length==0)
		return true;
	else
		return false;
}

function validateContactForm()
{
	var f=document.contactfrm;
	
	msg = "The following fields are required:\n";
	msg2= "";
	
	if(empty(f.txtfirstname))
		msg2+="\n - First name";
		
	if(empty(f.txtsurname))
		msg2+="\n - Surname";
		
	if(empty(f.txtemail))
		msg2+="\n - Email Address";
		
	if(empty(f.txtenquiry))
		msg2+="\n - Enquiry";
		
	if(msg2 != "")
		alert(msg2);
	else
		f.submit();

}

function checkEnter(obj,e)
{ //e is event object passed from function invocation
	var characterCode;// literal character code will be stored in this variable

	if(e && e.which)
	{
		//if which property of event object is supported (NN4)
		e = e
		characterCode = e.which //character code is contained in NN4's which property
	}
	else
	{
		e = event
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}

	if(characterCode == 13)
	{ //if generated character code is equal to ascii 13 (if enter key)
		if((obj.value % 4)!=0)
		{
			alert("Product quantity must be in multiples of 4");
			return false
		}
		else
		{
			var tmp = obj.form;
			tmp.submit();
		}
	}
	else
	{
		return true
	}
}

function NewTradeCustomerForm()
{
	window.open("uktrade_request6f03.html?type=new",null,"toolbars=0, width=450, height=510");
}

function ExistingTradeCustomerForm()
{
	window.open("uktrade_request5c2d.html?type=existing",null,"toolbars=0, width=450, height=540");
}

function TradeResellerForm()
{
	window.open("uktrade_request12ee.html?type=reseller",null,"toolbars=0, width=450, height=540");
}

function submitTradeRequest()
{
	var d = document.uktrade_request;
	msg="The following fields are required:\n";
	msg2="";
	
	if(empty(d.txtcompname))
		msg2+="\n - Company Name";
		
	if(empty(d.txtname))
		msg2+="\n - Name";
		
	if(empty(d.txtemail))
		msg2+="\n - Email";
		
	if(!empty(d.txtemail) && !ValidEmail(d.txtemail))
		msg2+="\n - Invalid Email Address";
		
	if(empty(d.txtphone))
		msg2+="\n - Phone";
		
	if(empty(d.txtpostaddr))
		msg2+="\n - Postal Address";
		
	if(empty(d.txtcomments))
		msg2+="\n - Comments / Questions";
		
	if(msg2!="")
		alert(msg+msg2);
	else	
		d.submit();
}

function submitEmergencyRequest()
{
	var d = document.emergency_request;
	msg="The following fields are required:\n";
	msg2="";
	
	if(empty(d.txtcompname))
		msg2+="\n - Authority Name";
		
	if(empty(d.txtname))
		msg2+="\n - Name";
		
	if(empty(d.txtemail))
		msg2+="\n - Email";
		
	if(!empty(d.txtemail) && !ValidEmail(d.txtemail))
		msg2+="\n - Invalid Email Address";
		
	if(empty(d.txtphone))
		msg2+="\n - Phone";
		
	if(empty(d.txtpostaddr))
		msg2+="\n - Postal Address";
		
	if(empty(d.txtcomments))
		msg2+="\n - Comments / Questions";
		
	if(msg2!="")
		alert(msg+msg2);
	else	
		d.submit();
}

function NewEmergencyCustomerForm()
{
	window.open("uk_emergency_request6f03.html?type=new",null,"toolbars=0, width=450, height=510");
}

function ExistingEmergencyCustomerForm()
{
	window.open("uk_emergency_request5c2d.html?type=existing",null,"toolbars=0, width=450, height=540");
}

function submitOverseasRequest()
{
	var d = document.overseas_request;
	msg="The following fields are required:\n";
	msg2="";
	
	if(empty(d.txtcompname))
		msg2+="\n - Company Name";
		
	if(empty(d.txtname))
		msg2+="\n - Name";
		
	if(empty(d.txtemail))
		msg2+="\n - Email";
		
	if(!empty(d.txtemail) && !ValidEmail(d.txtemail))
		msg2+="\n - Invalid Email Address";
		
	if(empty(d.txtphone))
		msg2+="\n - Phone";
		
	if(empty(d.txtpostaddr))
		msg2+="\n - Postal Address";
		
	if(empty(d.txtcomments))
		msg2+="\n - Comments / Questions";
		
	if(msg2!="")
		alert(msg+msg2);
	else	
		d.submit();
}

function OverseasInfoRequest()
{
	window.open("overseas_request.html",null,"toolbars=0, width=450, height=510");
}

function increaseQuantity()
{
	var d = document.basketfrm;

	d.shop_quantity.value = (parseInt(d.shop_quantity.value) + 1);
	if(d.shop_quantity.value == "NaN")
	{
		d.shop_quantity.value = "0";
	}
}

function decreaseQuantity()
{
	var d = document.basketfrm;
	
	if(parseInt(d.shop_quantity.value)=="1")
		return;

	d.shop_quantity.value = (parseInt(d.shop_quantity.value) - 1);
	if(d.shop_quantity.value == "NaN")
	{
		d.shop_quantity.value = "0";
	}
}

function RequestQuotation()
{
	window.open("request_quote.html",null,"toolbars=0, width=500, height=450");
}

function CheckQuoteTotal()
{
	var total = document.getElementById("total_req");
	var q_ar = document.quoteform.quote_product_quantity;
	var but=document.getElementById("conbtn");
	
	if(!q_ar)
		return;
	
	if(!total)
		return;
		
	var theTotal = 0;
		
	for(i=0;i<q_ar.length;i++)
	{
		var val = parseInt(q_ar[i].value);
		
		if(isNaN(val))
			continue;
			
		theTotal += val;
	}
	
	if(parseInt(theTotal) % 4 != 0 || parseInt(theTotal)==0)
	{
		col = "<font style='color:#FF0000'>";
		colend="</font>";
		total.innerHTML = "<B>"+col+theTotal+colend+"</B>";
		return false;
	}
	else
	{
		col = "";
		colend="";
		total.innerHTML = "<B>"+col+theTotal+colend+"</B>";
		
		return true;
	}
}

function CheckQuote()
{
	if(CheckQuoteTotal())
	{
		if(confirm("Are you happy with your quote request?"))
			document.quoteform.submit();
	}
	else
		alert("Total products must be a multiple of four to proceed");
}

function checkQuoteEnter(obj,e)
{ //e is event object passed from function invocation
	var characterCode;// literal character code will be stored in this variable

	if(e && e.which)
	{
		//if which property of event object is supported (NN4)
		e = e
		characterCode = e.which //character code is contained in NN4's which property
	}
	else
	{
		e = event
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}

	if(characterCode == 13)
	{ //if generated character code is equal to ascii 13 (if enter key)
		CheckQuoteTotal();
		return false;
	}
	else
	{
		return true
	}
}

function submitQuoteRequest2()
{
	var d = document.quoteform2;
	msg="The following fields are required:\n";
	msg2="";

	if(empty(d.txtcompname))
		msg2+="\n - Company Name";

	if(empty(d.txtname))
		msg2+="\n - Name";

	if(empty(d.txtemail))
		msg2+="\n - Email";

	if(!empty(d.txtemail) && !ValidEmail(d.txtemail))
		msg2+="\n - Invalid Email Address";

	if(empty(d.txtphone))
		msg2+="\n - Phone";

	if(empty(d.txtpostaddr))
		msg2+="\n - Postal Address";

	if(msg2!="")
		alert(msg+msg2);
	else	
		d.submit();
}

function ShowBasketAlert(q)
{
if(q==0)
	alert("You must have at least 1 item in your shopping basket to proceed to the checkout");
else
	alert("Due to how Hotcan is packaged, the total quantity of products must be a multiple of four\r\n\r\nCurrent Order Total: "+q);
}

function PrintOrderForm()
{
	window.open("print_form.html",null,"toolbars=0, width=500, height=450");
}

function HideOther()
{
	var d=document.getElementById("othertxt");
	d.style.display="none";
}

function ShowOther()
{
	var d=document.getElementById("othertxt");
	d.style.display="block";
}

function ValidatePrintOrder()
{
	var d = document.printorder;
	msg="The following field are required:\n";
	msg2="";
	
	chked=false;
	for(i=0;i<4;i++)
	{
		if(d.chk_title[i].checked)
			chked=true;
	}
	
	if(!chked)
		msg2+="\nTitle";
	
	if(d.chk_title[3].checked)
	{
		if(d.txttitleother.value=="")
			msg2+="\nTitle";
	}
	
	if(d.txtfirstname.value=="")
		msg2+="\nFirst Name";
		
	if(d.txtsurname.value=="")
		msg2+="\nSurname";
		
	if(d.txtaddr1.value=="")
		msg2+="\nAddress Line 1";
		
	if(d.txtaddr3.value=="")
		msg2+="\nTown / City";
		
	if(d.txtcounty.value=="")
		msg2+="\nCounty";
		
	if(d.txtpostcode.value=="")
		msg2+="\nPostcode";
		
	if(d.txtdaytel.value=="")
		msg2+="\nDaytime Telephone";
			
	if(msg2!="")
		alert(msg+msg2);
	else
		d.submit();
}

function ViewOps()
{
  // get the country into a variable
  var country = document.getElementById('country');
  
  // check to see if a country is selected
  if(country.selectedIndex > -1)
  {
    document.ViewOpportunitiesForm.submit();
  }
}
