﻿///
//工具类
///
Utils = {    
    htmlEncode: function(str) {
        if (typeof str == "undefined") return "";
        str = str.replace(/&/g, "&amp;");
        str = str.replace(/</g, "&lt;");
        str = str.replace(/>/g, "&gt;");
        str = str.replace(/"/g, "&quot;");
        return str;
    },
    htmlDecode: function(str) {
        if (typeof str == "undefined") return "";
        str = str.replace(/&amp;/g, "&");
        str = str.replace(/&lt;/g, "<");
        str = str.replace(/&gt;/g, ">");
        str = str.replace(/&quot;/g, "\"");
        return str;
    },    
    TextValue2Html: function(s){// 将“\”转义字符转换为HTML代码
	    return s.replace(/\r\n/g, "<br />").replace(/\r/g, "<br />").replace(/\n/g, "<br />");
    }
}

//
//获得URL参数
//key=参数名称， DefaultValue=默认值
//
Utils.queryString = function(Key, DefaultValue){
	var Str = window.location.href;
	var Pos = Str.indexOf('?');
	if (Pos == -1) return DefaultValue;
	Str = Str.substring(Pos + 1, Str.length);
	var List = Str.split("&");
	for (Loop = 0; Loop < List.length; Loop++)
	{
		if ((Pos = List[Loop].indexOf('=')) == -1) continue;
		Str = List[Loop].substring(0, Pos);
		if (Str == Key)
			return decodeURIComponent(String(List[Loop].substring(Pos + 1, List[Loop].length)));
	}
	return DefaultValue;
}

//获取event对象,主要用于兼容firefox
Utils.getEvent=function(A){
  var evt=A||window.event;
  if(!evt){
    var arr=[],C=this.getEvent.caller;
    while(C){
      evt=C.arguments[0];
      if(evt && (evt.constructor.target || evt.srcElement)){
	  //if(evt && evt.constructor==Event){
        break ;
      }
      var B=false;
      for(var D=0;D<arr.length;D++){
        if(C==arr[D]){
          B=true;
          break ;
        }
      }
      if(B){
        break ;
      }else {
        arr.push(C);
      }
      C=C.caller;
    }
  }
  return evt;
}

//停止事件冒泡
Utils.stopEvent=function(e){
  if(!e){
    e=this.getEvent();
  }
  if (e) {
  	if (e.stopPropagation) {
  		e.stopPropagation();
  		e.preventDefault();
  	}
  	else {
  		e.cancelBubble = true;
  		e.returnValue = false;
  	}
  }
}

// 浮点数格式校验(0,正负)
Utils.ValidFloat = function(s)
{
	var check = /^\d+(\.\d+)?$/;
	if (!check.test(s)) { return false; }
	return true;
}

// 英文,数字，字母，下划线字符串校验
Utils.ValidAsc = function(s)
{
	var check = /^[0-9a-z][\w-.]*[0-9a-z]$/;
	if (!check.test(s)) { return false; }
	return true;
}

//验证手机号码
Utils.ValidMobileNo = function(s)
{
	var check = /^(13|15|18)\d{9}$/;
	if (!check.test(s)) { return false; }
	return true;
}

// Email格式校验
Utils.ValidEmail = function(s)
{
	var check = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	if (!check.test(s)) { return false; }
	return true;
}

String.prototype.trim = function(){
	return this.replace(/^\s+|\s+$/g, "");
}
String.prototype.format = function() {
    var str = this;
    var tmp;
    for (var i = 0; i < arguments.length; i++) {
        tmp = String(arguments[i]).replace(/\$/g, "$$$$");
        str = str.replace(eval("/\\{" + i + "\\}/g"), tmp);
    }
    return str;
}

///获得cookie的值
function GetCookieEx(sName) {
     var aCookie = document.cookie.split("; ");
     for (var i = 0; i < aCookie.length; i++) {
         var aCrumb = aCookie[i].split("=");
         if (sName == aCrumb[0]) {
             if (aCrumb.length > 1) return decodeURIComponent(aCrumb[1]); else return "";
         } 
     }
     return null;
 }

/* >>>>>Begin waitpannel.js */
/**
 * 显示正在加载中.......
 */
var WaitPannel=new Object();
WaitPannel.show = function(msg,delay) {
    div = document.getElementById("contextWaitPannel");
    if (!div) {
        div = document.createElement("div");
        div.id = "contextWaitPannel";
        div.className = "loadingInfo";
        div.style.zIndex = "99999999";
        if (!msg) {
            msg = "加载中，请稍候........";
        }
        div.innerHTML = msg;  
        document.body.appendChild(div);
    }
    var windowWidth = document.body.clientWidth;
    var windowHeight = document.body.clientHeight;
    var popupHeight = $(div).height();
    var popupWidth = $(div).width();
    $(div).css({
        "position": "absolute",
        "top": windowHeight/2-popupHeight/2,
        "left": windowWidth/2-popupWidth/2
    }); 
    
	if(delay){
		setTimeout(WaitPannel.hide,delay);
	}
};

WaitPannel.hide = function() {
    div = document.getElementById("contextWaitPannel");
    if (div) {
        document.body.removeChild(div);
    }
};

/* >>>>>End waitpannel.js */

