/***************************************************************
EXTRANET JAVASCRIPT
****************************************************************/

function GetMonth(intMonth){
    var MonthArray = new Array("January", "February", "March",
                               "April", "May", "June",
                               "July", "August", "September",
                               "October", "November", "December") 
    return MonthArray[intMonth] 	  	 
}
function getDateStr(){
    var today = new Date()
    var year = today.getYear()
    if(year<1000) year+=1900
    var todayStr = GetMonth(today.getMonth()) + " " + today.getDate()
    todayStr += ", " + year
    return todayStr
}
function ClearForm(){
	var e = document.getElementById('download_id');
	if(e != null)
		e.value = '';
}
function DownloadFile(e){
	document.getElementById('download_id').value = e.split('?m=')[1];
	this.Submit();
}

function FormatDates(){
	var list = document.getElementsByTagName('span');
	var count = list.length;
	for(var i = 0; i<count; i++){
		var e = list[i];
		if(e.title == 'date'){		
			var d = e.innerText.split('/');
			var mm = GetMonthName(d[0]);
			var dd = parseInt(d[1], 10);
			var yy = d[2];
			e.innerText = mm + ' ' + dd + ', ' + yy;
			/*
			var d = new Date(e.innerText);
			
			var mm = GetMonthName(d.getMonth());
			var dd = d.getDay();
			var yy = d.getFullYear();
			e.innerText = mm + ' ' + dd + ', ' + yy;
			*/
			
		}
	}
	//Call displayAnchors
	//displayAnchors(releaseDatesArray);
	
}
function populateReleaseDateAnchors(){
var list = document.getElementsByTagName('span');
	var count = list.length;
	var releaseDatesArray = new Array();
	var y=0;	
	for(var i = 0; i<count; i++){
		var e = list[i];
		if(e.title == 'date'){		
			//Popullate release dates array
			releaseDatesArray[y]=e.innerText
			y++;
			
		}
	}
//Call displayAnchors
displayAnchors(releaseDatesArray);
} 

function populateCountryRegionAnchors() {
	var list = document.getElementsByTagName('span');
	var tempString = '';
	var count = list.length;
	var CountryRegionArray = new Array();
	var y=0;	
	for(var i = 0; i<count; i++){
		var e = list[i];

		if(e.title == 'countryregion'){		

			
			if (y> 0) {
				if (e.innerText == CountryRegionArray[y-1]) {
					// do nothin
				} else
				{
					CountryRegionArray[y]=e.innerText;
					y++;
				}
			} else
			{
				CountryRegionArray[y] = e.innerText;
				y++;

			}
		}
	}
	
	
//Call displayAnchors
displayCountryRegionAnchors(CountryRegionArray);
} 


function GetToday(){
	return getDateStr();
}
function GetMonthName(num){
	//if(num < 1)
	//	num = 1;
	//parseInt base 10 to avoid 09 returned as 0
	var v = parseInt(num, 10);
	switch(v){
		case(1):
			return 'January';
		case(2):
			return 'February';
		case(3):
			return 'March';
		case(4):
			return 'April';
		case(5):
			return 'May';
		case(6):
			return 'June';
		case(7):
			return 'July';
		case(8):
			return 'August';
		case(9):
			return 'September';
		case(10):
			return 'October';
		case(11):
			return 'November';
		case(12):
			return 'December';
		default:
			return 'January';
	}
}

function ConditionLinks(){
	document.getElementById('download_id').value = '';
	var links = document.getElementsByTagName('a');
	var len = links.length;
	for(var i = 0; i<len; i++){
		var lnk = links[i];
		if(lnk.title != 'Digital Asset')
			continue;
		if(lnk.name == '1'){		
			lnk.target = '_blank';
			continue;
		}
		
		lnk.target = '';
		lnk.href = 'javascript:DownloadFile("'+lnk.href.split('&')[0]+'");';
	}
}

var nc;
function FlipNotification(){	
	if(nc == null)
		nc = document.getElementById('notificationContainer');
	if(nc.style.display == 'none')
		nc.style.display = 'block';
	else
		nc.style.display = 'none';
}

function displayAnchors(releaseDatesArray){

	var theYear ="";
	var y=0;
	var year="";
	var month="";
	var prevMonth="";
	var htmlAnchors="";
	
	for(x=0; x<releaseDatesArray.length;x++){
	var j = releaseDatesArray[x].split('/');
	month = j[0];
	// set year after looping thru the first time as the year is being set the first time if year ="";
		if(x>0){
		year = j[2];
		}
		if(year==""){
		year = j[2];
		theYear = year;
		htmlAnchors="<table><tr><td valign='top' width='65' align='left' style='padding-right:5px;'>";
		htmlAnchors+="<div class='set'><div id='GalleryAnchorHeader' style='padding-left:5px;'>"+year+"</div>";
		htmlAnchors+="<div class='subItem'><div id='brandAnchor'>&bull;<a href='#"+releaseDatesArray[x]+"' id='brandAnchor' style='font-weight:normal;'>"+GetMonth(month-1)+"</a></div>";	
		}
		else if((year == theYear) && (prevMonth!=month)){
		//close subitem
		htmlAnchors+="</div>"
		htmlAnchors+="<div class='subItem'><div id='brandAnchor'>&bull;<a href='#"+releaseDatesArray[x]+"' id='brandAnchor' style='font-weight:normal;'>"+GetMonth(month-1)+"</a></div>";	
		}
		else if(year != theYear){
		htmlAnchors+="</div></div>";
		htmlAnchors+="</td><td valign='top' width='65' align='left' style='padding-right:5px;'>";
		htmlAnchors+="<div class='set'><div id='GalleryAnchorHeader' style='padding-left:5px;'>"+year+"</div>";

			if(prevMonth!=month){
			htmlAnchors+="<div class='subItem'><div id='brandAnchor'>&bull;<a href='#"+releaseDatesArray[x]+"' id='brandAnchor' style='font-weight:normal;'>"+GetMonth(month-1)+"</a></div>";
			}
		}
		
	theYear = year;
	prevMonth = month;
	} 
	
	//close div class subitem and div class set
	htmlAnchors+="</div></div>";
	htmlAnchors+="</td></tr></table><br><br><br>";
	document.getElementById('groupBox').innerHTML=htmlAnchors;
}

function displayCountryRegionAnchors(CountryRegionArray){
// implemented for soco, standard across pressrooms
var y=0;
var prevCountryRegion="";
var htmlAnchors="";
var shiftFlag=0;
var ix=CountryRegionArray.length;
var tempString = "";
var globalFlag = 0;
var urlLoc = location.href;

x=0;
while (x<ix) {
	if (CountryRegionArray[x] == "Global") {
		globalFlag = 1;
		x=ix;
	}
	x++;
}


	if (globalFlag==1) {

		if (ix > 2) {

			var newCountryRegionArray = new Array();
			newCountryRegionArray[0]="Global";
			var currentPtr = 1;
			for(x=0;x<=ix-1;x++){

				if (CountryRegionArray[x] != "Global") {
					newCountryRegionArray[currentPtr] = CountryRegionArray[x];
					currentPtr++;
				}
			}

			htmlAnchors="<table><tr>";
			for(x=0; x<newCountryRegionArray.length;x++){
				if ((typeof newCountryRegionArray[x]) != "undefined") {
					htmlAnchors+="<td width='100' align='left'><a href='#"+newCountryRegionArray[x]+"' id='brandAnchor' class='CompositeLink3'>"+newCountryRegionArray[x]+"</a></td>";	
				}
			} 
			htmlAnchors+="</tr></table><br>";
			document.getElementById('groupBox').innerHTML=htmlAnchors;
		}
		
	}
	if (globalFlag==0) {
		if (ix > 2) {

			htmlAnchors="<table><tr>";
			for(x=0; x<CountryRegionArray.length;x++){
				if ((typeof CountryRegionArray[x]) != "undefined") {
					htmlAnchors+="<td width='100' align='left'><a href='#"+CountryRegionArray[x]+"' id='brandAnchor' class='CompositeLink3'>"+CountryRegionArray[x]+"</a></td>";	
				}
			} 
			htmlAnchors+="</tr></table><br>";
			document.getElementById('groupBox').innerHTML=htmlAnchors;
		}
		
	}
	if (ix==2) {
		if (CountryRegionArray[ix-1]=="Global") {
			CountryRegionArray[ix-1] = CountryRegionArray[0];
			CountryRegionArray[0] = tempString;
			}
		htmlAnchors="<table><tr>";
		for(x=0; x<CountryRegionArray.length;x++){
			if ((typeof CountryRegionArray[x]) != "undefined") {
				htmlAnchors+="<td width='100' align='left'><a href='#"+CountryRegionArray[x]+"' id='brandAnchor' class='CompositeLink3'>"+CountryRegionArray[x]+"</a></td>";	
			}
		} 

		htmlAnchors+="</tr></table><br>";
		document.getElementById('groupBox').innerHTML=htmlAnchors;

	}

	if (ix==1) {
		htmlAnchors="<table><tr>";
		htmlAnchors+="<td width='100' align='left'><a href='#"+CountryRegionArray[0]+"' id='brandAnchor' class='CompositeLink3'>"+CountryRegionArray[0]+"</a></td>";	
		htmlAnchors+="</tr></table><br>";
		document.getElementById('groupBox').innerHTML=htmlAnchors;
	}

}



//Used in JDDP Releases Presentation File to get query string value by calling the name 
function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
  return false;
  //alert('Query Variable ' + variable + ' not found');
}
//Used in JDDP Releases Presentation File to build the URL for Printview
function gotoURL(location){
var gotoLocation = location+"?&printerFriendly=Y";
//alert(gotoLocation);
window.open(gotoLocation,'myWin');
}

//Used in JDDP website for search
function checkSearchString(){
var thisURL = location.href;
var queryStart= thisURL.lastIndexOf('?Query=')+7;
var query = thisURL.substring(queryStart,thisURL.length) ;
document.getElementById("search_txtSearch").value = query;
StartSearch(query)
}



function StartSearch(query){
var searchComponent = "/irj/servlet/prt/portal/prtroot/pcd!3aportal_content!2fcom.b-f.folder.bfContent!2fcom.b-f.PressRoom!2fcom.b-f.SCDP!2fcom.b-f.iViews!2fcom.b-f.SCDPSearch?QueryString=";

var srchQuery = searchComponent+query;
//alert(srchQuery);
if(Trim(query)!=""){
	if(location.href.lastIndexOf('index.jsp')>0){
		window.parent.location.href="./generate_abstract/Search.jsp?Query="+query;
	}
	else if(location.href.lastIndexOf('Search.jsp')==-1){
		window.parent.location.href="../generate_abstract/Search.jsp?Query="+query;
	}
var iFrameString = "loadintoIframe('myframe','"+ srchQuery +"')";
setTimeout(iFrameString,1000);
//parent.document.getElementById('myframe').style.height=this.document.body.scrollHeight+"px";
}
}


function checkKeyPress(event, query){
// key is ascii keycode
var key = ((navigator.appName == "Netscape") ? event.which : event.keyCode)
// enter is ASCII 13
isEnter = key == 13
if(isEnter) 
{
//return true;
StartSearch(query);
}
else
{
return false;
}
}
//TRIM
function Trim(s)
{
    return s.replace(/(^\s+)|(\s+$)/g, "")
}
