/* ---------------------------------------------------------
   JavaScript Functions used by Documents containing
                 Embedded Canned Queries
   _________________________________________________________ */

/* Used for opening fully customized pop-up windows
 the settings parameter may include the following key-value pairs:
 
 I. Boolean Settings:
 a. toolbar=no
 b. location=no
 c. directories=no
 d. status=no
 e. menubar=no
 f. scrollbars=yes
 g. resizable=no

 II. Numeric Settings:
 a. width=500
 b. height=300
 */
function openWindow (url,name,settings){
   var popupWin = window.open (url,name,settings);
   popupWin.focus();
}

/**
 * Opens a barebones pop-up window.
 * 
 * @param url    the URL of the window's contents
 * @param name   the name of the window
 * @param width  width of pop-up window
 * @param height height of pop-up window
 */
function simplePopup(url, name, width, height){

   openWindow(url, name, 'toolbar=no,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,width='+width+',height='+height);
}

/**
 * Another function for generating a pop-up window.
 * 
 * @param url    url of the resource to display in the pop-up
 * @param name   name of pop-up window
 */
function startWin(url, name) {
   var popupWin = null;
   popupWin = window.open(url, name,'resizable,toolbar,scrollbars,menubar,width=680,height=400');
   // focus window (only for Navigator >= 3.0)
   if ( (navigator.appName != "Microsoft Internet Explorer") && (navigator.appVersion.substring(0,1) >= "3") )
     popupWin.focus();
}

/** Entry point for Query Links **/
function queryLink(publication) {

   if (publication == "billtrack") queryBT(queryLink.arguments[1], queryLink.arguments[2]);
   if (publication == "member") queryMem(queryLink.arguments[1], queryLink.arguments[2]);
   if (publication == "floorvote") queryFV(queryLink.arguments[1]);
   if (publication == "floorvotes-bill") queryFVBill(queryLink.arguments[1], queryLink.arguments[2], queryLink.arguments[3]);
   if (publication == "billtext") queryBillText(queryLink.arguments[1], queryLink.arguments[2]);    
   if (publication == "schedules") querySchedules(queryLink.arguments[1]);   
}

// Author: Hank Hoffman
function doFindURL(collection, query, sortType, sortDirection, congressnum){

   var cannedURL = "find.do";
   cannedURL += ("?dataSource="+ collection);
   cannedURL += ("&queryFragment=" + escape(query));
   if (congressnum != null) {
     cannedURL += ("&congressnum=" + congressnum);
   }
   cannedURL += ("&sortSpec=" + sortType + "+" + sortDirection);
   // for debugging: 
   //alert("Canned URL = \n" + cannedURL +"\n\nClick OK to go there.");

   // If the current window was opened by another window,
   // close this window and load the URL into the parent window.
   if( window.opener ) {
     window.opener.location = cannedURL;
     window.close();
   } else {
     document.location = cannedURL;
   }
}

function doFindPopupURL(collection, query, sortType, sortDirection, congressnum){

   var cannedURL = "find.do";
   cannedURL += ("?dataSource="+ collection);
   cannedURL += ("&queryFragment=" + escape(query));
   if (congressnum != null) {
     cannedURL += ("&congressnum=" + congressnum);
   }
   cannedURL += ("&print=true&sortSpec=" + sortType + "+" + sortDirection);
   // for debugging: 
   //alert("Canned URL = \n" + cannedURL +"\n\nClick OK to go there.");

   startWin(cannedURL, 'newWin');
}

function doBrowseURL(collection, query, sortType, sortDirection, congressnum, title ){

   var cannedURL = "browse.do";
   
   cannedURL += ("?dataSource="+ collection);
   cannedURL += ("&queryFragment=" + escape(query));
   if (congressnum != null) {
     cannedURL += ("&congressnum=" + congressnum);
   }
   cannedURL += ("&sortSpec=" + sortType + "+" + sortDirection);
   cannedURL += ("&dest=doc_or_hitlist");
   
   // Only add a title parameter if a value was passed in
   if( title ) {
     cannedURL += ("&title=" + escape(title) );
   }
   //cannedURL += ("&autoForward=true");
   // for debugging:
   //alert("Canned URL = \n" + cannedURL +"\n\nClick OK to go there.");
   
   // If the current window was opened by another window,
   // close this window and load the URL into the parent window.
   if( window.opener ) {
     window.opener.location = cannedURL;
     window.close();
   } else {
     document.location = cannedURL;
   }
}


function doDisplayCardURL(numberType, billOrLawNumber, congress) {
  var cardURL;
  if (numberType == "publaw") {
    var tempArray = billOrLawNumber.split("-");

    cardURL = "loadlawcard.do";
    cardURL += "?lawNumber="+tempArray[1];
  }
  else {
    cardURL = "loadbillcard.do";
    cardURL += "?billNumber="+billOrLawNumber;
  }

  cardURL += "&congress="+congress;

  // for debugging:
  //alert("Card URL = \n" + cardURL +"\n\nClick OK to go there.");

  simplePopup(cardURL,'billCard',680,430);
}

function showSenateWatch() {
  startWin("http://www.cq.com/flatfiles/editorialFiles/senatewatch/index.html", "newWin");
}

function showArchive() {
  startWin("displayarchives.do", "newWin");
}


function what(){
   alert("What !?");
}

function queryBT(billno, congressno) {

   var form = document.form;
   var query;

   var billnoval = getvalfrompair(billno);
   var optionname = getnamefrompair(billno);
   var congressnoval = getvalfrompair(congressno);

   query =    billnoval + "<in>"+optionname;

   if (congressnoval < 107) { // hard coded, no bill card before 107th
     doFindURL("billtrack", query, "displaydate", "desc", congressnoval);
     return;
   }
   var currcongress = getCurrentCongress();

   doDisplayCardURL(optionname, billnoval, congressnoval);
}

function queryMem(memcodesno, congressno) {
   var form = document.form;
   var query = "";
   var memcodesnoval = getvalfrompair(memcodesno);
   var congressnoval = getvalfrompair(congressno);
   var currcongress = getCurrentCongress();
   var databaserequested;
   if ( congressnoval == currcongress ) {
      databaserequested = "memberchild";
      query = "("+memcodesnoval+")<IN>memcodes<and>(summary)<in>memberreports";
   }
   else {
      databaserequested = "memberhistoric";
      query = "("+memcodesnoval+")<IN>memcodes";
   }

   doFindPopupURL(databaserequested, query, "memcodes", "desc", congressnoval);
}

function queryFV(voteno) {
   var form = document.form;
   var query = "";
   var databaserequested = "floorvote";

   var votenoval = getvalfrompair(voteno);
   var voteyear = votenoval.substring(1,5);

   var congressnumber = getCongressFromYear(voteyear);
   var currcongress = getCurrentCongress();
   query = "(" + votenoval + ")<IN>votesortid";

   doFindURL(databaserequested, query, "publdate", "desc", congressnumber);
}

function queryFVBill(billno, congressno, chamber) {

   var form = document.form;
   var query;
   var databaserequested = "floorvote";
   
   // Build the title string that will be passed in the querystring and displayed on the hitlist
   var title = "CQ Floor Votes for " + beautifyBillNumber( getvalfrompair(billno) );

   var billnoval = getvalfrompair(billno);
   var optionname = getnamefrompair(billno);
   var congressnoval = getvalfrompair(congressno);
   var chamberval = getvalfrompair(chamber);
   
   query = billnoval + "<in>"+optionname + "<and>" + chamberval +"<in>chamber";

   doBrowseURL(databaserequested, query, "publdate", "desc", congressnoval, title);
}

function queryBillText(billno, congressno) {

   var form = document.form;
   var query;
   var databaserequested = "billtext";   

   var billnoval = getvalfrompair(billno);
   var optionname = getnamefrompair(billno);
   var congressnoval = getvalfrompair(congressno);

   query =    billnoval + "<in>"+optionname;

   doBrowseURL(databaserequested, query, "publdate", "desc", congressnoval);
}

function querySchedules(oid) {	 
   var query = "";	 
    	 
   var oidval = getvalfrompair(oid);	 
    	 
   query = "(" + oidval + ")<IN>oid";	 
   doFindURL("schedules,hsschedules", query, "actiondate", "asc");	 
}

/**
 * This function ensures a link is opened in the main window.
 * If the link is clicked in a pop-up window, the pop-up is
 * closed and the link is loaded in the parent window.
 * If the current window is not a pop-up window, the link
 * loads into the current window
 *
 * @author       Mark Kennedy, Really Strategies, Inc.
 * @param url    url of the resource to display
 */
function loadIntoMainWindow( url ) {
  if( window.opener ) {
    window.opener.location.href = url;
    window.close();
  } else {
    window.location.href = url;
  }
}

/**
 * This function loads links to sources from the Source Card.
 * It passes it's URL through to loadLinkToMainWindow() to
 * ensure the link loads in the main window.
 *
 * @author       Mark Kennedy, Really Strategies, Inc.
 * @param url    url of the resource to display
 */
function doSourceCardLink( url ) {
  loadIntoMainWindow( url );
}

/********************* Miscellaneous Functions: **********************/

function getvalfrompair (pairelement) {
   var valelement;
   tempArray = new Array();
   tempArray = pairelement.split("=");
   return tempArray[1];
}

function getnamefrompair (pairelement) {
   var valelement;
   tempArray = new Array();
   tempArray = pairelement.split("=");
   return tempArray[0];
}

function getCurrentCongress() {
   var currcongress = "108";
   return currcongress;
}

function getCongressFromYear(voteyear) {
   var returnedv = (voteyear - 1787) / 2;
   var specificcongress = parseInt(returnedv);
   return specificcongress;
}

function getDBArea(rcongress, ccongress) {
   var dbnameadd;
   if (rcongress == ccongress){
        dbnameadd = "";
   } else {
        dbnameadd = "archive";
   }
   return dbnameadd;
}

function getDBYear(ryear, cyear) {
   var dbnameadd;
   if (ryear == cyear){
        dbnameadd = "";
   } else {
        dbnameadd = "archive";
   }
   return dbnameadd;
}

/**
*  This function takes an old style bill number
*  and returns a CQ bill number.
*
*  It mimics the logic/formatting rules in the
*  BillNumberUtils java class
*
*  @author       Mark Kennedy, Really Strategies, Inc.
*  @param billNumber String to be beautified
*/
function beautifyBillNumber( billNumber ) {
    var CQBillNumber = "";
    if(billNumber.indexOf("HCONRES") == 0) {
      CQBillNumber = "H Con Res " + billNumber.substr(7);
    } else if (billNumber.indexOf("SCONRES") == 0) {
      CQBillNumber = "S Con Res " + billNumber.substr(7);
    } else if (billNumber.indexOf("HJRES") == 0) {
      CQBillNumber = "H J Res " + billNumber.substr(5);
    } else if (billNumber.indexOf("SJRES") == 0) {
      CQBillNumber = "S J Res " + billNumber.substr(5);
    } else if (billNumber.indexOf("SRES") == 0) {
      CQBillNumber = "S Res " + billNumber.substr(4);
    } else if (billNumber.indexOf("HRES") == 0) {
      CQBillNumber = "H Res " + billNumber.substr(4);      
    } else if (billNumber.indexOf("HR") == 0) {
      CQBillNumber = "HR " + billNumber.substr(2);
    } else if (billNumber.indexOf("S") == 0) {
      CQBillNumber = "S " + billNumber.substr(1);
    } else if (billNumber.indexOf("D") == 0) {
      CQBillNumber = "Draft Bill";
    } else if (billNumber.indexOf("T") == 0) {
      CQBillNumber = "Treaty Doc " + billNumber.substr(2);
    } else {
      CQBillNumber = billNumber;
    }
    return CQBillNumber;
}

