/*****************************************************
 * EOS.Common.js Copyright (C) CHAOS 2007
 *****************************************************/
 
// The max height of dialog page.
var eos_DialogMaxHeight = 700;

// The split char in return values.
var eos_SplitChar = '|';

// Window's status.
window.status = 'Welcome to Beijing Unesco.';

// Add getQuery function for request's url string.
String.prototype.getQuery = function(name){
	var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	var r = this.substr(this.indexOf("\?")+1).match(reg);
	if (r != null) return unescape(r[2]); 
	return null;
}

// Add trim function for string.
String.prototype.trim = function(){
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

// Add appendUrlParamter for string.
String.prototype.appendUrlParameter = function(name, val){
    var index = this.indexOf(name + '=');
    if (index > 0){
        var endPos = this.indexOf('&', index);
        if (endPos > index){
            return this.replace('/' + this.substr(index, endPos - index) + '/', name + '=' + val);
        }
        else{
            return this.substr(0, index) + name + '=' + val;
        }
    }
    else{
        return this.indexOf('?') > 0 ? (this + '&' + name + '=' + val) : (this + '?' + name + '=' + val);
    }
}

String.prototype.replaceXml = function(){
    return this.replace('&gt;', '&gt').replace('&lt;', '&lt');
}

// Ajax call server proc.
function ajaxCallback(objId, command, data, callback, sender){
    if (typeof data == "undefined" || data == null) data = "";
    if (typeof sender == "undefined" || sender == null) sender = "";
    document.getElementById('__CLIENTPOSTDATA').value = objId + eos_SplitChar + command + eos_SplitChar + data.replaceXml() + eos_SplitChar + sender;
    
    Anthem_InvokePageMethod("Client_Callback", null, callback);
}

function ajaxCall(command, data, callback){
    if (typeof command == "undefined" || command == null) command = "";
    if (typeof data == "undefined" || data == null) data = "";
    if (typeof callback == "undefined" || callback == null) callback = ajaxCallback_callback;
    ajaxCallback("", command, data, callback);
}

// Default callback.
function ajaxCallback_callback(rv){
    if (rv.value.split(':')[0]=='1'){
        alert('操作成功！');
    }
    else{
        alert('操作失败！' + rv.value.split(':')[1]);
    }
}

// Default dialog callback.
function ajaxCallback_close_callback(rv){
    if (rv.value.split(':')[0]=='1'){
        alert('操作成功！'); window.returnValue = 1; window.close();
    }
    else{
        alert('操作失败！' + rv.value.split(':')[1]);
    }
}

// Ajax call and returns object(single, table, dataset etc.)
function ajax(objId, command, data, callback){
    if (typeof data == "undefined" || data == null) data = "";
    if (typeof callback == "undefined" || callback == null) callback = ajax_callback;
    document.getElementById('__CLIENTPOSTDATA').value = objId + eos_SplitChar + command + eos_SplitChar + data.replaceXml();
    
    Anthem_InvokePageMethod("Execute", null, callback);
}

function ajax_callback(rv){
    if (rv.error == null) alert('操作成功!');
    else alert(rv.error);
}

// Resize page when page initlizating.
function pageResize(){
	var dWidth = parseInt(document.body.scrollWidth) - parseInt(document.body.clientWidth);
	var dHeight = parseInt(document.body.scrollHeight) - parseInt(document.body.clientHeight);
	var realHeight = parseInt(window.dialogHeight) + dHeight;
	if (realHeight > eos_DialogMaxHeight)
	{
		height = document.body.scrollHeight;
		realHeight = eos_DialogMaxHeight;
		dHeight = eos_DialogMaxHeight - parseInt(window.dialogHeight);
	}
	
	while (dWidth >= 40)
	{
		dWidth -= 40;
		window.dialogLeft = parseInt(window.dialogLeft) - 20 + "px";
		window.dialogWidth = parseInt(window.dialogWidth) + 40 + "px";
		if (dHeight >= 40){
		    dHeight -= 40;
		    window.dialogTop = parseInt(window.dialogTop) - 20 + "px";
		    window.dialogHeight = parseInt(window.dialogHeight) + 40 + "px";
		}
	}

	window.dialogTop = parseInt(window.dialogTop) - dHeight/2 + "px";
	window.dialogLeft = parseInt(window.dialogLeft) - dWidth/2 + "px";
	window.dialogHeight = realHeight + "px";
	window.dialogWidth = parseInt(window.dialogWidth) + dWidth + "px";
}

// Pop a window by url with getting width, height, name.
function pageShowWindow(url, width, height, name){
	var maxWidth = window.screen.availWidth - 10;
	var maxHeight = window.screen.availHeight - 30;
	var left = (maxWidth - width)/2;
	var top = (maxHeight - height)/2;
	var params = "directories=no,toolbar=no,scrollbars=no,height=" + height
	    + ",width=" + width + ",top=" + top + ",left=" + left;
	window.open(url, name, params);
}

// Show a dialog by url with getting width, height.
function pageShowDialog(url, width, height, resizable){	
	var left = (screen.width - width) / 2;
	var top = (screen.height - height) / 2;
	
	if (typeof resizable == "undefined" || resizable == null){
	    resizable = "no";
	}
	
	var params = "status:no;center:no;help:no;minimize:no;maximize:no;scroll=no;border:think;resizable:"
	    + resizable + ";dialogWidth:" + width + "px;dialogHeight:" + height + "px;dialogTop:"
	    + top + "px;dialogLeft:" + left + "px";
	return window.showModalDialog(url, window, params);
}

// Refresh page.
function pageRefresh(){
    window.location = window.location;
}

// go right.
function goRight(url, child){
    if (typeof child == "undefined" || child == null) child = true;
    var pk = window.location.toString().getQuery("PK");
    if ((pk == null || pk == '') && child){
        alert('基本信息未录入，请先录入！');
    }
    else{
        window.parent.Right.location = url + window.location.search;    
    }
}

// return frame index.
function goIndex(){
    window.parent.location = window.parent.location;
}

// Get input file name.
function inputFileChange(file, txtObj){
    var path = file.value;
    var start = path.lastIndexOf("\\");
    if (start < 0){
        alert("文件路径不正确！");
    }
    else{
        path = path.substr(start);
        var end = path.lastIndexOf(".");
        if (end < 0) txtObj.value = path;
        else txtObj.value = path.substr(1, end-1);
    }
}

// Check parent & children tree nodes.
function treeNodeChecked(node){
    if (node.Checked){
        var par = node.Parent;
        while (par != null){
            par.Check();
            par = par.Parent;
        }
    }
    
    // check children.
    treeCheckedNodes(node, node.Checked);    
}

// Check children tree nodes.
function treeCheckedNodes(node, checked){
    for (var i = 0; i < node.Nodes.length; i++){
        if (checked){
            node.Nodes[i].Check();
        }
        else{
            node.Nodes[i].UnCheck();
        }
        
        treeCheckedNodes(node.Nodes[i], checked);
    }
}

// Get cookies.
function getCookie(name){
    var arg = name + "=";       
    var alen = arg.length;   
    var clen = document.cookie.length;   
    
    var i = 0;   
    while (i < clen) {     
        var j = i + alen;     
        if (document.cookie.substring(i, j) == arg){  
            return getCookieVal (j);
        }
        i = document.cookie.indexOf(" ", i) + 1;     
        if (i == 0) break;   
    }   
    return null; 
} 

// Set cookies.  
function setCookie(name, value) {   
    var argv = SetCookie.arguments;   
    var argc = SetCookie.arguments.length;   
    var expires = (argc > 2) ? argv[2] : null;   
    var path = (argc > 3) ? argv[3] : null;   
    var domain = (argc > 4) ? argv[4] : null;   
    var secure = (argc > 5) ? argv[5] : false;  
     
    document.cookie = name + "=" + escape (value) +   
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +   
    ((path == null) ? "" : ("; path=" + path)) +   
    ((domain == null) ? "" : ("; domain=" + domain)) +     
    ((secure == true) ? "; secure" : ""); 
} 

// Private get cookie value by offset.
function getCookieVal(offset) {   
    var endstr = document.cookie.indexOf (";", offset);   
    if (endstr == -1) {    
        endstr = document.cookie.length;   
    }
    return unescape(document.cookie.substring(offset, endstr)); 
}


