﻿
function addListener(element, type, expression, bubbling)
{
    bubbling = bubbling || false;
    if(window.addEventListener) 
    { // Standard
        element.addEventListener(type, expression, bubbling);
        return true;
    } 
    else if(window.attachEvent) 
    { // IE
        element.attachEvent('on' + type, expression);
        return true;
    }
    else return false;
}
 var __isFireFox = navigator.userAgent.match(/gecko/i);
     //returns the absolute position of some element within document  
 function getElementAbsolutePos(element) {  
     var res = new Object();  
     res.x = 0; res.y = 0;  
     if (element !== null) {  
         res.x = element.offsetLeft;
         res.y = element.offsetTop;

         var offsetParent = element.offsetParent;  
         var parentNode = element.parentNode;  

         while (offsetParent !== null) {  
             res.x += offsetParent.offsetLeft;  
             res.y += offsetParent.offsetTop;  

             if (offsetParent != document.body && offsetParent != document.documentElement) {
                 res.x -= offsetParent.scrollLeft;  
                 res.y -= offsetParent.scrollTop;  
             }  
             //next lines are necessary to support FireFox problem with offsetParent  
             if (__isFireFox) {  
                 while (offsetParent != parentNode && parentNode !== null) {  
                     res.x -= parentNode.scrollLeft;  
                     res.y -= parentNode.scrollTop;  
                       
                     parentNode = parentNode.parentNode;  
                 }      
             }  
             parentNode = offsetParent.parentNode;  
             offsetParent = offsetParent.offsetParent;  
         }  
     }  
     return res;
 }  

function getCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	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++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function setCookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}
/*
function clearCookie(c_name)
{
   setCookie(c_name,'1',0);
}
*/
function clearCookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function selectAll(parentId, me) {
    var parent = document.getElementById(parentId);
    var list = parent.getElementsByTagName('Input');
    for (var i = 0; i < list.length; i++) {
        if (list[i].type == 'checkbox')
            list[i].checked = false;
    }
}


XmlHttpObject.prototype.Method ='GET';
XmlHttpObject.prototype.Async =true;
XmlHttpObject.prototype.Request = null;
XmlHttpObject.prototype.GetRequest= function()
{
    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

function XmlHttpObject(method, async)
{
    this.Method = method;
    this.Async = async;
    this.Request = this.GetRequest();
}


XmlHttpObject.prototype.Submit =  function(url, datum,requestCompleted) 
{

    if (this.Request!=null)
    {
        if( requestCompleted ==null)
            return ;
        this.Request.open(this.Method, url, this.Async)
         if(datum !=null && this.Method == 'POST')
        {
            var vars='';
            for( var key in datum)
            {
                vars+=key +'=';
                vars+=datum[key] +'&';
            }
            if(vars !='')
            {
                vars = vars.substring(0,vars.length-1);
            }
        }
        else
        {
            vars =null;
        }
        if(this.Async == true)
        {
            this.Request.onreadystatechange = function()
            {
                if (this.readyState==4)
                {
                     if(requestCompleted)
                     {
                        requestCompleted(this.responseText);
                     }
                }
            }
        }
        this.Request.send(vars);
        if(this.Async ==false)
        {
            if(requestCompleted)
            {
                 if (this.readyState==4)
                 {
                    requestCompleted(this.responseText);
                 }
                 else
                 {
                    requestCompleted('Error');
                 }
            }
        }
    }
    else
    {
        alert("Your browser does not support XMLHTTP.")
    }
}

Vinasource.prototype.AddListener =addListener;
Vinasource.prototype.GetElementAbsolutePos = getElementAbsolutePos;
Vinasource.prototype.GetCookie= getCookie;
Vinasource.prototype.SetCookie= setCookie;
Vinasource.prototype.ClearCookie = clearCookie;
Vinasource.prototype.Request = null;
//Vinasource.prototype.HttpRequest = XmlHttpObject;
function Vinasource()
{
}
