var Namespace = {

    Register: function(_Name) {
        var chk = false;
        var cob = "";
        var spc = _Name.split(".");
        for (var i = 0; i < spc.length; i++) {
            if (cob != "") { cob += "."; }
            cob += spc[i];
            chk = this.Exists(cob);
            if (!chk) { this.Create(cob); }
        }

        if (chk) { throw "Namespace: " + _Name + " is already defined."; }
    },

    Create: function(_Src) {
        eval("window." + _Src + " = new Object();");
    },

    Exists: function(_Src) {
        eval("var NE = false; try{if(" + _Src + "){NE = true;}else{NE = false;}}catch(err){NE=false;}");
        return NE;
    }
}

/************************
*   Ns Common Library
************************/
Namespace.Register("NS.Common");

NS.Common.PadDigits = function(n, totalDigits) {
    n = n.toString();
    var pd = '';
    if (totalDigits > n.length) {
        for (i = 0; i < (totalDigits - n.length); i++) {
            pd += '0';
        }
    }
    return pd + n.toString(); 
}

NS.Common.Focus = function(arg) {
    if ($(arg) != null) {
        if ($(arg).nodeName == 'INPUT') {
            setTimeout(function() { $(arg).focus(); $(arg).select(); }, 1);
        } else {
            setTimeout(function() { $(arg).focus(); }, 1);
        }
    }
}

NS.Common.IsValidEmail = function(strEmail) {
    return (strEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1);
}

NS.Common.SetCookie = function(cookieName, cookieValue, nDays) {
    var today = new Date();
    var expire = new Date();
    if (nDays == null || nDays == 0) nDays = 1;
    expire.setTime(today.getTime() + 3600000 * 24 * nDays);
    document.cookie = cookieName + "=" + escape(cookieValue)
                     + ";path=/;expires=" + expire.toGMTString();
}

NS.Common.ReadCookie = function(cookieName) {
    var theCookie = "" + document.cookie;
    var ind = theCookie.indexOf(cookieName);
    if (ind == -1 || cookieName == "") return "";
    var ind1 = theCookie.indexOf(';', ind);
    if (ind1 == -1) ind1 = theCookie.length;
    return unescape(theCookie.substring(ind + cookieName.length + 1, ind1));
}

NS.Common.DeleteCookie = function(cookieName) {
    var d = new Date();
    document.cookie = cookieName + "=;path=/;expires=" + d.toGMTString() + ";" + ";";
}


NS.Common.TwoDigitRound = function(arg) {
    return Math.round(arg * 100 + 0.1) / 100;
}

NS.Common.GetY = function(oElement) {
    var iReturnValue = 0;
    while (oElement != null) {
        iReturnValue += oElement.offsetTop;
        oElement = oElement.offsetParent;
    }
    return iReturnValue;
}

NS.Common.GetX = function(oElement) {
    var iReturnValue = 0;
    while (oElement != null) {
        iReturnValue += oElement.offsetLeft;
        oElement = oElement.offsetParent;
    }
    return iReturnValue;
}

NS.Common.ResolveURL = function(url, siteRoot) {
    if (siteRoot) {
        if (url.startsWith("~/")) {
            return url.replace("~/", siteRoot.endsWith("/") ? siteRoot : siteRoot + "/");
        } else {
            return (siteRoot.endsWith("/") ? siteRoot : siteRoot + "/") + 
                (url.startsWith("/") ? (url.substring(1, url.length-1)) : url);
        }
    }
    //full url or relative url is given by caller
    return url;
}

NS.Common.CenterDialog = function(dElement, topOffset) {
    if (dElement) {
        var IpopLeft = (document.documentElement.scrollWidth - dElement.getWidth()) / 2;
        if (IpopLeft < 0) { IpopLeft = 0; }
        dElement.style.left = IpopLeft + "px";
        
        var IpopTop = (document.documentElement.clientHeight - dElement.getHeight()) / 2 + document.documentElement.scrollTop +  (topOffset || 0);
        if (IpopTop < 0) { IpopTop = 0; }
        dElement.style.top = IpopTop + "px";
    }
}


NS.Common.AC_Flash = function(flashName, options){
	this.options = options || {};
	if (AC_FL_RunContent == 0) 
	{
		alert('This page requires AC_RunActiveContent.js.');
	} else {
		AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0',
		'wmode', this.options.wmode || 'opaque',
		'width', this.options.width || '100%',
		'height',this.options.height || '',
		'id', this.options.id || flashName,
		'name',this.options.id || flashName,
		'align',this.options.align || 'middle',
		'quality', this.options.quality || 'high',
		'bgcolor',this.options.bgcolor || '#ffffff',
		'allowscriptaccess', this.options.allowscriptaccess || 'sameDomain',
		'allowfullscreen', this.options.allowfullscreen || 'false',
		'pluginspage',this.options.pluginspage || 'http://www.macromedia.com/go/getflashplayer',
		'flashvars', this.options.flashvars || '',
		'movie', flashName ); //end AC code
	}
}


NS.Common.ReturnTrue = function() { return true; }




/************************
*   Protype Extension
************************/
String.prototype.replaceAll = function(pcFrom, pcTo) {
    var i = this.indexOf(pcFrom);
    var c = this;
    while (i > -1) {
        c = c.replace(pcFrom, pcTo);
        i = c.indexOf(pcFrom);
    }
    return c;
}

String.prototype.bool = function() {
    return (/^true$/i).test(this);
};


Date.prototype.toLocaleShort = function() {
    var lStr = this.toLocaleString();
    return lStr.substr(lStr.indexOf(",") + 1);
}


