var imgList=new Array();
var imgCtr=0;
var noImg=0;
function shiftImg(mVal){
	if (mVal == -1){
	 imgCtr=imgCtr-1
	 if (imgCtr < 0) imgCtr=noImg;
	} else {
	 imgCtr=imgCtr+1
	 if (imgCtr > noImg) imgCtr=0;
	}
	swapImage(imgList[imgCtr]);
	
}

function fixPics(){
	if (screen.width <1280){
	 if (document.getElementById('lifestyle')){
	  newImg=new Image();
	  newImg.src = document.getElementById('lifestyle').src;
	  var nW=newImg.width/1.3;
	  document.getElementById('lifestyle').width=nW;
	 }
	}
}
function doCustRP(cid){
        ajax = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
        ajax.open('POST', '/rp', true);
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        ajax.send('doCust='+cid);
}
function valCard(val){
	if (val.length >0 && val.length<15) {
	 alert('This does not appear to be a valid card number');
	}
}

function valForm() {
	//first check that all fields are populated
	//then escape evil characters
	document.getElementById('logFrm').submit();
	return;

	var msg="";
	if (document.getElementById('title').value == '') msg=msg+"Title\n";
	if (document.getElementById('fname').value == '') msg=msg+"Forename\n";
	if (document.getElementById('sname').value == '') msg=msg+"Surname\n";
	if (document.getElementById('password1').value == '') msg=msg+"Password 1\n";
	if (document.getElementById('password2').value == '') msg=msg+"Password 2\n";
	if (document.getElementById('postcode').value == '') msg=msg+"Postcode\n";
	if (document.getElementById('add1').value == '') msg=msg+"Address 1\n";
	if (document.getElementById('add2').value == '') msg=msg+"Address 2\n";
	if (document.getElementById('town').value == '') msg=msg+"Town\n";
	if (document.getElementById('county').value == '') msg=msg+"County\n";
	if (document.getElementById('dphone').value == '') msg=msg+"Daytime Phone\n";
	if (msg != "") {
	 alert("The following fields must be filled in\n"+msg);
	 return false;
	}
	if (document.getElementById('password1').value !=document.getElementById('password2').value) {
	 alert("Your passwords do not match. Please re-enter");
	}

	document.getElementById('title').value=(document.getElementById('title').value).replace("'","\'");
	document.getElementById('fname').value=(document.getElementById('fname').value).replace("'","\'");
	document.getElementById('sname').value=(document.getElementById('sname').value).replace("'","\'");
	document.getElementById('email').value=(document.getElementById('email').value).replace("'","\'");
	document.getElementById('password1').value=(document.getElementById('password1').value).replace("'","\'");
	document.getElementById('password2').value=(document.getElementById('password2').value).replace("'","\'");
	document.getElementById('add1').value=(document.getElementById('add1').value).replace("'","\'");
	document.getElementById('add2').value=(document.getElementById('add2').value).replace("'","\'");
	document.getElementById('town').value=(document.getElementById('town').value).replace("'","\'");
	document.getElementById('county').value=(document.getElementById('county').value).replace("'","\'");
	document.getElementById('dphone').value=(document.getElementById('dphone').value).replace("'","\'");
	document.getElementById('ephone').value=(document.getElementById('ephone').value).replace("'","\'");
	document.getElementById('mphone').value=(document.getElementById('mphone').value).replace("'","\'");
	document.getElementById('logFrm').submit();
	return;

}

function sentCase(e,r) {
	var marr=r.value.split(" ");
	var marl=marr.length
	for (i=0; i<marl; i++) {
	 marr[i]=marr[i].toLowerCase();
	 v1=marr[i].substring(0,1).toUpperCase();
	 v2=marr[i].substring(1);
	 marr[i]=v1+v2
	}
	r.value=marr.join(" ");
}

function chkTerm(){
	if (document.getElementById('termconf').checked==false){
	alert('Please click to confirm you have read and agree to our terms and conditions');
	return false;
	} else {
	return true;
	}
}


function chkMail(ml) {
	if (ml == "") {return;}
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if(filter.test(ml)){
         return (true)
        } else {
         alert("Invalid E-mail Address! Please re-enter.")
         return (false)
        }
}

function bookmarksite(title,url) { 
	if (window.sidebar) { 
		window.sidebar.addPanel(title, url,"");
	} else if( document.all ) {
		window.external.AddFavorite( url, title);
	} else if( window.opera && window.print ) {
		return true;
	}
}

function checkoutswap(toswap) {
        document.getElementById('card').style.display= "none";
        document.getElementById('v12').style.display= "none";
        document.getElementById('offline').style.display= "none";
        document.getElementById(toswap).style.display= "";
}

function swapProductTab(toswap) {
        document.getElementById('description').style.display= "none";
        document.getElementById('moreimages').style.display= "none";
        document.getElementById('descriptionleft').src="/images/tabl.gif";
        document.getElementById('moreimagesleft').src="/images/tabl.gif";
        document.getElementById('descriptionright').src="/images/tabr.gif";
        document.getElementById('moreimagesright').src="/images/tabr.gif";
        document.getElementById('descriptioncenter').className="productdetailsmoreinfotab";
        document.getElementById('moreimagescenter').className="productdetailsmoreinfotab";
	if (document.getElementById('accessoriesleft')){
        document.getElementById('accessoriesleft').src="/images/tabl.gif";
        document.getElementById('accessoriesright').src="/images/tabr.gif";
        document.getElementById('accessoriescenter').className="productdetailsmoreinfotab";
        document.getElementById('accessories').style.display= "none";
	}
	
        document.getElementById(toswap).style.display= "";
        document.getElementById(toswap+'left').src="/images/tablselected.gif";
        document.getElementById(toswap+'right').src="/images/tabrselected.gif";
        document.getElementById(toswap+'center').className="productdetailsmoreinfotabselected";
}

function swapCustomerTab(toshow) {
        document.getElementById('personaldetails').style.display= "none";
        document.getElementById('registeredaddresses').style.display= "none";
        document.getElementById('orderhistory').style.display= "none";
        document.getElementById('customerreviews').style.display= "none";
        document.getElementById(toshow).style.display= "block";
}

function swapCustomerAddress(toswap) {
        document.getElementById('addressid').value= document.getElementById('cad_id' + toswap).value;
        document.getElementById('addressname').value= document.getElementById('cad_name' + toswap).value;
        document.getElementById('addresscustomerid').value= document.getElementById('cad_custid' + toswap).value;
        document.getElementById('add1').value= document.getElementById('cad_add1' + toswap).value;
        document.getElementById('add2').value= document.getElementById('cad_add2' + toswap).value;
        document.getElementById('town').value= document.getElementById('cad_town' + toswap).value;
        document.getElementById('county').value= document.getElementById('cad_county' + toswap).value;
        document.getElementById('postcode').value= document.getElementById('cad_pcode' + toswap).value;
        document.getElementById('country').value= document.getElementById('cad_country' + toswap).value;
        document.getElementById('dphone').value= document.getElementById('cad_dphone' + toswap).value;
        document.getElementById('ephone').value= document.getElementById('cad_ephone' + toswap).value;
        document.getElementById('mphone').value= document.getElementById('cad_mphone' + toswap).value;
}

function swapImage(toswap) {
        document.getElementById('mainimage').src = "/images/"+toswap;
}

function setAdd1(){
	  document.getElementById('add1').value=document.getElementById('housename').value+' '+document.getElementById('aaad1').value;
	}

function doPcode(postcode) {
        ajax = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
        ajax.onreadystatechange = function(){
                if (ajax.readyState == 4){
                        if (ajax.status == 200){ 
 			mText=ajax.responseText;
           		mvals=mText.split("~");
			document.getElementById('add1').value=mvals[0];
			document.getElementById('aaad1').value=mvals[0];
			document.getElementById('add2').value=mvals[1];
			document.getElementById('town').value=mvals[2];
			document.getElementById('county').value=mvals[3];
			document.getElementById('postcode').value=mvals[4];
	 		setAdd1();
                        }
                }
        }
        ajax.open('POST', '/rp', true);
        ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        ajax.send('postcode='+postcode);
}



function refreshBasket(partno) {
        ajaxRB = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
        ajaxRB.onreadystatechange = function(){
                if (ajaxRB.readyState == 4){
                        if (ajaxRB.status == 200){
                                document.getElementById('minibasket').innerHTML=ajaxRB.responseText;
                        }
                }
        }
        ajaxRB.open('POST', 'index.n', true);
	ajaxRB.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        ajaxRB.send('action=script&script=refreshbasket');
}
function refreshBasketP(partno) {
        ajaxRB = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
        ajaxRB.onreadystatechange = function(){
                if (ajaxRB.readyState == 4){
                        if (ajaxRB.status == 200){
                                parent.document.getElementById('minibasket').innerHTML=ajaxRB.responseText;
                        }
                }
        }
        ajaxRB.open('POST', 'index.n', true);
	ajaxRB.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        ajaxRB.send('action=script&script=refreshbasket');
}


function refreshAvail(partno) {
        ajaxRA = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
        ajaxRA.onreadystatechange = function(){
                if (ajaxRA.readyState == 4){
                        if (ajaxRA.status == 200){
                                document.getElementById('mavail').innerHTML=ajaxRA.responseText;
                        }
                }
        }
        ajaxRA.open('POST', 'index.n', true);
        ajaxRA.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        ajaxRA.send('action=script&script=showavail&partno='+partno);
}


function updateAddonBasket(addon, bid) {
        ajax = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
        ajax.onreadystatechange = function(){
                if (ajax.readyState == 4){
                        if (ajax.status == 200){
                                location.reload(true);
				refreshBasket();
                        }
                }
        }
        ajax.open('POST', 'index.n', true);
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        ajax.send('action=script&script=updateaddonbasket&addon='+addon+'&BID='+bid+'&quantity='+document.getElementById(addon).value);
}

function updateBasketWarranty(BID, warid) {
        ajax = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
        ajax.onreadystatechange = function(){
                if (ajax.readyState == 4){
                        if (ajax.status == 200){
                                location.reload(true);
				refreshBasket();
                        }
                }
        }
        ajax.open('POST', 'index.n', true);
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        ajax.send('action=script&script=updatebasketwarranty&bid='+BID+'&warid='+document.getElementById(BID).value);
}

function updateBasket(partno) {
	ajax = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
        ajax.onreadystatechange = function(){
                if (ajax.readyState == 4){
                        if (ajax.status == 200){
                        	location.reload(true);
				refreshBasket();
			}
                }
        }
        ajax.open('POST', 'index.n', true);
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        ajax.send('action=script&script=updatebasket&partno='+partno+'&quantity='+document.getElementById(partno).value);
}
function adminWeek(partno,myear,mweek,price) {
	document.getElementById('adminframe').src='/index.n?action=script&script=admin&partno='+partno+'&myear='+myear+'&mweek='+mweek+'&quantity=1&price='+price;
	document.getElementById('availability').style.display='none';
	document.getElementById('admindiv').style.display='';
}

function closeAd(){
	parent.document.getElementById('admindiv').style.display='none';
	parent.document.getElementById('availability').style.display='';
}

function bookWeek(partno,myear,mweek,price) {
        ajax = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
        ajax.onreadystatechange = function(){
        	if (ajax.readyState == 4){
         		if (ajax.status == 200){
     //     			document.getElementById('minibasket').innerHTML=ajax.responseText;
				refreshBasket();
				refreshAvail(partno);
          			alert(partno+' added to basket on week '+mweek+"/"+myear);
          		}
        	}
        }
	ajax.open('POST', 'index.n', true);
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	ajax.send('action=script&script=addtobasket&partno='+partno+'&myear='+myear+'&mweek='+mweek+'&quantity=1&price='+price);
}
function delWeek(BID,partno) {
        ajax = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
        ajax.onreadystatechange = function(){
        	if (ajax.readyState == 4){
         		if (ajax.status == 200){
				refreshBasket();
				refreshAvail(partno);
          			alert(partno+" removed from basket");
          		}
        	}
        }
	ajax.open('POST', 'index.n', true);
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	ajax.send('action=script&script=deletefrombasket&BID='+BID);
}


function addToBasket(partno,quantity,price) {
        ajax = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
        ajax.onreadystatechange = function(){
        	if (ajax.readyState == 4){
         		if (ajax.status == 200){
     //     			document.getElementById('minibasket').innerHTML=ajax.responseText;
				refreshBasket();
          			alert(partno+' added to basket');
          		}
        	}
        }
	ajax.open('POST', 'index.n', true);
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	ajax.send('action=script&script=addtobasket&partno='+partno+'&quantity='+quantity+'&price='+price);
}

function addAddonToBasket(partno, quantity, addon) {
        ajax = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
        ajax.onreadystatechange = function(){
                if (ajax.readyState == 4){
                        if (ajax.status == 200){
				refreshBasket();
                                alert(ajax.responseText);
                        }
                }
        }
        ajax.open('POST', 'index.n', true);
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        ajax.send('action=script&script=addaddontobasket&partno='+partno+'&addon='+addon+'&quantity='+quantity);
}

function addWarrantyToBasket(warid, partno) {
        ajax = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
        ajax.onreadystatechange = function(){
                if (ajax.readyState == 4){
                        if (ajax.status == 200){
				refreshBasket();
                                alert(ajax.responseText);
                        }
                }
        }
        ajax.open('POST', 'index.n', true);
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        ajax.send('action=script&script=addwarrantytobasket&partno='+partno+'&warid='+warid);
}

function addToCompare(partno) {
        ajax = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
        ajax.onreadystatechange = function(){
                if (ajax.readyState == 4){
                        if (ajax.status == 200){
                                text = ajax.responseText;
				if(text == 'FOURMAX') {
					alert('A maximum of 4 products of the same category can be compared at a time');
				}else if(text == 'ALREADY') {
					alert('That product already included for comparison');
				} else {
					document.getElementById('minicompare').innerHTML=ajax.responseText;
				}
                        }
                }
        }
        ajax.open('POST', 'index.n', true);
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        ajax.send('action=script&script=addtocompare&partno='+partno);
}

function checkoutAddress(addressid) {
        ajax = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
        ajax.onreadystatechange = function(){
                if (ajax.readyState == 4){
                        if (ajax.status == 200){
				document.getElementById('address').innerHTML=ajax.responseText;
                        }
                }
        }
        ajax.open('POST', 'index.n');
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        ajax.send('action=script&script=checkoutaddress&addressid='+addressid);
}

function advancedSearch(group) {
        ajax = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
        ajax.onreadystatechange = function(){
                if (ajax.readyState == 4){
                        if (ajax.status == 200){
				text=ajax.responseText;
			//	document.getElementById('search').innerHTML=text;
                        }
                }
        }
        ajax.open('POST', 'index.n', true);
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        ajax.send('action=script&script=advancedsearch&group='+group);
}


function showSubMenu(parentid, childid) {
	var parent = document.getElementById(parentid);
	var child = document.getElementById(childid);
	var left = parent.offsetWidth;
	var top = -2;
	for (; parent; parent = parent.offsetParent) {
		top  += parent.offsetTop;
		left += parent.offsetLeft;
  	}
	child.style.position = "absolute";
	child.style.top = top+'px';
	child.style.left = left+'px';
	child.style.display = "";
}

function showMainMenu(parentid, childid) {
	var parent = document.getElementById(parentid);
        var child = document.getElementById(childid);
        var top = parent.offsetHeight + 3;
        var left = 0;
        for (; parent; parent = parent.offsetParent) {
                top  += parent.offsetTop;
                left += parent.offsetLeft;
        }
        child.style.position = "absolute";
        child.style.top = top+'px';
        child.style.left = left+'px';
        child.style.display = "";
}

function keepMainMenu(menu) {
	clearTimeout(document.getElementById(menu).timer);
	document.getElementById(menu).style.display = "";
}

function destroyMainMenu(menu) {
 	document.getElementById(menu).timer = setTimeout("document.getElementById(menu).style.display = 'none'", 300);
}

function hideSubMenu(menu) {
 	document.getElementById(menu).timer = setTimeout("document.getElementById('"+menu+"').style.display = 'none'", 0);
}

function hideMainMenu(menu) {
 	document.getElementById(menu).timer = setTimeout("document.getElementById('"+menu+"').style.display = 'none'", 300);
}
