//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////
/***
	General Functions
***/

var General = {
	
	
	/**************************************************
	Element
	**************************************************/
	$ : function(string)
	{
		string = string || null;
		var pointer = null;
		
		if (string === null)
			pointer = null;
		if (typeof(string) === 'string')
			pointer = document.getElementById(string);
		else if (typeof(string) === 'object')
			pointer = string;
			
		return pointer;
	},
	
	/**************************************************
	Frame
	**************************************************/
	$frame : function(id)
	{
		if (window.frames[id])	
			return window.frames[id];			
		else
			return null;
	},
	
	/**************************************************
	Browser info
	**************************************************/
	browser : '', 
	isIE : false,
	getBrowser : function()
	{
		////////////	defining browser	//////////////////////
		if (navigator.appVersion.indexOf("MSIE 6") !== -1)
			General.browser = "ie6";
		else if (navigator.appVersion.indexOf("MSIE 7") !== -1)
			General.browser = "ie7";
		else if (navigator.appVersion.indexOf("MSIE 8") !== -1)
			General.browser = "ie8";
		else if (navigator.appVersion.indexOf("Chrome") !== -1)
			General.browser = "chrome";
		else if (navigator.appVersion.indexOf("Safari") !== -1)
			General.browser = "safari";
		else if (navigator.userAgent.indexOf("Firefox/3") !== -1)
			General.browser = "ff3"; 
		else if (navigator.userAgent.indexOf("Firefox/2") !== -1)
			General.browser = "ff2"; 
		else if (navigator.userAgent.indexOf("Opera/") !== -1)
			General.browser = "opera"; 
		else 
			General.browser = navigator.userAgent; 	
			
		if (General.browser.indexOf("ie") === 0) General.isIE = true;
	}, 
	
	/**************************************************
	resize the page to specific values
	**************************************************/
	resizeToInner : function(innerWidth, innerHeight)
	{
		var frameWidth,frameHeight;
		if (self.innerHeight) // all except Explorer
		{
			frameWidth = self.innerWidth;
			frameHeight = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight) // Explorer 6 Strict Mode
		{
			frameWidth = document.documentElement.clientWidth;
			frameHeight = document.documentElement.clientHeight;
		}
		else if (document.body) // other Explorers
		{
			frameWidth = document.body.clientWidth;
			frameHeight = document.body.clientHeight;
		}
	
		var innerWidth = ( innerWidth!=null ? innerWidth : frameWidth );
		var innerHeight = ( innerHeight!=null ? innerHeight : frameHeight );
	
		difWidth = 0 + innerWidth - frameWidth;
		difHeight = 0 + innerHeight - frameHeight;
	
		/*
		alert("frameWidth = " + frameWidth + "\n" + "frameHeight = " + frameHeight);
		alert("difWidth = " + difWidth + "\n" + "difHeight = " + difHeight);
		*/
	
		if (difWidth != 0 )
		{
			window.resizeBy(difWidth,0);
		}
		if (difHeight != 0)
		{
			window.resizeBy(0,difHeight);
		}
	
		// scroll down
		window.scrollBy(0,120);
	},
	
	
	/**************************************************
	Calculation page viewable size (as in screen resolution)
	**************************************************/
	screenX : 0,
	screenY : 0,
	
	calcScreenSize : function()
	{
		if (self.innerHeight) // all except Explorer
		{
			General.screenX = self.innerWidth;
			General.screenY = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight) // Explorer 6 Strict Mode
		{
			General.screenX = document.documentElement.clientWidth;
			General.screenY = document.documentElement.clientHeight;
		}
		else if (document.body) // other Explorers
		{
			General.screenX = document.body.clientWidth;
			General.screenY = document.body.clientHeight;
		} 
	},
	
	/**************************************************
	Calculation page size
	**************************************************/
	pageX : 0,
	pageY : 0,
	
	calcPageSize : function()
	{
		var clientW = document.body.clientWidth;
		var clientH = document.body.clientHeight;
		
		var scrollW = document.body.scrollWidth;
		var scrollH = document.body.scrollHeight;
		
		///// saving Page width /////
		if (clientW > scrollW)
			General.pageX = clientW;
		else
			General.pageX = scrollW;
			
		///// saving Page Height /////
		if (clientH > scrollH)
			General.pageY = clientH;
		else
			General.pageY = scrollH;
	},
	
	/**************************************************
	Get Scroll Top + Left
	**************************************************/
	getScrollTop : function()
	{
		var scrollTop = 0;
		if (General.browser == 'ie7' || General.browser == 'ie6' || General.browser == 'ie8')
		{
			var scroll1 = document.body.scrollTop;
			var scroll2 = document.documentElement.scrollTop;
			scrollTop = scroll1 > scroll2 ? scroll1 : scroll2;
		}
		else
		{
			scrollTop =  window.pageYOffset;
		}
			
		return scrollTop;
	},
	
	getScrollLeft : function()
	{
		var scrollLeft = 0;
		if (General.browser == 'ie7' || General.browser == 'ie6' || General.browser == 'ie8')
		{
			var scroll1 = document.body.scrollLeft;
			var scroll2 = document.documentElement.scrollLeft;
			scrollLeft = scroll1 > scroll2 ? scroll1 : scroll2;
		}
		else
		{
			scrollLeft =  window.pageXOffset;
		}
			
		return scrollLeft;
	},
	
	/**************************************************
	Cookies
	**************************************************/
	Cookies :
	{
		_read : function(key)
		{
			// Get cookie string and separate into individual cookie phrases:
			var cookie_string = "" + document.cookie;
			var cookie_array = cookie_string.split ("; ");
		
			// Scan for desired cookie:
			for (var i = 0; i < cookie_array.length; ++ i)
			{
				var single_cookie = cookie_array[i].split ("=");
				if (single_cookie.length != 2)
					continue;
				var name  = unescape (single_cookie [0]);
				var value = unescape (single_cookie [1]);
		
				// Return cookie if found:
				if (key == name)
				{
					return value;
				}
			}
			
			// Cookie was not found:
			return null;
		},
		
		_write : function(name, value, path)
		{
			// Build the expiration date string:
			var expiration_date = new Date ();
			if (General.browser !== 'chrome')
				expiration_date.setYear (expiration_date.getFullYear () + 1);
			else
				expiration_date.setYear (expiration_date.getYear () + 1);
			expiration_date = expiration_date.toGMTString();
			
			// Build the set-cookie string:
			var cookie_string = escape (name) + "=" + escape (value) + "; expires=" + expiration_date;
			if (path != null)
				cookie_string += "; path=" + path;
		
			// Create/update the cookie:
			document.cookie = cookie_string;
		}
		
	},
	
	
	/**************************************************
	Object Position
	**************************************************/
	ObjectPosition :
	{
		X : function(PdivId)
		{
			if (!PdivId)
				return -1;
			else if (typeof(PdivId) == 'string')
				divId = document.getElementById(PdivId);
			else if (typeof(PdivId) == 'object')
				divId = PdivId;
			else
				return -1;
			
			var counterX = divId.offsetLeft;
			counterX -= divId.scrollLeft;
			
			var divIdX = divId;
			while (divIdX.offsetParent)
			{
				counterX += divIdX.offsetParent.offsetLeft;
				if (divIdX != document.body)
					counterX -= divIdX.scrollLeft;
				
				divIdX = divIdX.offsetParent;
			}
			return counterX;	
		},
		
		
		Y : function(PdivId)
		{
			if (!PdivId)
				return -1;
			else if (typeof(PdivId) == 'string')
				divId = document.getElementById(PdivId);
			else if (typeof(PdivId) == 'object')
				divId = PdivId;
			else
				return -1;
			
			var counterY = divId.offsetTop;
			counterY -= divId.scrollTop;
			
			var divIdY = divId;
			while(divIdY.offsetParent)
			{
				counterY += divIdY.offsetParent.offsetTop;
				if (divIdY != document.body)
					counterY -= divIdY.scrollTop;
				divIdY = divIdY.offsetParent;
			}
			return counterY;
			
		}
	}
};



///////////////////////////////////////////
/////////// [Base 64] ///////////
///////////////////////////////////////////
General.Base64 =
{
    // private property
    _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode: function (input)
    {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        //input = General.Base64._utf8_encode(input);

        while (i < input.length)
        {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2))
            {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3))
            {
                enc4 = 64;
            }

            output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },

    // public method for decoding
    decode: function (input)
    {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length)
        {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64)
            {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64)
            {
                output = output + String.fromCharCode(chr3);
            }

        }

        //output = General.Base64._utf8_decode(output);

        return output;

    },

    // private method for UTF-8 encoding
    _utf8_encode: function (string)
    {
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++)
        {

            var c = string.charCodeAt(n);

            if (c < 128)
            {
                utftext += String.fromCharCode(c);
            }
            else if ((c > 127) && (c < 2048))
            {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else
            {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode: function (utftext)
    {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while (i < utftext.length)
        {

            c = utftext.charCodeAt(i);

            if (c < 128)
            {
                string += String.fromCharCode(c);
                i++;
            }
            else if ((c > 191) && (c < 224))
            {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else
            {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    },

    encodeString: function (txt)
    {
        var hasUtfChars = false;
        for (var i = 0; i < txt.length && !hasUtfChars; ++i)
        {
            if (txt.charCodeAt(i) > 127) hasUtfChars = true;
        }

        var result;
        if (hasUtfChars) txt = General.Base64._utf8_encode(txt);
        return General.Base64.encode(txt);
    },

    decodeString: function (encodedString)
    {
        var result = General.Base64.decode(encodedString);
        var hasUtfChars = false;
        for (var i = 0; i < result.length && !hasUtfChars; ++i)
        {
            if (result.charCodeAt(i) > 127) hasUtfChars = true;
        }
        if (hasUtfChars) result = General.Base64._utf8_decode(result);
        return result;
    }
}




/***********
Calculation page viewable size
***********/
General.calcScreenSize();
/***********
getting user's browser
***********/
General.getBrowser();

