﻿String.prototype.trim=function(){
	return this.replace(/(^\s+)|(\s+$)/g, "");
}
String.prototype.remove = function(index,count){
	if (typeof(count)!="number") count=1;
	if(isNaN(index)||index>this.length||index<0||isNaN(count)||index+count>this.length||count<0){return this;}
	var t=this.substring(0,index);
	var e=this.substring(index+count);
	return t+e;
}

String.prototype.len = function(){
    return this.replace(/[^\x00-\xff]/g,"**").length;
}

var isIE=(window.navigator.userAgent.indexOf("MSIE")>=0);
var isFirefox=(window.navigator.userAgent.indexOf("Firefox")>=0);
function $() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }

  return elements;
}
function AddFavorite(url,title) 
{ 
   url=(url)?url:location.href;
   title=(title)?title:document.title;
   if (isIE) 
   { 
      window.external.addFavorite(url,title); 
   } 
   else if (window.sidebar) 
   { 
      window.sidebar.addPanel(title,url, false); 
   } 
   else{
    alert("你的浏览器不支持该功能，请手动添加收藏。\r\n添加方法：展开浏览器的“书签”菜单，点击“(将该页)加入书签”。")
   }
} 
function SetHomePage(obj,url){
    url=(url)?url:location.href;
    if (isIE){
        obj.style.behavior='url(#default#homepage)';
        obj.setHomePage(url);
    }
    else
        alert("你的浏览器不支持该功能，请手动设置。")
}

function CenterAlign(ctl){
	if (typeof(ctl)=="string")
		ctl=$(ctl);
	if (ctl==null) return;
	with(ctl){
		if(isIE)
			style.styleFloat="left";
		else
			style.cssFloat="left";
		style.width=offsetWidth+"px";
		if(isIE)
			style.styleFloat="none";
		else
			style.cssFloat="none";
		style.margin="auto";
	}
}

function ClearItem(select){
    var len=select.options.length;
	for (var i=0;i<len;i++){
		select.remove(0);
	}
}

function SetSubList(slct1,slct2,arr){
    if (typeof(slct1)=="string")
        slct1=$(slct1);
    if (typeof(slct2)=="string")
        slct2=$(slct2);
    if (slct1==null||slct2==null)
        return;
    if (slct1.selectedIndex>=0&&slct1.selectedIndex<arr.length){
        ClearItem(slct2);
        arr=arr[slct1.selectedIndex].split(",");
        for (var i=0;i<arr.length;i++){
            slct2.options.add(new Option(arr[i],arr[i]));
        }
    }        
}

function request(paramName,url){
	var query=self.location.search.remove(0,1);
	if (url!=undefined&&url.indexOf("?")>0)
	    query=url.substring(url.indexOf("?")+1);
	if (query.length==0) return null;
	var reg = new RegExp("(^|&)"+ paramName +"=([^&]*)(&|$)","i");
	var r = query.match(reg);	
	if (r!=null) 
		return decodeURI(r[2]);
	return null;
}

function SetDropDownListByValue(drop, value, defaultValue) {
    var finded = false;
    for (var i = 0; i < drop.options.length; i++) {
        if (drop.options[i].value == value) {
            drop.selectedIndex = i;
            finded = true;
            break;
        }
    }
    if (!finded&&defaultValue)
        SetDropDownListByValue(drop, defaultValue);
    return finded;
}

function CheckTextLength(textbox,viewer){    
    try{
        if (typeof(textbox)=="string")
            textbox=$(textbox);
        var checklen=function(evt){
            evt=(evt)?evt:window.event;
            var tmp=evt.srcElement;
            if (tmp==null||typeof(tmp.value)=="undefined") return;    
            viewer=tmp.getAttribute("chker");   
            if (viewer!=undefined&&viewer!=null&&viewer!=""){
                if (typeof(viewer)=="string")
                    viewer=$(viewer);
                    if (viewer==null) return;
                viewer.innerHTML=tmp.value.len();
            }
        }
        if (isIE){
            textbox.attachEvent("onpropertychange",checklen);
        }
        else{
            textbox.addEventListener("focus",function(evt){
                                                    evt=(evt)?evt:window.event;
                                                    var tmp=evt.srcElement;
                                                    if (tmp==null||typeof(tmp.value)=="undefined") return;
                                                    window.CheckLengthInterval=setInterval(function(){
                                                                                                    viewer=tmp.getAttribute("chker");   
                                                                                                    if (viewer!=undefined&&viewer!=null&&viewer!=""){
                                                                                                        if (typeof(viewer)=="string")
                                                                                                            viewer=$(viewer);
                                                                                                            if (viewer==null) return;
                                                                                                        viewer.innerHTML=tmp.value.len();
                                                                                                    }
                                                                                                },30);
                                                    },true);
            textbox.addEventListener("blur",function(evt){clearInterval(window.CheckLengthInterval);checklen(evt)},false);
        }
        textbox.setAttribute("chker",viewer);
    }
    catch(e){
    }
}

function GetPageSize(){
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    } 
    else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } 
    else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }

    var winW, winH,pageW,pageH;
    if (self.innerHeight) { // all except Explorer
        winW = self.innerWidth;
        winH = self.innerHeight;
    } 
    else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        winW = document.documentElement.clientWidth;
        winH = document.documentElement.clientHeight;
    } 
    else if (document.body) { // other Explorers
        winW = document.body.clientWidth;
        winH = document.body.clientHeight;
    }

    // for small pages with total height less then height of the viewport
    if(yScroll < winH){
        pageH = winH;
    } 
    else {
        pageH = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if(xScroll < winW){
        pageW = winW;
    } 
    else {
        pageW = xScroll;
    }

    _pageSize = {pageWidth:pageW,pageHeight:pageH,windowWidth:winW,windowHeight:winH};
    return _pageSize;
}

function GetCurrentStyle (obj, prop) {
	if (obj.currentStyle) {
		return obj.currentStyle[prop];
	}
	else if (window.getComputedStyle) {
		prop = prop.replace (/([A-Z])/g, "-$1");
		prop = prop.toLowerCase ();
		return window.getComputedStyle (obj, "").getPropertyValue(prop);
	}
	return null;
}

function SetCwinHeight(iframe){
    if (document.getElementById){
        if (iframe && !window.opera){
            if (iframe.offsetHeight<iframe.parentElement.offsetHeight)
                iframe.height=iframe.parentElement.offsetHeight;
            if (iframe.offsetWidth<iframe.parentElement.offsetWidth)
                iframe.width=iframe.parentElement.offsetWidth;
            if (iframe.contentDocument && iframe.contentDocument.body.offsetHeight&&iframe.offsetHeight<iframe.contentDocument.body.offsetHeight){
                iframe.height = iframe.contentDocument.body.offsetHeight;
            }else if(iframe.Document && iframe.Document.body.scrollHeight&&iframe.offsetHeight<iframe.Document.body.scrollHeight){
                iframe.height = iframe.Document.body.scrollHeight;
            }
        }
    }
}
function CloseWin() {
    window.opener = null;
    //window.opener=top;     
    window.open("", "_self");
    window.close();
}
