/*
	Just two things I like:  string.trim and I hate remembering Array.splice, so I made this Array.remove() guy.
*/
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };
Array.prototype.remove = function(n) { if (n < this.length && n > -1) this.splice(n, 1); }

function $(n) {
	return document.getElementById(n);
}

var Ajax = new Object(); 
Ajax.isUpdating = true; 

Ajax.Request = function(method, url, query, callback) { 
	this.isUpdating = true; 
	this.callbackMethod = callback; 
	this.request = (window.XMLHttpRequest)? new XMLHttpRequest(): new ActiveXObject("MSXML2.XMLHTTP"); 
	this.request.onreadystatechange = function() { Ajax.checkReadyState(); }; 

	if(method.toLowerCase() == 'get') url = url+"?"+query; 
	this.request.open(method, url, true); 
	this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	this.request.send(query);  
}
Ajax.checkReadyState = function(_id) { 
	switch(this.request.readyState) { 
		case 1: break; 
		case 2: break; 
		case 3: break; 
		case 4: 
		this.isUpdating = false; 
		this.callbackMethod(this.request.responseText); 
	} 
} 
var Url = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string + ''));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}

var Cookie = {
	Get: function(c_name) {
		if (document.cookie.length>0) {
			c_start=document.cookie.indexOf(c_name + "=");
			if (c_start!=-1) {
				c_start=c_start + c_name.length+1;
				c_end=document.cookie.indexOf(";",c_start);
				if (c_end==-1) c_end=document.cookie.length;
				return unescape(document.cookie.substring(c_start,c_end));
			}
		}
		return "";
	},
	Set: function(c_name, value) {
		var expiredays=50;
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+expiredays);
		document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
	}
}

var Encode = {
	Html: function(str) {
	   var div = document.createElement('div');
	   var text = document.createTextNode(str);
	   div.appendChild(text);
	   return div.innerHTML;
	},
	Safe: function(str) {
		// More than just html encoding, we need to replace " with &quote;
		// There may be other changes too that I haven't found yet.
		var tmp = Encode.Html(str);
		tmp = tmp.replace(/"/g, '&quot;');
		return tmp;	
	}
}

var Validate = {
	Email:		function(str) {
   					// return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
					//var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
					//return reg.test(str)
					var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ 
					return str.match(re);
   				}				
}



function qstring(ji) {
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for (i=0;i<gy.length;i++) {
		ft = gy[i].split("=");
		if (ft[0] == ji) {
			return ft[1];
		}
	}
}



var PollSelected = new Object;
function PollCheck(pid, obj) {
	if (PollSelected[pid]) {
		PollSelected[pid].setAttribute('chk', '0');
		PollSelected[pid].src = 'off.gif';
		// PollSelected[pid].src = 'reddot3.png';
	}
	PollSelected[pid] = obj;
	obj.setAttribute('chk', '1');
	obj.src = 'on.gif';
	// obj.src = 'reddot4.png';
}


