﻿/*******************************************
* setImgBtnMouseEvents(imgSef,textBox,enabled)
* 参数说明：
* imgSef：  提交图片按钮
* textBox： 被验证的字段
* enabled： true=添加鼠标事件, false=删除鼠标事件。
* 如果不存在 btnSubmit_click(imgSef,textBox) 函数，则抛出异常。
*******************************************/
function setImgBtnMouseEvents(imgSef,textBox,enabled){
    if(enabled){
        if(typeof textBox=='string')textBox=document.getElementById(textBox);
        if(typeof imgSef.onmouseout=='function'){return;}
        setImgBtnStatus(imgSef,'Active');
        imgSef.style.cursor='pointer';
        imgSef.alt='提交数据';
        imgSef.onmouseout=function(){setImgBtnStatus(this,'Enabled');}
        imgSef.onmousedown=function(){setImgBtnStatus(this,'Down');}
        imgSef.onmouseup=function(){setImgBtnStatus(this,'Enabled');}
        if(typeof btnSubmit_click!='function'){
            alert('缺少函数：\r\n\r\n function btnSubmit_click(imgSef,textBox){\r\n\t[native code]\r\n}');return;
        }
        imgSef.onclick=function(){btnSubmit_click(this,textBox);}
    }else{
        setImgBtnStatus(imgSef,'Disabled');
        imgSef.style.cursor='auto';
        imgSef.alt='';
        imgSef.onmouseover=null;
        imgSef.onmouseout=null;
        imgSef.onmousedown=null;
        imgSef.onmouseup=null;
        imgSef.onclick=null;
    }
}
function setImgBtnStatus(imgSef,state){
    imgSef.src=imgSef.src.replace(/(\w+)\.gif$/i,('go'+state+'.gif')); 
}
function submitWarning(imgSef,textBox,warnString){
    var warnDiv=document.getElementById('warn');
    if(!warnString)warnString='该项不能为空！';
    setImgBtnMouseEvents(imgSef,textBox,0);
    textBox.focus();
    warnDiv.style.display='block';
    warnDiv.innerHTML=warnString;
    setTimeout(function(){
        warnDiv.style.display='none';
        imgSef.onmouseover=function(){setImgBtnMouseEvents(imgSef,textBox,1);}
        setImgBtnStatus(imgSef,'Enabled');
    },1500);
}
/*******************************************
* ajaxQueueUserWorkItem(postData,postUri,postMethod)
* 参数说明：
* postData：异步请求的数据
* postUri： 异步请求的 URL 地址
* postMethod：请求方法(POST|GET)，必须大写。
* 如果不存在 ajaxWaitCallback(responseText) 函数，则抛出异常。 window.self.location.reload();
*******************************************/
function Ajax(postData,postUri,postMethod,parameters){
    this.ajax=getAjaxObj();
    this.postData=encodeData(postData);
    this.postUri=uriParse(postUri);
    this.postMethod=postMethod;
    this.parameters=parameters;
    this.queueUserWorkItem=queueUserWorkItem;
}

function queueUserWorkItem(ajaxWaitCallback,ajaxExceptionOverride){
    var ajax=this.ajax;
    var postData=this.postData;
    var postUri=this.postUri;
    var postMethod=this.postMethod;
    var parameters=this.parameters;
    ajax.onreadystatechange=function(){
		if (ajax.readyState==4){
			if (ajax.status==200) {
			    if(ajaxWaitCallback){
				    ajaxWaitCallback();
				}else{
				    window.self.location.reload();
				}
            }else{
                ajaxException(ajax,postData,postUri,postMethod,parameters,ajaxExceptionOverride);
                return;
            }
		}
	}
    ajax.open(postMethod,postUri,true);
    if(postMethod=='POST'){ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');}//setRequestHeader('If-Modified-Since', '0');
    ajax.send(postData);
}
function ajaxException(ajax,postData,postUri,postMethod,parameters,ajaxExceptionOverride){
    if(ajaxExceptionOverride){
       ajaxExceptionOverride();
    }
    var status=ajax.status;
    if(!confirm('Request failed，See more information on whether an exception'))return;
    var a=[];
    a.push('There was a problem with the request:\r\n');
    a.push('\tpostData: '+postData+'\r\n');
    a.push('\tpostUri: '+postUri+'\r\n');
    a.push('\tpostMethod: '+postMethod+'\r\n');
    a.push('\tajax.status:'+status+'\r\n');
    a.push('Do you want to searching for ajax.status:'+status+' now?');
    if(window.confirm(a.join(''))){
        window.open('http://www.google.cn/search?hl=zh-CN&q=http+'+status+'&meta=&aq=f&oq=','_blank');
    }
}
function encodeData(s){try{
    if(!s)return s;
    var a=s.split('&'),b=[];
    for(var i=0;i<a.length;i++){
        var x=a[i].split('=');
        b.push(x[0]+'='+escape(decodeURIComponent(x[1])));
        if(i!=a.length-1){
            b.push('&');
        }
    }
    return b.join('');
}catch(e){return s;}}
function uriParse(s){
    s=s.replace(/#/,'');
    var u=(!/\.(aspx|html?|\?(\w+=\w+)?)$/i.test(s))?((s.substring(s.lastIndexOf('/'))=='/'?'':'/')+'engine.aspx'):'';
    return s+u+((s.indexOf('?')>-1)?'&':'?')+'n='+Math.random();
}
function getAjaxObj(){var o=0;if(window.ActiveXObject){try{o=new window.ActiveXObject('Msxml2.XMLHTTP');}catch(e){try{o=new window.ActiveXObject('Microsoft.XMLHTTP');}catch(e){}}}else if(window.XMLHttpRequest){o=new XMLHttpRequest();	if(o.overrideMimeType){o.overrideMimeType('text/xml');}}if(!o){alert('Can\'t create XMLHttpRequest object.');}return o;}

