// Master.js
//--------------------------------------------------------------
// Function Name: Submit Form
// Description: This JS function looks at values in certain fields
// and determines which search should be executed
// --------------------------------------------------------------

 function go() {
 	return document.search.reallySubmit.value == "true";
 }

 function submitForm() {
 	document.search.onsubmit = go;
 
 	var flag = document.getElementById("setField").value;
 	var fname = document.getElementById("fname").value;
 	var lname = document.getElementById("lname").value;
 	var mint = document.getElementById("mint").value;
 	var casey = document.getElementById("caseYear").value;
 	var caseq = document.getElementById("caseSeq").value;
 	var app = document.getElementById("selType").value;
 	
 	document.search.reallySubmit.value = "true";
 	
 	fromDate = document.search.txtCalendar1.value;
 	toDate = document.search.txtCalendar2.value;
 	
 	if (fromDate != '') {
 	    if (toDate == '') {
 	    	tempToDate = new Date();
 	    	toDate = tempToDate.getMonth() + 1 + "/" + tempToDate.getDate() + "/" + tempToDate.getFullYear();
 	    	document.search.txtCalendar2.value = toDate;
 	    }
 	    
 	    fromParts = fromDate.split("/");
 	    toParts = toDate.split("/");
 	    
 	    fromDt = new Date();
 	    fromDt.setMonth(fromParts[0] - 1);
 	    fromDt.setDate(fromParts[1]);
 	    fromDt.setFullYear(fromParts[2]);
 	    
 	    toDt = new Date();
 	    toDt.setMonth(toParts[0] - 1);
 	    toDt.setDate(toParts[1]);
 	    toDt.setFullYear(toParts[2]);
 	    
 	    fromTime = fromDt.getTime();
 	    toTime = toDt.getTime();
 	    
 	    if (fromTime > toTime) {
 	    	alert("The From Date must be before the To Date");
 	    	document.search.reallySubmit.value = "false";
 	    }
 	}
 	
 	if (toDate != '' && fromDate == '') {
 		document.search.todt.value = '';
 	}
 	

 	// if the name is typed in issue the name search
 	if(flag==null || flag=="") {
 	    if (casey != "") {
 	    	flag = 2;
 	    } else if (fname != null || lname != null) {
 	    	flag = 1;
 	    }
 	}
 	
 	if (flag == 1) {
 	    document.search.action = "nameSearch";
 	} 
 	// if the case number is entered issue the case search
 	if (flag == 2) {
 		document.search.action = "caseSearch?" + randomString();
 	}
 	if (flag == 3) { // On the click of the case number link
 	    document.search.action = "caseSearch?" + randomString();
 	}
 }
 //------------------------------------------------------------
 // Function Name: submitForm2
 // Description: Does the same as the function above, but it is
 // mainly for the Court Schedule pages.
 //------------------------------------------------------------
 function submitForm2() {
  	var date = document.search.txtCalendar1;
 	var flag = document.getElementById("setField").value;
 	var ct = document.getElementById('courtt').value; 
 	if(flag==null)flag=-1;
 	if (flag == 3) { // On the click of the case number link
 	    document.search.action = "caseSearch?" + randomString();
 	    document.search.submit();
 	}
 	if (flag == 4) { 
 		document.search.action = "courtSchedule?" + randomString();
 		
 		// How would date be null here?
 		if (date != null) {
 			if (date.value == '') {
 				alert("Please enter a date");
 				date.focus();
 				return false;
 			}
 		}
 	}

 	return true;
 }
 
 function doEmail() {
    url = "caseWatch?command=start";
    for (i = 0; i < document.search.caseNumber.length; i++) {
        if (document.search.caseNumber[i].checked) {
            url += "&caseNumber=" + document.search.caseNumber[i].value;
        }
    }
    window.open(url, "subscribe_win", "status=1,toolbar=0,location=0,menubar=0,directories=0,height=450,width=500,resizable=1,scrollbars=1");
 }
 
 function randomString() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 20;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}
 // Function to set the flag to execute a certain search
 function setSearch(flag) {
 		document.getElementById("setField").value = flag;
 }
 function setAdv(flag) {
        alert("setting the flag to " + flag);
 		document.getElementById("advFlag").value = flag;
 }
 
 function goNext() {
 		    document.getElementById("page").value = "next";
 }
 function goPrev() {
            document.getElementById("page").value = "prev";
 
 }
 // Set the field value to go to the Next Record
 function goPrevCase() {
            document.forms[0].action = '/CaseInformationOnline/caseSearch';
 			document.getElementById("pagecase").value = "prev";
 			//alert(document.getElementById("pagecase").value);
 			document.forms[0].pagecase.value="prev";
 			//alert(document.forms[0].pagecase.value);
 			document.forms[0].submit();
 }
// Set the field value to go to the Previous Record
 function goNextCase() {
 			document.forms[0].action = '/CaseInformationOnline/caseSearch';
 			document.getElementById("pagecase").value = "next";
 			document.forms[0].pagecase.value="next";
 			document.forms[0].submit();
 }
 // Set the field value to go to the Next Record
 function doDocketPaging(action) {
 			document.forms[0].action = action;
 			document.forms[0].submit();
 }

 // Function to allow the advanced section to be expanded based on certain search criteria
 function showAdvAlreadyClicked() {
 	  //alert(document.getElementById("advFlag").value);
      if (document.getElementById("advFlag").value == "show") {
      	  document.getElementById("advdetail").style.display = '';
      	  //document.getElementById("advdetail1").style.display = '';
      	  document.getElementById('advimg').src = '/CaseInformationOnline/images/minus.png';
      	 
      } else {
      		document.getElementById("advdetail").style.display = 'none';
      		//document.getElementById("advdetail1").style.display = 'none';
      		document.getElementById('advimg').src = '/CaseInformationOnline/images/plus.png';
      		
      }
 }

//This function will dynamicaly count visible rows and alternate the colors
//In addition, rows with the class of 'detail' will show as the same when visable
function alternate(id){
        if(document.getElementsByTagName){
            //Initialize variables
            var thecount = 1;
            var lastcolor = '#d3d3d3';
            var table = document.getElementById(id);
            if(table!=null){
            var rows = table.getElementsByTagName('tr');
            //Loop through each row in the specified table
            if(rows!=null){
            for(i = 0; i < rows.length; i++){
                //Skip the row if class = 'label'
                if(rows[i].className != 'label') {
                    //If class = 'detail' use the last color
                    if(rows[i].className == 'detail') {
                        rows[i].style.background = lastcolor;
                    } else {
                        //If the row is not hidden set color
                        if(rows[i].style.display != 'none') {
                            if(thecount % 2 == 0){
                                rows[i].style.background = 'white';
                                lastcolor = "white";
                            }else{
                                rows[i].style.background = '#d3d3d3';
                                lastcolor = '#d3d3d3';
                            }
                            thecount += 1;
                        }
                    }
                }
            }
            }}
        }
    }
 // Function to show or hide the advanced section
 function showhideadv(therow, theimage) {
        //alert("in the show hide advanced function");
 		if( document.getElementById(therow).style.display=='none' ){
 			document.getElementById(therow).style.display = '';
        	document.getElementById(theimage).src = '/CaseInformationOnline/images/minus.png';
        	//document.getElementById("advFlag").value = 1;
        }else {
        	document.getElementById(therow).style.display = 'none';
        	document.getElementById(theimage).src = '/CaseInformationOnline/images/plus.png';
        	//document.getElementById("advFlag").value = 0;
        }
 }   
//This function toggles the display of the specified row and updates the gif
function showhide(therow,thetable,theimage) {
    //If the detail is currently hidden set to display
    if( document.getElementById(therow)!=null){
    if( document.getElementById(therow).style.display=='none' ){
        if(thetable != '') {
            var table = document.getElementById(thetable);
            //alert(table);
            if (table != null) {
            	var rows = table.getElementsByTagName('tr');       
            	for(i = 0; i < rows.length; i++){
                	rows[i].style.display = '';
            	}
            	document.getElementById(thetable).style.display = '';
         	} else {            	
         	document.getElementById(therow).style.display = '';
        	document.getElementById(theimage).src = '/CaseInformationOnline/images/minus.png';
        	}
        }
        
        document.getElementById(therow).style.display = '';
        document.getElementById(theimage).src = '/CaseInformationOnline/images/minus.png';
    }else{         
        document.getElementById(therow).style.display = 'none';
        document.getElementById(theimage).src = '/CaseInformationOnline/images/plus.png';          
    }
    }
}

// show or hide the advanced search panel
function toggleAdvancedSearch() {
	showhide('advdetail','','advimg');
	showhide('advdetail1','','advimg');
	
	if (document.search.advFlag.value == "show") {
		document.search.advFlag.value = "hide";
	} else {
		document.search.advFlag.value = "show";
	}
}

//This function uses the class assigned to a row to set visibility
//It is called from the onchange event in category select
function updatedocket(dockettype) {
    //Initialize variables
    var table = document.getElementById('docket');
    var rows = table.getElementsByTagName('tr');
    //If the user select All, show all header lines
    if(table!=null){
    if(rows!=null){
    if(dockettype != 'all') {
        //Loop through all rows in the docket table
        for(i = 0; i < rows.length; i++){
            //alert(dockettype + "---" + rows[i].className);
            //Ignore the row with class = 'label'
            if(rows[i].className != 'label') {
                //Show rows where class = docket type and hide all others
                if(rows[i].className != dockettype) {                       
                    rows[i].style.display = 'none';
                } else 
                    {
                    //alert(dockettype + "---" + rows[i].className);
                    rows[i].style.display = '';
                }
            }
        }
    //Show all the header rows
    } else {
        //Loop through each row in the docket table
        for(i = 0; i < rows.length; i++){
            //Show all header rows, and hide all detail rows
            if(rows[i].className != 'detail') {
                rows[i].style.display = '';
            } else {
                rows[i].style.display = 'none';
            }
        }
    }}}
    //Call the function to update the plus or minus images
    updatetableimages('docket','minus','plus');
    expandcollapse('docket', 'expand')
}

//This function will expand all detail lines or collapse all detail lines
function expandcollapse(thetable, activity) {
    //initialize variables
    var table = document.getElementById(thetable);
    var rows = table.getElementsByTagName('tr');
    var hidden = 1;
    if(table!=null){
    if(rows!=null){
    //Check parameter to expand or collapse
    //Expand
    if (document.search.sd.checked) {
    	activity = 'expand';
    } else {
    	activity = '';
    }
    if(activity == 'expand') {
        //Loop through each row in the specified table
        for(i = 0; i < rows.length; i++){
            //Ignore the rows with class = 'label'
            if(rows[i].className != 'label') {
                //If it is a header row, check visibility and set flag for detail row
                if(rows[i].className != 'detail') {
                    if(rows[i].style.display != 'none') {
                        hidden = 0;
                    } else {
                        hidden = 1;
                    }
                //For detail rows, set visibility based on flag for header row
                } else {
                    if(hidden == 0) { rows[i].style.display = ''; }
                }
            }
        }
        //Call the function to update the plus or minus images
        updatetableimages('docket','plus','minus');
    //Collapse
    } else {
        //Loop through each row in the specified table
        for(i = 0; i < rows.length; i++){
            //Hide all rows with class = 'detail'
            if(rows[i].className == 'detail') {
                rows[i].style.display = 'none';
            }
        }
        //Call the function to update the plus or minus images
        updatetableimages('docket','minus','plus');
    }}}
}
    
//This function will do a mass update on the specified images in the specified table
function updatetableimages(thetable, origimg, newimg) {
    //Initialize variables
    var table = document.getElementById(thetable);
    if(table!=null){
    var theimages = table.getElementsByTagName('img');
    //Loop through each image in the table
    if(theimages!=null){
    for(i = 0; i < theimages.length; i++){
        //Set the image to the specified image
        theimages[i].src = theimages[i].src.replace(origimg, newimg);
    }
    }}
}

// Toggles the visibility of elements
function toggleElementVisibility(elementId) {
    var block = document.getElementById(elementId);
    document.search.attyNum.value = '';
    if (document.search.ssNum3 != null)
    	document.search.ssNum3.value = '';
    if (block != null) {
        if (block.style.display == "none") {
            block.style.display = "inline";
        } else {
            block.style.display = "none";  
        }
    }
}

function searchByID(id) {
	document.search.attyIdx.value = id;
	setSearch(1);
	submitForm();
	document.search.submit();
}

// Put stuff to do on form load here
function loadBody() {
	// clear the atty num if it's all 0's
	if (document.search.attyNum.value == "000000000") {
		document.search.attyNum.value = "";
	}
	
	// show the advanced search panel if needed.
	if (document.search.advFlag.value == "show") {
		showhide('advdetail','','advimg');
		showhide('advdetail1','','advimg');
	}
	
	// make double-sure that the attorney number / ssn fields and the "Name Of" field 
	// match up
	var label;
	var input;
	var otherLabel;
	var otherInput;
	if (document.search.personType.selectedIndex == 0) {  // rely on Party being the first option
		label = document.getElementById('ssnLabel');
		otherLabel = document.getElementById('attyNumLabel');
		input = document.getElementById('ssnInput');
		otherInupt = document.getElementById('attyNum');
	} else {
		label = document.getElementById('attyNumLabel');
		otherLabel = document.getElementById('ssnLabel');
		input = document.getElementById('attyNum');
		otherInput = document.getElementById('ssnInput');
	}
	
	if (label != null) {
		label.style.display = 'inline';
	}
	
	if (otherLabel != null) {
		otherLabel.style.display = 'none';
	}
	
	if (input != null) {
		input.style.display = 'inline';
	}
	
	if (otherInput != null) {
		otherInput.style.display = 'none';
	}
}

// Make the cursor jump back and forth between the SSN fields
function ssnHandle(event) {
	setSearch(1);
	
	var field;
	var thisKey = 0;
	
	event = event || window.event;
	field = event.srcElement || event.target;
	
	if (typeof event.charCode != "undefined") {
		thisKey = event.charCode;
	} else if (event.which) {
		thisKey = event.which;
	} else {
		thisKey = event.keyCode;
	}
	
	if (field == document.search.ssNum1) {
		if (field.value.length == 3) {
			document.search.ssNum2.focus();
		}
	} else if (field == document.search.ssNum2) {
		if (field.value.length == 0 && thisKey == 8) {
			document.search.ssNum1.focus();
			range = document.search.ssNum1.createTextRange();
			range.move('character', 3);
			range.select();
		} else if (field.value.length == 2) {
			document.search.ssNum3.focus();
		}
	} else if (field == document.search.ssNum3) {
		if (field.value.length == 0 && thisKey == 8) {
			document.search.ssNum2.focus();
			range = document.search.ssNum2.createTextRange();
			range.move('character', 2);
			range.select();
		}
	}		
}

function rapSheetPopup(caseNumber, pldfNumber) {
	window.open("rapsheet?casenum=" + caseNumber + "&pldfnum=" + pldfNumber,
			"rap_sheet_window",
			"status=1,toolbar=0,location=0,menubar=0,directories=0,height=400,width=500,resizable=1,scrollbars=1");
}

function loadCaseInParent(caseNum) {
	window.opener.location = "caseSearch?alinkvalue=" + caseNum;
}

function openDoc(url, winName, options) {
	window.open(url, winName, options);
}