// JavaScript Document

function cleanPrice(price){
  var allNumbers = price.replace(/[^\d]/g, '');
  var finalPrice='';
  finalPrice = allNumbers.replace(/(\d{2}$)/, '.$1');
  if (finalPrice == ''){
    finalPrice = '0.00';
  }
  return finalPrice;
}

function RemoveComma(valueString){
  while (valueString.indexOf(",") != -1){ // removes comma from normal string
    valueString = valueString.replace(","," ");
  }
  return valueString;
}

function GetCleanProdName(valueString){
// first if the download button is part of the string, remove it.
        var newValueString = '';
  if (valueString.indexOf("href=") != -1 || valueString.indexOf("HREF=") != -1){
    var startPoint = valueString.indexOf("<B>") + 3;
    var endPoint = valueString.indexOf("</B>");
    newValueString = valueString.substring(startPoint,endPoint);
  }
  else {
    newValueString = valueString;
  }
// now remove the commas
  while (newValueString.indexOf(",") != -1){ // removes comma and HREF from normal string
    newValueString= newValueString.replace(","," ");
  }
  return newValueString;
}

function GetCookie(name) {
  var cname = name + "=";
  var dc = document.cookie;
  if (dc.length > 0) {
    begin = dc.indexOf(cname);
    if (begin != -1) {
      begin += cname.length;
      end = dc.indexOf(";", begin);
      if (end == -1) end = dc.length;
        return unescape(dc.substring(begin, end));
    }
  }
  return null;
} // end GetCookie function

var expDays = 1; // days til cookie expires
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function SetCookie(name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : exp;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;

document.cookie = name + "=" +  escape (value) + "; expires=" + exp + ";path=/";
}

function DeleteCookie(name) {
//var dc=document.cookie;
//document.write('cookies before delete:<br>' + dc + '<br><br><br>');
  var exp = new Date();
  exp.setTime (exp.getTime() - 1);
  var cval = GetCookie(name);
  document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString() + ";path=/";
//dc=document.cookie;
//document.write('cookies after delete:<br>' + dc + '<br>');
}

function getAndSetOemID() {
    var hasOemID = document.URL.indexOf('oemID=');
    var theOemIDArray = "";
    var theOemID = "";
    var url = document.URL;
    var urlArray = url.split("&");
    var count=0;
    if(hasOemID != -1) {
      while (count < urlArray.length) {
        if(!urlArray[count].indexOf('oemID=')) {
          theOemIDArray=urlArray[count].split("=");
          theOemID=theOemIDArray[1];
        }
        count++;
      }
      SetCookie('oemID', theOemID, '', '/', '', '');
//      alert("setting oemID cookie to: " + theOemID);
    }
}

  function getAid() { // If there's an aID in the URL, get the values of aID and productID
    var hasAID = document.URL.indexOf('aID=');
    var theAIDArray = "";
    var theAID = "";
    var url = document.URL;
    var urlArray = url.split("&");
    var count=0;
    var cookieName="";
    if(hasAID != -1) {
      while (count < urlArray.length) {
        if(!urlArray[count].indexOf('aID=')) {
          theAIDArray=urlArray[count].split("=");
          theAID=theAIDArray[1];
        }
        count++;
      }
      thePID = getThePID();
//      alert("aID= " + theAID + "PID= " + thePID);
      cookieName="aID_" + thePID;
//      alert(cookieName);
      SetCookie(cookieName, theAID, '', '/', '', '');
    }
  }

function getThePID() {
    var hasPID = document.URL.indexOf('productID=');
    var thePIDArray = "";
    var url = document.URL;
    var urlArray = url.split("&");
    var count=0;
    if(hasPID != -1) {
      while (count < urlArray.length) {
        if(!urlArray[count].indexOf('productID=')) {
          thePIDArray=urlArray[count].split("=");
          thePID=thePIDArray[1];
        }

        count++;
      }
      return thePID;
    }
}