 function getFront(m,s) {
   var f = m.indexOf(s);
   if (f <= 0) {
     return null;
   } else {
     return m.substring(0,f);
   }
 }
  
 function getTail(m,s) {
   var f = m.indexOf(s);
   if (f == -1) {
     return null;
   } else {
     if (f+s.length == m.length) {
       return null;
     } else {  
       return m.substring(f+s.length,m.length);
     }  
   }
 }

 function replaceString(y,s,r) {
   var f = getFront(y,s);
   var t = getTail(y,s);
   if (f == null && t == null) {
    return r;
   } else if (t == null) {
     return f + r;
   } else if (f == null) {
     return r + t;
   } else {
     return f + r + t;
   }
 }

 function insertString(m,s,i) {
  // inserts i immediately before s
   var f = getFront(m,s);
   var t = getTail(m,s);
   if (f != null && t != null) {
     return f + i + s + t;
   }
 }

 function deleteString(x,d) {
  return replaceString(x,d,'');
 }
    
 function readCookie() {
   var start = document.cookie.indexOf('storagesolutions=');
   if (start == -1) {
     return '';
   } else {
     start = document.cookie.indexOf('=',start) + 1;
     var end = document.cookie.indexOf(';',start);
     if (end == -1) {end = document.cookie.length;}
     var value = document.cookie.substring(start,end);
     return value;
   }  
  }

 function addItem(p) {
  var s;
  try{
    s = readCookie();
  }catch(e){
    s = '';
  }

  if ( s.indexOf(p) == -1 ) {
    // add to cookie
    if (s == '' || s == '.') {
       document.cookie = 'storagesolutions=.' + p + '.';
    } else {
       document.cookie = 'storagesolutions=.' + p + s;
    }
  }
  alert(readCookie());
  return true;
 }
 

  function doShop(i) {
     try {
         var b = addItem(i);
		alert('Thank you - you will specify the quantity required at the checkout stage.\nThe item has been added to your shopping-cart.');
	} catch(e) {
		alert('There has been a problem.\nPlease ensuer that cookies are enabled in your browser for this site ad try again.');
	}  
}

function doCart(bid,pid) {
    try {
         var tid = replaceString(bid,'btn','txt');
         var txt = document.getElementById(tid);
         var b = addItem(pid+'-'+txt.value);
         alert("Thank you - " + txt.value + ' ' + pid + " have been added to your cart");
         //alert(txt.value);
    }catch(e){
		alert('There has been a problem.\nPlease ensuer that cookies are enabled in your browser for this site ad try again.');
    }
}

// ======================================================

var httpRQ = false;
try {
  httpRQ = new XMLHttpRequest();
} catch (trymicrosoft) {
  try {
    httpRQ = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (othermicrosoft) {
    try {
      httpRQ = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (failed) {
      httpRQ = false;
    }
  }
}

if (!httpRQ)
  alert("Error initializing XMLHTTP object!");



function writeCart(pid,qty) {
try{
    var regex = /^\d+$/;
	var q = document.getElementById(qty);
	if ( !regex.test(q.value) || q.value==0 ) {
	   alert('Please enter a number larger than 0');
	} else {
	  var my_date = new Date();
	  var params = "?pid="+pid.toString()+"&q="+q.value+"&nocache="+my_date.getTime();
	  httpRQ.open("GET", "ajaxwritecart.aspx"+params, true); 
	  httpRQ.onreadystatechange = updateDiv; // cannot pass parameters to this funcvtion, apparently...
	  httpRQ.send(null);
	}
}catch(e){
alert('1. '+e);

}
}

function updateDiv() {
  if (httpRQ.readyState == 4) {
    try{
        if (httpRQ.readyState  == 4) {
          var response = httpRQ.responseText;
		  if (response=='') {
		    location.href = 'basket.aspx';
		  } else {
            alert(response);
		  }
        } else {
          alert(httpRQ.status);
        }

    }catch(e){
      alert('2. ');
    }
  }
}


// ******************************************************

function setCookie(name, value)
         {
         //If name is the empty string, it places a ; at the beginning
         //of document.cookie, causing clearCookies() to malfunction.
         if(name != '')
            document.cookie = name + '=' + value;
         }

function getCookie(name)
         {
         //Without this, it will return the first value 
         //in document.cookie when name is the empty string.
         if(name == '')
            return('');
         
         name_index = document.cookie.indexOf(name + '=');
         
         if(name_index == -1)
            return('');
         
         cookie_value =  document.cookie.substr(name_index + name.length + 1, 
                                                document.cookie.length);
         
         //All cookie name-value pairs end with a semi-colon, except the last one.
         end_of_cookie = cookie_value.indexOf(';');
         if(end_of_cookie != -1)
            cookie_value = cookie_value.substr(0, end_of_cookie);

         //Restores all the blank spaces.
         space = cookie_value.indexOf('+');
         while(space != -1)
              { 
              cookie_value = cookie_value.substr(0, space) + ' ' + 
              cookie_value.substr(space + 1, cookie_value.length);
							 
              space = cookie_value.indexOf('+');
              }

         return(cookie_value);
         }

function clearCookie(name)
         {                  
         expires = new Date();
         expires.setYear(expires.getYear() - 1);

         document.cookie = name + '=null' + '; expires=' + expires; 		 
         }
         
function clearCookies()
         {
         Cookies = document.cookie;
         Cookie = Cookies;
         expires = new Date();
         expires.setYear(expires.getYear() - 1);

         while(Cookie.length > 0)
              {
              //All cookie name-value pairs end with a semi-colon, except the last one.
              Cookie = Cookies.substr(0, Cookies.indexOf(';'));
              Cookies = Cookies.substr(Cookies.indexOf(';') + 1, Cookies.length);

              if(Cookie != '')
                 document.cookie = Cookie + '; expires=' + expires;
              else
                 document.cookie = Cookies + '; expires=' + expires;			  			  	  
              }		 		 
         }


