﻿// JavaScript Document
/*
powered|homepage: 
	http://www.fftoo.com/
	http://www.fftoo.net/
Author:
	QQ:55721736
	QQ:155293736
	Email:admin@fftoo.com
Design:
	2008.11.06
*/


if(!document.appPath || document.appPath=="/") document.appPath = "";
var isLocal = (document.URL.toLowerCase().indexOf("http://zp/fftoo/") == 0); //测试使用
if(isLocal) document.appPath = "/fftoo";

var cookie = {
	set : function(name,value,expires) {
		name = name.toLowerCase();
		var path = "/",domain = null,secure = false;
		var expString = ";expires="+ expires.toGMTString();
		var pathString = ((path==null) ? "" : (";path="+path));
		var domainString = ((domain==null) ? "" : (";domain="+domain));
		var secureString = ((secure==true) ? ";secure" : "" );
		document.cookie = name + "=" + escape(value) + expString + pathString + domainString + secureString;
	}
	,
	get:function(name){
		name = name.toLowerCase();
		var cookieValue = "";
		var search = name + "=";
		if(document.cookie.length > 0){ 
			offset = document.cookie.indexOf(search);
			if (offset != -1){ 
			  offset += search.length;
			  end = document.cookie.indexOf(";", offset);
			  if (end == -1) end = document.cookie.length;
			  cookieValue = unescape(document.cookie.substring(offset, end));
			}
		}
		return cookieValue;
	}
	,
	clear:function(name){
		name = name.toLowerCase();
		var ThreeDays=3*24*60*60*1000;
		var expDate = new Date();
		expDate.setTime(expDate.getTime() - ThreeDays);
		document.cookie=name+"=;expires="+expDate.toGMTString();
	}
}

var $ = function (s){return document.getElementById(s);}

var XHConn = function (){
	var xmlhttp, bComplete = false;
	try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
		catch (e) { try { xmlhttp = new XMLHttpRequest(); }
		catch (e) { xmlhttp = false; }}
	}
	if (!xmlhttp) return null;
	this.isUpLoad = false;
	this.open = function(sURL, sMethod, sVars,fnDone){
		if (!xmlhttp) return false;
		bComplete = false;
		sMethod = sMethod.toUpperCase();
		try {
			if (sMethod == "GET"){
				if(sVars.length > 0) sURL += ((sURL.indexOf("?")==-1)? "?":"&") + sVars
				xmlhttp.open(sMethod, sURL, true);
				sVars = "";
			}else{
				xmlhttp.open(sMethod, sURL, true);
				xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
				var ContentType = (this.isUpLoad) ? "multipart/form-data" : "application/x-www-form-urlencoded"
				xmlhttp.setRequestHeader("Content-Type",ContentType);
			}//end if
			xmlhttp.onreadystatechange = function(){
				if (xmlhttp.readyState == 4 && !bComplete){
					bComplete = true;
					fnDone(xmlhttp);
				}//end if
			};//end function
			xmlhttp.send(sVars);
			//return xmlhttp;
		}//end try
		catch(z) {
			return false;
		}
		return true;
	};//end function
	return this;
}//end function

String.prototype.trim = function(){
	return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.lengthW = function(){
	return this.replace(/[^\x00-\xff]/g,"**").length;
}
String.prototype.isEmail = function(){ 
	var pattern = /^[\s]*[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+\.[a-zA-Z0-9]{2,4}[\s]*$/
	return pattern.test(this);
} 
String.prototype.HasCN = function(){// 是否含有中文
	var pattern = /^[\x00-\xff]*$/;
	return pattern.test(this);
}

String.prototype.isUrl = function(){//检查url
	var pattern = /^http[s]?:\/\/([\w-]+\.)+[\w-]+([\w-./?%&=]*)?$/;
	return pattern.test(this);
} 
String.prototype.isPhoneCode = function(){//检查电话号码
	var pattern = /(^[0-9]{3,4}\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/;
	return pattern.test(this);
} 
String.prototype.isNumber = function(){ //检查整数
	return /^[0-9]+$/.test(this);
}
String.prototype.toNumber = function(def){ // 整数转换
	return isNaN(parseInt(this, 10)) ? def : parseInt(this, 10);
}
String.prototype.toMoney = function(def){// 小数转换
	return isNaN(parseFloat(this)) ? def : parseFloat(this);
} 
String.prototype.isDate = function(){
	var A=/^(\d{4})(-|\/|\.)(\d{1,2})\2(\d{1,2})$/,$=this.match(A);if($==null)return false;var C,B,_;C=$[1];B=parseInt($[3],10);_=parseInt($[4],10);if((B<1)||(B>12)||(_<1)||(_>31))return false;if(((B==4)||(B==6)||(B==9)||(B==11))&&(_>30))return false;if((C%4)==0){if((B==2)&&(_>29))return false}else if((B==2)&&(_>28))return false;return true}

// 类似ASP中的URLencode函数编码
String.prototype.encodeUTF8 = function()
{      
      var tmp = escape(this.toString());
      var regex = /\+/g;    // s 中含有+号
      tmp = tmp.replace(regex,"%2b");    
      tmp = tmp.replace(/\=/g,"%3d");
      return tmp;
}
String.prototype.cutString = function(len){
	var str_length = 0;
	var str_len = 0;
	var str_cut = new String();
	var str_len = this.length;
	for(var i = 0;i<str_len;i++){
		var a = str.charAt(i);
		str_length++;
		if(escape(a).length > 4)
		{
			//中文字符的长度经编码之后大于4
			str_length++;
		}
		str_cut = str_cut.concat(a);
		if(str_length>=len)
		{
			str_cut = str_cut.concat("...");
			return str_cut;
		}
	}
	//如果给定字符串小于指定长度，则返回源字符串；
	if(str_length<len){
		return  this;
	}
}

function setCopy(_sTxt){
	try{
		clipboardData.setData('Text',_sTxt);
	}catch(e){};
	alert('所选数据已经复制到剪贴板中！');
}

var Browser = {};
Browser.isMozilla = (typeof document.implementation!='undefined')&&(typeof document.implementation.createDocument!='undefined')&&(typeof HTMLDocument!='undefined');
Browser.isIE = window.ActiveXObject ? true : false;
Browser.isFirefox = (navigator.userAgent.toLowerCase().indexOf("firefox")!=-1);
Browser.isSafari = (navigator.userAgent.toLowerCase().indexOf("safari")!=-1);
Browser.isOpera = (navigator.userAgent.toLowerCase().indexOf("opera")!=-1);

// 数字格式化函数, 相当于Vbs里的FormatNumber, 自动补0
// 支持18位(包括.号), 多于18位时会有误差
// num : 要格式化的数字, 必须为数字型
// after : 小数位数, 默认2位, 最多5位
// 作者: xiaoyuehen(http://hi.baidu.com/xiaoyuehen/blog/item/ddcc93186b2b5ab64bedbc4d.html)
// 最后更新: 2007-09-05
function FormatNumber(H,G){var G=(G==null)?2:G;G=(G<0)?2:G;if(G>5){alert("\u8be5\u683c\u5f0f\u5316\u51fd\u6570\u53ea\u652f\u6301\u6700\u591a5\u4f4d\u5c0f\u6570!");G=5}var I="",K="",E=(H<0),H=Math.abs(H).toString();if(H.indexOf(".",0)<0){I=H;for(var F=1;F<=G;F++)K+="0";K=K.length?"."+K:""}else if(G>0){var _=H.split(".");I=_[0];K=_[1];var A=K.length,$=I.length;for(F=1;F<=G-A+1;F++)K+="0";var C=I+K.substr(0,G)+"."+K.substr(G,1),L=Math.round(C).toString(),J=L.length;for(F=1;F<=G+$-J;F++)L="0"+L;$=L.length-G;I=L.substr(0,$);K="."+L.substr($,G)}else{I=Math.round(H);K=""}var D="",B=I;while(B.length>3){D=","+B.substr(B.length-3,3)+D;B=B.substr(0,B.length-3)}D=B+D;return((E?"-":"")+D+K)}

// 功能: 构造表单键值对
// 返回: 以&字符分割的键值对, 中文已编码
function ParsePostData($){var B,_=[],A=[];for(E=0;E<$.elements.length;E++){B=$.elements[E];if(B.name&&B.name.length>0)if($[B.name].length)A.push(B.name);else _.push(B.name+"="+B.value.encodeUTF8())}if(A.length)A.unique();try{for(var E=0;E<A.length;E++){var C=[];for(var D=0;D<$[A[E]].length;D++)if($[A[E]][D].checked||$[A[E]][D].selected)C.push($[A[E]][D].value.encodeUTF8());_.push(A[E]+"="+C.join(","))}}catch(B){}return _.join("&")}

function setHome(n){
	try{
		if(Browser.isFirefox)
		{
			if(window.netscape)
			{
				try {  
				  netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  
				}  
				catch (e)  
				{  
				  alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将[signed.applets.codebase_principal_support]设置为'true'");  
				}
			}//end if
			var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
			prefs.setCharPref('browser.startup.homepage',document.URL);
		}
		else if (Browser.isIE)
		{
			n.style.behavior='url(#default#homepage)';
			n.setHomePage(document.URL);
		}
	}
	catch(e)
	{
		alert(e.message);
	}
	return false;
}

function addFavorite()
{
	var URL = (typeof(arguments[0])=="undefined") ? document.URL : arguments[0];
	var Title = (typeof(arguments[1])=="undefined") ? document.title : arguments[1];
	try{
		if (Browser.isIE)
		{
			window.external.addFavorite(URL,Title);
		}else{
			window.sidebar.addPanel(URL, Title, "");	
		}
	}
	catch(e)
	{
		alert(e.message);
	}
	return false;
}

var CodeImgReLoad = function(){
	var CodeImg = $("CodeImg");
	if(!CodeImg) return ;
	if(CodeImg.isLoading){
		alert("正在加载图片,请稍后!")
		return;
	}
	if(!CodeImg.rel) CodeImg.rel = CodeImg.src;
	//预加载模式:
	CodeImg.isLoading = true;
	document.Img = new Image;
	document.Img.src = document.appPath + "/imgCode/" +  Math.floor(Math.random() * 12345678) + ".gif";
	document.Img.onload = function(){
		CodeImg.isLoading = false;
		CodeImg.src = document.Img.src;
		var checkCode = $("checkCode");
		if(checkCode){
			checkCode.value = cookie.get("ImageV");	
		}
	}
}

var LoginPage = null;

cookie.userName = cookie.get("user.name");
cookie.userid = cookie.get("user.id");

var showUserName = function()
{
	var appPath = document.appPath;
	var url = document.URL.split("#")[0];
	if(cookie.userName.length != ""){
		var s = '你好 , <a href="' + appPath + '/users/myfftoo.aspx">' + cookie.userName + '</a> | <a href="' + appPath + '/users/logout.aspx?back=' + escape(url) + '" onclick="return confirm(\'是否要退出？\')">退出</a>';
	}else{
		var s = '<a href="' + appPath + '/users/reg.aspx" title="会员注册"><span>注册</span></a> | <a href="' + appPath + '/users/login.aspx?back=' + escape(url) + '" title="会员登录"><span>登录</span></a>'
	}
	document.write(s);

}
//===============================
var showTop = function()
{
	document.write('<ul id="nav_top"><li class="s">');
	showUserName();
	document.write('</li><li><a href="' + document.appPath + '/users/manage.do?act=myFftoo" title="我的兔窝" class="icon ft"><span>我的兔窝</span></a></li><li><a href="http://www.fftoo.com/" title="设为首页" class="icon home" onclick="return setHome(this);"><span>设为首页</span></a></li><li><a href="http://www.fftoo.com/" title="收藏本页" class="icon fav" onclick="return addFavorite();"><span>收藏本页</span></a></li><li><a href="#" title="帮助中心" class="icon help"><span>帮助中心</span></a></li></ul>');
}
//取得文件域的完整路径
var getFilePath = function (obj) 
{ 
	if(obj) 
	{ 
		//ie 
		if (window.navigator.userAgent.indexOf("MSIE")>=1) 
		{ 
			obj.select(); 
			return document.selection.createRange().text; 
		} 
		//firefox 
		else if(window.navigator.userAgent.indexOf("Firefox")>=1) 
		{ 
			if(obj.files) 
			{ 
				return obj.files.item(0).getAsDataURL(); 
			} 
			return obj.value; 
		} 
		return obj.value; 
	} 
} 
///===================
var openWin = function(rel,width,height){
	var winName = "fftoo_com";
	//获得窗口的垂直位置
	var top = (window.screen.availHeight-30-height)/2;        
	//获得窗口的水平位置
	var left = (window.screen.availWidth-10-width)/2; 
	var win = window.open(rel,winName,"height="+height+", width="+width+", top="+top+", left="+left+", toolbar= no,menubar=no,scrollbars=yes, resizable=no, location=no, status=no");
	return win.openner;
}

