function getElementsByClassName(clsName, source){
   if (source == undefined) {
      source = document;
   }

   var retVal = new Array();
   var elements = document.getElementsByTagName("*");

   for (var i=0;i < elements.length;i++) {
      if (elements[i].className.indexOf(" ") >= 0) {
      
         var classes = elements[i].className.split(" ");
         for (var j = 0;j < classes.length;j++) {
            if (classes[j] == clsName) { retVal.push(elements[i]); }
         }
      }
      else if(elements[i].className == clsName) {
         retVal.push(elements[i]);
      }
   }
   return retVal;
}

var Cookie = {

   set : function(name, value, days) {
      // Default to a 1 year cookie
      if (days == undefined) { days = 365; }

      // Format date string
      var date = new Date();
      date.setTime(date.getTime() + (days * 86400000));

      // Set cookie name, value, and expiration date
      document.cookie = name + "=" + value + "; expires=" + date.toGMTString() + "; path=/";
   },

   get : function(name) {
      // Find the cookie's value in the document cookie string
      var results = document.cookie.match(
         new RegExp("(?:^|; )" + name + "=" + "(.*?)(?:$|;)")
      );

      // Return the value if a match was found, undefined otherwise
      if (results && results.length > 1) return results[1];
      return undefined;
   },

   clear : function(name) {
      // Erase a cookie
      Cookie.set(name, "", -1);
   }

};

var splits = getElementsByClassName("split");

if (splits.length > 0) {
   // Look at the first split test and figure out how many splits we have in there.
   var firstSplit = splits[0];
   var splitSize = firstSplit.getElementsByTagName("span").length;
}

// Choose a random split number, i.e. 0 to 3
var currentSplit;


if (currentSplit = Cookie.get("split")) {
   currentSplit = parseInt(currentSplit);
}
else {
   currentSplit = Math.floor(Math.random() * splitSize);

   // Set a cookie and lock us into the current split
   Cookie.set("split", currentSplit);

   // Hit the PHP backend with our split...
   var img = new Image();
   img.src = "http://www.solarpower-kits.com//split.php?view=1";
}

for (var i=0; i<splits.length; i++) {
   var split = splits[i];

   var subSplits = split.getElementsByTagName("span");

   // Make all subsplits invisible
   for (var a=0; a<subSplits.length; a++) {
      subSplits[a].style.display = 'none';
   }

   // Make our chosen split visible
   subSplits[currentSplit].style.display = 'block';
}

