function PopUp(theURL,winName,features)
{
	window.open(theURL,winName,features);
}

function ToggleItem(myItem)
{
	if (myItem.style.visibility != 'hidden')
	{
		HideItem(myItem);
	}
	else
	{
		ShowItem(myItem);
	}
	return false;
}

function ShowItem(myItem)
{
	myItem.style.visibility = 'visible';
	myItem.style.display = '';
}

function HideItem(myItem)
{
	myItem.style.visibility = 'hidden';
	myItem.style.display = 'none';
}

//#### PINDAR : KW : Check that the user has added at least
//#### PINDAR : KW : one item to the basket
function CheckQuantity(VersionForm)
{
	//Do we have a number?
	var IsNotANumber = isNaN(VersionForm.Q1.value);
	if(IsNotANumber==false)
	// We Have a number
	{
		if(VersionForm.Q1.value=="" || VersionForm.Q1.value=="0")
		{
			alert("Please enter a valid quantity to add this item to your basket");
			VersionForm.Q1.focus();
			return false;
		}
		else
		{
			return true;
		}
	}		
	else
	{
		// they have entered a non-numeric value
		alert("Please enter a valid number to add this item to your basket");
		VersionForm.Q1.focus();
		return false;
	}
}

function CheckMailingList(TheForm)
{
	var EmailAddy = TheForm.strEmailAddress.value;
	if(EmailAddy=="")
	{
		alert('Please enter a valid email address');
		return false;
	}

	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

	if(reg.test(EmailAddy) == false) 
	{
		alert('Please enter a valid email address');
		return false;
	}
	else
	{
		return true
	}	
}

function ProcessContact(theForm) 
{
	Name=theForm.strContactName.value
	Address1=theForm.strContactAddress1.value
	Address2=theForm.strContactAddress2.value
	Address3=theForm.strContactAddress3.value
	Address4=theForm.strContactAddress4.value
	Postcode=theForm.strContactPostcode.value
	Email=theForm.strContactEmail.value
	Phone=theForm.strContactPhone.value

	//** Check the form

	if(Name=="")
	{
		alert("Please enter your Name");
		theForm.strContactName.focus();
		return false;
	}

	if(Name.length<5)
	{
		alert("Please enter at least 5 characters in the Name field");
		theForm.strContactName.focus();
		return false;
	}

	//if(Email=="")
	//{
	//	alert("Please enter your Email Address");
	//	theForm.strContactEmail.focus();
	//	return false;
	//}

	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(Email!="")
	{
		if(reg.test(Email) == false) {
			alert('Please enter a valid email address');
			theForm.strContactEmail.focus();
			return false;
		}
	}
	if(Address1=="")
	{
		alert("Please enter the first line of your address");
		theForm.strContactAddress1.focus();
		return false;
	}

	if(Address1.length<5)
	{
		alert("Please at least 5 characters in the first line of your address");
		theForm.strContactAddress1.focus();
		return false;
	}	

	if(Address3=="")
	{
		alert("Please enter the town of your address");
		theForm.strContactAddress3.focus();
		return false;
	}

	if(Address4=="")
	{
		alert("Please enter the county of your address");
		theForm.strContactAddress4.focus();
		return false;
	}

	if(Postcode=="")
	{
		alert("Please enter your post code");
		theForm.strContactPostcode.focus();
		return false;
	}

	if(Phone=="")
	{
		alert("Please enter a valid phone number");
		theForm.strContactPhone.focus();
		return false;
	}

	if(Phone.length<8)
	{
		alert("Please enter a valid phone number");
		theForm.strContactPhone.focus();
		return false;
	}

	if(Address2.length<3)
	{
		alert("Please at least 3 characters in your post code");
		theForm.strContactPotcode.focus();
		return false;
	}

	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(Email!="")
	{
		if(reg.test(Email) == false)
		{
			alert('Please enter a valid Email Address');
			return false;
		}
		else
		{
			return true
		}
	}
	else
	{
		return true
	}	
}

function CheckSpeedOrder(LineCount)
{
	// Check Product code format	
	// Format is 00-00000
	var objRegExp  = /[0-9]{2}-[0-9]{5}/;

	//check for valid email
	for (var x = 1; x <= LineCount; x++)
	{
		GetProdCode=document.forms['speedorder'].elements['strCode'+x].value;
		GetQuant=document.forms['speedorder'].elements['numQuant'+x].value;
		if(GetProdCode!="" && GetQuant=="")
		{
			alert("Please enter a quantity for Product Code "+GetProdCode);
			document.forms['speedorder'].elements['numQuant'+x].focus()	
			return false
		}

		if(GetQuant!="" && GetProdCode=="")
		{
			alert("Please enter a Product Code on line " + x);
			document.forms['speedorder'].elements['strCode'+x].focus();
			return false
		}

		// Validate product code format
		if (GetProdCode!="")
		{
 		if(objRegExp.test(GetProdCode) == false)
 			{
	 			alert("The Product code\n"+GetProdCode+"\ndoes not appear to be in a valid format");
	 			return false;
 			}
		}
	}

	// Now check to see if they've put anything in any of the fields - if not
	// don't submit the form
	blankCount = 0

	for (var x = 1; x <= LineCount; x++)
	{
		GetProdCode=document.forms['speedorder'].elements['strCode'+x].value;
		if(GetProdCode=="")
		{
			blankCount++
		}
	}		

	if(blankCount == LineCount)
	{
		alert("Please enter at least one product code");
		document.forms['speedorder'].elements['strCode1'].focus();
		return false;
	}
	else
	{
		return true
	}
}

//**********************************************************
// Function to create a cookie that holds the name of the
// content container, clicked tag and how long before expiry
//**********************************************************

function createCookie(name,value,days)
{
    if (days)
    {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";

    var ck = name+"="+value+expires+"; path=/";
   //if (days != -1) alert('Cookie\n' + ck + '\ncreated');
    document.cookie = ck;
}

//**********************************************************
// Function to read the data out of the stored cookie so
// That we know, and can close, the previous content and
// change previous tag style
//**********************************************************
function readCookie(name)
{
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i<ca.length;i++)
    {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
} 

//**********************************************************
// Style Sheet Switcher Script
//**********************************************************
function setActiveStyleSheet(title)
{
	var i, a, main;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
	{
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title"))
		{
			a.disabled = true;
			if(a.getAttribute("title") == title) a.disabled = false;
		}
	}
}

function getActiveStyleSheet() {
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
	{
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
	}
	return null;
}

function getPreferredStyleSheet()
{
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
	{
    	if(a.getAttribute("rel").indexOf("style") != -1
			&& a.getAttribute("rel").indexOf("alt") == -1
			&& a.getAttribute("title")
			) return a.getAttribute("title");
	}
	return null;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

//#### PINDAR : KW : Contact Form - Show/Hide "Other Sector" row
function CheckOther()
{
	if (document.ContactForm.strIndustry.options[document.ContactForm.strIndustry.selectedIndex].value == "Other")
	{
		document.getElementById('OtherSector').style.display="";
	}
	else
	{
		document.getElementById('OtherSector').style.display="none";
	}
}

// Contact Form
function CheckContact(MyForm)
{
	if(MyForm.strContactName.value=='')
	{
		alert("Please enter your name");
		MyForm.strContactName.focus();
		return false;
	}

	if(MyForm.strContactEmail.value=='')
	{
		alert("Please enter your email address");
		MyForm.strContactEmail.focus();
		return false;
	}

	EmailAddress = MyForm.strContactEmail.value	
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(EmailAddress) == false)
	{
		alert('Please enter a valid email address');
		MyForm.strContactEmail.focus();
		return false;
	}

	PhoneNum = MyForm.strContactPhone.value	
	if(PhoneNum=='')
	{
		alert("Please enter your Phone Number");
		MyForm.strContactPhone.focus();
		return false;
	}

	if(PhoneNum.length<8)
	{
		alert("Please enter a valid Phone Number");
		MyForm.strContactPhone.focus();
		return false;
	}	

	if(MyForm.strIndustry.value=='')
	{
		alert("Please select your industry");
		return false;
	}	

	if(MyForm.strContactComments.value=='')
	{
		alert("Please enter your Message");
		MyForm.strContactComments.focus();
		return false;
	}

	return true;	
}

//#### PINDAR : RH: Add Request Callback check
function CheckCallback(MyForm)
{
	if(MyForm.strContactName.value=='')
	{
		alert("Please enter your name");
		MyForm.strContactName.focus();
		return false;
	}

	PhoneNum = MyForm.strContactPhone.value	
	if(PhoneNum=='')
	{
		alert("Please enter your Phone Number");
		MyForm.strContactPhone.focus();
		return false;
	}

	if(PhoneNum.length<8)
	{
		alert("Please enter a valid Phone Number");
		MyForm.strContactPhone.focus();
		return false;
	}	

	if(MyForm.strContactRequestCallbackTime.value=='')
	{
		alert("Please select your callback time");
		return false;
	}	
	return true;	
}

//#### PINDAR : KW : Check to see if a basket value has been changed
var globalQTYChanged = 0;

function CheckBasketQuantity(BasketField)
{
	globalQTYChanged = 1;
}

//#### PINDAR : KW : Alerts user if they've clicked the Make Enquiry or checkout button at basket stage
function CheckBasketSubmit()
{
	alert("After changing the quantity, please make sure you\nhit the \"recalculate\" button to update your basket");
	return false;
}

/*
'######################################################################
'#### PINDAR : OM : 2010-10-15
'#### PINDAR : OM : Set payment type for button clicked (AS site only).
'######################################################################
*/

function SetPaymentType(ptype) {
	switch (ptype) {
		case 'card':
		case 'po':
		case 'paypal':
			document.getElementById('paymentOption').value = ptype;
			var basket = document.getElementById('frmBasket');
			basket.action = "checkout.asp";
			basket.method = "post";
			basket.submit();
			break;
		default: // Not a recognised payment type - don't proceed to checkout.
			return false;
	}
}

/*
'######################################################################
'#### PINDAR : OM : End of payment type changes.
'######################################################################
*/


/*
'######################################################################
'#### PINDAR : OM : 2010-10-15
'#### PINDAR : OM : Set login type prior to proceeding further through
'					checkout (AS site only).
'######################################################################
*/

function SetLoginType(ptype) {
	var btnProceed = document.getElementById('submitforward');
	var btnRemind = document.getElementById('remindme');
	var txtRemind = document.getElementById('remindertext');
	var frmLogin = document.getElementById('login');
	switch (ptype) {
		case 'proceed':
			btnProceed.style.display = 'block';
			btnRemind.style.display = 'none';
			txtRemind.style.display = 'none';
			try{
				frmLogin.setAttribute('action','/checkout.asp');
			}catch(err){
				btnProceed.setAttribute('disabled','disabled');
			}
			break;
		case 'remind':
			btnProceed.style.display = 'none';
			btnRemind.style.display = 'block';
			txtRemind.style.display = 'block';
			try{
				frmLogin.setAttribute('action','/emailreminder.asp');
			}catch(err){
				btnRemind.setAttribute('disabled','disabled');
			}
			break;
		default: // Not a recognised login type - don't do anything.
			return false;
	}
	return true;
}

/*
'######################################################################
'#### PINDAR : OM : End of login type changes.
'######################################################################
*/

//#### PINDAR : KW : Show/Hide script for basket help text
function showhide(id)
{
	if (document.getElementById)
	{
		obj = document.getElementById(id);
		if (obj.style.display == "none")
		{
			obj.style.display = "";
		} 
		else 
		{
			obj.style.display = "none";
		}
	}
}

function CheckSearch(TheForm)

{
	//Commented out - we can allow blank keywords
	//if(TheForm.strKeywords.value=='')
	//	{
	//	alert("Please enter your keywords to search for");
	//	TheForm.strKeywords.focus();
	//	return false;
	//	}
	//	else
	//	{
	//	return true;
	//	}

	//check price fields for numeric
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
	var Price;	
	//Min Price
	if(TheForm.numPriceMin.value!='')
	{
		Price = TheForm.numPriceMin.value;
		for (i = 0; i < Price.length && IsNumber == true; i++) 
		{ 
			Char = Price.charAt(i); 
			if (ValidChars.indexOf(Char) == -1) 
			{
				alert("Please enter a valid minimum price");
				TheForm.numPriceMin.focus();
				IsNumber = false;
			}
		}
	}

	//Max Price
	if(TheForm.numPriceMax.value!='')
	{
		Price = TheForm.numPriceMax.value;
	  	for (i = 0; i < Price.length && IsNumber == true; i++) 
		{ 
			Char = Price.charAt(i); 
			if (ValidChars.indexOf(Char) == -1) 
			{
				alert("Please enter a valid maximum price");
				TheForm.numPriceMax.focus();
				IsNumber = false;
			}
		}
	}
   return IsNumber;
}

function CheckOtherCheckOut()
{
	if (document.CheckoutForm.O_Referrer.options[document.CheckoutForm.O_Referrer.selectedIndex].value == "Other Search Engine" ||
		document.CheckoutForm.O_Referrer.options[document.CheckoutForm.O_Referrer.selectedIndex].value == "Mail Campaign")
	{
		document.getElementById('OtherRow').style.display="";
	}
	else
	{
		document.getElementById('OtherRow').style.display="none";
	}

	if (document.CheckoutForm.O_Sector.options[document.CheckoutForm.O_Sector.selectedIndex].value == "Other")
	{
		document.getElementById('SectorOtherRow').style.display="";
	}
	else
	{
		document.getElementById('SectorOtherRow').style.display="none";
	}
}

/***** NEWS SCROLLER ******/
/***********************************************
* Pausing up-down scroller- &copy; Dynamic Drive (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/

function pausescroller(content, divId, divClass, delay){
this.content=content //message array content
this.tickerid=divId //ID of ticker div to display information
this.delay=delay //Delay between msg change, in miliseconds.
this.mouseoverBol=0 //Boolean to indicate whether mouse is currently over scroller (and pause it if it is)
this.hiddendivpointer=1 //index of message array for hidden div
document.write('<div id="'+divId+'" class="'+divClass+'" style="position: relative; overflow: hidden"><div class="innerDiv" style="position: absolute; width: 100%" id="'+divId+'1">'+content[0]+'</div><div class="innerDiv" style="position: absolute; width: 100%; visibility: hidden" id="'+divId+'2">'+content[1]+'</div></div>')
var scrollerinstance=this
if (window.addEventListener) //run onload in DOM2 browsers
window.addEventListener("load", function(){scrollerinstance.initialize()}, false)
else if (window.attachEvent) //run onload in IE5.5+
window.attachEvent("onload", function(){scrollerinstance.initialize()})
else if (document.getElementById) //if legacy DOM browsers, just start scroller after 0.5 sec
setTimeout(function(){scrollerinstance.initialize()}, 500)
}

// -------------------------------------------------------------------
// initialize()- Initialize scroller method.
// -Get div objects, set initial positions, start up down animation
// -------------------------------------------------------------------

pausescroller.prototype.initialize=function(){
this.tickerdiv=document.getElementById(this.tickerid)
this.visiblediv=document.getElementById(this.tickerid+"1")
this.hiddendiv=document.getElementById(this.tickerid+"2")
this.visibledivtop=parseInt(pausescroller.getCSSpadding(this.tickerdiv))
//set width of inner DIVs to outer DIV's width minus padding (padding assumed to be top padding x 2)
this.visiblediv.style.width=this.hiddendiv.style.width=this.tickerdiv.offsetWidth-(this.visibledivtop*2)+"px"
this.getinline(this.visiblediv, this.hiddendiv)
this.hiddendiv.style.visibility="visible"
var scrollerinstance=this
document.getElementById(this.tickerid).onmouseover=function(){scrollerinstance.mouseoverBol=1}
document.getElementById(this.tickerid).onmouseout=function(){scrollerinstance.mouseoverBol=0}
if (window.attachEvent) //Clean up loose references in IE
window.attachEvent("onunload", function(){scrollerinstance.tickerdiv.onmouseover=scrollerinstance.tickerdiv.onmouseout=null})
setTimeout(function(){scrollerinstance.animateup()}, this.delay)
}


// -------------------------------------------------------------------
// animateup()- Move the two inner divs of the scroller up and in sync
// -------------------------------------------------------------------

pausescroller.prototype.animateup=function(){
var scrollerinstance=this
if (parseInt(this.hiddendiv.style.top)>(this.visibledivtop+5)){
this.visiblediv.style.top=parseInt(this.visiblediv.style.top)-5+"px"
this.hiddendiv.style.top=parseInt(this.hiddendiv.style.top)-5+"px"
setTimeout(function(){scrollerinstance.animateup()}, 50)
}
else{
this.getinline(this.hiddendiv, this.visiblediv)
this.swapdivs()
setTimeout(function(){scrollerinstance.setmessage()}, this.delay)
}
}

// -------------------------------------------------------------------
// swapdivs()- Swap between which is the visible and which is the hidden div
// -------------------------------------------------------------------

pausescroller.prototype.swapdivs=function(){
var tempcontainer=this.visiblediv
this.visiblediv=this.hiddendiv
this.hiddendiv=tempcontainer
}

pausescroller.prototype.getinline=function(div1, div2){
div1.style.top=this.visibledivtop+"px"
div2.style.top=Math.max(div1.parentNode.offsetHeight, div1.offsetHeight)+"px"
}

// -------------------------------------------------------------------
// setmessage()- Populate the hidden div with the next message before it's visible
// -------------------------------------------------------------------

pausescroller.prototype.setmessage=function(){
var scrollerinstance=this
if (this.mouseoverBol==1) //if mouse is currently over scoller, do nothing (pause it)
setTimeout(function(){scrollerinstance.setmessage()}, 100)
else{
var i=this.hiddendivpointer
var ceiling=this.content.length
this.hiddendivpointer=(i+1>ceiling-1)? 0 : i+1
this.hiddendiv.innerHTML=this.content[this.hiddendivpointer]
this.animateup()
}
}

pausescroller.getCSSpadding=function(tickerobj){ //get CSS padding value, if any
if (tickerobj.currentStyle)
return tickerobj.currentStyle["paddingTop"]
else if (window.getComputedStyle) //if DOM2
return window.getComputedStyle(tickerobj, "").getPropertyValue("padding-top")
else
return 0
}

/** See if we need to change Checkout URL to go to Google or not **/
function CheckPaymentGateway(TheForm)
{
	if(TheForm.strPaymentSystem.value=="google")
	{
		TheForm.action="googlecheckout.asp";
	}
	return true;
}
