﻿function doInnerHTML(elementId, stringHTML) {
	
	document.getElementById(elementId).innerHTML = stringHTML;
	
	/*
	
    try {
        var elem = document.getElementById(elementId);
        var children = elem.childNodes;

        for (var i = 0; i < children.length; i++) {
            elem.removeChild(children[i]);
        }
//tipa jabut pareizi
        var nodes = new DOMParser().parseFromString("<div xmlns=\"http://www.w3.org/1999/xhtml\">" + stringHTML + "</div>", 'application/xml').documentElement;
        var range = document.createRange();
        range.selectNodeContents(document.getElementById(elementId));
        range.deleteContents();

        for (var i = 0; i < nodes.childNodes.length; i++) {
            if (document.all)
            document.getElementById(elementId).appendChild(nodes.childNodes[i]);
            else
            document.getElementById(elementId).appendChild(document.importNode(nodes.childNodes[i], true));
        }
        return true;
    }
    catch (e) {
        try {
            document.getElementById(elementId).innerHTML = stringHTML;
            return true;
        }
        catch(ee) {
            return false;
        }
    }
	*/
}


var HTML_request = null; 
if(window.XMLHttpRequest)
	HTML_request = new XMLHttpRequest(); 
else if (window.ActiveXObject)
	HTML_request  = new ActiveXObject(Microsoft.XMLHTTP); 
			
if (!HTML_request) {
	alert('Giving up :( Cannot create an XMLHTTP instance');
	// return false;
}


			
	
			
			
function getPAGE(vairablji,objekts,links)	{ 
			var HTML_request = null; 
			if(window.XMLHttpRequest)
				HTML_request = new XMLHttpRequest(); 
			else if (window.ActiveXObject)
				HTML_request  = new ActiveXObject(Microsoft.XMLHTTP); 
						
			if (!HTML_request) {
				alert('Giving up :( Cannot create an XMLHTTP instance');
				// return false;
			}
		
		if(document.getElementById(objekts+'_loading')){document.getElementById(objekts+'_loading').style.visibility = "visible";}

		if(vairablji){
			links += vairablji;
		} 
		
		links += "&rand=" + Math.random();
	
		
		HTML_request.onreadystatechange = function()	{ 
		
			
			if(HTML_request.readyState == 4) {
				if(HTML_request.status == 200){
					if(document.getElementById(objekts+'_loading')){document.getElementById(objekts+'_loading').style.visibility = "hidden";}
					
					
				$(document).ready(function(){
					if(document.getElementById(objekts)){ 
						doInnerHTML(objekts,HTML_request.responseText);
						execJS(objekts);
					}
				});
					
					//document.getElementById('JS_BULLY').innerHTML = 'alert("ass")';
					
				
					
				} else {
					//	return HTML_request.status + " " + HTML_request.statusText;
					//if(document.getElementById(objekts)){document.getElementById(objekts).innerHTML = HTML_request.status + " " + HTML_request.statusText;}
				}	
			} 
		}; 
		
		
		
		HTML_request.open("GET", links, true); 
		HTML_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; Charset=utf-8");
		HTML_request.send(null);
	//	HTML_request.send("text=" +document.getElementById('dyn').value);
		//return "done";

} 
	
		





//stop ajax
function sajax() {
    if (callInProgress()) {
        HTML_request.abort();
    }
}
//check if a call is in progress
function callInProgress() {
	
    switch ( HTML_request.readyState ) {
        case 1, 2, 3:
            return true;
        break;

        default:
            return false;
        break;
    }
}