  // OriginalBatteries Web Site methods - Ben Jay May 2003  
  var siteTag = 'gb5'; 
  var siteLinkRoot = 'http://www.originalbatteries.co.uk/'; 
  var siteSecRoot = 'https://shop.stormserv.com/gb5/'; 
  var siteRoot = 'http://www.originalbatteries.co.uk/'; 
  var siteServRoot = 'http://shop.stormserv.com/gb5/'; 
  
  // include urchin web analysis:  
  document.write('<script src="/' + siteTag + '/__utm.js"></script>');  
  
  // #########################################################/  
  // check the email address is reasonable:  
  
  function checkEmail(email)  
  {  
    if(!isValidEmail(email))  
    {  
      alert('The email address is invalid.\n It should be similar to:\n\n yourname@yourcompany.com\n\nWith no spaces. Please enter it again.');  
      return false;  
    }  
    return true;  
  }  
  
  // #########################################################/  
  // check the email address is reasonable:  
  
  function isValidEmail(email)  
  {  
    if(isblank(email))  
      return false;  
  
    email = email.toLowerCase();  
  
    //  check for one '@':  
    var sections = email.split('@');  
    if(sections.length != 2)  
      return false;  
  
    // now split into sections on '.':  
    var username = sections[0].split('.');  
    var domain = sections[1].split('.');  
    if(domain.length < 2)  
      return false;  
  
    // check the username:  
    for(var i = 0; i < username.length; i++)  
      if(!isValidEmailSection(username[i]))  
        return false;  
  
    // check the domain:  
    for(var i = 0; i < domain.length; i++)  
      if(!isValidEmailSection(domain[i]))  
        return false;  
  
    return true;  
  }  
  
  // #########################################################/  
  // check this section of an email address is valid  
  // a-z 0-9 - . _  
  
  function isValidEmailSection(sec)  
  {  
    if(sec.length < 1)  
      return false;  
  
    for(var i = 0; i < sec.length; i++)  
    {  
      var cc = sec.charCodeAt(i);  
      if( ((cc < 97) || (cc > 122)) &&  
          ((cc < 48) || (cc > 57)) &&  
          ((cc < 45) || (cc > 46)) &&  
          (cc != 95) )  
  	  return false;  
    }  
    return true;  
  }  
  
  // #########################################################/  
  
  function showCountryList(start, selected)  
  {  
    document.writeln('<SELECT ' + start + '>');  
  
    var countries = [ 'GB','United Kingdom',
'AT','Austria',
'BE','Belgium',
'CZ','Czech Republic',
'DK','Denmark',
'FI','Finland',
'FR','France',
'DE','Germany',
'GR','Greece',
'IE','Ireland',
'IT','Italy',
'LU','Luxembourg',
'NL','Netherlands',
'NO','Norway',
'PT','Portugal',
'SK','Slovakia',
'ES','Spain',
'SE','Sweden' ]; 
  
    for(var i = 0; i < countries.length; i = i+2)  
    {  
      var html = '<OPTION value="' + countries[i] + '"';  
      if(countries[i] == selected)  
        html += ' selected';  
      html += '>' + countries[i+1] + '</OPTION>';  
      document.writeln(html);  
    }  
    document.writeln('</SELECT>');  
  }  
  
  // #########################################################/  
  
  function showYearList(start, selected, full)  
  {  
//    var currentYear = 2004;   
    var currentYear = (new Date()).getFullYear();  
  
    document.writeln('<SELECT ' + start + '>');  
    var maxYear = currentYear + 10;  
    if(full)  
    {  
      maxYear = currentYear + 1;  
      currentYear = currentYear - 5;  
      if(selected == ' ')  
        document.writeln('<OPTION value=" " selected> </OPTION>');  
      else  
        document.writeln('<OPTION value=" "> </OPTION>');  
    }  
  
    for(var i = currentYear; i < maxYear; i++)  
    {  
      var html = '<OPTION value="' + i + '"';  
      if(i == selected)  
        html += ' selected';  
      html += '>' + i + '</OPTION>';  
      document.writeln(html);  
    }  
    document.writeln('</SELECT>');  
  }  
  
  // #########################################################/  
  
  function showMonthList(start, selected, full)  
  {  
    document.writeln('<SELECT ' + start + '>');  
  
    var list = [' ',' ', '01','01', '02','02', '03','03', '04','04', '05','05', '06','06', '07','07', '08','08', '09','09', '10','10', '11','11', '12','12'];  
    var start = 2;  
    var end = list.length -2;  
    if(full)  
      start = start - 2;  
  
    for(var i = start; i <= end; i = i+2)  
    {  
      var html = '<OPTION value="' + list[i] + '"';  
      if(list[i] == selected)  
        html += ' selected';  
      html += '>' + list[i+1] + '</OPTION>';  
      document.writeln(html);  
    }  
    document.writeln('</SELECT>');  
  }  
  
  // #########################################################/  
  // function call, embedded within notes etc in the database:  
  
  function pl(linkOb, linkValue)  
  {  
    linkOb.href=siteLinkRoot + 'b/plink.html?ps=' + linkValue;    
  }  
  
  // #########################################################/  
  // if a number ends with .0, remove it  
  
  function trimdp(str)  
  {  
    if(str.lastIndexOf('.0') == str.length -2)  
      str = str.substring(0, str.length -2);  
  
    document.write(str);  
  }  
  
  // #########################################################/  
  
  function magnitude(str)  
  {  
    num = str.valueOf();  
  
    if(num > 999)  
    {  
      num = num / 1000;  
      str = num.toString() + "K";  
    }  
    else if(num > 999999)  
    {  
      num = num / 1000000;  
      str = num.toString() + "M";  
    }  
  
    document.write(str);  
  }  
  
  // #########################################################/  
  // check there is a value:  
  
  function isblank(str)  
  {  
    for(var i = 0; i < str.length; i++)  
    {  
      var ch = str.charAt(i);  
      if((ch != ' ') && (ch != '\n') && (ch != '\t'))  
        return false;  
    }  
    return true;  
  }  
  
  // #########################################################/  
  // check there is a value:  
  
  function validCardNo(val, min, max)  
  {  
    var count = 0;  
    for(var i = 0; i < val.length; i++)  
    {  
      var ch = val.charAt(i);  
      if((ch == '0') || (ch == '1')|| (ch == '2') || (ch == '3') || (ch == '4') ||   
         (ch == '5') || (ch == '6') || (ch == '7') || (ch == '8') || (ch == '9'))  
        count++;  
      else if(ch != ' ')  
        return false;  
    }  
  
    if((count.valueOf() < min) || ((count.valueOf() > max) && (max > -1)))  
      return false;  
  
    return true;  
  }  
  
  // #########################################################/  
  
  function showHelp(helpTopic)  
  {  
    var win = window.open(siteServRoot+'e/Page.Filler?_page=help/'+helpTopic,'_help','status=no,menubar=no,toolbar=no,resizable=yes,scrollbars=yes, width=650, height=400');  
    win.focus();  
  }  
  
  // #########################################################/  
  
  function stockMessage(level, order, central)  
  {  
    if(level > 0) 
      document.write('In Stock'); 
    else if(order > 0) 
      document.write('On Order'); 
    else if(central > 0) 
      document.write('On Request<BR>(8 Days)'); 
    else 
      document.write('Order On Request'); 
  }  
  
  // #########################################################/  
  
  function report(ob)  
  {  
    reportN(ob, 20);  
  }  
  
  // #########################################################/  
  
  function reportN(ob, num)  
  {  
    mes = '';  
    i = 0;  
    for(a in ob)  
    {  
      mes += "[" + a + " : " + ob[a] + "]\n";  
      i++;  
      if(i > num)  
      {  
        alert(mes);  
        i = 0;  
        mes = "";  
      }  
    }  
    alert(mes);  
  }  
  
  // #########################################################/  
  
  var promos = new Object(); 
  promos.numbers = [ 1,2,3,4,5 ]; 
  promos.imgSrc = [ 'unibat.gif','xdrive.gif','keyboard.gif','travelplug.gif','sleeves.gif' ]; 
  promos.imgAlt = [ 'Universal Batteries. Slim, Mouse Mat Size. 3 to 9 Hours!','External Hard Disks. FireWire &amp; USB2. 60GB Extra Space!','Full size Roll-up Keyboard, USB and PS2, Ideal for Travelling','UK to European, US, NZ and Australian Plug Adapter','Laptop Sleeves. Soft and Cushioned. Fit 12&quot;,14&quot;,15&quot; &amp; 17&quot; Laptops' ]; 
  promos.prodLocator = [ '58570','61579','61343','55166','61583' ]; 
  promos.prodLink = [ 'b/ubp0767b.html','b/ehd0060g.html','b/key0004a.html','b/uni0001a.html','b/bag0017b.html' ]; 
  var promoCount = 0; 
  var promoPsList = []; 
   
  // #########################################################/   
  function writePromo(ps)   
  {   
	if(promos.numbers.length == 0)   
		return;   
   
	// pick 1 as i the first time, then randomise it:  
	var i = 0;  
	if(promoCount > 0)  
	  i = Math.round((Math.random()*promos.numbers.length)-0.5);  
  
   	if(promos.prodLink[i] != '')   
	        document.write('<A id="promolink'+ps+'" href="'+siteLinkRoot+promos.prodLink[i] + '">');  
	document.write('<IMG id="promoimg'+ps+'" src="/'+siteTag+'/i/promo/' + promos.imgSrc[i] + '" height="61" width="200" border="0" alt="' + promos.imgAlt[i] + '">');  
	if(promos.prodLink[i] != '')   
		document.write('</A>');   
	promoCount++;  
	promoPsList[promoPsList.length] = ps;  
  }   
   
  // #########################################################/   
  function startPromos()  
  {  
    if(promoPsList.length > 0)  
      setInterval('changePromos()', 6000);  
  }  
  // #########################################################/   
  function changePromos()  
  {  
	for(n in promoPsList)  
	{  
	  var ps = promoPsList[n];  
	  var i = Math.round((Math.random()*promos.numbers.length)-0.5);  
  	  var el = document.getElementById('promoimg'+ps);  
	  if(el)  
	  {  
	    el.src='/'+siteTag+'/i/promo/' + promos.imgSrc[i]; 
	    el.alt=promos.imgAlt[i]; 
	  } 
  	  el = document.getElementById('promolink'+ps); 
	  if(el) 
	    el.href=siteLinkRoot+promos.prodLink[i]; 
	}  
  }  
  
  // #########################################################/   
  function writePromos(xPrices)   
  {   
	if(promos.numbers.length == 0)  
		return;  
  
	document.write('Amazing deals on accessories when you buy a Battery or Adapter as well:<br>'); 
	for(var i=0;i<promos.numbers.length;i++)  
	{  
      		if(promos.prodLink[i] != '')  
		        document.write('<a href="'+siteLinkRoot+promos.prodLink[i]+'">');  
		document.write('<img id="promoimg'+i+'" src="/'+siteTag+'/i/promo/'+promos.imgSrc[i]+'" alt="'+promos.imgAlt[i]+'">');  
		if(promos.prodLink[i] != '')  
			document.write('</a>');  
	}  
 
	if(xPrices) 
	{ 
		for(var i=0;i<promos.numbers.length;i++)  
		{  
			if(xPrices['x'+promos.prodLocator[i]])  
				document.write('<div id="promotext'+i+'" style="position: absolute; top: 0px; left: 0px;"><a href="javascript:order('+promos.prodLocator[i]+');">Only '+xPrices['x'+promos.prodLocator[i]]+'! &nbsp;&nbsp;&bull; ORDER</a></div>');  
		}  
	} 
  }   
   
  // #########################################################/  
  function arrangePromoText()  
  {  
	if(promos.numbers.length == 0)   
		return;   
  
	for(var i=0;i<promos.numbers.length;i++)   
	{   
		if(document.getElementById('promotext'+i))  
		{  
	    		document.getElementById('promotext'+i).style.top = document.getElementById('promoimg'+i).offsetTop+61;  
    			document.getElementById('promotext'+i).style.left = document.getElementById('promoimg'+i).offsetLeft+20;  
		}  
	}  
  }  
  
  // #########################################################/  
  
  function writeBsi()  
  {  
    var src = 'bsi2.jpg" alt="BSI and ISO9001 Certified"'; 
    if(src.length > 1) 
      document.writeln('<img style="float: right; margin-right: 20px;" src="/'+siteTag+'/i/logos/' + src + '>'); 
  }  
  
  // #########################################################/   
  // check if the servlets have been able to set a Cookie, direct them to an error page if blocked.  
  function cookieCheck()  
  {      
    if(document.cookie.length < 1 && document.location.pathname.indexOf('/e/') != -1 && document.location.search.indexOf('cookies') == -1)  
      document.location = siteLinkRoot + 'info/cookies.html?returl=' + escape(document.location);  
  }  
  cookieCheck();  
  
  // #########################################################/   
  // reload the page which sent them to the Cookie error page.  
  function cookieContinue()  
  {  
    var i = document.location.search.indexOf('returl=');  
    var j = document.location.search.indexOf('&', i);  
    if(j < 0)  
      j = document.location.search.length;  
  
    if(i < 0 || i+7 == j)  
      document.location = 'http://www.originalbatteries.co.uk/'; 
    else  
      document.location = unescape(unescape(document.location.search.substring(i+7, j)));  
  }  
  
  // #########################################################/   
  //--------------------------------------------  
  // link when moving between servers. Using the Urchin monitor to transfer tracking data. If we stop using Urchin, can simply remove the inner utm function call.  
  function svrLink(url)  
  {  
    __utmLinker(url);  
  }  
  
  //--------------------------------------------  
  
//  function order()  
//  {  
//    svrLink(siteLinkRoot + 'e/Shopping.Cart');  
//  }  
  
  //--------------------------------------------  
  
  function order(locator, pno)  
  {  
    if(typeof(locator) == "undefined")  
      svrLink(siteServRoot + 'e/Shopping.Cart');  
    else //if(typeof(pno) == "undefined")  
      svrLink(siteServRoot + 'e/Shopping.Cart?_action=add&p_surrogate=' + locator);   
//    else  
//      svrLink(siteServRoot + 'e/Shopping.Cart?_action=add&p_surrogate=' + locator + '&part_number=' + escape(pno.toUpperCase()));   
  }  
  
  //--------------------------------------------  
  // link to secure site: 
  function secLink(page)  
  {  
    svrLink(siteSecRoot + page);  
  }  
  // link to insecure site: 
  function insLink(page)  
  {  
    svrLink(siteLinkRoot + page);  
  }  
  // link to home page: 
  function rootLink()  
  {  
    svrLink(siteRoot);  
  }  
  
  //-------------------------------------------  
  // page, can include a ?. Current search string and xtra will be appended to the end of the page url.  
  // to use xtra, a non empty page must be give. do not include leading ? or & in the xtra string.  
  
  function pageView(page, xtra)    
  {    
    // if a specific page has been provided, log that:    
    if(typeof(page) != "undefined" && page.length > 0)    
//      urchinTracker(addSearch(page, xtra));    
      urchinTracker(page);    
    
    // is this a page filler call, if so, log the page:    
    var i = document.location.search.indexOf('_page=');    
    if(document.location.pathname.indexOf('Page.Filler') > 0 && i > 0)    
    {    
      var j = document.location.search.indexOf('&', i+6);    
      if(j > 0)    
        page = document.location.search.substring(i+6, j);    
      else    
        page = document.location.search.substring(i+6);    
//      urchinTracker(addSearch(document.location.pathname + '/' + page, xtra));    
      urchinTracker(document.location.pathname + '/' + page);    
    }    
    
    // else, let the tracker figure it out:    
    else    
      urchinTracker();    
  }    
    
  //-------------------------------------------    
  // adds the current locations search params to the specified url for logging, plus the xtra string.  
  // unused : should prevent it adding the _page param, and any Urchin params starting __utmxxxx  
  
  function addSearch(url, xtra)  
  {  
    var search = document.location.search;  
    if(typeof(xtra) != "undefined" && xtra.length > 0)  
    {  
      if(search.length == 0)  
        search = '?' + xtra;  
      else   
        search = search + '&' + xtra;  
    }  
    if(url.indexOf('?') > -1 && search.length > 0)  
    {  
//      alert(url + '&' + search.substring(1));  
      return url + '&' + search.substring(1);  
    }  
    else  
    {  
//      alert(url + search);  
      return url + search;  
    }      
  }  
  
  //-------------------------------------------  
  
  function postForm(f)  
  {  
    __utmLinkPost(f);  
    f.submit();  
  }  
  
  //------------------------------------------- 
  // modify the height of the select box to use up and free space. 
 
  function adjustHeight() 
  { 
    if(typeof document.body.clientHeight == 'undefined')   
      return;       // clientHeight is not supported by old netscape 
 
//alert('document.body.clientHeight '+document.body.clientHeight); 
 
    if(document.body.clientHeight > 2000)  
      return;       // prevents runaway height bug while resizing in ie6 
 
    var selOb = document.getElementsByName('sellist').item(0); 
    var pageHeight = document.body.clientHeight; 
 
    var newHeight = pageHeight - extraHeight; 
    if(newHeight < initialSelectHeight) 
      newHeight = initialSelectHeight; 
 
    selOb.style.height = newHeight; 
  } 
 
  //------------------------------------------- 
 
 
 
    // create a link to the live page: 
 
    function liveLink(root) 
    { 
      var pageLoc = this.location.pathname; 
      var statI = pageLoc.indexOf('/'+root+'/'); 
      pageLoc = pageLoc.substring(statI); 
      return '/'+siteTag+'/e/Page.Filler?_page=' + pageLoc; 
    } 
  //------------------------------------------- 
 
    function showLiveLink(isLive, root) 
    { 
      // should use location.replace() rather than setting the location directly, to prevent the back button entering a loop. 
 
      if(!isLive) 
        document.write('<A class="button_flash" href="javascript:svrLink(\'' + liveLink(root) + '\');"><IMG src="/'+siteTag+'/i/icons/button_flash.gif" align="absmiddle" width="16" height="16" border="0"> View Live Price &amp; Availability</A><BR>'); 
    } 
    // ---------------------------------------- 
 
    function showLive(isLive, txt) 
    { 
      if(isLive) 
        document.write(txt); 
    }  
    // ---------------------------------------- 
 
    function showLiveStock(isLive, level, order, central) 
    { 
      if(isLive) 
        stockMessage(level, order, central) 
    } 
    // ---------------------------------------- 
 
    function customPrice(currency, amount) 
    { 
      if(currency == 'GBP') 
        return ''; 
 
      var html = '<TR>' + 
'                 <TD class="empty" colspan="2">&nbsp;</TD>' + 
'  	          <TD class="unit2"><IMG src="/'+siteTag+'/i/lcd/lcd_' + currency + '.gif" width="24" height="22"></TD>' +  
'    	          <TD class="value2">' + amount + '&nbsp;</TD>' + 
'                </TR>'; 
      return html; 
    } 
    // ---------------------------------------- 
    // modify the height of the tiles to match the tallest. 
    function adjustTileHeight()  
    {  
      if(typeof document.body.clientHeight == 'undefined')    
        return;       // clientHeight is not supported by old netscape  
 
      var offHeight=0; 
      var divs=document.getElementsByTagName('div'); 
      for(var i=0;i<divs.length;i++) 
      { 
        var div=divs[i]; 
        if(div.id.substr(0,5)=='tile-' && div.offsetHeight>offHeight) 
          offHeight=div.offsetHeight; 
      }         
      for(var i=0;i<divs.length;i++) 
      { 
        var div=divs[i]; 
        if(div.id.substr(0,5)=='tile-') 
          div.style.height=offHeight+'px'; 
      }         
    }  
    //-------------------------------------------  
 
    function postPromos()  
    {  
      if(promos.numbers.length==0) 
        return; 
 
      var el=document.getElementById('promotd');  
      var ch=el.clientHeight;  
//    var ch=document.body.clientHeight;  
  
      // clientHeight is not supported by old netscape   
      if(typeof ch=='undefined')  
        return;  
//      alert(ch);  
//      ch=ch-230;  
      if(ch<100)  
        return;  
  
//      alert(ch);  
      var max=Math.floor(ch/85);  
//      alert(max);  
      if(max > promos.numbers.length)  
        max=promos.numbers.length;  
      var html='';  
 
 
      var i=Math.round((Math.random()*promos.numbers.length)-0.5); 
      var count=0; 
      while(count<max) 
      { 
        if(promos.prodLink[i]!='')    
          html+='<a href="'+siteLinkRoot+promos.prodLink[i]+'">';    
        html+='<img id="promoimg'+i+'" src="/'+siteTag+'/i/promo/'+promos.imgSrc[i]+'" alt="'+promos.imgAlt[i]+'">';    
        if(promos.prodLink[i]!='')    
        html+='</a>';    
 
        i++; 
        if(i==promos.numbers.length) 
          i=0; 
        count++; 
      } 
      el.innerHTML=html;  
    }  
    //-------------------------------------------   
 

