// JavaScript Document, let's minify when we're done




function clearIt(n) {
    var name = n;
	document.getElementById(name).value="";
}

function swap(url, id) {
    if(document.images) {
        document.images[id].src = url;
    }
}
	


/* --------------------------------------------------------- */
/* for MarkLogic search of New York Court Decisions */
/* --------------------------------------------------------- */

// var theDomain = ""; //declare theDomain if not set in the calling jsp page

// global variables for pagination, intitial search, etc
var currentPageCount = 0;
var currentUrl = "";
var resultsPerPage = 25; //results per page limit -- set to desired number if not passed from form
var start = 1; //default for initial search return
var end = resultsPerPage;  //default for initial search return
var userId = "unknownuser";  //default if user is not signed in
var beginString = "";
var endString1 = "&sortField=date&sortOrder=descending";
//var endString1 = "&sortField=rank&sortOrder=descending";
//var endString2 = "&box1=on&callback=?";
var endString2 = "&source=lawdecision&scully=mulder&callback=?";
var nxt = "\"next\"";
var prev = "\"previous\"";
var numResults = 0; 
var numPages = 0;
var decisionLink="http://www.newyorklawjournal.com//CaseDecisionNY.jsp?id=";
var pdfUrl = "";
var publicationName = "law.com";  //default
var adDivider = "<div class='adDivider'></div>";
var errorMessage = "Sorry, a problem has occurred - please try your search again.<br />";
var pageType = "";
var browserIE; 
if(navigator.appName == 'Microsoft Internet Explorer') {
    browserIE = true;
}



function displayResults(txt,pt) { //pass the json object, parse the results, cleanup and display

	var text = txt;
	var pageType = pt; //publicNotices or classifieds
	
	//decisions vars
	
	var headline;
	var court;
	var practicearea;
	var thePubdate;
	var decisionID;
	var casenumber;
	

   $.each(text.searchresults.result, function(i,item){
	
      try {
      
		 if(text.searchresults.count==1) {
	
			 headline= text.searchresults.result.HEADLINE;
			 court= text.searchresults.result.COURT;
			 
			 practicearea= text.searchresults.result.PRACTICEAREAS;
			 thePubdate = text.searchresults.result.PUBLICATION_DATE;
			 decisionID = text.searchresults.result.DOC_ID;
			 casenumber = text.searchresults.result.CASENUMBER;
			 
			 //decisions: check to see if Marklogic is adding the span node on search terms
			 if(headline!= null) {
				 if(typeof headline == "object") {
					 headline = text.searchresults.result.HEADLINE.span;
				 }
			 }
			 
			 if(court!= null) {
				 if(typeof court == "object") {
					 court = text.searchresults.result.COURT.span;
				 }
			 }
			 
			 if(practicearea!= null) {
				 if(typeof practicearea == "object") {
					 practicearea = text.searchresults.result.PRACTICEAREAS.span;
				 }
			 }
			 
			
			 if(thePubdate!= null) {
				 if(typeof thePubdate == "object") {
					 thePubdate = text.searchresults.result.PUBLICATION_DATE.span;
				 }
			 }
			 
			 if(decisionID!= null) {
				 if(typeof decisionID == "object") {
					 decisionID = text.searchresults.result.DOC_ID.span;
				 }
			 }
			 
			 if(casenumber!= null) {
				 if(typeof casenumber == "object") {
					 casenumber = text.searchresults.result.CASENUMBER.span;
				 }
			 }
			 
			


		 } else if(text.searchresults.count>0) {

			 
			 //decisions
			 headline = item.HEADLINE;
			 court = item.COURT;
			 practicearea = item.PRACTICEAREAS;
			 thePubdate = item.PUBLICATION_DATE;
			 decisionID = item.DOC_ID;
			 casenumber = item.CASENUMBER;
			 
			 
			 //decisions: check to see if Marklogic is adding the span node on search terms
			 if(headline!= null) {
				 if(typeof headline == "object") {
					 headline = item.HEADLINE.span;
				 }
			 }
			 
			 if(court!= null) {
				 if(typeof practicearea == "object") {
					 court = item.COURT.span;
				 }
			 }
			 
			 if(practicearea!= null) {
				 if(typeof practicearea == "object") {
					 practicearea = item.PRACTICEAREAS.span;
				 }
			 }
			 
			 if(thePubdate!= null) {			 
				 if(typeof thePubdate == "object") {
					 thePubdate = item.PUBLICATION_DATE.span;
				 }
			 }
			 
			 if(decisionID!= null) {			 
				 if(typeof decisionID == "object") {
					 decisionID = item.DOC_ID.span;
				 }
			 }
			 if(casenumber!= null) {			 
				 if(typeof casenumber == "object") {
					 casenumber = item.DOC_ID.span;
				 }
			 }
			 
		  }
		  
		  
		 //decisions 
		 if(headline == null || headline==undefined) {
			headline = "undefined";
		 }
		 
		 if(court == null || court==undefined) {
			court = "";
		 }
		 
		 if(practicearea == null || practicearea==undefined) {
			practicearea = "undefined";
		 }
		 

		 if(thePubdate == null || thePubdate==undefined) {
			thePubdate = "undefined";
		 }
		 
		 if(decisionID == null || decisionID==undefined) {
			decisionID = "undefined";
		 }
		 
		 if(casenumber == null || casenumber==undefined) {
			casenumber = "undefined";
		 }

		 //Clean up the results

		 thePubdate = thePubdate.replace(/00:00:00.000/,"");
		 thePubdate = thePubdate
		 thePubdate = thePubdate.replace(/\w.{0,8}.000/," ");
		 
		 
		 var formattedPubDate;
		 
		 var year;
		 var day;
		 var month;
		 var monthText;
		 
		 var monthsList = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
		 
		 var values = thePubdate.split(/[T\+-]/);
		 year = values[0];
		 month = values[1];
		 day = values[2];
		 
		 if(month == 1) {
		 	monthText = "January";
		 } else if(month == 2) {
		 	monthText = "February";
		 } else if(month == 3) {
		 	monthText = "March";
		 }else if(month == 4) {
		 	monthText = "April";
		 } else if(month == 5) {
		 	monthText = "May";
		 } else if(month == 6) {
		 	monthText = "June";
		 } else if(month == 7) {
		 	monthText = "July";
		 } else if(month == 8) {
		 	monthText = "August";
		 } else if(month == 9) {
		 	monthText = "September";
		 } else if(month == 10) {
		 	monthText = "October";
		 } else if(month == 11) {
		 	monthText = "November";
		 } else if(month == 12) {
		 	monthText = "December";
		 } else {
		 	monthText = "undefined";
		 }
		 
		 //remove leading 0's from single digit day values, add in a comma before the extra space appearing after the day value
		 
		 day=day.replace(/^0/,"");
		 day = day.replace(" ", ", ");		 
		 
		 //put it all together
		 formattedPubDate = monthText + " "  + day + year;


		 //display the results
		 $("#message2").html("&nbsp;");
		 

		 if(text.searchresults.count==1) {
			
			$("#results").html("<div class=\"digest\"><h5 class=\"case\">"+court+"</h5><h3><a href='"+decisionLink+decisionID+"'>" + headline + "</a></h3><p class=\"decided\">Date Published: " + formattedPubDate + "</p></div>");

		
			// $("#results").append(adDivider);

		 } else if(text.searchresults.count>0) {
			$("#results").append("<div class=\"digest\"><h5 class=\"case\">"+court+"</h5><h3><a href='"+decisionLink+decisionID+"'>" + headline + "</a></h3><p class=\"decided\">Date Published: " + formattedPubDate + "</p></div>");	

			// $("#results").append(adDivider);

		 }
       } catch(e) {
	   	$("#results").html("<p>"+ errorMessage +  e + "</p>");
	   
       }
		 if ( i == text.searchresults.count) return false;
	 });
}




function paginate(p) {// for additional results based on original search criteria

    var end2;

    if(p == "next") {
        if(currentPageCount>0 && numResults>end) {
    		
			//clear previous results
			$("#records_displayed").html("&nbsp;");
			$("#results").html("&nbsp;");
			$("#message2").html("<p><img src='/img/ny_lawyer/ajax_loader.gif' alt='Searching...' />&nbsp;Searching...</p>");

	
    		start = start+resultsPerPage;
    		end = end+resultsPerPage;
    		currentUrl = beginString + endString1 + "&userId=" + userId + "&start=" + start + "&end=" + end + endString2;

    		//get json with new start and end
    		$.getJSON(theDomain + currentUrl,
			 function(data){

			   var text = data;
			   if(text !=null && text.searchresults !="" && text.searchresults !=undefined) {

			       currentPageCount = text.searchresults.count;
			       //document.getElementById("urlTest").innerHTML = "Query string with variables from form (for testing):<br /> " + currentUrl + ""; //test
			       end2 = end;
			       if(end>text.searchresults.estimate) {
			           end2 = text.searchresults.estimate;
			          document.getElementById("toppagination").innerHTML = "<a href='JavaScript:void(0);' onClick='paginate("+prev+"); return false;'>&lt;&lt; Previous</a>";
			          document.getElementById("botpagination").innerHTML = "<a href='JavaScript:void(0);' onClick='paginate("+prev+"); return false;'>&lt;&lt; Previous</a>";

			       } else if(end==text.searchresults.estimate) {
		               document.getElementById("toppagination").innerHTML = "<a href='JavaScript:void(0);' onClick='paginate("+prev+"); return false;'>&lt;&lt; Previous</a>";
		               document.getElementById("botpagination").innerHTML = "<a href='JavaScript:void(0);' onClick='paginate("+prev+"); return false;'>&lt;&lt; Previous</a>";
			       } else {
			           document.getElementById("toppagination").innerHTML = "<a href='JavaScript:void(0);' onClick='paginate("+prev+"); return false;'>&lt;&lt; Previous</a> | <a href='JavaScript:void(0);' onClick='paginate("+nxt+"); return false;'>Next &gt;&gt;</a>";
			           document.getElementById("botpagination").innerHTML = "<a href='JavaScript:void(0);' onClick='paginate("+prev+"); return false;'>&lt;&lt; Previous</a> | <a href='JavaScript:void(0);' onClick='paginate("+nxt+"); return false;'>Next &gt;&gt;</a>";
			       }
			       if(text.searchresults.count != 0) {
			           $("#records_displayed").html("Viewing  " + start + " through " + end2);
			       }

                   displayResults (text, pageType);

		       } else {document.getElementById("message2").innerHTML = "<p>We are currently unable to fulfill your request.  Please try again later.</p>";}
	         }); //end getJSON
    	}//end if
    	
	} else if(p == "previous") {
		if(end>resultsPerPage) {
		
	        //clear previous results

	        $("#records_displayed").html("&nbsp;");
	        $("#results").html("&nbsp;");
		$("#message2").html("<p><img src='/img/ny_lawyer/ajax_loader.gif' alt='Searching...' />&nbsp;Searching...</p>");

	
	    	start = start-resultsPerPage;
    		end = end-resultsPerPage;
    		currentUrl = beginString + endString1 + "&userId=" + userId + "&start=" + start + "&end=" + end + endString2;
  
  	    	//get json with new start and end
	    	$.getJSON(theDomain + currentUrl,
	    	 function(data){

	    	   var text = data;
	    	   if(text !=null && text.searchresults !="" && text.searchresults !=undefined) {

	    	   currentPageCount = text.searchresults.count;
		   
	    	   //test
	    	   //document.getElementById("urlTest").innerHTML = "Query string with variables from form (for testing):<br /> " + currentUrl + ""; //test
	
	           end2 = end;
	    	   if(end>text.searchresults.estimate) {
	    	       end2 = text.searchresults.estimate;
	    	   }
		   
	    	   if(start == 1) {
	    	       document.getElementById("toppagination").innerHTML = "<a href='JavaScript:void(0);' onClick='paginate("+nxt+"); return false;'>Next &gt;&gt;</a>";
	    	       document.getElementById("botpagination").innerHTML = "<a href='JavaScript:void(0);' onClick='paginate("+nxt+"); return false;'>Next &gt;&gt;</a>";
	    	   } else {
	    	       document.getElementById("toppagination").innerHTML = "<a href='JavaScript:void(0);' onClick='paginate("+prev+"); return false;'>&lt;&lt; Previous</a> | <a href='JavaScript:void(0);' onClick='paginate("+nxt+"); return false;'>Next &gt;&gt;</a>";
	    	       document.getElementById("botpagination").innerHTML = "<a href='JavaScript:void(0);' onClick='paginate("+prev+"); return false;'>&lt;&lt; Previous</a> | <a href='JavaScript:void(0);' onClick='paginate("+nxt+"); return false;'>Next &gt;&gt;</a>";
	    	   }
		   
	    	   if(text.searchresults.count != 0) {
	    	       $("#records_displayed").html("Viewing  " + start + " through " + end2);
	    	   }

	    	   displayResults (text, pageType);

	    	} else {document.getElementById("message2").innerHTML = "<p>We are currently unable to fulfill your request.  Please try again later.</p>";}
	     }); //end getJSON
	   }//end if
	}//end if p
 
}

function myJSON(theRegion,startDt,endDt,pa,key,resultspp,pt,pubname,court) { //for initial search results
    start = 1;
    if(resultspp != undefined && resultspp != "") {
	    resultsPerPage = parseInt(resultspp);
    }
	end = resultsPerPage;
	
	pageType = pt;

	var region = theRegion;
	var startDate = startDt;
	var endDate = endDt;
	var practicearea = pa;
	var keywords = key;
	var court=court;
	var scCourt=court;
	var courtTermDisplay=court;
	var scPracticeArea = practicearea;
	
	
	if(scCourt == "All%20New%20York%20Courts") {
		court="%28court:%22Appellate%20Division,%201st%20Dept%22+or+court:%22Appellate%20Division,%20First%20Department%22+or+court:%22Appellate%20Division,%202nd%20Dept%22+or+court:%22Appellate%20Division,%20Second%20Department%22+or+court:%22Appellate%20Division,%203rd%20Dept%22+or+court:%22Appellate%20Division,%20Third%20Department%22+or+court:%22Appellate%20Division,%204th%20Dept%22+or+court:%22Appellate%20Division,%20Fourth%20Department%22+or+court:%22Supreme%20Court%22+or+court:%22Civil%20Court%22+or+court:%22City%20Court%22+or+court:%22Civil%20Court%22+or+court:%22Family%20Court%22+or+court:%22Small%20Claims%20Court%22+or+court:%22Surrogate%27s%20Court%22+or+court:%22Village%20Justice%20Court%22%29";
	}
	
	if(scCourt == "All%20Federal%20Courts") {
		court="%28court:%22U.S.%20Court%20of%20Appeals,%20Second%20Circuit%22+or+court:%22U.S.%20District%20Court,%20Northern%20District%22+or+court:%22U.S.%20District%20Court,%20Eastern%20District%22+or+court:%22U.S.%20District%20Court,%20Southern%20District%22+or+court:%22U.S.%20District%20Court,%20Western%20District%22%29";
	
	}
	
	if(scCourt =="court:%22Appellate%20Division,%201st%20Dept%22"){
		court=
		"%28court:%22Appellate%20Division,%201st%20Dept%22+or+court:%22Appellate%20Division,%20First%20Department%22%29";
	}


	if(scCourt =="court:%22Appellate%20Division,%202nd%20Dept%22"){
		court=
		"%28court:%22Appellate%20Division,%202nd%20Dept%22+or+court:%22Appellate%20Division,%20Second%20Department%22%29";
	}


	if(scCourt =="court:%22Appellate%20Division,%203rd%20Dept%22"){
		court=
		"%28court:%22Appellate%20Division,%203rd%20Dept%22+or+court:%22Appellate%20Division,%20Third%20Department%22%29";
	}


	if(scCourt =="court:%22Appellate%20Division,%204th%20Dept%22"){
		court=
		"%28court:%22Appellate%20Division,%204th%20Dept%22+or+court:%22Appellate%20Division,%20Fourth%20Department%22%29";
	}
	
		
	
        if (pubname != undefined && pubname != "") {
        publicationName = pubname;
        }
        
        if(keywords == "Enter Search Terms") {
        	keywords = "";
        }

	keywords = encodeURIComponent(keywords);
	keywords = keywords.replace(/\'/g, "%27");
	keywords = keywords.replace(/\*/g, "%2A");
	keywords = keywords.replace(/\./g, "%2E");
		
	//court = encodeURIComponent(court);
	
	
	scCourt = decodeURIComponent(scCourt);
		
		
	//get user email address from jsp if there is one
	if(theEmail !="null" && theEmail !=null && theEmail != "" && theEmail != undefined) {
	    userId = theEmail;
	} else {
	    userId = "";
	}
	
	var endString = endString1 + "&userId=" + userId + "&start=" + start + "&end=" + end + endString2;
	
	
	var startDateArr = startDate.split("/");
	var endDateArr = endDate.split("/");
	
	startDay = startDateArr[1]; //dd
	startMonth = startDateArr[0]; //mm
	startYear = startDateArr[2]; //yyyy
	
	stopDay = endDateArr[1]; //dd
	stopMonth = endDateArr[0]; //mm
	stopYear = endDateArr[2]; //yyyy
        //beginString = "?queryString="+catagory+"%20atex-paper:"+paper+"%20"+keywords+"&startYear="+startYear+"&startMonth="+startMonth+"&startDay="+startDay+"&stopYear="+stopYear+"&stopMonth="+stopMonth+"&stopDay="+stopDay;

	beginString = "?queryString=region:"+region+"%20and%20publication:"+pubname+"%20and%20"+court+"%20and%20%28"+practicearea+"%29%20"+keywords+"&startYear="+startYear+"&startMonth="+startMonth+"&startDay="+startDay+"&stopYear="+stopYear+"&stopMonth="+stopMonth+"&stopDay="+stopDay;


	var theUrl = beginString + endString;

	
	//for testing
	//document.getElementById("urlTest").innerHTML = "Query string with variables from form (for testing):<br /> " + theDomain + theUrl;
	
	
	//clear previous results
	$("#message2").html("");
	
	$("#number_of_records").html("");
	$("#records_displayed").html("&nbsp;");
	$("#toppagination").html("");
	$("#botpagination").html("");
	$("#topdivider").html("");
	$("#results").html("");
	$("#message2").html("<p><img src='/img/ny_lawyer/ajax_loader.gif' alt='Searching...' />&nbsp;Searching...</p>");

   
	 $.getJSON(theDomain + theUrl,
		 function(data){

		   var text = data;
		   if(text !=null && text.searchresults !="" && text.searchresults !=undefined) {
		   
		   numResults = text.searchresults.estimate;
		   
		   
		   
		   currentPageCount = text.searchresults.count;
		
		   
		   var end2 = end;
		   if(end>text.searchresults.estimate) {
		       end2 = text.searchresults.count;
		       resultsPerPage =end2;
		   }
		   
		   
		   $("#number_of_records").html("<h2>Search Results</h2><p>Number of Records Found: " + numResults +"</p>");
		   $("#topdivider").html("<div class='adDivider'></div>");
		   if(text.searchresults.count != 0) {
		       $("#records_displayed").html("Viewing  " + start + " through " + end2);
		   } 
		   
		   //document.getElementById("test1").innerHTML = "<p>"+end2+"</p>";
		   
		   
		   court=decodeURIComponent(court);
		   //court=court.replace(", ","");
		   
		   
		   //$("#searchCrumb").append(" &gt; "+keywords+" "+practicearea+" "+court+"</p>");
		   
		   if(practicearea != "") {
		   	practicearea=practicearea.replace(/%22/g,"");
		   	practicearea=decodeURIComponent(practicearea);
		   	practicearea = practicearea.replace ("practicearea:"," ");
		   	practicearea = practicearea.replace (/\+/g," ");
		   	practicearea = " | In Practice Area: <span style='text-transform:capitalize;'>" + practicearea + "</span> ";
		   }
		   
		   if(practicearea == "") {
			practicearea = " | In All Practice Areas";
		   }		  

		   if(courtTermDisplay != "") {
		   	courtTermDisplay = " | In " + scCourt;
		   	courtTermDisplay = courtTermDisplay.replace("court:","Jurisdiction: ");
		   	courtTermDisplay = courtTermDisplay.replace(/"/g,"");

		   }
		   
		   if(courtTermDisplay == "") {
		   	courtTermDisplay = " | In All Jurisdictions";
		   }
		   



		   if(keywords != "") {
		   	keywords = keywords + " ";
		   	keywords = decodeURIComponent(keywords);
		   	keywords = " | Keyword: "+ keywords;
		   }
		   
		   
		   

		   
		   document.getElementById("searchTerms").innerHTML = "<p><span class=\"youSearched\">You searched:</span> Decisions from: " + startDate + " - "+ endDate + keywords + courtTermDisplay + practicearea + "</p>";		   
				   
		   if(text.searchresults.estimate>resultsPerPage) {
		       document.getElementById("toppagination").innerHTML = "<a href='JavaScript:void(0);' onClick='paginate("+nxt+"); return false;'>Next &gt;&gt;</a>";
		       document.getElementById("botpagination").innerHTML = "<a href='JavaScript:void(0);' onClick='paginate("+nxt+"); return false;'>Next &gt;&gt;</a>";
		   }
		   if(numResults !=0) {
              		 displayResults (text,pageType);
              		 

              		 
              		 
              		 
		   } else {
		       $("#message2").html("&nbsp;");
		   }
		   	//site catalyst
		   	
		   	
		   	if(scPracticeArea == "") {
		   		scPracticeArea = "all";
		   	}
		   	
		   
		   	
		   	
			if(scCourt == "") {
		   		scCourt = "all";
		   	}
		   	

		   			 
		   	
              		 var s = s_gi(s_account);
			 s.linkTrackVars = "prop9,prop19,prop39,prop44,prop45,prop49,events";
			 s.linkTrackEvents = "event2,event27";
              		 s.prop9 = keywords;//search term
              		 s.prop19="New York";
              		 s.prop44 = scCourt + "," + scPracticeArea;//refinement
              		 s.prop45 ="court decisions";
              		 s.prop49 = numResults; //number of results
              		 s.events = "event2,event27";
              		 s.tl(this,'o','nylj decision search');    

		   	// testing output
		   	//$("#message2").html("prop44:"+s.prop44);		   
		   
		} else {document.getElementById("message2").innerHTML = "<p>We are currently unable to fulfill your request.  Please try again later.</p>";}
	 });
 
 	 

 }       
 


