/*	trend_day_forecast.js
	081609 hac		v1.0	from system ranking tool

	Grabs the current trend day odds from the server and displays them w/ the google speedometer

*/


var xmlhttp = null;
var percent = 0;

function writedate() {

	document.write(Date());
}


//encode chart url
function encodecharturl(rank, min, max, index){
	var chartstr	= "";
	var chs, chd, chds, cht, chtt, chco, chdl, chls	="";
	var chxt, chxl, chg, chm	= "";
	var dx, dy = 0;
	var titlestr	= "Odds(%)";
	
	rank		= rank.toPrecision(2);		// one decimal place for label
	dx			= rank;
	
	chs			= "chs=300x200";
	
    chd 		= "chd=t:" + dx.toString();
    
    chds 		= "chds=" + min.toString() + "," + max.toString();

    cht			= "cht=gom";
    
    chtt		= "chtt=" + titlestr;
    
   	chco		= "chco=ff0000,ffff00,adff2f,32cd32"; 				// colors
   	
   	chl			= "chl=" + rank.toString() + "%";		// label
    
    
    chartstr 	= "http://chart.apis.google.com/chart?" 
    				+ chs + "&amp;" 
    				+ chd + "&amp;" 
    				+ chds + "&amp;"
    				+ chco + "&amp;" 
    				+ cht + "&amp;" 
    				+ chtt + "&amp;"
    				+ chl;
    
	return chartstr;
}


//returns the url to fetch the chart from google
function getcharturl(rank, min, max){
	var chartstr = "";
	
    
    //encode url from the median series for plotting
    chartstr = encodecharturl(rank, min, max, 2);
    
    /*if (window.console) {
			window.console.log("url: " + chartstr);
	}*/
    
    return chartstr;
}


function loadXMLDoc(url, percent){    
    var doctext = "";
	xmlhttp=new XMLHttpRequest();
	
	if (xmlhttp!=null){
	  //xmlhttp.onreadystatechange= state_Change;
	  xmlhttp.open("GET",url, false);
	  //xmlhttp.overrideMimeType('text/xml'); for mozilla
	  xmlhttp.send(null);
	  
	  //falls through on synchronous
		if (window.console) {
			window.console.log(xmlhttp.readyState.toString());
		}
		
		if (xmlhttp.readyState==4){
		  // 4 = "loaded"
		  
			if (window.console) {
				window.console.log("state change status");
			}
		
		  if (xmlhttp.status==200){
			// 200 = "OK"
			
			
			doctext = xmlhttp.responseText;
			
			if (window.console) {
				window.console.log("raw doctext: " + doctext);
			}
			
			//doctext = doctext.replace(/^\s*|\s*$/g,'');
			
			doctext = doctext.replace(/[^0-9]+/g, '');
			
			if (window.console) {
				window.console.log("clean doctext: " + doctext);
			}
			
			percent = parseFloat(doctext);
			
			if (window.console) {
				window.console.log("state change " + percent.toString());
			}
			
		  }
		  else{
			alert("Problem retrieving data from server:" + xmlhttp.statusText);
			
			percent = 0;
		  }
	  
	  }
	}
	else{
	  alert("Your browser does not support XMLHTTP.");
	}
	
	return percent;
}

/*

function state_Change(){
	var doctext = "";
	
	if (window.console) {
		window.console.log(xmlhttp.readyState.toString());
	}
	
	if (xmlhttp.readyState==4){
	  // 4 = "loaded"
	  
		if (window.console) {
			window.console.log("state change status");
		}
	
	  if (xmlhttp.status==200){
		// 200 = "OK"
		
		
		doctext = xmlhttp.responseText;
		
		if (window.console) {
			window.console.log("raw doctext: " + doctext);
		}
		
		//doctext = doctext.replace(/^\s*|\s*$/g,'');
		
		doctext = doctext.replace(/[^0-9]+/g, '');
		
		if (window.console) {
			window.console.log("clean doctext: " + doctext);
		}
		
		percent = parseFloat(doctext);
		
		if (window.console) {
			window.console.log("state change " + percent.toString());
		}
		
	  }
	  else{
		alert("Problem retrieving data from server:" + xmlhttp.statusText);
		
		percent = 0;
	  }
	}
} 
*/


function trendday () {
	var min = 0;
	var max = 100;
	//var percent = 20;
	var url = "http://www.verticalsolutions.com/odds.txt"
	
	if (window.console) {
		window.console.log(percent.toString());
	}
	
	// get the current odds from the server
	percent = loadXMLDoc(url, percent);
	
	if (window.console) {
		window.console.log("post loadxml " + percent.toString());
	}
	
    // build out the url to get a chart from google
	charturl = getcharturl(percent, min, max);
	
	// display the chart
	t = "<img src=" + charturl + " />"
			+ "<br /> <b>&nbsp &nbsp &nbsp &nbsp &nbsp Range Day &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Trend Day</b>"	
			+ "<br /><br />"
			+ "Last updated: "
			+ Date();
			
	document.getElementById("chart").innerHTML = t;
    
}


