function NeatDialog(sHTML, sTitle, bCancel)
{
window.neatDialog = null;
this.elt = null;
if (document.createElement && document.getElementById)
{
var dg = document.createElement("div");
dg.className = "neat-dialog";
if (sTitle)
sHTML = '<div class="neat-dialog-title">'+sTitle+
((bCancel)?
'<img src="x.gif" alt="Cancel" class="nd-cancel" />':'')+
'</div>\n' + sHTML;
dg.innerHTML = sHTML;

var dbg = document.createElement("div");
dbg.id = "nd-bdg";
dbg.className = "neat-dialog-bg";

var dgc = document.createElement("div");
dgc.className = "neat-dialog-cont";
dgc.appendChild(dbg);
dgc.appendChild(dg);

//adjust positioning if body has a margin
if (document.body.offsetLeft > 0)
dgc.style.marginLeft = document.body.offsetLeft + "px";

document.body.appendChild(dgc);
if (bCancel) document.getElementById("nd-cancel").onclick = function()
{
window.neatDialog.close();
};
this.elt = dgc;
window.neatDialog = this;
}
}
NeatDialog.prototype.close = function()
{
if (this.elt)
{
this.elt.style.display = "none";
this.elt.parentNode.removeChild(this.elt);
}
window.neatDialog = null;
}

function openDialog()
{

var sHTML = '<p>用&nbsp;户&nbsp;&nbsp;名：<input name="regusername" type="text" id="regusername" /></p><p>密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码：<input name="regpassword" type="password" id="regpassword" /></p> <p>确认密码：<input name="regpassword2" type="password" id="regpassword2" /></p><p>电子邮件：<input name="regmail" type="text"  id="regmail"/></p><span id="regmsg"></span><p class="btn" ><input name="注册" type="button" class="btn1" onclick="regsubmit()" id="regbutton" /><input name="关闭" type="button" class="btn2" onclick="window.neatDialog.close()"/></p>';
new NeatDialog(sHTML, "会员注册", false);

}

NeatDialog.prototype.close = function()
{
if (this.elt)
{
this.elt.style.display = "none";
this.elt.parentNode.removeChild(this.elt);
}
window.neatDialog = null;
}

function openDialog_login()
{

var sHTML = '<p>用&nbsp;户&nbsp;&nbsp;名：<input type="text" id="loginusername" name="username" value="" /></p><p>密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码：<input type="password" id="loginpassword" name="password" /></p> '+
'<span id="loginmsg"></span><p class="btn" ><input name="登录" type="button" class="btn3" onclick="loginsubmit()" id="loginbutton" /><input name="关闭" type="button" class="btn2" id="loginclose" onclick="window.neatDialog.close()"/></p> </form>';
new NeatDialog(sHTML, "会员登录", false);

}

function openDialog_password()
{

var sHTML = '<p>原&nbsp;&nbsp;&nbsp;密&nbsp;&nbsp;&nbsp;码：<input type="password" id="oldpassword" name="oldpassword" value="" /></p><p>新&nbsp;&nbsp;&nbsp;密&nbsp;&nbsp;&nbsp;码：<input type="password" id="newpassword" name="newpassword" /></p><p>确认新密码：<input type="password" id="rnewpassword" name="rnewpassword" /></p> '+
'<span id="passwordmsg"></span><p class="btn" ><input name="更改密码" type="button" class="btn3" onclick="passwordsubmit()" id="passwordbutton" /><input name="关闭" type="button" class="btn2" id="loginclose" onclick="window.neatDialog.close()"/></p> </form>';
new NeatDialog(sHTML, "会员登录", false);

}

function kajax(url,handleResponse,data,method){
	var http_request = false;
	method = method=="POST" ? 'POST' : 'GET';
	var t=new Date().getTime();
		url+="&t="+t;

	if(window.XMLHttpRequest){
		http_request = new XMLHttpRequest();
    }else if (window.ActiveXObject){
        try{
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
        }
    }
    if(!http_request){
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    http_request.onreadystatechange=function (){
		if(http_request.readyState == 4) {
			if(http_request.status==200) {
				eval(handleResponse+"(http_request.responseText)");
			}else{
				if(http_request.status==404){
					alert("找不到路径"+url);
				}
			}
		}
	};
	http_request.open(method,url,true);
	if(data){
		sdata = '';
		if(typeof data !== "string"){
			 for (var i = 0; i < data.length; i++) {
				sdata += data[i][0] + "=" + $e(data[i][1]) + "&";
			 }
			 data = sdata.substring(0, sdata.length - 1);
		}
		http_request.setRequestHeader("Content-Length",data.length);   
		http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		http_request.send(data);
	}else{
	    http_request.setRequestHeader("Content-Type","text/html");
		http_request.send(null);
	}
}

function trim(str){
	return str.replace(/(^\s*)|(\s*$)/g, "");
}
var ct = 0;
function loginsubmit(){
	var loginusername = trim($f("loginusername"));
	var loginpassword = trim($f("loginpassword"));
	
	if(!ct && loginusername && loginpassword){
		ct =1;
		$obj('loginmsg').innerHTML='正在登陆中......';
		$obj('loginmsg').style.display='block';
		var data = "username="+$e(loginusername) +'&password='+$e(loginpassword);
		kajax('index.php?do=login_ajax',"login",data,'POST');
	}else {
		alert('用户名、密码未填写');
	}
}
var refer = '';
function login(t){
		if(t=='success'){
			$obj('loginmsg').innerHTML='成功登陆,跳转中....';
			if(refer){
				window.location.href = refer;
			}else{
				window.location.reload(); 
			}
		}else{
			ct =0;
			$obj('loginmsg').innerHTML=t;
		}
}

function passwordsubmit(){
	var oldpassword = trim($f("oldpassword"));
	var newpassword = trim($f("newpassword"));
	var rnewpassword = trim($f("rnewpassword"));
	
	if(!ct && oldpassword && newpassword && rnewpassword){
		if(newpassword != rnewpassword){
			alert('二次密码不一致。请再输入一遍！');
			document.getElementById("newpassword").value ='';
			document.getElementById("rnewpassword").value ='';
			$obj('newpassword').focus();
			return;
		}
		ct =1;
		$obj('passwordmsg').innerHTML='正在更新密码中......';
		$obj('passwordmsg').style.display='block';
		var data = "oldpassword="+$e(oldpassword) +'&newpassword='+$e(newpassword);
		kajax('index.php?do=password_ajax',"changepassword",data,'POST');
	}else {
		alert('数据未填写完毕...');
	}
}

function changepassword(t){
		if(t=='success'){
			ct =1;
			alert('更新密码成功...请重新登录！');
			window.neatDialog.close();
		}else{
			ct =0;
			$obj('passwordmsg').innerHTML=t;
			document.getElementById("newpassword").value ='';
			document.getElementById("rnewpassword").value ='';
			document.getElementById("oldpassword").value ='';
			$obj('oldpassword').focus();
		}
}

function regsubmit(){
	var regusername = trim($f("regusername"));
	var regpassword = trim($f("regpassword"));
	var regpassword2 = trim($f("regpassword2"));
	var regemail = trim($f("regmail"));

	
	if(!ct && regusername && regpassword && regpassword2 && regemail){
		if(regpassword != regpassword2){
			alert('二次密码不一致。请再输入一遍！');
			document.getElementById("regpassword").value ='';
			document.getElementById("regpassword2").value ='';
			$obj('regpassword').focus();
			return;
		}

		ct =1;
		document.getElementById('regmsg').innerHTML='正在登陆中......';
		document.getElementById('regmsg').style.display='block';
		var data = "username="+$e(regusername) +'&password='+$e(regpassword) +'&password2='+$e(regpassword2) +'&email='+$e(regemail);
		kajax('index.php?do=reg_ajax',"reg",data,'POST');
	}else {
		alert('未填写完全');
	}
}

function reg(t){
		if(t=='success'){
			$obj('regmsg').innerHTML='成功注册,跳转中....';
			window.location.reload(); 
		}else{
			ct =0;
			$obj('regmsg').innerHTML=t;
		}
}

function $e(str){
	return encodeURIComponent(str);	
}

function $obj(id){
	return document.getElementById(id);
}
function $f(id){
	return document.getElementById(id).value;
}

function ask_selected_cid(cid){
	if(cid){
		var obj = $obj("ask_cid");
		var len = obj.options.length;
        for(var j = 0; j < len; j++){
           if (obj.options[j].value == cid){
			   obj.options[j].selected = true;
			   return;
		   }
		}
	}
}

function closemsgbox(){
	var obj = $obj('msgboxd');
	obj.parentNode.removeChild(obj);
}

function ztkc(id){
    if(confirm('确认暂停课程？')){
       window.location.href="?do=user_ztkc&id="+id;
    }
}

function openkc(id,num){
    if(confirm('确认开通课程？')){
       window.location.href="?do=user_openkc&id="+id;
    }
}
