﻿// JScript File
var DTAjax=function()
{
	this.xmlHttpObj=null;
	this.postStr="";
	this.getXmlHttpObj=function()
	{
		var obj;
		if(window.XMLHttpRequest){    
		   obj = new XMLHttpRequest();         
		   if(obj.overrideMimeType){    
			   obj.overrideMimeType("text/xml");    
		   }    
		}else if(window.ActiveXObject){    
			try{    
				 obj = new ActiveXObject("Msxml2.XMLHTTP");    
			}catch(e){    
				  try{    
					   obj = new ActiveXObject("Microsoft.XMLHTTP");    
				  }catch(e){}    
			}     
		} 
		return obj; 
	}
	

	this.getStringFromUrl=function(method,url,syn)
	{
		if(url.indexOf("?")>=0)
		{
			url=url+"&"+Math.random(1)
		}
		else
		{
			url=url+"?a="+Math.random(1)
		}
		if(/get/i.test(method))
		{
			this.xmlHttpObj.open("GET", url, syn);
			this.xmlHttpObj.send(null);
		}
		else
		{
			this.xmlHttpObj.open("POST", url, syn);
			this.xmlHttpObj.setRequestHeader("Content-Length",this.postStr.length);  
			this.xmlHttpObj.setRequestHeader("Content-type","application/x-www-form-urlencoded;CharSet=GB2312");   
			this.xmlHttpObj.send(this.postStr);
			
		}
		
		//alert("eee");
		//document.write(this.xmlHttpObj.responseText);
	}
	
	
	this.getReadyStateChangeHandle=function(obj)
	{
		return function()
		{
			//document.write(obj.xmlHttpObj.readyState);
			obj.onReadyStateChange(obj.xmlHttpObj.readyState,obj.xmlHttpObj);
			if(obj.xmlHttpObj.readyState==4)
			{
				//alert(obj.xmlHttpObj.responseText);
				obj.onGet(unescape(obj.xmlHttpObj.responseText));
			}
		}
	}
	
	this.addPostData=function(name,value)
	{
		if(!this.postStr)
		{
			this.postStr=name+"="+escape(value);
		}
		else
		{
			this.postStr+="&"+name+"="+escape(value);
		}
	}
	this.onGet=function(rc){}
	this.onReadyStateChange=function(id,xmlHttpObj){};
	this.xmlHttpObj=this.getXmlHttpObj();
	this.xmlHttpObj.onreadystatechange = this.getReadyStateChangeHandle(this);

}
/////////////////////////////////////
//
//////////////////////////////////
/////golbal var
var currentRowObj = null;
var rowindex = 0;
var _head = "";
var _foot = "";
///////////////////////
function retriveData(cachId,page)
{
    //alert(cachId);
    var ajaxRequest = new DTAjax();
    ajaxRequest.onGet = requestOnGetHandler(cachId, page);
    ajaxRequest.addPostData("key", cachId);
    ajaxRequest.getStringFromUrl("post", page, true);
}

function requestOnGetHandler(cachId, page)
{
    return function(res)
    {
            try
            {
                document.getElementById("template_cont").value = res;
                //alert(res);
            }catch(e){}
            if(/DATA_NOT_READY_PLEASE_TRY_AGAIN/.test(res))
            {
                setTimeout(function()
                {
                    return function()
                    {
                        var ajaxRequest = new DTAjax();
                        ajaxRequest.onGet = requestOnGetHandler(cachId, page);
                        ajaxRequest.addPostData("key", cachId);
                        ajaxRequest.getStringFromUrl("post", page, true);
                    }
                }(cachId, page),3000);
            }
            else
            {
                //alert(res);
                rendBlock(res);
            }
    }
}


function rendBlock(htmlStr)
{
    var jsonStr1 = htmlStr.match(/\<span id\=\"retrive_data_start\"\>\{json\-start\}([\d\D]*?)\{json\-end\}\<\/span\>/);
    try
    {
        var jsonStr = (jsonStr1[1]);
    }
    catch(e){return; }
    
    
    var dataObject  = JSON.parse(jsonStr);
    
    if(!dataObject){alert("failed to rend contant.");return;}
    
    var displayStr = "";
    
    for(var i = 0,n=dataObject.length;i < n; i++)
    {
        var rowStrTmp = dataObject[i].template.string.replace(/\{\$head\$\}([\d\D]+?)\{\$\/head\$\}/gi,rendReplaceHeadFunc);
        rowStrTmp = rowStrTmp.replace(/\{\$foot\$\}([\d\D]+?)\{\$\/foot\$\}/gi,rendReplaceFootFunc);
        
        for(var j=0,m=dataObject[i].data.length;j < m;j++)
        {
            currentRowObj = dataObject[i].data[j];
            rowindex = j+1;
            
            var rowStr = rowStrTmp.replace(/\{\$if\(([\d\D]+?)\)\$\}([\d\D]+?)\{\$else\$\}([\d\D]+?)\{\$endif\$\}/gi,rendReplaceIfElseFunc);
            rowStr = rowStr.replace(/\{\$if\(([\d\D]+?)\)\$\}([\d\D]+?)\{\$endif\$\}/gi,rendReplaceIfFunc);
            rowStr = rowStr.replace(/\{\$=([\d\D]+?)\$\}/gi,rendReplaceFunc);
            displayStr+=rowStr;
        }
        
        displayStr = _head +displayStr +_foot;
        //alert(displayStr);
        
        setTimeout(timeoutDisplayHtml("asyncblock_"+dataObject[i].head.name,displayStr),100);
        /*
        if(document.getElementById("asyncblock_"+dataObject[i].head.name))
        {
            document.getElementById("asyncblock_"+dataObject[i].head.name).innerHTML = displayStr;
            document.getElementById("asyncblock_"+dataObject[i].head.name).style.visibility = "";
        }
        else
        {
            
            setTimeout(timeoutDisplayHtml("asyncblock_"+dataObject[i].head.name,displayStr),100);
        }*/
        displayStr = "";
    }
}

function timeoutDisplayHtml(id, html)
{
    return function()
    {
        
        if(document.getElementById(id))
        {
            document.getElementById(id).innerHTML = html;
            document.getElementById(id).style.visibility = "";
        }
        else
        {
            //dataWaitToRend[id] = displayStr;
            setTimeout(timeoutDisplayHtml(id,displayStr),2000);
        }
    }
}

function rendReplaceFunc(a,b)
{
    if(b.toLowerCase()=='pic_path'){
        return (currentRowObj[b]!=""?currentRowObj[b]:"/img/tcico.png");    
    }
    else{
        return (currentRowObj[b]?currentRowObj[b]:"");
    }
}

function rendReplaceIfFunc(a,b,c,d)
{
    return (eval(b)?c:"");
}

function rendReplaceIfElseFunc(a,b,c,d)
{
    return (eval(b)?c:d);
}

function rendReplaceHeadFunc(a,b)
{
    _head = b;
    return '';
}

function rendReplaceFootFunc(a,b)
{
    _foot = b;
    return '';
}
