// Requires Prototype
var generalUtils = Class.create();

generalUtils.prototype = {
	initialize: function(){
		/* General variables for the flash dials */
		this.gaugeW = 320;
		this.gaugeH = 145;
		this.dialBgColor = "#FFFFFF";
		
		// Global variables used for custom plots
		this.startDate = '';
		this.endDate = '';
		
		this.setRadio = '';
		this.pLoad = 'true';
		
		this.dataSource = ''; // Data source for flash dials
		
		this.fileInfo = 0; // this is from the PHP scripts to know which file to use for processing
	},
	
	// The following three functions are used for manipulating text
	replaceText: function (e1,text){
		if (e1 !== null) {
			this.clearText(e1);
			this.newNode = document.createTextNode(text);
			e1.appendChild(this.newNode);
		}
	},

	// Returns the text within an html element object.  Must pass in $(id)
	getText: function (e1){
		this.text = "";
		if (e1 !== null) {
			if (e1.childNodes) {
				for (var i = 0; i < e1.childNodes.length; i++) {
					this.childNode = e1.childNodes[i];
					if (this.childNode.nodeValue !== null) {
						this.text = this.text + this.childNode.nodeValue;
					}
				}
			}
		}
		return this.text;
	},
	
	clearText: function (e1){
		if (e1 !== null) {
			if (e1.childNodes) {
				for (var i = 0; i < e1.childNodes.length; i++) {
					this.childNode = e1.childNodes[i];
					e1.removeChild(this.childNode);
				}
			}
		}
	},

	// Used to get a flash object for updating it. Called by setDialValue function
	getFlashObject: function (objectName) {
		//return document.embeds[objectName];
		if (navigator.appName.indexOf ("Microsoft") !=-1) {
			return window[objectName];
		} else {
			return document.embeds[objectName];
		}
		
	},
	
	// Returns the value of a selected radio button. radObj is an object
	// passed in through the document.getElementsByName(name) function
	getRadio: function (radObj){
		for(var i = 0; i < radObj.length; i++) {
			if(radObj[i].checked) {
				return radObj[i].value;
			}
		}	
	},
	
	// Returns a value with a leading zero if it's less than 10
	leadingZero: function (val){
		return (val < 10 ? '0' : '') + val;
	},
	
	// Takes in a string and searches for a known set of html entities.
	// Replaces them with unicode that is compatible with javascript
	getUnicode: function (str){
		if (str.indexOf("^2") != -1){
			return str.replace("^2","\u00b2");
		}else if (str.indexOf("deg") != -1){
			return str.replace("deg","\u00b0");
		}else if (str.indexOf("nbs") != -1){
			return "\u00A0";
		}else if (str.indexOf("H2O") != -1){
			return str.replace("H2O","H\u2082O");
		}else if (str.indexOf("delta") != -1){
			return str.replace("delta","\u0394");
		}else{
			return str;
		}
	},
	
	// returns the value of an XML text node. xml input variable is an ajax request.responseXML object
	$TAG: function(xmlResponse,tagName) {
		return 	xmlResponse.getElementsByTagName(tagName)[0].firstChild.nodeValue;	
	},
	
	
	failedAjax: function(){
		alert("Ajax Request Failed!");
	},
	
	//toggle function
	toggle: function(id,obj){
		$(id).toggle();
		this.toggleImage(obj);
	},
	
	
	//expand and collapse table menus
	toggleImage:function(tr) {
		td = tr.firstDescendant();
		td.innerHTML = td.innerHTML == '+' ? "-" : "+";
	},
	
	// function used to permanently change the tabs above and below the graphs
	changeTabs: function(obj,class1,class2){
		// get the currently selected element and deselect it
		var button = $(obj.parentNode.id).getElementsByClassName(class2)[0];
		Element.addClassName(button,class1);
		Element.removeClassName(button,class2);
		
		// Select the element that has been clicked
		Element.removeClassName(obj,class1);
		Element.addClassName(obj,class2);
	},
	
	verifyDates: function(sD,eD){
		// Create new date utility object 
		var dateUtil = new dateUtils();
		
		// Get the start and end dates and make sure they are legit
		this.startDate = $(sD).value;
		this.endDate = $(eD).value;

		if (!dateUtil.isDate(gen.startDate,'MM/dd/yyyy')){
			alert('Start date is not in the correct format: mm/dd/yyyy');
		} else if (!dateUtil.isDate(gen.endDate,'MM/dd/yyyy')){
			alert('End date is not in the correct format: mm/dd/yyyy');
		} else if (dateUtil.compareDates(gen.startDate,'MM/dd/yyyy',gen.endDate,'MM/dd/yyyy')){
			alert('Start date must be before end date');	
		} else if (dateUtil.compareDates(dataStart,'MM/dd/yyyy',gen.startDate,'MM/dd/yyyy')){
			alert('There is no data from before ' + dataStart);
		} else if (dateUtil.compareDates(gen.endDate,'MM/dd/yyyy',dataEnd,'MM/dd/yyyy') || dateUtil.compareDates(gen.endDate,'MM/dd/yyyy',dataEnd,'MM/dd/yyyy')){
			alert('The end date is outside the range of this data set.\nPlease select an end date on or before ' + dataEnd + '.');
		} else {
			return true;
		}	
		return false;
	}
}

