/* 使用网站自带登录栏目配置单点登录时用的专用js，发布于20130609 by hzl */
ppltrim = function(s){
    if(typeof(s) == 'string'){
        return s.replace(/(^\s*)|(\s*$)/g, '');
    }    
    return s;
}

pplgettextvalue = function(id, defaultValue) {

    var value;

    try {
        var obj = document.getElementById(id);
        value = obj.value;
    }
    catch (e) {
        value = defaultValue;
    }
    return value;
}

pplgetcheckvalue = function(id, defaultValue) {

    var value;

    try {
        var obj = document.getElementById(id);
        value = obj.checked;
    }
    catch (e) {
        value = defaultValue;
    }

    return value;
}


pplsetcookie = function(name, value) {
    var argv = arguments;
    var argc = arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : '/';
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    document.cookie = name + "=" + escape(value) +
            ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
            ((path == null) ? "" : ("; path=" + path)) +
            ((domain == null) ? "" : ("; domain=" + domain)) +
            ((secure == true) ? "; secure" : "");
            ("; HttpOnly");
};

pplgetcookie = function(name) {

    var value = document.cookie.match('(?:^|;)\\s*' + name + '=([^;]*)');
    return value ? unescape(value[1]) : '';
};

pplclearcookie = function(name) {

    if (pplgetcookie(name)) {
        var expdate = new Date();
        expdate.setTime(expdate.getTime() - (86400 * 1000 * 1));
        pplsetcookie(name, "", expdate);
    }
};


//检测是否登录，获取Ticet
pplcheck = function () 
{
    ////验证是否登录成功，如果成功则返回Ticket。
    //document.write("<script language=\"javascript\" type=\"text/javascript\" src=\"" + p_passporturl + "/scriptLogin?action=check&server="+p_serverid+"&checkServerType=5&callback=true&charset=utf-8&v=" + new Date().getTime() + "\"></scr" + "ipt>");
    //验证是否登录成功，如果成功则返回用户信息。
    document.write("<script language=\"javascript\" type=\"text/javascript\" src=\"" + p_passporturl + "/scriptLogin?action=check&server="+p_serverid+"&checkServerType=5&charset=utf-8&v=" + new Date().getTime() + "\"></scr" + "ipt>");
	//上行执行后，passport会返回 isLogined 值，接着ppllogininit()就可以执行了
	document.write("<script language=\"javascript\" type=\"text/javascript\" >ppllogininit()</scr" + "ipt>");
};

////登录成功返回Ticke后再获取用户信息
//scriptVerify = function (ticket,auth,context) 
//{
//    //判断是否登录
//    if (isLogined) 
//    {
//        //获取用户信息
//	    document.write("<script language=\"javascript\" type=\"text/javascript\" src=\"" + p_passporturl + "/scriptLogin?action=check&server="+p_serverid+"&checkServerType=5&charset=utf-8&v=" + new Date().getTime() + "\"></scr" + "ipt>");
//	    document.write("<script language=\"javascript\" type=\"text/javascript\" >ppllogininit()</scr" + "ipt>");
//	    
//    }
//};

//从passport检测完后执行的方法
ppllogininit = function () 
{
    //判断是否登录
    if (isLogined) 
    {
	    p_username = account.realName;    //Passport的用户昵称、名字
        //p_username = account.nickname;  //Passport的用户账号
        pp_init();
    }
    else {
        // 自动登录
        
//        var auto_login = pplgetcookie('__redirect');
//        if (auto_login) {

//            var auto_login_time = parseInt(auto_login, 10);
//            if (!auto_login_time) {
//                auto_login_time = 2000;
//            }

//            setTimeout(function() {

//                var userName = pplgetcookie('__userName');
//                var password = pplgetcookie('__password');
//                passportset(userName, password, '', true);

//            }, auto_login_time);
//        }
//        else
//        {
//            
//        }
    }
};
