////////////////////////////////////////////////////
/*
*   Copyright By www.jspsoft.cn
*
*	版权所有   support@jspsoft.cn
*
*/
///////////////////////////////////////////////////
//导入进度条组件脚本
document.write("<script language=javascript src='/script/util/progress.js?rnd="+Math.random()+"'></script>");
//导入哈希表组件脚本
document.write("<script language=javascript src='/script/util/hashtable.js?rnd="+Math.random()+"'></script>");
//ajax提交处理action页面
var post_action_url="/pub/post/";
//隐藏进度条快捷键
document.onkeydown=function(){if(event.keyCode==27)try{if(progressDisplay)progressDisplay.hide();}catch(exp1){}};
//刷新鼠标双击模块
document.ondblclick=function(){refreshBlockByElement(event.srcElement);}
//根据元素ID获取对象和对象集合
function $(elemid){return document.all(elemid);}
//得到元素绝对坐标
function getAbsolutePosition(element){
	var x=0;
	var y=0;
	do{
		x += element.offsetLeft;
		y += element.offsetTop;
	}while((element=element.offsetParent));
	var obj=new Object();
	obj.x=x-document.body.scrollLeft;
	obj.y=y-document.body.scrollTop;
	return obj;
}
//得到元素绝对坐标
function getElementRect(element){
	if(!element)return null;
	var x1=0;
	var y1=0;
	var obj=new Object();
	obj.w=element.offsetWidth;
	obj.h=element.offsetHeight;
	do{
		x1 += element.offsetLeft;
		y1 += element.offsetTop;
		if(element.style.borderLeftWidth)x1+=parseInt(element.style.borderLeftWidth);
		if(element.style.borderTopWidth)y1+=parseInt(element.style.borderTopWidth);
	}while((element=element.offsetParent));
	obj.x=x1;
	obj.y=y1;
	return obj;
}
//等比缩放图片
function resizeImage(ImgObj,max_width,max_height){
	if(!max_width)max_width=100;
	if(!max_height)max_height=max_width;
	size_scale=max_width/max_height;
	if(!ImgObj)return setTimeout(function(){resizeImage(ImgObj)},200);
	var buffer_image=new Image();
	buffer_image.src=ImgObj.src;
	if(buffer_image.width<1||buffer_image.height<1)return;
	if(buffer_image.width/buffer_image.height>size_scale){
		if(buffer_image.width>max_width){
			ImgObj.width=max_width;
			ImgObj.height=(buffer_image.height*max_width)/buffer_image.width;
		}
	}else{
		if(buffer_image.height>max_height){
			ImgObj.height=max_height;
			ImgObj.width=(buffer_image.width*max_height)/buffer_image.height;
		}
	} 
}
//页面模块xml装载刷新
function XmlLoading(xmldoc){
	if(xmldoc==null||xmldoc.readyState!="complete")return;
	if(document.readyState!="complete")return setTimeout(function(){XmlLoading(xmldoc)},200);
	setTimeout(function(){
		try{
			var tmp=null;
			if(xmldoc.parseError.errorCode!=0)return;
			if((tmp=xmldoc.selectSingleNode('/root'))!=null){
				if(tmp.getAttribute('success')=="1"){
					 var divObj=document.all("block_div"+tmp.getAttribute("page_block_id"))
					 if(divObj)divObj.outerHTML=tmp.getAttribute("content");
					 return;
				}
			}
			return alert("服务器忙，获取数据失败，请手动刷新页面。");
		}catch(e){return alert("服务器忙，获取数据失败，请手动刷新页面。")};
	},500);
}
//刷新模块通过html元素对象
function refreshBlockByElement(evtObj){
	while(evtObj&&!evtObj.id&&!evtObj.id.indexOf("block_div")==0&&evtObj.tagName!="BODY"){
		evtObj=evtObj.parentElement;
		if(evtObj.id&&evtObj.id.indexOf("block_div")==0)return refreshBlock(evtObj.id.substring(9));
	}
}
//刷新模块
function refreshBlock(page_block_id11){//xml装载处理函数
   var divObj=document.all("block_div"+page_block_id11)
   if(!divObj)return;
   var newurl=replaceURLParameter(location.href,"load_block_id",page_block_id11);
   divObj.innerHTML="<xml onreadystatechange=\"XmlLoading(this);\" src=\""+newurl+"\" /><div style=\"padding:5 0 0 15;height:26;width:100%;overflow hidden;text-overflow: ellipsis;white-space:normal;font-size:9pt;word-wrap:break-word;border:1 #abc solid;background:#FFFAE6;\"><img src='/images/loading.gif' align=absmiddle>&nbsp;请稍候，正在刷新页面模块数据...</div>";
}
//替换URL参数
function replaceURLParameter(surl,param,value){
	var pos=surl.indexOf("?");
	if(pos==-1||pos==surl.length-1){
		if(param!=null){
			var atag="";
			if(surl.indexOf("#")>0){
				atag=surl.substring(surl.indexOf("#"));
				surl=surl.substring(pos+1,surl.indexOf("#"));
			}
			surl+="?"+param+"="+(value?value:"")+atag;
		}
		return surl;
	}
	var atag="";
	var sNewURL=surl.substring(0,pos)+"?";
	if(surl.indexOf("#")>0){
		atag=surl.substring(surl.indexOf("#"));
		surl=surl.substring(pos+1,surl.indexOf("#"));
	}else surl=surl.substring(pos+1);
	
	var aParam=surl.split("&");
	var bParamFound=false;
	var bFirstParam=true;
	for(var i=0;i<aParam.length;i++){
		if(aParam[i].length==0||aParam[i].indexOf("=")==-1)continue;
		else if(param==null)sNewURL+=(bFirstParam?"":"&")+aParam[i];
		else if(aParam[i].toUpperCase().indexOf(param.toUpperCase()+"=")==0){
			sNewURL+=(bFirstParam?"":"&")+param+"="+(value?value:"");
			bParamFound=true;
		}else sNewURL+=(bFirstParam?"":"&")+aParam[i];
		bFirstParam=false;
	}
	if(!bParamFound&&param)sNewURL+="&"+param+"="+(value?value:"");
	return sNewURL+atag;
}
//超级文本替换
function replaceEx(str1,str2,str3){
	if(str1==null||str1=="")return "";
	var re=new RegExp(str2,"gi");
	return str1.replace(re,str3);
}
/////AjAX通讯API//////////////////////////////////////////////////////////////////////////////////////////////////////
//进度条句柄
var progressDisplay=null;
//连接重试次数
var MAX_RETRY=3;

//得到XMLHTTP对象
function getXMLHttp(){
	var objXMLHttp=null;
	for(var ix=0;ix<MAX_RETRY&&objXMLHttp==null;ix++){
		if(window.XMLHttpRequest){
			objXMLHttp=new XMLHttpRequest();
			if(objXMLHttp.readyState == null){ 
				objXMLHttp.readyState=0;
				objXMLHttp.addEventListener("load", 
					function () { 
						objXMLHttp.readyState=4;
						if (typeof(objXMLHttp.onreadystatechange) == "function") { 
							objXMLHttp.onreadystatechange();
						} 
					}, 
					false
				);
			} 
			return objXMLHttp;
		}else{ 
			var MSXML=['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
			for(var n=0;n < MSXML.length;n ++) { 
				try{
					return objXMLHttp=new ActiveXObject(MSXML[n]);
				}catch(e){} 
			} 
			throw new Error("Your browser does not support xmlhttp objects");
		}
	}
}
//销毁关闭连接对象和进度条显示
function destroyComponent(xmlhttpObj){
	for(var i=0;i<MAX_RETRY&&xmlhttpObj!=null;i++){
		try{
			if(progressDisplay)progressDisplay.hide();
		}catch(exp1){}
		try{
			xmlhttpObj.abort();xmlhttpObj.readyState=0;
		}catch(exp2){}
		try{
			xmlhttpObj=null;
		}catch(exp3){}
		try{
			delete(xmlhttpObj);
		}catch(exp4){}
	}
}
//向服务器端发送Post数据
function postRequest(post_data,callbackfunction){
	if(!post_data)return;
	var postData="",response_js_string="";
	try{if(!progressDisplay)progressDisplay=new Progress("progressDisplay",50);}catch(exp1){};
	for(fldname in post_data){
		if(fldname==null||fldname=="")continue;
		postData+=(postData==""?"":"&")+encodeURIComponent(fldname)+"="+escape(post_data[fldname]?post_data[fldname]:"");
	}
	try{
		var xmlhttp=getXMLHttp();
		if(!xmlhttp)return alert("您的浏览器不支持 xmlhttp objects");
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState!=4)return;
			try{
				if(xmlhttp.status==200){
					response_js_string=unescape(xmlhttp.responseText);
					if(callbackfunction!=null&&callbackfunction!=""&&response_js_string!=null&&response_js_string!=""){//回调参数值不为空并且返回值不为空
						if(typeof callbackfunction == "function"){//回调参数为函数对象，将返回值做为参数传入回调函数
							eval("callbackfunction.call(window,\""+response_js_string+"\");");
						}else{//回调函数为字符串，构造函数对象执行
							eval(callbackfunction+"(\""+response_js_string+"\");");
						}
					}else if(response_js_string!=null&&response_js_string!=""&&response_js_string.indexOf("//Return Script")<=3)eval(response_js_string);//回调函数为脚本函数
					else if(response_js_string!=null&&response_js_string!="")alert("服务器返回：\n"+response_js_string);//返回值为字符串，回调函数为空
				}
			}catch(expt){}
			destroyComponent(xmlhttp);
		};
		xmlhttp.open("POST",post_action_url,true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		try{progressDisplay.show('正在和服务器通讯，请稍候....',true);}catch(exp){}
		xmlhttp.send(postData);
	}catch(getxmlhttperror){}
	return response_js_string;
}
//集思鹏ajax提交回调函数
function JSPSoftCallBack(){
	this.login=false;
	this.callback=function(){};
	this.isLogin=function(){return this.isLogin};
	this.setLogin=function(){this.login=true;}
	this.sendLoginData=function(){__sendLoginData(this)};
	this.sendData=function(){__sendData(this)};
}
//须登录提交
function __sendLoginData(funObj){
	postRequest({action:"islogin"},function(resData){
		if(resData=="true"){//已登录
			funObj.setLogin();
			funObj.callback.call(funObj,'');
		}else{//未登录重新登录
			postRequest(window.showModalDialog("/shop/login_dlg.htm?rnd="+Math.random(),null, "dialogHeight:320px;dialogWidth:408px;center:Yes;help:No;resizable:No;status:No"),function(res11){if(res11!="OK"){alert('抱歉，登录失败！\n可能由于用户名，密码，或验证码错误。');isLogined(funObj);}else{funObj.setLogin();funObj.callback.call(funObj,'')}});
		}
	});
}
//非登录提交
function __sendData(funObj){
	funObj.callback.call(funObj,'');
}
///////////常规验证函数//////////////
//验证字段长度
function isInRange(str,min_len11,max_len11){
	if(str==null||str=="")return min_len11<0;
	return (str.length>=min_len11&&str.length<=max_len11);
}
//判断邮件字符格式
function isEmail(vEMail){
	var regInvalid=/(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
	var regValid=/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
	return (!regInvalid.test(vEMail)&&regValid.test(vEMail));
}