/*
  NAME     : printerFriendly
  ABSTRACT : Opens a new window which loads a printer-friendly version of the current page
  
  COPYRIGHT NOTICE : Function taken from MIT Technology Review web site
 */
 
function printerFriendly(url, width, height, winName) {
  var features = "bookmarks=yes,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=no,width=" + width + ",height=" + height;
  if (winName == "") {
  	winName = "childWin";
  }
  SmallWin = window.open(url,winName,features);
  if (window.focus) {
    SmallWin.focus();
  }
}



/*
   NAME     : CheckForm
   ABSTRACT : Checks the form before submitting to the search engine. 
   
 */

function CheckForm () {

	//Check for a word to search
	if (document.frmSiteSearch.search.value==""){
		alert("Please enter at least one keyword to search");
		document.frmSiteSearch.search.focus();
		return false;
	}
	
	return true
}

/*
  Uploads the contents of a given page to the side bar.
  for the purpose, the page being loaded must have a <div> 
  element with id="Contents".
  
  If that is the case, then the HTML get copied.
 */

function showContents()
{
	var side_content = document.getElementById('SideContents');
	var content = document.getElementById('Contents');
	var side_news = document.getElementById('SideNews');
	var news = document.getElementById('News');
	var side_contact = document.getElementById('SideContact');
	var contact = document.getElementById('Contact');
	
	if (content) {
		side_content.innerHTML += content.innerHTML;
	} else {
		side_content.innerHTML = '';
    side_content.style.visibility = 'hidden';
  }
	
	if (news) {
		side_news.innerHTML += news.innerHTML;
	} else {
		side_news.innerHTML = '';
    side_news.style.visibility = 'hidden';
  }
	
	if (contact) {
		side_contact.innerHTML += contact.innerHTML;
	} else {
		side_contact.innerHTML = '';
    side_contact.style.visibility = 'hidden';
  }
	
	side_content = null;
	content = null;
	side_news = null;
	//news = null;
	side_contact = null;
	contact = null;
}


/*
 *  Changes the visibility of the sidebar
 */
function toggleSidebar()
{
		var side_bar = document.getElementById('sidebar');
		var main_content = document.getElementById('main');
		
		if (side_bar.style.visibility == 'visible'){
			side_bar.style.visibility = 'hidden';
			side_bar.style.display = 'none';
			main_content.style.marginRight = 10;
		}else{
			side_bar.style.visibility = 'visible';
			side_bar.style.display = 'block';
			main_content.style.marginRight =250;
		}
		return true;
}


/*
 *  Turns off visibility of the sidebar
 */
function toggleSidebarOff()
{
		var side_bar = document.getElementById('sidebar');
		var main_content = document.getElementById('main');
		
		side_bar.style.visibility = 'hidden';
		side_bar.style.display = 'none';
		main_content.style.marginRight = 10;

		return true;
}


/* 
 * Following function called when the document is loaded
 */
function onDocumentLoad()
{
	showContents();
	toggleSidebar();
}


/*
 * This function parses &-separated name=value argument pairs from
 * the query string of the URL. It stores the name=value pairs in
 * properties of an object and returns that object
 */

function getArgs() {
  var args = new Object();
  var query = location.search.substring(1);   // Get query string
  var pairs = query.split("&");               // Break at ampersand
  for(var i=0; i<pairs.length; i++) {
    var pos = pairs[i].indexOf('=');          // Look for name=value
    if (pos==-1) continue;                    // If not found, skip
    var argname = pairs[i].substring(0,pos);  // Extract the name
    var value = pairs[i].substring(pos+1);    // Extract the value
    args[argname] = unescape(value);          // Store as a property
  }
  return args;
}    



/*
 *  Changes the visibility of the div
 */
function expandDiv(sDiv,sImg)
{
		var eDiv = document.getElementById(sDiv);
		var eImg = document.getElementById(sImg);
		
		eDiv.style.visibility = 'visible';
		eDiv.style.display = 'block';

		return true;
}

/*
 *  Expand div and change icon  
 */
function expandDiv(sDiv,sImg)
{
		var eDiv = document.getElementById(sDiv);
		var eImg = document.getElementById(sImg);
		
		eImg.src = '/images/icon_minus.gif';
		eImg.onClick = 'collapseDiv(sDiv,sImg);';
		eDiv.style.visibility = 'visible';
		eDiv.style.display = 'block';
		
		return true;
}

/*
 *  Collapse div and change icon  
 */
function collapseDiv(sDiv,sImg)
{
		var eDiv = document.getElementById(sDiv);
		var eImg = document.getElementById(sImg);
		
		eImg.src = '/images/icon_plus.gif';
		eImg.onclick = 'expandDiv(sDiv,sImg);';
		eDiv.style.visibility = 'hidden';
		eDiv.style.display = 'none';
				
		return true;
}

/*
 *  Expand/Collapse div and icon  
 */
function toggleDiv(sDiv,sImg)
{
		var eDiv = document.getElementById(sDiv);
		var eImg = document.getElementById(sImg);
		
		if (eDiv.style.visibility == 'visible'){
  		eImg.src = '/images/icon_plus.gif';
  		eImg.onclick = 'expandDiv(sDiv,sImg);';
  		eDiv.style.visibility = 'hidden';
  		eDiv.style.display = 'none';  		
		}else{
  		eImg.src = '/images/icon_minus.gif';
  		eImg.onclick = 'collapseDiv(sDiv,sImg);';
  		eDiv.style.visibility = 'visible';
  		eDiv.style.display = 'block';  		
		}

		return true;
}


function convert4Issue2Month(nIssue){
  var sMonth;
  switch (nIssue){
    case 1:
      sMonth = "Jan";
      break;
    case 2:
      sMonth = "Apr";
      break;
    case 3:
      sMonth = "Jul";
      break;
    case 4:
      sMonth = "Oct";
      break;
  }
  return sMonth;
}

function convert6Issue2Month(nIssue){
  var sMonth;
  switch (nIssue){
    case 1:
      sMonth = "Jan";
      break;
    case 2:
      sMonth = "Mar";
      break;
    case 3:
      sMonth = "May";
      break;
    case 4:
      sMonth = "Jul";
      break;
    case 5:
      sMonth = "Sep";
      break;
    case 6:
      sMonth = "Nov";
      break;      
  }
  return sMonth;
}

function convert12Issue2Month(nIssue){
  var sMonth;
  switch (nIssue){
    case 1:
      sMonth = "Jan";
      break;
    case 2:
      sMonth = "Feb";
      break;
    case 3:
      sMonth = "Mar";
      break;
    case 4:
      sMonth = "Apr";
      break;
    case 5:
      sMonth = "May";
      break;
    case 6:
      sMonth = "Jun";
      break;
    case 7:
      sMonth = "Jul";
      break;
    case 8:
      sMonth = "Aug";
      break;
    case 9:
      sMonth = "Sep";
      break;
    case 10:
      sMonth = "Oct";
      break;
    case 11:
      sMonth = "Nov";
      break;
    case 12:
      sMonth = "Dec";
      break;      
  }
  return sMonth;
}

function convertVol2Year(nVol){
  var nYear = 1953 + nVol;
  var sYear = String(nYear);
  return sYear;  
}

/*
 * Write link to Transactions issue
 */
function writeTr4VolLink(nVol,nIssue) {
  var sVol = String(nVol);
  var sIssue = String(nIssue);
  var sMonth = convert4Issue2Month(nIssue);
  var sYear = convertVol2Year(nVol);
  var sHref = '<tr><td><a href="/main/publications.asp?type=tr&vol=' + sVol + '&issue=' + sIssue + '">Vol ' + sVol + ' Issue ' + sIssue + '</a></td><td> (' + sMonth + ' ' + sYear + ')</td></tr>';
  document.write(sHref);
  return true;
} 

function writeTr6VolLink(nVol,nIssue) {
  var sVol = String(nVol);
  var sIssue = String(nIssue);
  var sMonth = convert6Issue2Month(nIssue);
  var sYear = convertVol2Year(nVol);
  var sHref;
  
  // Special Case: Vol 31 Issue 2 was published in 2 parts
  if ( (nVol==31) & (nIssue==2) ){
    sHref = '<tr><td><a href="/main/publications.asp?type=tr&vol=' + sVol + '&issue=' + sIssue + '&part=1">Vol ' + sVol + ' Issue ' + sIssue + ' Part I </a></td><td> (' + sMonth + ' ' + sYear + ')</td></tr>';
    document.write(sHref);
    
    sHref = '<tr><td><a href="/main/publications.asp?type=tr&vol=' + sVol + '&issue=' + sIssue + '&part=2">Vol ' + sVol + ' Issue ' + sIssue + ' Part II </a></td><td> (' + sMonth + ' ' + sYear + ')</td></tr>';
    document.write(sHref);
  }
  else {
    sHref = '<tr><td><a href="/main/publications.asp?type=tr&vol=' + sVol + '&issue=' + sIssue + '">Vol ' + sVol + ' Issue ' + sIssue + '</a></td><td> (' + sMonth + ' ' + sYear + ')</td></tr>';
    document.write(sHref);
  }
  return true;
} 

function writeTr12VolLink(nVol,nIssue) {
  var sVol = String(nVol);
  var sIssue = String(nIssue);
  var sMonth = convert12Issue2Month(nIssue);
  var sYear = convertVol2Year(nVol);
  var sHref = '<tr><td><a href="/main/publications.asp?type=tr&vol=' + sVol + '&issue=' + sIssue + '">Vol ' + sVol + ' Issue ' + sIssue + '</a></td><td> (' + sMonth + ' ' + sYear + ')</td></tr>';
  document.write(sHref);
  return true;
} 

function writeTrVolLinks(nVol,nIssue) {
  var ii;
  for (ii=nIssue;ii>=1;ii--){
    switch (nIssue){
      case 4:
        writeTr4VolLink(nVol,ii);
        break;
      case 6:
        writeTr6VolLink(nVol,ii);
        break;
      case 12:
        writeTr12VolLink(nVol,ii);
        break;
    }
  }
  return true;
} 



function openTrVol(sVol){
  var nYr = parseInt(sVol) + 1953;  
  var sYr = String(nYr);
  var sDecade = sYr.substr(0,3) + '0s';
  var sDiv_Decade = 'div_' + sDecade;
  var sImg_Decade = 'img_' + sDecade;
  var sDiv_Yr = 'div_' + sYr;
  var sImg_Yr = 'img_' + sYr;
  
  if ( toggleDiv(sDiv_Decade,sImg_Decade) ){
    toggleDiv(sDiv_Yr,sImg_Yr);
  }
  
  return true;
}