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

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 != null) {
				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
		str-=1;
		str+=1;
		
		return str || 0;
	},



	dp: function(val) {
		var val = toNumber(val || 0);
		var decimalPlaces = arguments[1] || 2;
		
		//if ((val > 31) && (val < 32) ) {
			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 + "=");
		if (index==-1) return "";
		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.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(/.+\@.+\..+/);
				}
				
				// ==================================================================================
				// remember not to include a comma after the last function, otherwsie IE gets upset!
				// ==================================================================================
	
}







