//............................................................
//  Register object in Page
//............................................................
function RegisterNamespace(name)
{
   eval("window." + name + " = new Object();");
}

//............................................................
//  Blocking Page
//............................................................
function BlockingPage()
{
    //  Cretae element Shadow
    var shadowDiv = document.createElement('div');   
    shadowDiv.style.position = "absolute";
    shadowDiv.style.visibility="visible";
    shadowDiv.style.zIndex = 999; 
    shadowDiv.style.height = document.body.scrollHeight + 'px';
    shadowDiv.style.width = document.body.scrollWidth + 'px';
    shadowDiv.style.top = '0';
    shadowDiv.style.left = '0';    
    shadowDiv.style.backgroundColor = "#fff"; 
    shadowDiv.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=50)";
    shadowDiv.style.MozOpacity = 0.5;    
    document.body.appendChild(shadowDiv);
    
    //  Cretae element Image
    var image = document.createElement('img');
    image.style.position = "absolute";
    image.style.visibility="visible";
    image.style.zIndex = 1000;
    image.style.top = (document.body.clientHeight/2 + document.body.scrollTop - 50) + 'px';
    image.style.left = (document.body.clientWidth/2 + document.body.scrollLeft - 50) + 'px';           
    image.setAttribute("src", "http://www.ki-work.com/Modules/KiWork/img/loading2.gif");    
    document.body.appendChild(image);
    
    //  Hidden elements: SELECTs and DIVs.className = "myVideoDisplay"
    if (document.all)
    {
		var selects = document.getElementsByTagName("SELECT");
		for(var i=0; i<selects.length; i++)
		{
			selects[i].style.visibility="hidden";
		}
		var videos = document.getElementsByTagName("DIV");
		for(var i=0; i<videos.length; i++)
		{
			if(videos[i].className == "myVideoDisplay")
				videos[i].style.visibility="hidden";
		}
    }
}

//............................................................
//  Turns on(block) or off(none) a defined element.
//............................................................
function TurnOnOff(elemId)
{
    elem = document.getElementById(elemId);
    if(elem)
        elem.style.display = (elem.style.display == "block" ? "none" : "block");    
}

//............................................................
//  Checks or unchecks all input elements of the type checkbox in a defined table.
//............................................................
function CheckElements(sender, tableId, cbName)
{
    var regExp = new RegExp(cbName);
    var table = document.getElementById(tableId);
    if(table)
    {
        var inputs = table.getElementsByTagName("INPUT");
        for(var i=0; i < inputs.length; i++)
        {
            if(regExp.test(inputs[i].id))
            {
                inputs[i].checked = sender.checked;
            }
        }
    }
}

function CheckElement(cbId, check)
{
    var cb = document.getElementById(cbId);
    if(cb != null)
        cb.checked = check;
    var i = 10;
}

//............................................................
//  Sets all blocks to the maximum height.
//............................................................
function setMaxHeight(){
	var elements = [];
	var maxHeight = 0;	
	if(arguments.length < 2)
	    return;
	//	Looks for max height among the elements.
    for(var i=1; i < arguments.length; ++i){
	    var elem = document.getElementById(arguments[i]);
	    if(elem != null){
		    elements.push(elem);
		    maxHeight = Math.max(maxHeight, elem.offsetHeight);
	    }
    }

	//  Sets max heigh.
	if(0 < maxHeight && 1 < elements.length){
		for(var i=0; i < elements.length; ++i)
			elements[i].style.height = maxHeight + "px";
	}			
	
	if(arguments[0] === false)
	    return;	
	//  Looks for elements which must be in bottom part of the parent element.    
    var blocks = [];    
    for(var iElem = 0; iElem < elements.length; ++iElem){
        var divs = elements[iElem].getElementsByTagName("DIV");
        for(var iMdlBot = 0; iMdlBot < divs.length; ++iMdlBot){
            if(divs[iMdlBot].className === "mdlBot")
                blocks.push(divs[iMdlBot]);
        }            
        if(0 < blocks.length){
            var parentLocat = Sys.UI.DomElement.getLocation(elements[iElem]);                
            var blockLocat;
            var spaceBottom = 5;
            for(var iBlock = 0; iBlock < blocks.length; ++iBlock){
                if(blocks[iBlock].style.top !== "")
                    continue;
                blockLocat = Sys.UI.DomElement.getLocation(blocks[iBlock]);
                blocks[iBlock].style.top = (maxHeight - ((blockLocat.y - parentLocat.y) + blocks[iBlock].offsetHeight + spaceBottom)) + "px";
            }
        }
    }
}

//............................................................
//  Sends asynchronous WebRequest.
//............................................................
function sendWebRequest(url, httpVerb, body, callback, userContext){
    var wRequest = new Sys.Net.WebRequest();    
    if(wRequest){        
        // Set the request URL.      
        wRequest.set_url(url);
        
        // Set the request verb.
        wRequest.set_httpVerb(httpVerb);        
        if(httpVerb === "POST"){
            // Set the body for he POST.
            wRequest.set_body(body);
            wRequest.get_headers()["Content-Length"] = body.length;
            //wRequest.get_headers()["Content-Type"] = "application/x-www-form-urlencoded";            
        }
        
        // Set the request callback function.
        wRequest.add_completed(callback);
        
        //  Set user's context information that is associated with the request.
        //  Value can be null or any primitive type or JavaScript object.
        wRequest.set_userContext(userContext);
        
        // Execute the request.
        wRequest.invoke();
    }
}
//............................................................

//............................................................
//  Creates an image and sets it in a container.
//............................................................
function setImage(imgSrc, container, isCenter){
    var elem = document.getElementById(container);
    if(elem == null)
        return;
    
    //  Clears container.
    elem.innerHTML = "";
    //  Creates image.
    if(imgSrc !== ""){
        var img = document.createElement("img");
        img.src = imgSrc;
        elem.appendChild(img);
        //  Sets the position image by center.
        if(isCenter){
            img.style.position = "relative";
            var top = ((img.offsetHeight < elem.offsetHeight) ? ((elem.offsetHeight - img.offsetHeight) / 2) : 0);
            img.style.top = top+"px";
            var left = ((img.offsetWidth < elem.offsetWidth) ? ((elem.offsetWidth - img.offsetWidth) / 2) : 0);
            img.style.left = left+"px";
        }
    }
}

//............................................................
//  Sets certain html text in a container.
//............................................................
function setHtml(html, container){
    var elem = document.getElementById(container);
    if(elem != null)
        elem.innerHTML = html;
}
