var SAPPHIRE_DISCOUNT = 40;
var EMERALD_DISCOUNT = 60;
var DIAMOND_DISCOUNT = 64;


function logOut()
{
    var thisLoc = window.location + "";
    setCookie("elysee_tier", "", now);
    alert("You are now logged out!");
    window.location = thisLoc;
}


function getQueryVariable(variable) 
{
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) 
  {
    var pair = vars[i].split("="); 
    if (pair[0] == variable) 
    {
      return pair[1];
    }
  } 
  return "";
}


function login()
{
  window.location = "http://site.theelyseecollection.com";
}


function writeOrderForm(itemID)
{
   var Message = "";
   var TAid = "orderForm" + itemID;
   var OrderForm = document.getElementById(TAid).value;
   var SPNid = "spn" + itemID;
   var Span = document.getElementById(SPNid);
   Span.innerHTML = OrderForm;
   var priceID = "msrp" + itemID;
   var msrp_str = document.getElementById(priceID).innerHTML;
   var disPriceID = "disPrice" + itemID;
   var disPrice = document.getElementById(disPriceID);
   
   //get the msrp in float $4,808.95 converted to 4808.95
   
   // Step 1: remove the commas
   var temp = new Array();
   temp = msrp_str.split(',');
   var x = 0;
   var msrp = '';
   var discount_price = '';
   for (x=0; x<temp.length;x++)
   {
     msrp = msrp + temp[x]; 
   }
   
   // Step2: remove the Dollar sign
   msrp = msrp.substring(1);
   
   if(LoggedIn)
   {
      var user_tier = getCookie("elysee_tier");
      var str = '';
      if(user_tier == "Sapphire")
      {
        discount_price = parseFloat(msrp) * (100 - parseInt(SAPPHIRE_DISCOUNT))/100;
        discount_price = Math.round(discount_price*100)/100;
        discount_price = setDecimal(discount_price);
        discount_amt = Math.round((parseFloat(msrp) - parseFloat(discount_price))*100)/100;
        discount_amt = setDecimal(discount_amt);
        
        // build the option string
        str = "<br>Wholesale Discount: " + SAPPHIRE_DISCOUNT + "% " + "&nbsp;"; 
        str = str + "<select name='Wholesale Discount'>" + "<option value='Sapphire Discount (-" + discount_amt + ")'>";
        str = str + "Sapphire Discount( -$" + discount_amt + ")</option></select>";
        str = str + "<br>Your price: <b>$" + discount_price + "</b>";        
      }
      else if(user_tier == "Emerald")
      {
        discount_price = parseFloat(msrp) * (100 - parseInt(EMERALD_DISCOUNT))/100;
        discount_price = Math.round(discount_price*100)/100;
        discount_price = setDecimal(discount_price);
        discount_amt = Math.round((parseFloat(msrp) - parseFloat(discount_price))*100)/100;
        discount_amt = setDecimal(discount_amt);
        
        // build the option string
        str = "<br>Wholesale Discount: " + EMERALD_DISCOUNT + "% " + "&nbsp;"; 
        str = str + "<select name='Wholesale Discount'>" + "<option value='Emerald Discount (-" + discount_amt + ")'>";
        str = str + "Emerald Discount( -$" + discount_amt + ")</option></select>";
        str = str + "<br>Your price: <b>$" + discount_price + "</b>";        
      }      
      else if(user_tier == "Diamond")
      {
        discount_price = parseFloat(msrp) * (100 - parseInt(DIAMOND_DISCOUNT))/100;
        discount_price = Math.round(discount_price*100)/100;
        discount_price = setDecimal(discount_price); 
        discount_amt = Math.round((parseFloat(msrp) - parseFloat(discount_price))*100)/100;
        discount_amt = setDecimal(discount_amt);
        
        // build the option string        
        str = "<br>Wholesale Discount: " + DIAMOND_DISCOUNT + "% " + "&nbsp;";  
        str = str + "<select name='Wholesale Discount'>" + "<option value='Diamond Discount (-" + discount_amt + ")'>";
        str = str + "Diamond Discount( -$" + discount_amt + ")</option></select>";
        str = str + "<br>Your price: <b>$" + discount_price + "</b>";        
      }      
      disPrice.innerHTML = str;      
   }
   else
   {   
     Span.innerHTML = Message;  
   }
}


// function to set the decimal points
// 100 converted to 100.00
// 100.5 converted to 100.50


function setDecimal(number)
{
  noToParse = number.toString();
  var i = noToParse.indexOf('.');
  if (i < 0)
  {
    noToParse =  noToParse + ".00";
  }  
  else if ((i + 2) == noToParse.length)
  {
    noToParse += "0";
  }  
  return noToParse;
}


function writeMiniForm(itemID)
{
   var TAid = "orderForm" + itemID;
   var OrderForm = document.getElementById(TAid).value;
   var SPNid = "spn" + itemID;
   var Span = document.getElementById(SPNid);
   if(LoggedIn)
   {   Span.innerHTML = OrderForm;  }
   else
   {   Span.innerHTML = "";  }
}


function setCookie(name, value, expires, path, domain, secure) 
{
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}


function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


function fixDate(date)
{
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

