function OpenNews(nid)
{
	var win= null;
	var winl = (screen.width-550)/2;
	var wint = (screen.height-350)/2;

	var settings  ='height=350,';
	settings +='width=550,';
	settings +='top='+wint+',';
	settings +='left='+winl+',';
	settings +='toolbar=no,';
	settings +='location=no,';
	settings +='menubar=no,scrollbars=yes,resizable=no,status=no';
	
	news=window.open("news.php?ID="+nid,"newswin",settings);
	
	if(news.focus)
		news.focus();
}

function OpenNewsE(nid)
{
	var win= null;
	var winl = (screen.width-550)/2;
	var wint = (screen.height-350)/2;

	var settings  ='height=350,';
	settings +='width=550,';
	settings +='top='+wint+',';
	settings +='left='+winl+',';
	settings +='toolbar=no,';
	settings +='location=no,';
	settings +='menubar=no,scrollbars=yes,resizable=no,status=no';

	news=window.open("externalnews.php?article="+nid,"newswin",settings);
	
	if(news.focus)
		news.focus();
}

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 checkRegisterEnter(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(ValidAddCandidate())
			obj.form.submit();
		return false;
	}
	else
	{
		return true
	}
}

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_request.php?type=new",null,"toolbars=0, width=450, height=510");
}

function ExistingTradeCustomerForm()
{
	window.open("uktrade_request.php?type=existing",null,"toolbars=0, width=450, height=540");
}

function TradeResellerForm()
{
	window.open("uktrade_request.php?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_request.php?type=new",null,"toolbars=0, width=450, height=510");
}

function ExistingEmergencyCustomerForm()
{
	window.open("uk_emergency_request.php?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.php",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.php",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.php",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 ToggleMenu(id)
{
	btnid = "menubtn"+id;
	id = "menuchild"+id;
	
	if(document.getElementById)
	{
		theObj = document.getElementById(id);
		theBtn = document.getElementById(btnid);
		
		if(!theObj || !theBtn)
			return;
			
		if(theObj.style.display=="block")
		{
			theObj.style.display="none";
			theBtn.src="images/base_graphics/plus.gif";
		}
		else
		{
			theObj.style.display="block";
			theBtn.src="images/base_graphics/minus.gif";
		}
	}
}

function scrollUp()
{
	var d = document.getElementById('news_scroll');
	var dragBar = document.getElementById('dragBar');

	scrolling = setInterval("doScrolling('-1')", 10);
}

function scrollDown()
{
	var d = document.getElementById('news_scroll');
	var dragBar = document.getElementById('dragBar');

	scrolling = setInterval("doScrolling('1')", 10);
}

function doScrolling(direction)
{
	var d = document.getElementById('news_scroll');
	var dragBar = document.getElementById('dragBar');

	switch(direction)
	{
		case "-1":
			d.scrollTop = parseInt(d.scrollTop) - 3;
			dragBar.style.top = 50 + ((parseFloat(d.scrollTop) / parseFloat(d.scrollHeight)) * 153);
		break;

		case "1":
			d.scrollTop = parseInt(d.scrollTop) + 3;
			dragBar.style.top = 50 + ((parseFloat(d.scrollTop) / parseFloat(d.scrollHeight)) * 153);
		break;
	}
}

function stopScroll()
{
	clearInterval(scrolling);
}

function mouseScroll()
{
	var d = document.getElementById('news_scroll');
	var dragBar = document.getElementById('dragBar');

	//dragBar.style.top = yMousePos - parseInt(dragBar.style.top);
}
 
function swap_sub(num, dir){
 if (dir==0){
  document.getElementById('pic'+num).src=subpics2[num].src;
 }
 else {
  document.getElementById('pic'+num).src=subpics[num].src;
 } 
}

function swap(num, dir){
 if (dir==0){
  document.getElementById('pic'+num).src=homepics2[num].src;
 }
 else {
  document.getElementById('pic'+num).src=homepics[num].src;
 } 
}


function countchars(what, val)
{
	formcontent=what.enquiry.value;
	count=val-formcontent.length;
	if (count<0)
	{
		count=0
	}
	
	document.getElementById('chars').firstChild.nodeValue=count;
}

function countchars2(what, val)
{
	formcontent=what.desc.value;
	count=val-formcontent.length;
	
	alert(formcontent.length);
	
	if (count<0)
	{
		count=0
	}
	document.getElementById('chars').firstChild.nodeValue=count;
}



function testcontact(val){
if (document.forms['contact'].enquiry.value.length<(val+1)){
 document.forms['contact'].submit();
}
else {
alert ("Your enquiry MUST be less than 500 characters");
}
}
function testaddjob(val){
if (document.forms['addjob'].desc.value.length<(val+1)){
 document.forms['addjob'].submit();
}
else {
alert ("Your description MUST be less than 500 characters");
}
}

function validateLogin()
{
	var d = document.loginfrm;
	
	msg="The following fields are required:\n";
	msg2="";
	
	if(d.login_username.value == "")
	{
		msg2 += "\nLogin Username";
	}
		
	if(d.login_password.value=="")
	{
		msg2+="\nLogin Password";
	}
	
	if(msg2!="")
		alert(msg+msg2);
	else
		d.submit();	
}

function ValidAddJob(chkspec)
{
	var d = document.addjob;
		
	msg="The following fields are required:\n";
	msg2="";
	
	if(d.ref.value=="")
		msg2+="\n - Client Ref #";
	
	if(d.title.value=="")
		msg2+="\n - Title";
		
	if(d.location.value=="")
		msg2+="\n - Location";
		
	if(chkspec!=0)
		if(d.jobspec.value=="")
			msg2+="\n - Job Spec ";
			
	if(msg2!="")
	{
		alert(msg+msg2);
		return false;
	}
	else
		return true;
}

function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		if(limitCount)
			limitCount.innerHTML = limitNum - limitField.value.length;
	}
}

function ConfirmRemoveJob(id)
{
	if(confirm("Are you sure you wish to delete this job from the system? (This cannot be undone)"))
		location="clientjobs.php?cmd=remove&id="+id;
}

function ForgotClientPass()
{
	wind=window.open("login.php?page=forgotclient",null,"toolbars=0, width=400, height=160");
	wind.focus();
}

function ForgotCandPass()
{
	wind=window.open("login.php?page=forgotcand",null,"toolbars=0, width=400, height=160");
	wind.focus();
}

function AttachNewJobSpec(jobid)
{
	win=window.open("clientjobs.php?cmd=attach&id="+jobid,null,"toolbars=0,width=400, height=160");
	win.focus();
}

function ValidAttachSpec()
{
	var d = document.attfrm;
		
	msg="The following fields are required:\n";
	msg2="";
	
	if(d.fattach.value == "")
		msg2+="\n - Please attach a file";
	
	if(msg2!="")
	{
		alert(msg+msg2);
		return false;
	}
	else
		return true;
}

function ValidAddCandidate()
{
	var d = document.candfrm;
			
	msg="The following fields are required:\n";
	msg2="";
	
	if(d.username.value=="")
		msg2+="\n - Username";
		
	if(d.password.value=="")
		msg2+="\n - Password";
	else
		if(d.password.value.length < 6)
			msg2 += "\n - Password must be 6 characters or more";
			
	if(d.password.value != d.passwordconf.value)
		msg2 += "\n - Passwords do not match - please confirm password";
		
	if(d.name.value=="")
		msg2+="\n - Name";
		
	if(d.surname.value=="")
		msg2+="\n - Surname";
		
	if(d.telephone.value=="")
		msg2+="\n - Telephone";
		
	if(d.email.value=="")
		msg2+="\n - Email";
	else
		if(!ValidEmail(d.email))
			msg2 += "\n - Invalid Email";	
	
	if(msg2!="")
	{
		alert(msg+msg2);
		return false;
	}
	else
		return true;
}

function ValidContactFrm()
{
	var d = document.contact;
			
	msg="The following fields are required:\n";
	msg2="";
	
	if(d.firstname.value=="")
		msg2+= "\n - First Name";
		
	if(d.lastname.value=="")
		msg2+= "\n - Last Name";
		
	if(d.email.value=="")
		msg2+= "\n - Email";
	else
		if(!ValidEmail(d.email))
			msg2+= "\n - Invalid email address";
			
	if(d.enquiry.value=="")
		msg2 += "\n - You must enter an enquiry";
	
	if(msg2!="")
	{
		alert(msg+msg2);
		return false;
	}
	else
		return true;
}

function ConfirmRemoveJobCandidate(id)
{
	if(confirm("Are you sure you wish to remove this candidate? (This cannot be undone)"))
		location.href="clientjobs.php?cmd=removecand&id="+id;
}

function FocusOnObject(obj)
{
	var d=null;

	if(document.getElementById)
		if(document.getElementById(obj))
			d=document.getElementById(obj);

	if(d!=null)
		d.focus();
}

function submit_on_enter(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)
	{ 
		obj.form.submit();
	}
	else
	{
		return true
	}
}

function confirmUpdateClientData()
{
	var d=document.clform;
	var msg="Please enter a value for the following fields:\n";
	var msg2="";
	
	if(d.txtusername.value=="")
		msg2+="\n - Username";
		
	if(d.txtname.value=="")
		msg2+="\n - Name";
		
	if(d.txtemail.value=="")
		msg2+="\n - Email Address";
	else
		if(!ValidEmail(d.txtemail))
			msg2+="\n - Invalid Email Address";
			
	if(msg2!="")
	{
		alert(msg+msg2);
		return false;
	}
	else
	if(d.txtpassword.value!="" || d.txtconfirm.value!="")
		if(d.txtpassword.value!=d.txtconfirm.value)
		{
			alert("The passwords you entered do not match. Please re-enter");
			return false;
		}
		
	return true;
}