//estimated ship date functions
var ESDDefault = 1;
var ESDValue;
var theESD = new Date();
var displayESD = "";
var showXmasMsg = false; /* used to determine if christmas messaging should show */

document.write("<scr" + "ipt language='javascript' type='text/javascript' src='assets/images/includes/timeinc1.js'></scr" + "ipt>");
document.write("<scr" + "ipt language='javascript' type='text/javascript' src='assets/images/includes/timeinc3.js'></scr" + "ipt>");

function getESDDate (theSku)
{
  if (sa_array.length > 0)
  {
    for (i=0;i<sa_array.length;i++)
    {
      if (theSku == sa_array[i].sku && sa_array[i].name == "ESD" && parseInt(sa_array[i].value))
      {
        if (sa_array[i].value == ".0")  sa_array[i].value = "0";
        CalculateESD(parseInt(sa_array[i].value));
        return (theESD);
      }
    }
    //if there is no ESD in the sa_array, use Default
    CalculateESD(ESDDefault);
    return (theESD);

  }else{
    //if there is no sa_array, use Default
    CalculateESD(ESDDefault);
    return (theESD);
  }
}

function getQVESDDate (sa_array, theSku)
{
  if (sa_array.length > 0)
  {
    for (i=0;i<sa_array.length;i++)
    {
      if (theSku == sa_array[i].sku && sa_array[i].name.toUpperCase() == "ESD" && parseInt(sa_array[i].value))
      {
        if (sa_array[i].value == ".0")  sa_array[i].value = "0";
        CalculateESD(parseInt(sa_array[i].value));
        return (theESD);
      }
    }
    //if there is no ESD in the sa_array, use Default
    CalculateESD(ESDDefault);
    return (theESD);

  }else{
    //if there is no sa_array, use Default
    CalculateESD(ESDDefault);
    return (theESD);
  }
}

function CalculateESD (theDays, mode)
{
  if (typeof mode == "undefined") mode = 0;

  //if the mode = 0 then we are on the product page and going to adjust the ESDValue number as necessary.

  ESDValue = parseInt(theDays);
  var dateNY = checkNYTime();
  dateNY[0] = dateNY[0].split("/"); //mm,dd,yyyy
  dateNY[1] = dateNY[1].split(":"); //hh:mm:ss
  var thisDate = new Date(dateNY[0][2],dateNY[0][0] - 1,dateNY[0][1], dateNY[1][0],dateNY[1][1],dateNY[1][2]);
  var hoursNY = thisDate.getHours();

  //FOR XMAS we are adding a day by default
  if (mode == 0 && showXmasMsg==true)  { ESDValue++; }


  //this value is hour when the rollover to the next shipping day occurs. 15 = 3pm eastern
  if (hoursNY >= 23 && mode == 0)
  {
    //we need to add a day to the ESD
    ESDValue++;
  }
  
  thisDate.setDate(thisDate.getDate() + ESDValue);
  
  //if day is Sunday add 1 to make Monday
  if (thisDate.getDay() == 0)
  {
    thisDate.setDate(thisDate.getDate() + 1);
  }
  //if day is Saturday add 2 to make Monday
  else if (thisDate.getDay() == 6)
  {
    thisDate.setDate(thisDate.getDate() + 2);
  }
  
  theESD = thisDate.DMY();
}

function formatESD(format)
{
	var tmpDateStr = theESD.split("/");
	switch(format)
	{
		case 0:
		//sku dropdown display
			if (ESDValue >=6 && showXmasMsg==true)
			{
				displayESD = " | Est. Ship Date Post Xmas";
			}else{
				displayESD = " | Est. Ship Date " + (tmpDateStr[0]) + "/" + tmpDateStr[1];
			}
			break;
		case 1:
		//product page display
			if (ESDValue >=6 && showXmasMsg==true)
			{
				displayESD = "<span class='ESD'>Estimated Ship Date: Post Xmas Delivery</span>";
			}else{
				displayESD = "<span class='ESD'>Estimated Ship Date: " + (theESD) + "</span>";
			}
			break;
		case 2:
		//basket page display
			if (ESDValue >=6 && showXmasMsg==true)
			{
				displayESD = "<span class='ESD'><br>Post Xmas</span>";
			}else{
				displayESD = "<span class='ESD'><br>Estimated Ship Date: "+(theESD)+ "</span>";
			}
			break;
	}

	return displayESD;
}

function checkDay()
{
}