// ADAP javaScript functions
// to do useful stuff such as getting request parameters, and cookies etc.

String.prototype.ucFirst = function () {
   return this.substr(0,1).toUpperCase() + this.substr(1,this.length);
}

String.prototype.lcFirst = function () {
   return this.substr(0,1).toLowerCase() + this.substr(1,this.length);
}

String.prototype.ucLast = function () {
   return this.substr(0,this.length-1) + this.substr(this.length-1,1).toUpperCase();
}

String.prototype.lcLast = function () {
   return this.substr(0,this.length-1) + this.substr(this.length-1,1).toLowerCase();
}

String.prototype.num = function () {
   return this - 0;
}



ADAP = {

//	this.param = param;
//	this.dp = dp;
//	this.cookie = cookie;
//	this.toNumber = toNumber;

	radio: function(name, valueToSelect) {
		var value = null;
		var radios = document.getElementsByName(name);
		for (var i=0; i<radios.length; i++) {
			if (valueToSelect) {
				if (radios[i].value == valueToSelect) {
					value=radios[i].checked=true;
				}
			} else {
				if (radios[i].checked) {
					value=radios[i].value;
				}
			}
		}
		return value;
	},

	param: function(name) {
		// get a value from the query string
		var query = location.search;
		query=query.replace(/^\?/,"");
		var pairs = query.split("&");
		for (var i =0; i < pairs.length; i++) {
			var bits = pairs[i].split("=");
			if (bits[0] == name) {
				return unescape(bits[1]);
			}
		}
		return "";
	},


	toNumber: function(str) {
		// convert a string into a number;
		// there is probably a better way to do this.
		// we should really use parseFloat and parseInt... or something
		str = str + " " ;
		
		str = str.replace(/[^\d\.-]/g, "");
		str-=1;
		str+=1;
		
		return str || 0;
	},



	dp: function(val) {
		var val = ADAP.toNumber(val || 0);
		var decimalPlaces = arguments[1] || 2;
		var debug = arguments[2] || 0;
		
		if (debug) {
			alert("dp " + val);
		}
		
		val = _round(val, decimalPlaces);	// actually get the right value
		val += ''; // make us a string;
	//	alert("val:" + val + "    decimal:"+decimalPlaces);
		
		if (decimalPlaces > 0) {	
			if (val.indexOf('.') == -1) {val = val + "."};	// add a dot if there isn;t one, so we don't get stuck in a loop
			var count=0;
			while ((count < 10) && val.charAt(val.length - decimalPlaces -1) != '.') {
				val = val + "0";
				count++;
			}
		}
		return val;	
	},
	
	_round: function(n, places) {
		var factor = Math.pow(10, (places || 0));
		return parseInt((n * factor) + (n < 0 ? -1 : 1) * 0.5) / factor;
	},


	
	cookie: function(name) {
		
		// return the values of cookies that may be set in the current document
		var allCookies = document.cookie || "";
		
		var index = allCookies.indexOf(name + "=");
		var start = index + name.length + 1;
		var end = allCookies.indexOf(";", start);
		if (end == -1) end = allCookies.length;
		var value = allCookies.substring(start,end);
		value = unescape(value);
		return value;
	},
	
	selectSelectOptions: function(selectElementId, valuesToSelect) {
			// valuesToSelect is an array of values
			var selectElement = document.getElementById(selectElementId);
			
			// turn everything off first
			for (loop=0; loop<selectElement.options.length; loop++) {
				selectElement.options[loop].selected=false;
			}
			
			for (var valIndex=0; valIndex<valuesToSelect.length; valIndex++) {
				for (loop=0; loop<selectElement.options.length; loop++) {
					if (selectElement.options[loop].value == valuesToSelect[valIndex]) {
						selectElement.options[loop].selected=true;
					}
				}
			}
		},
	
	popUp: function(url, width, height) {
			var ok=true;
			if (!url) {
				alert("Error: No URL supplied")
				ok =false;
			}
			
			if (ok) {
				var windowname = Math.floor(Math.random());
				newWin = window.open(url, windowname, "toolbar=yes,menubar=yes,location=yes,scrollbars=yes,resizable=yes,width=" + width + ",height=" + height);
				newWin.focus();
			}
		},
		
	validEmail: function(emailAddress) {
		return emailAddress.match(/.+\@.+\..+/);
	},
		
	addTableStripes: function(tableId, dark, mid, light) {
		if (!dark) {
			dark = "DDDDDD";
		}
		if (!mid) {
			mid = "EEEEEE";
		}
		if (!light) {
			light = "FFFFFF";
		}

		
		var table = document.getElementById(tableId);
		if (!table) {
			return null;	
		}

		var tableSection = table.firstChild;
		
		var rowCount=-1;
		
		for (var row = tableSection.firstChild; row!=null; row = row.nextSibling) {
			rowCount++;

			var cellCount=-1;
			
			
			for (var cell= row.firstChild; cell!=null; cell=cell.nextSibling) {
				cellCount++;
				if (cell.nodeType == 1) {	// an element node e.e. table, tr, td etc.
					var colour = (rowCount %2 ==0 ? (cellCount %2 ==0 ? light : mid) : (cellCount %2 ==0 ? mid : dark));
					
						cell.style.background = "#" + colour;	// I suspect this causes css errors if cell doesn't happen to be a table cell. I'm not sure how to check though...
					
				}
				
			}
		}
	}
	
}






