
function getAjaxConnector(){
    return "http://localhost/Pea/ajaxserv.php";
}

function createXmlHttp(xml, onStateChange){
    
    //Letesztelj�k, hogy ie-t haszn�lunk-e.
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch (e){
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch (E){
            xmlhttp = false;
        }
    }
    //�s ha nem ie, akkor nem kell j�tszani az activex-es vacakokkal :-).
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        xmlhttp = new XMLHttpRequest();
    }
    
    xmlhttp.open("POST", getAjaxConnector(), true);
    xmlhttp.setRequestHeader("Content-type", "text/xml");
    xmlhttp.setRequestHeader("Content-length", xml.length);
    xmlhttp.setRequestHeader("Connection", "close");

    xmlhttp.onreadystatechange = onStateChange;
    
    xmlhttp.send(xml);
    
    return xmlhttp;
}

function changeElement(elementId, content){
	var element = document.getElementById(elementId);
	element.innerHTML = content;
}

function appendElement(elementId, content){
	var element = document.getElementById(elementId);
	element.innerHTML = element.innerHTML + content;
}

function refreshElement(elementId, bindedTo, content){
	var element = document.getElementById(bindedTo);
	var old = document.getElementById(elementId);
	var n_ew = document.createElement('div'); //nem sz�zas hogy m�xik el�g bunda :-)
	n_ew.innerHTML = content;
	var neww = n_ew.firstChild; 
	element.replaceChild(neww, old);
}

function executeXml(xml){
	var xmlDoc;
	try{
  		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  		xmlDoc.async="false";
  		xmlDoc.loadXML(xml);
  	}catch(e){
  		try{
    		parser=new DOMParser();
    		xmlDoc=parser.parseFromString(xml, "text/xml");
    	}catch(e) {alert(e.message)}
  	}
  	
  	try{
  		if (xmlDoc.childNodes[0].nodeName == "exception"){
  			alert(xmlDoc.getElementsByTagName("message")[0].childNodes[0].nodeValue);
  		}else{ 
  			var events = xmlDoc.getElementsByTagName("event");
  	
  			for (var i = 0; i < events.length; i++){
  				var sendto = events[i].getElementsByTagName("sendto")[0].childNodes[0].nodeValue;
  				var content = events[i].getElementsByTagName("content")[0].childNodes[0].nodeValue;
  				var type = events[i].getElementsByTagName("type")[0].childNodes[0].nodeValue;
  				
  				if (type == "change"){
  					changeElement(sendto, content);
  				}else if (type == "append"){
  					appendElement(sendto, content);
  				}else if (type == "refresh"){
  					var eid = events[i].getElementsByTagName("eid")[0].childNodes[0].nodeValue;
  					refreshElement(eid, sendto, content);
  				}
  			}
  		}
  	}catch (e){
  		alert("Feldolgozhatatlan �zenet.");
  	}
}

function peaMethodCall(className, methodName, params){
	var xml = "<call><class>"+className+"</class><name>"+methodName+"</name>";
	
	if (params instanceof Array){
		xml += "<params>";
		for (var i = 0; i < params.length; i++){
			xml += "<p>"+params[i]+"</p>";
		}
		xml += "</params>";
	}
	
	xml += "</call>";
	
	xmlhttp = createXmlHttp(xml, function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
                executeXml(xmlhttp.responseText);
        }
    });
}

function sendAjaxEvent(type, sendedFrom, bindedTo, lock){
	var xml = "<event>";
	xml += "<type>"+type+"</type>";
	xml += "<from>"+sendedFrom+"</from>";
	xml += "<bindedto>"+bindedTo+"</bindedto>";
	xml += "<lock>"+lock+"</lock>";
	
	if (type == "AjaxOnFormSend"){
		xml += "<formdata>";
		var form = document.getElementById(sendedFrom);
		for (var i = 0; i < form.childNodes.length; i++){
			var element = form.childNodes[i];
			if (element.nodeName == "INPUT" && element.name != null){
				xml += "<e>";
				xml += "<k>"+element.name+"</k>";
				xml += "<v>"+element.value+"</v>";
				xml +="</e>";
			}
		}
		xml += "</formdata>";
	}
	
	xml += "</event>";
	
	xmlhttp = createXmlHttp(xml, function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
                executeXml(xmlhttp.responseText);
        }
    });
}

function sendAjaxEventWithOthers(type, sendedFrom, bindedTo, lock, others){ // valosznuleg nem kell majd, de meg nem tudom
	var xml = "<event>";
	xml += "<type>"+type+"</type>";
	xml += "<from>"+sendedFrom+"</from>";
	xml += "<bindedto>"+bindedTo+"</bindedto>";
	xml += "<lock>"+lock+"</lock>";
	xml += others;
	xml += "</event>";
	
	xmlhttp = createXmlHttp(xml, function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
                executeXml(xmlhttp.responseText);
        }
    });
}


