﻿// JScript 文件

//获取浏览器类型
function getSysBrowserInfo(){
    var browserInfo;
    var Sys = {};   
    var ua = navigator.userAgent.toLowerCase();   
    window.ActiveXObject ? Sys.ie = ua.match(/msie ([\d.]+)/)[1] :   
    document.getBoxObjectFor ? Sys.firefox = ua.match(/firefox\/([\d.]+)/)[1] :   
    window.opera ? Sys.opera = ua.match(/opera.([\d.]+)/)[1] :   
    window.openDatabase ? Sys.safari = ua.match(/version\/([\d.]+)/)[1] : 0;
    
    if(Sys.ie) {browserInfo = "ie";}
    if(Sys.firefox) {browserInfo = "firefox";}
    if(Sys.opera) {browserInfo = "opera";}
    if(Sys.safari) {browserInfo = "safari";}
    
    return browserInfo;
}

//获取字符长度
String.prototype.ECharsLen = function() {
		return this.replace(/[^\x00-\xff]/g, "aa").length;
};

//去掉空格
String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
};

//验证邮箱
String.prototype.isEmail = function() {
	var pattern = /^[^@\s]+@[A-Za-z0-9-\u4e00-\u9fa5]+(?:\.[A-Za-z0-9-\u4e00-\u9fa5]+)+$/;
	if (pattern.test(this)) return true;
	else return false;
};

//验证URL
String.prototype.isUrl = function() {
	var pattern = /^(?:http:\/\/)?[A-Za-z0-9-\u4e00-\u9fa5]+(?:\.[A-Za-z0-9-\u4e00-\u9fa5]+)+(?:\/[^\s]*)?$/;
	if (pattern.test(this)) return true;
	else return false;
};

//验证Money(精确两位小数)
String.prototype.isMoney = function() {
	var pattern = /^(?:^\d+$)|(?:^\d+.\d(?:\d)?$)$/;
	if (pattern.test(this)) return true;
	else return false;
};

//验证数字
String.prototype.isNum = function() {
    var pattern = /^[0-9]+$/;
    if (pattern.test(this)) return true;
	else return false;
};

//图片缩放
function DrawImage(imgObject,iWidth,iHeight)
{
  if(imgObject.width>iWidth) {imgObject.resized=true; imgObject.height=imgObject.height*iWidth/imgObject.width;imgObject.width=iWidth; };if(imgObject.height>iHeight) {imgObject.resized=true; imgObject.width=imgObject.width*iHeight/imgObject.height;imgObject.height=iHeight; }
}


/*------------------------------------------------------------------------------*/

var isIE = false;
if(getSysBrowserInfo() == "ie"){
    isIE = true;
}

function CreateElement(t) {
	if (isIE){
	    return document.createElement(t);
	}else {
	    return document.createElementNS("http://www.w3.org/1999/xhtml",t);
	}
}

var Tip_Onscroll;
function Tip(Msg,TipName,Top,Left) {
	if (!TipName) TipName="MyTip";
	if (Top==undefined) Top=(document.documentElement.scrollTop+(clientHeight()-30)/3);
	if (Left==undefined) Left=((document.body.clientWidth-300)/2);
	if (document.getElementById(TipName)) {
		Tip_Remove(TipName,true);
	}
	if (isIE) {
		var pTip = document.createElement("<div id='"+TipName+"' style='position:absolute;z-index:2007;top:"+Top+"px;left:"+Left+"px;background-color:#c44;padding-left:6px;padding-right:6px;padding-top:2px;padding-bottom:2px;font-size:14px;color:white;/>");
		pTip.appendChild(document.createTextNode(Msg));
		pTip.appendChild(document.createTextNode(" "));
		pTip.appendChild(document.createTextNode(" "));

		var pTipFunnyWeber = document.createElement("<a style='color:white;text-decoration:none;'/>");
		pTipFunnyWeber.setAttribute("href","javascript:Tip_Remove('"+TipName+"')");
		pTipFunnyWeber.appendChild(document.createTextNode("×"));

		pTip.appendChild(pTipFunnyWeber);
		document.body.appendChild(pTip);
	}
	else {
		if (document.getElementById(TipName)==null) {
			var pTip = document.createElement("div");
			pTip.setAttribute("id",TipName);
			pTip.setAttribute("style","position:absolute;z-index:2007;top:"+Top+"px;left:"+Left+"px;background-color:#c44;padding-left:6px;padding-right:6px;padding-top:2px;padding-bottom:2px;font-size:14px;color:white;");
			pTip.appendChild(document.createTextNode(Msg));
			pTip.appendChild(document.createTextNode(" "));
			pTip.appendChild(document.createTextNode(" "));

			var pTipFunnyWeber = document.createElement("a");
			pTipFunnyWeber.setAttribute("style","color:white;text-decoration:none;");
			pTipFunnyWeber.setAttribute("href","javascript:Tip_Remove('"+TipName+"')");
			pTipFunnyWeber.appendChild(document.createTextNode("×"));

			pTip.appendChild(pTipFunnyWeber);
			document.body.appendChild(pTip);
		}
	}
	Tip_Onscroll=window.onscroll;
	window.onscroll = function() {
		var tmpTop=(document.documentElement.scrollTop+(clientHeight()-30)/3);
		if (document.getElementById(TipName)) {
			document.getElementById(TipName).style.top=tmpTop+"px";
		}
	}
}

function Tip_Remove(TipName,isInit) {
	if (!TipName) TipName="MyTip";
	if (!isInit) isInit=false;
	if (document.getElementById(TipName)) {
		if (isIE) {
			document.getElementById(TipName).removeNode(true);
		}
		else {
			document.getElementById(TipName).parentNode.removeChild(document.getElementById(TipName));
		}
		if (!isInit) {
			window.onscroll=Tip_Onscroll;
		}
	}
}

function clientHeight() {
	if (isIE) {
		if (document.documentElement.clientHeight!=0) {
			return document.documentElement.clientHeight;
		}
		else {
			return document.body.clientHeight;
		}
	}
	else {
		if (document.documentElement.clientHeight<=document.body.clientHeight && document.documentElement.clientHeight!=0) {
			return document.documentElement.clientHeight;	
		}
		else {
			return document.body.clientHeight;
		}
	}
}



/*------------------------------------------------------------------------------*/

var Tools = {
    Ajax:{
        CreateXmlHttpObj:function() {
                var xmlHttp = null;
                var createObjectFlag = true;
                try{
                    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
                    createObjectFlag = true;
                }catch(e){ 
                    createObjectFlag = false;
                }
                
                if(!createObjectFlag){
                    try{
                        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                        createObjectFlag = true;
                    }catch(e2){
                        createObjectFlag = false;
                    }
                }
                
                if(!createObjectFlag && typeof(XMLHttpRequest)!="undefined"){
                    try{
                        xmlHttp = new XMLHttpRequest();
                        createObjectFlag = true;
                    }catch(e3){
                        createObjectFlag = false;
                    }
                }
                
                return xmlHttp;
        },
    
        Updater:function(element, url, option) {
            if(url){
                var xmlHttp = null;
                xmlHttp = Tools.Ajax.CreateXmlHttpObj();
                var method = "GET";
                var postbody = null;
                if(option){
                    if(option.method.toLowerCase() == "post"){
                        method = "POST";
                        if(option.postBody){
                            postbody = option.postBody;
                        }
                    }
                }
                
                xmlHttp.open(method,url,true);
                if(method == "POST"){
                    var len = 0;
                    if(postbody != "" || postbody != null){
                        len = postbody.length;
                    }
                    xmlHttp.setRequestHeader("content-length",len);
	                xmlHttp.setRequestHeader("content-type","application/x-www-form-urlencoded");
                }
                
                xmlHttp.onreadystatechange = function(){ Tools.Ajax.onreadystatechange(element, xmlHttp, option); };
	            xmlHttp.send(postbody);
    	        
	         }else{
	            alert("Ajax Error:Url is null!");
	         }
        },
        
        
        onreadystatechange:function(element, xmlHttp, option) {
			switch (parseInt(xmlHttp.readyState))  {
				case 0: //the send( ) method has not yet been invoked
					break;
				case 1: //the send( ) method has been invoked, request in progress
					break;
				case 2: //the send( ) method has completed, entire response received
					break;
				case 3: //the response is being parsed
					break;
				case 4: //the response has been parsed, is ready for harvesting
					var responseText = xmlHttp.responseText;
					if (option) if (option.onComplete) {
						option.onComplete(responseText);
					}
					if (element) {
						if (typeof(element)!="string") {
							if (element.length) {
								if (element.length>1) {
									for (var i=0; i<element.length; i++) {
										Tools.Ajax.replaceInnerHTML(element[i], responseText);
									}
								}
								else {
									Tools.Ajax.replaceInnerHTML(element, responseText);
								}
							}
							else {
								Tools.Ajax.replaceInnerHTML(element, responseText);
							}
						}
						else {
							Tools.Ajax.replaceInnerHTML(element, responseText);
						}
					}
					break;
			}
		},
		
		replaceInnerHTML:function(element, responseText) {
			var loop = function(element, responseText) { if (element) if (element.nodeType) if (element.nodeType==1) {element.innerHTML=responseText; return true;} return false; };
			if (!loop(element, responseText)) {
				element=document.getElementById(element);
				loop(element, responseText);
			}
		}
    }
}


/*----------------------------------------------------------------------------*/

/*
页面LOADING显示
iDocId:页面元素的ID号
*/
function SetLoadingStyle(iDocId){
    var showCode="";
	showCode+="<span style='text-align:center;font-size:12px;'>";
	showCode+="<center>";
	showCode+="<img src='/images/ajaxloading.gif'  border='0' />&nbsp;正在加载中......";
	showCode+="</center>";
	showCode+="</span>";
	document.getElementById(iDocId).innerHTML=showCode;
}


function centerdiv(divid){  
    var divobj=document.getElementById(divid);
    var w=200;
    var h=55;
    divobj.style.width=w+"px";
    divobj.style.height=h+"px";
    divobj.style.textAlign="center";
    divobj.style.verticalAlign="middle";
    divobj.style.fontSize="14px";
    divobj.style.color="#000000";
    divobj.style.border="1px #cccccc solid";
    divobj.style.padding="5px";
    divobj.style.filter = "alpha(opacity=70)";
    divobj.style.opacity=0.7;
    divobj.style.backgroundColor="#e6e6e6";
    divobj.style.position='absolute';

    divobj.style.top = document.documentElement.scrollTop + parseInt((bodyClientHeight() - divobj.offsetHeight) / 2) + "px";
    divobj.style.left = document.documentElement.scrollLeft + parseInt((bodyClientWidth() - divobj.offsetWidth) / 2) + "px";
}


/*
获取页面代码	
iDocId:页面元素的ID号
strURL:要获取页面的URL
iLoading:1:显示loading效果
*/
function getPageCode(iDocId,strURL,iLoading){
	getPageCode(iDocId,strURL,iLoading,null);
}


/*
获取页面代码	
iDocId:页面元素的ID号
strURL:要获取页面的URL
iLoading:1:显示loading效果
evalFun:需要执行的js代码内容
*/
function getPageCode(iDocId,strURL,iLoading,evalFun){
    var xmlObj = new Tools.Ajax.CreateXmlHttpObj();
    if(iLoading == 1){
        SetLoadingStyle(iDocId);
    }
    if(xmlObj != null){
        if(strURL.indexOf("?")>-1){
            strURL = strURL+"&tag="+new Date().getTime();
        }else{
            strURL = strURL+"?tag="+new Date().getTime();
        }
        
        xmlObj.open('get',strURL,true);
        xmlObj.onreadystatechange=function(){
            if(xmlObj.readyState == 4){
                if(xmlObj.status == 200 || xmlObj.status == 0){
                    try{
                        document.getElementById(iDocId).innerHTML = xmlObj.responseText;
                        //urchintracker(window.top.location.href);
                    }catch(e){
                        ;
                    }
                    if(evalFun != null){
                        try{
                            eval(evalFun);
                        }catch(e){
                            ;
                        }
                    }
                }
            }
        }
        
        xmlObj.send(null);
    }
}


