/*

 _link.js

Includes functions to display reline version of the code
and
link to code sections based on which document is being displayed

onload and some functions require scrollDn() which is loaded with _coolbluemenu.js
scrollDn() accepts no variables and puts a 200 ms delay before scrolling

*/

// FUNCTIONS RELATING TO DISPLAY OF REDLINE VERSION

// SAVING & READING COOKIE -- TO REMEMBER WHICH DISPLAY HAS BEEN CHOSEN  (IS SHOW REVISION TRUE OR FALSE?)

  function saveRevData(show){
    createCookie("showRev",show,1)
    self.location.reload()
  }

  function saveCaseData(hide){
    createCookie("hideCase",hide,1)
    self.location.reload()
  }

  function createCookie(name,value,days) {
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
  }

  function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
  }

// SHOW REVISION = TRUE OR FALSE

  cookieResult=readCookie("showRev")

  var showRev=false
  if ( cookieResult=="true" ) { 
    showRev=true
  } else {
    showRev=false
  }
  
  if ( showRev ) {
    document.writeln("<style type='text/css'>font.a { text-decoration: underline; color: navy }" )
    document.writeln("font.d { text-decoration: line-through; color: red }" )
    document.writeln("font.de { color: red }</style>" )
  }

// HIDE CASE ANNOTATION = TRUE OR FALSE

  cookieResult=readCookie("hideCase")

  var hideCase=false
  if ( cookieResult=="true" ) { 
    hideCase=true
  } else {
    hideCase=false
  }

// TEXT SHORTCUTS
var textNoChangePara="<p><font class='g'>[No changes made in preceeding section by <I>The Bankruptcy Abuse Prevention and Consumer Protection Act of 2005.</I>]</font></p>"
var textChanged=" <font class='a'>[Changed by <i>BAPCPA</i>]</font>"

// LINKS
function link(url,loc1,loc2) {

  var u=url,l1=loc1,l2=loc2

  // No link given
  if ( u==null || u=="" ) {
    confirmBox( "The referenced material is not availible on this site." )
    return 
  }

  // Links to omitted chapters

  if ( u.substring(0,4) == "ch09" || u.substring(0,2) == "09" ) {
    confirmBox( "Chapter 9, Adjustment of Debts of a Municipality, is omitted from these materials." )
    return
  }
  if ( u.substring(0,4) == "ch11" || u.substring(0,2) == "11" ) {
    confirmBox( "Chapter 11, Reorganization, is omitted from these materials." )
    return
  }
  if ( u.substring(0,4) == "ch12" || u.substring(0,2) == "12" ) {
    confirmBox( "Chapter 12, Adjustment of Debts of a Family Farmer or Fisherman with Regular Annual Income, is omitted from these materials." )
    return
  }
  if ( u.substring(0,4) == "ch15" || u.substring(0,2) == "15" ) {
    confirmBox( "Chapter 15, Ancillary and Other Cross Border Cases, is omitted from these materials." )
    return
  }
  if ( u.substring(0,4) == "frbp" ) {
    confirmBox ( "Federal Rules of Bankruptcy Procedure is omitted from these materials." )
    return
  }

function confirmBox(ex){ 
  if (confirm(ex+"\nWould you like to open a window to search for that material at Cornell University Law School's Legal Information Institute?\n\nOK=Yes - Cancel=No")){
   mapWin = window.open ("http://www4.law.cornell.edu/uscode/","Map","scrollbars=yes,width=800,height=500,toolbar=yes,location=yes,directories=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes")
  } 
} 
 

  // find present document name -- strip from window url

  var name=String(window.location)    // get url of document, including document name
  var nameArray = name.split("/")     // break string into arrays at each "/"
  var l = nameArray.length - 1        // find how many names there are
  var thisDocName = nameArray[l]      // the page name will be the last name in the array, but will have anchor
  var p = thisDocName.indexOf(".htm")
  thisDocName = thisDocName.substring(0,p+4)

  // determine whether link will go to anchor in document--subject to change later
  var noAnchor=true
  if ( u.length > 4 ) { noAnchor=false }

  // determine if links should go to chapters instead of individual sections
  var linkToChapter=false
  if ( thisDocName.substring(0,2) == "ch" ) {    // chapter loaded
    linkToChapter=true
  }

  // determine linked document url
  var linkedDocName = u.substring(0,4) + ".htm"
  if ( u.charAt(0) != "c" ) {
    linkedDocName = "11usc"+linkedDocName
  }

  // determine anchor or hash name
  var anchorName=""
  if ( u.charAt(0)== "0" ) {
    anchorName=u.substring( 1,u.length )
  } else {
    anchorName=u.substring( 0,u.length )
  }

  // go to anchor if in present document
  // otherwise go to new document

// alert("thisDocName="+thisDocName+"; linkedDocName="+linkedDocName )

  var inDoc = false, newDoc = linkedDocName, linkValue = parseFloat( u.substring(0,4) ), thisDoc = thisDocName.substring(0,4)
  if ( thisDocName == linkedDocName ) { inDoc = true }
  if ( thisDoc == "11usc" ) { inDoc = true }
  if ( linkValue > 100 && linkValue < 200 ) { chLink("ch01") }
  if ( linkValue > 300 && linkValue < 400 ) { chLink("ch03") }
  if ( linkValue > 500 && linkValue < 600 ) { chLink("ch05") }
  if ( linkValue > 700 && linkValue < 800 ) { chLink("ch07") }
  if ( linkValue > 1300 && linkValue < 1400 ) { chLink("ch13") }

  function chLink(c){
    var ch=c
    if ( thisDoc == ch ) {
      inDoc = true
      return
    }
    if ( thisDoc == "11usc" ) { 
      inDoc=true 
    } else {
      if ( thisDoc.substring(0,2) == "ch" ) {
        linkedDocName = ch+".htm"
        if ( noAnchor ) {
          anchorName=linkValue
          noAnchor=false
        }
      }
    }
  }

//alert( "linkedDocName="+linkedDocName+"; thisDocName="+thisDocName+"; linkValue="+linkValue+"; u.substring(0,4)="+u.substring(0,4)+"; inDoc="+inDoc+"; " )

  if ( inDoc ) {
    location.hash=anchorName
//alert("location.hash="+anchorName)
    scrollDn()
    return
  } else {
    var fullLink=linkedDocName
    if ( ! noAnchor ) {
      fullLink=fullLink+"#"+anchorName
    }
//alert("New document="+fullLink)
    window.location=fullLink
  }

// alert ("attempted link to document: "+fullLink)



//this.href=linkName
//linkWin = window.open (linkName,"_self")




/*
  // Linking within present document
  if (docName.substring(0,4)==u.substring(0,4) ) {
    
  }
*/


//  var locArray = window.location.split('/')
 
// alert ("array length="+locArray.length)

/*
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
*/


//    linkWin = window.open ("map_bullhead.htm","_self")
//    string.charAt(x) where x = position of character starting at 0


/*
  var href, src, address
  if (loc == "Bullhead"){
    return 
  }
  if ( src == "" ) {
    mapWin = window.open (href,"Map") 
  } else {
    mapWin = window.open ("","Map","scrollbars=yes,width=510,height=350,toolbar=no,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes") 
    mapWin.document.writeln ("<head><title>Map</title></head>")
    mapWin.document.writeln ("<body>")
    mapWin.document.writeln ("<table border='0'>")
    mapWin.document.writeln ("<td><a href='"+href+"'><IMG src='"+src+"' border='3' width='456' height='259' alt='MapBlast! Map! Click to Navigate'></a></td></tr>")
    mapWin.document.writeln ("<tr><td>"+address+"</td>")
    mapWin.document.writeln ("</tr></table></body>")
    mapWin.document.close()
  }
*/
}
