//=========================================
// JavaScript Document
// BY: Michale Cai
// CREATED: October 13, 2006
// ABSTRACT: Contains javascript functions used with the "seven feature items" forms in EIS Platform

//Name of  component/date we want to retrieve information forr
var dateRetrieve = "";
var weekClass= "";

//Update the week div class
function updateDayTabClass(dayCode)
{
               var days= new Array(7);
               days[0]= "sunday";
               days[1]= "monday";
               days[2]= "tuesday";
               days[3]= "wednesday";
               days[4]= "thursday";
               days[5]= "friday";
               days[6]= "saturday";
 
               var this_date= new Date();                                       
               var ctrl= document.getElementById("week");
                if(ctrl!=null)
	        ctrl.className = "current_day"+dayCode;
                this_date.setDate(this_date.getDate()- dayCode);

               var request_month= this_date.getMonth()+1;
               var request_year= this_date.getFullYear();
               var request_day= this_date.getDate();
               var request_day_number= this_date.getDay();

               if(request_day<10)
	        request_day= "0"+request_day;
               if(request_month<10)
                        request_month= "0"+request_month;
               dateRetrieve= request_day+"/"+request_month+"/"+request_year;

               //alert("dateRetrieve is "+ dateRetrieve);	   
                
	//update the div featureDate content
	var featureDate = document.getElementById("featureDate");
	var featureDate_content= getFeatureDate(request_day_number, request_day, request_month, request_year);
                //temporarily disable this dynamic function for show dynamic date in the feature seven items home page, may be used in the future
	//featureDate.innerHTML = "<span>"+featureDate_content+"</span>";
			   
	//Ajax request
	xmlHTTPRetrievePost("/FeatureSeven", "content_container");  
}

function updateDayTabClassWithServerTime(dayCode)
{
               dateRetrieve= dayCode;
	//Ajax request
	xmlHTTPRetrievePost("/FeatureSeven", "topstories_container");
	//xmlHTTPRetrievePost("/FeatureSeven", "feature"); 
}

//Show the featured items for the selected day
//Show the featured items for the selected day
function changeItems(dayCode)
{
     //code for web trends
     //trackHomeFeature(dayCode);

     //client side time
     //updateDayTabClass(dayCode); 

    //server side time
    updateDayTabClassWithServerTime(dayCode);
}

//function for Web trends track Feature Seven
function trackHomeFeature(dayCode){
      if (dayCode!=0) {
            dcsuri_ORIG = DCS.dcsuri;
            ti_ORIG=WT.ti;
            var dayDelta = "_Previous_Day(-"+dayCode+")"; 
            var fileName = DCS.dcsuri.substring(DCS.dcsuri.lastIndexOf("/")+1);
            fileName = fileName.replace(/\./,dayDelta+"\.");
            var pathNoFile = DCS.dcsuri.substring(0,DCS.dcsuri.lastIndexOf("/")+1);
            var datedURI = pathNoFile+fileName;
            dcsMultiTrack("DCS.dcssip",DCS.dcssip,"DCS.dcsuri",datedURI,"DCS.dcsqry",DCS.dcsqry,"WT.ti",WT.ti+dayDelta,"WT.ajax","1");
            WTajax="";
            DCS.dcsuri=dcsuri_ORIG;
            WT.ti=ti_ORIG;
     }
}

//when page landed, generate the class for the div week
function refreshItems()
{
	if ($('topstories_container'))
	{
		var dayCode="0";

		//For Client time, disable it
		//updateDayTabClass(dayCode);  
 
		// For server time
		updateDayTabClassWithServerTime(dayCode);
	}
}

//Post an XML HTTP Request for AJAX
function xmlHTTPRetrievePost(strURL, divName) 
{
    var xmlHttpReq = false;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlHttpReq.open('POST', strURL, true);
    xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttpReq.onreadystatechange = function() {
        if (xmlHttpReq.readyState == 4) {
            populateItems(xmlHttpReq.responseText, divName);
        }
    }
	
    //Compose and send the request string
    xmlHttpReq.send("forDate=" + dateRetrieve);
}

//Update the page according to results returned from the AJAX request
// This code was taken from common.css originally written by Will Price
function populateItems(strResult, divName)
{
    var ctrl = document.getElementById(divName);

    if (ctrl) 
          ctrl.innerHTML = strResult;
}


//Does load testing script fire client-side JS?
function doJSLoadTest() 
{
    var xmlHttpReq = false;
    var self = this;
    var strURL = "/Comment";

    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            populateItems(self.xmlHttpReq.responseText);
        }
    }
	
	//Compose and send the request string
    self.xmlHttpReq.send("commentAction=post&uri=tcm:13-435-64&comment=load_test_JS_fired" );
}

//Generate client side computer date
function getFeatureDate(request_day_number, request_day, request_month, request_year)
{
   var months = new Array(13);
   months[0]  = "January";
   months[1]  = "February";
   months[2]  = "March";
   months[3]  = "April";
   months[4]  = "May";
   months[5]  = "June";
   months[6]  = "July";
   months[7]  = "August";
   months[8]  = "September";
   months[9]  = "October";
   months[10] = "November";
   months[11] = "December";
   
   var days= new Array(7);
   days[0]= "Sunday";
   days[1]= "Monday";
   days[2]= "Tuesday";
   days[3]= "Wednesday";
   days[4]= "Thursday";
   days[5]= "Friday";
   days[6]= "Saturday";      

   var day= days[request_day_number];
   var monthname   = months[request_month-1];
   var dateString = day + ", "+ monthname +
                    ' ' +
                    request_day +
                    ', ' +
                    request_year;
   return dateString;
} // function getFeatureDate()

//This function print out the month name for Feature Seven Items
//Param Daycode passing from template page
function printMonth(dayCode){
      var months = new Array(13);
      months[0]  = "Jan";
     months[1]  = "Feb";
     months[2]  = "Mar";
     months[3]  = "Apr";
     months[4]  = "May";
     months[5]  = "Jun";
     months[6]  = "Jul";
     months[7]  = "Aug";
     months[8]  = "Sep";
     months[9]  = "Oct";
     months[10] = "Nov";
     months[11] = "Dec";

      var this_date= new Date();   
      this_date.setDate(this_date.getDate()- dayCode);
      var request_month= months[this_date.getMonth()];
      document.write(request_month);
}

//This function print out name of day for Feature Seven Items
function printDay(dayCode){
    var this_date= new Date();   
      this_date.setDate(this_date.getDate()- dayCode);
      var request_day= this_date.getDate();
      document.write(request_day);
} //fucntion printDay

function doClear(elementId)
{
	var el = document.getElementById(elementId);
	if ((el)  && (!focused))
	{
		focused = true;
		el.value = '';
	}
}

//=========================================
// Start - Functionality used in Photo Gallery - AJAX
function photoGalleryFirstImage()
{
	maxIndex = Number(IDs.substring(IDs.indexOf('%')+1));
	//alert('ID before: ' + IDs);
	IDs = IDs.substring(0,IDs.indexOf('%')) + '|';
	//alert('ID after: ' + IDs);
	//alert('maxIndex: ' + maxIndex);
	//alert('curIndex: ' + curIndex);
	//alert('compID: ' + photoGalleryGetID());
	photoGalleryGoForward();
}

function photoGalleryGoBack()
{
	curIndex = curIndex - 1;
	if (curIndex == 0) {
		curIndex = maxIndex;
	}
	photoGalleryRefreshImage(photoGalleryGetID());
	photoGalleryRefreshNav();
}

function photoGalleryGoForward()
{
	curIndex = curIndex + 1;
	if (curIndex > maxIndex) {
		curIndex = 1;
	}
	photoGalleryRefreshImage(photoGalleryGetID());
	photoGalleryRefreshNav();
}

function photoGalleryRefreshNav()
{
	populateItems(curIndex + ' of ' + maxIndex + ' photos', 'photo_gallery_nav');
}

function photoGalleryGetID()
{
	var i = 1;
	var ID = '';
	var newIDs = IDs;
	while (i < curIndex+1)
	{
		ID = newIDs.substring(0,newIDs.indexOf('|'));
		newIDs = newIDs.substring(newIDs.indexOf('|')+1);
		i = i + 1;
	}
	return Number(ID.substring(ID.indexOf('-')+1));
}

function photoGalleryRefreshImage(imageID)
{
	//Ajax request
	xmlHTTPRetrievePostImage("/PhotoGallery", "gallery_photo", imageID);
}

function xmlHTTPRetrievePostImage(strURL, divName, imageID)
{
	var xmlHttpReq = false;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlHttpReq.open('POST', strURL, true);
	xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttpReq.onreadystatechange = function() {
		if (xmlHttpReq.readyState == 4) {
			populateItems(xmlHttpReq.responseText, divName);
		}
	}

	//Compose and send the request string
	xmlHttpReq.send("imageID=" + imageID);
}
// End - Functionality used in Photo Gallery - AJAX
//=========================================