
var i = 0;
var lastCat    = '';
var lastCoupon = '';
var lastBus    = '';
var eitheror   = 'category';
var county   = 'calvert';


function couponData(cat, name, img, web){
  this.cat      = cat;
  this.name     = name;
  this.web      = web;
  this.img = img.split(",");

  return this;
}
var bus;


var calvertData = new Array(
new couponData('Home Improvements', 'Continental Heating & Air ', "continentalheatingair_1.png,continentalheatingair_2.png", 'www.continentalservices.net'),
new couponData('Restaurants', 'Mexico Restaurant ', "mexicorestaurant_1.png,mexicorestaurant_2.png", ''),
new couponData('Home Improvements', 'O’Hennon Builders ', "ohennonbuilders_1.png,ohennonbuilders_2.png", 'www.ohennon.com'),
new couponData('Gas', 'Town Center Amoco ', "towncenteramoco_1.png,towncenteramoco_2.png", ''),
new couponData('Community', 'Annmarie Garden - Gift Shop', "annmariegardengiftshop_1.png", 'www.annmariegarden.org'),
new couponData('Community', 'Annmarie Garden - Studio School', "annmariegardenstudioschool_1.png", 'www.annmariegarden.org'),
new couponData('Community', 'Bayside History Museum', "baysidehistorymuseum_1.png", 'www.baysidehistorymuseum.org'),
new couponData('Car Dealers', 'Bortnick Ford ', "bortnickford_1.png", 'www.ronbortnickford.com'),
new couponData('Community', 'Calvert Memorial Hospital ', "calverthospital.png", 'www.calverthospital.com'),
new couponData('Community', 'Calvert Marine Museum ', "calvertmarinemuseum_1.png", 'www.calvertmarinemuseum.com'),
new couponData('Eye Care', 'Chesapeake Bay Optical ', "chesapeakebayoptical_1.png", ''),
new couponData('Home Improvements', 'Chesapeake Sealcoating ', "chesapeakesealcoating_1.png", 'www.chesapeakesealcoating.com'),
new couponData('Health and Wellness', ' Creation’s', "creationshairsalon_1.png", 'creationshairsalonowings.com'),
new couponData('Retail Stores', 'Educate & Celebrate ', "educatecelebrate_1.png", 'www.learning-experts.com'),
new couponData('Home Improvements', 'Floor Systems ', "floorsystems_1.png", 'WWW.FLOORSYSTEMS.NET'),
new couponData('Community', 'Calvert Hospice ', "hospiceshoppe_1.png", 'www.calverthospice.org'),
new couponData('Home Improvements', 'Interiors by Decorating Den ', "interiorsbydecoratingden_1.png", ''),
new couponData('Retail Stores', 'J.W. Jewelers ', "j.w.jewelers_1.png", ''),
new couponData('Community', 'Kelly McConkey ', "kellymcconkey_1.png", 'www.mcconkeyforcommissioner.com'),
new couponData('Lawyers', 'Law Offices of Julie ', "lawofficesofjulie_1.png", 'www.JASEstateLaw.com'),
new couponData('Home Improvements', 'Masterpiece Refinishing ', "masterpiecerefinishing_1.png", 'www.masterpiece-refinishing.com'),
new couponData('Restaurants', 'Papa John’s -Calvert', "papajohns_calvert_1.png", 'www.papajohnsbeachboyspizza.com      '),
new couponData('Restaurants', 'Papa John’s - St. Marys', "papajohns_stmarys_1.png", 'www.papajohnsbeachboyspizza.com      '),
new couponData('Eye Care', 'Pearle Vision - North', "pearlevision_north_1.png", ''),
new couponData('Eye Care', 'Pearle Vision - St. Marys', "pearlevision_stmarys_1.png", ''),
new couponData('Printing', 'Prince Frederick Graphics & Printing ', "princefrederickgraphicsprintingcenter_1.png", ''),
new couponData('Restaurants', 'Quizno’s ', "quiznos_1.png", 'www.quiznos.com'),
new couponData('Home Improvements', 'Serve Pro ', "servpro_1.png", 'www.greenvans.com'),
new couponData('Home Improvements', 'Southern Maryland Windows ', "southernmarylandwindows_1.png", 'www.smws.net'),
new couponData('Home Improvements', 'Stanley Steemer ', "stanleysteemer_1.png", ''),
new couponData('Home Improvements', 'Stoney’s Seafood ', "stoneysseafood_1.png", 'www.stoneysprincefrederick.com'),
new couponData('Restaurants', 'Stuff 4 Sale ', "stuff4sale_1.png", 'www.stuff4saleusa.com'),
new couponData('Community', 'Sue Kullen ', "suekullenssummerfundraiser_1.png", ''),
new couponData('Retail Stores', 'The Curved Closet ', "thecurvedcloset_1.png", '')
);
var calvertCategory = new Array (
"Car Dealers",
"Community",
"Eye Care",
"Gas",
"Health and Wellness",
"Home Improvements",
"Lawyers",
"Printing",
"Restaurants",
"Retail Stores"
);
var charlesData = new Array(

);
var charlesCategory = new Array (

);
var stmarysData = new Array(

);
var stmarysCategory = new Array (

);

var nowArray = calvertData;
var nowCat   = calvertCategory;

var busArray   = new Array(calvertData.length);
var alpha = new Array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");




function disCoupon(coupon, cat){
  var string ='';
  var webLink = '';

  for(i=0; i < nowArray.length; i++){
    if(coupon == nowArray[i].name ){

      get(nowArray[i].name,county);
      string += '<h3><u>Click coupon for a printable version</u> | ';
      if(cat == 'business'){
        string += '<a href="#" onclick="disBus(\'' + bus + '\');">Back</a></h3>';
      } else {
        string += '<a href="#" onclick="disCat(\'' + cat + '\');">Back</a></h3>';
      }

      for (j=0; j<nowArray[i].img.length; j++){

        string += '<a href="coupons/'+ nowArray[i].img[j] + '" border=0 target="_blank">';
        string += '<img border=0 src="couponsThumb/'+ nowArray[i].img[j] + '" width="600"><br>';
        string += '</a>';

      }
      string += '<br>';
      if(nowArray[i].web) 
          string += '<a href="http://' + nowArray[i].web + '" target="_blank">Click here to visit the website</a> | ';
    }
  }
      

      if(cat == 'business'){
        string += '<a href="#" onclick="disBus(\'' + bus + '\');">Back</a>';
      } else {
        string += '<a href="#" onclick="disCat(\'' + cat + '\');">Back</a>';
      }
      string += '<br><br>';
      document.getElementById('printCoupons').innerHTML = string;


}
function disInfo(cat){
  if(lastCat)  
    document.getElementById(lastCat).innerHTML = '<a href="#" onclick="disInfo(\'' + lastCat + '\')">' + lastCat + '</a>';
  var string = '';
  var i;
  string += '<u>' + cat +'</u><br>';
  for(i=0; i<nowArray.length; i++){
      string += '<div id="business">';
    if(cat == nowArray[i].cat ){
      string += '<a href="#" onclick="disCoupon(\'' + nowArray[i].name + '\',\''+ cat+'\')">' + nowArray[i].name + '</a><br>';
    }
      string += '</div>';
  }

    document.getElementById(cat).innerHTML = string;
    lastCat = cat;
}

function disCat(cat){

  var string = '';
  var i;
  string += '<table border=0 cellpadding="5" cellspacing="10">';
  for(i = 0; i < nowCat.length; i++){
    string += '<tr><td id="leftCell" valign="top">';
    string += '<div id="' + nowCat[i] + '">';
    string += '<a href="#" onclick="disInfo(\'' + nowCat[i] + '\')">' + nowCat[i] + '</a>';

    string += '</div>';
    string += '</td>';

    if(nowCat[i+1]){
      string += '<td id="rightCell" valign="top">';
      string += '<div id="' + nowCat[i+1] + '">';

      string += '<a href="#" onclick="disInfo(\'' + nowCat[i+1] + '\')">' + nowCat[i+1] + '</a>';

      string += '</div>';
      string += '</td>';
    } else {
      string += '<td></td>';
    }
    string += '</tr>';
    i++;
  }
  string += '</table>';
  document.getElementById('printCoupons').innerHTML = string;
  if(cat) disInfo(cat);

}

function disBus(inbus){
  var i = '';
  var string = '';
  bus = inbus;
  if(!inbus){ bus = 'A'; }

  for (i=0;i<nowArray.length;i++){
    busArray[i] = nowArray[i].name;    
  }
  busArray.sort();

  string = '<br><br>';

  for(i = 0; i < alpha.length; i++){
    if(alpha[i] == bus){
      string += alpha[i];
    } else {
      string += '<a href="javascript:disBus(\'' + alpha[i] + '\')">' + alpha[i] + '</a>';
    }
      if(i != alpha.length-1){
        string += ', ';
      }
  }
  document.getElementById('printCoupons').innerHTML = string;
  string = '';

  string += '<div id="business"><br>';

  for(i = 0; i < busArray.length; i++){
   var busEx = /^(\w)/;
   busEx.exec(busArray[i]);
   if(RegExp.$1 == bus){
    string += '<a href="javascript:disCoupon(\'' + busArray[i] + '\',\'business\')">';
    string += busArray[i] + '</a><br><br>';
   }
  }
   string += '</div>';

   document.getElementById('printCoupons').innerHTML += string;
}

function displayCoupons(eitheror){
  var string = '';
  if(eitheror == 'category'){
    disCat('');
  } else {
    disBus('');
  }

}


function switchCounty(newArray){

    nowArray = calvertData;
    nowCat   = calvertCategory;


  lastCat = '';
  displayCoupons(eitheror);
}



