Event.observe(window, 'load', HandleHiddenFields);

//Global XMLHTTP Request object
var XmlHttp;

var selectedID ;

//Creating and setting the instance of appropriate XMLHTTP Request object to a “XmlHttp” variable  
function CreateXmlHttp()
{
	//Creating object of XMLHTTP in IE
	try
	{
		XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttp = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttp && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttp = new XMLHttpRequest();
	}
}

//Gets called when country combo box selection changes
function ViewSubtitles(record_id) 
{
	///Check if selectedTitle is expanded
	var subtitleList = document.getElementById('bcoSubtitles_' + record_id);
	if(subtitleList.style.display == '')
	{
		subtitleList.style.display = 'none' ;
		return ;
	}

	///Close all open titles
	HideSubTitles();

	///Show Loading 
	ShowLoading(record_id) ;

	selectedID = record_id;
	
	// URL to get states for a given country
	var requestUrl = AjaxGetBCOSubtitlePage + "?recordid=" + encodeURIComponent(record_id);
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleSubtitleResponse;
		
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("GET", requestUrl,  true);
		
		//Sends the request to server
		XmlHttp.send(null);		
	}
}


//Called when response comes back from server
function HandleSubtitleResponse()
{
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{			
			SetSubtitles(XmlHttp.responseXML.documentElement);
		}
		else
		{
			alert("There was a problem retrieving data from the server." );
			HideSubTitles() ;
		}
	}
}

//Clears the contents of state combo box and adds the states of currently selected country
function SetSubtitles(subTitleNode)
{
	var subtitleStr = "" ;
	var subtitleNodes = subTitleNode.getElementsByTagName('subtitle');
	//Add new states list to the state combo box.
	for (var count = 0; count < subtitleNodes.length; count++)
	{
		var lastClass = "" ;
		if(count == subtitleNodes.length - 1)
		{
			lastClass = " class=\"last\"" ;
		}
   		var record_id = subtitleNodes[count].attributes[0].value ;
		var subtitle = GetInnerText(subtitleNodes[count]);
		subtitleStr += "\t\t<li" + lastClass + "><a href=\"wa-bco-view.aspx?id=" + record_id + "\">" + subtitle + "</a></li>\n"
	}
	var subtitleList = document.getElementById('bcoSubtitles_' + selectedID);
	subtitleList.innerHTML = subtitleStr ;
	subtitleList.style.display = '' ;

}

function HideSubTitles()
{
	///Get reference to bonus_box div
	var bonusBox = document.getElementById('bonus_box');
	var bulletedList = document.getElementsByTagName('ul');
	for(var count = 0 ; count < bulletedList.length ; count++ )
	{
		if(bulletedList[count].id != null && bulletedList[count].id.indexOf('bcoSubtitles_') == 0)
		{
			bulletedList[count].style.display = 'none' ;
		}
	}
}

function ShowLoading(record_id)
{
	var subtitleList = document.getElementById('bcoSubtitles_' + record_id);
	subtitleList.innerHTML = "<li class=\"last\">Loading.....</li>" ;
	subtitleList.style.display = '' ;
}

//Returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}

function GetBlog()
{
	var requestUrl = 'Utilities/Ajax/BlogAtom.aspx';
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleBlogResponse;
		
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("GET", requestUrl,  true);
		
		//Sends the request to server
		XmlHttp.send(null);		
	}
	var blogLabel = GetReferenceToBlogEntryLabel() ;
	blogLabel.innerHTML = '<br />Loading....<br /><br /><br /><br />' ;
	return '' ;
}

//Called when response comes back from server
function HandleBlogResponse()
{
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{			
			SetBlogEntry(XmlHttp.responseXML.documentElement);
		}
		else
		{
			var blogLabel = GetReferenceToBlogEntryLabel() ;
			blogLabel.innerHTML = '<br />There was problem retrieving data from the server.<br /><br /><br /><br />' ;
		}
	}
}

//Called when response comes back from server
function SetBlogEntry(blogNode)
{
	try{
		var firstEntryNode = blogNode.getElementsByTagName('entry')[0];
		var titleNode = firstEntryNode.getElementsByTagName('title')[0];
		var contentNode = firstEntryNode.getElementsByTagName('content')[0];

		var titleStr = GetInnerText(titleNode);
		var contentStr = GetInnerText(contentNode);

		var blogTitle = GetReferenceToBlogEntryTitle() ;
		var blogLabel = GetReferenceToBlogEntryLabel() ;

		blogTitle.innerHTML = titleStr ;
		//contentStr = contentStr.replace('<embed src="http://www.youtube.com/v/cS4f6wiQJh4&rel=0&color1=0x006699&color2=0x54abd6&border=1" type="application/x-shockwave-flash" wmode="transparent" width="350" height="307" align="right"></embed>','<embed src="http://www.youtube.com/v/cS4f6wiQJh4&rel=0&color1=0x006699&color2=0x54abd6&border=1" type="application/x-shockwave-flash" wmode="transparent" width="350" height="307" align="right"></embed>');
		blogLabel.innerHTML = contentStr ;
	}
	catch(Err){
		var blogLabel = GetReferenceToBlogEntryLabel() ;
		blogLabel.innerHTML = '<br />There was problem retrieving data from the server.<br /><br /><br /><br />' ;
	}
	SetBlogEntry2(blogNode);
	SetBlogEntry3(blogNode);
}

function GetReferenceToBlogEntryLabel()
{
	var spanToReturn ;
	var spanList = document.getElementsByTagName("span");
	for(count = 0 ; count < spanList.length ; count++)
	{
		var currSpanID = spanList[count].id ;
		if(currSpanID != null && currSpanID.length > 0)
		{
			if(currSpanID.indexOf('lblBlogEntryText') != -1)
			{
				spanToReturn = spanList[count];
				break ;
			}
		}
	}
	return spanToReturn ;
}

function GetReferenceToBlogEntryTitle()
{
	var spanToReturn ;
	var spanList = document.getElementsByTagName("span");
	for(count = 0 ; count < spanList.length ; count++)
	{
		var currSpanID = spanList[count].id ;
		if(currSpanID != null && currSpanID.length > 0)
		{
			if(currSpanID.indexOf('lblBlogEntryTitle') != -1)
			{
				spanToReturn = spanList[count];
				break ;
			}
		}
	}
	return spanToReturn ;
}

/* Blog Entry 2 */

function SetBlogEntry2(blogNode)
{
	try{
		var firstEntryNode = blogNode.getElementsByTagName('entry')[1];
		var titleNode = firstEntryNode.getElementsByTagName('title')[0];
		var contentNode = firstEntryNode.getElementsByTagName('content')[0];

		var titleStr = GetInnerText(titleNode);
		var contentStr = GetInnerText(contentNode);

		var blogTitle2 = GetReferenceToBlogEntryTitle2() ;
		var blogLabel2 = GetReferenceToBlogEntryLabel2() ;

		blogTitle2.innerHTML = titleStr ;
		blogLabel2.innerHTML = contentStr ;

		///Show horizontal ruler
		document.getElementById('hrBlogSeparator').style.display = '' ;
	}
	catch(Err){
		var blogLabel2 = GetReferenceToBlogEntryLabel2() ;
		blogLabel2.innerHTML = '<br />There was problem retrieving data from the server.<br /><br /><br /><br />' ;
	}
}

function GetReferenceToBlogEntryLabel2()
{
	var spanToReturn ;
	var spanList = document.getElementsByTagName("span");
	for(count = 0 ; count < spanList.length ; count++)
	{
		var currSpanID = spanList[count].id ;
		if(currSpanID != null && currSpanID.length > 0)
		{
			if(currSpanID.indexOf('lblBlogEntryText2') != -1)
			{
				spanToReturn = spanList[count];
				break ;
			}
		}
	}
	return spanToReturn ;
}

function GetReferenceToBlogEntryTitle2()
{
	var spanToReturn ;
	var spanList = document.getElementsByTagName("span");
	for(count = 0 ; count < spanList.length ; count++)
	{
		var currSpanID = spanList[count].id ;
		if(currSpanID != null && currSpanID.length > 0)
		{
			if(currSpanID.indexOf('lblBlogEntryTitle2') != -1)
			{
				spanToReturn = spanList[count];
				break ;
			}
		}
	}
	return spanToReturn ;
}

/* End of Blog Entry 2 */


/* Blog Entry 3 */

function SetBlogEntry3(blogNode)
{
	try{
		var firstEntryNode = blogNode.getElementsByTagName('entry')[2];
		var titleNode = firstEntryNode.getElementsByTagName('title')[0];
		var contentNode = firstEntryNode.getElementsByTagName('content')[0];

		var titleStr = GetInnerText(titleNode);
		var contentStr = GetInnerText(contentNode);

		var blogTitle3 = GetReferenceToBlogEntryTitle3() ;
		var blogLabel3 = GetReferenceToBlogEntryLabel3() ;

		blogTitle3.innerHTML = titleStr ;
		blogLabel3.innerHTML = contentStr ;

	}
	catch(Err){
		var blogLabel3 = GetReferenceToBlogEntryLabel3() ;
		blogLabel3.innerHTML = '<br />There was problem retrieving data from the server.<br /><br /><br /><br />' ;
	}
}

function GetReferenceToBlogEntryLabel3()
{
	var spanToReturn ;
	var spanList = document.getElementsByTagName("span");
	for(count = 0 ; count < spanList.length ; count++)
	{
		var currSpanID = spanList[count].id ;
		if(currSpanID != null && currSpanID.length > 0)
		{
			if(currSpanID.indexOf('lblBlogEntryText3') != -1)
			{
				spanToReturn = spanList[count];
				break ;
			}
		}
	}
	return spanToReturn ;
}

function GetReferenceToBlogEntryTitle3()
{
	var spanToReturn ;
	var spanList = document.getElementsByTagName("span");
	for(count = 0 ; count < spanList.length ; count++)
	{
		var currSpanID = spanList[count].id ;
		if(currSpanID != null && currSpanID.length > 0)
		{
			if(currSpanID.indexOf('lblBlogEntryTitle3') != -1)
			{
				spanToReturn = spanList[count];
				break ;
			}
		}
	}
	return spanToReturn ;
}

/* End of Blog Entry 2 */


function ShowHideNewsletter(year)
{
	HideAllNewsletter(year) ;
	var selectedList = document.getElementById('monthList_' + year) ;
	if(selectedList.style.display == null || selectedList.style.display == '')
	{
		selectedList.style.display = 'none' ;
	}
	else
	{
		selectedList.style.display = '' ;
	}
}

function HideAllNewsletter(year)
{
	var bonusBox = document.getElementById('bonus_box');
	var ulList = bonusBox.getElementsByTagName('ul');
	for(count = 0 ; count < ulList.length ; count++)
	{
		if(ulList[count].id != null && ulList[count].id.indexOf('monthList_') != -1 && ulList[count].id.indexOf('monthList_' + year) == -1)
		{
			ulList[count].style.display = 'none' ;
		}
	}
}

function SetNewsletterLayout()
{
	try
	{
		var mainDiv = document.getElementById('main');
		mainDiv.style.width = '100%' ;

		var sideDiv = document.getElementById('side');
		sideDiv.style.display = 'none' ;

		///Set width of tables in newsletters
		SetTableWidth();
	}
	catch(Err)
	{}

	return '' ;
}

function SetNewsletterYear(selectedYear)
{
	var monthsActive = document.getElementById('monthsActive');
	monthsActive.style.display = 'none' ;
	
	var monthsHidden = document.getElementById('monthsHidden');
	monthsHidden.style.display = '' ;

	var selectMonthText = document.getElementById('selectMonthText');
	selectMonthText.style.visibility = '' ;

	var selectedYearTableCells = document.getElementById('newsletterYeartbl').getElementsByTagName('td');
	for(count = 0 ; count < selectedYearTableCells.length ; count++ )
	{
		var currentCellID = selectedYearTableCells[count].id ;
		if(currentCellID != null && currentCellID == 'year_' + selectedYear)
		{
			selectedYearTableCells[count].className = 'selected' ;
		}
		else
		{
			selectedYearTableCells[count].className = null ;
		}
	}
	
	var selectedMonthTableCell =  document.getElementById('newsletterMonthtbl').getElementsByTagName('td');
	for(count = 0 ; count < selectedMonthTableCell.length ; count++)
	{
		var currentCellID = selectedMonthTableCell[count].id ;
		if(currentCellID == null || currentCellID.length == 0)
		{
			continue ;
		}
		currentCellID = currentCellID.replace('month_','');
		selectedMonthTableCell[count].className = null ;
		var linksStr = selectedMonthTableCell[count].getElementsByTagName('a');
		linksStr[0].href = 'wa-newsletter.aspx?show=' + selectedYear + currentCellID ;
	}
}

function SetNewsletterMonth(selectedMonth)
{
	var selectedMonthTableCells = document.getElementById('newsletterMonthtbl').getElementsByTagName('td');
	for(count = 0 ; count < selectedMonthTableCells.length ; count++ )
	{
		var currentCellID = selectedMonthTableCells[count].id ;
		if(currentCellID != null && currentCellID == 'month_' + selectedMonth)
		{
			selectedMonthTableCells[count].className = 'selected' ;
		}
		else if(currentCellID != null)
		{
			selectedMonthTableCells[count].className = null ;
		}
	}

	var selectMonthText = document.getElementById('selectMonthText');
	selectMonthText.style.visibility = 'hidden' ;

}

function SelectNewsletterMonthYear(monthYear)
{
	var selectedYear = monthYear.substring(0,4);
	var selectedMonth = monthYear.substring(4);
	SetNewsletterYear(selectedYear) ;
	SetNewsletterMonth(selectedMonth);
	ShowMonths() ;
	return '' ;
}

function ShowMonths()
{
	var monthsHidden = document.getElementById('monthsHidden');
	monthsHidden.style.display = 'none' ;

	var monthsActive = document.getElementById('monthsActive');
	monthsActive.style.display = '' ;

}

function SetTableWidth()
{
	try
	{
		var allNewsletterDivs = document.getElementById('main').getElementsByTagName('div') ;
		for(count = 0 ; count < allNewsletterDivs.length ; count++)
		{
			if(allNewsletterDivs[count].className != null 
				&& allNewsletterDivs[count].className.toLowerCase().indexOf('image-') == 0)
			{
				///Get table within this div
				var currentTable = allNewsletterDivs[count].getElementsByTagName('table');
				for(anotherCount = 0 ; anotherCount < currentTable.length ; anotherCount++ )
				{
					var imageWidth = currentTable[anotherCount].getElementsByTagName('img')[0].width;
					/*
					if(imageWidth > currentTable[anotherCount].width)
					{
						alert(currentTable[anotherCount].getElementsByTagName('img')[0].src);
					}
					*/

					if(currentTable[anotherCount].width != null && currentTable[anotherCount].width > 0)
					{
						allNewsletterDivs[count].style.cssText = 'width: ' + currentTable[anotherCount].width + 'px !important;'
						///currentTable[anotherCount].style.cssText = 'width: ' + currentTable[anotherCount].width + 'px !important;'
						///alert(allNewsletterDivs[count].style.cssText);
					}
				}
			}
		}
	}
	catch(Err)
	{}
}


function HandleHiddenFields()
{
	///Set display = none to all hidden fields

	var allFields = document.getElementsByTagName('input');
	for(var i = 0 ; i < allFields.length ; i++)
	{
		if (allFields[i]["type"] == 'hidden')
		{
			allFields[i].style.display = 'none' ;
		}
	}
}
