function include(jsfile){
     var _sctag = document.createElement("script");
     _sctag.type = "text/javascript";
     _sctag.src = jsfile;
     document.getElementsByTagName('head')[0].appendChild(_sctag);
}
include("/TLimages/ctocio/js/outputcookies.js");

var div = document.getElementById('container');


var handleSuccess = function(o) {
    if (o.responseText !== undefined) {
        div.innerHTML = "<li>Transaction id: " + o.tId + "</li>";
        div.innerHTML += "<li>HTTP status: " + o.status + "</li>";
        div.innerHTML += "<li>Status code message: " + o.statusText + "</li>";
        div.innerHTML += "<li>HTTP headers: <ul>" + o.getAllResponseHeaders + "</ul></li>";
        div.innerHTML += "<li>Server response: " + o.responseText + "</li>";
        div.innerHTML += "<li>Argument object: Object ( [foo] => " + o.argument.foo +
                         " [bar] => " + o.argument.bar + " )</li>";
    }
}

var handleFailure = function(o) {
    if (o.responseText !== undefined) {
        div.innerHTML = "<li>Transaction id: " + o.tId + "</li>";
        div.innerHTML += "<li>HTTP status: " + o.status + "</li>";
        div.innerHTML += "<li>Status code message: " + o.statusText + "</li>";
    }
}
var callback =
{
    success:handleSuccess,
    failure: handleFailure,
    argument: { foo:"foo", bar:"bar" }
};

var callyoyback = {
    onSuccess:handyoySuccess,
    onFailure:handyoyFailure
};
var handyoySuccess = function(o) {
    if (o.responseText !== undefined) {
    }
}

var handyoyFailure = function(o) {
    if (o.responseText !== undefined) {
        alert("提取文章失败!");
    }
};

function loadLoginHtml(){
    var loginUrl = "http://" + document.location.host  + "/TLimages/ctocio/html/cc_login.html";
    var request = YAHOO.util.Connect.asyncRequest('POST', loginUrl, callartback);
    
}
//登陆 passport 验证 并获取文章
function loginajax() {
    var load = document.getElementById("swzxt");
    //load.innerHTML="正在加载正文......."
    var handloginSuccess = function(o) {
        /*  var test="http://192.168.1.40:8080/ctocio/feedback.jsp";
       var feelback=YAHOO.util.Connect.asyncRequest('GET',test,callyoyback);*/
        if (ret == 1) {
            getArticle();
        } else {
            alert("用户名或密码错误！");
            //var checkloginajax = document.getElementById("checkloginajax");
            //checkloginajax.innerHTML = "<font color = 'red'>" + "登陆失败!" + "</font>";
        }
    };
    var handloginFailure = function(o) {
        if (o.responseText !== undefined) {
            alert("提取文章失败!");
        }
    };
    /*var loginurl="http://passport.ctocio.com.cn/cas/login?username="+document.getElementById("uname").value+"&password="+document.getElementById("pwd").value
+"&service=" + document.getElementById("service").value + "&furl=" + document.getElementById("furl").value + "&remember=true";*/

    var loginurl = "http://passport.ctocio.com.cn/cas/login?username=" + form1.username.value + "&password=" + form1.password.value
            + "&service=" + form1.service.value + "&furl=" + form1.furl.value;

   /* if (form1.remember.checked) {
        loginurl = "http://passport.ctocio.com.cn/cas/login?username=" + form1.username.value + "&password=" + form1.password.value
                + "&service=" + form1.service.value + "&furl=" + form1.furl.value + "&remember=true";
    }*/
    var request = YAHOO.util.Get.script(loginurl, {
        onSuccess: handloginSuccess,
        onFailure: handloginFailure
    });
}


//var artUrl="http://winsystem.ctocio.com.cn/art/345/8641845_1.html";
var handartSuccess = function(o) {
    if (o.responseText !== undefined) {
        var swzxt = window.top.document.getElementById("swzxt");
        swzxt.innerHTML = o.responseText;
    }
}
var handartFailure = function(o) {
    if (o.responseText !== undefined) {
        var checklogin = parent.document.getElementById("checklogin");
        checklogin.value = "提取文章失败！";
    }
}

var callartback = {
    success:handartSuccess,
    failure:handartFailure
}
//读取文章
window.getArticle = function() {
    var testurl = getarturl();
    /*"http://database.ctocio.com.cn/art/318/8662818.html"*/
    var request = YAHOO.util.Connect.asyncRequest('POST', testurl, callartback);
    var wpath = location.pathname;
    var wename = "";
    reg = /\/(\w+)\/(\d+)\/(\S+)/;
    if(reg.test(wpath)) {
        reg.exec(wpath);
        wename = RegExp.$1;
    }
    if(typeof(atitle) == "undefined")
        atitle = document.title.split('_')[0];
    testurl = "http://check.ctocio.com.cn/ttapps/counter.jsp?customerid="+window.customerid+"&aid="+aid+"&title="+atitle;
    YAHOO.util.Get.script(testurl, null);
}

//验证 用户是否登陆
function IndexDemo() {
    //document.domain="ctocio.com.cn";
    var aCookie=document.cookie.split(",");
    window.customerid = uidCookie();
    if(window.customerid != null && window.customerid != ''){
        getArticle();
    }else{
        //cookie 中不包含 ctocio，则读取登录界面
        loadLoginHtml();
    }
}
//获取文章地址
function getarturl() {
    var url = location.href;
	var re = new RegExp("\\d+(?:_(\\d+))?.shtml$","ig")
    var arr = re.exec(url);
    var page = 1;
    if(arr && RegExp.$1){
   		page = parseInt(RegExp.$1) + 1;
   	}

    return  "http://"+document.location.host + "/" + "art" + "/" + ( aid % 500) + "/" + aid + "_" + page + ".html";
}


//找回密码
var handleSuccessemail = function(o) {
    //var checkmaildiv = document.getElementById("checkmail");
    if (ret == 0) {
        alert("密码确认邮件已经发送！");
        //checkmaildiv.innerHTML = "<font color='red'>" + "密码确认邮件已经发送到" + "</font>";
    }
    if (ret == 1) {
        alert("密码确认邮件发送失败！请您联系管理员");
        //checkmaildiv.innerHTML = "<font color='red'>" + "密码确认邮件未发送成功，请您联系管理员" + "</font>";
    }
    if (ret == 2) {
        alert("对不起，您的email地址未在本网站注册！");
        //checkmaildiv.innerHTML = "<font color='red'>" + "对不起，您的email地址未在本网站注册。" + "</font>";
    }
};

var handleFailuremail = function(o) {
    var checkmaildiv = document.getElementById("checkmail");
    if (o.responseText !== undefined) {
        checkmaildiv.innerHTML = "<font color='red'>" + "发生错误，请于管理员联系！" + "</font>";
    }
};
/*var callbackemail =
{
    success:handleSuccessemail,
    failure: handleFailuremail
};*/

function checkmail() {
    var email = document.form2.email;
    if (/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email.value)) {
        var checkmailurl = "http://check.ctocio.com.cn/ctocio/doubleCheckMail.jsp?email=" + email.value;
        var request = YAHOO.util.Get.script(checkmailurl, {onSuccess:handleSuccessemail,onFailure: handleFailuremail});
    } else {
        alert("格式错误！请输入正确的Email。");
        //var checkmaildiv = document.getElementById("checkmail");
        //checkmaildiv.innerHTML = "<font color='red'>" + "请输入正确的Email!" + "</font>";
    }
}
YAHOO.util.Event.onDOMReady(IndexDemo,null, false);

