gsInSituBreastCode = '400';
gsMapPage          = "map.withimage.php?";
gsSlowLoadWarning  = "slowMapWarning.php?";
gsMapAnchor        = "#map";

function GetParams() 
	{
    var sLocation = new String(window.location);     //property contains domain, directory, 
                                                     //filename of the document plus the params (and the "?" before the params).
    var sParamsList = new String(sLocation.substr(sLocation.lastIndexOf("?")+1));
        
    if (sParamsList.search("#") != -1)	
       var newParamsList =  sParamsList.substring(0, sParamsList.search("#"));
    else 
    	 var newParamsList = new String(sParamsList);
    
    return newParamsList.split('&');
	}

function verifyCheckBoxes() {
	var formObj = document.UserForm;
	
   var list = formObj.CancerBox
	var index = list.selectedIndex;
	var Cancer = list.options[index].value;

   var list     = formObj.IncdMortBox;
	var index = list.selectedIndex;
	var DataType = list.options[index].value;
	
   var list = formObj.GeoAreaBox
	var index = list.selectedIndex;
	var Geo = list.options[index].value;
	
   var list = formObj.RaceBox
	var index = list.selectedIndex;
	var Race = list.options[index].value;
	
   var list = formObj.AgeBox
	var index = list.selectedIndex;
	var Age = list.options[index].value;
	
   var list = formObj.GenderBox
	var index = list.selectedIndex;
	var Sex = list.options[index].value;
	
   var list  = formObj.YearBox
	var index = list.selectedIndex;
	var Year  = list.options[index].value;
	
   var list = formObj.IntervalTypeBox
	var index = list.selectedIndex;
	var IntType = list.options[index].value;

   var list = formObj.NumIntervalsBox
	var index = list.selectedIndex;
	var NumInts = list.options[index].value;

   var list = formObj.ColorsBox
	var index = list.selectedIndex;
	var Color = list.options[index].value;

	var okToSubmit = 0;
	var listForError = "";
	
	if (Geo == "**") 
		listForError = listForError + "Area\n";
	if (Cancer == "***") 
		listForError = listForError + "Cancer\n";
	if (DataType == "*") 
		listForError = listForError + "Data\n";
	if (Race == "**") 
		listForError = listForError + "Race\n";
	if (Sex == "*") 
		listForError = listForError + "Sex\n";
	if (Age == "***") 
		listForError = listForError + "Age\n";
	if (Year == "*") 
		listForError = listForError + "Year\n";
	if (IntType == "*") 
		listForError = listForError + "Interval Type\n";
	if (NumInts == "**") 
		listForError = listForError + "Number of Intervals\n";
	if (Color == "*") 
		listForError = listForError + "Color Scheme\n";
		
	return listForError;
}


function submitQueryString() {	
	//alert("in submitQueryString");
   var formObj = document.UserForm;

   var list = formObj.RaceBox
	var index = list.selectedIndex;
	var Race = list.options[index].value;
	
   var list = formObj.AgeBox
	var index = list.selectedIndex;
	var Age = list.options[index].value;
	
   var list = formObj.GenderBox
	var index = list.selectedIndex;
	var Sex = list.options[index].value;
	
	/*IncdMortBox is currently a hidden value*/
   var list = formObj.IncdMortBox
	var index = list.selectedIndex;
	var DataType = list.options[index].value;
	
   var list = formObj.GeoAreaBox
	var index = list.selectedIndex;
	var Geo = list.options[index].value;
	
   var list = formObj.CancerBox
	var index = list.selectedIndex;
	var Cancer = list.options[index].value;
	
   var list    = formObj.YearBox;
	var index   = list.selectedIndex;
	var Year    = list.options[index].value;

   var list    = formObj.IntervalTypeBox;
	var index   = list.selectedIndex;
	var IntType = list.options[index].value;
	
   var list    = formObj.NumIntervalsBox;
	var index   = list.selectedIndex;
	var NumInts = list.options[index].value;
	
   var list    = formObj.ColorsBox;
	var index   = list.selectedIndex;
	var Colors  = list.options[index].value;
	

	var queryString = Geo+"&"+Age+"&"+Cancer+"&"+Race+"&"+Sex+"&"+DataType+"&"+Year+"&"+IntType+"&"+NumInts+"&"+Colors;
	
	if(Geo != '99')
		{
		formObj.target='_self';
   	formObj.action = gsMapPage + queryString + gsMapAnchor;
		}
	else
		{
		formObj.target='_self';
   	formObj.action = gsSlowLoadWarning + queryString + gsMapAnchor;
		}
		
  
   var listForError = verifyCheckBoxes() ;
		
	if (listForError != "")
		alert("Please make a selection for each category before continuing.");
	
	else formObj.submit();
	
	/*
	//No longer displaying alert box, just showing map of no data;
	else 
		{
		var hispProblem = 0;
  		if ((Race == '05') && (DataType == '1') && ((Geo == '20') || (Geo == '21')))  
			hispProblem++;

		if (hispProblem == 0) //no problem with the Hispanic selection
	  		formObj.submit();
	  	else
	   	alert ("Hispanic data is not available for the geographic area/data type combination that you have selected.");
		}// else boxes < 7
		*/
}

function submitHiddenFormString() {	
	//This function is intended to act like SubmitQueryString, except it gets is data from the Tool Form 
	// and  is only to be called by the intermediate warning page
	//alert("in submitQueryString");
   var formObj = document.ToolForm;

   var list = formObj.RaceBox
	var Race = list.value;
	
   var list = formObj.AgeBox
	var Age = list.value;
	
   var list = formObj.GenderBox
	var Sex = list.value;
	
	/*IncdMortBox is currently a hidden value*/
   var list = formObj.IncdMortBox
	var DataType = list.value;
	
   var list = formObj.GeoAreaBox
	var Geo = list.value;
	
   var list = formObj.CancerBox
	var Cancer = list.value;
	
   var list    = formObj.YearBox;
	var Year    = list.value;

   var list    = formObj.IntervalTypeBox;
	var IntType = list.value;
	
   var list    = formObj.NumIntervalsBox;
	var NumInts = list.value;
	
   var list    = formObj.ColorsBox;
	var Colors  = list.value;
	

	var queryString = Geo+"&"+Age+"&"+Cancer+"&"+Race+"&"+Sex+"&"+DataType+"&"+Year+"&"+IntType+"&"+NumInts+"&"+Colors;
	
	formObj.target='_self';
  	formObj.action = gsMapPage + queryString + gsMapAnchor;
	formObj.submit();
}

function changeSex() {
//	alert ('in changeSex');
	
    var list = document.UserForm.CancerBox
	var index = list.selectedIndex;
	var cancer = list.options[index].value;
	
	var genderBox = document.UserForm.GenderBox;
	var current=genderBox.selectedIndex;
	var currentGender = genderBox.options[current].value;
	if (  (cancer == '055') || (cancer == '057') || (cancer == '058') || (cancer == '061') || (cancer == '400')  )  {
		genderBox.length = 0;
		genderBox.options[0] = new Option("Females");
		genderBox.options[0].value = "2";
		genderBox.selectedIndex = 0; //female
		//genderBox.disabled = true;
	}
	else if (cancer == '066') {
		genderBox.length = 0;
		genderBox.options[0] = new Option("Males");
		genderBox.options[0].value = "1";
		genderBox.selectedIndex = 0; //male
		//genderBox.disabled = true;
	}
	else {
		//genderBox.disabled = false;
		genderBox.length = 0;
		genderBox.options[0] = new Option(CHOOSE_BEGINNING + "choose sex" + CHOOSE_END);
		genderBox.options[0].value = "*";
		genderBox.options[1] = new Option("Both Sexes");
		genderBox.options[1].value = "0";
		genderBox.options[2] = new Option("Males");
		genderBox.options[2].value = "1";
		genderBox.options[3] = new Option("Females");
		genderBox.options[3].value = "2";
		
		genderBox.selectedIndex = 0; 
		for (i=0; i<genderBox.length; i++) {
			if (genderBox.options[i].value == currentGender) {
				genderBox.selectedIndex=i;
				break;
			}
		}
	}//else
		
}

function changeAge() {
//	alert("in changeAge()");
	//var cancer = document.UserForm.CancerBox.value;
	
    var list = document.UserForm.CancerBox
	var index = list.selectedIndex;
	var cancer = list.options[index].value;
	
	var ageBox = document.UserForm.AgeBox;
	var current=ageBox.selectedIndex;
	var currentAge = ageBox.options[current].value;
	if   (cancer == '515')  {
		ageBox.length = 0;
		ageBox.options[0] = new Option("<20");
		ageBox.options[0].value = "015";
		ageBox.selectedIndex = 0; //female
		//ageBox.disabled = true;
	}
	else if (cancer == '516') {
		ageBox.length = 0;
		ageBox.options[0] = new Option("<15");
		ageBox.options[0].value = "016";
		ageBox.selectedIndex = 0; //male
		//ageBox.disabled = true;
	}
	else {
		//ageBox.disabled = false;
		ageBox.length = 0;
		ageBox.options[0] = new Option(CHOOSE_BEGINNING + "choose age group" + CHOOSE_END);
		ageBox.options[0].value = "***";
		ageBox.options[1] = new Option("All Ages");
		ageBox.options[1].value = "001";
		ageBox.options[2] = new Option("<50");
		ageBox.options[2].value = "009";
		ageBox.options[3] = new Option("50+");
		ageBox.options[3].value = "136";
		ageBox.options[4] = new Option("<65");
		ageBox.options[4].value = "006";
		ageBox.options[5] = new Option("65+");
		ageBox.options[5].value = "157";
		
		ageBox.selectedIndex = 0; 
		for (i=0; i<ageBox.length; i++) {
			if (ageBox.options[i].value == currentAge) {
				ageBox.selectedIndex=i;
				break;
			}
		}
	}//else
		
}

function changeYear() 
{
   var list    = document.UserForm.GeoAreaBox
	var index   = list.selectedIndex;
	var geoarea = list.options[index].value;

	var yearBox = document.UserForm.YearBox;
	var current=yearBox.selectedIndex;
	var currentYear = yearBox.options[current].value;
	yearBox.length = 0;
	yearBox.options[0] = new Option(CHOOSE_BEGINNING + "choose year" + CHOOSE_END);
	yearBox.options[0].value = "*";
	yearBox.options[1] = new Option("Latest 5-year average");
	yearBox.options[1].value = "0";
	if( geoarea == '00') 
   	{//Incidence by State, add in the single year
	   yearBox.options[2] = new Option("Latest single year (US by state)");
	   yearBox.options[2].value = "1";
	   }
	yearBox.selectedIndex = 0; 
	bValueFound = false;
	for (i=0; i<yearBox.length && !bValueFound; i++) 
	   {
		if (yearBox.options[i].value == currentYear) 
		   {
			yearBox.selectedIndex=i;
			bValueFound = true;
		   }
	   }
}

function changeNumIntervals() 
{
   var list    = document.UserForm.ColorsBox;
	var index   = list.selectedIndex;
	var color   = list.options[index].value;
	
	var intervalBox = document.UserForm.NumIntervalsBox;
	var prevIndex   = intervalBox.selectedIndex;
	intervalBox.length = 0;
	intervalBox.options[0] = new Option("4");
	intervalBox.options[0].value = "4";
	intervalBox.options[1] = new Option("5");
	intervalBox.options[1].value = "5";
	intervalBox.options[2] = new Option("6");
	intervalBox.options[2].value = "6";
	intervalBox.options[3] = new Option("7");
	intervalBox.options[3].value = "7";
	intervalBox.options[4] = new Option("8");
	intervalBox.options[4].value = "8";
	intervalBox.options[5] = new Option("9");
	intervalBox.options[5].value = "9";
	if ( color == '0' || color == '3' )  
		{//Diverging color schemes, 10 Intervals allowed
		intervalBox.options[6] = new Option("10");
		intervalBox.options[6].value = "10";
		}
	if(intervalBox.length >= (prevIndex+1))
		{
		intervalBox.selectedIndex = prevIndex;	
		}
	else
		{
		intervalBox.selectedIndex = (intervalBox.length -1);	
		}
}

function changeColors() 
{
   var list    		= document.UserForm.NumIntervalsBox;
	var index   		= list.selectedIndex;
	var numIntervals 	= list.options[index].value;
	
	var colorBox 		= document.UserForm.ColorsBox;
	index   				= colorBox.selectedIndex;
	var currentColor 	= colorBox.options[index].value;
	colorBox.length = 0;
	if(numIntervals < 10)
		{
		colorBox.options[0] = new Option("Red-Yellow-Blue (Div)");
		colorBox.options[0].value = "0";
		colorBox.options[1] = new Option("Brown-White-Blue/Green (Div)");
		colorBox.options[1].value = "3";
		colorBox.options[2] = new Option("Red-Orange-White (Seq)");
		colorBox.options[2].value = "1";
		colorBox.options[3] = new Option("Blue-Green-Yellow (Seq)");
		colorBox.options[3].value = "2";
		}
	else
		{//Diverging color schemes, 10 Intervals allowed
		colorBox.options[0] = new Option("Red-Yellow-Blue (Div)");
		colorBox.options[0].value = "0";
		colorBox.options[1] = new Option("Brown-White-Blue/Green (Div)");
		colorBox.options[1].value = "3";
		}
	colorBox.selectedIndex = 0; 
	bValueFound = false;
	for (i=0; i<colorBox.length && !bValueFound; i++) {
		if (colorBox.options[i].value == currentColor) {
			colorBox.selectedIndex=i;
			bValueFound = true;
		}
	}
}



function UpdateSelected() {
//	alert ("in UpdateSelected");
   paramArray = GetParams();

   var VisibleForm 		= document.UserForm;
	var CurrentGeo 	  	= VisibleForm.GeoAreaBox;
	var CurrentAge 		= VisibleForm.AgeBox;
	var CurrentCancer   	= VisibleForm.CancerBox;
	var CurrentRace 		= VisibleForm.RaceBox;
	var CurrentGender   	= VisibleForm.GenderBox;
	var CurrentIncdMort 	= VisibleForm.IncdMortBox;
	var CurrentYear		= VisibleForm.YearBox;
	var CurrentIntvType 	= VisibleForm.IntervalTypeBox;
	var CurrentNumIntvs 	= VisibleForm.NumIntervalsBox;
	var CurrentColors    = VisibleForm.ColorsBox;

	var CurrentGeoHidden 		= paramArray[0];
	var CurrentAgeHidden 		= paramArray[1];
	var CurrentCancerHidden 	= paramArray[2];
	var CurrentRaceHidden 		= paramArray[3];
	var CurrentGenderHidden 	= paramArray[4];
	var CurrentIncdMortHidden 	= paramArray[5];
	var CurrentYearHidden      = paramArray[6];
	var CurrentIntvTypeHidden  = paramArray[7];
	var CurrentNumIntvsHidden	= paramArray[8];
	var CurrentColorScheme     = paramArray[9];
	
	var bValueFound;
	
	//Update Geographic Areas
	bValueFound = false;
	for (var i=0; i<CurrentGeo.length && !bValueFound; i++) {
	  if (CurrentGeoHidden == CurrentGeo.options[i].value) {
	    CurrentGeo.selectedIndex=i;
	    bValueFound = true;
	  }
	}
	//Update Cancer Type
	bValueFound = false;
	for (var i=0; i<CurrentCancer.length && !bValueFound; i++) {
	  if (CurrentCancerHidden == CurrentCancer.options[i].value) {
	    CurrentCancer.selectedIndex=i;
	    bValueFound = true;
	  }
	}
	//Update Interval Type
	bValueFound = false;
	for (var i=0; i<CurrentIntvType.length && !bValueFound; i++)	{
	  if (CurrentIntvTypeHidden == CurrentIntvType.options[i].value) {
	    CurrentIntvType.selectedIndex=i;
	    bValueFound = true;
	  }
	}
	//Update Number Of Intervals
	bValueFound = false;
	for (var i=0; i<CurrentNumIntvs.length && !bValueFound; i++)	{
	  if (CurrentNumIntvsHidden == CurrentNumIntvs.options[i].value) {
	    CurrentNumIntvs.selectedIndex=i;
	    bValueFound = true;
	  }
	}
	//Update Color Scheme
	bValueFound = false;
	for (var i=0; i<CurrentColors.length && !bValueFound; i++)	{
	  if (CurrentColorScheme == CurrentColors.options[i].value) {
	    CurrentColors.selectedIndex=i;
	    bValueFound = true;
	  }
	}
	
	//Update Incd/Mort selection
	bValueFound = false;
	for (var i=0; i<CurrentIncdMort.length && !bValueFound; i++) {
	  if (CurrentIncdMortHidden == CurrentIncdMort.options[i].value)  {
	    CurrentIncdMort.selectedIndex=i;
	    bValueFound = true;
	  }
	}
	
	//Refresh Incd/Mort,Race, Gender and Age boxes base on Geo region cancer type selection
	changeDataSource();
	changeSex();
	changeAge();
	changeYear();
	
	//Update Gender
	bValueFound = false;
	for (var i=0; i<CurrentGender.length && !bValueFound; i++) {
	  if (CurrentGenderHidden == CurrentGender.options[i].value) {
	    CurrentGender.selectedIndex=i;
	    bValueFound = true;
	  }
	}
	//Update Race
	bValueFound = false;
	for (var i=0; i<CurrentRace.length && !bValueFound; i++) {
	  if (CurrentRaceHidden == CurrentRace.options[i].value) {
	    CurrentRace.selectedIndex=i;
	    bValueFound = true;
	  }
	}
   //Update Age
	bValueFound = false;
	for (var i=0; i<CurrentAge.length && !bValueFound; i++) {
	  if (CurrentAgeHidden == CurrentAge.options[i].value)  {
	    CurrentAge.selectedIndex=i;
	    bValueFound = true;
	  }
	}
   //Update Year
	bValueFound = false;
	for (var i=0; i<CurrentYear.length && !bValueFound; i++) {
	  if (CurrentYearHidden == CurrentYear.options[i].value)  {
	    CurrentYear.selectedIndex=i;
	    bValueFound = true;
	  }
	}
}

function UpdateZoomSelection()
{
	var ZoomForm     = document.ZoomBoxForm;
	var ZoomStateBox = ZoomForm .ZoomToStateBox;
	var sSearchStr   = oCurrExtent.minx + ";" + oCurrExtent.maxy + ";" + oCurrExtent.maxx + ";" + oCurrExtent.miny;
	var bFoundStr    = false;
	var i;
	
	for(i = 0; i<ZoomStateBox.length && !bFoundStr;i++)
		{
		if(ZoomCoordinates[ZoomStateBox.options[i].value] == sSearchStr)
			{
			ZoomStateBox.selectedIndex=i;
			bFoundStr = true;
			}
		}
}

function build_URL()
	{
	var baseRef = location.href.substring(0, location.href.lastIndexOf("#"));
	var newRef = baseRef + gsMapAnchor;
		
	window.location.href = newRef;
	}

//Updates the data sources available in the Data listbox
function changeDataSource() 
	{ // if in situ breast is selected then mortality can't be an option
	var VisibleForm = document.UserForm;
	var CurrentCancer = VisibleForm.CancerBox; 
	var CurrentIncdMort = VisibleForm.IncdMortBox;
	
	var CurrentSelectedIndex = CurrentIncdMort.selectedIndex;
	if (CurrentSelectedIndex == -1)
		CurrentSelectedIndex = 0;

	var CurrentSelected = CurrentIncdMort.options[CurrentSelectedIndex].text;
	
	var CurrentCancerSelectedIndex = CurrentCancer.selectedIndex;
	if (CurrentCancerSelectedIndex == -1) 
		CurrentCancerSelectedIndex = 0;
	
	var CurrentCancerSelected = CurrentCancer.options[CurrentCancerSelectedIndex].value;

	CurrentIncdMort.length=0;	
	CurrentIncdMort.options[0] = new Option(CHOOSE_BEGINNING + "choose data type" + CHOOSE_END);
	CurrentIncdMort.options[0].value = "*";         
	var counter = 1;
	CurrentIncdMort.options[counter] = new Option("Incidence");
	CurrentIncdMort.options[counter].value = "1";
	counter++;
	
	if (CurrentCancerSelected != gsInSituBreastCode) 
		{
		CurrentIncdMort.options[counter] = new Option("Mortality");
		CurrentIncdMort.options[counter].value = "2";
		counter++;
		}	
	
	CurrentIncdMort.selectedIndex = CurrentSelectedIndex	;
	}

