﻿var newId = 1;
var newId2 = 1;
var MaxCategoryName = 10;//分页标题长度。最大为10
var MaxCategoryNum = 8;//分页数。最大为8
var MaxUrlNum = 15;//网址数。最大为15

//数据库是否存在标题
var firstPageChecked = false;

//数据库是否存在用户
var userNameChecked = false;

//是否登陆成功
var loginSuccess = false;

var uploadStatus = false;

var timeHandle;

String.prototype.trim = function()
{ 
	return this.replace(/(^\s*)|(\s*$)/g, ""); 
};


//字符串是否为空
function isEmpty(str)
{
	str = str.replace(/\r\n/g,"");
	str = str.replace(/\r/g,"");
	str = str.replace(/\n/g,"");
	str = str.replace(/\s/g,"");
	if(str=="")
		return true;
	else
		return false;
}


//字符串是否合法首页网址
function isFirstPage(str)
{
	var RegEx =/^[A-Za-z0-9]{3,20}$/; 
	if(RegEx.test(str)) 
		return true;
	else
		return false; 	
}

function isUrl(str)
{
	var  RegEx = /(http\:\/\/)?([\w.]+)(\/[\w-   \.\/\?%&=]*)?/gi;
	if(RegEx.test(str)){
		return true;	
	}else{
		return false;	
	}
}

function $get(id){
	return document.getElementById(id);	
}

function getEvent(event){
	return event? event : window.event;
}

function timestamp(){
    var timestamp = Date.parse(new Date());
    return timestamp;
}
///////////
function setHomePage(homePage)
{
	if (document.all) {
		   document.body.style.behavior='url(#default#homepage)';
		   document.body.setHomePage(homePage);
	
		}
		else if (window.sidebar)
		{
			if(window.netscape)
			{
				 try
				 { 
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
				 } 
				 catch (e) 
				 { 
					alert("你的浏览器禁止了设置主页功能,如果你想开启这个功能,请在地址栏输入about:config ,然后把signed.applets.codebase_principal_support选项设置为true");
					return false;
				 }
			} 
			var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
			prefs.setCharPref('browser.startup.homepage',homePage);
	  }
	  return true;
}

var jsonObject = {
	
}

var jsonUtil = {
	
	setModify: function(modify){
		this.json.modify = modify;	
	},
	
	setJson : function(json){
		this.json = json;	
	},
	
	setRegister: function(register){
		this.json.register = register;	
	},
	
	setCurrentPage:function(page){
		if(page == null || page.trim() == ""){
			return;	
		}
		this.json.currentPage = page;	
	},
	
	setTitle: function(title){
		if(title == null || title.trim() == ""){
			return;	
		}
		this.json.pageTitle = title;	
		this.setModify(true);
	},
	
	setRemember: function(remember){
		this.json.remember = remember;	
	},
	
	//add by blueyb on 20090816
	setLogo: function(logo){
		this.json.logo = logo;
		this.setModify(true);
	},
	
	setFirstPage: function(firstPage){
		if(firstPage == null || firstPage.trim() == ""){
			return;	
		}
		this.json.firstPage = firstPage;
	},
	
	setUserName: function(userName){
		if(userName == null || userName.trim() == ""){
			return;	
		}
		this.json.name = userName;	
	},
	
	setPassword: function(password){
		if(password == null || password.trim() == ""){
			return;	
		}
		this.json.password = password;	
	},
	
	setSearch: function(name, on, num){
		if(name == "baidu"){
			this.json.baidu = {
				"name" : name,
				"on" : on,
				"number" : num
			}
		}else if(name == "gougou"){
			this.json.gougou = {
				"name" : name,
				"on" : on,
				"number" : num
			}
		}else if(name == "caifu"){
			this.json.caifu = {
				"name" : name,
				"on" : on,
				"number" : num
			}
		}else if(name == "taobao"){
			this.json.taobao = {
				"name" : name,
				"on" : on,
				"number" : num
			}
		}else if(name == "sogou"){
			this.json.sogou = {
				"name" : name,
				"on" : on,
				"number" : num
			}
		}else if(name == "youdao"){
			this.json.youdao = {
				"name" : name,
				"on" : on,
				"number" : num
			}
		}else if(name == "soso"){
			this.json.soso = {
				"name" : name,
				"on" : on,
				"number" : num
			}
		}else if(name == "yahoo"){
			this.json.yahoo = {
				"name" : name,
				"on" : on,
				"number" : num
			}
		}else if(name == "bing"){
			this.json.bing = {
				"name" : name,
				"on" : on,
				"number" : num
			}
		}else if(name == "google"){
			this.json.google = {
				"name" : name,
				"on" : on,
				"number" : num
			}
		}else{
			alert("名称错误");
		}
	},
	
	testCategoryAction : function (fullId, testAction){
		return this.json[fullId].action == testAction;
	},
	
	addCategory: function(fullId, name){
		this.json[fullId] = {"action" : "a", "name" : name};
		this.setModify(true);
	},
	
	delCategory: function(fullId){
		//如果是新增的则标记为null,否则就标记action为"d"
		if(this.testCategoryAction(fullId, "a")){
			this.json[fullId] = null;
		}else{
			var id = this.json[fullId].id;
			//重设
			this.json[fullId] = {"action": "d", "id": id};
		}
		this.setModify(true);
	},
	
	modifyCategory: function(fullId, name){
		//如果目录是新增的则直接修改属性name，否则要把action也标记为更新
		this.json[fullId].name = name;
		if(!this.testCategoryAction(fullId, "a")){
			this.json[fullId].action = "u";
		}
		this.setModify(true);
	},
	
	setCategory: function(fullId, id, name){
		this.json[fullId] = {"action": "n", "id": id, "name": name};
	},
	
	//////////////////链接操作///////////////
	testLinkAction : function (cfullId, lfullId, testAction){
		return this.json[cfullId][lfullId].action == testAction;
	},
	
	addLink: function(cfullId, lfullId, name, links, line, num){
		this.json[cfullId][lfullId] = {"action": "a", "name": name, "link": links, "line": line, "number": num};
		this.setModify(true);
	},
	
	delLink: function(cfullId, lfullId){
		//如果是新增的则标记为null,否则就标记action为"d"
		if(this.testLinkAction(cfullId, lfullId, "a")){
			this.json[cfullId][lfullId] = null;
		}else{
			var id = this.json[cfullId][lfullId].id;
			this.json[cfullId][lfullId] = {"action": "d", "id": id};
		}
		this.setModify(true);
	},
	
	modifyLink: function(cfullId, lfullId, name, links, num){
		//如果目录是新增的则直接修改属性，否则要把action也标记为更新
		this.json[cfullId][lfullId].name = name;
		this.json[cfullId][lfullId].link = links;
		this.json[cfullId][lfullId].number = num;
		if(!this.testLinkAction(cfullId, lfullId, "a")){
			this.json[cfullId][lfullId].action = "u";
		}
		this.setModify(true);
	},
	
	setLink: function(cfullId, lfullId, cId, lId, name, links, line, num){
		this.json[cfullId][lfullId] = {"action": "n", "categoryId": cId, "id": lId, "name": name, "link": links, "line": line, "number": num};
	}
}
jsonUtil.setJson(jsonObject);

function infoCollect(isReg){
	
	//当前页的网址
	jsonUtil.setCurrentPage($get("current_page").value);
	
	//标题
	jsonUtil.setTitle($get("input_title").value);
	//首页地址,用户名和密码
	if(isReg){
		jsonUtil.setRegister(true);
		jsonUtil.setFirstPage($get("input_firstPage").value);	
		jsonUtil.setUserName($get("input_name_two").value);
		jsonUtil.setPassword($get("input_password_two").value);
	}else{
		jsonUtil.setRegister(false);
		//		jsonUtil.setFirstPage($get("current_page").value);
		if($get("current_page").value==""){
			jsonUtil.setFirstPage(getCookie("firstPage"));
		}else{
			jsonUtil.setFirstPage($get("current_page").value);
		}
		jsonUtil.setUserName($get("input_name_one").value);
		jsonUtil.setPassword($get("input_password_one").value);
	}
	//搜索引擎
	var check_baidu = $get("check_baidu").checked? 1:0;
	var select_baidu = $get("select_baidu").value == "排序"? 1 : $get("select_baidu").value;
	jsonUtil.setSearch("baidu", check_baidu, select_baidu);
	
	var check_gougou = $get("check_gougou").checked? 1:0;
	var select_gougou = $get("select_gougou").value == "排序"? 1 : $get("select_gougou").value;
	jsonUtil.setSearch("gougou", check_gougou, select_gougou);

	var check_caifu = $get("check_caifu").checked? 1:0;
	var select_caifu = $get("select_caifu").value == "排序"? 1 : $get("select_caifu").value;
	jsonUtil.setSearch("caifu", check_caifu, select_caifu);

	var check_taobao = $get("check_taobao").checked? 1:0;
	var select_taobao = $get("select_taobao").value == "排序"? 1 : $get("select_taobao").value;
	jsonUtil.setSearch("taobao", check_taobao, select_taobao);

	var check_sogou = $get("check_sogou").checked? 1:0;
	var select_sogou = $get("select_sogou").value == "排序"? 1 : $get("select_sogou").value;
	jsonUtil.setSearch("sogou", check_sogou, select_sogou);

	var check_youdao = $get("check_youdao").checked? 1:0;
	var select_youdao = $get("select_youdao").value == "排序"? 1 : $get("select_youdao").value;
	jsonUtil.setSearch("youdao", check_youdao, select_youdao);

	var check_soso = $get("check_soso").checked? 1:0;
	var select_soso = $get("select_soso").value == "排序"? 1 : $get("select_soso").value;
	jsonUtil.setSearch("soso", check_soso, select_soso);

	var check_yahoo = $get("check_yahoo").checked? 1:0;
	var select_yahoo = $get("select_yahoo").value == "排序"? 1 : $get("select_yahoo").value;
	jsonUtil.setSearch("yahoo", check_yahoo, select_yahoo);

	var check_bing = $get("check_bing").checked? 1:0;
	var select_bing = $get("select_bing").value == "排序"? 1 : $get("select_bing").value;
	jsonUtil.setSearch("bing", check_bing, select_bing);
	
	var check_google = $get("check_google").checked? 1:0;
	var select_google = $get("select_google").value == "排序"? 1 : $get("select_google").value;
	jsonUtil.setSearch("google", check_google, select_google);
	
	if($get("remember").checked){
		jsonUtil.setRemember("true");	
	}else{
		jsonUtil.setRemember("false");
	}
}
function RunOnBeforeUnload()
{
    if(hasChange)
    {
        window.event.returnValue = '如果是,你将丢失没有保存的内容。'; 
    }
}

function getCookie(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]);
}
}

function displaybaidu(){
	var str = '<form action="http://www.baidu.com/s?" method="get" name="baidu" target="_blank">';
	str += '<table align="center" id="baidu" name="baidu" class="search_table" width="21%" border="0" cellspacing="0" cellpadding="2">';
        str += '<tr><td><span class="logo"><a href="http://www.baidu.com/" target="_blank"><img src="images/baidu_logo.png" width="72" height="23" /></a></span></td>';
        str += '<td><input name="ie" value="utf-8" type="hidden"></td>';
	str += '<td><input name="wd" class="inputbox-text" type="text" id=query value=""  size="42" maxlength="100"></td>';
	str += '<td><input id=sb class="input-submit" type="submit" value="百度一下"></td></tr></table>';
	str += '</form>';
	document.write(str);
}

function displaygougou(){
	var str = '<form name="gougou" action="http://web.gougou.com/search" method="get" target="_blank">';
	str += '<table align="center" id="gougou" name="gougou" class="search_table" width="21%" border="0" cellspacing="0" cellpadding="2">';
	str += '<tr><td><span class="logo"><a href="http://web.gougou.com/" target="_blank"><img src="images/gougou_logo.png" width="72" height="23" /></a></span></td>';
	str += '<td><input id="s" class="inputbox-text" type="text" value=""  name="search"/></td>';
	str += '<td><input id="" class="input-submit" type="submit" value="狗狗搜索" name=""/></td></tr></table>';
	str += '<input name="id" value="13327" type="hidden">';
	str += '</form>';
	document.write(str);
}

function displaycaifu(){
	var str = '<form action="http://niu.bi/search.php" method="get" name="caifu" target="_blank">';
	str += '<table align="center" id="caifu" name="caifu" class="search_table" width="21%" border="0" cellspacing="0" cellpadding="2">';
        str += '<tr><td><span class="logo"><a href="http://niu.bi/" target="_blank"><img src="images/niubi_logo.png" width="72" height="23" /></a></span></td>';
	str += '<td><input name="keyword" class="inputbox-text" type="text" id=kw value=""  size="42" maxlength="100"></td>';
	str += '<td><input id=sb class="input-submit" type="submit" value="牛逼一下"></td></tr></table>';
	str += '</tr><tr><td colspan="3"><p align="center">';
	str += '<select name="left">';
	str += '<option value="google">谷歌</option><option value="baidu"selected>百度</option><option value="bing">必应</option><option value="youdao">有道</option><option value="soso">搜搜</option><option value="gougou">狗狗</option><option value="yahoo">雅虎</option><option value="zhongsou">中搜</option>            </select> VS <select name="right">';
	str += '<option value="google"selected>谷歌</option><option value="baidu">百度</option><option value="bing">必应</option><option value="youdao">有道</option><option value="soso">搜搜</option><option value="gougou">狗狗</option><option value="yahoo">雅虎</option><option value="zhongsou">中搜</option>            </select>';
	str += '</form>';
	document.write(str);
}

function displaytaobao(){
	var str = '<table align="center" id="taobao" name="taobao" class="search_table" width="21%" border="0" cellspacing="2" cellpadding="2">'
	str += '<tr><td>'
	str += '<script type=\'text\/javascript\'>'
	str += 'alimama_pid=\'mm_10301706_0_0\';'
	str += 'alimama_type=\'g\';'
	str += 'alimama_tks={};'
	str += 'alimama_tks.style_i=1;'
	str += 'alimama_tks.lg_i=1;'
	str += 'alimama_tks.w_i=500;'
	str += 'alimama_tks.h_i=69;'
	str += 'alimama_tks.btn_i=1;'
	str += 'alimama_tks.txt_s=\'\';'
	str += 'alimama_tks.hot_i=1;'
	str += 'alimama_tks.hc_c=\'#999999\';'
	str += 'alimama_tks.c_i=1;'
	str += 'alimama_tks.cid_i=0;'
	str += '<\/script>'
	str += '<script type=\'text\/javascript\' src=\'http:\/\/a.alimama.cn\/inf.js\'><\/script>'
	str += '</td></tr></table>';
	document.write(str);
}

function displaysogou(){
	var str = '<form action="http://www.sogou.com/web?" method="get" name="sogou" target="_blank">';
	str += '<table align="center" id="sogou" name="sogou" class="search_table" width="21%" border="0" cellspacing="0" cellpadding="2">';
        str += '<tr><td><span class="logo"><a href="http://www.sogou.com/" target="_blank"><img src="images/sogou_logo.png" width="72" height="23" /></a></span></td>';
	str += '<td><input name="query" class="inputbox-text" type="text" id=query value=""  size="42" maxlength="100"></td>';
	str += '<td><input id=sb class="input-submit" type="submit" value="搜狗搜索"></td></tr></table>';
	str += '</form>';
	document.write(str);
}

function displayyoudao(){
	var str = '<form action="http://www.youdao.com/search?" method="get" name="youdao" target="_blank">';
	str += '<table align="center" id="youdao" name="youdao" class="search_table" width="21%" border="0" cellspacing="0" cellpadding="2">';
        str += '<tr><td><span class="logo"><a href="http://www.youdao.com/" target="_blank"><img src="images/youdao_logo.png" width="72" height="23" /></a></span></td>';
	str += '<td><input name="q" class="inputbox-text" type="text" id=query value=""  size="42" maxlength="100"></td>';
	str += '<td><input id=sb class="input-submit" type="submit" value="有道搜索"></td></tr></table>';
	str += '</form>';
	document.write(str);
}


function displaysoso(){
	var str = '<form action="http://www.soso.com/q?" method="get" name="soso" target="_blank">';
	str += '<table align="center" id="soso" name="soso" class="search_table" width="21%" border="0" cellspacing="0" cellpadding="2">';
        str += '<tr><td><span class="logo"><a href="http://www.soso.com/" target="_blank"><img src="images/soso_logo.png" width="72" height="23" /></a></span></td>';
        str += '<td><input name="ie" value="utf-8" type="hidden"></td>';
	str += '<td><input name="w" class="inputbox-text" type="text" id=query value=""  size="42" maxlength="100"></td>';
	str += '<td><input id=sb class="input-submit" type="submit" value="搜搜搜索"></td></tr></table>';
	str += '</form>';
	document.write(str);
}

function displayyahoo(){
	var str = '<form action="http://one.cn.yahoo.com/s?" method="get" name="yahoo" target="_blank">';
	str += '<table align="center" id="yahoo" name="yahoo" class="search_table" width="21%" border="0" cellspacing="0" cellpadding="2">';
        str += '<tr><td><span class="logo"><a href="http://www.yahoo.cn/" target="_blank"><img src="images/yahoo_logo.png" width="72" height="23" /></a></span></td>';
	str += '<td><input name="p" class="inputbox-text" type="text" id=query value=""  size="42" maxlength="100"></td>';
	str += '<td><input id=sb class="input-submit" type="submit" value="雅虎搜索"></td></tr></table>';
	str += '</form>';
	document.write(str);
}

function displaybing(){
	var str = '<form action="http://cn.bing.com/search?" method="get" name="bing" target="_blank">';
	str += '<table align="center" id="bing" name="bing" class="search_table" width="21%" border="0" cellspacing="0" cellpadding="2">';
    str += '<tr><td><span class="logo"><a href="http://cn.bing.com/" target="_blank"><img src="images/bing_logo.png" width="72" height="23" /></a></span></td>';
	str += '<td><input name="q" class="inputbox-text" type="text" id=query value=""  size="42" maxlength="100"></td>';
	str += '<td><input id=sb class="input-submit" type="submit" value="必应搜索"></td></tr></table>';
	str += '</form>';
	document.write(str);
}

function displaygoogle(){
	var str = '<form action="http://www.google.com.hk/search?" method="get" name="google" target="_blank">';
	str += '<table align="center" id="google" name="google" class="search_table" width="21%" border="0" cellspacing="0" cellpadding="2">';
    str += '<tr><td><span class="logo"><a href="http://www.google.com.hk/" target="_blank"><img src="images/google_logo.png" width="72" height="23" /></a></span></td>';
	str += '<td><input name="q" class="inputbox-text" type="text" id=query value=""  size="42" maxlength="100"></td>';
	str += '<td><input id=sb class="input-submit" type="submit" value="谷歌搜索"></td></tr></table>';
	str += '</form>';
	document.write(str);
}

function changeCategory(id, isnew){
	if(isnew == true || isnew == "true"){
		var cid = "cate_" + id;
	}else{
		var cid = "category_" + id;
	}
	var categorys = document.getElementsByName("cList");
	if(id == "first" && categorys.length > 0){
		for(var i =0 ; i < categorys.length; i++){
		var category = 	$get(categorys[i].value);
		if(i == categorys.length - 1){
			category.style.display = "block";	
		}else{
			category.style.display = "none";
		}
	}
	}else{
		for(var i =0 ; i < categorys.length; i++){
			var category = 	$get(categorys[i].value);
			if(category.id == cid){
				category.style.display = "block";
			}else{
				category.style.display = "none";
			}
		}
	}
}

function disabled(id, disabled){
	var d = true;
	if(disabled == 0){
		d = false;
	}
	$get(id).disabled = d;
}

function checked(id, checked){
	var c = true;
	if(checked == 0){
		c = false;
	}
	$get(id).checked = c;
}

function check(id){
	var checkbox = $get(id);
	var checked = !checkbox.checked;
	if(checkbox.id == "check_baidu"){
		disabled("select_baidu", checked);
	}else if(checkbox.id == "check_gougou"){
		disabled("select_gougou", checked);
	}else if(checkbox.id == "check_caifu"){
		disabled("select_caifu", checked);
	}else if(checkbox.id == "check_sogou"){
		disabled("select_sogou", checked);
	}else if(checkbox.id == "check_youdao"){
		disabled("select_youdao", checked);
	}else if(checkbox.id == "check_soso"){
		disabled("select_soso", checked);
	}else if(checkbox.id == "check_yahoo"){
		disabled("select_yahoo", checked);
	}else if(checkbox.id == "check_bing"){
		disabled("select_bing", checked);
	}else if(checkbox.id == "check_google"){
		disabled("select_google", checked);
	}
}

function selectOption(id, num){
	var option = $get(id);
	var optionList = option.getElementsByTagName("option");
	for(var i = 0; i < optionList.length ; i++){
		var option = optionList[i];
		if(option.value == num){
			option.selected = "selected";
			return;
		}
	}
}

function initSearchs(name, able, num){
	if(name == "baidu"){
		selectOption("select_baidu", num);
		checked("check_baidu", able);
		disabled("select_baidu", !able);
	}else if(name == "gougou"){
		selectOption("select_gougou", num);
		checked("check_gougou", able);
		disabled("select_gougou", !able);
	}else if(name == "caifu"){
		selectOption("select_caifu", num);
		checked("check_caifu", able);
		disabled("select_caifu", !able);
	}else if(name == "taobao"){
		selectOption("select_taobao", num);
		checked("check_taobao", able);
		disabled("select_taobao", !able);
	}else if(name == "sogou"){
		selectOption("select_sogou", num);
		checked("check_sogou", able);
		disabled("select_sogou", !able);
	}else if(name == "youdao"){
		selectOption("select_youdao", num);
		checked("check_youdao", able);
		disabled("select_youdao", !able);
	}else if(name == "soso"){
		selectOption("select_soso", num);
		checked("check_soso", able);
		disabled("select_soso", !able);
	}else if(name == "yahoo"){
		selectOption("select_yahoo", num);
		checked("check_yahoo", able);
		disabled("select_yahoo", !able);
	}else if(name == "bing"){
		selectOption("select_bing", num);
		checked("check_bing", able);
		disabled("select_bing", !able);
	}else if(name == "google"){
		selectOption("select_google", num);
		checked("check_google", able);
		disabled("select_google", !able);
	}else{
		alert("搜索引擎名字错误");	
	}
}

function recordCategory(id){
	var list = $get("category_list_hidden");
	var hide ;
	hide = "<input type=\"hidden\" name=\"cList\" value=\"cate_"  + id + "\" >";
	list.innerHTML += hide;
}

function removeRecord(id, isnew){
	if(isnew == true || isnew == "true"){
		var cid = "cate_" + id;
	}else{
		var cid = "category_" + id;
	}
	var categorys = document.getElementsByName("cList");
	for(var i =0 ; i < categorys.length; i++){
		var category = 	categorys[i];
		if(category.value == cid){
			category.parentNode.removeChild(category);
		}
	}
}

function addCategory(){
	//先判断分页是否到达上限
	var categorys = document.getElementsByName("cList");
	if(categorys.length >= MaxCategoryNum){
		alert("分页到达上限，不能创建新分页");
		return;
	}
	//用时间来做id
	var id = timestamp() + newId;
	recordCategory(id);
	createCategory(id);
}

function createCategory(id){
	
	//创建分类菜单
	var parent = $get("links_category_list");
	var categoryName = "新分类" + newId++;
	var category;
	category = "<div class=\"tab\" id=\"cate_list_" + id + "\" onclick=\"changeCategory('" + id + "', 'true')\"><a href=\"\" onclick=\"return false;\" id=\"cate_list_name_" + id +"\" >" + categoryName + "</a>";
	
	category += "<div class=\"tab_del\" style=\"top:3px; left:70px;\" >";
	
	category += "<a><img src=\"images/tab_del.gif\" alt=\"删除分类\" width=\"11\" height=\"11\" border=\"0\" onclick=\"delCategory(" + id + ", 'true')\"/></a></div>";
	category += "<div class=\"tab_cha\" style=\"top:15px; left:70px;\"><a><img src=\"images/tab_cha.gif\" alt=\"修改名称\" width=\"11\" height=\"11\" border=\"0\" onclick=\"modifyCategory(event,'" + id + "', 'true')\"/></a></div></div>";
	
	parent.innerHTML += category;
	
	//创建分类的链接
	var link_list = $get("links_list");
	var links;
	links = "<div class=\"links_box_m\" id=\"cate_" + id + "\" style=\"display:none\"><div class=\"links_bb\"><p class=\"l_t_info\">点击文字可修改链接</p>";
	links += "<div class=\"links\"><ul ><li>第1行</li><li id=\"cline_" + id + "_1\" value=\"0\"><a class=\"l_add\" href=\"\" onclick=\"addLink(event, 'cline_" + id + "_1'); return false;\">添加链接</a></li></ul></div>";
	
	links += "<div class=\"links\"><ul ><li>第2行</li><li id=\"cline_" + id + "_2\" value=\"0\"><a class=\"l_add\" href=\"\" onclick=\"addLink(event, 'cline_" + id + "_2'); return false;\">添加链接</a></li></ul></div>";

	links += "<div class=\"links\"><ul ><li>第3行</li><li id=\"cline_" + id + "_3\" value=\"0\"><a class=\"l_add\" href=\"\" onclick=\"addLink(event, 'cline_" + id + "_3'); return false;\">添加链接</a></li></ul></div>";

	links += "</div></div>";
	link_list.innerHTML += links;
	changeCategory(id, 'true');
	var cfullId = "cate_" + id;
	jsonUtil.addCategory(cfullId, categoryName);
//	alert(JSON.stringify(jsonObject));
}

function delCategory(id, isnew){
	if(!confirm("是删除分类?")){
		return false;
	}
	removeRecord(id, isnew);
	var categoryListId;
	var categoryId;
	if(isnew == "true" || isnew == true){
		categoryListId = "cate_list_" + id;
		categoryId = "cate_" + id;
	}else{
		categoryListId = "category_list_" + id;
		categoryId = "category_" + id;
	}
	//删除分类菜单项
	var menu = $get(categoryListId);
	menu.parentNode.removeChild(menu);
	//删除分类内容
	var content = $get(categoryId);
	content.parentNode.removeChild(content);
	changeCategory("first", 'true');
	jsonUtil.delCategory(categoryId);
//	alert(JSON.stringify(jsonObject));
}

function modifyCategory(event, id, isnew){
	if(isnew == true || isnew == "true"){
		var nameId = "cate_list_name_" + id; 	
	}else{
		var nameId = "category_list_name_" + id;
	}
	$get("modify_category_id").value = id;
	$get("modify_category_isnew").value = isnew;
	$get("modify_category_name").value = $get(nameId).innerHTML;
	var target = $get("modify_category_frame");
	//var e = getEvent(event);
	target.style.position = "absolute";
	//target.style.left = e.clientX  + "px";
	//target.style.top = e.clientY +  document.documentElement.scrollTop + "px";
	//target.style.display = "block";
	showSC("modify_category_frame");
}

function modifyCategory0(){
	var id = $get("modify_category_id").value;
	var name = $get("modify_category_name").value;
	name = name.trim();
	name = name.replace(/</g, "&lt;").replace(/>/g, "&gt;");
	if(name == null || name.trim() == ""){
		alert("分类名称不能为空");
		return;
	}
	name = name.trim();
	
	var isnew = $get("modify_category_isnew").getAttribute('value');
	if( isnew == "true" || isnew == true ){
		var cfullId = "cate_" + id;
		var cln = "cate_list_name_" + id;
	}else{
		var cfullId = "category_" + id;
		var cln = "category_list_name_" + id;
	}
	//修改网页上的分类名称
	$get(cln).innerHTML = name;
	//记录修改
	jsonUtil.modifyCategory(cfullId, name);
	closeModifyFrame();
//	alert(JSON.stringify(jsonObject));
}

function createLink(lfullId, name, links, line, num, isnewc){
	var str ;
	var aId = "a_" + lfullId;
	str = "<li id=\"" + lfullId + "\" >";
	str += "<div class=\"tab_del\" style=\"top:4px; left:0px;\">";
	str += "<a a href=\"\" onclick=\"return false;\"><img src=\"images/tab_del.gif\" alt=\"删除链接\" width=\"11\" height=\"11\" border=\"0\" onclick=\"delLink('" + lfullId + "', '" + line + "', '" + isnewc + "', 'true');return false;\" /></a>";
	str += "</div>";
	str += "<a id=\"a_" + lfullId + "\" name=\"" + name + "\" links=\"" + links + "\" num=\"" + num + "\"  href=\"\" onclick=\"modifyLink(event, '" + lfullId + "','" + isnewc + "', 'true', this);return false; \">" + name + "</a>";
	str += "</li>";
	return str;
}


function addLink(event, id){
	if($get(id).getAttribute('value') >= MaxUrlNum){
		alert("每行不能超过" + MaxUrlNum + "个链接");
		return;
	}
	var target = $get("add_link_frame");
	//var e = getEvent(event);
	target.style.position = "absolute";
	//target.style.left = e.clientX  + "px";
	//target.style.top = e.clientY +  document.documentElement.scrollTop + "px";
	$get("input_line_id").value= id;
	$get("input_link_link").value = "http://";
	$get("input_link_num").value = "0";
	//target.style.display = "block";
	showSC("add_link_frame");
}

function addLink0(){
	var fullId = $get("input_line_id").value;
	var idArray = fullId.split("_");
	var categoryId = idArray[1];
	var line = idArray[2];
	var linkId = timestamp() + newId2++;
	var lfullId = "li_" + categoryId + "_" + linkId;
	var name = $get("input_link_title").value;
	name = name.replace(/\||\;|\,|\'|\"/g,""); //去除,;|符号
	name = name.replace(/</g, "&lt;").replace(/>/g, "&gt;");
  if(isEmpty(name) || name.length<1)
  {
      alert("请输入标题");
      $get("input_link_title").focus();
      return false;
  }
	
	if(isEmpty(name) || name.length<1)
  {
      alert("请输入标题");
      $get("input_link_title").focus();
      return false;
  }

	var links = $get("input_link_link").value;
	links = links.replace(/⊙|◎|\'|\"/g,"");//去除⊙◎符号
	links = links.replace(/</g, "&lt;").replace(/>/g, "&gt;");
    if(isEmpty(links) || links.length<4)
    {
        alert("请输入网址");
        $get("input_link_link").focus();
        return false;
    }
  var num = $get("input_link_num").value;
  num = num.replace(/</g, "&lt;").replace(/>/g, "&gt;");
	if(num == null || num == ""){
		num = 0;
	}
	
	//添加到页面上
	var isnew = 'false';
	if(fullId.indexOf('c') == 0){
		isnew = 'true';	
	}
	var linkContent = createLink(lfullId, name, links, line, num, isnew);
	var line_list = $get(fullId);
	var ul = line_list.parentNode;
	ul.removeChild(line_list);
	ul.innerHTML += linkContent;
	ul.appendChild(line_list);
	//line_list.innerHTML = line_list.innerHTML;
	//更新行的个数
	var lineTarget = $get(fullId);
	var newCount = parseInt(lineTarget.getAttribute('value')) + 1;
	lineTarget.setAttribute('value', newCount);
	//记录添加
	if( isnew == "true" || isnew == true ){
		var c = "cate_" + categoryId;
	}else{
		var c = "category_" + categoryId;
	}
	var l = "li_" + linkId;
	jsonUtil.addLink(c, l, name, links, line, num);
//	alert(JSON.stringify(jsonObject));
	closeLinkFrame();
}

function closeLinkFrame(){
	$get("input_link_title").value= "";
	$get("input_link_link").value= "";
	$get("input_link_num").value= "";
	hideSC("add_link_frame");
}

function closeModifyFrame(){
	hideSC("modify_category_frame");
	$get("modify_category_name").value= "";
}

function delLink(id, line, isnewc, isnewl){
	if(!confirm("是否删除链接?")){
			return false;
		}
	//删除内容
	var li = $get(id);
	li.parentNode.removeChild(li);
	//删除记录
	var idArray = id.split("_");
	var cfullId;
	var lfullId;
	var lineId;
	if(isnewc == "true" || isnewc == true){
		cfullId = "cate_" + idArray[1];
		lineId = "cline_" + idArray[1] + "_" + line;
	}else{
		cfullId = "category_" + idArray[1];
		lineId = "line_" + idArray[1] + "_" + line;
	}
	if(isnewl == "true" || isnewl == true){
		lfullId = "li_" + idArray[2];
	}else{
		lfullId = "link_" + idArray[2];
	}
	jsonUtil.delLink(cfullId, lfullId);
	var lineTarget = $get(lineId);
	var newCount = parseInt(lineTarget.getAttribute('value')) - 1;
	lineTarget.setAttribute('value', newCount);
//	alert(JSON.stringify(jsonObject));
}

function closeModifyLinkFrame(){
	//var target = $get("modify_link_frame");
	hideSC("modify_link_frame");
}

function modifyLink(event, lfullId, isnewc, isnewl, alink){
	var target = $get("modify_link_frame");
	//var e = getEvent(event);
	target.style.position = "absolute";
	//target.style.left = e.clientX  + "px";
	//target.style.top = e.clientY +  document.documentElement.scrollTop + "px";
	var idArray = lfullId.split("_");
	var categoryId = idArray[1];
	var linkId = idArray[2];
	if(isnewc == "true" || isnewc == true){
		categoryId = "cate_" + categoryId;	
	}else{
		categoryId = "category_" + categoryId;	
	}
	if(isnewl == "true" || isnewl == true){
		linkId = "li_" + linkId;	
	}else{
		linkId = "link_" + linkId;	
	}
	var linkName = alink.getAttribute('name');
	var links = alink.getAttribute('links');
	var linkNum = alink.getAttribute('num');
	$get("modify_category_fid").value = categoryId;
	$get("modify_link_fid").value = linkId;
	$get("modify_link_title").value = linkName;
	$get("modify_link_link").value = links;
	$get("modify_link_num").value = linkNum;
	$get("modify_link_owner").value = alink.id;
	//Target.style.display = "block";
	showSC("modify_link_frame");
}

function modifyLink0(){
	var cfullId = $get("modify_category_fid").value;
	var lfullId = $get("modify_link_fid").value;
	var title = $get("modify_link_title").value;
	title = title.replace(/\||\;|\,|\'|\"/g,""); //去除,;|符号
    if(isEmpty(title) || title.length<1)
    {
        alert("请输入名称");
        $get("modify_link_title").focus();
        return false;
    }
	var links = $get("modify_link_link").value;
	links = links.replace(/⊙|◎|\'|\"/g,"");//去除⊙◎符号
    if(isEmpty(links) || links.length<4)
    {
        alert("请输入网址");
        $get("modify_link_link").focus();
        return false;
    }
	var num = $get("modify_link_num").value;
	if(num == null || num == ""){
		num = 0;	
	}
	var owner = $get($get("modify_link_owner").value);
	owner.setAttribute('name', title);
	owner.setAttribute('links', links);
	owner.setAttribute('num', num);
	owner.innerHTML = title;
	//记录修改
	closeModifyLinkFrame();
	jsonUtil.modifyLink(cfullId, lfullId, title, links, num);
//	alert(JSON.stringify(jsonObject));
}

function checkUserOne(){
	var one = $("#come_user").attr("value");
	var two = $("#input_name_one").attr("value");
	if(two == null || two == ""){return false;}
	if(one != two){
		return false;
	}else{
		return true;
	}
}

function existFirstPage(firstPage, async){
	if(firstPage == null || firstPage == ""){
		firstPageChecked = false;
		return false;	
	}
	firstPage = firstPage.trim();//////////////
	$.ajaxSetup({
 	 async: async
  	});
	$.get("checkUser.php", 
		  {firstPage : firstPage},
		  function(data, textStatus){
			   data = data.trim();
			  if(data == "true"){
				  firstPageChecked = true;
				  $("#bmsg").html("此首页网址已经存在");
			  }else if(data == "false"){
				  firstPageChecked = false;
				  $("#bmsg").html("");
			  }
		  });
}

function existUser(userName, async){
	if(userName == null || userName == ""){
		userNameChecked = false;
		return false;	
	}
	userName = userName.trim();
	$.ajaxSetup({
 	 async: async
  	});
	$.get("checkUser.php", 
		  {userName : userName},
		  function(data, textStatus){
			  data = data.trim();
			  if(data == "true"){
				  userNameChecked = true;
				  $("#bmsg").html("此用户名已经存在");
			  }else if(data == "false"){
				  userNameChecked = false;
				  $("#bmsg").html("");
			  }
		  });
}

function login(name, password){
	$.ajaxSetup({
 	 async: false
  	});
	loginSuccess = false;
	$.get("login.php", 
		  {userName : name , password : password},
		  function(data, textStatus){
			  data = data.trim();
			  if(data == "false"){
				  loginSuccess = false;
			  }else if(data == "true"){
				  loginSuccess = true;
			  }
		  });
}

function sendData(){
	var jsonStr = JSON.stringify(jsonObject);
	$.ajax({
		type: "POST",
		url: "save.php",
		async: false,
		data: {json: jsonStr},
		dataType: "script",
		success: function(data, status){
		
		},
		error: function(xml, textStatus, error){
			alert("请求发生如下错误:\n" + error);
		}
	});
}
		
function sendDataResponse(statusCode){
//	alert(statusCode);
	if(statusCode == 2){
		jsonUtil.setModify(false);
		var homePage = "http://" + jsonObject.firstPage + ".niu.bi";
		if(confirm("        保存数据成功!\n是否将" + homePage + "设置为主页？")){
			setHomePage(homePage);
		}
		window.location.href = homePage;
	}else if(statusCode == 0){
		alert("保存失败,用户已经存在!");
	}else{
		alert("保存失败,服务器端错误!");
	}
}

function save(){
	
	var name = $("#input_name_one").attr("value");
	name = name.trim();
	var password = $("#input_password_one").attr("value");
	password = password.trim();
	
	if(name == null || name == ""){
		alert("用户名不能为空");	
		return false;
	}
	if(password == null || password == ""){
		alert("密码不能为空");	
		return false;
	}
	
	login(name, password);
	if(loginSuccess != true){
		$("#amsg").html("用户不存在或密码错误");
		return;
	}
	if(!checkUserOne()){
		if(!confirm("这个操作将会覆盖你原来的数据!\n是否继续?")){
			return false;
		}
		jsonObject.override = "true";
	}
	infoCollect(false);
	sendData();
}

function regAndSave(){
	var name = $("#input_name_two").attr("value");
	name = name.trim();
	name = name.replace(/</g, "&lt;").replace(/>/g, "&gt;");
	var password = $("#input_password_two").attr("value");
	password = password.trim();
	password = password.replace(/</g, "&lt;").replace(/>/g, "&gt;");
	/////add by blueyb 200908116
	var comfrimPassword = $("#input_password_two_comfrim").attr("value");
	comfrimPassword = comfrimPassword.trim();
	password = password.replace(/</g, "&lt;").replace(/>/g, "&gt;");
	////////
	var firstPage = $("#input_firstPage").attr("value");
	firstPage = firstPage.trim();
	
	if(firstPage == null || firstPage == ""){
		alert("首页网址不能为空");	
		return false;
	}
	if(name == null || name == ""){
		alert("用户名不能为空");	
		return false;
	}
	if(password == null || password == ""){
		alert("密码不能为空");	
		return false;
	}
	
	if(comfrimPassword != password){
		alert("两次输入的密码不相同");	
		return false;
	}
	
	existFirstPage(firstPage);
	if(firstPageChecked){
		alert("已存在此首页网址");
		return false;
	}
	existUser(name);
	if(userNameChecked){
		alert("已存在此用户名");
		return false;
	}
	infoCollect(true);
	sendData();
}

function updataad(event){
	var target = $get("update_ad_frame");
	//var e = getEvent(event);
	target.style.position = "absolute";
	showSC("update_ad_frame");
}

function closeUpdateAdFrame(){
	//$get("adcode").value = "";
	hideSC("update_ad_frame");
}

function updatead0(){
	var ad_pic = $get("ad_pic").value;
	var ad_link = $get("ad_link").value;
	var adcode="<a href="+ad_link+" target=_blank><img src="+ad_pic+" border=0></a>";
	//记录修改
	closeUpdateAdFrame();
	jsonObject.adcode = adcode ;
//	alert(JSON.stringify(jsonObject));
}

function uploadPic(event){
	$get("upload_pic").value = "";
	$get("upload_pic_status").innerHTML = "";
	var target = $get("upload_pic_frame");
	//var e = getEvent(event);
	target.style.position = "absolute";
	//target.style.left = e.clientX - 450 + "px";
	//target.style.top = e.clientY + document.documentElement.scrollTop + "px";
	//target.style.display = "block";
	showSC("upload_pic_frame");
}

function uploadPic0(){
	var pic = $get('upload_pic').value;
	if(pic == null || pic == ""){
		alert("请选择图片");
			return false;
	}
	if(!checkPicType(pic)){
		alert("请上传jpg,gif,bmp,png格式的图片");
		return false;
	}
	var logoForm = $get("upload_pic_form");
	logoForm.submit();
	changeStatus(4);
}

function changeStatus(count){
	count = count % 3;
	var suffix = "....";
	var msg = "正在上传" + suffix.substring(count);
	$get("upload_pic_status").innerHTML = msg;
	count++;
	var command = "changeStatus(" + count + ")";
	timeHandle = setTimeout(command, 300);
}

function closeUploadFrame(){
	hideSC("upload_pic_frame");
}

function changeLogo(i){
	clearTimeout(timeHandle);
	if(i == 1){
		$get("upload_pic_status").value = "图片格式不正确";
		return;
	}if(i == 3){
		$get("upload_pic_status").value = "上传Logo失败";
		return;
	}
	$get("upload_pic_status").innerHTML = "上传Logo成功";
	jsonObject.modify = true;
	$get("logo").src = jsonObject.logo;
	closeUploadFrame();
}

function checkPicType(picName){
		if(picName == null || picName == "" ||  picName.length < 4){
				return false;
		}
		var suffix  = picName.substring(picName.length -4 , picName.length);
		suffix = suffix.toLowerCase();
		if(suffix == ".png" || suffix == ".jpg" || suffix == "jpeg" || suffix == ".gif" || suffix == ".bmp"){
				return true;
		}else{
				return false;
		}
}


////////////////

function closeModifyPasswordFrame(){
	$get("modify_user_name").value = "";
	$get("modify_user_old_passsword").value = "";
	$get("modify_user_new_passsword").value = "";
	$get("modify_user_comfrim_passsword").value = "";
	hideSC("modify_password_frame");
}

function modifyPassword(event){
	var target = $get("modify_password_frame");
	//var e = getEvent(event);
	target.style.position = "absolute";
	//target.style.left = e.clientX  + "px";
	//target.style.top = e.clientY + document.documentElement.scrollTop - 100 + "px";
	//target.style.display = "block";
	showSC("modify_password_frame");
}

function modifyPassword0(){
		var name = $get("modify_user_name").value;
		name = name.trim();
	  if(isEmpty(name) || name.length<1)
	  {
	      alert("请输入用户名");
	      $get("modify_user_name").focus();
	      return false;
	  }
		var oldPassword = $get("modify_user_old_passsword").value;
		oldPassword = oldPassword.trim();
    if(isEmpty(oldPassword) || oldPassword.length<1)
    {
        alert("请输入原密码");
        $get("modify_user_old_passsword").focus();
        return false;
    }
		var newPassword = $get("modify_user_new_passsword").value;
		newPassword = newPassword.trim();
		newPassword = newPassword.replace(/</g, "&lt;").replace(/>/g, "&gt;");
    if(isEmpty(newPassword) || newPassword.length<1)
    {
        alert("请输入新密码");
        $get("modify_user_new_passsword").focus();
        return false;
    }
	var comfrimPassword = $get("modify_user_comfrim_passsword").value;
	comfrimPassword = comfrimPassword.trim();
	comfrimPassword = comfrimPassword.replace(/</g, "&lt;").replace(/>/g, "&gt;");
    if(isEmpty(comfrimPassword) || comfrimPassword.length<1)
    {
        alert("请输入确认密码");
        $get("modify_user_comfrim_passsword").focus();
        return false;
    }
		if(newPassword != comfrimPassword){
			alert("两次输入的密码不相等");
		    $get("modify_user_new_passsword").focus();
		    return false;
		}
		modifyPasswordSuccess = false;
		$.ajax({
			type: "POST",
			url: "password_modify.php",
			async: false,
			data: {name: name, oldPassword: oldPassword, newPassword: newPassword},
			success: function(data, textStatus){
				data = data.trim();
				 if(data == "3"){
					  alert("修改密码失败");
				  }else if(data == "2"){
				  	modifyPasswordSuccess = true;
					  alert("修改密码成功");
				  }else if(data == "1"){
				  	alert("不存在此用户或密码错误");
				  }else if(data == "0"){
					alert("不存在此用户或密码错误");
				  }
			},
			error: function(xml, textStatus, error){
				alert("上传请求发生如下错误:\n" + error);
			}
		});
		if(modifyPasswordSuccess){
			closeModifyPasswordFrame();
		}
}

function defaultLogo(){
	$get("logo").src = "images/logo.jpg";
	jsonUtil.setLogo("default");
}

//////////////
	
function resetSC(SCid) {
    var lay2 = oo(SCid);
    lay2.css("left",(document.body.clientWidth-lay2.width())/2 + document.documentElement.scrollLeft+"px");
    lay2.css("top",document.body.scrollTop+($(window).height()-lay2.height())/2 + document.documentElement.scrollTop+"px");
}
function showSC(SCid){
   var lay2=oo(SCid);
   if(lay2.css("display")=="block")
       return;
   lay2.css("left",(document.body.clientWidth-lay2.width())/2+"px");
   lay2.css("top",document.body.scrollTop+($(window).height()-lay2.height())/2+190+"px");
   //alert(lay2.css("top"));
   lay2.css("display",'block');
   lay2.css("z-index",'100');
  if($("#_frame_select").size()==0){
    $("body").append('<iframe id="_frame_select" name="_frame_select" style="position: absolute; z-index: 0; width: 0px; height: 0px; top: 0;left: 0; scrolling: no;" frameborder="0" src="about:blank"></iframe>');
  }
  $("#_frame_select").css("top",lay2.css("top"));
	$("#_frame_select").css("left",lay2.css("left"));
	$("#_frame_select").css("width",(lay2.width()+8)+"px");
	$("#_frame_select").css("height",(lay2.height()+8)+"px");
}
function hideSC(SCid){
   var lay2=oo(SCid);
   if(lay2.css("display")=="block")
       lay2.css("display",'none');
  $("#_frame_select").css("top",'0');
	$("#_frame_select").css("left",'0');
	$("#_frame_select").css("width",'0');
	$("#_frame_select").css("height",'0');
}
function oo(obj){
   return typeof(obj)=="string"?$("#"+obj):obj
}

function doWindowClose(event){
	if(jsonObject.modify == true){
		var e = getEvent(event);
		e.returnValue="你还有数据没有保存,离开后未保存的数据将丟失!";
	}
}