
	String.prototype.parseJSON = function () {
		try {
			if(this.substr(0,1) == "{") { /*/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(this)) {*/
				return eval('(' + this + ')');			
			}
		} 
		catch (e) {
		}
		if(this != "")
			alert("parseJSON Error:\n\n" + this);
		throw new SyntaxError("parseJSON");		
	}
	
	function GetPhone(text) {
		var nums = "0123456789";
		text = text + " ";
		var phone = "";
		for(var i = 0; i < text.length; i++) {
			if(nums.indexOf(text.substr(i,1)) >= 0)
				phone += text.substr(i,1);
		}
		return phone;
	}
	
	function trim(text) {
		while(text.substr(0,1) == " ")
			text = text.substr(1);
		while(text.substr(text.length-1,1) == " ")
			text = text.substr(0,text.length-1);
		return text;
	}
	
	function substr_count(needle, haystack) {
		var x = 0;
		var count = 0;
		while(x >= 0) {
			x = haystack.indexOf(needle,x);
			if(x >= 0) {
				count++;
				x++;
			}
		}
		return count;
	}	
	
	function GetSelectValue(id) {
		if(!$(id))
			return false;
		var value;
		for(i = 0; i < $(id).options.length; i++)
			if($(id).options[i].selected)
				value = $(id).options[i].value;
		return value;
	}
	
	function SetSelectValue(id, value) {
		for(i = 0; i < $(id).options.length; i++)
			if($(id).options[i].value == value)
				$(id).options[i].selected = true;
	}	
	
	function GetObjectPos(obj) {
		var pos = new Array(0,0);
		if(obj.offsetParent) {
			while (obj.offsetParent) {
		  		pos[0] += obj.offsetLeft;
		  		pos[1] += obj.offsetTop;
		  		obj = obj.offsetParent;
			}
		}
	  	else if(obj.x) {
			pos[0] += obj.x;  
			pos[1] += obj.y;
		}		
		return pos;
	}
	
	function Enter(event) {
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if(keyCode == 13)
			return true;
		else
			return false;		
	}		
		
	function urlencode(theText) {
		if(!theText)
			return "";
		var SAFECHARS = "0123456789" +					// Numeric
						"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
						"abcdefghijklmnopqrstuvwxyz" +
						"-_.!~*'()";					// RFC2396 Mark characters
		var HEX = "0123456789ABCDEF";
		var plaintext = theText;
		var encoded = "";
		for (var i = 0; i < plaintext.length; i++ ) {
			var ch = plaintext.charAt(i);
			if (ch == " ") {
				encoded += "%20";				
				//encoded += "+";				// x-www-urlencoded, rather than %20
			} else if (SAFECHARS.indexOf(ch) != -1) {
				encoded += ch;
			} else {
				var charCode = ch.charCodeAt(0);
				if (charCode > 255) {
					alert( "Unicode Character '" 
							+ ch 
							+ "' cannot be encoded using standard URL encoding.\n" +
							  "(URL encoding only supports 8-bit characters.)\n" +
							  "A space (+) will be substituted." );
					encoded += "+";
				} else {
					encoded += "%";
					encoded += HEX.charAt((charCode >> 4) & 0xF);
					encoded += HEX.charAt(charCode & 0xF);
				}
			}
		}
		return encoded;
	}


	function urldecode(encoded) {
		if(!encoded)
			return "";
	   var HEXCHARS = "0123456789ABCDEFabcdef"; 
	   var plaintext = "";
	   var i = 0;
	   while (i < encoded.length) {
		   var ch = encoded.charAt(i);
		   if (ch == "+") {
			   plaintext += " ";
			   i++;
		   } else if (ch == "%") {
				if (i < (encoded.length-2) 
						&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
						&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
					plaintext += unescape( encoded.substr(i,3) );
					i += 3;
				} else {
					alert( 'Bad escape combination near ...' + encoded.substr(i) );
					plaintext += "%[ERROR]";
					i++;
				}
			} else {
			   plaintext += ch;
			   i++;
			}
		} 
		return plaintext;
	}
	
	
	function GetWindowWidth() {
		var myWidth = 0, myHeight = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		} 
		else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		} 
		else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		}
		return myWidth;
	}
	
	function GetWindowHeight() {
		var myWidth = 0, myHeight = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		} 
		else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		} 
		else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		}
		return myHeight;
	}
	
	function DisplayImage(path, width, height, style, imgclass, imgid, add) {
		var isIE55 = (navigator.appVersion.indexOf("MSIE 5.5") != -1);
		var isIE6 = (navigator.appVersion.indexOf("MSIE 6.0") != -1);
		var isOP = (navigator.userAgent.indexOf("Opera") != -1);
		var isWin = (navigator.userAgent.indexOf("Win") != -1);		
		var html = "";
		if(!add)
			add = "";
		if((isIE55 || isIE6) && isWin && !isOP)
			html = "<div style=\"font-size: 1px; height:" + height + "px; width:" + width + "px; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + path + "', sizingMethod='scale'); " + style + "\" id=\"" + imgid + "\" class=\"" + imgclass + "\" " + add + "></div>";
		else
			html = "<img src=\"" + path + "\" style=\"width:" + width + "px; height:" + height + "px; " + style + "\" id=\"" + imgid + "\" class=\"" + imgclass + "\" " + add + "/>";
		return html;
	}

  function HeaderSignIn(r) {
		if(!r) {
      var phone = GetPhone($('HeaderPhone').value);
      if(phone.length != 10) {
				ShowHeaderError("Please enter your 10-digit phone #");
      }
			else if($('HeaderPassword').value.length < 4) {
				ShowHeaderError("Please enter a valid password");
      }
			else {
				var url = web_root + "groups-account.php";
				var data = "&Operation=signin&Phone=" + phone + "&Password=" + urlencode($('HeaderPassword').value);
				//if($('Remember').checked)
					data += "&Remember=0";
				var request = new Ajax.Request(url,{method: 'post', parameters: data, onComplete: HeaderSignIn});				
			}
		}
		else {
			r = r.responseText.parseJSON();
			if(!r.Error)
				document.location.href = "groups-manage.php";
			else {
				ShowHeaderError(urldecode(r.Error));
			}
		}
	}
  
  function ShowHeaderError(msg) {
    var errorBox = $('HeaderError');
    errorBox.innerHTML = msg;
    errorBox.show();
  }
  