// JavaScript Document

function doAjaxCall(funname,param1,param2)
{
    var params = "";
    var i=0;
    for(i=1;i<arguments.length;i++)
	{
	    params += "&param=" + arguments[i];
	}
   return XmlHttpHelper.synCall("CallHandler.ashx?funname="+funname+params);
}
var ClipboardHelper ={
    IsAutoCopy:true,
    Copy:function(text)
    {
	    window.clipboardData.setData("Text",text);
	},
	AutoCopy:function(text)
	{
	    if(!this.IsAutoCopy) return ;
	    this.Copy(text);
	}
}

function GridViewHelper()
{
	
}

//tableObj - 表对象
//isChecked - null 获取所有的 true 获取选择的  false 获取没选择的
//ctlCheckBoxObj - 触发获取请求的对象,此对象将不包含在列表中
//valueIsArr - 值（以,分割）自身是否要分解为数组
//返回value数组
function GridViewHelper.getCheckedValues(name,isChecked,valueIsArr)
{
    var values = [];
	var checkBoxs = GridViewHelper.getCheckedObjs(name,isChecked);
	for(i=0;i<checkBoxs.length;i++)
	{
	    if(valueIsArr)
	    {
	       values[values.length] = checkBoxs[i].value.split(",");
	    }else
	    {
	        values[values.length] = checkBoxs[i].value;
	    }
	}
    return values;
}

//tableObj - 表对象
//isChecked - null 获取所有的 true 获取选择的  false 获取没选择的
//ctlCheckBoxObj - 触发获取请求的对象,此对象将不包含在列表中
//
function GridViewHelper.getCheckedObjs(name,isChecked)
{
	var objs = [];
	
    var checkBoxs = document.all(name);
    if(typeof(checkBoxs) == 'undefined' || checkBoxs == null) return objs;
    if(typeof(checkBoxs.length) == 'undefined')
    {
        checkBoxs[0] = checkBoxs;
        checkBoxs.length = 1;
    }
	for(var i=0;i<checkBoxs.length;i++)
	{
	    var currCheckBox = checkBoxs[i];
		if(typeof(isChecked) == "undefined" || isChecked == null)
		{
			objs[objs.length] = currCheckBox;
		}
		else if(isChecked && currCheckBox.checked)
		{
			objs[objs.length] = currCheckBox;
		}
		else if(!isChecked && !currCheckBox.checked)
		{
			objs[objs.length] = currCheckBox;
		}
	}
	return objs;
}

function GridViewHelper.getParentElement(currObj,parentTagName)
{
	var currParentObj = currObj;
	while(currParentObj.tagName != parentTagName)
	{
		currParentObj = currParentObj.parentElement;
		if(currParentObj == null ) break; 
	}
	return currParentObj;
}

function GridViewHelper.doSelectAllCheckBox()
{
	var currCheckBox = event.srcElement;
	var currTable = currCheckBox.parentElement;
	var currChecked = currCheckBox.checked;
    var checkHelperObj = GridViewHelper.getCheckBoxHelperObj(currCheckBox);
    if(checkHelperObj == null) return null;
	var checkBoxs = checkHelperObj.checkBoxs;
	for(var i=0;i<checkBoxs.length;i++)
	{
		checkBoxs[i].checked =  currChecked;
		var currTrObj = checkHelperObj.trObjs[i];
		currTrObj.className = (checkBoxs[i].checked?"selected":"");
	}
	if(checkBoxs[0].checked) return checkBoxs[0];
	return null;
}


function GridViewHelper.getCheckBoxHelperObj(currCheckObj)
{
    var currCheckName = currCheckObj.name;
    if(currCheckName.substring(0,3) == 'all') currCheckName = currCheckName.substring(3);
	var checkHelperObjName = currCheckName+"helper";
	var checkHelperObj = window[checkHelperObjName];
	if(typeof(checkHelperObj) == 'undefined' || checkHelperObj == null)
	{
	    var allcheckbox = null;
	    
	    if(currCheckName.substring(0,3) == 'all')
	        allcheckbox = currCheckObj;
	    else
	        allcheckbox = document.getElementById("all"+currCheckName);
	    if(allcheckbox == null) return null;

	    checkHelperObj = window[checkHelperObjName] = {};
        var checkBoxs = document.all(currCheckName);
        if(typeof(checkBoxs.length) == 'undefined')
        {
            checkBoxs[0] = checkBoxs;
            checkBoxs.length = 1;
        }
	    checkHelperObj.checkBoxs = checkBoxs;
	    checkHelperObj.allcheckbox = allcheckbox;
	    checkHelperObj.trObjs = [];
	    for(var i=0;i<checkBoxs.length;i++)
	    {
		    var currTrObj = GridViewHelper.getParentElement(checkBoxs[i],"TR");
	        checkHelperObj.trObjs[i] = currTrObj;
	    }
	}
	return checkHelperObj;
}

function GridViewHelper.doSelectCheckBox(currCheckObj)
{
	if(typeof(currCheckObj) == "undefined" || currCheckObj == null) currCheckObj = event.srcElement;
	
	ClipboardHelper.AutoCopy(currCheckObj.value);
	
    var checkHelperObj = GridViewHelper.getCheckBoxHelperObj(currCheckObj);
    if(checkHelperObj == null) return null;
	var currTrObj = GridViewHelper.getParentElement(currCheckObj,"TR");
	if(currTrObj == null) return null;
	currTrObj.className = (currCheckObj.checked?"selected":"");
	
	var allcheckbox = checkHelperObj.allcheckbox;
	var checkBoxs = checkHelperObj.checkBoxs;

    var firstCheckedObj = null;
	allcheckbox.checked = true;
	for(var i=0;i<checkBoxs.length;i++)
	{
		if(!checkBoxs[i].checked)
		{
			allcheckbox.checked = false;
		}else if(firstCheckedObj == null)
		{
		    firstCheckedObj = checkBoxs[i];
		}
		if(!allcheckbox.checked && firstCheckedObj != null) break;
	}
	return firstCheckedObj;
}

function GridViewHelper.setSelectCheckBoxs(name,value)
{
	var allRadio = document.all(name);
	if(allRadio == null || typeof(allRadio) == 'undefined' ) return ;
    if(typeof(allRadio.length) == 'undefined')
    {
        allRadio[0] = allRadio;
        allRadio.length = 1;
    }
    value = ","+value+",";
	for(var i=0;i<allRadio.length;i++)
	{
		if(value.indexOf(","+allRadio[i].value+",")>=0) allRadio[i].click();
	}
}

function GridViewHelper.getRadioHelperObj(currRadioObj)
{
	var checkHelperObjName = currRadioObj.name+"helper";
	var checkHelperObj = window[checkHelperObjName];
	if(typeof(checkHelperObj) == 'undefined' || checkHelperObj == null)
	{
        checkHelperObj = window[checkHelperObjName] = {};

	    var allRadio = document.all(currRadioObj.name);
	    if(allRadio == null || typeof(allRadio) == 'undefined' ) return ;
        if(typeof(allRadio.length) == 'undefined')
        {
            allRadio[0] = allRadio;
            allRadio.length = 1;
        }
        checkHelperObj.allRadio = allRadio;
        checkHelperObj.trObjs = [];
	    for(var i=0;i<allRadio.length;i++)
	    {
	        allRadio[i].index = i;
		    var currTrObj = GridViewHelper.getParentElement(allRadio[i],"TR");
		    checkHelperObj.trObjs[i] = currTrObj;
	    }
	    checkHelperObj.checkedRadio = currRadioObj.checked?currRadioObj:null;
    }
    
    return checkHelperObj;
}
function GridViewHelper.doSelectRadio(currRadioObj)
{
	if(typeof(currRadioObj) == "undefined" || currRadioObj == null) currRadioObj = event.srcElement;

	ClipboardHelper.AutoCopy(currRadioObj.value);
	
    var checkHelperObj = GridViewHelper.getRadioHelperObj(currRadioObj);
    if(checkHelperObj == null) return null;
    var beforeSelectRadio = checkHelperObj.checkedRadio;
    if(beforeSelectRadio != null && beforeSelectRadio != currRadioObj)
    {
		var currTrObj = checkHelperObj.trObjs[beforeSelectRadio.index];
		currTrObj.className = "";
    }
	var currTrObj = checkHelperObj.trObjs[currRadioObj.index];
	currTrObj.className = "selected";
    checkHelperObj.checkedRadio = currRadioObj;
	return currRadioObj.value;
}

function GridViewHelper.setSelectRadio(name,value)
{
	var allRadio = document.all(name);
	if(allRadio == null || typeof(allRadio) == 'undefined' ) return ;
    if(typeof(allRadio.length) == 'undefined')
    {
        allRadio[0] = allRadio;
        allRadio.length = 1;
    }
	for(var i=0;i<allRadio.length;i++)
	{
		if(allRadio[i].value == value) allRadio[i].click();
			
	}
	
}

function GetParentWindow(currWin)
{
    if(typeof(currWin) == "undefined" || currWin == null) currWin = window;
	var parentWin =	 (((currWin.parent != currWin) ? currWin.parent : null)
			|| currWin.opener || (currWin.dialogArguments
				&& currWin.dialogArguments.window));
	return parentWin;
}

function GetParentWindowForObject(objId)
{
	var parentWin = null;
	var currWin = window;
	while(true)
	{   
		parentWin = GetParentWindow(currWin);
		if(parentWin == currWin) return null;
		currWin = parentWin;
		if(parentWin == null) break;
		if(typeof(parentWin[objId]) != "undefined") break; 
	}
	return parentWin;
	
}

function DoResultClientProcess(isOk,msg,isCloseSelfWindow,isRefreshParentWindow)
{
	if(msg != "")
	{
		alert(msg);
	}
	if(isRefreshParentWindow)
	{
		var parentWin =	 GetParentWindow();
		if(parentWin != null)
		{
		    // parentWin.document.execCommand("Refresh");
		    parentWin.location.href = parentWin.location.href;
		}
	}
	if(isCloseSelfWindow) window.close();

}

function ShowDialog(url, width, height,modal,args)
{
	if(typeof(modal)=='undefined') modal = true;
	if(typeof(args)=='undefined') args = window;
		
	if(modal)
	{
		return showModalDialog(url, args, "dialogWidth:" + width + "px;dialogHeight:" + height + "px;help:no;scroll:no;status:no");
	}
	else 
	{
		return showModelessDialog(url, args, "dialogWidth:" + width + "px;dialogHeight:" + height + "px;help:no;scroll:no;status:no");
	}

}



var CheckBoxListHelper = {
    doSelectAll:function (checkBoxListId,ctlCheckBoxId)
    {
        var ctlCheckBox = document.getElementById(ctlCheckBoxId);
        var i=0;
        while(true)
        {
            var currCheckBox = document.getElementById(checkBoxListId+"_"+i);
            if(typeof(currCheckBox) == "undefined" || currCheckBox == null) break;
            currCheckBox.checked = ctlCheckBox.checked;
            i++;
        }
    }
}

var FormViewHelper = {
    SetFocus : function(firstFocusInputId)
    {
		if(typeof(firstFocusInputId) != "undefined" && firstFocusInputId != null)
		{
		    var firstFocusInputObj = document.getElementById(firstFocusInputId); 
		    if(firstFocusInputObj != null)
		    {
		        try
		        {
                    firstFocusInputObj.focus();
                    var   rng   =   firstFocusInputObj.createTextRange();     
                    rng.moveStart("character",firstFocusInputObj.value.length);  
                    rng.collapse(false);       
                    rng.select();     
		        }catch(e){}
		    }
		}
 
    },
    EnterToTab : function(formId,firstFocusInputId)
    {
        FormViewHelper.SetFocus(firstFocusInputId);
        
        var frmobj = document.getElementById(formId); 
	    if(typeof(frmobj) == 'undefined' || frmobj == null)
		    return;
        
        frmobj.attachEvent("onkeydown", frmobj_onkeydown);    
	    function frmobj_onkeydown()
	    {
		    if(event.srcElement.type =='textarea' || event.srcElement.type =='button') 
		    { // button 和 textarea不需要将回车换为tab
			    return ;
		    }
		    if(event.keyCode == 13)
		    {
			    event.keyCode = 9;
		    }
	    }
	    return ;
    },
    EnterToSumbit : function(formId,firstFocusInputId,submitBtnId)
    {
        FormViewHelper.SetFocus(firstFocusInputId);
        
        var frmobj = document.getElementById(formId); 
	    if(typeof(frmobj) == 'undefined' || frmobj == null)
		    return;
        
        var submitBtnObj = null;
 		if(typeof(submitBtnId) != "undefined" && submitBtnId != null)
		    submitBtnObj = document.getElementById(submitBtnId); 
        if(submitBtnObj == null)
        {
	        // 将焦点定位到form的第一个显示的元素上
	        for(i=0;i<frmobj.length;i++)
	        {
		        if(frmobj[i].type == 'button')
		        {
		            submitBtnObj = frmobj[i];
    		        break;
    		    }
	        }	
        }
		if(submitBtnObj == null) return ;
		   
        frmobj.attachEvent("onkeydown", frmobj_onkeydown);    
	    function frmobj_onkeydown()
	    {
		    if(event.srcElement.type =='textarea' || event.srcElement.type =='button') 
		    { // button 和 textarea不需要将回车换为tab
			    return ;
		    }
		    if(event.keyCode == 13)
		    {
			    event.keyCode = 0;
			    submitBtnObj.click();
		    }
	    }
	    return ;
    }   
    
    
}

var WaitHelper = 
{
    showWaitMessage:function(msg) {
        WaitHelper.closeWaitMessage();
        if(typeof(msg) != 'string' || msg == null || msg == "")
        msg = "Please waiting ...";
	    var waitwin = document.createElement("div");
	    document.body.appendChild(waitwin);
 	    var top = 3;
	    var left = 3;
    /*	
	    var html = '<div id="waitwin" style="left:'+left+'px;top:'+top+'px;'
	    +'position:absolute;z-index:79000;background-color:#FFF0C8;'
	    +'white-space:nowrap;border:1px solid #77a;padding:6px;">'
	    +'<img alt="." src="../images/progress.gif"/> '
	    +msg+'</div>';*/
        var style = "left:50%;margin-left:-110px;";
	    var html = '<div id="waitwin" style="left:'+left+'px;top:'+top+'px;'
	    +'position:absolute;z-index:79000;'
	    +'width:300px;height:60px;top:50%;margin-top:-62px;'+style+'">'
	    +'<iframe style="width:180px;margin:0;height:40px;" id="waitwiniframe"'
	    +' name="waitwiniframe" frameborder="0" scrolling="no" src="../js/waiter/WaitWin.html"></iframe></div>';

        waitwin.outerHTML = html;
        if(event == null)
        {
            window.attachEvent( "onload", WaitHelper.closeWaitMessage );
        }
        else
        {
           var btn = event.srcElement ;
           btn.disabled = true;
           //window.setTimeout("try{document.getElementById('"+btn.id+"').disabled=false;}catch(e){}",8000);
        }
    },

    initWaitMessageButtons:function(btnIds)
    {
        if(typeof(btnIds) == "string")
        {
            var btnIds = btnIds.split(",");
            for(var i=0;i<btnIds.length;i++)
            {
                var currBtn = document.getElementById(btnIds[i]);
                if(currBtn == null) continue;
                //objs[objs.length] = currBtn;
                WaitHelper.initWaitMessageButtons(currBtn);
            }
            return ;
         }
        var currBtn = btnIds;
        if ( currBtn.onclick != null )
        {
            var oldOnclick = currBtn.onclick;
            if(oldOnclick.toString().indexOf("__doPostBack")<0) return;
            currBtn.onclick = function ()
            {
	            var result = oldOnclick();
	            if(typeof(result) == "undefined" || result)
	                WaitHelper.showWaitMessage();
            };
        }
        else 
        {
            currBtn.onclick = WaitHelper.showWaitMessage();
        }        
    },

    initWaitMessageForms:function(formId)
    {
        var frmobj = document.getElementById(formId); 
        if(typeof(frmobj) == 'undefined' || frmobj == null)
	        return;
        // 将焦点定位到form的第一个显示的元素上
        for(i=0;i<frmobj.length;i++)
        {
            if(frmobj[i].type == 'button') WaitHelper.initWaitMessageButtons(frmobj[i]);
        }
    },

    closeWaitMessage:function()
    {
        try
        {
            var waitwin = document.getElementById("waitwin");
            if(waitwin != null)
            {
              waitwin.parentNode.removeChild(waitwin);
            }
        }catch(ex){alert(ex);};
    },
    initWaitWin:function(url,msg,param)
    {
        if(typeof(url) == 'undefined' || url == null) url = "";
        if(typeof(msg) == 'undefined' || msg == null || msg == "" ) msg = "正在处理中，请稍候...";
        if(typeof(param) == 'undefined' || param == null) param = "";
        
	    var html = '<form id="waitfrm" action="../Utility/Wait.aspx" target="_blank" method="post">'
                +'<input type="hidden" id="url" name="url" value="'+url+'" />'
                +'<input type="hidden" id="param" name="param" value="'+param+'" />'
                +'<input type="hidden" id="msg" name="msg" value="'+msg+'" />'
                +'</form>';
	    document.write(html);
    },
    showWaitWin:function(param)
    {
        try
        {
            var createExcelfrm = document.getElementById("waitfrm");
            var paramObj = document.getElementById("param");
            paramObj.value = param;
            createExcelfrm.submit();
        }catch(e){}
    },
    initFileDownloadWaitWin:function(url,msg,param)
    {
        if(typeof(url) == 'undefined' || url == null) url = "";
        if(typeof(msg) == 'undefined' || msg == null || msg == "" ) msg = "正在处理中，请稍候...";
        if(typeof(param) == 'undefined' || param == null) param = "";
	   
	    var waitwin = document.createElement("div");
	    document.body.appendChild(waitwin);
        
	    var html = '<form id="filedownloadfrm" action="'+url+'" target="filedownloadwin" method="post">'
                +'<input type="hidden" id="url" name="url" value="'+url+'" />'
                +'<input type="hidden" id="param" name="param" value="'+param+'" />'
                +'<input type="hidden" id="msg" name="msg" value="'+msg+'" />'
                +'</form>'
                +'<iframe id="filedownloadwin" name="filedownloadwin" style="display:none" src=""></iframe>';

	    waitwin.outerHTML = html;
    },
    showFileDownloadWaitWin:function(param)
    {
        try
        {
            WaitHelper.showWaitMessage();
            var filedownloadfrm = document.getElementById("filedownloadfrm");
            var paramObj = document.getElementById("param");
            paramObj.value = param;
            filedownloadfrm.submit();
            if(event != null)
            {
               var btn = event.srcElement ;
               window.setTimeout("try{document.getElementById('"+btn.id+"').disabled=false;}catch(e){}",8000);
            }
        }catch(e){WaitHelper.closeWaitMessage();}
    }

}
function redirectPage(typeId)
{
    alert("Please Login!");
    var url = window.location.href;
    url = encodeURIComponent(url);
    window.location.href = "UserLogin.aspx?url="+url;
}





if(typeof(JsDebug) != "undefined" && JsDebug)
    alert("app.edit.js");
    
    

