/*******************************************************************************
* updateChart refreshes the passed chartid by forwarding the passed daterange
* parameter to the passed dataphp file to generate the chart xml.
*******************************************************************************/
function updateChart(chartid, dataphp, daterange, viewby, dateFrom, dateTo, in_itemIDs){ 

   //dataURL for the chart with the time appended to prevent caching
   var strURL = "Data/" + dataphp + ".php?daterange=" + daterange + "&viewby=" + viewby + 
   "&dateFrom=" + dateFrom + 
   "&dateTo=" + dateTo +
   "&itemIDs=" + in_itemIDs +
   "&currTime=" + getTimeForURL();

   //URLEncode it - NECESSARY.
   strURL = escape(strURL);

   //Get reference to chart object using passed id & request XML
   var chartObj = getChartFromId(chartid); 
   chartObj.setDataURL(strURL);
}   

      
/*******************************************************************************
* getTimeForURL method returns the current time in a URL friendly format, so
* that it can be appended to dataURL for effective non-caching.
*******************************************************************************/
function getTimeForURL(){
	var dt = new Date();
	var strOutput = "";
	strOutput = dt.getHours() + "_" + dt.getMinutes() + "_" + dt.getSeconds() + "_" + dt.getMilliseconds();
	return strOutput;
} 


// To be included in the javascript functions script
function loadData(URL, daterange, viewby, category, chainid, dateFrom, dateTo, itemids) {
	// Create the XML request

    xmlReq = null;
    if(window.XMLHttpRequest) xmlReq = new XMLHttpRequest();
    else if(window.ActiveXObject) xmlReq = new ActiveXObject("Microsoft.XMLHTTP");
    if(xmlReq==null) return; // Failed to create the request

	// Anonymous function to handle changed request states
    xmlReq.onreadystatechange = function() {
        switch(xmlReq.readyState)
        {
        case 0: // Uninitialized
            break;
        case 1: // Loading
            break;
        case 2: // Loaded
            break;
        case 3: // Interactive
            break;
        case 4: // Done!
        
	        // Retrieve the data between the <PIXELSIZE> tags
	        var xmlDoc=xmlReq.responseXML.documentElement;
    		//setWidthHeight(xmlDoc.getElementsByTagName("pixels")[0].childNodes[0].nodeValue);
    		
    		datastr = xmlDoc.getElementsByTagName("pixels")[0].childNodes[0].nodeValue;
			var dimArray = datastr.split('~');
	 	   	thDivHeight = dimArray[0];
		    thDivWidth = dimArray[1];
			  
			document.getElementById("actionComplianceDiv").innerHTML = "";
    		//Instantiate the Chart	
			var chart_actionCompliance = new FusionCharts("../../Charts/Drawingpad.swf", "actionCompliance", thDivWidth, thDivHeight, "0", "1");
    	 	chart_actionCompliance.setTransparent("false");
    
			//Set the dataURL of the chart
			var dataURL = "Data/actions_trafficlight.php?category="+category+
				"&viewby="+viewby +
				"&daterange="+daterange+
				"&chain="+chainid+
				"&dateFrom=" + dateFrom + 
   				"&dateTo=" + dateTo +
   				"&itemIDs=" + itemIDs;
   				
			//encode the URL
			dataURL = urlencode(dataURL);
			
			chart_actionCompliance.setDataURL(dataURL);
			//Finally, render the chart.
			chart_actionCompliance.render("actionComplianceDiv");
			
            break;
        default:
            break;
        }
    }

    // Make the request
    xmlReq.open ('GET', URL, true);
    xmlReq.send (null);
}

function urlencode( str ) {
                
    var histogram = {}, unicodeStr='', hexEscStr='';
    var ret = (str+'').toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urldecode.
    histogram["'"]   = '%27';
    histogram['(']   = '%28';
    histogram[')']   = '%29';
    histogram['*']   = '%2A';
    histogram['~']   = '%7E';
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    histogram['\u00DC'] = '%DC';
    histogram['\u00FC'] = '%FC';
    histogram['\u00C4'] = '%D4';
    histogram['\u00E4'] = '%E4';
    histogram['\u00D6'] = '%D6';
    histogram['\u00F6'] = '%F6';
    histogram['\u00DF'] = '%DF';
    histogram['\u20AC'] = '%80';
    histogram['\u0081'] = '%81';
    histogram['\u201A'] = '%82';
    histogram['\u0192'] = '%83';
    histogram['\u201E'] = '%84';
    histogram['\u2026'] = '%85';
    histogram['\u2020'] = '%86';
    histogram['\u2021'] = '%87';
    histogram['\u02C6'] = '%88';
    histogram['\u2030'] = '%89';
    histogram['\u0160'] = '%8A';
    histogram['\u2039'] = '%8B';
    histogram['\u0152'] = '%8C';
    histogram['\u008D'] = '%8D';
    histogram['\u017D'] = '%8E';
    histogram['\u008F'] = '%8F';
    histogram['\u0090'] = '%90';
    histogram['\u2018'] = '%91';
    histogram['\u2019'] = '%92';
    histogram['\u201C'] = '%93';
    histogram['\u201D'] = '%94';
    histogram['\u2022'] = '%95';
    histogram['\u2013'] = '%96';
    histogram['\u2014'] = '%97';
    histogram['\u02DC'] = '%98';
    histogram['\u2122'] = '%99';
    histogram['\u0161'] = '%9A';
    histogram['\u203A'] = '%9B';
    histogram['\u0153'] = '%9C';
    histogram['\u009D'] = '%9D';
    histogram['\u017E'] = '%9E';
    histogram['\u0178'] = '%9F';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
 
    for (unicodeStr in histogram) {
        hexEscStr = histogram[unicodeStr];
        ret = replacer(unicodeStr, hexEscStr, ret); // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
}

function SetDate(field,format) {
	document.getElementById(field).value = "";
	NewCal(field,format);
}

function showDropDown()
{
    document.getElementById("ddlDiv").style.visibility = "visible";
}

var itemIDs = "";

function acceptDropDown() {
	itemIDs = "";
    document.getElementById("ddlDiv").style.visibility = "hidden";
    // Get values into textbox
    var myList = document.getElementById("items");
    var myListCount = myList.options.length; // number of items
    var textToDisplay = "";
    var selectCount = 0;
    
    for (i=0; i < myListCount; i++) {
    	if (myList.options[i].selected == true) {
    		
    		if(selectCount>0) {
    			textToDisplay = "Multiple Items Selected";
    		}
    		else {
    			textToDisplay = myList.options[i].text;
    		}
    		itemIDs += myList.options[i].value + "|";
    		selectCount ++;
    	}
    }
    
    if(textToDisplay == "") {
    	textToDisplay = "Select Items";
    }
    
    document.getElementById("txtDropDown").value = textToDisplay;
    //alert(itemIDs);
}


