// JavaScript Document
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

var theTimer;

function showMenu(theDivToShow) {
  MM_swapImgRestore();
  MM_showHideLayers(theDivToShow,'','show');
	 if (theDivToShow != 'DivNewProducts') {
	   MM_showHideLayers('DivNewProducts','','hide');
	 }
	 if (theDivToShow != 'DivPreOwned') {
	   MM_showHideLayers('DivPreOwned','','hide');
	 }
}

function startTimer() {
  theTimer = setTimeout('timeOut()',300);
}

function resetTimer() {
 clearTimeout(theTimer);
}

function timeOut() {
 MM_showHideLayers('DivNewProducts','','hide');
 MM_showHideLayers('DivPreOwned','','hide');
 MM_swapImgRestore();
}

function replace(originalsentence,oldword,newword) {
  var newsentence='', walk=0, index=0, first='', last='';

  newsentence = '' + originalsentence;
  walk=0;
  index=newsentence.indexOf(oldword,walk);
      
  while( (walk<newsentence.length) && (index!=(-1)) ) {
    first=newsentence.substring(0,index);
    last=newsentence.substring(index+oldword.length,newsentence.length);
    newsentence=first + '' + newword + '' + last;

    walk=index+newword.length;
    index=newsentence.indexOf(oldword,walk);
  }
  return(newsentence);
}

function EmptyCart(urlString) {
  var link = 'cart_action.asp?' + urlString + '&emptycart=1&updateurl=0';
  if (confirm('Are you sure you want to empty your cart?')) {
    document.location.href = link;
  }
}

//  anchor name, resting graphic, over graphic, alternate text
var featuresArray = new Array(
  ['granny','grannys_rest.gif','grannys_over.gif','Granny\'s'], // Feature 1
  ['krispy','krispy_rest.gif','krispy_over.gif','Krispy Kreme'], // Feature 2
  ['mama','mama_rest.gif','mama_over.gif','Mama'], // Feature 3
  ['saralee','saralee_rest.gif','saralee_over.gif','SaraLee'], // Feature 4
  ['tasty','tasty_rest.gif','tasty_over.gif','TastyKake'] // Feature 5 
);

function selectFeature(URL,featureToInsert,featureLocation) {
  if (featureToInsert == 0) {
    var loop = 0;
    var notTaken = 1;
    while (notTaken != 0) {
      featureToInsert = rand(featuresArray.length);
      while ((featureToInsert != myFeatures[loop]) && (loop < 5)) {
        loop++
      }
      if (loop >= 5) {
        notTaken = 0;
        myFeatures[featureLocation-1] = featureToInsert;
      } else {
        loop = 0;
      }
    }
  }
  if ((featureToInsert <= featuresArray.length) && (featureToInsert > 0)) {
    document.writeln('<a href="store.asp?' + URL + '&page_id=53#' + featuresArray[featureToInsert-1][0] + '" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage(\'logo' + featureLocation + '\',\'\',\'/prototype/graphics/logos/' + featuresArray[featureToInsert-1][2] + '\',1)"><img src="/prototype/graphics/logos/' + featuresArray[featureToInsert-1][1] + '" alt="' + featuresArray[featureToInsert-1][3] + '" name="logo' + featureLocation + '" border="0"></a>');
  } else { alert('You have entered a feature that does not exist. Please correct the features.js page'); }
}

rnd.today = new Date();
rnd.seed = rnd.today.getTime();

function rnd() {
  rnd.seed = (rnd.seed*9301+49297) % 233280;
  return rnd.seed/(233280.0);
}

function rand(number) {
  return Math.ceil(rnd()*number);
}

function GetSelectedValue(selObj) {
  var selectedIndex = selObj.options.selectedIndex;
  if (selectedIndex < 0) {
    return '';
  } else {
    return selObj.options[selObj.options.selectedIndex].value;    
  }
}