var mondayStart = 1;
function getDayOfWeekMethod() {
  dow = this.getDay()-mondayStart;
  if (dow<0) {dow = 6}
  return dow
}
function getDIMMethod() {
  return getDaysInMonth(this.getYear(),(this.getMonth()+1))
}
function getQuarterMethod() {
  return Math.ceil((this.getMonth()+1)/3);
}
function getQuarterStartMonthMethod() {
  return quarterStarts[(this.getQuarter())]
}
function getQuarterEndMonthMethod() {
  return quarterEnds[(this.getQuarter())]
}
Date.prototype.getDayOfWeek = getDayOfWeekMethod;
Date.prototype.getDIM = getDIMMethod;
Date.prototype.getQuarter = getQuarterMethod;
Date.prototype.getQuarterStartMonth = getQuarterStartMonthMethod;
Date.prototype.getQuarterEndMonth = getQuarterEndMonthMethod;
var q = "'"
var MENU = document.cookie;
var position = MENU.indexOf("JMENU");
if (position==-1) { MENU = "1,0,0,0,0,0,0,0" }
else { MENU = MENU.substr(position+6,15) }
var JMENU = MENU.split(",");
var SiteStatistics=JMENU[0];
var ReachOptimizer=JMENU[1];
var PageStatistics=JMENU[2];
var BrowserStatistics=JMENU[3];
var ReferrerStatistics=JMENU[4];
var SystemStatistics=JMENU[5];
var ECommerceStatistics=JMENU[6];
var GeoDataStatistics=JMENU[7];
var SSLStatistics=JMENU[8];
var selectedReport = "";
function prepData(stateoc,sitecode,report,language,startday,startmonth,startyear,endday,endmonth,endyear,unit,datatype,item,search,datadisplay,gtype,displaysize,row,abbtagid,campaignmode,getplacements,otherpre,otherpost,newwindow,geturlonly) {
  top.fraNavBar.validateDates();
  top.fraNavBar.unhighlightAll(fromCalendar);
  top.fraNavBar.unhighlightAll(toCalendar);
  top.fraNavBar.setDate(fromCalendar,startYear,startMonth);
  top.fraNavBar.setDate(toCalendar,endYear,endMonth);
  if (!geturlonly){
    startday = frmDates.inpStartDay.value;
    startmonth = frmDates.inpStartMonth.value;
    startyear = frmDates.inpStartYear.value;
    endday = frmDates.inpEndDay.value;
    endmonth = frmDates.inpEndMonth.value;
    endyear = frmDates.inpEndYear.value;
  }
  if (!datadisplay) {
    datadisplay = '';
    for (i=0; i<DATADISPLAYARRAY.length; i++) {
      datadisplay += DATADISPLAYARRAY[i]+","
    }
    datadisplay = datadisplay.substring(0,(datadisplay.length-1))
  }
  if (!datatype) {
    if (DATADISPLAYARRAY[0]==1) {datatype = "count"}
    else if (DATADISPLAYARRAY[1]==1) {datatype = "response"}
    else if (DATADISPLAYARRAY[2]==1) {datatype = "revenue"}
    else if (DATADISPLAYARRAY[3]==1) {datatype = "saving"}
    else if (DATADISPLAYARRAY[4]==1) {datatype = "value"}
    else if (DATADISPLAYARRAY[5]==1) {datatype = "cost"}
  }
  if (!stateoc) {stateoc = ASTATEOC}
  if (!sitecode) {sitecode = ASITECODE}
  if (!report) {report = selectedReport}
  if (report=="") {report = top.fraContent.AREPORT}
  if (!language) {language = ALANGUAGE}
  if (!unit) {unit = UNIT}
  if (!item) {item = ""}
  if (!search) {search = ""}
  if (!gtype) {gtype = GTYPE}
  if (!displaysize) {displaysize = DISPLAYSIZE}
  if (!row) {row = ""}
  if (!abbtagid) {abbtagid = ""}
  if (!campaignmode) {campaignmode = ""}
  if (!getplacements) {getplacements = ""}
  if (!otherpre) {otherpre = ""}
  if (!otherpost) {otherpost = ""}
  buildLink(stateoc,sitecode,report,language,startday,startmonth,startyear,endday,endmonth,endyear,unit,datatype,item,search,datadisplay,gtype,displaysize,row,abbtagid,campaignmode,getplacements,otherpre,otherpost,newwindow,geturlonly)
}
function buildLink(stateoc, sitecode, report, language, startday, startmonth, startyear, endday, endmonth, endyear, unit, datatype, item, search, datadisplay, gtype, displaysize, row, abbtagid, campaignmode, getplacements, otherpre, otherpost, newwindow, geturlonly) {
  build = "/"+report+"?aStatEOC="+stateoc
        + "&aSiteCode="+sitecode
        + "&aReport="+report
        + "&aLanguage="+language
        + "&aStartDay="+startday
        + "&aStartMonth="+startmonth
        + "&aStartYear="+startyear
        + "&aEndDay="+endday
        + "&aEndMonth="+endmonth
        + "&aEndYear="+endyear
        + "&aUnit="+unit
        + "&aDataType="+datatype
        + "&aItem="+item
        + "&aSearch="+search
        + "&aBBTagID="+abbtagid
        + "&CampaignMode="+campaignmode
        + "&GetPlacements="+getplacements
        + otherpre
        + "&_NoLiveCache"
        + "&DataDisplay="+datadisplay
        + "&GType="+gtype
        + "&DisplaySize="+displaysize
        + "&aRow="+row
        + otherpost
        + "&ShowTable="+SHOWTABLE;
  if (!report){
    alert('Please click on a report to view');
  }
  else
  {
    if (geturlonly=='Y'){
      top.fraContent.document.write('<A HREF='+build+'>From '+startmonth+'/'+startday+'/'+startyear+' To '+endmonth+'/'+endday+'/'+endyear+'</A>');
    }
    else if (!NEWWIN) {
      top.fraContent.document.location.href = build;
    }
    else {
      build += "&newwindow=1"
      window.open(build)
    }
  }
}
function goButton(newwindow) {
  if(unitButton.style.color=='#cc0000'){
   alert('This selection can not be displayed in '+unitButton.innerHTML);
  }
  else
  {
   prepData('','',selectedReport,'','','','','','','','','','','','','','','','','','','','',newwindow)
  }
}
function goStat(reportalias, reportname, groupname, supportcode) {
  selectedReport = reportalias;
  tdReportName.innerHTML = reportname;
  tdGroupName.innerHTML = groupname;
  showUnitSupport(supportcode)
}
function goSearch(searchstring) {
  prepData('','','PageBreakdown','','','','','','','','','','',searchstring)
}
selectedOption = '';
SHOWTABLE = true;
function toggleTable() {
  SHOWTABLE = !SHOWTABLE;
  buildGraphButton(graphButton)
}
NEWWIN = false;
function toggleNewWin() {
  NEWWIN = !NEWWIN;
  buildGraphButton(graphButton)
}
function selectGraph(optionref, graph, buttonref) {
  GTYPE = graph;
  selectedOption = optionref;
  prevColor = optionref.style.backgroundColor
  for (i=0; i<graphArray.length; i++) {
    graphArray[i].style.backgroundColor = graphTable.style.backgroundColor
  }
  optionref.style.backgroundColor = prevColor;
  buildGraphButton(buttonref)
}
function buildGraphButton(buttonref) {
  buttonHTML = selectedOption.innerHTML;
  if (SHOWTABLE) {
    buttonHTML += tableButton.value
  }
  if (NEWWIN) {
    buttonHTML += newWindowButton.value
  }
  buttonref.innerHTML = buttonHTML;
}
function selectUnit(optionref, unit, buttonref) {
  UNIT = unit;
  prevColor = optionref.style.backgroundColor
  for (i=0; i<unitArray.length; i++) {
    unitArray[i].style.backgroundColor = unitTable.style.backgroundColor
  }
  optionref.style.backgroundColor = prevColor;
  buttonref.innerHTML = optionref.innerHTML;
  buttonref.style.color = optionref.style.color;
  manageDropDownMenus();
  validateUnits();
}
function incDays(dateObj, val) {
  if (dateObj) {newDate = new Date(dateObj.getTime())}
  else {newDate = new Date()}
  val = parseFloat(val);
  newDate = new Date(newDate.setDate(newDate.getDate()+val));
  return newDate
}
function decDays(dateObj, val) {
  if (dateObj) {newDate = new Date(dateObj.getTime())}
  else {newDate = new Date()}
  val = parseFloat(val);
  newDate = new Date(newDate.setDate(newDate.getDate()-val));
  return newDate
}
function getWeekStart(dateObj) {
  offset = dateObj.getDayOfWeek();
  return decDays(dateObj,offset)
}
function getWeekEnd(dateObj) {
  offset = 6-dateObj.getDayOfWeek();
  return incDays(dateObj,offset)
}
function validateUnits() {
  with (document.frmDates) {
    startyear = parseFloat(inpStartYear.value);
    startmonth = parseFloat(inpStartMonth.value);
    startday = parseFloat(inpStartDay.value);
    endyear = parseFloat(inpEndYear.value);
    endmonth = parseFloat(inpEndMonth.value);
    endday = parseFloat(inpEndDay.value)
  }
  startdatetext = monthNames[startmonth]+' '+startday+', '+startyear;
  startdate = new Date(startdatetext);
  enddatetext = monthNames[endmonth]+' '+endday+', '+endyear;
  enddate = new Date(enddatetext);
  if (UNIT=="week") {
    adjust = 0;
    if (startdate.getDayOfWeek()!=0||enddate.getDayOfWeek()!=6) {
      start = getWeekStart(startdate);
      end = getWeekEnd(enddate);
      adjustDates(start,end)
    }
  }
  else if (UNIT=="month") {
    if (startdate.getDate()!=1||enddate.getDate()!=enddate.getDIM()) {
      startdate.setDate(1);
      enddate.setDate(enddate.getDIM());
      adjustDates(startdate,enddate)
    }
  }
  else if (UNIT=="quarter") {
    if (startdate.getDate()!=1||(startdate.getMonth()+1)!=startdate.getQuarterStartMonth()||enddate.getDate()!=enddate.getDIM()||(enddate.getMonth()+1)!=enddate.getQuarterEndMonth()) {
      startdate.setMonth(startdate.getQuarterStartMonth()-1);
      startdate.setDate(1);
      enddate.setMonth(enddate.getQuarterEndMonth()-1);
      enddate.setDate(enddate.getDIM());
      adjustDates(startdate,enddate)
    }
  }
}
function adjustDates(start,end) {
  setPeriod(start.getYear(),(start.getMonth()+1),start.getDate(),end.getYear(),(end.getMonth()+1),end.getDate())
}
function navOnLoad() {
  weekButton.style.backgroundColor='#d0dce8';
  selectUnit(weekButton,'week',unitButton);
  barButton.style.backgroundColor='#d0dce8';
  selectGraph(barButton,'bar',graphButton);
  setDataDisplayArray(0)
  start = getWeekStart(todayDate);
  end = getWeekEnd(todayDate);
  adjustDates(start,end)
}
function showUnitSupport(supportcode) {
  supportedUnitArray = supportcode.split(",");
  unitButton.style.color = '#000000';
  hourButton.style.color = '#000000';
  dayButton.style.color = '#000000';
  weekButton.style.color = '#000000';
  monthButton.style.color = '#000000';
  quarterButton.style.color = '#000000';
  if (supportedUnitArray[0]==0) {
    hourButton.style.color = '#cc0000';
    if (UNIT=="hour") {
      unitButton.style.color = '#cc0000';
    }
  }
  if (supportedUnitArray[1]==0) {
    dayButton.style.color = '#cc0000';
    if (UNIT=="day") {
      unitButton.style.color = '#cc0000';
    }
  }
  if (supportedUnitArray[2]==0) {
    weekButton.style.color = '#cc0000';
    if (UNIT=="week") {
      unitButton.style.color = '#cc0000';
    }
  }
  if (supportedUnitArray[3]==0) {
    monthButton.style.color = '#cc0000';
    if (UNIT=="month") {
      unitButton.style.color = '#cc0000';
    }
  }
  if (supportedUnitArray[4]==0) {
    quarterButton.style.color = '#cc0000';
    if (UNIT=="quarter") {
      unitButton.style.color = '#cc0000';
    }
  }
}
var lastgroup='';
function toggleMenuGroup(groupalias) {
  for (i=0; i<document.all.NavTable.rows.length; i++){
    if(document.all.NavTable.rows[i].NavGroup=="Main"){
      document.all.NavTable.rows[i].style.display='none'
    }
  }
  if(lastgroup==groupalias) {
    eval("document.all."+groupalias+".style.display='none'")
    lastgroup = '';
  }
  else {
    eval("document.all."+groupalias+".style.display='block'")
    lastgroup = groupalias ;
  }
}
function setMenu(themenu) {
  if (themenu==0) {themenu+"=1"}
  else {themenu+"=0"}
  JMENU = SiteStatistics+","+PageStatistics+","+BrowserStatistics+","+ReferrerStatistics+","+SystemStatistics+","+ECommerceStatistics+","+GeoDataStatistics+","+SSLStatistics+","+ReachOptimizer
  var expDate = new Date()
  expDate.toGMTString()
  expDate.setYear(expDate.getYear()+1)
  document.cookie = "JMENU="+JMENU+"; expires="+expDate+"domain=."+FULLDOMAIN
}
function restoreMenu() {
  if (SiteStatistics==1&&document.all.SiteStatistics) {document.all.SiteStatistics.style.display="block"} else {document.all.SiteStatistics.style.display="none"}
  if (PageStatistics==1&&document.all.SiteStatistics) {document.all.PageStatistics.style.display="block"} else {document.all.PageStatistics.style.display="none"}
  if (BrowserStatistics==1&&document.all.SiteStatistics) {document.all.BrowserStatistics.style.display="block"} else {document.all.BrowserStatistics.style.display="none"}
  if (ReferrerStatistics==1&&document.all.SiteStatistics) {document.all.ReferrerStatistics.style.display="block"} else {document.all.ReferrerStatistics.style.display="none"}
  if (SystemStatistics==1&&document.all.SiteStatistics) {document.all.SystemStatistics.style.display="block"} else {document.all.SystemStatistics.style.display="none"}
  if (GeoDataStatistics==1&&document.all.SiteStatistics) {document.all.GeoDataStatistics.style.display="block"} else {document.all.GeoDataStatistics.style.display="none"}
  if (SSLStatistics==1&&document.all.SiteStatistics) {document.all.SSLStatistics.style.display="block"} else {document.all.SSLStatistics.style.display="none"}
  if (ReachOptimizer==1&&document.all.SiteStatistics) {document.all.ReachOptimizer.style.display="block"} else {document.all.ReachOptimizer.style.display="none"}
}
function manageDropDownMenus(menu) {
  menuOn = 0;
  if (menu&&menu.style.display=='none') {
    menuOn = 1
  }
  for (i=0; i<dropdownArray.length; i++) {
    dropdownArray[i].style.display = 'none'
  }
  if (menu&&menuOn==1) {
    menu.style.display = 'block'
  }
}
var prevColor = '';
function highlightButton(buttonref, hexvalue) {
  prevColor = buttonref.style.backgroundColor;
  buttonref.style.backgroundColor = hexvalue
}
function unHighlightButton(buttonref) {
  buttonref.style.backgroundColor = prevColor
}
function setDataDisplayArray(index) {
  if (top.fraContent.SupportMultiDataType==0) {
  }
  if (DATADISPLAYARRAY[index]==1) {
    DATADISPLAYARRAY[index] = 0
  }
  else {
    DATADISPLAYARRAY[index] = 1
  }
  SelectDataTypes();
}
function UnSelectAllDataTypes() {
  for (i=0; i<DATADISPLAYARRAY.length; i++) {
    DATADISPLAYARRAY[i] = 0
  }
}
function SelectDataTypes() {
  for (i=0; i<DATADISPLAYARRAY.length; i++) {
    if (DATADISPLAYARRAY[i]==1) {
      inpDataType[i].checked = true
    }
    else {
      inpDataType[i].checked = false
    }
  }
}
function manageTimeRangeDropDown(dates) {
  if (dates) {
    dates = dates.split(",")
    setPeriod(dates[0],dates[1],dates[2],dates[3],dates[4],dates[5])
    if (dates[6]=="day") {
      dayButton.style.backgroundColor='#d0dce8';
      selectUnit(dayButton,'day',unitButton)
    }
    else if (dates[6]=="week") {
      weekButton.style.backgroundColor='#d0dce8';
      selectUnit(weekButton,'week',unitButton)
    }
  }
}
function buildTimeRangeSelectBox() {
  timeRanges = '<select name="inpStandardTimeRanges" onchange="manageTimeRangeDropDown(this.value)">'
             + '<option value="">Time Ranges...</option>';
  sdate = new Date(todayDate.getTime());
  edate = new Date(todayDate.getTime());
  timeRanges += '<option value="'+sdate.getFullYear()+','+(sdate.getMonth()+1)+','+sdate.getDate()+','+edate.getFullYear()+','+(edate.getMonth()+1)+','+edate.getDate()+',day">Today</option>'
  sdate = getWeekStart(todayDate);
  edate = getWeekEnd(todayDate);
  timeRanges += '<option value="'+sdate.getFullYear()+','+(sdate.getMonth()+1)+','+sdate.getDate()+','+edate.getFullYear()+','+(edate.getMonth()+1)+','+edate.getDate()+',day">This Week</option>';
  sdate = decDays(sdate,7);
  edate = decDays(edate,7);
  timeRanges += '<option value="'+sdate.getFullYear()+','+(sdate.getMonth()+1)+','+sdate.getDate()+','+edate.getFullYear()+','+(edate.getMonth()+1)+','+edate.getDate()+',day">Last Week</option>';
  sdate = new Date(todayDate.getTime());
  edate = new Date(todayDate.getTime());
  sdate.setDate(1);
  edate.setDate(edate.getDIM());
  timeRanges += '<option value="'+sdate.getFullYear()+','+(sdate.getMonth()+1)+','+sdate.getDate()+','+edate.getFullYear()+','+(edate.getMonth()+1)+','+edate.getDate()+',day">This Month</option>';
  sdate = decDays(todayDate,9);
  edate = new Date(todayDate.getTime());
  timeRanges += '<option value="'+sdate.getFullYear()+','+(sdate.getMonth()+1)+','+sdate.getDate()+','+edate.getFullYear()+','+(edate.getMonth()+1)+','+edate.getDate()+',day">Last 10 Days</option>';
  sdate = decDays(todayDate,29);
  edate = new Date(todayDate.getTime());
  timeRanges += '<option value="'+sdate.getFullYear()+','+(sdate.getMonth()+1)+','+sdate.getDate()+','+edate.getFullYear()+','+(edate.getMonth()+1)+','+edate.getDate()+',day">Last 30 Days</option>';
  sdate = decDays(getWeekStart(todayDate),28);
  edate = getWeekEnd(todayDate);
  timeRanges += '<option value="'+sdate.getFullYear()+','+(sdate.getMonth()+1)+','+sdate.getDate()+','+edate.getFullYear()+','+(edate.getMonth()+1)+','+edate.getDate()+',week">Last 5 Weeks</option>'
             + '<option value="'+parseFloat(top.GlobalVar.INSERTYEAR)+','+parseFloat(top.GlobalVar.INSERTMONTH)+','+parseFloat(top.GlobalVar.INSERTDAY)+','+parseFloat(top.GlobalVar.TODAYYEAR)+','+parseFloat(top.GlobalVar.TODAYMONTH)+','+parseFloat(top.GlobalVar.TODAYDAY)+',day">'+durationLabel+' (days)</option>'
             + '<option value="'+parseFloat(top.GlobalVar.INSERTYEAR)+','+parseFloat(top.GlobalVar.INSERTMONTH)+','+parseFloat(top.GlobalVar.INSERTDAY)+','+parseFloat(top.GlobalVar.TODAYYEAR)+','+parseFloat(top.GlobalVar.TODAYMONTH)+','+parseFloat(top.GlobalVar.TODAYDAY)+',week">'+durationLabel+' (weeks)</option>'
             + '</select>';
  return timeRanges
}

