var period="daily";
var periodCount=28;
var locationID=0;
var locationType="station";
var locationTypeString="";
var plannerURL="";
var plannerID="data_table";
var graphType="jpeg";
var draw_temps=0;
var begWeekDay="sunday";
var endWeekDay="saturday";

var statsURL="";
var statsID="ver_stat";

var max_days=365;



//function drawGraph(beg,end,id){
function run_graph(id){
	//Get the variables we need for a particular script
	var time=new Date();
  var time_sec=time.getTime();
  var height=490;
  var width=721;
  
  var functionResult=new Object;
  //Will use this later to store the settings for all the graphs
  var graphSettings=new Object;
  

  
  var overlay_string="";
  
  functionHash="units";
  functionResult=getUnitSettings(functionHash);
  if(functionResult['valid'] && functionResult['valid'] == 1){
  	units=functionResult[functionHash];
  }
  else{
  	//Failed
  	alert("There was a problem loading the settings in getUnitSettings()");
  	return 1;
  }
  
  functionHash="calendar";
  functionResult=getCalendarSettings(functionHash);
  if(functionResult['valid'] && functionResult['valid'] == 1){
  	calendar=functionResult[functionHash];
  }
  else{
  	//Failed
  	alert("There was a problem loading the settings in getCalendarSettings()");
  	return 1;
  }
  
  functionHash="flash";
  functionResult=getFlashSettings(functionHash);
  if(functionResult['valid'] && functionResult['valid'] == 1){
  	flash=functionResult[functionHash];
  }
  else{
  	//Failed
  	alert("There was a problem loading the settings in getFlashSettings()");
  	return 1;
  }
  
  if(currentPage == "console"){
  	//Get the start date
  	
  	/*REMOVE OWN FUNCTION
  	if(document.getElementById('start_date') && document.getElementById('start_date').value){
  		startDate=document.getElementById('start_date').value;
  		startYmd=dateConvertm_d_Y_to_Ymd(startDate);
  		if(startYmd && startYmd.length == 8){
  			//Good to go
  		}
  		else{
  			//Must be an issue w/the date
  			return;
  		}
  	}
  	else{
  		//Start date does not appear to be set
  		alert("start_date does not appear to be set");
  		return;
  	}
  	*/
  	
  	
  	
  	functionHash="period";
  	functionResult=getPeriodSettings(functionHash);
  	if(functionResult['valid'] && functionResult['valid'] == 1){
  		period=functionResult[functionHash];
  	}
  	else{  		
  		//Failed
  		alert("There was a problem loading the settings in getPeriodSettings()");
  		return 1;
  	}
  	
  	if(period == "daily"){
  		if(isChecked('elem',document.ver_graph.weekend) == 1){
  			overlay_string+="&weekend=1";
  		}
  		per="d";
  	}
  	else if(period == "weekly"){
  		per="w";
  	}
  	
  	
  	functionResult=getLocationSettings();
  	if(functionResult['valid_locationID'] && functionResult['valid_locationID'] == 1 && functionResult['valid_locationType'] && functionResult['valid_locationType'] == 1){
  		locationID=functionResult['locationID'];
  		locationType=functionResult['locationType'];
  		locationTypeString=functionResult['locationTypeString'];
  	}
  	else{  		
  		//Failed
  		//REMOVEalert("There was a problem loading the settings in getLocationSettings()");
  		alert("Please choose a valid location from the dropdown boxes");
  		return 1;
  	}
  	
  	
  	
  	functionHash="vis";
  	functionResult=getVisSettings(functionHash);
  	if(functionResult['valid'] && functionResult['valid'] == 1){
  		vis=functionResult[functionHash];
  	}
  	else{  		
  		//Failed
  		alert("There was a problem loading the settings in getVisSettings()");
  		return 1;
  	}
  	
  	functionHash="startYmd";
  	functionID="start_date";
  	functionResult=getDateSettings(functionHash,functionID);
  	if(functionResult['valid'] && functionResult['valid'] == 1){
  		startYmd=functionResult[functionHash];
  	}
  	else{  		
  		//Failed
  		//REMOVEalert("There was a problem loading the settings in getDateSettings()");
  		alert("Please specify a valid start date in MM/DD/YYYY format (ex. 01/20/2009)");
  		return 1;
  	}
  	
  	functionHash="periodCount";
  	functionResult=getPeriodCountSettings(functionHash);
  	if(functionResult['valid'] && functionResult['valid'] == 1){
  		periodCount=functionResult[functionHash];
  	}
  	else{  		
  		//Failed
  		alert("There was a problem loading the settings in getPeriodCountSettings()");
  		return 1;
  	}
  	
  	if(periodCount == 28){
  		width=721;
  		$("#data_table table").css("margin-left","0px","width","721px");
  		$("#graph").css("margin-left","79px","width","721px");
  	}
  	else if(periodCount == 35){
  		width=882;
  		$("#data_table table").css("margin-left","1px","width","882px");
  		$("#graph").css("margin-left","0px");
  	}
  	else{
  		//Invalid
  		alert("Invalid # of periods ("+periodCount+")");
  		return;
  	}
  	
  	functionHash="data_var";
  	functionResult=getVariableSettings(functionHash);
  	if(functionResult['valid'] && functionResult['valid'] == 1){
  		data_var=functionResult[functionHash]
  	}
  	else{  		
  		//Failed
  		alert("There was a problem loading the settings in getVariableSettings()");
  		return 1;
  	}
  	
  	
  	if(data_var == "temp"){
  		draw_temps=0;
  		if(isChecked('elem',document.ver_graph.maxTemp) == 1){
  			overlay_string+="&max=1";
  			draw_temps++;
  		}
  		if(isChecked('elem',document.ver_graph.minTemp) == 1){
  			overlay_string+="&min=1";
  			draw_temps++;
  		}
  		if(isChecked('elem',document.ver_graph.avgTemp) == 1){
  			overlay_string+="&avg=1";
  			draw_temps++;
  		}

  	}

  	url="/analysis_data.php?beg="+startYmd+"&time="+time_sec+"&units="+units+"&vis="+vis+"&per="+per+"&var="+data_var+"&type=a&calendar="+calendar+"&periods="+periodCount;
  	//url+="&locData="+locationType+"|"+locationID;
  	plannerURL="/ajax/planner_ajax.php?count=1&beg="+startYmd+"&units="+units+"&per="+per+"&tmin=1&tmax=1&tavg=1&prcp=1&tminvsly=0&tmaxvsly=0&tavgvsly=0&loc_1="+locationID+"&type_1="+locationType+"&periods="+periodCount+"&page=console&snow=1&calendar="+calendar;
  }
  else if(currentPage == "holiday"){
  	
  	functionHash="holiday_id";
  	functionResult=getHolidaySettings(functionHash);
  	if(functionResult['valid'] && functionResult['valid'] == 1){
  		holiday_id=functionResult[functionHash];
  	}
  	else{  		
  		//Failed
  		alert("Please choose a valid holiday from the list");
  		return 1;
  	}
  	
  	functionResult=getLocationSettings();
  	if(functionResult['valid_locationID'] && functionResult['valid_locationID'] == 1 && functionResult['valid_locationType'] && functionResult['valid_locationType'] == 1){
  		locationID=functionResult['locationID'];
  		locationType=functionResult['locationType'];
  		locationTypeString=functionResult['locationTypeString'];
  	}
  	else{  		
  		//Failed
  		//REMOVEalert("There was a problem loading the settings in getLocationSettings()");
  		alert("Please choose a valid location from the dropdown boxes");
  		return 1;
  	}
  	
  	functionHash="period";
  	functionResult=getPeriodSettings(functionHash);
  	if(functionResult['valid'] && functionResult['valid'] == 1){
  		period=functionResult[functionHash];
  	}
  	else{  		
  		//Failed
  		alert("There was a problem loading the settings in getPeriodSettings()");
  		return 1;
  	}
  	
  	if(period == "daily"){
  		per="d";
  	}
  	else if(period == "weekly"){
  		per="w";
  	}
  	
  	functionHash="data_var";
  	functionResult=getVariableSettings(functionHash);
  	if(functionResult['valid'] && functionResult['valid'] == 1){
  		data_var=functionResult[functionHash]
  	}
  	else{  		
  		//Failed
  		alert("There was a problem loading the settings in getVariableSettings()");
  		return 1;
  	}
  	
  	functionHash="vis";
  	functionResult=getVisSettings(functionHash);
  	if(functionResult['valid'] && functionResult['valid'] == 1){
  		vis=functionResult[functionHash];
  	}
  	else{  		
  		//Failed
  		alert("There was a problem loading the settings in getVisSettings()");
  		return 1;
  	}
  	vis="all";
  	
  	url="/holiday_data.php?hol="+holiday_id+"&time="+time_sec+"&units="+units+"&vis="+vis+"&per="+per+"&var="+data_var+"&type=a&calendar="+calendar;
  	
  	//END else if(currentPage == "holiday")
  }
  else if(currentPage == "time_period"){
  	
  	functionResult=getLocationSettings();
  	if(functionResult['valid_locationID'] && functionResult['valid_locationID'] == 1 && functionResult['valid_locationType'] && functionResult['valid_locationType'] == 1){
  		locationID=functionResult['locationID'];
  		locationType=functionResult['locationType'];
  		locationTypeString=functionResult['locationTypeString'];
  	}
  	else{  		
  		//Failed
  		//REMOVEalert("There was a problem loading the settings in getLocationSettings()");
  		alert("Please choose a valid location from the dropdown boxes");
  		return 1;
  	}
  	
  	functionHash="beg_mmdd";
  	functionID="beg_mmdd";
  	functionResult=getMMDDSettings(functionHash,functionID);
  	if(functionResult['valid'] && functionResult['valid'] == 1){
  		beg_mmdd=functionResult[functionHash];
  	}
  	else{  		
  		//Failed
  		//REMOVEalert("There was a problem loading the settings in getMMDDSettings()");
  		alert("Please specify a valid start date in MMDD format (ex. 0925)");
  		return 1;
  	}
  	
  	functionHash="end_mmdd";
  	functionID="end_mmdd";
  	functionResult=getMMDDSettings(functionHash,functionID);
  	if(functionResult['valid'] && functionResult['valid'] == 1){
  		end_mmdd=functionResult[functionHash];
  	}
  	else{  		
  		//Failed
  		//REMOVEalert("There was a problem loading the settings in getMMDDSettings()");
  		alert("Please specify a valid end date in MMDD format (ex. 0925)");
  		return 1;
  	}
  	
  	functionHash="end_yyyy";
  	functionID="end_yyyy";
  	functionResult=getYYYYSettings(functionHash,functionID);
  	if(functionResult['valid'] && functionResult['valid'] == 1){
  		end_yyyy=functionResult[functionHash];
  	}
  	else{  		
  		//Failed
  		//REMOVEalert("There was a problem loading the settings in getYYYYSettings()");
  		alert("Please specify a valid year in YYYY format (ex. 2009)");
  		return 1;
  	}
  	
  	
  	draw_temps=0;
  	if(isChecked('elem',document.ver_graph.maxTemp) == 1){
  		overlay_string+="&max=1";
  		draw_temps++;
  	}
  	if(isChecked('elem',document.ver_graph.minTemp) == 1){
  		overlay_string+="&min=1";
  		draw_temps++;
  	}
  	if(isChecked('elem',document.ver_graph.avgTemp) == 1){
  		overlay_string+="&avg=1";
  		draw_temps++;
  	}
  	if(isChecked('elem',document.ver_graph.prcp) == 1){
  		overlay_string+="&prcp=1";
  	}
  	if(isChecked('elem',document.ver_graph.snow) == 1){
  		overlay_string+="&snow=1";
  	}
  	
  	var url="time_period_data.php?beg_mmdd="+beg_mmdd+"&end_mmdd="+end_mmdd+"&time="+time_sec+"&units="+units+"&type=a&calendar="+calendar+"&end_yyyy="+end_yyyy;
  	
  	
  }
  else if(currentPage == "verification"){
  	functionHash="period";
  	functionResult=getPeriodSettings(functionHash);
  	if(functionResult['valid'] && functionResult['valid'] == 1){
  		period=functionResult[functionHash];
  	}
  	else{  		
  		//Failed
  		alert("There was a problem loading the settings in getPeriodSettings()");
  		return 1;
  	}
  	
  	if(period == "daily"){
  		if(isChecked('elem',document.ver_graph.weekend) == 1){
  			overlay_string+="&weekend=1";
  		}
  		per="d";
  	}
  	else if(period == "weekly"){
  		per="w";
  	}
  	
  	
  	functionResult=getLocationSettings();
  	if(functionResult['valid_locationID'] && functionResult['valid_locationID'] == 1 && functionResult['valid_locationType'] && functionResult['valid_locationType'] == 1){
  		locationID=functionResult['locationID'];
  		locationType=functionResult['locationType'];
  		locationTypeString=functionResult['locationTypeString'];
  	}
  	else{  		
  		//Failed
  		//REMOVEalert("There was a problem loading the settings in getLocationSettings()");
  		alert("Please choose a valid location from the dropdown boxes");
  		return 1;
  	}
  	
  	
  	
  	functionHash="vis";
  	functionResult=getVisSettings(functionHash);
  	if(functionResult['valid'] && functionResult['valid'] == 1){
  		vis=functionResult[functionHash];
  	}
  	else{  		
  		//Failed
  		alert("There was a problem loading the settings in getVisSettings()");
  		return 1;
  	}
  	
  	functionHash="startYmd";
  	functionID="start_date";
  	functionResult=getDateSettings(functionHash,functionID);
  	if(functionResult['valid'] && functionResult['valid'] == 1){
  		startYmd=functionResult[functionHash];
  	}
  	else{  		
  		//Failed
  		//REMOVEalert("There was a problem loading the settings in getDateSettings()");
  		alert("Please specify a valid start date in MM/DD/YYYY format (ex. 01/20/2009)");
  		return 1;
  	}
  	
  	functionHash="endYmd";
  	functionID="end_date";
  	functionResult=getDateSettings(functionHash,functionID);
  	if(functionResult['valid'] && functionResult['valid'] == 1){
  		endYmd=functionResult[functionHash];
  	}
  	else{  		
  		//Failed
  		//REMOVEalert("There was a problem loading the settings in getDateSettings()");
  		alert("Please specify a valid end date in MM/DD/YYYY format (ex. 01/20/2009)");
  		return 1;
  	}
  	
  	var day_num=get_date_diff(period,"start_date","end_date");
  	
  	if(day_num && day_num<=max_days && day_num>0){
  		//Not over the limit
  	}
  	else{
  		var period_name="";
  		if(period == "weekly"){
  			period_name="weeks";
  		}
  		else{
  			period_name="days";
  		}
  		alert("Please specify fewer than "+max_days+" "+period_name+" (current selection= "+day_num+" "+period_name+")");
  		
  	}
  	
  	functionHash="data_var";
  	functionResult=getVariableSettings(functionHash);
  	if(functionResult['valid'] && functionResult['valid'] == 1){
  		data_var=functionResult[functionHash]
  	}
  	else{  		
  		//Failed
  		alert("There was a problem loading the settings in getVariableSettings()");
  		return 1;
  	}
  	
  	
  	url="/ver_data.php?beg="+startYmd+"&end="+endYmd+"&units="+units+"&per="+per+"&vis="+vis+"&var="+data_var+"&calendar="+calendar+"&act=1";
  	
  	//END else if(currentPage == "verification")
  }
  else{
  	alert("Invalid page in run_graph");
  	return;
  }
  
  
  /*REMOVE OWN FUNCTION
  if(isChecked('elem',document.ver_graph.units[0]) == 1){
  	units=document.ver_graph.units[0].value;
  }
  else if(isChecked('elem',document.ver_graph.units[1]) == 1){
  	units=document.ver_graph.units[1].value;
  }
  else{
  	alert("Invalid units setting");
  	return;
  }
  */

  
  if(currentPage != "verification"){
  	
  		
  	/*REMOVE OWN FUNCTION
  	var overlay_string="";
  	if(isChecked('elem',document.ver_graph.periods[0]) == 1){
  		periodCount=28;
  		width=721;
  		$("#data_table table").css("margin-left","0px","width","721px");
  		$("#graph").css("margin-left","79px","width","721px");
  	}
  	else if(isChecked('elem',document.ver_graph.periods[1]) == 1){
  		periodCount=35;
  		width=882;
  		$("#data_table table").css("margin-left","1px","width","882px");
  		$("#graph").css("margin-left","0px");
  	}
  	else{
  		alert("Invalid periodCount setting");
  		return;
  	}
  	*/
  	
  	/*REMOVE OWN FUNCTION
  	data_var="temp";
  	if(isChecked('elem',document.ver_graph.data_var) == 1 || isChecked('elem',document.ver_graph.data_var[0]) == 1){
  		data_var="temp";
  	}
  	else if(isChecked('elem',document.ver_graph.data_var[1]) == 1){
  		data_var="prcp";
  	}
  	else{
  		alert("Invalid data_var setting");
  		return;
  	}
  	*/
  	
  	/*REMOVE
  	if(data_var == "temp"){
  		draw_temps=0;
  		if(isChecked('elem',document.ver_graph.maxTemp) == 1){
  			overlay_string=overlay_string+"&max=1";
  			draw_temps++;
  		}
  		if(isChecked('elem',document.ver_graph.minTemp) == 1){
  			overlay_string=overlay_string+"&min=1";
  			draw_temps++;
  		}
  		if(isChecked('elem',document.ver_graph.avgTemp) == 1){
  			overlay_string=overlay_string+"&avg=1";
  			draw_temps++;
  		}
  	}
  	
  	
  	if(period == "daily"){
  		if(isChecked('elem',document.ver_graph.weekend) == 1){
  			overlay_string=overlay_string+"&weekend=1";
  		}
  	}
  	
  	if(currentPage == "console"){
  		url="/analysis_data.php?beg="+startYmd+"&id="+locationID+"&time="+time_sec+"&units="+units+"&vis="+vis+"&per="+per+"&var="+data_var+"&type=a&calendar="+calendar+"&periods="+periodCount;
  		plannerURL="/ajax/planner_ajax.php?count=1&beg="+startYmd+"&units="+units+"&per="+per+"&tmin=1&tmax=1&tavg=1&prcp=1&tminvsly=0&tmaxvsly=0&tavgvsly=0&loc_1="+locationID+"&type_1="+locationType+"&periods="+periodCount+"&page=console&snow=1&calendar="+calendar;
  	}
  	*/
  }
  else{
  	
  	/*REMOVE OWN FUNCTION
  	//Get the end date
   	if(document.getElementById('end_date') && document.getElementById('end_date').value){
  		endDate=document.getElementById('end_date').value;
  		endYmd=dateConvertm_d_Y_to_Ymd(endDate);
  		if(endYmd && endYmd.length == 8){
  			//Good to go
  		}
  		else{
  			//Must be an issue w/the date
  			return;
  		}
  	}
  	else{
  		//Start date does not appear to be set
  		alert("end_date does not appear to be set");
  		return;
  	}
  	*/
  	
  	
  }
  
  url+="&h="+height+"&w="+width+"&time="+time_sec;
  
  if(locationType == "station"){
  	url+="&id="+locationID;
  	if(locationTypeString == "custom_location"){
  		url+="&locationTypeString="+locationTypeString;
  	}
  }
  else if(locationType == "region"){
  	url+="&r="+locationID;
  }
  else if(locationType == "custom_region"){
  	url+="&r="+locationID+"&cr=1";
  }
  
  if(overlay_string.length >= 1){
    url+=overlay_string;
  }
  
  if(flash == 1){
  	var flash_chart="";
  	if(draw_temps>=1){
      flash_chart="/ScrollCombiDY2D.swf";
    }else{
      flash_chart="/ScrollCombi2D.swf";
    }
    if(currentPage === "time_period"){
    	flash_chart="MSCombiDY2D.swf";
    }
    var flashGraphID=id+"_flash";
    var chart1 = new FusionCharts(flash_chart, flashGraphID, width, height, "0", "1");
    
    url+="&xml=1";
    if(currentPage === "console"){
    	planner_url=url;
    }
    else if(currentPage == "verification"){
  		statsURL=url+"&stat=1";
  	}
    url=escape(url);
    
    chart1.setDataURL(url);
    chart1.addParam("WMode", "Transparent");
    chart1.render(id);
  }
  else{
  	if(currentPage == "console"){
  		url+="&alt=1";
  		showLoading(id,"Please wait while the graph is being generated");
  		loadGraphURL(id,url);
  	}
  	else if(currentPage == "holiday"){
  		showLoading(id,"Please wait while the graph is being generated");
  		$('#'+id).html("<img src=\""+ url +"\">");
  	}
  	else if(currentPage == "time_period"){
  		showLoading(id,"Please wait while the graph is being generated");
  		$('#'+id).html("<img src=\""+ url +"\">");
  	}
  	else if(currentPage == "verification"){
  		showLoading(id,"Please wait while the graph is being generated");
  		url+="&alt=1";
  		statsURL=url+"&stat=1";
  		loadGraphURL(id,url);
  	}
  	
  } 
  
  return;
}

function changeGraphIncrement(incr){
	//Make sure a value was passed
	if(incr && incr.length >= 1){
		//It was
	}
	else{
		//Invalid incr supplied
		alert("Invalid increment supplied");
		return;
	}
	
	//Make sure the incr is valid
	if(incr == "daily" || incr == "weekly" || incr == "monthly"){
		//Valid
		period=incr;
	}
	else{
		//Invalid
		alert("Invalid value supplied for incr ("+incr+") in changeGraphIncrement");
		return;
	}
	
	//Take the necessary steps for each page
	if(currentPage === "console"){
		if(incr === "daily"){
			$('#graph_var').html("<input type='radio' name='data_var' value='temp' checked>Temp&nbsp;&nbsp;&nbsp;");
			//GOODmatt_chg_daily();
			calendarUnrestrictSelectDay();
			changeElementDisplay('weekend_span','');
		}
		else if(incr === "weekly"){
			//REMOVE$('#graph_var').html("<input type='radio' name='data_var' value='temp' onclick=\"show_overlay_span(1);\" checked>Temp&nbsp;&nbsp;&nbsp;<input type='radio' name='data_var' value='prcp' onclick=\"show_overlay_span(0);\">Precip");
			$('#graph_var').html("<input type='radio' name='data_var' value='temp' onclick=\"changeGraphVariableOverlays('temp');\" checked>Temp&nbsp;&nbsp;&nbsp;<input type='radio' name='data_var' value='prcp' onclick=\"changeGraphVariableOverlays('prcp');\">Precip");
			//GOODmatt_chg_weekly("sunday");
			calendarRestrictSelectDay('sunday');
			chk_day_of_week("start_date");
			changeElementDisplay('weekend_span','none');
		}
	}
	else if(currentPage === "holiday"){
		if(incr === "daily"){
			
		}
		else if(incr === "weekly"){
			$('#graph_var').html("<input type='radio' name='data_var' value='temp' onclick=\"changeGraphVariableOverlays('temp');\" checked>Temp&nbsp;&nbsp;&nbsp;<input type='radio' name='data_var' value='prcp' onclick=\"changeGraphVariableOverlays('prcp');\">Precip");
		}
	}
	else if(currentPage === "energy_planner"){
		if(incr === "daily"){
			calendarUnrestrictSelectDay();
		}
		else if(incr === "weekly"){
			calendarRestrictSelectDay('sunday');
			chk_day_of_week("start_date");
		}
		else if(incr === "monthly"){
			calendarRestrictSelectDay('sunday');
		}
	}
	else if(currentPage === "planner"){
		if(incr === "daily"){
			calendarUnrestrictSelectDay();
		}
		else if(incr === "weekly"){
			calendarRestrictSelectDay('sunday');
			chk_day_of_week("start_date");
		}
		else if(incr === "monthly"){
			calendarRestrictSelectDay('sunday');
		}
	}
	else if(currentPage === "verification"){
		if(incr === "daily"){
			$('#graph_var').html("<input type='radio' name='data_var' value='temp' checked>Temp&nbsp;&nbsp;&nbsp;");
			//GOODmatt_chg_daily();
			calendarUnrestrictSelectDay();
		}
		else if(incr === "weekly"){
			//REMOVE$('#graph_var').html("<input type='radio' name='data_var' value='temp' onclick=\"show_overlay_span(1);\" checked>Temp&nbsp;&nbsp;&nbsp;<input type='radio' name='data_var' value='prcp' onclick=\"show_overlay_span(0);\">Precip");
			$('#graph_var').html("<input type='radio' name='data_var' value='temp' onclick=\"changeGraphVariableOverlays('temp');\" checked>Temp&nbsp;&nbsp;&nbsp;<input type='radio' name='data_var' value='prcp' onclick=\"changeGraphVariableOverlays('prcp');\">Precip");
			//GOODmatt_chg_weekly("sunday");
			calendarRestrictSelectDay('sunday');
			chk_day_of_week("start_date");
			chk_day_of_week("end_date");
		}
	}
	else{
		alert("Invalid currentPage ("+currentPage+") in changeGraphIncrement()");
	}
		
	return;
}

function changeGraphType(type){
	
	if(type && type.length >= 1){
		//Defined
		
		if(type == "jpeg"){
			graphType=type;
			changeElementDisplay("graph_span","none");
		}
		else if(type == "flash"){
			graphType=type;
			changeElementDisplay("graph_span","");
		}
		
	}
	else{
		alert("No type defined in call to changeGraphType");
		return;
	}
	
	return;
}

function changeGraphVariableOverlays(variable){
	if(variable && variable.length >= 1){
		//Exists but is it valid
		if(variable === "prcp"){
			if(currentPage === "console"){
				$('#temp_overlay_span').html("");
			}
			else if(currentPage === "holiday"){
				$('#temp_overlay_span').html("");
			}
			else if(currentPage === "verification" || currentPage === "time_period"){
				return;
			}
			else{
				alert("Invalid currentPage ("+currentPage+") in changeGraphVariableOverlays prcp section");
				return;
			}
		}
		else if(variable === "temp"){
			if(currentPage === "console"){
				$('#temp_overlay_span').html("<input type='checkbox' id='maxTemp'>Max Temp <br /><input type='checkbox' id='minTemp'>Min Temp <br /><input type='checkbox' id='avgTemp'>Avg Temp <br />");
			}
			else if(currentPage === "holiday"){
				$('#temp_overlay_span').html("<input type='checkbox' id='maxTemp' disabled checked>Max Temp <br /><input type='checkbox' id='minTemp' disabled checked>Min Temp <br /><input type='checkbox' id='avgTemp' disabled checked>Avg Temp <br />");
			}
			else if(currentPage === "verification" || currentPage === "time_period"){
				return;
			}
			else{
				alert("Invalid currentPage ("+currentPage+") in changeGraphVariableOverlays temp section");
			}
		}
		else{
			//Invalid
			alert("Invalid variable ("+variable+") passed to changeGraphVariableOverlays");
			return;
		}
	}
	else{
		//Invalid
		alert("Invalid or undefined variable passed to changeGraphVariableOverlays");
		return;
	}
	return;
}

function loadGraphURL(graphID,graphURL){
	var statusFlag=0;
	$.ajax({
		type: "GET",
		url: graphURL,
		dataType : "html",
		success: function(html) {
			//Make sure it exists
			if(document.getElementById(graphID)){
				//Exists
				statusFlag=1;
				document.getElementById(graphID).innerHTML=html;
				if(currentPage === "console"){
					replaceDynamicContent(plannerID,plannerURL);
				}
				else if(currentPage === "verification"){
					parseVerificationStats(statsID,statsURL);
				}				
				
			}
			else{
				//Does not exist
				alert("graphID ("+graphID+") does not exist");
				statusFlag=0;
			}
		},
		error: function(html) {
			statusFlag=0;
		}
	}); //END $.ajax
	
	return statusFlag;
}

function replaceDynamicContent(contentID,contentURL){
	
	var postContent="";
	//Make sure the ID is set
	if(contentID && contentID.length >= 1){
		//Good to go
	}
	else{
		//No ID set
		alert("No ID set in call to replaceDynamicContent()");
		return;
	}
	
	//Make sure the URL is set
	if(contentURL && contentURL.length >= 1){
		//Good to go
	}
	else{
		//No ID set
		alert("No URL set in call to replaceDynamicContent()");
		return;
	}
	
	$('#'+contentID).load(contentURL);

	return;
}

function FC_Rendered(){
	if(currentPage === "console"){
		replaceDynamicContent(plannerID,plannerURL);
	}
	else if(currentPage === "verification"){
		parseVerificationStats(statsID,statsURL);
	}
	return;
}

function getCalendarSettings(hash){
	
	var functionResult=new Object;
	functionResult['valid']=0;
	functionResult[hash]=0;
	
	if(isChecked('elem',document.ver_graph.calendar[0]) == 1){
  	functionResult['valid']=1;
		functionResult[hash]="julian";
  }
  else if(isChecked('elem',document.ver_graph.calendar[1]) == 1){
  	functionResult['valid']=1;
  	functionResult[hash]="retail";
  }
  else{
  	
  }
  return functionResult;
	
}

function getDateSettings(hash,id){
	
	var functionResult=new Object;
	functionResult['valid']=0;
	functionResult[hash]="";
	
	var datem_d_Y="";
	var dateYmd="";
	
	if(id === "start_date"){
		//Need start
	}
	else if(id === "end_date"){
		//Need end
	}
	else{
		//Invalid
		alert("Invalid id given in getDateSettings ("+id+")");
		return functionResult;
	}
	
	if(document.getElementById(id) && document.getElementById(id).value){
		datem_d_Y=document.getElementById(id).value;
		dateYmd=dateConvertm_d_Y_to_Ymd(datem_d_Y);
		if(dateYmd && dateYmd.length == 8){
			//Good to go
			functionResult['valid']=1;
			functionResult[hash]=dateYmd;
		}
		else{
			//Must be an issue w/the date
		}
	}
	
  
	return functionResult;
}


function getFlashSettings(hash){
	
	var functionResult=new Object;
	functionResult['valid']=0;
	functionResult[hash]=0;
	
  if(isChecked('elem',document.ver_graph.graph_type[0]) == 1){
  	functionResult['valid']=1;
  	functionResult[hash]=0;  	
  }
  else if(isChecked('elem',document.ver_graph.graph_type[1]) == 1){
  	functionResult['valid']=1;
  	functionResult[hash]=1;
  	
  }
  else{

  }
	return functionResult;
}

function getHolidaySettings(hash){
	
	var functionResult=new Object;
	functionResult['valid']=0;
	functionResult[hash]=0;
	
	if(document.getElementById('holiday') && document.getElementById('holiday').value){
		var holidayID=document.getElementById('holiday').value;
		//Make sure it is a valid id
		if(holidayID >= 1){
			functionResult['valid']=1;
			functionResult[hash]=holidayID;
		}
	}

  return functionResult;
	
}

function getLocationSettings(){
	
	var functionResult=new Object;
	functionResult['valid_locationID']=0;
	functionResult['valid_locationType']=0;
	
	functionResult['locationID']="";
	functionResult['locationType']="";
	functionResult['locationTypeString']="";
	
	if(isChecked('elem',document.ver_graph.use_reg[0]) == 1){
  	functionResult['valid_locationType']=1;
		functionResult['locationType']="station";
  	
  }
  else if(isChecked('elem',document.ver_graph.use_reg[1]) == 1){
  	functionResult['valid_locationType']=1;
  	functionResult['locationType']="region";
  	
  }
  else if(isChecked('elem',document.ver_graph.use_reg[2]) == 1){
  	functionResult['valid_locationType']=1;
  	functionResult['locationType']="custom_region";
  	
  }
  else{
  	alert("Invalid locationType setting2");
  	return functionResult;
  }
  
  var locationElemID=functionResult['locationType']+"_location";
  
  var locationKey = "";
  var locationID = 0;
	
  if(document.getElementById(locationElemID) && document.getElementById(locationElemID).value){
  	
  	locationValueString=document.getElementById(locationElemID).value;
  	locationArray=locationValueString.split("|");

  	//Did we find a key?
  	if(locationArray[0] && locationArray[0].length >= 1){
  		//Yes
  		locationKey=locationArray[0];
  		locationID=locationArray[1];
  	}
  	else{
  		//No
  		alert("Invalid key in this string ("+locationValueString+")");
  		return;
  	}
  	if(locationKey.length >= 1){
  		if(locationKey === "custom_location"){
  			functionResult['locationTypeString']="custom_location";
  		}
  		else if(locationKey === "custom_region"){
  			functionResult['locationTypeString']="custom_region";
  		}
  		else if(locationKey === "region"){
  			functionResult['locationTypeString']="region";
  		}
  		else if(locationKey === "station"){
  			functionResult['locationTypeString']="station";
  		}
  		else{
  			alert("Invalid locationValue locationKey="+locationKey+" locationID="+locationID);
  			//return;
  		}
  	}
  	
  	if(locationID >= 1){
  		//Valid locationID
  		functionResult['valid_locationID']=1;
  		functionResult['locationID']=locationID;
  	}
  	else{
  		//Invalid locationID
  		alert("Invalid locationID ("+functionResult['locationID']+") specified");
  		//return;
  	}
  }
	return functionResult;
}

function getMMDDSettings(hash,id){
	
	var functionResult=new Object;
	functionResult['valid']=0;
	functionResult[hash]="";
	
	var datemmdd="";
	
	if(id === "beg_mmdd"){
		//Need start
	}
	else if(id === "end_mmdd"){
		//Need end
	}
	else{
		//Invalid
		alert("Invalid id given in getmmddSettings ("+hash+","+id+")");
		return functionResult;
	}
	
	if(document.getElementById(id) && document.getElementById(id).value){
		//REMOVEdatemmdd=check_mmdd(document.getElementById(id));
		datemmdd=check_mmdd(id);
		if(datemmdd){
			functionResult['valid']=1;
			functionResult[hash]=datemmdd;
		}
	}
	
  return functionResult;	
}



function getPeriodSettings(hash){
	var functionResult=new Object;
	functionResult['valid']=0;
	functionResult[hash]="";
	
	if(isChecked('elem',document.ver_graph.increment[0]) == 1){
  	functionResult['valid']=1;
		functionResult[hash]="daily";
  }
  else if(isChecked('elem',document.ver_graph.increment[1]) == 1){
  	functionResult['valid']=1;
  	functionResult[hash]="weekly";
  }
  else{
  	
  }
  return functionResult;	
}

function getPeriodCountSettings(hash){
	var functionResult=new Object;
	functionResult['valid']=0;
	functionResult[hash]="";
	
	if(isChecked('elem',document.ver_graph.periods[0]) == 1){
		functionResult['valid']=1;
		functionResult[hash]=28;
		/*REMOVE
		width=721;
		$("#data_table table").css("margin-left","0px","width","721px");
		$("#graph").css("margin-left","79px","width","721px");
		*/
	}
	else if(isChecked('elem',document.ver_graph.periods[1]) == 1){
		functionResult['valid']=1;
		functionResult[hash]=35;
		/*REMOVE
		width=882;
		$("#data_table table").css("margin-left","1px","width","882px");
		$("#graph").css("margin-left","0px");
		*/
	}
	else{
		/*REMOVE
		alert("Invalid periodCount setting");
		return;
		*/
	}

  return functionResult;	
}

function getUnitSettings(hash){
	
	var functionResult=new Object;
	functionResult['valid']=0;
	functionResult[hash]=0;
	
	if(isChecked('elem',document.ver_graph.units[0]) == 1){
  	functionResult['valid']=1;
		functionResult[hash]=document.ver_graph.units[0].value;
  }
  else if(isChecked('elem',document.ver_graph.units[1]) == 1){
  	functionResult['valid']=1;
  	functionResult[hash]=document.ver_graph.units[1].value;
  }
  else{
  	
  }

	return functionResult;
}

function getVariableSettings(hash){
	
	var functionResult=new Object;
	functionResult['valid']=0;
	functionResult[hash]="";
	
	if(isChecked('elem',document.ver_graph.data_var) == 1 || isChecked('elem',document.ver_graph.data_var[0]) == 1){
		functionResult['valid']=1;
		functionResult[hash]="temp";
	}
	else if(isChecked('elem',document.ver_graph.data_var[1]) == 1){
		functionResult['valid']=1;
		functionResult[hash]="prcp";
	}
	else{
		
	}
	
	return functionResult;
}

function getVisSettings(hash){
	
	var functionResult=new Object;
	functionResult['valid']=0;
	functionResult[hash]=0;
	
	//REMOVE
	/*	
	if(isChecked('elem',document.ver_graph.calendar[0]) == 1){
  	functionResult['valid']=1;
		functionResult[hash]="all";
  }
  else{
  	functionResult['valid']=1;
  	functionResult[hash]=35;
  }
  */
	if(document.ver_graph.graph_span && isChecked('elem',document.ver_graph.graph_span[0]) == 1){
  	functionResult['valid']=1;
		functionResult[hash]=35;
  }
  else{
  	functionResult['valid']=1;
  	functionResult[hash]="all";
  }
  
	return functionResult;
}

function getYYYYSettings(hash,id){
	
	var functionResult=new Object;
	functionResult['valid']=0;
	functionResult[hash]="";
	
	var dateyyyy="";
	
	if(id === "end_yyyy"){
		//Need end
	}
	else{
		//Invalid
		alert("Invalid id given in getYYYYSettings ("+hash+","+id+")");
		return functionResult;
	}
	
	if(document.getElementById(id) && document.getElementById(id).value){
		//REMOVEdateyyyy=check_yyyy(document.getElementById(id));
		dateyyyy=check_yyyy(id);
		if(dateyyyy){
			functionResult['valid']=1;
			functionResult[hash]=dateyyyy;
		}
	}
	
  return functionResult;	
}


function draw_ver_graph(beg,end,id,units){
  var time=new Date();
  var time_sec=time.getTime();
  var width=650;
//  var height=570;
//  var height=520;
  var width=673;
  width=683;
  width=701;
  width=711;
  width=721;
  //width=882;
  if(need_act==1){
//  width=711;
//    width=697;
  }

  var use_reg=0;
  var reg=0;
  var use_custom_reg=0;
  var custom_reg=0;

update_tool_links(id,beg,end);


  var height=490;

  document.getElementById('graph').innerHTML = "\
    <center>\
    <img src=/images/loading.gif><br />Please wait while the graph is being generated\
    </center>\
  ";


  if(document.ver_graph.graph_type[0].checked){
    flash=0;
  }else if(document.ver_graph.graph_type[1].checked){
    flash=1;
  }else{
    alert("THERE IS A PROBLEM WITH FLASH");
  }

  if(document.ver_graph.increment[0].checked){
    var per="d";
    show_export_button(1);
  }else if(document.ver_graph.increment[1].checked){
    var per="w";
    show_export_button(0);
  }else{
    alert("THERE IS A PROBLEM WITH INCREMENT");
  }
  
  if(need_act != 1){
  	if(document.ver_graph.periods[0] && document.ver_graph.periods[0].checked){
  		periods=28;
  		width=721;
  		$("#data_table table").css("margin-left","0px","width","721px");
  		$("#graph").css("margin-left","79px","width","721px");
  	}else if(document.ver_graph.periods[1] && document.ver_graph.periods[1].checked){
  		periods=35;
  		width=882;
  		$("#data_table table").css("margin-left","1px","width","882px");
  		$("#graph").css("margin-left","0px");
  	}else{
  		alert("THERE IS A PROBLEM WITH THE PERIOD");
  	}
  }
  
  

  if(document.ver_graph.data_var.checked){
      var data_var="temp";
  }else{
    if(document.ver_graph.data_var[0].checked){
      var data_var="temp";
    }else if(document.ver_graph.data_var[1].checked){
      var data_var="prcp";
    }else{
      alert("THERE IS A PROBLEM WITH DATA VAR");
    }
  }

  if(document.ver_graph.use_reg[0].checked){
    var use_reg=0;
    location_type="station";
  }else if(document.ver_graph.use_reg[1].checked){
    var use_reg=1;
    var reg=document.getElementById('reg_loc').value;
    var use_custom_reg=0;
    location_type="region";
  }else if(document.ver_graph.use_reg[2].checked){
    var use_reg=1;
    var use_custom_reg=1;
    var custom_reg=document.getElementById('client_reg_loc').value;
    location_type="client_region";
    location_type="custom_region";
  }else{
    alert("THERE IS A PROBLEM WITH REGION");
  }

  if(need_act==0){
    //CHECK OVERLAYS
    var draw_temps=0;
    var overlay_string="";
    if(data_var=="temp"){
      if(document.ver_graph.maxTemp.checked){
        overlay_string=overlay_string+"&max=1";
        draw_temps++;
      }
      if(document.ver_graph.minTemp.checked){
        overlay_string=overlay_string+"&min=1";
        draw_temps++;
      }
      if(document.ver_graph.avgTemp.checked){
        overlay_string=overlay_string+"&avg=1";
        draw_temps++;
      }
    }
    if(per=="d"){
      if(document.ver_graph.weekend.checked){
        overlay_string=overlay_string+"&weekend=1";
      }
    }
    
  }
  if(document.ver_graph.calendar){
  	if(document.ver_graph.calendar[0].checked){
  		calendar="julian";
  	}else if(document.ver_graph.calendar[1].checked){
  		calendar="retail";
  	}
  }

  if(document.ver_graph.graph_span[1].checked){
    var vis="all";
  }else{
    var vis=35;
  }

  if(need_act==0){
    var url="analysis_data.php?beg="+beg+"&end="+end+"&id="+id+"&time="+time_sec+"&units="+units+"&vis="+vis+"&per="+per+"&var="+data_var+"&type=a&calendar="+calendar+"&periods="+periods;
  }else{
    var url="ver_data.php?beg="+beg+"&end="+end+"&id="+id+"&time="+time_sec+"&units="+units+"&vis="+vis+"&per="+per+"&var="+data_var+"&calendar="+calendar;
  }
  stat_url="ver_data.php?beg="+beg+"&end="+end+"&id="+id+"&units="+units+"&per="+per+"&var="+data_var+"&stat=1";

  if(overlay_string){
    url=url+overlay_string;
  }
  if(need_act==1){
    url=url+"&act=1";
  }else{
  }

  if(reg>=1){
    stat_url=stat_url+"&r="+reg;
  }else if(custom_reg>=1){
    stat_url=stat_url+"&cr=1&r="+custom_reg;
  }

  if(flash==1){
    var debug=0;

    if(draw_temps>=1){
      var flash_chart="ScrollCombiDY2D.swf";
    }else{
      var flash_chart="ScrollCombi2D.swf";
    }
//NO DEBUG    var chart1 = new FusionCharts(flash_chart", "sampleChart", width, height, "0", "1");
//YES DEBUG
    var chart1 = new FusionCharts(flash_chart, "sampleChart", width, height, debug, "1");
    if(reg>=1){
      url=url+"&r="+reg;
      var reg_url=url;
      id=reg;
    }else if(custom_reg>=1){
      url=url+"&r="+custom_reg+"&cr=1";
      var reg_url=url;
      id=custom_reg;
    }else{
      var reg_url=url;

    }
    url=url+"&xml=1";
    var url2=url;

    planner_url=reg_url;
    
    url=escape(url);



    chart1.setDataURL(url);
    chart1.addParam("WMode", "Transparent");
    chart1.render("graph");
  }else{
    url=url+"&w="+width+"&h="+height;
    if(reg>=1){
      url=url+"&r="+reg;
      id=reg;
    }else if(custom_reg>=1){
      url=url+"&r="+custom_reg+"&cr=1";
      id=custom_reg;
    }else{
    }
    var reg_url=url;


    if(need_act!=1){
      use_alt_img(url);
    }else{
      document.getElementById('graph').innerHTML = "<img src="+ url +">";
    }
  }
  if(need_act==1 && flash!=1){
    get_ver_stats(stat_url);
  }

  reg_url=reg_url+"&table=1&max=1&min=1&avg=1&prcp=1";
  if(need_act!=1){
  	var new_reg_url="/ajax/planner_ajax.php?count=1&beg="+beg+"&units="+units+"&per="+per+"&tmin=1&tmax=1&tavg=1&prcp=1&tminvsly=0&tmaxvsly=0&tavgvsly=0&loc_1="+id+"&type_1="+location_type+"&periods="+periods;
  	new_reg_url+="&page=console&snow=1&calendar="+calendar;
  	reg_url=new_reg_url;
  	planner_url=reg_url;

  }

  var data="";
  if(need_act==1){
    var page="verification";
  }else{
    var page="console";
  }

  if(flash==1){
    var graph_type="flash";
  }else{
    var graph_type="jpeg";
  }
  var userid="";

  if(use_custom_reg==1){
    enter_log(userid,page,data,beg,end,id,custom_reg,use_custom_reg,per,graph_type,units);
  }else{
    enter_log(userid,page,data,beg,end,id,reg,use_custom_reg,per,graph_type,units);
  }


  http3.open("GET", url3 + id, true);
  http3.onreadystatechange = handleHttpResponse3;
  http3.send(null);
  

                

}


/*FUNCTIONS BELOW NEED TO BE RE-WORKED*/

function matt_chg_daily(){





	YAHOO.example.calendar.start_cal = new YAHOO.widget.Calendar("start_cal","start_date_Container",{ title:"Start date:", close:true ,
	//                                                                      mindate:"1/1/2006",
	//                                                                      maxdate:"5/3/2008"
	maxdate:"11/1/2008"
	});



	YAHOO.example.calendar.start_cal.selectEvent.subscribe(handleSelect, YAHOO.example.calendar.start_cal, true);



	YAHOO.example.calendar.start_cal.render();

	//                                        YAHOO.example.calendar.start_cal.renderCellDefault(1, YAHOO.example.calendar.start_cal.renderOutOfBoundsDate);
	//                                YAHOO.example.calendar.start_cal.render();



	if(need_act==1){

		YAHOO.example.calendar.end_cal = new YAHOO.widget.Calendar("end_cal","end_date_Container",{ title:"End date:", close:true ,
		//                                                                      mindate:"1/1/2006",
		//                                                                      maxdate:"5/3/2008"
		maxdate:"11/1/2008"
		});



		YAHOO.example.calendar.end_cal.selectEvent.subscribe(handleSelect_end, YAHOO.example.calendar.end_cal, true);



		//                                        YAHOO.example.calendar.end_cal.renderCellDefault(1, YAHOO.example.calendar.end_cal.renderOutOfBoundsDate);
		YAHOO.example.calendar.end_cal.render();
	}
	YAHOO.util.Event.addListener("update", "click", updateCal);
	YAHOO.util.Event.addListener("ver_graph", "submit", handleSubmit);
	YAHOO.util.Event.addListener("show_start", "click", YAHOO.example.calendar.start_cal.show, YAHOO.example.calendar.start_cal, true);
	if(need_act==1){
		YAHOO.util.Event.addListener("show_end", "click", YAHOO.example.calendar.end_cal.show, YAHOO.example.calendar.end_cal, true);
	}
	YAHOO.util.Event.addListener(window, "load", init);
}


function matt_chg_weekly(day){

  var m_date_regexp=/^\d\d\/\d\d\/\d\d\d\d/;


  if(day=="sunday"){
    if(m_date_regexp.test(document.getElementById("start_date").value)){
      chk_day_of_week("start_date");
    }
 

    YAHOO.example.calendar.start_cal.addWeekdayRenderer(2, YAHOO.example.calendar.start_cal.renderOutOfBoundsDate);
    YAHOO.example.calendar.start_cal.addWeekdayRenderer(3, YAHOO.example.calendar.start_cal.renderOutOfBoundsDate);
    YAHOO.example.calendar.start_cal.addWeekdayRenderer(4, YAHOO.example.calendar.start_cal.renderOutOfBoundsDate);
    YAHOO.example.calendar.start_cal.addWeekdayRenderer(5, YAHOO.example.calendar.start_cal.renderOutOfBoundsDate);
    YAHOO.example.calendar.start_cal.addWeekdayRenderer(6, YAHOO.example.calendar.start_cal.renderOutOfBoundsDate);
    YAHOO.example.calendar.start_cal.addWeekdayRenderer(7, YAHOO.example.calendar.start_cal.renderOutOfBoundsDate);

    YAHOO.example.calendar.start_cal.selectEvent.subscribe(handleSelect, YAHOO.example.calendar.start_cal, true);

    YAHOO.example.calendar.start_cal.render();
    
    if(need_act == 1){
    	if(m_date_regexp.test(document.getElementById("end_date").value)){
    		chk_day_of_week("end_date");
    	}


    	YAHOO.example.calendar.end_cal.addWeekdayRenderer(2, YAHOO.example.calendar.end_cal.renderOutOfBoundsDate);
    	YAHOO.example.calendar.end_cal.addWeekdayRenderer(3, YAHOO.example.calendar.end_cal.renderOutOfBoundsDate);
    	YAHOO.example.calendar.end_cal.addWeekdayRenderer(4, YAHOO.example.calendar.end_cal.renderOutOfBoundsDate);
    	YAHOO.example.calendar.end_cal.addWeekdayRenderer(5, YAHOO.example.calendar.end_cal.renderOutOfBoundsDate);
    	YAHOO.example.calendar.end_cal.addWeekdayRenderer(6, YAHOO.example.calendar.end_cal.renderOutOfBoundsDate);
    	YAHOO.example.calendar.end_cal.addWeekdayRenderer(7, YAHOO.example.calendar.end_cal.renderOutOfBoundsDate);

    	YAHOO.example.calendar.end_cal.selectEvent.subscribe(handleSelect_end, YAHOO.example.calendar.end_cal, true);

    	YAHOO.example.calendar.end_cal.render();
    }

    YAHOO.util.Event.addListener("update", "click", updateCal);
    YAHOO.util.Event.addListener("ver_graph", "submit", handleSubmit);
    YAHOO.util.Event.addListener(window, "load", init);

  }else{


    if(m_date_regexp.test(document.getElementById("start_date").value)){
      chk_day_of_week("start_date");
    } 


    if(m_date_regexp.test(document.getElementById("end_date").value)){
    	chk_day_of_week("end_date");
    	if(document.getElementById("end_date").value==document.getElementById("start_date").value){
    		//alert("SAME DAY");
    		subtract_week("start_date");
    		//alert("SAME DAY");
    	}
    }


    YAHOO.example.calendar.start_cal.addWeekdayRenderer(1, YAHOO.example.calendar.start_cal.renderOutOfBoundsDate);
    YAHOO.example.calendar.start_cal.addWeekdayRenderer(2, YAHOO.example.calendar.start_cal.renderOutOfBoundsDate);
    YAHOO.example.calendar.start_cal.addWeekdayRenderer(3, YAHOO.example.calendar.start_cal.renderOutOfBoundsDate);
    YAHOO.example.calendar.start_cal.addWeekdayRenderer(4, YAHOO.example.calendar.start_cal.renderOutOfBoundsDate);
    YAHOO.example.calendar.start_cal.addWeekdayRenderer(5, YAHOO.example.calendar.start_cal.renderOutOfBoundsDate);
    YAHOO.example.calendar.start_cal.addWeekdayRenderer(6, YAHOO.example.calendar.start_cal.renderOutOfBoundsDate);
 
    YAHOO.example.calendar.end_cal.addWeekdayRenderer(1, YAHOO.example.calendar.end_cal.renderOutOfBoundsDate);
    YAHOO.example.calendar.end_cal.addWeekdayRenderer(2, YAHOO.example.calendar.end_cal.renderOutOfBoundsDate);
    YAHOO.example.calendar.end_cal.addWeekdayRenderer(3, YAHOO.example.calendar.end_cal.renderOutOfBoundsDate);
    YAHOO.example.calendar.end_cal.addWeekdayRenderer(4, YAHOO.example.calendar.end_cal.renderOutOfBoundsDate);
    YAHOO.example.calendar.end_cal.addWeekdayRenderer(5, YAHOO.example.calendar.end_cal.renderOutOfBoundsDate);
    YAHOO.example.calendar.end_cal.addWeekdayRenderer(6, YAHOO.example.calendar.end_cal.renderOutOfBoundsDate);
 
 
 
    YAHOO.example.calendar.start_cal.selectEvent.subscribe(handleSelect, YAHOO.example.calendar.start_cal, true);
    YAHOO.example.calendar.end_cal.selectEvent.subscribe(handleSelect_end, YAHOO.example.calendar.end_cal, true);

    YAHOO.example.calendar.start_cal.render();
    YAHOO.example.calendar.end_cal.render();

    YAHOO.util.Event.addListener("update", "click", updateCal);
    YAHOO.util.Event.addListener("ver_graph", "submit", handleSubmit);
    YAHOO.util.Event.addListener(window, "load", init);

  }

}

function chk_day_of_week(id){
	if(checkValidDate(document.getElementById(id).value) != 1){
		//Not valid
		return;
	}

  if(period === "weekly"){
    id_date=document.getElementById(id).value;

    cur_date=new Date();
    cur_date.setFullYear(id_date.substring(6,10),id_date.substring(0,2)-1,id_date.substring(3,5));
    cur_day=cur_date.getDay();
    var new_date=new Date();
    
    if(cur_day == 0){
    	//We have already picked the start of the week
    	new_date.setTime(cur_date.getTime());
    }
    else{
    	//We have picked a day which is not the first in the week so we need to alter it.
    	new_date.setTime(cur_date.getTime()-(cur_day*one_day_milliseconds));
    }

    var new_cur_mm=new_date.getMonth()+1;
    var new_cur_dd=new_date.getDate();
    var new_cur_yyyy=new_date.getFullYear();
    if(new_cur_mm<10){
      new_cur_mm="0"+new_cur_mm;
    }
    if(new_cur_dd<10){
      new_cur_dd="0"+new_cur_dd;
    }
  
    var new_cur_date=new_cur_mm+"/"+new_cur_dd+"/"+new_cur_yyyy;
    
    document.getElementById(id).value=new_cur_date;
  
  }
}

function update_data_table_legend(){

  if(document.ver_graph.units[0].checked){
    document.getElementById('table_settings').innerHTML="\
      <table align=center border=0 width='100%' style=\"font-size:10px;\">\
        <tr>\
          <th colspan=10 align=left>\
            Data Table Legend (Versus Last Year):\
          </th>\
        </tr>\
        <tr>\
          <td class='temp_much_colder' width='22px'>&nbsp;</td>\
          <td>Much Colder <=-5&deg;</td>\
          <td class='temp_colder' width='22px'>&nbsp;</td>\
          <td>Colder -1&deg; to -5&deg;</td>\
          <td class='temp_similar' width='22px'>&nbsp;</td>\
          <td>Similar -1&deg; to +1&deg;</td>\
          <td class='temp_warmer' width='22px'>&nbsp;</td>\
          <td>Warmer +1&deg; to +5&deg;</td>\
          <td class='temp_much_warmer' width='22px'>&nbsp;</td>\
          <td>Much Warmer >=5&deg;</td>\
        </tr>\
        <tr>\
          <td class='prcp_much_wetter' width='22px'>&nbsp;</td>\
          <td>Much Wetter >200%</td>\
          <td class='prcp_wetter' width='22px'>&nbsp;</td>\
          <td>Wetter 125% - 200%</td>\
          <td class='prcp_similar' width='22px'>&nbsp;</td>\
          <td>Similar 75% - 125%</td>\
          <td class='prcp_drier' width='22px'>&nbsp;</td>\
          <td>Drier 50% - 75%</td>\
          <td class='prcp_much_drier' width='22px'>&nbsp;</td>\
          <td>Much Drier <50%</td>\
        </tr>\
      </table>\
    ";
  }else if(document.ver_graph.units[1].checked){
    document.getElementById('table_settings').innerHTML="\
      <table align=center border=0 width='100%' style=\"font-size:10px;\">\
        <tr>\
          <th colspan=10 align=left>\
            Data Table Legend (Versus Last Year):\
          </th>\
        </tr>\
        <tr>\
          <td class='temp_much_colder' width='22px'>&nbsp;</td>\
          <td>Much Colder <=-2.8&deg;</td>\
          <td class='temp_colder' width='22px'>&nbsp;</td>\
          <td>Colder -0.6&deg; to -2.8&deg;</td>\
          <td class='temp_similar' width='22px'>&nbsp;</td>\
          <td>Similar -0.6&deg; to +0.6&deg;</td>\
          <td class='temp_warmer' width='22px'>&nbsp;</td>\
          <td>Warmer +0.6&deg; to +2.8&deg;</td>\
          <td class='temp_much_warmer' width='22px'>&nbsp;</td>\
          <td>Much Warmer >=2.8&deg;</td>\
        </tr>\
        <tr>\
          <td class='prcp_much_wetter' width='22px'>&nbsp;</td>\
          <td>Much Wetter >200%</td>\
          <td class='prcp_wetter' width='22px'>&nbsp;</td>\
          <td>Wetter 125% - 200%</td>\
          <td class='prcp_similar' width='22px'>&nbsp;</td>\
          <td>Similar 75% - 125%</td>\
          <td class='prcp_drier' width='22px'>&nbsp;</td>\
          <td>Drier 50% - 75%</td>\
          <td class='prcp_much_drier' width='22px'>&nbsp;</td>\
          <td>Much Drier <50%</td>\
        </tr>\
      </table>\
    ";

  }
  
  return;

}


function get_date_diff(period,start_id,end_id){

  var beg_date=document.getElementById(start_id).value;
  var beg_date2=new Date();
  beg_date2.setFullYear(beg_date.substring(6,10),beg_date.substring(0,2)-1,beg_date.substring(3,5));

  if(need_act && need_act==1){
    var end_date=document.getElementById(end_id).value;
    var end_date2=new Date();
    end_date2.setFullYear(end_date.substring(6,10),end_date.substring(0,2)-1,end_date.substring(3,5));
    if(period=="weekly"){
    	var day_diff=Math.ceil((end_date2.getTime()-beg_date2.getTime())/(one_day_milliseconds*7))+1;
    }else{
    	var day_diff=Math.ceil((end_date2.getTime()-beg_date2.getTime())/one_day_milliseconds)+1;
    }
  }else{
  	var day_diff=null;
  }
  return day_diff;
}

