var ns4 = (document.layers) ? true:false
var ie4 = (document.all) ? true:false
var ie5up = document.getElementById&&document.all;
var ns6up = document.getElementById&&!document.all;

bgColor1= "#eee8aa"

/*-----This function used in detailresult page to display instruction window--------*/
function NewWindow(mypage, myname, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { 
		win.window.focus(); 
	}
}

/* Show and Hide Layers */
function show(id) {
	if (ie5up || ns6up){
		document.getElementById(id).style.visibility="visible";
	} else {
		if (ns4){ 
			document.layers[id].visibility = "show"
		} else { 
			if (ie4) document.all[id].style.visibility = "visible" 
		}
	}
}

/* Show and Hide Layers */
function hide(id) {
	if(ie5up || ns6up)
		document.getElementById(id).style.visibility="hidden";
	else 
	if (ns4)
		 document.layers[id].visibility = "hide"
	else 
		if (ie4) ducument.all[id].visibility = "hidden"
}

/* Color Change for text boxes */
function change(color){
	if (event.srcElement.tagName=="INPUT")
		event.srcElement.style.backgroundColor=color
}

/* Validate name search forms */
function ValidateFormName() {
	if ((document.searchform.lastname.value == "") || (document.searchform.lastname.length < 3) ) {	
		alert("Please enter a Last Name.");
		return false;
	}
	// 27 September 2007 check if to date or from date is not null
	else  if(document.searchform.fromdate.value != null && document.searchform.fromdate.value != ""){
		if(isDate(document.searchform.fromdate.value)==false){
			document.searchform.fromdate.focus()
			return false;
		}
	}else if(document.searchform.todate.value != null && document.searchform.todate.value != ""){
		if(isDate(document.searchform.todate.value)==false){
			document.searchform.todate.focus()
			return false;
		}
	
	//else if ((isDate(document.searchform.fromdate.value)==false) || (isDate(document.searchform.todate.value)==false)) {
	//	document.searchform.fromdate.focus();
	//	return false;
	} else {
	    objForm = document.searchform;
	    objForm.name.value = objForm.lastname.value + ' ' + objForm.firstname.value ;
	    hide('namesearch');
	    show('message');
	    return true;
	}
}


/* Validate name search forms */
function ValidateFormProperty() {
	if ((document.searchform.name.value == "") || (document.searchform.name.length < 3) ) {	
		alert("Please enter a Street Name.");
		return false;
	}
	// 27 September 2007 check if to date or from date is not null
	else  if(document.searchform.fromdate.value != null && document.searchform.fromdate.value != ""){
		if(isDate(document.searchform.fromdate.value)==false){
			document.searchform.fromdate.focus()
			return false;
		}
	}else if(document.searchform.todate.value != null && document.searchform.todate.value != ""){
		if(isDate(document.searchform.todate.value)==false){
			document.searchform.todate.focus()
			return false;
		}
	//else if ((isDate(document.searchform.fromdate.value)==false)|| (isDate(document.searchform.todate.value)==false)) {
	//	document.searchform.fromdate.focus()
	//	return false;
	} else {
	    hide('namesearch');
	    show('message');
	    return true;
	}
}

/* Validate document search forms */
function ValidateFormDocument() {
	if (document.searchform.volume != null)  {
		if ((document.searchform.volume.value == "" && document.searchform.instrumentnumber.value == ""))  {

			if((document.searchform.doctype.value == "") && (document.searchform.town.value == "") ){
				alert("Please enter a (Document Number) or (Book) or (Document Type & Date range ) or (Town & Date Range)!");			
				return false;
			}
		} 
	// 27 September 2007 check if to date or from date is not null
	else  if(document.searchform.fromdate.value != null && document.searchform.fromdate.value != ""){
		if(isDate(document.searchform.fromdate.value)==false){
			document.searchform.fromdate.focus()
			return false;
		}
	}else if(document.searchform.todate.value != null && document.searchform.todate.value != ""){
		if(isDate(document.searchform.todate.value)==false){
			document.searchform.todate.focus()
			return false;
		}
	}
	//else if (  (isDate(document.searchform.fromdate.value)==false) ||  (isDate(document.searchform.todate.value)==false) ) {
	//	document.searchform.fromdate.focus()
	//		return false;
	//	} 
		else{
		    hide('namesearch');
		    show('message');
			return true;
		}
  }
   else	{
		if ((document.searchform.casenum.value == "" && document.searchform.instrumentnumber.value == ""))  {
		alert("Please enter a Court Case number!");
		return false;
		} else{
		    hide('namesearch');
	    	show('message');
			return true;
		}
	
	}	
}


/* Validate document search forms */
function ValidateUnIndexedForm() {
	 
  
	if (document.searchform.volume.value == ""  )  {
		alert("Please enter valid Book number");
		return false;
	} else{
	    hide('namesearch');
	    show('message');
		return true;
	}
  }
  


/* Validate Certificate search forms */
function ValidateFormCertificate() {
	if (document.searchform.nameid.value == "")  {
		alert("Please enter a Certificate Number!");
		return false;
	} else{
	    hide('namesearch');
	    show('message');
		return true;
	}
}

/* Get the clicked page */
function clickProcess (refer)
{	
  if ( refer == "contact.jsp" )	
  {
	document.commonForm.commandflag.value = refer;
    document.commonForm.countypage.value = "true";
    document.commonForm.submit();
  }
  else 	
  {
	document.commonForm.commandflag.value = refer;
    document.commonForm.submit();
  }
  
	return true;
}


/* Get the default tab page */
function clickTab (tabNo)
{	
  // This is to set default value of the radiobutton to 70 (first radio button)
  // when a tab is clicked first time.
//  if ( document.forms[0].officeid ){
 //   if ( document.forms[0].officeid[0] ){
      //officeid id radio button
   //   document.forms[0].officeid[0].checked = "true";
   // }    
 // }
	return refreshTab (tabNo);
}


/* Get the refreshed tab page */
function refreshTab (tabNo)
{	
  var searchPage = "page_dispatcher.jsp?pagetype=search.page&tab=";
  document.forms[0].optflag.value = "MenuCommand";
  document.forms[0].commandflag.value = searchPage + tabNo;
  document.forms[0].submit();  
  return true;
}




/* Changes the form action url with encoded url by appending jsessionid to the url */
function encodeURL(url) {
    var encodedUrl = url;
    var previousUrl = document.URL;
    var jsessionStr = "";
 
    if ( previousUrl != null ){     
		//check if jsessionid is in the url or not
      	if (previousUrl.indexOf("jsessionid") != -1 ){
        	if ( previousUrl.indexOf("?") != -1){
            	jsessionStr = previousUrl.substring(previousUrl.indexOf("jsessionid"), previousUrl.indexOf("?")); 
          	} else {
          		jsessionStr = previousUrl.substring(previousUrl.indexOf("jsessionid"), previousUrl.length); 
          	}
          	jsessionStr = ";" + jsessionStr;
      	}
	}
 
    //alert("jsessionStr = " + jsessionStr);
    if ( jsessionStr != null && jsessionStr != ""){
    	encodedUrl = url + jsessionStr;
    }
    //alert("encodedUrl = " + encodedUrl);
    document.forms[0].action=encodedUrl;
}

function ValidateNonNull(field,name) {
    // field cannot be null
    
	if (field.value == "")  {
		alert("Please enter a value for "+name);
        field.focus();
		return false;
	}
    
    return true;
}

function ValidateNumeric(field,name) {

    // field must be numeric
    
	if (! field.value.match(/^\d+\s*$/))  {
		alert(name+" must be a number");
        field.focus();
		return false;
	}
    
    return true;
}

function ValidateDate(field,name) {

    // field must be a date
    
    if (! field.value.match(/^\d{2}\/\d{2}\/\d{4}$/))  {
		alert(name+" must be a date in MM/DD/YYYY format");
        field.focus();
		return false;
	}
    
    return true;
}

function AllowNull(field,name) {

    // field can be null
    
    if (field.value.length == 0)  {
		return true;
	}
    
    return false;
}


/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy");
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month");
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day");
		return false;
	}
	if (strYear.length != 4 || year==0 || year>maxYear){
		alert("Please enter a valid 4 digit year less than "+maxYear);
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date");
		return false;
	}
return true
}
