 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;
 }
 



// ======================================================

var httpRQ = false;
var divID;
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(bid,pid) {
try {
      var regex1 = /btn/g;
      var tid = bid.replace(regex1,'txt');
      //alert(tid);
      var txt = document.getElementById(tid);
      divID = bid.replace(regex1,'div');
      //alert(txt.value);
	   var my_date = new Date();
	   var params = "?pid=" + pid + "&q=" + txt.value + "&nocache=" + my_date.getTime();
	   httpRQ.open("GET", "ajaxbasket.aspx"+params, true); 
	   httpRQ.onreadystatechange = updateDiv; // cannot pass parameters to this funcvtion, apparently...
	   httpRQ.send(null);
	   //return false;
   } catch(e) {
     //alert('1. '+e);
   }
}

function updateDiv() {
  if (httpRQ.readyState == 4) {
    try {
        var regex2 = /$/g;
        divID = divID.replace(regex2,'_');
        divID = divID.substring(0,divID.length-1);
        //alert(divID);
        var response = httpRQ.responseText;
        document.getElementById(divID).innerHTML = response;
        //document.getElementById("divCode").innerHTML = response.replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/\n/g,'<br>');
       //location.href = '#here';
    }catch(e){
      // alert('2. '+e);
    }
  }
}




// ******************************************************

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;			  			  	  
              }		 		 
         }


