<!--

/* Joe Fugate's custom code for this theme */

var coup = -1;            // discount coupon not active
var cdis = 0;             // amount of coupon discount
var coupCode = '';        // valid coupon code applied to price
var newPrice = 0;		  // new discounted price
var couponCode = new Array ();  // array of valid coupon codes
couponCode[0] = 'NS0573k';      // list as many as you want...
couponCode[1] = 'MM0102';   
couponCode[2] = '';        // Issue 9 code
couponCode[3] = 'EASYDCC!';      
couponCode[4] = 'MRHcc09';      // good forever
couponCode[5] = '';

var couponProd = new Array ();  // array of valid coupon products
couponProd[0] = '';             // list as many as you want...
couponProd[1] = '';             // blank means applies to any product
couponProd[2] = '';
couponProd[3] = 'TSL3';
couponProd[4] = '';
couponProd[5] = 'MTV1-5';

var couponAmt = new Array ();  // array of valid coupon discount amounts
couponAmt[0] = 10.00;          // list as many as you want...
couponAmt[1] = 10.00;
couponAmt[2] = 15.00;
couponAmt[3] = 10.00;
couponAmt[4] = 10.00;
couponAmt[5] = 50.00;

function ChkCoup(val, amt, show, prod) {
  var i;
  coup = -1;              // assume the worst
  cdis = 0;
  if (amt == "" || isNaN (amt)) {
	alert ("\n\n   ChkCoup arg (amt) not valid value  \n\n");
	return;
  }

  // ignore a blank coupon code
  if (val == '') {  
      return;
  }

  for (i=0; i<couponCode.length; i++) {
    if (val == couponCode[i]) {
	  var myCookie = readCookie('myMemoirs'); // check the coupon against the cookie to see if used
	  if (myCookie == (val + "used")) {
		  alert ("\nValid coupon code!\nBut you've used it already.\nCode ignored.");
		  return;
	  }  
	  if (couponProd[i] != '') {  //if product array entry non-blank then check coupon against product
	     if (couponProd[i] != prod) {
		  alert ("\nValid coupon code!\nBut you can't use it on this product.\nCode ignored.");
		  return;
		 }
	  }
	  
      coup = 1;            // user hit the coupon value
	  coupCode = val;      // remember the coupon code
      cdis = couponAmt[i]; // remember the discount amt
      newPrice = Dollar(amt - cdis);
	  if (show) {
        alert ("\nValid coupon code: " + val + "!\nDiscounted price: $" + newPrice + "\nYou save $" + cdis + "\n");
		writeCookie('myMemoirs', coupCode, 1);
	  }
      return;
    }
  }

  // if not blank, then code is invalid!
  if (val != '') {  
    if (show) {
	  alert ("\n'" + val + "' not a valid coupon code!\nCode ignored.");
	}
	newPrice = Dollar(amt);  // set price to original
  }
}


function setSubmitInfo (fObj, iLabel, amt, iNbr) {
  var myCookie = "***initial***";
  // default values in case the read cookie fails
  fObj.amount.value = amt;
  fObj.item_name.value = iLabel;
  fObj.item_number.value = iNbr;
  
  var myCookie = readCookie('myMemoirs'); // look for a cookie with the discount code
  //alert("Cookie: '" + myCookie + "', CoupCode: '" + coupCode + "', " + myCookie.length);
  //alert("form values: " + fObj.item_name.value + ", " + fObj.amount.value + "," + fObj.item_number.value);
  if ((myCookie == coupCode) && (coupCode.length>0)) {
	//alert("We think we have a match: cookie to coupon code!");
    fObj.amount.value = newPrice;
    fObj.item_name.value = iLabel + " (" + coupCode + "-save $" + cdis + ")";
	fObj.item_number.value = iNbr;
	writeCookie('myMemoirs', coupCode + "used", 999);  // okay we used the coupon code
  }
}

// Example:
// alert( readCookie("myCookie") );
function readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}

// Example:
// writeCookie("myCookie", "my name", 24);
// Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.
function writeCookie(name, value, hours)
{
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire;
}


function Dollar (val) {  // force to valid dollar amount
var str,pos,rnd=0;
  if (val < 1) rnd = 1;
  str = escape (val*1.0 + 0.005001 + rnd);  // float, round, escape
  pos = str.indexOf (".");
  if (pos > 0) str = str.substring (rnd, pos + 3);
  return str;
}

function getStyleObject(objectId) {
    // cross-browser function to get an object's style object given its id
    if(document.getElementById && document.getElementById(objectId)) {
	  // W3C DOM
	  return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
	  // MSIE 4 DOM
	  return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
	  // NN 4 DOM.. note: this won't find nested layers
	  return document.layers[objectId];
    } else {
	return false;
    }
} // getStyleObject

function changeObjectVisibility(objectId, newVisibility) {
    // get a reference to the cross-browser style object and make sure the object exists
	alert("In changeObjectVisibility, objectId: " + objectId + ", newVisibility: " + newVisibility);
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	  alert("We found the object, and it's current visibility is: " + styleObject.visibility);	
	  styleObject.visibility = newVisibility;
	  return true;
    } else {
	  // we couldn't find the object, so we can't change its visibility
	  alert ("changeObjectVisibility, we couldn't find: " + objectId);
	  return false;
    }
} // changeObjectVisibility

function moveObject(objectId, newXCoordinate, newYCoordinate) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	styleObject.left = newXCoordinate;
	styleObject.top = newYCoordinate;
	return true;
    } else {
	// we couldn't find the object, so we can't very well move it
	return false;
    }
} // moveObject


function togglePopup(szDivID, iState) // 1 visible, 0 hidden
{
//	alert("In togglePopup!");
    if(document.layers)	   //NN4+
    {
//	   alert("document.layers before: " + document.layers[szDivID].visibility);
       document.layers[szDivID].visibility = iState ? "show" : "hide";
//	   alert("document.layers after: " + document.layers[szDivID].visibility);
    }
    else if(document.getElementById)	  //gecko(NN6) + IE 5+
    {
        var obj = document.getElementById(szDivID);
//	    alert("getElementById before: " + obj.style.visibility);
        obj.style.visibility = iState ? "visible" : "hidden";
//		alert("getElementById after: " + obj.style.visibility);
    }
    else if(document.all)	// IE 4
    {
        document.all[szDivID].style.visibility = iState ? "visible" : "hidden";
    }
}

function test() {
  alert('Dang, this works, too! Coupon: ' + Dollar(1.2));
}

//-->
