// This file is for Javascript functions relating to ECHO 3 year
// Created by Matt Malachowski on October 10, 2004

function BrowsercheckMM() {
  //resets value and hidden field used for SFIP search
	document.forms[1].otis_otm_sfi_universe.selectedIndex="0";
	document.forms[1].idea_linkage.value="LINKED NONLINKED";
	document.forms[1].idea_db_filter.value="INC FRS AFS ICI PCS RCR TRI";
}

function submitSearch() {
  //Submits the contents of the form
  if (preSubmissionCheck()){
    document.forms[1].submit();
  }
}

function submitSearchCAA() {
  //Submits the contents of the form
  if (preSubmissionCheckCAA()){
    document.forms[1].submit();
  }
}

function submitSearchCAAmap() {
  //Submits the contents of the form
  if (preSubmissionCheckCAAmap()){
    document.forms[1].submit();
  }
}

function remove_carriage_returns(media) {
 //the value of media is equal to the number of characters that the permit id should be, AFS=10, PCS=9, RCRA=12
    var browserName=navigator.appName;
    var browserVer=parseInt(navigator.appVersion); 
if (browserName=="Microsoft Internet Explorer") {    
 
  var limit= media;
  nameValue = document.forms[1].otis_facility_id_l.value;
  nameValue = nameValue.replace(/\r\n/g, ",")  // getting rid of carriage returns
  nameValue = nameValue.replace(/\s/g, "")  // getting rid of spaces
  var length = nameValue.length;
  if ((length != "0") && (nameValue.charAt(length-1) != ",")) { //adding comma as last digit, of last permit
      nameValue = nameValue + ",";
    }
  document.forms[1].otis_facility_id_l.value = nameValue;
  var length2 = nameValue.length;
  var counter = 0; //setting up a counter value to determine the number of commas in the list

		 for (var i = 1; i <= length2; i++) { //for loop determines number of commas in list
			if ((nameValue.charAt(i-1) == ",")) {
				  counter = counter +1;
			}
			else {
				   counter = counter;
			}
		}
		a = nameValue.split(",");  //splitting the list at commas
		y = "";  //setting up an array for valid ids
		q = "";  //setting up an array for invalid ids
		var invalid = 0; //variable to denote whether the invalid array is ever accessed

		 for (var p = 0; p < counter; p++) {
			  var sublength = a[p].length;  //determining length of each id in array

			  if (sublength == limit) {  //if id is correct length, add to array of accepted ids
				if (y != "") {
					s = new Array (y, ",", a[p]);
					y = s.join("");
				}
				else {
					s = new Array (a[p]);
					y = s.join("");
				}
			 }
			  else if (sublength != limit) {  //if id is incorrect length, add to array of invalid ids
				if (q != "") {
					z = new Array (q, ",", a[p]);
					q = z.join("");
					invalid = 1;
				}
				else {
					z = new Array (a[p]);
					q = z.join("");
					invalid = 1;
				}
			 }
		  }

 if (invalid == 1) { //if there are ids in our invalid list

   window.alert("The following invalid ids are not " + limit + " characters long:" + "\n" +  q + "\n" + "These invalid IDs will be removed");
   document.forms[1].otis_facility_id_l.value = y;
   return true;
 }

 else {  //no ids we in our invalid list, allow user to proceed
     return true;
 }

}
}


function  preSubmissionCheckCAA() {
  //Verifies that a user also enters a State when a City is inputted
  var stateSelectionIndex = document.forms[1].idea_state.selectedIndex;
  var cityValue = document.forms[1].idea_city_name_l.value;
  var tri1 = document.forms[1].idea_tri_air_releases.selectedIndex;
  var tri2 = document.forms[1].idea_tri_air_releases_amt.selectedIndex;
  var nei1 = document.forms[1].idea_nei_poll.selectedIndex;
  var nei2 = document.forms[1].idea_nei_poll_amt.selectedIndex;



    if (stateSelectionIndex == 0 && cityValue != "") {
  	alert('Please select a state in addition to a city.');
  	return false;
     }
     else if (tri1 != 0 && tri2 == 0) {
  	alert('Users must select a release amount after selecting TRI release category.  Please change your selection');
  	return false;
     }
     else if (tri1 == 0 && tri2 != 0) {
  	alert('Users must select a TRI release category when selecting a TRI release amount.  Please change your selection');
  	return false;
     } 
     else if (nei1 != 0 && nei2 == 0) {
  	alert('Users must select an emission amount after selecting an NEI Emission category.  Please change your selection');
  	return false;
     }
     else if (nei1 == 0 && nei2 != 0) {
  	alert('Users must select a NEI Emission category when selecting an NEI emission amount.  Please change your selection');
  	return false;
     }      
   else {
      if ((nei2 != 0)) {
  	document.forms[1].idea_report.value = "OTISECHO PARM AIRPAGE_SORTNAME_AIRPARTIAL_DEMRADIUS=3_TRICOMMAS_actionstart=#coqbd-19 PARM actionstart2=#coqbd-19_violqtrsmax=12_chemrel";
  	// window.alert(document.forms[1].idea_report.value);
  	return isValidFacLength(document.forms[1].idea_otis_name_l, 1);
      }
      else {
  	document.forms[1].idea_report.value = "OTISECHO PARM AIRPAGE_SORTNAME_AIRPARTIAL_DEMRADIUS=3_TRICOMMAS_actionstart=#coqbd-19 PARM actionstart2=#coqbd-19_violqtrsmax=12";
  	// window.alert(document.forms[1].idea_report.value);
  	return isValidFacLength(document.forms[1].idea_otis_name_l, 1);
      }
   }
}

function  preSubmissionCheckCAAmap() {
  //Verifies that a user also enters a State when a City is inputted
  var stateSelectionIndex = document.forms[1].idea_state.selectedIndex;
  var cityValue = document.forms[1].idea_city_name_l.value;
  var tri1 = document.forms[1].idea_tri_air_releases.selectedIndex;
  var tri2 = document.forms[1].idea_tri_air_releases_amt.selectedIndex;
  var nei1 = document.forms[1].idea_nei_poll.selectedIndex;
  var nei2 = document.forms[1].idea_nei_poll_amt.selectedIndex;



    if (stateSelectionIndex == 0 && cityValue != "") {
  	alert('Please select a state in addition to a city.');
  	return false;
     }
     else if (tri1 != 0 && tri2 == 0) {
  	alert('Users must select a release amount after selecting TRI release category.  Please change your selection');
  	return false;
     }
     else if (tri1 == 0 && tri2 != 0) {
  	alert('Users must select a TRI release category when selecting a TRI release amount.  Please change your selection');
  	return false;
     } 
     else if (nei1 != 0 && nei2 == 0) {
  	alert('Users must select an emission amount after selecting an NEI Emission category.  Please change your selection');
  	return false;
     }
     else if (nei1 == 0 && nei2 != 0) {
  	alert('Users must select a NEI Emission category when selecting an NEI emission amount.  Please change your selection');
  	return false;
     }      
   else {
      if ((nei2 != 0)) {
  	document.forms[1].idea_report.value = "OTISECHO PARM AIRPAGE_SORTNAME_AIRPARTIAL_DEMRADIUS=3_TRICOMMAS_actionstart=#coqbd-19 PARM actionstart2=#coqbd-19_violqtrsmax=12 R XMLRPT PARM AFS_CHKLAT";
  	// window.alert(document.forms[1].idea_report.value);
  	return isValidFacLength(document.forms[1].idea_otis_name_l, 1);
      }
      else {
  	document.forms[1].idea_report.value = "OTISECHO PARM AIRPAGE_SORTNAME_AIRPARTIAL_DEMRADIUS=3_TRICOMMAS_actionstart=#coqbd-19 PARM actionstart2=#coqbd-19_violqtrsmax=12 R XMLRPT PARM AFS_CHKLAT";
  	// window.alert(document.forms[1].idea_report.value);
  	return isValidFacLength(document.forms[1].idea_otis_name_l, 1);
      }
   }
}


function  preSubmissionCheck() {
  //Verifies that a user also enters a State when a City is inputted
  var stateSelectionIndex = document.forms[1].idea_state.selectedIndex;
  var cityValue = document.forms[1].idea_city_name_l.value;



    if (stateSelectionIndex == 0 && cityValue != "") {
  	alert('Please select a state in addition to a city.');
  	return false;
     }
   else {
  	return isValidFacLength(document.forms[1].idea_otis_name_l, 1);
     }
}

function isValidFacLength(element, formIndex) {
  //verifying that facility name is no longer than 18 characters long
   var nameArray = element.value.split(" ");
   var nameCount = nameArray.length;  // checking to see how many words were entered
   for (var i = 0; i < nameCount; i++) {
     if (nameArray[i].length > 18) {
       alert('You may enter multiple words for a Facility Name, but each word must be at most 18 characters long.');
       eval("document.forms[" + formIndex + "]." + element.name + ".focus()");
       return false;
     }
   }
   return true;
} 


function remove_special_characters(field,position) {
  //Used to Remove Special Characters that users may input
  var documentName = field.value;
  var n = documentName.length;
  var j = position;
  for (var i = 1; i <= n; i++)
  {
    if ( documentName.charAt(i-1) == "~"
      ||   documentName.charAt(i-1) == "="
      ||   documentName.charAt(i-1) == "{"
      ||   documentName.charAt(i-1) == "}"
      ||   documentName.charAt(i-1) == "["
      ||   documentName.charAt(i-1) == "]"
      ||   documentName.charAt(i-1) == "@"
      ||   documentName.charAt(i-1) == "|"
      ||   documentName.charAt(i-1) == "\\")
    {
      alert ('You have entered one of the following special characters:\n\n ~ = {  } [ ] @ | \\ \n\n Please edit your entry before submitting the form.');
      document.forms[1].elements[j].focus();
      break;
    }
  }
return false;
}


function check_for_numbers_and_length(field,position) {
  //Used to verify that only numbers are entered.  "Type" is a numeric field used to determine if zip, sic, or naics code is being entered
  var documentName = field.value;
  var n = documentName.length;  
  var j = position;

  for (var i = 1; i <= n; i++) {
    if ( documentName.charAt(i-1) != "0"
      &&   documentName.charAt(i-1) != "1"
      &&  documentName.charAt(i-1) != "2"
      &&   documentName.charAt(i-1) != "3"
      &&   documentName.charAt(i-1) != "4"
      &&   documentName.charAt(i-1) != "5"
      &&   documentName.charAt(i-1) != "6"
      &&   documentName.charAt(i-1) != "7"
      &&   documentName.charAt(i-1) != "8"
      &&   documentName.charAt(i-1) != "9"
      &&   documentName.charAt(i-1) != ",")
    {
      alert ('Please enter only numbers or numbers separated by commas.\n\nEdit your entry before submitting the form.');
      document.forms[1].elements[j].focus();
      break;
    }
    }
   
  return false;
}


function resetFirst(position) {
  //The "Any" option cannot be selected if any other option is selected-> corrects IDEA bug
  var varall_length = document.forms[1].elements[position].length; 
  //alert('var_all length= ' +  varall_length);

    for (i = 1; i<varall_length;i++) {
    	if  (document.forms[1].elements[position][i].selected == true) {
		document.forms[1].elements[position][0].selected = false;
  	}
    }
}


function showCounties() {
  //Produces a list of Counties when State is selected on all OTIS query pages

  var counter =0;
  var  varall_length= document.forms[1].idea_state.length;
    //alert('var_all length= ' +  varall_length);
    for (i = 0; i<varall_length;i++) {
    	if  (document.forms[1].idea_state[i].selected == true) {
		counter ++;
  	}
    }
    
  if (counter <2) { //run only if multiple states are not selected
  	var countyCodeIndex = document.forms[1].idea_state.selectedIndex; 
  }
  
  else { //run if multiple states are selected
    	var countyCodeIndex = 0;  
  }

  var countyCodeValue = "";
  var countyName = "";
  var countyIndexMin = 0;
  var countyIndexMax = 0;

    document.forms[1].idea_county.options.length = 0;

	for (var j = 0; j <= countyCodeIndex; j++) {
  	  countyIndexMax = countyIndexMax + parseInt(countiesByStateArray[j].substring(3));
  	}
    countyIndexMin = countyIndexMax - countiesByStateArray[countyCodeIndex].substring(3);

    numberOfCounties = countiesByStateArray[countyCodeIndex].substring(3);
    document.forms[1].idea_county.options.length = numberOfCounties;

  for (var i = countyIndexMin; i < countyIndexMax; i++) {
    countyCodeValue = countyCodesArray[i].substring(0,5);
    countyName = countyCodesArray[i].substring(8);
    document.forms[1].idea_county.options[i-countyIndexMin] = new Option(countyName, countyCodeValue);
  }
}

function setRegion() {
  //Resets Region when State is selected on AFS, PCS, RCRA and MM pages
  document.forms[1].idea_region[0].selected = true;
  showCounties();

}

function setState() {
  //Resets State when State is selected on AFS, PCS, and MM pages
  var n = document.forms[1].idea_state.options.length;
  var i =1;
  for (i; i<n; i++) {
      document.forms[1].idea_state[i].selected = false; 
  }  
  document.forms[1].idea_state[0].selected = true;
  showCounties();
}


function showWarningAFS() {
  //pops up a warning upon selection of "minor facility"
  if (document.forms[1].otis_facility_designation[3].checked)
  {
    window.open('minor_facilities_warning_air.html','','height=220,width=350,resizing=0,scrollbars=1,menubar=0');
				//alert('You have selected a source category (minors) that is not required to be put into the data system.  Some states enter data for minors, others enter partial or very limited amounts of data.  Comparisons across states relating to these facilities are not valid, and data may be less reliable.');
  }
}

function csowarning() {
  //resets search idea_permit_type from STANDARD to STANDARD,GENERAL,STORMWATER when CSO search is used
  var CSO = document.forms[1].idea_pcs_cso.selectedIndex;
   if (CSO ==0) {
	document.forms[1].idea_permit_type.value="STANDARD";
	  }
   else {
    window.open('cso_warning.html','','height=350,width=350,toolbar=yes,resizable=yes,scrollbars=yes,menubar=yes');
    document.forms[1].idea_permit_type.value="STANDARD,GENERAL,STORMWATER";
    }

}
  

function showWarningPCS() {
  //pops up a warning upon selection of "minor facility"
  if (document.forms[1].idea_facility_designation[1].checked)
  {
    window.open('minor_facilities_warning_water.html','','height=220,width=350,resizing=0,scrollbars=1,menubar=0');
				//alert('You have selected a source category (minors) that is not required to be put into the data system.  Some states enter data for minors, others enter partial or very limited amounts of data.  Comparisons across states relating to these facilities are not valid, and data may be less reliable.');
 //   document.forms[1].idea_facility_designation[0].value = "MINOR";
//	document.forms[1].idea_facility_designation[1].value = "MAJOR";
  }
		else
		{
		  document.forms[1].idea_facility_designation[0].value = "MAJOR";
		}
}

function showWarningMM() {
  //pops up a warning upon selection of "minor facility"
  if (document.forms[1].minor.checked)
  {

  	document.forms[1].otis_universe.value = "";
    window.open('minor_facilities_warning_all.html','','height=220,width=350,resizing=0,scrollbars=1,menubar=0');
				//alert('You have selected a source category (minors) that is not required to be put into the data system.  Some states enter data for minors, others enter partial or very limited amounts of data.  Comparisons across states relating to these facilities are not valid, and data may be less reliable.');
    //document.forms[1].otis_facility_designation.value = "MINOR";
  }
		else
		{
		  document.forms[1].otis_universe.value = "Y";
		}
}


function uncheckAll(field) {
  //unchecks individual designations on the RCRA page when "all" is checked
 for (i = 0; i < field.length; i++)
        field[i].checked = false;
}


function notAll() {
  //unchecks "All" on the RCRA page when Individual Designations are checked
  document.echo_search_form_rcra.idea_facility_designationAll.checked = false;
}

function adjustSFI() {
  //resets search parameters from LINKED NONLINKED to LINKED SFI when SFI is selected in MM query 
  var SFI = document.forms[1].otis_otm_sfi_universe.selectedIndex;
   if (SFI ==0) {
	document.forms[1].idea_linkage.value="LINKED NONLINKED";
	document.forms[1].idea_db_filter.value="INC FRS AFS ICI PCS RCR TRI";
	  }
   else {
    window.open('sfi_explanation.html','','height=350,width=350,resizing=0,scrollbars=1,menubar=0');
    document.forms[1].idea_linkage.value="LINKED SFI";
    document.forms[1].idea_db_filter.value="INC SFI AFS ICI PCS RCR TRI";
    }
}

function reset_SIC_2digit() {
  //resets type in 4 digit sic code when pull down is utilized in CAA, CWA, and MM pages
  document.forms[1].idea_sic2.selectedIndex = "0";
}

function reset_SIC_4digit() {
  //resets pull down sic code when text write in is utilized in CAA, CWA, and MM pages
  document.forms[1].idea_sic_any.value = "";
}

function rcra_SIC_2digit() {
  //resets type in 4 digit sic code when pull down is utilized in RCRA page
  document.forms[1].otis_rcr_sic2.selectedIndex = "0";
}

function rcra_SIC_4digit() {
  //resets pull down sic code when text write in is utilized in RCRA page
  document.forms[1].otis_rcr_sic_any.value = "";
}

function permitwarningAFS() {
  //used to give otis users a warning if they enter permit ids and do not have minor facilities checked
  //a hidden field of permit_warning determines whether it is the first time a pop-up is being displayed. 
  var warning = document.forms[1].permit_warning.value;
     if (((document.forms[1].otis_facility_designation[1].checked == false) || (document.forms[1].otis_facility_designation[2].checked == false) || (document.forms[1].otis_facility_designation[3].checked == false)) && (warning == "0")) {
         window.open('/echo/permit_warning.html','','height=250,width=300,resizing=0,scrollbars=1,menubar=0');
	 document.forms[1].permit_warning.value="1";
   	
  }
  
}

function permitwarningPCS() {
  //used to give otis users a warning if they enter permit ids and do not have minor facilities checked
  //a hidden field of permit_warning determines whether it is the first time a pop-up is being displayed. 
  var warning = document.forms[1].permit_warning.value;
     if ((document.forms[1].idea_facility_designation[1].checked == false) && (warning == "0")) {
        window.open('/echo/permit_warning.html','','height=250,width=300,resizing=0,scrollbars=1,menubar=0');
       	document.forms[1].permit_warning.value="1";
   	
  }
  
}

function permitwarningICP() {
  //used to give otis users a warning if they enter permit ids and do not have minor facilities checked
  //a hidden field of permit_warning determines whether it is the first time a pop-up is being displayed. 
  var warning = document.forms[1].permit_warning.value;
     if ((document.forms[1].idea_facility_designation[1].checked == false) && (warning == "0")) {
        window.open('/echo/permit_warning.html','','height=250,width=300,resizing=0,scrollbars=1,menubar=0');
       	document.forms[1].permit_warning.value="1";
   	
  }
  
}

function permitwarningRCR() {
  //used to give otis users a warning if they enter permit ids and do not have minor facilities checked
  //a hidden field of permit_warning determines whether it is the first time a pop-up is being displayed. 
  var warning = document.forms[1].permit_warning.value;
     if (((document.forms[1].idea_facility_designation[1].checked == false) || (document.forms[1].idea_facility_designation[3].checked == false) || (document.forms[1].idea_facility_designation[4].checked == false) || (document.forms[1].idea_facility_designation[5].checked == false)) && (warning == "0")) {
        window.open('/echo/permit_warning.html','','height=250,width=300,resizing=0,scrollbars=1,menubar=0');
       	document.forms[1].permit_warning.value="1";
   	
  }
  
}

function permitwarningMM() {
  //used to give otis users a warning if they enter permit ids and do not have minor facilities checked
  //a hidden field of permit_warning determines whether it is the first time a pop-up is being displayed. 
  var warning = document.forms[1].permit_warning.value;
     if ((document.forms[1].idea_major.checked == false) && (warning == "0")) {
        window.open('/echo/permit_warning.html','','height=250,width=300,resizing=0,scrollbars=1,menubar=0'); 
       	document.forms[1].permit_warning.value="1";
   	
  }
  
}

function opencustomcaa() {
  //opens custom column pop-up
  var extra = document.forms[1].otis_custom_col.value;
  var link =  '/echo/caa_customized_columns.html?vars=' + extra;
  window.open(link,'sub','height=500,width=500,left=120,top=200,resizable,scrollbars');


}

function opencustomcwa() {
  //opens custom column pop-up
  var extra = document.forms[1].otis_custom_col.value;
  var link =  '/echo/cwa_customized_columns.html?vars=' + extra;
  window.open(link,'sub','height=500,width=500,left=120,top=200,resizable,scrollbars');  

}

function opencustomicp() {
  //opens custom column pop-up
  var extra = document.forms[1].otis_custom_col.value;
  var link =  '/echo/icp_customized_columns.html?vars=' + extra;
  window.open(link,'sub','height=500,width=500,left=120,top=200,resizable,scrollbars');    

}

function opencustomrcra() {
  //opens custom column pop-up
  var extra = document.forms[1].otis_custom_col.value;
  var link =  '/echo/rcra_customized_columns.html?vars=' + extra;
  window.open(link,'sub','height=500,width=500,left=120,top=200,resizable,scrollbars');    

}

function opencustommm() {
  //opens custom column pop-up
  var extra = document.forms[1].otis_custom_col.value;
  var link =  '/echo/mm_customized_columns.html?vars=' + extra;  
  window.open(link,'sub','height=500,width=500,left=120,top=200,resizable,scrollbars');

}

function resetCustom() {
  document.forms[1].otis_custom_col.value = "7,21,12,24,13,19,18,23,15,29";
  document.forms[1].idea_db_filter.value = "INC AFS ICI FRS PCS ICP RCR TRI NEI DEM"; 
  document.forms[1].map_database.value = "";
  document.forms[1].idea_report.value="OTISECHO PARM SORTNAME_TRICOMMAS_PENCOMMAS_DEMRADIUS=3_violqtrsmax=12_actionstart=#coqbd-19 PARM actionstart2=#coqbd-19";
}

function resetMapmm() {
  document.forms[1].otis_custom_col.value = "7,21,12,24,13,19,18,23,15,29";
  document.forms[1].idea_db_filter.value = "INC AFS ICI FRS PCS ICP RCR TRI NEI DEM";
  document.forms[1].map_database.value = "MAPECHO_MM";  
  document.forms[1].idea_report.value="OTISECHO PARM SORTNAME_TRICOMMAS_PENCOMMAS_DEMRADIUS=3_violqtrsmax=12_actionstart=#coqbd-19 PARM actionstart2=#coqbd-19 R XMLRPT PARM CHKLAT";
}

function resetCustomCAA() {
  document.forms[1].map_database.value = "";
  document.forms[1].otis_custom_col.value = "7,21,12,13,19,18,23,15,29";
  document.forms[1].idea_db_filter.value = "INC 1DBF AFS ICI TRI NEI FRS DEM";
  document.forms[1].idea_report.value = "OTISECHO PARM AIRPAGE_SORTNAME_AIRPARTIAL_DEMRADIUS=3_TRICOMMAS_actionstart=#coqbd-19 PARM actionstart2=#coqbd-19_violqtrsmax=12";
}

function resetMapCAA() {
  document.forms[1].map_database.value = "MAPECHO_AFS";
  document.forms[1].otis_custom_col.value = "7,21,12,13,19,18,23,15,29";
  document.forms[1].idea_db_filter.value = "INC 1DBF AFS ICI TRI NEI FRS DEM";
  document.forms[1].idea_report.value = "OTISECHO PARM AIRPAGE_SORTNAME_AIRPARTIAL_DEMRADIUS=3_TRICOMMAS_actionstart=#coqbd-19 PARM actionstart2=#coqbd-19_violqtrsmax=12 R XMLRPT PARM AFS_CHKLAT";
}

function resetCustomPCS() {
  document.forms[1].map_database.value = "";
  document.forms[1].otis_custom_col.value = "7,21,12,24,13,19,18,23,15,29";
  document.forms[1].idea_db_filter.value = "INC 1DBF PCS TRI FRS DEM ICI";
  document.forms[1].idea_report.value = "OTISECHO PARM WATERPAGE_SORTNAME_DEMRADIUS=3_TRICOMMAS_PENCOMMAS PARM actionstart=#coqbd-19_actionstart2=#coqbd-19_violqtrsmax=12";  
}

function resetMapPCS() {
  document.forms[1].map_database.value = "MAPECHO_PCS";
  document.forms[1].otis_custom_col.value = "7,21,12,24,13,19,18,23,15,29";
  document.forms[1].idea_db_filter.value = "INC 1DBF PCS TRI FRS DEM ICI";
  document.forms[1].idea_report.value = "OTISECHO PARM WATERPAGE_SORTNAME_DEMRADIUS=3_TRICOMMAS_PENCOMMAS PARM actionstart=#coqbd-19_actionstart2=#coqbd-19_violqtrsmax=12 R XMLRPT PARM PCS_CHKLAT";  
}

function resetCustomICP() {
  document.forms[1].map_database.value = "";
  document.forms[1].otis_custom_col.value = "7,21,12,24,13,19,18,23,15,29";
  document.forms[1].idea_db_filter.value = "INC 1DBF ICP TRI FRS DEM ICI";
  document.forms[1].idea_report.value = "OTISECHO PARM WATERPAGE_SORTNAME_DEMRADIUS=3_TRICOMMAS_PENCOMMAS PARM actionstart=#coqbd-19_actionstart2=#coqbd-19_violqtrsmax=12";
}

function resetMapICP() {
  document.forms[1].map_database.value = "MAPECHO_ICP";
  document.forms[1].otis_custom_col.value = "7,21,12,24,13,19,18,23,15,29";
  document.forms[1].idea_db_filter.value = "INC 1DBF ICP TRI FRS DEM ICI";
  document.forms[1].idea_report.value = "OTISECHO PARM WATERPAGE_SORTNAME_DEMRADIUS=3_TRICOMMAS_PENCOMMAS PARM actionstart=#coqbd-19_actionstart2=#coqbd-19_violqtrsmax=12 R XMLRPT PARM ICP_CHKLAT";
}

function resetCustomRCR() {
  document.forms[1].map_database.value = "";
  document.forms[1].otis_custom_col.value = "7,21,12,13,19,18,23,15,29";
  document.forms[1].idea_db_filter.value = "INC 1DBF RCR ICI TRI FRS DEM";
  document.forms[1].idea_report.value = "OTISECHO PARM rcrapage_SORTNAME_tricommas_pencommas_DEMRADIUS=3_violqtrsmax=12 PARM actionstart=#coqbd-19_actionstart2=#coqbd-19";
}

function resetMapRCR() {
  document.forms[1].map_database.value = "MAPECHO_RCR";
  document.forms[1].otis_custom_col.value = "7,21,12,13,19,18,23,15,29";
  document.forms[1].idea_db_filter.value = "INC 1DBF RCR ICI TRI FRS DEM";
  document.forms[1].idea_report.value = "OTISECHO PARM rcrapage_SORTNAME_tricommas_pencommas_DEMRADIUS=3_violqtrsmax=12 PARM actionstart=#coqbd-19_actionstart2=#coqbd-19 R XMLRPT PARM RCR_CHKLAT";
}

function resetDefaultValues(what) {
  //function for resetting the form
    for (var i=0, j=what.elements.length; i<j; i++) {
        myType = what.elements[i].type;
        if (myType == 'checkbox' || myType == 'radio')
            what.elements[i].checked = what.elements[i].defaultChecked;
        if (myType == 'hidden' || myType == 'password' || myType == 'text' || myType == 'textarea')
            what.elements[i].value = what.elements[i].defaultValue;
        if (myType == 'select-one' || myType == 'select-multiple')
            for (var k=0, l=what.elements[i].options.length; k<l; k++)
                what.elements[i].options[k].selected = what.elements[i].options[k].defaultSelected;
        }
          
}

function lengthtest(min,max,field) {
//function takes 3 parameters - minimum # of characters, maximum# of characters, and field propety
  var minimum = min;
  var maximum = max;
  strValue = field.value;
  strValue = strValue.replace(/"/g, "")  // getting rid of quotes
  strValue = strValue.replace(/\r\n/g, ",")  // getting rid of carriage returns
  strValue = strValue.replace(/\s/g, "")  // getting rid of spaces
  var length = strValue.length;
  if ((length != "0") && (strValue.charAt(length-1) != ",")) { //adding comma at last digit of last permit
      strValue = strValue + ",";
   } //end if length !=0

  var counter = 0; //setting up a counter value to determine the number of commas in the list

		 for (var r = 1; r <= length+1; r++) { //for loop determines number of commas in list
			if ((strValue.charAt(r-1) == ",")) {
				  counter = counter +1;
			}  //end if strValue.charAt(r-1)
			else {
				   counter = counter;
			}  //end else
		  }  //end for r=1
		a = strValue.split(",");  //splitting the list at commas
		y = "";  //setting up an array for valid ids
		q = "";  //setting up an array for invalid ids
		var invalid = 0; //variable to denote whether the invalid array is ever accessed

		 for (var k = 0; k < counter; k++) { //increment through each id
			  var sublength = a[k].length;  //determining length of each id in array

              for (var b=minimum;b<=maximum;b++) { //for number of characters in id, starting with minimum
				  if (sublength == b) {  //if id is correct length, add to array of accepted ids, break from loop
					if (y != "") {
					s = new Array (y, ",", a[k]);
						y = s.join("");
						break;
					} // end if (y != "")
					else {
						s = new Array (a[k]);
						y = s.join("");
						break;
					}  //end else
				  }  //end if sublength ==b
				  if ((sublength != b) && (b==maximum)) {  //if id is incorrect length, add to array of invalid ids
					if (q != "") {
					  z = new Array (q, ",", a[k]);
					  q = z.join("");
					  invalid = 1;
					 }  // end if (q != "")
					else {
					  z = new Array (a[k]);
					  q = z.join("");
					  invalid = 1;
					}  //end else
				  } //end if ((sublength != b)

			} // end for (var b=minimum;b<=maximum;b++)
		 }  // end for (var k = 0; k < counter; k++)

		 if (invalid == 1) { //if there are ids in our invalid list
		 	if (minimum == maximum) {
			window.alert("The following invalid values are not " + minimum + " characters long:" + "\n" +  q + "\n" + "These invalid values will be removed");
		 	}
		 	else {
			window.alert("The following invalid values are not between " + minimum + " and " + maximum + " characters long:" + "\n" +  q + "\n" + "These invalid values will be removed");
			}
			field.value = y;
		   }  //end if invalid

} //end lengthtest


