function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

String.prototype.Trim = function() { return this.replace(/(^\s*)|(\s*$)/g, ""); }
String.prototype.LTrim = function() { return this.replace(/(^\s*)/g, ""); }
String.prototype.RTrim = function() { return this.replace(/(\s*$)/g, ""); }

function addBookmark(title,url)
{
	if (window.sidebar)
	{ 
		window.sidebar.addPanel(title, url,""); 
	}
	else if( document.all )
		{
			window.external.AddFavorite( url, title);
		}
		else if( window.opera && window.print )
			{
			return true;
			}
}

function EmailAddressVerify(Email)
{
	var CheckResult=false;
	$.ajax({
		type: "POST",
		url:"/Ajax.InputValueVerify.php" + "?querytime=" + new Date().getTime(),
		dataType: "html",
		async: false,
		data: "ValueType=EmailVerify&InputValue="+Email,
		success: function(data) {
			if (data == true)
			{
				CheckResult=true;
			}
		},
		error: function(data) {
			alert("Email address verify error, Please try again later!");
		}
	});
	return CheckResult;
}

function MemberFunction()
{
	this.SubmitLogin =function (ThisObj){
			ThisObj.LoginBtn.style.display="none";
			$('#WaitForSubmitLogin').show();
			$.ajax({
				type: "POST",
				url:"Ajax.Login.php" + "?querytime=" + new Date().getTime(),
				dataType: "json",
				data:this.GetFormData(ThisObj),
				success: function(data) {
					if (data['LoginOK'])
					{
						location.href=data['GoUrl'];
					}
					else
					{
						$('#WaitForSubmitLogin').html("<br><font color='red'><b>"+data['Result']+"</b></font>");
						ThisObj.LoginBtn.style.display="";
					}
				},
				error: function(data) {
					$('#WaitForSubmitLogin').html("<br><font color='red'><b>System failed, Please try again later!</b></font>");
					ThisObj.LoginBtn.style.display="";
				}
			});
		};

	this.SubmitNewProfile =function (ThisObj){
			ThisObj.SubmitBtn.style.display="none";
			$('#WaitForSubmitProfile').show();
			$.ajax({
				type: "POST",
				url:"Ajax.ChangeMemberProfile.php" + "?querytime=" + new Date().getTime(),
				dataType: "json",
				data:this.GetFormData(ThisObj),
				success: function(data) {
					if (data['ChangeOK'])
					{
						alert(data['Result']);
						oMemberFunction.GetLoginInfo();
						ThisObj.SubmitBtn.style.display="";
						$('#WaitForSubmitProfile').hide();
					}
					else
					{
						alert(data['Result']);
						ThisObj.SubmitBtn.style.display="";
						$('#WaitForSubmitProfile').hide();
					}
				},
				error: function(data) {
					alert("Change profile was faile, Please try again later!");
					ThisObj.SubmitBtn.style.display="";
					$('#WaitForSubmitProfile').hide();
				}
			});
		};

	this.SubmitReg =function (ThisObj){
			ThisObj.CreateBtn.style.display="none";
			$('#WaitForSubmitReg').show();
			$.ajax({
				type: "POST",
				url:"Ajax.MemberReg.php" + "?querytime=" + new Date().getTime(),
				dataType: "json",
				data:this.GetFormData(ThisObj),
				success: function(data) {
					if (data['RegOK'])
					{
						//alert(data['Result']);
						location.href=data['GoUrl'];
					}
					else
					{
						alert(data['Result']);
						ThisObj.CreateBtn.style.display="";
						$('#WaitForSubmitReg').hide();
					}
				},
				error: function(data) {
					alert("Register was faile, Please try again later!");
					ThisObj.CreateBtn.style.display="";
					$('#WaitForSubmitReg').hide();
				}
			});
		};
	
	this.GetPassword =function (ThisObj){
			ThisObj.GetPasswordBtn.style.display="none";
			$('#WaitForSubmitGetPassword').show();
			$.ajax({
				type: "POST",
				url:"Ajax.MemberGetPassword.php" + "?querytime=" + new Date().getTime(),
				dataType: "json",
				data:this.GetFormData(ThisObj),
				success: function(data) {
					if (data['SendOK'])
					{
						//alert(data['Result']);
						location.href=data['GoUrl'];
					}
					else
					{
						alert(data['Result']);
						ThisObj.GetPasswordBtn.style.display="";
						$('#WaitForSubmitGetPassword').hide();
					}
				},
				error: function(data) {
					alert("Submit was failed, Please try again later!");
					ThisObj.GetPasswordBtn.style.display="";
					$('#WaitForSubmitGetPassword').hide();
				}
			});
		};

	this.CheckEmailAllowReg =function (Email) {
			var CheckResult=false;
			$.ajax({
				type: "POST",
				url:"/Ajax.InputValueVerify.php" + "?querytime=" + new Date().getTime(),
				dataType: "html",
				async: false,
				data: "ValueType=EmailAllowReg&InputValue="+Email,
				success: function(data) {
					if (data == true)
					{
						CheckResult=true;
					}
				},
				error: function(data) {
					alert("Can't check email register status, Please try again later!");
				}
			});
			return CheckResult;
		};

	this.GetFormData =function(theForm) {
			var query_string='';
			var and='';
			//alert(form_obj.length);
			for (i=0;i<theForm.length ;i++ )
			{
				e=theForm[i];
				element_value='';
				if (e.name!='')
				{
					if (e.type=='select-one')
					{
						element_value=e.options[e.selectedIndex].value;
					}
					else
					{						
						if (e.type=='checkbox' || e.type=='radio')
						{
							if (e.checked!=false)
							{
								element_value=e.value;
							}
						}
						else
						{
							element_value=e.value;
						}
					}
					query_string+=and+e.name+'='+element_value.replace(/\&/g,"%26");
					and="&";
				}
			}
			return query_string;
        };

	this.GetLoginInfo = function() {//登录
			$.ajax({
				type: "GET",
				url:"Ajax.GetLoginInfo.php" + "?querytime=" + new Date().getTime(),
				dataType: "html",
				async: false,
				success: function(data) {
				$("#PublicLoginInfo").html(data);
				},
				error: function(data) {
					$("#PublicLoginInfo").html("GetLoginInfo.Error;");
				}
			});
		};
	
	this.SubmitLogout = function(ThisObj) {//退出
			$.ajax({
				type: "GET",
				url:"Ajax.Logout.php" + "?querytime=" + new Date().getTime(),
				dataType: "json",
				async: false,
				success: function(data) {
					location.href="/";
				},
				error: function(data) {
					alert("Error!");
				}
			});
		};
}
var oMemberFunction = new MemberFunction();
