﻿//http://www.codeproject.com/KB/scripting/jsnamespaces.aspx?display=Print
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;
    }
}
Namespace.Register('Utils');

Utils.RegEx={
    Email:/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/
}
Utils.Cookie={
    getByName:function(check_name){
        var a_all_cookies = document.cookie.split( ';' );
        var a_temp_cookie = '';
        var cookie_name = '';
        var cookie_value = '';
        var b_cookie_found = false; // set boolean t/f default f	
        for ( i = 0; i < a_all_cookies.length; i++ )
        {
	        a_temp_cookie = a_all_cookies[i].split( '=' );
	        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	        if ( cookie_name == check_name )
	        {
		        b_cookie_found = true;
		        if ( a_temp_cookie.length > 1 )
		        {
			        cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
		        }
		        return cookie_value;
		        break;
	        }
	        a_temp_cookie = null;
	        cookie_name = '';
        }
        if ( !b_cookie_found )
        {
	        return null;
        }

    },
    setCookie:function(key,value){
        document.cookie=key+"="+escape(value);
    }
}
Utils.Request={
    querystring:function(key){
        return this.queryParam(window.location.search.substring(1),key);
    },
    queryParam:function(href,key){
        var _query=href.toLowerCase();
        var params=_query.split('&');
        for(var i=0;i<params.length;i++){
            var pair=params[i].split('=');
            if(pair[0]==key.toLowerCase()) return pair[1];
        }
        return false;
        
    }
}
Utils.Ajax = {
    AsyncPost:function(url,data,clientCallback){
        var xmlhttp=false;
        for(var i=0;i<1;i++){
            try{xmlhttp=new ActiveXObject('Msxml2.XMLHTTP');break;}catch(e){}
            try{xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');break;}catch(e){}
            try{xmlhttp=new XMLHttpRequest();break;}catch(e){}                         
        }
        if(xmlhttp){
            xmlhttp.open('POST',url,true);
            xmlhttp.setRequestHeader("Accept-charset","utf-8");                
            xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            
            xmlhttp.onreadystatechange=function(){
                if(xmlhttp.readyState==4) clientCallback(xmlhttp.responseText);
            }
            xmlhttp.send(data);
        }       
    }
}
Utils.Position={
    center: function(width, height, win) {
        var _window = win ? win : window;
        var _scrollXY = this.scrollXY(_window);
        var _viewport = this.viewport(_window);
        /*
        var _x = ((this.clientWidth(_window) - width) / 2) + this.scrollLeft(_window);
        var _y = ((this.clientHeight(_window) - height) / 2) + this.scrollTop(_window);
        */
        var _x = (_viewport[0] - width) / 2 + _scrollXY[0];
        var _y = (_viewport[1] - height) / 2 + _scrollXY[1];
        return [_x, _y];
    },
    scrollXY: function(targetWin) {
        if (typeof targetWin.pageYOffset != 'undefined') return [targetWin.pageXOffset, targetWin.pageYOffset];
        if (targetWin.document.body.scrollLeft) return [targetWin.document.body.scrollLeft, targetWin.document.body.scrollTop];
        return [targetWin.document.documentElement.scrollLeft, targetWin.document.documentElement.scrollTop];
    },
    viewport: function(targetWin) {
        if (typeof targetWin.innerWidth != 'undefined') return [targetWin.innerWidth, targetWin.innerHeight];
        if (typeof targetWin.document.documentElement != 'undefined' &&
                typeof targetWin.document.documentElement.clientWidth != 'undefined' &&
                targetWin.document.documentElement.clientWidth != 0)
            return [targetWin.document.documentElement.clientWidth, targetWin.document.documentElement.clientHeight];
        return [targetWin.document.body.clientWidth, targetWin.document.body.clientHeight];
    },
    f_clientWidth:function() {
	    return this.f_filterResults (
		    window.innerWidth ? window.innerWidth : 0,
		    document.documentElement ? document.documentElement.clientWidth : 0,
		    document.body ? document.body.clientWidth : 0
	    );
    },
    f_clientHeight:function() {
	    return this.f_filterResults (
		    window.innerHeight ? window.innerHeight : 0,
		    document.documentElement ? document.documentElement.clientHeight : 0,
		    document.body ? document.body.clientHeight : 0
	    );
    },
    f_scrollLeft:function() {
	    return this.f_filterResults (
		    window.pageXOffset ? window.pageXOffset : 0,
		    document.documentElement ? document.documentElement.scrollLeft : 0,
		    document.body ? document.body.scrollLeft : 0
	    );
    },
    f_scrollTop:function() {
	    return this.f_filterResults (
		    window.pageYOffset ? window.pageYOffset : 0,
		    document.documentElement ? document.documentElement.scrollTop : 0,
		    document.body ? document.body.scrollTop : 0
	    );
    },
    f_filterResults:function(n_win, n_docel, n_body) {
	    var n_result = n_win ? n_win : 0;
	    if (n_docel && (!n_result || (n_result < n_docel)))
		    n_result = n_docel;
	    return n_body && (!n_result || (n_result < n_body)) ? n_body : n_result;
    }    
}