var isIE = (document.all) ? true : false;

var isIE6 = isIE && (navigator.userAgent.indexOf('MSIE 6.0') != -1);

var $ = function (id) {
    return "string" == typeof id ? document.getElementById(id) : id;
};

var ajax = function(sURL, fnHandler){
	var xmlhttp;
	try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
	catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
	catch (e) { try { xmlhttp = new XMLHttpRequest(); }
	catch (e) { return null; }}}
	try {
		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
				fnHandler(xmlhttp.responseText);
		};
		xmlhttp.open("GET", sURL, true);
		xmlhttp.send("");
	} catch(z) { return null; }
}

var Class = {
  create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    }
  }
}

Object.extend = function(destination, source) {
	for (var property in source) {
		destination[property] = source[property];
	}
	return destination;
}


function Event(e){
	var oEvent = isIE ? window.event : e;
	if (isIE) {
		oEvent.target = oEvent.srcElement;
		oEvent.pageX = oEvent.clientX + document.documentElement.scrollLeft;
		oEvent.pageY = oEvent.clientY + document.documentElement.scrollTop;
		oEvent.charCode = (oEvent.type == "keypress") ? oEvent.keyCode : 0;
		oEvent.preventDefault = function () { this.returnValue = false; };
		oEvent.detail = oEvent.wheelDelta / (-40);
		oEvent.stopPropagation = function(){ this.cancelBubble = true; }; 
		
		if(oEvent.type == "mouseout") {
			oEvent.relatedTarget = oEvent.toElement;
		}else if(oEvent.type == "mouseover") {
			oEvent.relatedTarget = oEvent.fromElement;
		}
	}
	return oEvent;
}


function addEventHandler(oTarget, sEventType, fnHandler) {
	if (oTarget.addEventListener) {
		oTarget.addEventListener(sEventType, fnHandler, false);
	} else if (oTarget.attachEvent) {
		oTarget.attachEvent("on" + sEventType, fnHandler);
	} else {
		oTarget["on" + sEventType] = fnHandler;
	}
};

function removeEventHandler(oTarget, sEventType, fnHandler) {
    if (oTarget.removeEventListener) {
        oTarget.removeEventListener(sEventType, fnHandler, false);
    } else if (oTarget.detachEvent) {
        oTarget.detachEvent("on" + sEventType, fnHandler);
    } else { 
        oTarget["on" + sEventType] = null;
    }
};

//数据检测
//检测空值
function IsEmpty(value) {
	return /^\s*$/g.test(value);
}

//检测数字
function IsNumber(value) {
	return (!isNaN(value));
}

//检测空值或数字
function IsEmptyOrNumber(value) {
	return (IsEmpty(value) || IsNumber(value));
}

//按键检测
function CheckKeyPress(e, ActionFlag){

	var key = Event(e).charCode;
	
	switch (ActionFlag){
		case 1 ://允许小数
			return (key>=48&&key<=57||key==46||key==8||key==0);
		case 2 ://允许负数
			return (key>=48&&key<=57||key==45||key==8||key==0);
		case 3 ://只允许正整数
			return (key>=48&&key<=57||key==8||key==0);
		default :
			return true;
	}  
}

//给select添加OPTION(text在前 value在后)
function AddOption(oSelect, arr){
	for (var i = 0, len = arr.length; i < len; i++) {
		var op = document.createElement("OPTION");
		op.innerHTML = arr[i][0]; op.value = arr[i][1];
		oSelect.appendChild(op);
	}
}

//清空File类型input的值
function clearFileInput(file){
    var form=document.createElement('form');
    document.body.appendChild(form);
    var pos = file.nextSibling;
    form.appendChild(file);
    form.reset();
    pos.parentNode.insertBefore(file, pos);
    document.body.removeChild(form);
}

//获取url参数值
function GetUrlPar(par){
	var re = new RegExp(par + "=([^&]*)");
	return re.test(window.location.href.replace(/.*?\?(.*)/, "$1")) ? RegExp["$1"] : "";
}

function Each(list, fun){
	for (var i = 0, len = list.length; i < len; i++) { fun(list[i], i); }
};

function SetSelected(value, obj, fun){
	if(value != null && value != ""){
		var op = $(obj).options;
		for (var i = 0, len = op.length; i < len; i++){
			if(value == op[i].value) { op[i].selected = true; break; }
		}
		if("function" == typeof fun) fun();
	}
}

function ScalePhoto(pic, w, h){
	var scale = pic.width/pic.height;
	pic.height = h; pic.width = h * scale;
	if(pic.width > w){ pic.width = w; pic.height = w/scale; }
}

var getXmlhttp = function(){
	var xmlhttp;
	try { return new ActiveXObject("Msxml2.XMLHTTP"); }
	catch (e) { try { return new ActiveXObject("Microsoft.XMLHTTP"); }
	catch (e) { try { return new XMLHttpRequest(); }
	catch (e) { return null; }}}
}

var ajaxget = function(sURL, fnHandler, fnError){
	var xmlhttp = getXmlhttp();
	if(!xmlhttp) { alert("not support"); return; }
	try {
		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4){
				if(xmlhttp.status == 200){ fnHandler(xmlhttp.responseText); } else { fnError(); }
			}
		};
		xmlhttp.open("get", sURL, true);	//true异步，fasle同步
		xmlhttp.send(null);
	} catch(z) { return null; }
}

var ajaxpost = function(sURL, data, fnHandler, fnError){	//提交的地址，表单数据，正常时返回，错误时返回
	var xmlhttp = getXmlhttp();
	if(!xmlhttp) { alert("not support"); return; }
	try {
		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4)
				if(xmlhttp.status == 200){ fnHandler(xmlhttp.responseText); } else { fnError(); }
		};
		xmlhttp.open("post", sURL, true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlhttp.send(data);
	} catch(z) { return null; }
}

