/** 一些方法 prototype.js为基类
 */

/**
 * @classDescription 判断是否是正确格式的email
 * @author ice deng
 * @param {String} emailStr 
 * @return (Boolean)
 */
function IsEmail(emailStr){
    var re=/^[\w-]+(\.*[\w-]+)*@([0-9a-z]+(([0-9a-z]*)|([0-9a-z-]*[0-9a-z]))+\.)+[a-z]{2,3}$/i;
    if(re.test(emailStr))
        return true;
    else
        return false;
};
/**
 * @classDescription 判断是否含有汉字
 * @author ice deng
 * @param {String} str
 * @return (Boolean)
 */
function checkchar(str){
	if(/[^\x00-\xff]/g.test(str))
		return false;
	else
		return true;
};
//参数:后缀名
var ImgExtError = "上传的图片格式只能是：.jpg、.gif、.bmp、.png";
/**
 * @classDescription 判断图片格式是否符合要求
 * @author ice deng
 * @param {String} str 图片路径
 * @return (Boolean)
 */
function checkImgExt(){
	try{
		var tmp = arguments[0].toLowerCase();
		if(tmp != "jpg" && tmp != "gif" && tmp != "bmp" && tmp != "png"){
			return false;
		}
	}catch(e){
		return false;
	}
	return true;
};
/**
 * @classDescription 移出数组中指定的索引的
 * @author ice deng
 * @param (Numbe) 移出的索引值
 */
Array.prototype.remove = function(){
	var index = arguments[0];
	var _array = this;
	var tmp_array = new Array();
	for(var i = 0; i< _array.length;i++){
		var l = 0;
		try{l=tmp_array.length;}catch(e){l=0;}
		if(i != index)
			tmp_array[l] = _array[i];
	}
	for(var j = 0; j<tmp_array.length; j++)
		this[j] = tmp_array[j];
	this.length--;
};
/**
 * @classDescription 去出数组中重复的项，此方法很笨，没优化过
 * @author ice deng
 */
Array.prototype.removeRepeat=function(){
	var $array=this;
	var tmp_array=new Array();
	for(var i=0;i<$array.length;i++){
		var value=$array[i];
		if(tmp_array.indexOf(value)==-1)
			tmp_array.push(value)
	}
	for(var j = 0; j<tmp_array.length; j++)
		this[j] = tmp_array[j];
	this.length=tmp_array.length;
};
/**
 * @classDescription 获取滚动条上边距
 * @author ice deng
 * @return (Array)
 */
function getPageScroll(){
	var yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}
	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
};
/**
 * @classDescription 获取窗口大小
 * @author ice deng
 * @return (Array)
 */
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 windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
};
/**
 * @classDescription 自动适应iframe高度
 * @author ice deng
 */
function iframeAutoFit(){}
iframeAutoFit.prototype={
	Init:function(){
		Event.observe(window, "load", arguments[0], false);
	},
	Auto:function(){
		if(self!=top){
			try{
				var _iframe = parent.$(window.name);
				if(_iframe){
					var arrayPageSize = getPageSize();
					_iframe.style.height = arrayPageSize[1] + "px";
				}
			}catch(e){alerts(e);}
		}
	}
};
/**
 * @classDescription 判断字符串是否为空
 * @author ice deng
 * @return (Boolean)
 */
function isNull(){
	var value = arguments[0];
	if(value == undefined || value == "undefined" || value == "" || value.length < 1)
		return true;
	return false;
};
/**
 * @author ice deng
 * @classDescription 判断是否为 Number 类型
 * @return (Boolean)
 */
function isNumber(){
	var tmp=arguments[0];
	if(typeof(tmp) != "number" || isNaN(tmp))
		return false;
	else
		return true;
};
/**
 * @author ice deng
 * @classDescription 复制内容到剪贴板中
 * @param {Object} meintext 需复制的内容
 * @return (Boolean)
 */
function setCopy(meintext){
 	if(window.clipboardData){
		window.clipboardData.setData("Text", meintext);
	}else {
		if(!setCopy.isInit){
			Body = document.getElementsByTagName("body").item(0);
			var flashCopier = document.createElement("div");
			flashCopier.setAttribute("id","flashcopier");
			Body.appendChild(flashCopier);
			setCopy.isInit = true;
		}
		$("flashcopier").innerHTML = "<embed src=\"/swf/copy.swf?flashcopier="+meintext+"\" width=\"0\" height=\"0\" type=\"application/x-shockwave-flash\"></embed>";
	}
 return true;
};
setCopy.isInit = false;
/**
 * @author ice deng
 * @classDescription 把连接加入收藏
 * @param {Object} url 地址
 * @param {Object} title 标题
 * @return (Boolean)
 */
function setHome(url,title){
	try{
		if(window.sidebar && "object" == typeof( window.sidebar ) && "function" == typeof( window.sidebar.addPanel )){
			window.sidebar.addPanel(title,url, "");
		}else if( document.all && "object" == typeof( window.external )){
			window.external.addFavorite(url,title);
		}
	}catch(e){}
};
/**
 * @author zq
 * @classDescription 去除字符串的前后空白符
 * @param {Object} inputString
 * @return (String)
 */
function trim(inputString){
  if(typeof inputString != "string") { return inputString; }
  var retValue = inputString;
  var ch = retValue.substring(0, 1);
  while (ch == " ") { 
	//检查字符串开始部分的空格
	  retValue = retValue.substring(1, retValue.length);
	  ch = retValue.substring(0, 1);
  }
  ch = retValue.substring(retValue.length-1, retValue.length);
  while (ch == " ") {
	 //检查字符串结束部分的空格
	 retValue = retValue.substring(0, retValue.length-1);
	 ch = retValue.substring(retValue.length-1, retValue.length);
  }
  while (retValue.indexOf("  ") != -1) { 
	//将文字中间多个相连的空格变为一个空格
	 retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); 
  }
  return retValue;
};
/**
 * @author ice deng
 * @classDescription 切取html、补充完整 (<br><img ><col> <hr> input)
 * @param {Object} input
 */
function SupplyHtml(input){
	try{
		var up=input.lastIndexOf("<");
		var next=input.lastIndexOf(">");
		if (up==-1 && next==-1)
			return input;
		try{
			if (up>next)
				input=input.substring(0, up);
			var regexp=/<[^<>]*>/ig;
			var matchs=input.match(regexp);
			if(matchs==null)
				return input;
			var stack=new Array();
			for(var i=0;i<matchs.length;i++){
				var tmp = matchs[i].toLowerCase();
				if(tmp.substr(0,2)=="</"){
					stack.pop();
				}else{
					if(!(tmp.substr(0,3)=="<br" || tmp.substr(0,4)=="<img" || tmp.substr(0,4)=="<col" || tmp.substr(0,3)=="<hr" || tmp.substr(0,6)=="<input")){
						if(tmp.indexOf(" ") != -1)
							tmp = tmp.substring(0,tmp.indexOf(" ")) + ">";
						stack.push(tmp);
					}
				}
			}
			while(stack.length>0){
				var tmp = stack.pop().toString();
				input += tmp.substr(0,1) + "/" + tmp.substr(1);
			}
		}catch(e){alerts(e);}
	}catch(e){alerts(e);}
	return input;
};
/**
 * 获取对象的位置.
 * @author ice
 * @param {Object} o
 */
function getInfo(o){
	var to=new Object();
	to.left=to.right=to.top=to.bottom=0;
	var twidth=o.offsetWidth;
	var theight=o.offsetHeight;
	if(o.style.position.toLowerCase()=="relative" || o.style.position.toLowerCase()=="absolute"){
		to.left+=o.offsetLeft;
		to.top+=o.offsetTop;
	}else{
		while(o!=document.body){
			try{
				to.left+=o.offsetLeft;
				to.top+=o.offsetTop;
				o=o.offsetParent;
			}catch(e){break;}
		}
	}
	to.right=to.left+twidth;
	to.bottom=to.top+theight;
	return to;
}
function contains(e,parent){
	e = e ? e : (window.event ? window.event : null); 
	if(Browser.isIE){
		return $(parent).contains(e.srcElement);
	}else{
		return FFcontains($(parent),e.target);
	}
};
/**
 * @author ice deng
 */
var db_ajax=Class.create();
db_ajax.prototype={
	initialize:function(url,data){
		this.url = url;
		this.data = data;
		this.objX = this.init();
	},
	init:function(){
		var tmp=false;
		var X;
		var msxmls=["MSXML3", "MSXML2", "Microsoft"];
		for(var i=0 ; i<msxmls.length && !tmp; i++){
			try{
				X=new ActiveXObject(msxmls[i]+".XMLHTTP");
				tmp=true;
			}catch(e){
				tmp=false;
				X=null;
			}
		}
		if(!X && typeof XMLHttpRequest != "undefined"){
			try{
				X=new XMLHttpRequest();
			}
			catch(e){}
		}
		return X;
	},
	send:function(){
		var method;
		if(arguments[1] != 1)
			method="POST";
		else
			method="GET";
		this.objX.open(method,this.url,false);
		this.objX.setRequestHeader("Method", "POST " + this.url + " HTTP/1.1");
		this.objX.setRequestHeader("Content-Length",this.data.length);
		this.objX.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		this.objX.send(this.data);
		var re;
		if(arguments[0] != 1)
			re=this.objX.responseXML;
		else
			re = this.objX;
		return re;
	}
};
function FFcontains(parent,child){
	if(!child) return false;
	if(parent == child) return true;
	var o_tag = child.tagName;
	var o = parent.getElementsByTagName(o_tag);
	var l = o.length;
	for (var i=0; i<l; i++)
	{
		if (o[i] == child)
		{
			return true;
		}
	}
	return false;
};
/**
 * @projectDescription 为了兼容，相当于IE的outerHTML.
 * @param {Object} element
 */
function GetOuterHTML(element)
{
	return document.createElement("DIV").appendChild(element.cloneNode(true)).parentNode.innerHTML;
};
//图片插入小图片
function InsertSmallToImgUrl(imgUrl,small){
	var index = imgUrl.lastIndexOf(".");
	imgUrl = imgUrl.substring(0,index)+small+imgUrl.substr(index);
	return imgUrl;
}
function cancelBubble(e){
e = e?e:window.event;
e.cancelBubble = true;
}
