
var gddress = window.location.href;
gddress = gddress.replace("http://","").split("/")[0];

var oldurladdress = window.location.href;
oldurladdress = oldurladdress.split("?")[0];
if(oldurladdress.split(".html")[1]!=null){
	oldurladdress="http://"+gddress+"/index.htm"
}
if(oldurladdress="http://"+gddress+"/")
	oldurladdress="http://"+gddress+"/index.htm"


//全局变量
var debug = true;
//用户模型
var code = "0";
var regcode="6";//默认未知
//注意内部数据以$开始
function CookieUser(){
	this.$cookie = new Cookie(document,"gkcx_eol_cn_user",null,"/",".eol.cn",false);
	this.$userid="";
	this.$username="";
	this.$password="";
	this.$manage=-1;
	this.$html2link=0;
	this.$roomId="";
	this.$nickname="";
	this.$lastTime="";
	this.$area=6;
	//this.$style="gkcx_2009_bule";
	if(this.$cookie.load()){
		this.$userid=this.$cookie.userid;
		this.$username=this.$cookie.username;
		this.$manage=this.$cookie.manage;
		this.$roomId=this.$cookie.roomId;
		this.$html2link=this.$cookie.html2link;
		this.$nickname=getCookieNickname("gkcx_eol_cn_user");
		this.$lastTime=this.$cookie.lastTime;
		this.$area=this.$cookie.area;
		//this.$style=this.$cookie.style;
		
	}
	
	this.$userMenu='<form style="margin-top:2px;margin-bottom:10px;" id="ssologinForm"  name="ssologinForm" method="post" action="http://reg.eol.cn/interface/sublogin.php">\
						<div class="lin_right_title"><span class="z_333_12"><strong>教育在线社区登录</strong></span></div> \
						<p class="z_333_12">通行证：<input name="email" id="username" type="text" class="lin_right_input" style="font-size:11px" value="Email地址" onfocus="this.select()" onblur="if (value ==\'\'){value=\'Email地址\'}" onclick="if(value==\'Email地址\'){value=\'\'}" onmouseover="this.focus()" />　<INPUT name="image" type="image" src="/images/login.gif" align="absmiddle"/>\
						</p>\
						<p class="z_333_12">密　码：<input name="passwd" id="password" type="password" class="lin_right_input" />　<a href="http://reg.eol.cn/register.php?url=http://"+gddress+"/" class="z_org_12l">我要注册</a></p>\
						<input  name="subcode" id="subcode" type="hidden" value="1002"/>\
						<input type="hidden" name="from" value="http://"+gddress+"/userpages/indexLogin.jsf" />\
						<input type="hidden" name="type" value="1" />\
						<input type="hidden" name="_acegi_security_remember_me" value="true" />\
					</form>';
	
}




CookieUser.prototype.store = function(){
	this.$cookie.username = "";	
	this.$cookie.nickname =	"";
	this.$cookie.manage = -1;
	this.$cookie.roomId = "";
	this.$cookie.html2link = 0;
	this.$cookie.lastTime = "";
	this.$cookie.area = 6;
	this.$cookie.store();
}





//显示用户登陆后的菜单
CookieUser.prototype.showLoginMenu=function(){
	var menu="";
	if(this.$manage=='1'){
		/*
		this.$userMenu='欢迎你，'
						+'<font style="color:red">'+ this.$nickname +'</font><br>'
						+'<br>登录日期：'+ this.$lastTime +'<br>'
						+'<br><a href="http://i.eol.cn/" class="blue" style="font-weight:bold;padding-left:30px;">>> 进入个人中心</a><br><br><br>'
						+'<p><a href="#" class="blue" style="font-size:12px;">使用帮助</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
						+'<a href="http://reg.eol.cn/interface/sublogout.php?from=http://gkcx.eol.cn/userpages/logout.jsf" class="blue" style="font-size:12px;">退出登录</a></p>';
		*/
		this.$userMenu='<div class="lin_right_title"><span class="z_333_12"><strong>教育在线社区登录</strong></span></div>' 
					  +'<div class="login_right_01">你好，<a href="http://i.eol.cn/"><strong>'+ this.$nickname +'</strong></a></div>'
					  +'<div class="login_right_02">'
						  +'<input name="image2" type="image" src="images/s_xiugai.gif" align="absmiddle"/>&nbsp;'
						  +'<INPUT name="image" type="image" src="images/exit.gif" align="absmiddle"/>'
					  +'</div>'
					  +'<div class="no_way" ></div>'
					  +'<p><a href="#" class="z_666_12l">订阅资讯</a>(5)&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" class="z_666_12l">关注高校</a>(10)&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" class="z_666_12l">加入群组</a>(10)</p>';
	}else if(this.$manage=='0'){
		this.$userMenu='冻结用户!'
						+ this.$nickname
						+'<p><a href="#" class="blue" style="font-size:12px;">使用帮助</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
						+'<a href="http://reg.eol.cn/interface/sublogout.php?from=http://gkcx.eol.cn/userpages/logout.jsf" class="blue" style="font-size:12px;">退出登录</a></p>';
	}
	$('userMenu').innerHTML = this.$userMenu;
	
	
}

var user = new CookieUser();
//alert("manage: "+user.$manage+" ,   html2link: "+user.$html2link);




function CookieStyle(){
	this.$scookie = new Cookie(document,"gkcx_eol_cn_style",null,"/",".eol.cn",false);
	this.$style="gkcx_2009_blue";
	if(this.$scookie.load()){
		this.$style=this.$scookie.style;
	}
}

CookieStyle.prototype.store = function(){
	this.$scookie.style = this.$style;
	this.$scookie.store();
}



function setStyle(title) {
	   //预定义变量
	   var i, links;
	   //用DOM方法获得所有的link元素
	   links = document.getElementsByTagName("link");
	   for(i=0; links[i]; i++) {
		    //判断此link元素的rel属性中是否有style关键字
		    //即此link元素是否为样式表link
		    //同时判断此link元素是否含有title属性
		    if(links[i].getAttribute("rel").indexOf("style") != -1 && links[i].getAttribute("title")) {
		         //先设为disabled
		         links[i].disabled = true;
		         //再判断它的title中是否有指定的关键字
		         if(links[i].getAttribute("title").indexOf(title) != -1){
			         //如果有则将其激活
			         links[i].disabled = false;
			         gkcxStyle.$style = title;
			         gkcxStyle.store();
			     }
	        }
	   }
}

function getStyle() {
	   //预定义变量
	   var i, links;
	   var title = (getCookie("gkcx_eol_cn_style").split(":"))[1];
	   if(typeof(title)=='undefined')
	   		title = "gkcx_2009_blue";
	   //用DOM方法获得所有的link元素
	   links = document.getElementsByTagName("link");
	   for(i=0; i<links.length; i++) {
		    //判断此link元素的rel属性中是否有style关键字
		    //即此link元素是否为样式表link
		    //同时判断此link元素是否含有title属性
		    if(links[i].getAttribute("rel").indexOf("style") != -1 && links[i].getAttribute("title")) {
		         //先设为disabled
		         links[i].disabled = true;
		         //再判断它的title中是否有指定的关键字
		         if(links[i].getAttribute("title").indexOf(title) != -1 || links[i].getAttribute("title") == title){
			         //如果有则将其激活
			         links[i].disabled = false;
			     }
	        }
	   }
}


var gkcxStyle = new CookieStyle();
getStyle();




function setSortSelect(e,selement){
	var xml = e.responseXML;
	var root = xml.getElementsByTagName('sorts')[0];
	var sorts = root.getElementsByTagName('sort');
	for(var i=0;i<sorts.length;i++){
		var sort = sorts[i];
		selement.options[selement.options.length]=new Option(sort.getElementsByTagName('name').item(0).firstChild.nodeValue,sort.getElementsByTagName('id').item(0).firstChild.nodeValue);
		if(sort && sort.getElementsByTagName('subsorts')){
			var subsorts = sort.getElementsByTagName('subsorts')[0].getElementsByTagName('subsort');
			if(subsorts){
				for(var j=0;j<subsorts.length;j++){
					var ss = subsorts[j];
					if(ss){
						var optionElement = document.createElement("option");
						optionElement.value = ss.getElementsByTagName('id').item(0).firstChild.nodeValue;
						optionElement.innerHTML = ' |-'+ss.getElementsByTagName('name').item(0).firstChild.nodeValue;
						selement.appendChild(optionElement);
					}
						
					//element.options[element.options.length]=new Option(' |-'+ss.getElementsByTagName('name').item(0).firstChild.nodeValue,ss.getElementsByTagName('id').item(0).firstChild.nodeValue);
				}							
			}
		}
	}
}




function login(){
	var myAjax = new Ajax.Request('/login.do',{method: 'post',parameters: Form.serialize('loginForm'),onComplete: checklogin,asynchronous:false});
	
}

function ssologin(){
	$('ssologinForm').submit();
}

function checklogin(e){
	code = e.responseText;
	
}
function closeInfo(){
	setTimeout(function() {Dialog.closeInfo()}, 500);
}
function openInfo(info){
	if(!info)
		info = '正在处理数据，请等待...';
	Dialog.info(info,{width: 200, height: 50, showProgress: true,destroyOnClose: true});
}




function showError(e,err){
	$(e).innerHTML=err; 
	$(e).show();
	if(Windows.focusedWindow)
		Windows.focusedWindow.updateHeight();
	return false;
}
function logout(){
	openInfo();
	user.store();	
	
	user.$manage = -1;
	closeInfo();
	//user = new CookieUser();
	user.showLoginMenu();
	return true;
}
function afterlogout(e){
	closeInfo();
	user=new CookieUser();
	user.showLoginMenu();
}


function getCookieNickname(cookie_name)
{
   
    var aCookie = document.cookie.split("; ");
    for (var i=0; i<aCookie.length; i++)
    {
       
        var aCrumb = aCookie[i].split("=");
        if (cookie_name == aCrumb[0])
        {
            var gcc = aCrumb[1].split(":");
            var nickname = (gcc[2].split("&"))[0];
			return decodeURI(nickname);
        }
    }
    return "gkcx cookie not found!";
}


function getCookie(cookie_name)
{
   
    var aCookie = document.cookie.split("; ");
    for (var i=0; i<aCookie.length; i++)
    {
       
        var aCrumb = aCookie[i].split("=");
        if (cookie_name == aCrumb[0])
        {
            return aCrumb[1];
        }
    }
    // a cookie with the requested name does not exist
    //return null;
    return "not found";
}



function getEolUserName(){
	return getCookie("eol_name");
}


function getSsoStatus(){
	if (getCookie('REG1002')=='Y')
		return true;
	else 
		return false;
}


function deleteCookie2(name,path,domain)
{
 document.cookie = name + "=" +
  ((path) ? "; path=" + path : "") +
  ((domain) ? "; domain=" + domain : "") +
  "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}


function getLoginBackUrl(){
	var oldhref= window.location.href;
		return oldhref.substring(0,8+oldhref.substring(8,oldhref.length).indexOf('/')) +'/j_acegi_eolsso_security_check';//?rurl='+encodeURIComponent(window.location.href));
}

//alert(getEolUserName()+"  "+user.$nickname);
//alert(getSsoStatus()+" "+user.$manage);


/*
// 调用函数

if (getSsoStatus() && user.$manage==-1) {
	user.$html2link = 1;
	window.location.href='http://reg.eol.cn/interface/sublink.php?type=2&from='+getLoginBackUrl();
	deleteCookie2('REG1002','/','.eol.cn');	
}else if(getSsoStatus() && user.$manage!=-1){
	window.location.href='http://reg.eol.cn/interface/sublink.php?type=2&from='+getLoginBackUrl();
	deleteCookie2('REG1002','/','.eol.cn');
}else if(getCookie('REG1001')=="not found" && user.$manage!=-1){
		window.location.href="http://reg.eol.cn/interface/sublogout.php?from=http://"+gddress+"/userpages/logout.jsf";
}
*/

