// CONFIGURATION
// -----------------------------------------------------------------------------
// Message to display
var g_strFlyerMsg     = "Just click on any item to automatically\nadd it to your shopping list.\n\nIf you need an item that is not in this ad, you\ncan type it on your printable shopping list in\n the NOTES section.\n\nTo go to your shopping list, just click on the\nView Shopping List link.";
var g_strDirectoryMsg = "This page is a list of items you may want\nto add to your grocery list.\n\nJust click on any item to automatically\nadd it to your shopping list.\n\nIf you need an item that is not on this list, you\ncan type it on your printable shopping list in\nthe NOTES section.\n\nTo go to your shopping list, just click on the\nView Shopping List button.\n\n\n           EVERYDAY LOW PRICES"

// Shopping list expiration time span, in days
var g_nShopListExpirationDays = 100;

// FUNCTIONS:
// -----------------------------------------------------------------------------

//
// Displays a message once the flyer page is loaded
//
// If bShowOnce is true - show once per session, otherwise show every time page loads
//
function Flyer_ShowMessage(str, bShowOnce){
   if(!bShowOnce || !GetCookie('flyer_msg')){
      if(str != ''){ alert(str); }
      SetCookie('flyer_msg', 1);
   }
}


//
// Sets a cookie
// Parameters:
//    expires_sec - time interval when cookie should expire, in seconds
//
function SetCookie(name, value, expires_sec, path, domain, secure){
   var now = new Date();
   now.setTime(now.getTime());

   if(expires_sec){
      expires_sec = expires_sec * 1000;
   }
   var expires_date = new Date(now.getTime() + (expires_sec));

   document.cookie = name + "=" + escape(value) +
                     ((expires_sec) ? ";expires=" + expires_date.toGMTString() : "") +
                     ((path)    ? ";path=" + path : "") +
                     ((domain)  ? ";domain=" + domain : "") +
                     ((secure)  ? ";secure" : "");
}


//
// Gets a cookie value
//
function GetCookie(name){
   var start = document.cookie.indexOf(name + "=");
   var len   = start + name.length + 1;
   if((!start) && (name != document.cookie.substring(0, name.length)))
   {
      return null;
   }
   if(start == -1){ return null; }

   var end = document.cookie.indexOf(";", len);
   if(end == -1){
      end = document.cookie.length;
   }

   return unescape(document.cookie.substring(len, end));
}


//
// Deletes a cookie
//
function DeleteCookie(name, path, domain){
   if(GetCookie(name)){
      document.cookie = name + "=" +
                        ((path)   ? ";path=" + path : "") +
                        ((domain) ? ";domain=" + domain : "") +
                        ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
   }
}


g_Days = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
g_Months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");

function ShowDate(){
   var mydate = new Date();
   var year  = mydate.getFullYear();
   var day   = mydate.getDay();
   var month = mydate.getMonth();
   var daym  = mydate.getDate();
   if (daym < 10){ daym = "0" + daym; }
   var hours   = mydate.getHours();
   var minutes = mydate.getMinutes();
   var dn = "AM";
   if (hours >= 12){ dn = "PM"; hours = hours - 12; }
   if (hours == 0){ hours = 12; }
   if (minutes <= 9){ minutes = "0" + minutes; }
   document.write(g_Days[day],", ",g_Months[month]," ",daym,", ",year);
}


// cookieForms saves form content of a page.
//
// use the following code to call it:
//  <body onLoad="cookieForms('open', 'form_1', 'form_2', 'form_n')" onUnLoad="cookieForms('save', 'form_1', 'form_2', 'form_n')">
//
// It works on text fields and dropdowns in IE 5+
// It only works on text fields in Netscape 4.5

function cookieForms() {  
   var mode = cookieForms.arguments[0];

   for(f=1; f<cookieForms.arguments.length; f++) {
      formName = cookieForms.arguments[f];
      
      if(mode == 'open') { 
         cookieValue = GetCookie('saved_'+formName);
         if(cookieValue != null) {
            var cookieArray = cookieValue.split('#cf#');
            
            if(cookieArray.length == document[formName].elements.length) {
               for(i=0; i<document[formName].elements.length; i++) {

                  if(cookieArray[i].substring(0,6) == 'select') { document[formName].elements[i].options.selectedIndex = cookieArray[i].substring(7, cookieArray[i].length-1); }
                  else if((cookieArray[i] == 'cbtrue') || (cookieArray[i] == 'rbtrue')) { document[formName].elements[i].checked = true; }
                  else if((cookieArray[i] == 'cbfalse') || (cookieArray[i] == 'rbfalse')) { document[formName].elements[i].checked = false; }
                  else { document[formName].elements[i].value = (cookieArray[i]) ? cookieArray[i] : ''; }
               }
            }
         }
      }
            
      if(mode == 'save') { 
         cookieValue = '';
         for(i=0; i<document[formName].elements.length; i++) {
            fieldType = document[formName].elements[i].type;
            
            if(fieldType == 'password') { passValue = ''; }
            else if(fieldType == 'checkbox') { passValue = 'cb'+document[formName].elements[i].checked; }
            else if(fieldType == 'radio') { passValue = 'rb'+document[formName].elements[i].checked; }
            else if(fieldType == 'select-one') { passValue = 'select'+document[formName].elements[i].options.selectedIndex; }
            else { passValue = document[formName].elements[i].value; }
         
            cookieValue = cookieValue + passValue + '#cf#';
         }
         cookieValue = cookieValue.substring(0, cookieValue.length-4); // Remove last delimiter
         
         var dtExp = new Date(); 
         dtExp.setTime(dtExp.getTime() + (g_nShopListExpirationDays*24*60*60*1000));

         SetCookie('saved_'+formName, cookieValue, dtExp);    
      }  
   }
}





 var tableheadcolor = "#FFFFFF"; //Table Header and Footer
 var tablebodycolor = "#FFFFFF"; //Body Color
 var tablefontcolor = "#ff0000"; //Font Color

// buyItem - adds an item to the shopping list
function buyItem(newItem, newPrice, newQuantity) {
   if (newQuantity <= 0) {
      //rc = alert('The quantity entered is incorrect');
   } else {
      if (confirm('Add '+newItem+' To Your Shopping List?')) { //
         var CookieName = "OnAdThisWeek";
         var cookieFound = false;
         var start = 0;
         var end = 0;
         var cookieString = document.cookie;
         var i = 0;
         while (i <= cookieString.length) {
           start = i;
           end = start + CookieName.length;
           if (cookieString.substring(start,end) == CookieName) {
             cookieFound = true;
            break;
           }
           i++;
         }
         if (cookieFound) {
           start = end + 1;
           end = document.cookie.indexOf(";",start);
           if (end < start) end = document.cookie.length;
           document.cookie.substring(start,end);
         }
         document.cookie="OnAdThisWeek="+document.cookie.substring(start, end)+"["+newItem+"|"+newPrice+"|"+newQuantity+"]; expires=Friday, 31-Dec-2010 08:00:00 GMT";
         //   window.location.href=document.location.event.target;
      }
   }
}

//Show shoppinglist.html contents
function showbasket() {
   var aisle;
    var CookieName = "OnAdThisWeek";
     var cookieFound = false;
     var start = 0;
     var end = 0;
     var cookieString = document.cookie;
     var i = 0;
     while (i <= cookieString.length) {
       start = i;
       end = start + CookieName.length;
       if (cookieString.substring(start,end) == CookieName) {
         cookieFound = true;
        break;
       }
       i++;
     }
     if (cookieFound) {
       start = end + 1;
       end = document.cookie.indexOf(";",start);
       if (end < start)
         end = document.cookie.length;
       document.cookie.substring(start,end);
     }
      fulllist = document.cookie.substring(start, end);
      totprice = 0;
      document.writeln('<FORM NAME="updateform">');

      document.writeln('<TABLE WIDTH="100%" CELLSPACING=0 CELLPADDING=2 BGCOLOR="#FFFFFF" >'); //BGCOLOR="' + tablebodycolor + '"
      document.writeln('<TR bgcolor=' + tableheadcolor + '><TD COLSPAN=3 align="center"><h3>Shopping List</h3></TD></TR>');

      // REWRITE
      if(!fulllist || fulllist == 'null'){ 
         document.write('<tr><td colspan="3" align="center"><br>Your shopping list is empty<br></td></tr>');

      } else {
         document.writeln('<TR bgcolor=' + tableheadcolor + '><TD WIDTH="16"></TD><TD WIDTH="100%"></TD>');                 //<TD><B>Special</B></TD> <B>Location</B>
         document.writeln('<TD WIDTH="20"><B>Delete</B></TD></TR>');
         itemlist = 0;
         for (var i = 0; i <= fulllist.length; i++) {
            if (fulllist.substring(i,i+1) == '[') {
               itemstart = i+1;
               thisitem = 1;
            } else if (fulllist.substring(i,i+1) == ']') {
               itemend = i;
               thequantity = fulllist.substring(itemstart, itemend);
               itemtotal = 0;
               itemtotal = (eval(theprice*thequantity));
               temptotal = itemtotal * 100;
               totprice = totprice + itemtotal;
               itemlist=itemlist+1;
               document.write('<TR><TD STYLE="border-bottom:1px solid #999"  WIDTH=20 VALIGN="BOTTOM"><select><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option><option>6</option><option>7</option><option>8</option><option>9</option><option>10</option><option>11</option><option>12</option></select></TD>');
               document.write('<TD STYLE="border-bottom:1px solid #999" width=550 VALIGN="middle">'+theitem+'</TD>');  //<IMG SRC="Images/check.gif" BORDER=0 height=15 align="top">
               document.write('<TD STYLE="border-bottom:1px solid #999" VALIGN="middle"><FONT SIZE=1><a href="javascript:removeItem('+itemlist+')"<CENTER><IMG SRC="Images/cross.gif" BORDER=0 height=15 valign="middle"></A></FONT></center></TD></TR>'); //<TD ALIGN=CENTER>'+thequantity+'</TD>

            } else if (fulllist.substring(i,i+1) == '|') {
               if (thisitem==1) theitem = fulllist.substring(itemstart, i);
               if (thisitem==2) theprice = fulllist.substring(itemstart, i);
               thisitem++;
               itemstart=i+1;
            }
         }
      }

      document.writeln('</TABLE>');
      document.write('<br><br><TABLE width="100%"><TR><TD COLSPAN=2 align="center"><input type="button" value="Print This Page" onclick="printPage()">&nbsp;&nbsp;&nbsp;<input type="button" value="Clear List" onClick="removeall();"></TD></TR></TABLE>');
      document.writeln('</FORM>');
   }


function printPage() {
  if (window.print) 
    window.print()   
   else
    alert("Oops!\nYour browser doesn't support this feature, but don't worry.\nJust press 'Ctrl' and 'P' simultaneously to print this page.");
}



//Remove item from shoppinglist.html
function removeItem(itemno) {
      newItemList = null;
      itemlist = 0;
      for (var i = 0; i <= fulllist.length; i++) {
         if (fulllist.substring(i,i+1) == '[') {
            itemstart = i+1;
         } else if (fulllist.substring(i,i+1) == ']') {
            itemend = i;
            theitem = fulllist.substring(itemstart, itemend);
            itemlist=itemlist+1;
            if (itemlist != itemno) {
               newItemList = newItemList+'['+fulllist.substring(itemstart, itemend)+']';
            }
         }
      }
      index = document.cookie.indexOf("OnAdThisWeek");
      document.cookie="OnAdThisWeek="+newItemList+"; expires=Friday, 31-Dec-2010 08:00:00 GMT";
      self.location = "shoppinglist.html";
      }

//Remove item from shoppinglist.html
function removeall(itemno) {
      newItemList = null;
      itemlist = 0;
      for (var i = 0; i <= fulllist.length; i++) {
         if (fulllist.substring(i,i+1) == '[') {
            itemstart = i+1;
         } else if (fulllist.substring(i,i+1) == ']') {
            itemend = i;
            theitem = fulllist.substring(itemstart, itemend);
            itemlist=itemlist+1;
            if (itemlist != itemno) {
               newItemList = newItemList+'['+fulllist.substring(itemstart, itemend)+']';
            }
         }
      }
      index = document.cookie.indexOf("OnAdThisWeek");
      document.cookie="OnAdThisWeek="+newItemList+"; expires=Friday, 31-Dec-2000 08:00:00 GMT";

      self.location = "shoppinglist.html";
      }
