//Dingtai Namespace
var Dingtai={
    $:function(el){
        if (document.getElementById) {
		    return document.getElementById(el);
	    }
	    else if (document.all){
		    return document.all[el];
	    }
	    else{
	        return;
	    }
    },
    $cls:function(cl){
        var retnode = [];
	    var myclass = new RegExp('\\b'+cl+'\\b');
	    var elem = document.getElementsByTagName('*');
	    for (var j = 0; j < elem.length; j++) {
		    var classes = elem[j].className;
		    if (myclass.test(classes)) retnode.push(elem[j]);
	    }
	    return retnode;
    },
    AscLength:function(str){
        var r = 0;
        for(var i=0;i<str.length;i++){
           if(str.charCodeAt(i)>=255){
            r = r + 2;
           }
           else{
            r = r + 1;
           }
        }
        return r;
    },
    LenStr:function(p,l){
        var r = "";
        for(var i=0;i<l;i++){
            r = r + p;
        }
        return r;
    }
};

Dingtai.Const = function(name){
    var ajaxRoot = "/AJaxPage/";
    switch (name) {
        case "cookiename":
            return ".vpod.com";
        case "index":
            return "/html/default.htm";    
        case "LoginPage":
            return ajaxRoot + "Login.aspx";
        case "authentPage":
            return ajaxRoot + "Authenticate.aspx";  
        case "searchPage":                 
            return ajaxRoot + "Search.aspx";  
        case "registerPage":
            return ajaxRoot + "Regsiter.aspx";  
        case "playerPage":
            return ajaxRoot + "PlayerHandler.aspx";  
        case "commentPage":
            return ajaxRoot + "CommentHandler.aspx";  
        case "submitcommentPage":
            return ajaxRoot + "SubmitCommentHandler.aspx";  
        case "configPage":
            return ajaxRoot + "GetConfig.aspx";  
        case "common_Type":
            return "type";
        case "search_hdmedia" :
            return "hdmedia";
        case "search_media" :
            return "media";
        case "search_user" :
            return "user";
        case "search_type" :
            return "searchType";
         case "search_val" :
            return "q";
        case "search_pageCount" :
            return "pageCount";
        case "search_pageSize" :
            return "pageSize";
        case "search_page" :
            return "page";
    }
};

//Dingtai.Page Namespace
Dingtai.Page = {};
Dingtai.Page.getPagePath =function (id,type) {
    switch (type.toLocaleString().toLowerCase()) {
        case "userhome" :
//            return "/html/user/UserHome_" + id + "00.htm";
               return "/My/Member.aspx";
        case "hdmedia":
            return "/html/hdplayer/HDPlayer_" + id + ".htm";
    }
};
Dingtai.Page.getCookie = function (name) {
    var cookie_start = document.cookie.indexOf(name);
    var cookie_end = document.cookie.indexOf(";", cookie_start);
    return cookie_start == -1 ? '' : unescape(document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length)));
};

Dingtai.Page.getCookieValue = function (objName){//获取指定名称的cookie的值
    var arrStr = document.cookie.split("; ");
    for(var i = 0;i < arrStr.length;i ++){
     var temp = arrStr[i].split("=");
     if(temp[0] == objName) return unescape(temp[1]);
    }
};

Dingtai.Page.setCookie = function (name,pwd) {
     var ms = 24*3600*1000;
     var date = new Date();
    date.setTime(date.getTime() + ms);
   var str = ""
    str = "username" + "=" + escape(name);
     str += "; expires=" + date.toGMTString();
     document.cookie = str;
    str = "pwd=" + escape(pwd);    
     str += "; expires=" + date.toGMTString();   
//    alert("setcookie: " + str);
    document.cookie = str;
};

Dingtai.Page.clearCookie = function() {   
    var date = new Date();  
    date.setTime(date.getTime() - 48*3600*1000);
    
    var a = document.cookie;
//    alert("clearCookie1: " + a);
    
    var str = "username=;"
    str += " expires=" + date.toGMTString();      
    document.cookie =str;
    str = "pwd=;"
    str += " expires=" + date.toGMTString();      
    document.cookie =str;
   
    document.cookie =str;
    a = document.cookie;
//    alert("clearCookie2: " + a);
};

//Dingtai.Page.Passport Namespace
Dingtai.Page.Passport = {};

Dingtai.Page.Passport.GetCookieInfor = function(fun) {
    var name = Dingtai.Page.getCookie(Dingtai.Const("cookiename"));
    if (name == undefined || name == null ){
        name = "";
    }
    var authentPage = Dingtai.Const("authentPage");  
    var a = document.cookie;
//    alert("GetCookieInfor : " + a);
    jQuery.getJSON(authentPage, { "cookie": name, "myrandom": Math.random() }, fun);
  
};

 

Dingtai.Page.Passport.Login = function (username,pwd,remember) {
    var loginPage = Dingtai.Const("LoginPage");

    jQuery.getJSON(loginPage, { "type": "login", "userName": username, "pwd": pwd, "myrandom": Math.random() }, function(data, textStatus) {   
    alert(data[0].msg);                          
        
        if (data[0].msg == "登录成功") {  
//            alert(remember);
            if(remember == true) { Dingtai.Page.setCookie(username,pwd); }
            else{   Dingtai.Page.clearCookie()  }
            document.location.href = '/html/default.htm'            
        }
        else{             
            //alert("登录失败!" + data[0].msg);
        }        
    });
};
Dingtai.Page.Passport.LoginCallback = function(data, textstatus) {

    if (data == null || data == undefined || data[0] == null || data[0] == undefined) { return; }

    var control = jQuery(".top_submenu_top a").eq(0);
    control.html(data[0].username);
    control.attr("href", Dingtai.Page.getPagePath(data[0].userID, "userHome"));

    control = jQuery(".top_submenu_top a").eq(1);
    control.hide();
    control = jQuery(".top_submenu_top span").eq(1);
    control.hide();

    control = jQuery(".top_submenu_top a").eq(2);
    control.attr("href", Dingtai.Page.getPagePath(data[0].userID, "userHome"));
    //alert(data[0].manager);
    if (data[0].manager != "False") {
        control = jQuery(".top_submenu_top a").eq(0);
        jQuery("<a />").attr("href", "/Management/default.aspx").attr("target", "_blank").html("进入后台管理").appendTo(jQuery("<span />").html("  ").insertAfter(control));
        jQuery("<span />").html("&nbsp;|&nbsp;").insertAfter(control);
    }

    control = jQuery(".top_submenu_top a").last();
    jQuery("<a />").attr("href", "javascript:Dingtai.Page.Passport.Logout();").attr("onclick", "").html("退出").appendTo(jQuery("<span />").html("  ").insertAfter(control));
    jQuery("<span />").html("&nbsp;|&nbsp;").insertAfter(control);
};
 
 Dingtai.Page.Passport.LoginVerified = function() {
         Dingtai.Page.Passport.GetCookieInfor(Dingtai.Page.Passport.LoginCallback);
 };

Dingtai.Page.Passport.Logout = function (){
//    alert("aaa");
    var loginPage = Dingtai.Const("LoginPage");
    jQuery.getJSON(loginPage, { "type": "logout", "myrandom": Math.random() }, function(data, textStatus) {
        if (data[0].msg = "成功退出")  {
//            alert(data[0].msg);
            document.location.reload();
        }
    });
};


Dingtai.Page.Passport.ForgetPassword = function (username,email) {
    var loginPage = Dingtai.Const("LoginPage");
    jQuery.getJSON(loginPage, { "type": "forgetpassword", "userName": username, "email": email, "myrandom": Math.random() }, function(data, textStatus) {   
//      alert(data[0].msg);                          
        if (data[0].msg == "True") {  
            alert("您的密码已经发送到您的安全邮箱中");      
        }
        else{
            alert(data[0].msg);
        }        
    });
};

//--------------------------Register----------------------------------

Dingtai.Page.Passport.RegsiterVeritifyCallBack = function (data,textstatus) {
//    alert(data[0].msg);
    jQuery(".txt ul li").eq(0).html("<b style='color:red'>" + data[0].msg + "</b>");
};

Dingtai.Page.Passport.RegsiterVeritify = function(event) {
    var type = event.data.type[0].name;
    var value = event.data.type[0].value;
    var i = event.data.num;
//   alert(i);
    var regPage = Dingtai.Const("registerPage");
    var strJSON = "{\"" + Dingtai.Const("common_Type") + "\":\"" + type + "\" , \"" + type + "\":\"" + value + "\" , \"myrandom\":\"" + Math.random() + "\"}";      
    
    switch(type){       
        case "Password2":
            strJSON = "{\"" + Dingtai.Const("common_Type") + "\":\"" + type + "\" , \"" + type + "\":\"" + value + "\" , \"" + event.data.pwd[0].name + "\":\"" + event.data.pwd[0].value + "\" , \"myrandom\":\"" + Math.random() + "\"}";      
    }       
    
          
    strJSON = jQuery.parseJSON(strJSON);
   
    jQuery.getJSON(regPage, strJSON ,function (data,textstatus) {

       var color = "red";
       if(data[0].status.toLowerCase() == "true") { color = "green"; }
       
        jQuery(".txt ul li").eq(i).html("<b style='color:" + color + "'>" + data[0].msg + "</b>");
    });
    jQuery("#logo").ajaxError(function(event,request, settings){
        alert("Error: " + event.toString() + "  " + settings.toString());
    });
    return;
    switch(type){
        case "UserName":
    
        case "Password":        
            var reg = new RegExp(".{6,32}");            
            var r = value.match(reg);            
            if(value!=r) { alert("密码格式错误!"); }
            return;
        case "Password2":
            var reg = new RegExp(".{6,32}");
            var r = value.match(reg);            
            if(value!=r) { alert("重复密码格式错误!"); }
//            alert(event.data.type.eq(0).parent().prev("li").find("input")[0].value);
            var pwd = event.data.type.eq(0).parent().prev("li").find("input")[0].value;
            if(pwd != value) { alert("二次密码输入不一致!");  }
    }         
    
};
Dingtai.Page.Passport.DefaultInfo = function(event){
    var i = event.data.num;
  
    switch (i) {
        case 0:
            event.data.type.eq(0).html("由英文、数字以及下划线或汉字组成,长度在 4-32 位之间<br />例如: dingtai、dingtai_biz或鼎太网络 ")
            return; 
        case 1:
            event.data.type.eq(0).html("由英文、数字以及汉字组成,长度 1-16 位.<br />例如: 鼎太网络 ")             
            return; 
        case 2:
            event.data.type.eq(0).html("6-32 个非汉字字符,区分大小写,为提高安全性,建议除英文字母外<br />至少应包含1个或更多数字或特殊字符.例如: dingtai_888 ")
            return; 
        case 3:
            event.data.type.eq(0).html("必须与上面的密码输入一致 ")
            return; 
        case 4:
            event.data.type.eq(0).html("4-32 个任意字符")
            return; 
        case 5:
            event.data.type.eq(0).html("4-32 个任意字符")
            return; 
        case 6:
            event.data.type.eq(0).html("在找回密码时,将根据电子邮件地址获取密码提示答案")
            return;             
    }
};

Dingtai.Page.Passport.RegsiterCallBack =  function (data,textstatus){
    alert(data[0].msg);
    if (data[0].msg == "注册成功"){
        document.location.href = "/html/Default.htm";
    }
};

Dingtai.Page.Passport.Register = function (){
    var reg = jQuery(".Register_left .text input");
    var span = jQuery(".Register_left .text span");
//    alert(reg[0].name);
        
    var UserName = reg[0].value;
    var Nick = reg[1].value;
    var Password = reg[2].value;
    var Password2 = reg[3].value;
    var PasswordQuestion = reg[4].value;
    var PasswordAnswer = reg[5].value;
    var Email = reg[6].value;
    var checked = reg[7].checked;
    if(!checked) {
        alert("请阅读并同意服务条款!");
        return;
    }
//    alert(jQuery(".Register_left .text span")[0].innerHTML.replace("：",""));

    //Verifity Form
//    if (Password !=  Password2) { alert("二次密码输入不一致!"); }
    
    var strJSON = "{"
    strJSON = strJSON + "\""+ Dingtai.Const("common_Type") +"\":\"register\""; 
    for( var i = 0 ; i<reg.length ;i++){
        if(reg[i].name == "" ){ continue;}
        if (i != reg.length-1 ) { strJSON = strJSON + ","; }        
        strJSON = strJSON +"\""+ reg[i].name +"\":\""+reg[i].value +"\"";        
    }
    strJSON = strJSON + " , \"myrandom\":\"" + Math.random() + "\"}"; 
//    alert(strJSON);
//    return;
    strJSON = jQuery.parseJSON(strJSON);


    var regPage = Dingtai.Const("registerPage");       
    jQuery.getJSON(regPage, strJSON , Dingtai.Page.Passport.RegsiterCallBack)
    jQuery("#logo").ajaxError(function(event,request, settings){
        alert("Error: " + event.toString() + "  " + settings.toString());
        
    });
};


//-------------------------- Scripts ----------------------------------
//Dingtai.Scripts 命名空间
Dingtai.Scripts = {};

Dingtai.Scripts.StringsFilter = function(sender,args){

    var configPage = Dingtai.Const("configPage");
    jQuery.getJSON(configPage, { config: "Strings", "myrandom": Math.random() }, function(data) {
        if (data == undefined || data== null || data.length == 0 ) {return;}       
        var strs = "";
        strs = data[0].value;   
        if (strs == undefined || strs == "" || strs.indexOf(",") < 1) {return;}
        
        var ss = strs.split(",");
        for(var i=0;i<ss.length;i++){		
            var reg = new RegExp(ss[i],"ig");            
            var len = Dingtai.Scripts.StringsFilter._LenStr("*",Dingtai.Scripts.StringsFilter._AscLength(ss[i]));
            Dingtai.Scripts.StringsFilter._do(document.getElementsByTagName('a'),reg,len);
            Dingtai.Scripts.StringsFilter._do(document.getElementsByTagName('span'),reg,len);
            Dingtai.Scripts.StringsFilter._do(document.getElementsByTagName('label'),reg,len);

            document.title = document.title.replace(reg,len);
        }
    });
    
    
};
Dingtai.Scripts.StringsFilter._do = function(els,reg,len){
    for(var i = 0; i < els.length; i++){      
        if (els[i].innerHTML == "") continue;      
         els[i].innerHTML = els[i].innerHTML.replace(reg,len);
    }
};
Dingtai.Scripts.StringsFilter._do2 = function(obj,reg,len) {
    obj = obj.replace(reg,len);
};
Dingtai.Scripts.StringsFilter._AscLength = function(str){
		var r = 0;
        for(var i=0;i<str.length;i++){
           if(str.charCodeAt(i)>=255){
            r = r + 2;
           }
           else{
            r = r + 1;
           }
        }
        return r;
};
Dingtai.Scripts.StringsFilter._LenStr = function(p,l){
		var r = "";
        for(var i=0;i<l;i++){
            r = r + p;
        }
        return r;
};