﻿/////////////////////////////////////////////
//      Site Common JavaScript Libaray     // 
//  Author: Qiu Bin (qiubin(at)gmail.com)  //
//           Updated: 2007-06-03           //
/////////////////////////////////////////////

function isIE() {
    return (document.all && !window.opera) ? true : false;
}

function CloseWindow() {
    window.close();
   // window.open('javascript:window.close();','_self','');
   // won't work under IE6,WIN2000Server
}

function RefreshOpener(ControlId,EventArgument) {
    if (window.dialogArguments)     
    {
        dialogArguments.__doPostBack(ControlId,EventArgument);
    }
    else
    {
        //window.opener.__doPostBack(ControlId,EventArgument);
        //causing updating with ajax
        //--todo--
        window.opener.location.href=window.opener.location.href;
    }
}

function RefreshOpenerAndClose(ControlId,EventArgument) {
    RefreshOpener(ControlId,EventArgument);
    CloseWindow();
}

function RedirectOpener(url) {
    if (window.dialogArguments)     
    {
        dialogArguments.location=url;
    }
    else
    {
        window.opener.location=url;
    }
}

function CreateModalWindow(url,width,height,sizeable,status,scroll,data) {
    var rndParam=Math.ceil(Math.random()* 100000);
    var newUrl;
    if (url.indexOf("?")==-1)
    {
        newUrl=url+"?rp="+rndParam;
    }
    else
    {
        newUrl=url+"&rp="+rndParam;
    }
    
    if(window.showModalDialog)
    {
        if (!data) data=window;
        if (!status) status='no';
        if (!scroll) scroll='no';
        if (!sizeable) sizeable='yes';
        winprops="dialogHeight:"+height+"px;dialogWidth:"+width+"px;"+"edge:Raised;center:Yes;help:No;resizable:"+sizeable+";status:"+status+";scroll:"+scroll+";";
        var result=window.showModalDialog(newUrl,data,winprops);
        return result;
    }
    else
    {
        var newWindowWidth=(width)?width:(screen.availWidth*.9);
        var newWindowHeight=(height)?height-30:(screen.availHeight*.9);
        var newLeft=(screen.availWidth-newWindowWidth)/2;
        var newTop=(screen.availHeight-newWindowHeight)/2-30;
        var newWindowAttribs="modal=yes,width="+newWindowWidth+",height="+newWindowHeight+",status=no,resizable=yes,menubar=no,toolbar=no,left="+newLeft+",top="+newTop;
        window.open(newUrl,"SuperDialog",newWindowAttribs);
    }
}

function CreateModelessWindow(url,width,height,sizeable,status,scroll,data) {
    if(window.showModelessDialog)
    {
        if (!data) data=window;
        if (!status) status='no';
        if (!scroll) scroll='no';
        if (!sizeable) sizeable='yes';
        winprops="dialogHeight:"+height+"px;dialogWidth:"+width+"px;"+"edge:Raised;center:Yes;help:No;resizable:"+sizeable+";status:"+status+";scroll:"+scroll+";";
        var result=window.showModelessDialog(url,data,winprops);
        return result;
    }
    else
    {
        var newWindowWidth=(width)?width:(screen.availWidth*.9);
        var newWindowHeight=(height)?height-30:(screen.availHeight*.9);
        var newLeft=(screen.availWidth-newWindowWidth)/2;
        var newTop=(screen.availHeight-newWindowHeight)/2-30;
        var newWindowAttribs="modal,width="+newWindowWidth+",height="+newWindowHeight+",status=no,resizable=yes,menubar=no,toolbar=no,left="+newLeft+",top="+newTop;
        window.open(url,"",newWindowAttribs);
    }
}

function CreateWindow(windowName,url,width,height,sizeable,status,scroll,toolbar,menubar) {
    if (!status) status='no';
    if (!scroll) scroll='yes';
    if (!sizeable) sizeable='yes';
    if (!toolbar) toolbar='no';
    if (!menubar) menubar='no';
    var newWindowWidth=(width)?width:(screen.availWidth*.9);
    var newWindowHeight=(height)?height-30:(screen.availHeight*.9);
    var newLeft=(screen.availWidth-newWindowWidth)/2;
    var newTop=(screen.availHeight-newWindowHeight)/2-30;
    var newWindowAttribs="modal,width="+newWindowWidth+",height="+newWindowHeight+",status="+status+",resizable="+sizeable+",menubar="+menubar+",scroll="+scroll+",toolbar="+toolbar+",left="+newLeft+",top="+newTop;
    var popup=window.open(url,windowName,newWindowAttribs);
    popup.focus();
}

function clear_monitor() {
	change_monitor_value('', true);
}
function LTrim( value ) {	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");	
}
function RTrim( value ) {	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");	
}
function Trim( value ) {	
	return LTrim(RTrim(value));	
}