﻿//
//获取页内对象
//
//@e    :对象的ID或NAME值(或对象本身)
//@frm  :对象所有的窗体或框架(只可以是对象)(可选项)
function $(e,frm) 
{
    if(frm == null)
        frm = self;
        
    if (typeof(e) == 'string')
        return frm.document.all[e];
    else
        return e;
}

function P$(e)
{
    return $(e,parent);
}

//
//获取某一个框架
//
//@e        :框架的ID或NAME(或对象本身)
//@p_frm    :框架的父框架或窗体(只可以是对象)(可选项)
function _(e,p_frm) //@f:p_frm
{
    var frm;
    
    if(p_frm == null)
        p_frm = self;
    
    if(typeof(e) == 'string')
        frm = p_frm.frames[e];
    
    if(frm == null)
        return null;
    
    frm.$ = function(e1)
    {
        if (typeof(e1) == 'string')
            return frm.document.all[e1];
        else
            return e1;
    }
    
    var my = this;    
    
    return frm;
}

//
//根据控件的属性值来获取控件(控件类型由t决定)
//
//@p    :属性名
//@v    :属性值(如果==null,则返回具有@p的所有对象)
//@t    :标记(要查询的对象的标记名,如 input)
//@c    :容器(相当于范围,提共该参数是为了支持小范围快速查询)
function $PV(p,v,t,c)
{
	if(t == null)
		t = "input";
	
	if(c == null)
		c = document;
		
	var list0 = c.getElementsByTagName(t);
	var list1 = new Array();
	
	var len = list0.length;
	
	for(var i=0; i<len; i++)
	{
	    if(v != null)
	    {
		    if(list0[i][p] == v)
			    list1.push(list0[i]);
	    }
	    else
	    {
	        if(list0[i][p] != null)
			    list1.push(list0[i]);
	    }
	}
	
	return list1;
}

//
//if(val == null) $R as getter
//if(val != null) $R as setter
//
function $R(name ,val)
{
    var e = $PV("name",name,"input");
   
    if(val == null)
	{
	    $Foreach(e,function(e1){
	        if(e1.checked)
	            val = e1.value;
	    });
	}
	else
	{
		$Foreach(e,function(e1){
	        if(e1.value == val)
	            e1.checked = true;
	    });
	}
        
    return val;
}

//----------------------------------
//
//导入某一个脚本文件
//
function $Import(path,key)
{
    if(key !== null)
    {
        if(window.JS_IMPORTS == null)
            window.JS_IMPORTS = new HashTable();
        
        if(window.JS_IMPORTS.contain(key) == false)
            window.JS_IMPORTS.add(key,'1');
        else
            return;
    }
    
    document.write("<" + "script type=\"text/javascript\" src=\"" + path + "\"></" + "script>");
    
    if($Import.onimport != null)
        $Import.onimport();
}

function $Foreach(list1,func)
{   
	var list = list1;
	
    if(list == undefined || list == null)
        return;
        
    if(list.length == undefined)
        func(list);
    else
    {
        var len = list.length;
        
        for(var i=0; i<len; i++)
            func(list[i],i);
    }
}
//
//虚拟类,不具有继随的概念
//
var Abstract = {
	create: function() 
	{
		var temp = function(){}
		
		temp.extend = function(source)
		{
			for (property in source) 
				temp.prototype[property] = source[property];
		}
		
		return temp;
	}
}

//
//类
//
var Class = {
	create: function() 
	{
		var temp = function() 
		{
			this.initialize.apply(this, arguments);
		}
		temp.prototype.base = null;
		temp.inherits = function(base)
        {       	
	        var p = base.prototype;
        	
	        for(p1 in p)
	            temp.prototype[p1] = p[p1];
	        						
	        temp.prototype.base = p;
        }
				
		temp.extend = function(source)
		{
			for (property in source) 
				temp.prototype[property] = source[property];
		}
		
		return temp;
	}
}

//----------------------------------

//
//把一个字符串,转为XML标准的字符串
//
function xmlEscape(text)
{    
    text = text.replace(/&/g,"&amp;");
    text = text.replace(/</g,"&lt;");
    text = text.replace(/>/g,"&gt;");
    text = text.replace(/\r\n/g,"\\r\\n");
    
    return text;
}

//
//把一个XML字符串,还原回去
//
function xmlUnescape(text)
{
    text = text.replace(/&lt;/g,"<");
    text = text.replace(/&gt;/g,">");
    text = text.replace(/&amp;/g,"&");
    text = text.replace(/\\r\\n/g,"\r\n");
    
    return text;
}
//
//对string对象进行扩展
//
String.prototype.trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.trimStart = function()
{
    return this.replace(/(^\s*)/g, "");
}
String.prototype.trimEnd = function()
{
    return this.replace(/(\s*$)/g, "");
}
//
//对array 对象进行扩展
//
Array.prototype.removeAt=function(index)
{
    if(isNaN(index) || index>this.length)
		return false;
	
	if(index < 0)
	    return false;
    
    var len = this.length -1 ;
    
    for(var i= index; i < len; i++)
    {
        this[i] = this[i+1];
    }
    
    this.pop();
 }
 
 Array.prototype.indexOf = function(e)
 {
    var len = this.length;
    
    for(var i=0; i<len; i++)
    {
        if(this[i] == e)
            return 0;
    }
    
    return -1;
 }
 
Array.prototype.clear = function()
{
    var len = this.length;
    
    while(len)
    {
        this.pop();
        len--;
    }
}

//
//哈西表对象
//
var HashTable = Class.create();
HashTable.extend({
	initialize : function()
	{	
	    this.list = new Array();	
		this.onchange = null;
		this.length   = 0;
	},
	toString : function()
	{
	    return "[HashTable]";
	},	
	
	add : function(key,value)
	{
		if(key == null || key.length == 0)
			return false;
			
		if(this.contain(key))
			return false;
			
		var temp     = [key,value];
		temp.key    = key;
		temp.value = value;
		temp.toString = function(){return temp.key + ":object"}
		
		this.list.push(temp);
		
		this.length++;
		
		//
		//加入对个数变换事件的支持
		//
		if(this.onchange != null)
		    this.onchange("+");//+:说明是由加引起的
		
		return true;
		 
	},
	remove : function(key)
	{
		var index = this.indexOf(key);
		
		if(index > -1)
		{
			this.removeAt(index);
			
			//
		    //加入对个数变换事件的支持
		    //
		    if(this.onchange != null)
		        this.onchange("-");//-:说明由减引起的
			
			return index;
		}
		else
			return -1;
	},
	
	removeAt : function(index)
    {
        this.list.removeAt(index);
        
        this.length--;
        
        //
	    //加入对个数变换事件的支持
	    //
	    if(this.onchange != null)
	        this.onchange("-");//-:说明由减引起的
    },
	
	contain : function(key)
	{
		if(this.indexOf(key) > -1)
			return true;
		else
			return false;
	},
	
	indexOf : function(key)
	{
		var temp = -1;
		
		$Foreach(this.list,function(e,index)
		{
			if(e.key == key)
				temp = index;
		});
		
		return temp;
	},
	
	clear : function()
	{
	    this.list.clear();
	    
	    this.length = 0;
	},
	item : function(key)
	{
	   var index;
		
		if(typeof(key) == "string")
		    index = this.indexOf(key);
		else
		    index = key;
		
		if(index >= 0)
			return this.list[index];
		else
			return null;
	},
	
	$V : function(key)
	{
		var temp = this.item(key);
		
		if(temp == null)
		    return null;
		else
		    return temp.value;
	}
});

Object.extend = function(destination, source) {
  for (property in source) {
    destination[property] = source[property];
  }
  return destination;
}

Function.prototype.bind = function(object) {
  var __method = this;
  return function() {
    return __method.apply(object, arguments);
  }
}

Function.prototype.bindAsEventListener = function(object) {
  var __method = this;
  return function(event) {
    return __method.call(object, event || window.event);
  }
}

var Try = {
  these: function() {
    var returnValue;

    for (var i = 0; i < arguments.length; i++) {
      var lambda = arguments[i];
      try {
        returnValue = lambda();
        break;
      } catch (e) {}
    }

    return returnValue;
  }
}
