// ------------------------------------ sets up css for cross browser code

isMac = (navigator.appVersion.indexOf("Mac") != -1);
isWindows = (navigator.appVersion.indexOf("Windows") != -1);


isIE = (navigator.appName == "Microsoft Internet Explorer")
isNN = (navigator.appName == "Netscape")
isIE5 = (navigator.appVersion.indexOf("MSIE 5") != -1)
isSafari = (navigator.appVersion.indexOf("Safari") != -1);
isFirefox = (navigator.userAgent.indexOf("Firefox") != -1);

if (isIE) {
	document.write('<link rel="stylesheet" type="text/css" href="' + strRoot + '/style/common/ie.css">');
}
if (isNN) {
	document.write('<link rel="stylesheet" type="text/css" href="' + strRoot + '/style/common/nn.css">');
}
if (isMac && isIE) {
	document.write('<link rel="stylesheet" type="text/css" href="' + strRoot + '/style/common/mac.css">');
}
if (isIE5) {
	document.write('<link rel="stylesheet" type="text/css" href="' + strRoot + '/style/common/ie5.css">');
}
if (isSafari) {
	document.write('<link rel="stylesheet" type="text/css" href="' + strRoot + '/style/common/safari.css">');
}
if (isFirefox) {
	document.write('<link rel="stylesheet" type="text/css" href="' + strRoot + '/style/common/firefox.css">');
}

// ------------------------------------ form validation functions

function isBlank(v) {
	if ((v.length>0) && (v!=" ")) {
		return false;
	}
	else {
		return true;
	}
}

function isNumber(v) {
    	if (isNaN(v.replace(/ /g,""))) {
    		return false;
    	}
    	else return true;
}

function isEmail(v) {
	at=v.indexOf('@');
	dot=v.lastIndexOf('.');
	end=v.length;
	subone=v.substring(at+1,dot);
	domainbeforedot=subone.length;
	subtwo=v.substring(dot+1,end);
	domainafterdot=subtwo.length;
   	if (v.indexOf ('@',0) == -1 || v.indexOf ('.',0) == -1  || domainbeforedot < 1 || domainafterdot < 2 || subone.indexOf ('.',0) == 0) {
      		return false;
	}
   	else { 
   		return true; 
   	}
}

function isPostcode(v) {
 	size = v.length;
  	//Strip leading spaces
 	while (v.slice(0,1) == " ") {
 		v = v.substr(1,size-1);
 		size = v.length;
 	}
  	//Strip trailing spaces
	while(v.slice(size-1,size)== " ") {
		v = v.substr(0,size-1);
		size = v.length;
	}	
 	if (size < 6 || size > 8){
 		//Code length rule
   		return false;
  	}
 	if (!(isNaN(v.charAt(0)))){ 
 		//leftmost character must be alpha character rule
   		return false;
  	}
 	if (isNaN(v.charAt(size-3))){ 
 		//first character of inward code must be numeric rule
   		return false;
  	}
 	if (!(isNaN(v.charAt(size-2)))){ 
 		//second character of inward code must be alpha rule
   		return false;
  	}
 	if (!(isNaN(v.charAt(size-1)))){ 
 		//third character of inward code must be alpha rule
   		return false;
  	}
 	count1 = v.indexOf(" ");
 	count2 = v.lastIndexOf(" ");
 	if (count1 != count2){
 		// only one space rule
  		return false;
  	}
	return true;
}

function chkRadio(f, btnName) {
	var btn = f[btnName]
	var valid

	valid = f[btnName].checked;  // checks the status of a single radio button

	for (var x = 0;x < btn.length; x++)
	{
		valid = btn[x].checked
		if (valid) { break }
	}
	if(!valid)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function chkFieldDefaultText(v, defaultValue, action) {
	if (action == "blur") {
		if (isBlank(v.value)) {
			v.value = defaultValue
		}
	}
	if (action == "click") {
		if (v.value == defaultValue) {
			v.value = ""
		}
	}
}

// ------------------------------------ popup functions

function QAS_Popup(fname) {
	QAS_PRO = window.open("/qas/popup.asp?CountryCodeName=GBRUnited+Kingdom&fname="+fname,"QAS_PRO", "scrollbars=yes,resizable=yes,width=600,height=450");
}

function openEmailFriend(url) {
	window.open("/sendemail/sendemail_form.asp?page=" + url,'sendemail','top=140,left=140,width=390,height=460,resizable=0,scrollbars=0,toolbar=0,directories=0,status=0,menubar=0')
}

// ------------------------------------ 



function validateTitleBarKeywordSearch(f){
	if ((f.keyword.value == '') || (f.keyword.value == 'Product Code / Keyword')) {
		alert('You must enter a keyword to search for');
		
		return false;
	}

}

function validateKeywordSearch(f){

}

function openContentPopup(url) {
	window.open(url,'content','top=100,left=100,width=600,height=450,resizable=0,scrollbars=1,toolbar=0,directories=0,status=0,menubar=0');
}

function chkWebLinks(f) {
	if ((f.keyword.value == '') || (f.keyword.value == 'Enter Code')) {
			alert('You must enter a code to search for');
		return false;
	} else {
		return true;
	}
}

function emailSignup(f)
{

	if (!isEmail(f.email.value)) {
		alert("You must enter a valid email address");
		f.email.focus();
		return false;
	}
	else {
		window.open('', 'EmailSignUp', 'menu=no, toolbar=no, scroll=no, width=340, height=500, left=210, top=210');
		return true;
	}
}

var showProducts

function toggleDisplay() {
	if (showProducts == "TRUE") {
		showProducts = "FALSE";
	}
	else {
		showProducts = "TRUE";
	}
	switchDisplay(getElement("detailsBrief"),getElement("detailsExpanded"));
}

function switchDisplay(el1,el2){ 
	if(el1!=null){
		if(el1.style.display=="block" || el1.style.display==""){
			el1.style.display='none';
			el2.style.display='block';
			document.toggle.src=strRoot+'/images/buttons/basket_toggle_up.gif';
		} else {
			el1.style.display='block';
			el2.style.display='none';
			document.toggle.src=strRoot+'/images/buttons/basket_toggle.gif';
		}
	}
}

function getElement(id){
	if(document.all){
		el = document.all(id)
	} else if(document.getElementById){
		el = document.getElementById(id)
	} else {
		el = null
	}
	return el
}

function checkEmail (f) {
	
	if (isBlank(f.Email.value)) {
		alert("The email address must be entered");
		f.Email.focus();
		return false;
	}

	if(!isEmail(f.Email.value)) {
		alert("The email address is not in the correct format");
		f.Email.focus();
		return false;
	}

	return true;
}

function checkReviewProduct (f) {
	if (isBlank(f.reviewTitle.value)) {
		alert("Please enter a title. ");
		f.reviewTitle.focus();
		return false;
	}
	if (isBlank(f.reviewersName.value)) {
		alert("Please enter your name. ");
		f.reviewersName.focus();
		return false;
	}
	if (isBlank(f.reviewText.value)) {
		alert("Please enter your review. ");
		f.reviewText.focus();
		return false;
	}		
	if (f.rating.value == '#') {
		alert("Please select a rating. ");
		f.rating.focus();
		return false;
	}
	if (f.displayName.value == '#') {
		alert("Please select a display option. ");
		f.displayName.focus();
		return false;
	}
	return true;
}


function bookmarksite(title, url){
if (document.all)
window.external.AddFavorite(url, title);
else if (window.sidebar)
window.sidebar.addPanel(title, url, "")
}

function ListMultipleAddToBasket(f,intStart,numOfProducts){ 	
	var productCodes = '';
	var quantities = '';
	var numRows = '';
	var intnumRows = 0;

	for(var i = intStart;i<numOfProducts + 1;i++)
	{
		if(i < 10){
			pF = eval('document.forms.productForm0' + i)
		} else {
			pF = eval('document.forms.productForm' + i)
		}
		if(pF){
			if(pF.quantity.value > 0 && !pF.quantity.disabled){
				productCodes += pF.productCode.value + ", "
				quantities += pF.quantity.value + ", "
				intnumRows = intnumRows + 1;
			}
		}
	}

	productCodes = productCodes.substr(0,productCodes.length-2)
	quantities = quantities.substr(0,quantities.length-2)
	
	if (productCodes == '')
	{
		alert('Please enter a quantity for at least one product')
		return;
	}
	else
	{
		f.productCode.value = productCodes
		f.quantity.value = quantities
		f.numRows.value = intnumRows
		
		f.submit();
		return true;
	}
}

// ------------------------------------ sitestat tracker function

	function sitestat(ns_l){
		ns_l+='&amp;ns__t='+(new Date()).getTime();ns_pixelUrl=ns_l;
		ns_0=document.referrer;
		ns_0=(ns_0.lastIndexOf('/')==ns_0.length-1)?ns_0.substring(ns_0.lastIndexOf('/'),0):ns_0;
		if(ns_0.length>0)ns_l+='&amp;ns_referrer='+escape(ns_0);
		if(document.images){ns_1=new Image();ns_1.src=ns_l;}else
		document.write('<img src="'+ns_l+'" width="1" height="1" alt="">');
		}

// ------------------------------------