﻿function CmfuAjax()
{
    this.xml=false;
	this.GetXmlHttp=function()
	{
	    /*@cc_on @*/
	    /*@if (@_jscript_version >= 5)
	    try {
	      this.xml = new ActiveXObject("Msxml2.XMLHTTP");
	    } catch (e) {
	      try {
		    this.xml = new ActiveXObject("Microsoft.XMLHTTP");
	      } catch (e2) {
		    this.xml = false;
	      }
	    }
	    @end @*/
		if (!this.xml && typeof XMLHttpRequest != 'undefined') {
		   this.xml = new XMLHttpRequest();
		}
	}
	this.GetXmlHttp();
	var xmlHttp  = this.xml;
	var ajax     = this;	
	var callBack = null;//外部接口	
	//定义updatePage方法
	this.updatePage=function() 
    {
		if (xmlHttp.readyState == 4) {				
			var response = eval('('+xmlHttp.responseText+')');	
			if(callBack!=null && typeof  callBack == "function")
			{
				callBack(response);
			}			
		}
    }    
	//定义toQueryString方法
	this.toQueryString=function(json)
	{
		var query="";
		if(json!=null)
		{ 
			for(var param in json)
			{
				query += param + "=" + escape(json[param]) + "&"
			}
		}        
		return query; 
	}
	
    //定义Invoke方法
	this.invoke=function(opName,params,pageCallBack,method)
	{
		if(xmlHttp)
		{
			var query = "opName="+opName+"&";
			query += this.toQueryString(params);
			query = query.substring(0,query.length-1);
			callBack = pageCallBack;				
			if(method!=null &&method.toUpperCase()=="GET")
			{
				var url =  "../AjaxService.aspx?"+query;
				xmlHttp.onreadystatechange = ajax.updatePage;
				xmlHttp.open("GET", url, true);
				xmlHttp.setRequestHeader("CMFUAJAX-Ver","ver1.0");
				xmlHttp.send(null);
			}else{
				var url =  "../AjaxService.aspx";
				xmlHttp.onreadystatechange =ajax.updatePage; 
				xmlHttp.open("POST", url, true);
				xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
				xmlHttp.setRequestHeader("CMFUAJAX-Ver","ver1.0");
				xmlHttp.send(query);
			} 
		}
	}
}

function getRootPath()
{
	var p = window.location.pathname;				//取当前页地址
	if(p.substring(0,1)!="/")
		p = "/"+p;
	var str = p.replace(/^[/]\w*[/]/,"");			//将虚拟目录从字符串中清除
	return p.substring(0,p.length - str.length);	//返回虚拟目录
}

///////////////////// 定义 MyAjax方法 ////////////////////////////
var MyAjax={
    //检查用户名是否可用	
	CheckUserName:function(pUserName){
	    new CmfuAjax().invoke("CheckUserName", {"pUserName":pUserName},  arguments[1]);
	},
	   //检查用户名是否可用	
	CheckSDOEmailMobileExist:function(pUserName,type){
	    new CmfuAjax().invoke("CheckSDOEmailMobileExist", {"pUserName":pUserName,"type":type},  arguments[2]);
	    //  new CmfuAjax().invoke("CheckUserName", {"pUserName":pUserName},  arguments[1]);
	},
	
	
	//用户登录
//	UserLogin:function(userName, password, token, challenge, saveCookie,validateCode) {
//		new CmfuAjax().invoke("UserLogin", {"userName":userName, "password":password, "token":token, "challenge":challenge, "saveCookie":saveCookie,"validateCode":validateCode}, arguments[6]);
//	},
    UserLogin:function(userName, password, token, challenge, saveCookie,validateCode,sn) {
		new CmfuAjax().invoke("UserLogin", {"userName":userName, "password":password, "token":token, "challenge":challenge, "saveCookie":saveCookie,"validateCode":validateCode,"sn":sn}, arguments[7]);
	},
	//获取用户信息
	CheckLoginWithInfo:function(){
		new CmfuAjax().invoke("CheckLoginWithInfo", {}, arguments[0]);
	},
	//用户注销
	SignOut:function(){
		new CmfuAjax().invoke("SignOut", {}, arguments[0]);
	},
	//书籍加入书架
	AddBookToFavor:function(pBookId){
	    new CmfuAjax().invoke("AddBookToFavor", {"pBookId":pBookId},  arguments[1]);
	},
	//书籍下架
	DelFavorBook:function(pUserId,pBookCaseId,pBookId){
	    new CmfuAjax().invoke("DelFavorBook", {"pUserId":pUserId,"pBookCaseId":pBookCaseId, "pBookId":pBookId},  arguments[3]);
	},
	//添加书签
	AddOrReplaceBookmark:function(pBookId,pChapterId){
	    new CmfuAjax().invoke("AddOrReplaceBookmark", {"pBookId":pBookId,"pChapterId":pChapterId},  arguments[2]);
	},
	//修改密码
	ModifyPass:function(pUserId,pOldPass,pNewPass,pEkey){
	    new CmfuAjax().invoke("ModifyPass", {"pUserId":pUserId,"pOldPass":pOldPass, "pNewPass":pNewPass, "pEkey":pEkey},  arguments[4]);
	}
}

