﻿/*相对网站路径,即http://localhost/pet/,需要在jsp页面初始化初始化*/
var bashpath; 
/* 通过ID值获得对象 */
function $(i){
	return document.getElementById(i);
}
/* 封装的Ajax对象 */
function Ajax(){		
	Ajax.prototype.getXMLHttp = function(){
		var xmlHttp;
		if (window.ActiveXObject) {
				try {
						xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
				}catch (e) {
					try {
						xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
					}catch (e) {
					}
			}
		} else {
			if (window.XMLHttpRequest) {
					xmlHttp = new XMLHttpRequest();
					if (xmlHttp.overridMimeType) {
							//xmlHttp.overridMimeType("txt/html");
					}
			}
			if (!xmlHttp) {
				window.alert("Create XMLHttpRequest fail");
				return false;
			}			
		}
		return 	xmlHttp;
	}	 	
	
	
	Ajax.prototype.formToHash = function(form){	
		var oHash = {};
		var el;
		for(var i = 0,len = form.elements.length;i < len;i++){
					el = form.elements[i];
					if(el.name == "") continue;
					if(el.disabled) continue;		
					switch(el.tagName.toLowerCase()){
						case "fieldset":
							break;
						case "input":
								switch(el.type){
										case "radio":
												if(el.checked)
														oHash[el.name] = el.value;
												break;
										case "checkbox":
												if(el.checked){
					   								if(!oHash[el.name])
																oHash[el.name] = [el.value];
					   								else
																oHash[el.name].push(el.value);
												}												
												break;
										case "button":
												break;
										case "image":
												break;
										default:
												oHash[el.name] = el.value;
												break;
								}
							break;
						case "select":
							if(el.multiple){
									for(var i = 0, len = el.options.length;i < len; i++){
											if(el.options[i].selected){
													if(!oHash[el.name])
															oHash[el.name] = [el.options[i].value];
													else
															oHash[el.name].push(el.options[i].value);
											}
									}
							}else{
									oHash[el.name] = el.value;
							}
							break;
					default:
						 oHash[el.name] = el.value;
				break;
					}
		}
		el = null;
		return oHash;
	}	
	
	Ajax.prototype.formHashToArray =function(hash){		
		var a = [];
		for(var k in hash){		
		if(typeof(hash[k]) == "string"){		
			a.push(k + "=" + encodeURIComponent(hash[k]));			
		}else{			
			if(hash[k].length == undefined){			
				a.push(k + "=" + encodeURIComponent(hash[k]));			
			}else{
				for(var i = 0, len = hash[k].length; i < len; i++){
					a.push( k + "=" + encodeURIComponent(hash[k][i]));					
				}
			}
		}
	}
	return a;
	}
	
	
	
	/**提交表单*/	
	Ajax.prototype.submitForm= function(type, form, callFunc, callparam){	  
	   var xmlReq =  this.getXMLHttp();
	   if(typeof(form) == "string"){
		  form = $(form);
	   }		
	   var formHash = this.formToHash(form); 
	   var formData = this.formHashToArray(formHash);
	   xmlReq.open("post", form.action, true);
	   xmlReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
	   xmlReq.send(formData.join("&"));		 
       xmlReq.onreadystatechange = function(){
    		if(callFunc){
				callFunc.call(this,xmlReq,callparam);
			}else{
				doHander(xmlReq,callparam);
			}
	  }		
	}
	
	/*参数以{name:"x",age:1的形式传递}*/
	Ajax.prototype.submitHash= function(type,url,hash,callFunc,callparam ){
	   var xmlReq =  this.getXMLHttp();	 
	   if(!hash){
		   xmlReq.open("GET", url);
		   xmlReq.send(null); 
	   }else{
		   var data = this.formHashToArray(hash);
		   xmlReq.open("post",url,true);
		   xmlReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");			
		   xmlReq.send(data.join("&"));
	   }	   
	   xmlReq.onreadystatechange = function(){		   
			if(callFunc){
				callFunc.call(this,xmlReq,callparam);
			}else{
				doHander(xmlReq,callparam);
			}
	  }	
	}
	
	/*普通开式传递参数*/
	Ajax.prototype.submitString =  function(type,url,str, callFunc, callparam){	
	    var xmlReq =  this.getXMLHttp();
		if(!str){
			   xmlReq.open("GET", url);
			   xmlReq.send(null); 
		}else{
			   xmlReq.open("post",url,true);
			   xmlReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");	 
			   xmlReq.send(str);		 
		}			 
		xmlReq.onreadystatechange = function(){	
			if(callFunc){
				callFunc.call(this,xmlReq,callparam);
			}else{
				doHander(xmlReq,callparam);
			}	
		}	
	}
}


/*AJAX返回的回调函数*/	
function doHander(xmlReq,callparam){	  
	if(xmlReq.readyState == 1) {
		$(callparam).innerHTML=getActionInfo(1,"加载中，请稍后...")
	}
	if (xmlReq.readyState == 4 ) {	
		if (xmlReq.status == 200) {		
			$(callparam).innerHTML = xmlReq.responseText;						
		}
	}	
}

/**总入口调用提交Ajax
 * type 0字符串提交,1HASH提交,2Form表单提交
 * url 提交地址
 * param 传递参数,如果为FORM表单提交,则param为表单名称
 * callFunc 回调函数
 * callparam 回调函数参数
 * */
function sendRequest(type,url, param, callFunc, callparam){	
	if(type==0){
		new Ajax().submitString(type, url, param, callFunc, callparam);
	}else if(type==1){
		new Ajax().submitHash(type, url,  param,  callFunc, callparam);
	}else if(type==2){
		new Ajax().submitForm(type, param, callFunc, callparam);
	}
	
}





/****************************************弹出层部分*************************************/
/*
 * _id div ID
 * title 标题栏
 * msg 主体内容
 * w 长度  
 * h 宽度
 * z-index 如果同时需要求弹出多个层, z轴大小请自0开始,依次向上累加
 * 
 */
function dialog(_id, title, msg, w, h){
	var titleheight = "22"; //提示窗口标题高度 
	var bordercolor = "#979697"; //提示窗口的边框颜色 
	var titlecolor = "#fff"; //提示窗口的标题颜色 
	var titlebgcolor = "#0599EF";
	
	if (!w) w = 400; /*设置宽度默认值*/
	if (!h) h = 300; /*设置宽度默认值*/
	
			
	var bgcolor = "#FFFFFF"; 
	var iWidth = document.body.clientWidth; 
	var iHeight = document.body.clientHeight; 

	
    /*检测DIV是否存在*/
	var exists = function(){
		return document.getElementById(arguments[0]) || false;
	}	  	  	  	  
	var bgObj = "bgObj_"+_id;	
	var msgObj = "msgObj_"+_id;
	
	
	if (exists(bgObj)) document.body.removeChild(bgObj);	
	if (exists(msgObj)) document.body.removeChild(msgObj);  			
	
	/*背景遮罩层*/
	bgObj= document.createElement("div"); 		
	bgObj.id = "bgObj_"+_id;	
	bgObj.style.cssText = "position:absolute;left:0px;top:0px;width:"+iWidth+"px;height:"+Math.max(document.body.clientHeight, iHeight)+"px;filter:Alpha(Opacity=80);opacity:0.3;background-color:#eee;"; 
	document.body.appendChild(bgObj);
	
	var isIE6 = navigator.userAgent.toLowerCase().indexOf("msie 6.0") != -1;	

	/*解决IE6无法挡住select的问题*/		
	if(isIE6){
		var mask = document.createElement("iframe");		
		mask.src="javascript:''";
		mask.scrolling="no";
		mask.frameborder=1;
		mask.style.cssText = "width:100%;height:100%";  
		bgObj.appendChild(mask);
	}
	
	msgObj=document.createElement("div"); 
	msgObj.id = "msgObj_"+_id;	
	msgObj.style.cssText = "position:absolute;font:11px '宋体';top:20px;left:"+((iWidth-w)/2-50)+"px;width:"+w+"px;height:"+h+"px;text-align:center;border:1px solid "+bordercolor+";background-color:"+bgcolor+";border-right:2px ridge #999; border-bottom:2px ridge #999;padding:0px;line-height:22px;";
	document.body.appendChild(msgObj); 
	
	var titleBar = document.createElement("titleBar"); 
	titleBar.style.cssText = "width:100%; display:block; height:"+titleheight+"px;text-align:left;padding:3px 0px 0px 0px;margin:0px;font:bold 13px '宋体';color:"+titlecolor+ ";border-bottom:0px solid "+bordercolor+"; cursor:move;background-color:" + titlebgcolor+";"; 		
	
	titleBar.innerHTML ="<span style='float:left; margin-left:3px;'>"+title +"</span><span style='font-size:15px;cursor:pointer;margin-right:5px; float:right;' onClick='closeBtn(\""+_id+"\");' >×</span>";	
	msgObj.appendChild(titleBar);	
	var msgBox = document.createElement("msgBox");
	msgBox.id	=	_id;	
	msgBox.style.overflow="scroll";
	msgBox.style.cssText="overflow-x:hidden; font-size:12px; display:block; margin:0 auto; padding-top:10px; height:"+(h-titleheight-5)+"px;";	
	msgBox.innerHTML = msg;		
	msgObj.appendChild(msgBox);

	/*拖动*/
	var moveX = 0; 
	var moveY = 0; 
	var moveTop = 0; 
	var moveLeft = 0; 
	var moveable = false;
	addEvent(titleBar, "mousedown", mousedown);
	addEvent(msgObj, "mouseup", mouseup);
	//addEvent(msgObj, "selectstart", function() {return false;});
	addEvent(msgObj, "mousemove", mousemove);
	var resizeEvent;
	
	addEvent(window, "resize", function() {		
			bgObj.style.width =  document.body.clientWidth + "px";
			bgObj.style.height = document.body.clientHeight + "px";			
		});
	
	function mousedown() {			    
		moveable = true; 
		resizeEvent=window.onresize;
		window.onresize = function() {
			bgObj.style.width =  document.body.clientWidth + "px";
			bgObj.style.height = document.body.clientHeight + "px";			
		}
		if (getBrowser()=="IE") msgObj.setCapture();
		var evt = getEvent(); 
		moveX = evt.clientX; 
		moveY = evt.clientY; 
		moveTop = parseInt(msgObj.style.top); 
		moveLeft = parseInt(msgObj.style.left);
	} 


	
	function mousemove() {
		if (moveable) { 
			var evt = getEvent(); 
			var x = moveLeft + evt.clientX - moveX; 
			var y = moveTop + evt.clientY - moveY; 
			if(y<0 || x<0)return;
			if(x> iWidth)return;
			msgObj.style.left = x + "px"; 
			msgObj.style.top = y + "px"; 		
		} 
	}
	
	function mouseup() {
		if (moveable) { 
			moveX = 0; 
			moveY = 0; 
			moveTop = 0; 
			moveLeft = 0; 
			moveable = false; 
			window.onresize = resizeEvent;
			if (getBrowser()=="IE") msgObj.releaseCapture();
		} 
	}
} 

/*关闭弹出层*/
function closeBtn(_id){ 
	   /*检测DIV是否存在*/	  
	  var exists = function(){
 		return document.getElementById(arguments[0]) || false;
	  }	
	  var obj = "bgObj_" + _id;	 
	  if (exists(obj)) document.body.removeChild(exists(obj));	 
	  obj = "msgObj_"+_id;
	  if (exists(obj)) document.body.removeChild(exists(obj));		  
} 

/**
 * 提示框
 * @param _id 提示框ID
 * @param msg 消息内容
 * @param fn 完成之后调用的函数
 * @param param 函数内容
 * @return
 */
function DConfirm(_id, msg, fnOK, okparam, fnCancel, canCelparam){
	 var arr=[];
	 arr.push("<div style='margin:0 auto'>");
	 arr.push("<div style='font-size:15px; border:0px solid #ADDFF2;margin-bottom:10px'>"+msg+"</div>");
	 arr.push("<div>");
	 arr.push("<input style='border: #999999 1px solid;' id =submit_"+_id+" type='button'   onClick='closeBtn(\""+_id+"\");' value='确&nbsp;&nbsp;定'>");
	 arr.push("<input style='border: #999999 1px solid;' id =Cancel_"+_id+" type='button'  onClick='closeBtn(\""+_id+"\");' value='取&nbsp;&nbsp;消'>");
	 arr.push("</div></div>");	 
	 var box = new dialog(_id,"提示",arr.join("\n"), 250, 120);
	 /*给确定按钮绑定事件*/
	 var OK =  "submit_"+_id;	
	 var Cancel ="Cancel_"+_id;
	 if(fnOK){
		 function addFnOK() {
			 fnOK.call(this,okparam);
		 }	
		 addEvent(document.getElementById(OK),"click", addFnOK);
	 }
	 if(fnCancel){
		 function addFnCancel() {
			 fnCancel.call(this,canCelparam);
		 }		
		 addEvent(document.getElementById(Cancel),"click", addFnCancel);
	 }	
}
/**
 * 提示框
 * @param _id 提示框ID
 * @param msg 消息内容
 * @param fn 完成之后调用的函数
 * @param param 函数内容
 * @return
 */
function DAlert(_id, msg, fn, param){
	 var arr=[];
	 arr.push("<div style='margin:0 auto'>");
	 arr.push("<div style='font-size:15px; border:0px solid #ADDFF2;margin-bottom:10px'>"+msg+"</div>");
	 arr.push("<div>");
	 arr.push("<input style='border: #999999 1px solid;' id =submit_"+_id+" type='button' onClick='closeBtn(\""+_id+"\");' value='确&nbsp;&nbsp;定'>");
	 arr.push("</div></div>");	 
	 var box = new dialog(_id,"提示",arr.join("\n"), 250, 120);
	 /*给确定按钮绑定事件*/
	 var OK =  "submit_"+_id; 
	 if(fn){
		 function addFn() {
			 fn.call(this,param);
		 }
		 addEvent(document.getElementById(OK),"click", addFn);
	 }
}



/**
 * 提示信息封装
 * @param type 1大LOAD图标,2小LOAD图标,3正确图标,4错误图标
 * @param msg
 * @return
 */

function getActionInfo(type, msg){
	if(msg==null){
		msg ="";
	}
	if(type==1){
	    return "<img style='margin:0 auto;' src='"+bashpath+"/theme/images/load.gif'/>" + msg;
	}else if(type==2){
		 return "<img src='"+bashpath+"/theme/images/check_loader.gif'/>" + msg;
	}else if(type==3){
		 return "<img src='"+bashpath+"/theme/images/check_right.gif'/>" + msg;
	}else if(type==4){
		return "<img  src='"+bashpath+"/theme/images/check_error.gif'/>" + msg;
	}else {
		return msg;
	}
	
}






/****************************************常用函数部分*************************************/

/*获得事件Event对象，用于兼容IE和FireFox*/ 
function getEvent() { 
	return window.event || arguments.callee.caller.arguments[0]; 
} 

/*事件绑定,FF和IE通用*/
function addEvent(obj,eventTypeName, fn){
	if(obj.addEventListener){
			obj.addEventListener(eventTypeName,fn,true);
			return true;
	}else if(obj.attachEvent){
			return obj.attachEvent("on"+eventTypeName,fn);
	}else{
		return false;
	}
}


/*检测浏览器类型*/
function getBrowser() {
	if ((navigator.userAgent.indexOf("MSIE") >= 0) && (navigator.userAgent.indexOf('Opera') < 0)) {
			return "IE"; 
	}else if (navigator.userAgent.indexOf("Firefox") >= 0) {
			return "Firefox"; 
	}else if (navigator.userAgent.indexOf("Opera") >= 0){
		return "Opera"; 
	}else {
		return "Other";
	}
}

/*设置鼠标移动颜色*/
function setCursoBgColor(obj,isOver){
	if(isOver){/*mouseOver*/
		obj.style.cursor="hand";		
		obj.style.backgroundColor="#66FFFF";
	}else{/*mouseOut*/
		obj.style.cursor="auto";		
		obj.style.backgroundColor="#CEEBFF";
	}
}



/*去除左右全部空格*/
String.prototype.trim = function(){ return this.replace(/^\s+|\s+$/g,"")} 

/*去除左边空格*/
String.prototype.ltrim = function(){ return this.replace(/^\s+/g,"")} 

/*去除右边空格*/
String.prototype.rtrim = function(){ return this.replace(/\s+$/g,"")}


/*输入验证,检测是否为空和是否为数字.为空或不是数字则返回false，否则返回true*/
/*type为验证的方式，1为验证空和是否为数字，2表示只验证是否为空*/
function validata(type, value) {
	var flag = false;
	if(type==1) {
		if(value == null || value == "") {
			flag = false;
		}else {
			var pattern = /^(\d){1,}$/;
			flag = pattern.test(value);
		}
	} else if(type==2){
		if(value != null && !value == "") {
			flag = true;
		}
	}
	return flag;
}
/*输入验证,检测是否为空和是否为数字.为空或不是数字则返回false，否则返回true*/
/*type为验证的方式，1为验证空和是否为数字，2表示只验证是否为空*/
function validataMsg(type, valueObj, msg) {
	if(validata(type,valueObj.value)) {
		$("validataMsg").innerHTML = "";
		$("dialogSubmit").removeAttribute("disabled");
	} else {
		$("validataMsg").innerHTML = msg;
		$("dialogSubmit").setAttribute("disabled","disabled");
	}
}








