﻿/*
 * 
 * 作者:Programbin
 * QQ:271658294
 * 
 */
//url     : String   类型,WebService的URL路径  (必须)
//method  : String   类型,WebService的方法名称 (必须)
//params  : Object   类型,WebService方法的参数 (必须)
//asyn    : Bool     类型,是否进行异步操作     (可选,默认为同步)
//asynfun : Function 类型,异步操作的函数入口   (可选)
//time    : Number   类型,设置超时的时间长度.  (可选,单位为秒)
function Webservice(url,method,asyn,asynfun,time)
{
    this.url = url;
    this.method = method;
    this.asyn = asyn;
    this.asynfun = asynfun;
    this.ps = new Object();
    this.time = time;
    this.stop = false;
    if(window.XMLHttpRequest){   
        this.xmlhttp = new XMLHttpRequest(); 
    }
    else{
        try{
	        this.xmlhttp = new ActiveXObject("MSXML2.XMLHTTP");
        }
        catch(e){
	        try
	        {
		        this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	        }
	        catch(e2){}
        }
    }
    this.service = function(){
	    if(this.asyn && typeof this.asynfun != "function") throw "缺少 asynfun 或类型不正确.";
        var soap = "<?xml version =\"1.0\" encoding=\"utf-8\"?>\n"
	    soap += "<soap:Envelope "
	    soap += "  xmlns:xsi=\" http://www.w3.org/2001/XMLSchema-instance\""
	    soap += "  xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
	    soap += "  xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
	    soap += "   <soap:Body>\n";
	    soap += "   <" + this.method + " xmlns=\"http://tempuri.org/\">\n";
        for (var property in this.ps){
		    soap += "    <" + property + "><![CDATA[" + this.ps[property] + "]]></" + property + ">\n";
        }
        soap += "    </" + this.method + ">\n";
        soap += "   </soap:Body>\n";
	    soap += " </soap:Envelope>";
        if(this.time != null){
		    var lResolve = this.time * 1000;
		    var lConnect = this.time * 1000;
		    var lSend    = this.time * 1000;
		    var lReceive = this.time * 1000;
		    this.xmlhttp.setTimeouts(lResolve, lConnect, lSend, lReceive);
        }
        this.xmlhttp.open("POST",this.url, this.asyn == null ? false : this.asyn);
        this.xmlhttp.setRequestHeader("Content-Type","text/xml;Charset=utf-8");
        this.xmlhttp.setRequestHeader("SOAPAction","http://tempuri.org/" + this.method);
        if(this.asyn && typeof this.asynfun == "function"){
            var selfobj = this;
		    this.xmlhttp.onreadystatechange = function()
		    {
			    if(selfobj.xmlhttp.readyState == 4){
				    selfobj.resultRecord(selfobj.method,selfobj.xmlhttp,selfobj.asynfun);
			    }
		    }
	    }
        this.xmlhttp.send(soap);
        if(!this.asyn && !this.stop) return this.resultRecord(method,this.xmlhttp);
    }
    this.resultRecord = function(method,xmlhttp,asynfun){
        var backtrack = xmlhttp.responseText
        var xmldoc;
        var content;
        if( window.ActiveXObject )//ie内核的浏览器
        {
            xmldoc = new ActiveXObject( "Msxml2.DOMDocument" );
            xmldoc.loadXML(backtrack);
            if(xmldoc.getElementsByTagName("faultcode").length > 0){
                var error = xmldoc.getElementsByTagName("faultstring").item(0).text;
                return this.backError(asynfun,error);
            }
            content = xmldoc.getElementsByTagName(method+"Result").item(0);
            if(content == null || content.childNodes.length  == 0)
                content = "<split>";
            else
                content = content.childNodes[0].text;
        }
        else if(window.DOMParser)//firefox内核的浏览器
        {
            var dom = new DOMParser();
            xmldoc = dom.parseFromString(backtrack,"text/xml");
            if(xmldoc.getElementsByTagName("faultcode").length > 0){
                var error = xmldoc.getElementsByTagName("faultstring").item(0).nodeValue;
                return this.backError(asynfun,error);
            }
            content = xmldoc.getElementsByTagName(method+"Result").item(0);
            if(content == null || content.childNodes.length  == 0)
                content = "<split>";
            else
                content = content.childNodes[0].nodeValue;
        }
        return this.backFun(asynfun,content);
    }
    this.backFun = function(asynfun,content){
        var splitStr = content.substring(0,content.indexOf("<split>"));
        var arrayJoin = content.substring(content.indexOf("<split>")+"<split>".length,content.length);
        var result = arrayJoin.split(splitStr);
        result.error = false;
        if(asynfun != null && typeof asynfun == "function" && !this.stop) asynfun(result);
        else return result;
    }
    this.backError = function(asynfun,error){
        var result = new Array();
        result.error = true;
        result.errorString = error;
        if(asynfun != null && typeof asynfun == "function" && !this.stop) asynfun(result);
        else return result;
    }
}
//只能用于异步操作
var Flicker = {
    "object" : null,
    "start" : function(webserivce,objid,text,time,key,count,timeoutmessage){
        var timeout = 26;
        var timeoutmessage = "加载数据超时,请重新加载.";
        var obj = document.getElementById(objid);
        if(obj.flicker == false || obj.flicker == null)
            obj.flicker = true;
        else
            return false;
        if(time == null) time = 0.5;
        if(key == null) key = ".";
        if(count == null) count = 9;
        time = time * 1000;
        var selfobj = new Object();
        selfobj.startFlash = function(){
            if(obj.selfObject != null) window.clearTimeout(obj.selfObject);
            if(obj.flicker)
            {
                if(obj.flickerCount == null) 
                    obj.flickerCount = 0;
                obj.flickerCount ++;
                var inner = text;
                for(var i = 0;i < obj.flickerCount;i++){
                    inner += key;
                }
                obj.innerHTML = inner;
                if(obj.flickerCount > count){
                    obj.innerHTML = text + key;
                    obj.flickerCount = 1;
                }
                obj.selfObject = window.setTimeout(selfobj.startFlash,time);
            }
        }
        selfobj.startFlash();
        var timeobj = new Object();
        timeobj.passtime = 0;
        timeobj.startTime = function(){
            if(obj.timeObject != null) window.clearTimeout(obj.timeObject);
            if(obj.flicker)
            {
                timeobj.passtime += 1;
                if(timeobj.passtime > timeout){
                    webserivce.stop = true;
                    Flicker.stop(objid);
                    Doc.s(objid,timeoutmessage);
                    return;
                }                
                obj.timeObject = window.setTimeout(timeobj.startTime,1000);
            }                
        }
        timeobj.startTime();
        return obj.flicker;
    },
    "stop" : function(objid){
        var obj = Doc.g(objid);
        obj.flicker = false;
        obj.innerHTML = "";
        if(obj.selfObject != null) window.clearTimeout(obj.selfObject);
        obj.flickerCount = 0;
    }
}