// ajaxSubmit.js
// 用來實現(xiàn)通過無刷新技術(shù)提交數(shù)據(jù)并將返回值更新到頁面對象的工作
//-----------------------基礎(chǔ)函數(shù)---------------------------------
/**
函數(shù) ajaxLoadPage
通過Msxml2.XMLHTTP對象向服務(wù)器發(fā)送請求并得到響應(yīng)數(shù)據(jù)
@param url 向服務(wù)器發(fā)送的Url 格式:servletnane?paramname=paramvalue&......<b>
不論發(fā)送GET請求還是POST請求,此參數(shù)都可以被服務(wù)器達到
@param param 此參數(shù)只在POST請求時發(fā)生作用,內(nèi)容是需要提交的的參數(shù)
格式:param1_name=param1_value¶m2_name=param2_value&......
@param method 請求方式 GET/POST 必須大寫
@param container 接受接受數(shù)據(jù)的對象,此對象必須擁有innerHTML屬性,此時用異步方式請求數(shù)據(jù)
如果傳入""就用同步方式請求數(shù)據(jù),并將響應(yīng)數(shù)據(jù)返回
**/
var currobj = null;
var imageSrc="<img src='images/loading.gif'>";
function doGet(url,obj,flag){
if(!flag){
flag=false;
}
else{
flag = true;
}
var oBao=new ActiveXObject("Msxml2.XMLHTTP");//require Cross-Browser XMLHttpRequest
oBao.open("GET",url+"&ajaxdate="+new Date(),flag);
if(flag){
oBao.onreadystatechange=function(){
if(oBao.readyState==1){
obj.innerHTML=imageSrc+"loading...";
}
if (oBao.readyState==4){
obj.innerHTML="";
obj.innerHTML=oBao.responseText;
}
}
oBao.send();
}
else{
if(obj){
oBao.send();
obj.innerHTML="";
obj.innerHTML=oBao.responseText;
oBao = null;
}
else{
oBao.send();
return oBao.responseText;
}
}
}
function doPost(url,formobj,obj,flag){
try{
if(!flag){
flag=false;
}
else{
flag = true;
}
var oBao=new ActiveXObject("Msxml2.XMLHTTP");//require Cross-Browser XMLHttpRequest
var formsvalue=formToRequestString(formobj);
formsvalue=encodeURI(formsvalue);
//alert(formsvalue);
//return;
oBao.open("POST",url+"&isAjax=1&ajaxdate="+new Date(),flag);
oBao.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
if(flag){
oBao.onreadystatechange=oBao.onreadystatechange=function(){
if(oBao.readyState==1){
obj.innerHTML=imageSrc+"loading...";
}
if (oBao.readyState==4){
obj.innerHTML="";
obj.innerHTML=oBao.responseText;
}
}
oBao.send(formsvalue);
}
else{
if(obj){
oBao.send(formsvalue);
obj.innerHTML="";
obj.innerHTML=oBao.responseText;
oBao = null;
}
else{
oBao.send(formsvalue);
return oBao.responseText;
}
}
}
catch(e){
self.status = e.message;
return "error";
}
}
function doPostForParam(url,params,obj,flag){
try{
if(!flag){
flag=false;
}
else{
flag = true;
}
var oBao=new ActiveXObject("Msxml2.XMLHTTP");//require Cross-Browser XMLHttpRequest
var formsvalue=params;
oBao.open("POST",url+"&isAjax=1&ajaxdate="+new Date(),flag);
oBao.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
if(flag){
oBao.onreadystatechange=oBao.onreadystatechange=function(){
if(oBao.readyState==1){
obj.innerHTML=imageSrc+"loading...";
}
if (oBao.readyState==4){
obj.innerHTML="";
obj.innerHTML=oBao.responseText;
}
}
oBao.send(formsvalue);
}
else{
if(obj){
oBao.send(formsvalue);
obj.innerHTML="";
obj.innerHTML=oBao.responseText;
oBao = null;
}
else{
oBao.send(formsvalue);
var str = oBao.responseText;
return str;
}
}
}
catch(e){
self.status = e.message;
return "error";
}
}
/**
函數(shù) formToRequestString(form_obj)
將form對象中的數(shù)據(jù)組織成鍵值對,便于通過Msxml2.XMLHTTP對象發(fā)送
@param form_obj 需要提交的form對象
@return 組織好的鍵值對字符串,在組織數(shù)據(jù)時對值進行了escape編碼,在末尾會加上ajax=1的參數(shù),幫助服務(wù)識別是否通過ajax方式
涮新
*/
function formToRequestString(form_obj)
{
var query_string='';
var and='';
for (i=0;form_obj&&i<form_obj.length ;i++ )
{
e=form_obj[i];
if(e.type=="button"){
continue;
}
element_value="";
if (e.name!=''&&!e.disabled)
{
if (e.type=='select-one')
{
if(e.options.length>0){
if(e.selectedIndex>-1){
element_value=e.options[e.selectedIndex].value;
}
}
}
else if (e.type=='checkbox' || e.type=='radio')
{
if (e.checked==true)
{
element_value=e.value;
}
}
else
{
element_value=e.value;
}
if(element_value!=""){
query_string+=and+e.name+'='+escape(element_value);
query_string = query_string.replace(/\+/g,"%2b");
//alert(element_value);
and="&";
}
}
}
//alert(query_string);
return query_string;
}
function writeFile(filestr,filename){
var fso = new ActiveXObject("Scripting.FileSystemObject");
var file=fso.CreateTextFile(filename, true);
file.WriteLine(filestr);
file.Close();
}
function doExtGet(urlstr,obj,funcobj){
var loadid=Ext.Ajax.request({
url:urlstr,
method: 'GET',
success:function(result, request){
obj.innerHTML=result.responseText;
if(funcobj!=null){
funcobj();
}
},
failure: function(result, request){
obj.innerHTML="加載失敗!";
}
});
if(Ext.Ajax.isLoading(loadid)){
obj.innerHTML=imageSrc;
}
}
function doExtPost(urlstr,formobj,obj,funcobj){
var loadid=Ext.Ajax.request({
url:urlstr,
method: 'POST',
form:formobj,
success:function(result, request){
obj.innerHTML=result.responseText;
if(funcobj!=null){
funcobj();
}
},
failure: function(result, request){
obj.innerHTML="加載失??!";
}
});
if(Ext.Ajax.isLoading(loadid)){
obj.innerHTML=imageSrc;
}
}
/**
* 使用Ajax技術(shù)保存數(shù)據(jù),顯示數(shù)據(jù)處理過程進度框,調(diào)用保存方法
* @param loadDo 保存數(shù)據(jù)的方法 此參數(shù)必有
*/
function showLoad(loadDo){
if(loadDo==null||loadDo==undefined||loadDo==""){
return;
}/*
var divobj= null;
if(!infoDiv){
infoDiv = document.createElement("<div id='infoDiv'></div>");
document.body.insertBefore(infoDiv);
divobj=document.createElement("<div id='dealWithdivobj'></div>");
infoDiv.insertBefore(divobj);
}
else{
infoDiv = document.getElementById("infoDiv");
divobj = document.getElementById("dealWithdivobj");
}
if(!infoDiagnose){
infoDiagnose = new Ext.Window({
contentEl: infoDiv,
height: 130,
width: 300,
title:"信息框",
minHeight: 100,
minWidth: 150,
autoCreate:true,
closable:false,
modal: true,
proxyDrag: true,
shadow: true,
layout:"fit",
items: [{
contentEl: "dealWithdivobj"
}]
});
}
divobj.innerHTML="<table height='100%' width='98%'><tr><td align='center' height='100%'>"
+imageSrc+"數(shù)據(jù)處理中,請稍候!</td></tr></table>";
hiddenObj(document,'hidden');
infoDiagnose.show();*/
var htmlstr="<table height='100%' width='98%'><tr><td align='center' height='100%'>"
+imageSrc+"數(shù)據(jù)處理中,請稍候!</td></tr></table>";
showLayerForHtml("showLoadLayer",htmlstr,"信息框",300,130,true);
window.setTimeout(loadDo,1);
}
/**
* 使用Ajax技術(shù)保存數(shù)據(jù)失敗后調(diào)用的函數(shù)
* @param error 出錯信息,可選參數(shù)
*/
function errorInfo(error){
//var divobj = document.getElementById("dealWithdivobj");
if(error==null){
error="數(shù)據(jù)處理出錯,請將輸入信息另存后,重新執(zhí)行操作,如果操作不能完成,請聯(lián)系系統(tǒng)管理員!";
}
var str ="<table height='100%' width='98%'><tr><td align='left' height='100%'>"
+error+"</td></tr><tr><td align='center' height='30'>"
+"<input type='button' name='btnErrorClose' value='關(guān) 閉' class='f' onclick='closeLayer(\"showLoadLayer\");'></td></tr></table>";
showLayerForHtml("showLoadLayer",str,"信息框",300,130,true);
}
/**
* 使用Ajax技術(shù)保存數(shù)據(jù)成功后調(diào)用的函數(shù)
* @param str 成功信息
* @param fndo 保存成功后應(yīng)該調(diào)用的功能
*/
function doSuccess(str,fndo){
//var divobj = document.getElementById("dealWithdivobj");
if(!str){
str="操作成功!";
}
var htmlstr="<table height='100%' width='98%'><tr><td align='center' height='100%'><font size='4'>"
+ str +"</font></td></tr>"
+"</table>";
showLayerForHtml("showLoadLayer",htmlstr,"信息框",300,130,true);
window.setTimeout("closeLayer('showLoadLayer');"+(fndo==null?"":";"+fndo+";"),50);
}
/**
* 隱藏保存過程顯示框
*/
function hideLoad(){
//hiddenObj(document,'visible');
//infoDiagnose.hide();
}
function saveDatainfo(){
var str = "";
try{
str = document.all.editdiv.outerHTML;
}
catch(e){
try{
str = document.all.mycontrol.GetDocContent("getalldoctext");
str=str.replace(/\r/g,"<br>");
}
catch(e1){}
}
writeFile(str,"c:\\htcom\\errorInfo.html");
}
function loadDateinfo(){
var fso = new ActiveXObject("Scripting.FileSystemObject");
var file=fso.OpenTextFile("c:\\htcom\\errorInfo.html",1,true);
var filestr="";
var temp=null;
try{
while((temp=file.ReadLine())!=null){
filestr+=temp+"\n";
}
file.Close();
}
catch(e){
file.Close();
}
return filestr;
}
/**
* 校驗日期字符串格式是否為yyyy-MM-dd
* @param dateStr 校驗的日期字符串
*/
function isAValidDate(dateStr) {
var datePat = /^(\d{4})-(\d{2})-(\d{2})$/;
var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
return false;
}
month = matchArray[2]; // parse date into variables
day = matchArray[3];
year = matchArray[1];
if (month < 1 || month > 12) { // check month range
return false;
}
if (day < 1 || day > 31) {
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
return false;
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
return false;
}
}
return true;
}
function fastSearchShow(url,obj){
if(!obj){
obj = event.srcElement;
}
if(obj){
var divObj = document.getElementById("Fast_Search_Div");
if(!divObj){
divObj = document.createElement("div");
divObj.id = "Fast_Search_Div";
divObj.className="area_div_border";
divObj.style.position="absolute";
divObj.style.width="500px";
divObj.style.height="200px";
divObj.style.zIndex=100000000;
document.body.insertBefore(divObj);
divObj.innerHTML="";
if(url){
doGet(url,divObj,false);
}
}
divObj.relationId=obj.id;
var left = event.srcElement.offsetLeft;//(document.body.offsetWidth-500)/2;
var top = event.srcElement.offsetTop;//(document.body.offsetHeight-400)/2;
divObj.style.posLeft=document.body.offsetWidth-510;
divObj.style.posTop =document.body.offsetHeight-240;
focusObj(document.all.searchId);
}
}
function fastSearchHide(){
var divObj = document.getElementById("Fast_Search_Div");
if(divObj){
divObj.style.posLeft=-1000;
divObj.style.posTop=-1000;
var relobj = document.getElementById(divObj.relationId);
if(relobj){
relobj.focus();
}
}
}
function fastSearchWrite(str){
var divObj = document.getElementById("Fast_Search_Div");
if(divObj){
var relobj = document.getElementById(divObj.relationId);
if(relobj&&relobj.value){
relobj.value+=str;
}
}
}
function doInputFloat(obj){
if(obj){
obj.onkeydown=function(){
var keycode = window.event.keyCode;
if(!window.event.shiftKey&&((keycode>=48&&keycode<=57)||(keycode>=96&&keycode<=105)||keycode==190)){
if(keycode==190){
if(this.value.indexOf(".")>0){
window.event.keyCode=0;
return false;
}
else if(this.value==""){
this.value="0";
}
}
}
else{
if(keycode<8||keycode>=46){
window.event.keyCode=0;
return false;
}
}
}
}
}
function doInputInt(obj){
if(obj){
obj.onkeydown=function(){
var keycode = window.event.keyCode;
if(!window.event.shiftKey&&((keycode>=48&&keycode<=57)||(keycode>=96&&keycode<=105))){
}
else{
if(keycode<8||keycode>46){
window.event.keyCode=0;
return false;
}
}
}
}
}