﻿
var $ = function(elstr) {
    if (typeof elstr == 'string') {
        return document.getElementById(elstr);
    } else {
        return elstr;
    }
}

if(!Number.prototype.toFixed)   
{
	Number.prototype.toFixed=function(num)   
	{
		with(Math) return round(this.valueOf()*pow(10,num))/pow(10,num);   
	}   
}

function checkByteLength(str,minlen,maxlen) 
{
	if (str == null) return false;
	var l = str.length;
	var blen = 0;
	for(i=0; i<l; i++) 
	{
		if ((str.charCodeAt(i) & 0xff00) != 0) 
		{
			blen ++;
		}
		blen ++;
	}
	if (blen > maxlen || blen < minlen) 
	{
		return false;
	}
	return true;
}

function getByteLength(str) 
{
	if (str == null) return false;
	var l = str.length;
	var blen = 0;
	for(i=0; i<l; i++) 
	{
		if ((str.charCodeAt(i) & 0xff00) != 0) 
		{
			blen += 0.5;
		}
		blen += 0.5;
	}
	return blen;
}

String.prototype.trim = function()
{
	return this.replace(/(^\s*)|(\s*$)/g,'');
}

BaseClass = function()
{
    this.ModalDialog       =           ModalDialog; 
}

function Main()
{
	this.c=BaseClass;
	this.c();
}

function ModalDialog(pName,diaLogName,diaWidth,diaHeight)
{
	var obj = null;
	if (document.all)
	{
		obj=window.showModalDialog(pName,diaLogName,'help=0;status=0;dialogWidth='+diaWidth+';dialogHeight='+diaHeight+''); //px
	}
	else
	{
		obj = window.open(pName,null,'width='+diaWidth.replace('em','')+',height='+diaHeight.replace('em','')+',modal=yes');
		//var evt = SearchEvent();
		//window.onclick = function() {obj.focus();}
		//evt.cancelBubble = true;
	}
	return obj;
}


function GetParaStr(strname)
{
	var hrefstr,pos,parastr,para,tempstr;
	hrefstr = window.location.href;
	pos = hrefstr.indexOf("?")
	parastr = hrefstr.substring(pos+1);
	para = parastr.split("&");
	tempstr="";
	for(i=0;i<para.length;i++) 
	{
		tempstr = para[i];
		pos = tempstr.indexOf("=");
		if(tempstr.substring(0,pos) == strname)
		{
			tempstr = tempstr.substring(pos+1);
			pos = tempstr.indexOf("#");
			if (pos > 0)
				return tempstr.substring(0,pos);
			return tempstr;
		}
	}
	return '';
}


function isDigital(obj)
{
	if (obj.value.trim() == '' || obj.value.trim().indexOf('.')==0 || isNaN(obj.value.trim()))
	{
		alert('请输入数字型数据!');
		obj.focus();
		return false;
	}
	return true;
}

function cfrEmail(obj)
{
	var RegMail = /^[_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]*)*@[a-zA-Z0-9\-]+([\.][a-zA-Z0-9\-]+)+$/;
	if (!RegMail.test(obj.value.trim()))
	{
		alert('邮箱格式出错!');
		obj.focus();
		return false;
	}
	return true;
}

function isDate(obj,ty)
{
	///^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/; 2008-05-06 12:30:46
	var patn ; ///^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/; //2007-08-09
	if (ty.toLowerCase() == 'long')
		patn = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
	else
		patn = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/; 
	if (!patn.test(obj.value.trim()))
	{
		alert('时间格式错误!');
		obj.focus();
		return false;
	}
	return true;
}

function SearchEvent() 
{ 
	if(document.all)
		return window.event;
	func=SearchEvent.caller; 

	while(func!=null) 
	{ 
		var arg0=func.arguments[0]; 
		//alert(arg0);
		if(arg0) 
		{ 
			//alert(arg0.constructor);
			//if(arg0.constructor==MouseEvent || arg0.constructor==Event || arg0.constructor==KeyboardEvent) // 如果就是event 对象 
			return arg0; 
		} 
		func=func.caller; 
	} 
	return null; 
}

function $X()
{
	if (document.all)
	{
		return event.clientX;
	}
	else
	{
		var evt = SearchEvent();
		return evt.pageX;
	}
}

function $Y()
{
	if (document.all)
	{
		return event.clientY;
	}
	else
	{
		var evt = SearchEvent();
		return evt.pageY;
	}
}

function srcState()
{
	var evt = SearchEvent();
	var El = null;
	if (typeof evt != 'undefined')
	{
		if (document.all)
			El = evt.srcElement.id;
		else
			El = evt.target.id; 
	}
	return El;
}

function srcType()
{
	var evt = SearchEvent();
	var El = null;
	if (typeof evt != 'undefined')
	{
		if (document.all)
			El = evt.srcElement;
		else
			El = evt.target; 
	}
	return El;
}

if(typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement)
{

	HTMLElement.prototype.insertAdjacentElement = function(where,parsedNode)
	{
		switch (where)
		{
			case 'beforeBegin':
				this.parentNode.insertBefore(parsedNode,this)
				break;
			case 'afterBegin':
				this.insertBefore(parsedNode,this.firstChild);
				break;
			case 'beforeEnd':
				this.appendChild(parsedNode);
				break;
			case 'afterEnd':
				if (this.nextSibling) this.parentNode.insertBefore(parsedNode,this.nextSibling);
				else this.parentNode.appendChild(parsedNode);
				break;
		}
	}

	HTMLElement.prototype.insertAdjacentHTML = function(where,htmlStr)
	{
		var r = this.ownerDocument.createRange();
		r.setStartBefore(this);
		var parsedHTML = r.createContextualFragment(htmlStr);
		this.insertAdjacentElement(where,parsedHTML)
	}

	HTMLElement.prototype.insertAdjacentText = function(where,txtStr)
	{
		var parsedText = document.createTextNode(txtStr)
		this.insertAdjacentElement(where,parsedText)
	}

}

//selection
function returnSelection(Edit)
{
	if (document.selection)
		return Edit.document.selection.createRange()
	else if (window.getSelection)
		return Edit.window.getSelection()
	else if (document.getSelection)
		return Edit.document.getSelection()
	else
		return null;
}

function getElPosi(e)
{
    var t=e.offsetTop; 
    var l=e.offsetLeft; 
    while(e=e.offsetParent) 
    { 
       t+=e.offsetTop; 
       l+=e.offsetLeft; 
    } 

    return {Top:t,Left:l} 
}

function CrtElement(tag, json, pnode)
{
    var _node = document.createElement(tag);
    for (itm in json)
    {
        var N = json[itm];
        switch (typeof N)
        {
            case "number":
            case "string":
                if (itm == 'innerHTML')
                    _node.innerHTML = N;
                else if (itm == 'className')
                    _node.setAttribute('class',N);
                else
                    _node.setAttribute(itm,N);
                break;
            case "function":
                if (document.all)
                {
                    try {
                        _node.attachEvent('on'+itm, N);
                    }catch (e) {
                        _node['e'+itm+N] = N;
		                _node[itm+N] = function() { _node['e'+itm+N](window.event); }
                        _node.attachEvent('on'+itm,_node[itm+N]);
                    }
                }
                else if (document.addEventListener)
                {
                    _node.addEventListener(itm, N, false);
                }
                else {
                    obj['on'+itm] = N;
                }
                break;
            case "object":
                for (style in N)
                {
                    _node[itm][style] = N[style];
                    
                    if (style == 'float') {
                        if (document.all)
                            _node.style.styleFloat = N[style];
                        else
                            _node.style.cssFloat = N[style];
                    }
                }
                break;
        }

    }
    pnode.appendChild(_node);
    
    _node.remove = function()
    {
        this.parentNode.removeChild(this);
    }
    _node.setXY = function(x, y)
    {
        this.style.left = x+'px';
        this.style.top = y+'px';
    } 
    _node.setView = function(flag)
    {
        if (flag)
            this.style.display = '';
        else
            this.style.display = 'none';
    }
    _node.setWH = function(w, h) {
        this.style.width = w + 'px';
        this.style.height = h + 'px';
    }
    _node.backImg = function(img) {
        if (typeof img=='undefined'||img==null||img==''){
            var str = this.style.backgroundImage;
            return str.substring(4,str.length-1);
        }
        else
            this.style.backgroundImage = 'url('+img+')';
    }
    _node.setHTML = function(str) {
        this.innerHTML = str;
    } 
    
    return _node;
}

function setaddfirst(obj)
{
    window.external.addFavorite('http://www.ikuzone.com','酷我地带IKuZone');
    obj.style.behavior='url(#default#homepage)';
    obj.setHomePage('http://www.ikuzone.com');
}
