/*
$Id: functions_xml.js,v 1.1 2008/08/12 09:28:01 petr Exp $
*/

var getUrl;
var contentTarget = '';
var objTarget = null;

// XMLHttpRequest
function getHTTPObject() {
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	@else
	xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

function insertContent(result) {
	if (contentTarget=='') {
		return;
	}
	objTarget = new getObj(contentTarget);
	objTarget.innerHTML = result;
	contentTarget = '';
}

function insertClassName(result) {
	if (contentTarget=='') {
		return;
	}
	objTarget = new getObj(contentTarget);
	objTarget.className = result;
	contentTarget = '';
}

function getContent(getUrl, target) {
	contentTarget = target;
	receiveContent(getUrl);
}

/*
var contentData = null;

function getContentPost(getUrl, target, str) {
	contentTarget = target;
	contentData = str;
	receiveContent(getUrl);
}
*/

function receiveContent( getUrl, class_id ) {
	if (httpGetXML.readyState == 4 || httpGetXML.readyState == 0) {
		httpGetXML.open("GET",getUrl + '&rand='+Math.floor(Math.random() * 1000000), true);
		// httpGetXML.open("POST",getUrl + '&rand='+Math.floor(Math.random() * 1000000), true);
		// httpGetXML.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		if (class_id==true) {
			httpGetXML.onreadystatechange = getClassReceive;
			document.body.style.pointer='watch';
		} else {
			httpGetXML.onreadystatechange = getContentReceive;
			document.body.style.pointer='watch';
		}
		// httpGetXML.send('html='+encodeURIComponent(contentData));
		httpGetXML.send(null);
	}
}

// callback
function getClassReceive() {
	if (httpGetXML.readyState == 4) {
		results = httpGetXML.responseText;
		insertClassName(results);
	}
}

function appendXML( xml ) {
	//Get the reference of the DIV in the HTML DOM by passing the ID
	var obj = document.getElementById(contentTarget);
	//Check if the TextNode already exist
	obj.replaceChild(xml.childNodes[0], obj.childNodes[0]);
	return;

	if(obj.childNodes[0]) {
		//If yes then replace the existing node with the new one
		obj.replaceChild(xml.childNodes[0], obj.childNodes[0]);
	} else {
		//If not then append the new Text node
		obj.appendChild(xml.childNodes[0]);
	}
}

// callback
function getContentReceive() {
	if (httpGetXML.readyState == 4) {
		results = httpGetXML.responseText;
		insertContent(results);
	}
}

var httpGetXML = getHTTPObject();


function createMethodReference(object, methodName) {
	return function () {
		object[methodName]();
	};
};

function requestParser() {

	this.unsetCurrent = function() {
		if (this.s_item!=null) {
			this.s_item.className = '';
		}
	}

	this.setCurrent = function( obj ) {
		this.unsetCurrent();
		if (obj!='undefined' && obj!=null) {
			this.s_item = obj;
			this.s_item.className = 'selected';
			return true;
		}
		this.s_item = null;
		return false;
	}

	this.cloneAttributes = function( nodes, target ) {
		for(var i=0; i<nodes.length; i++) {
			target.setAttribute(nodes[i].nodeName, nodes[i].nodeValue);
		}
	}

	this.parseData = function ( node, source ) {
		if (source==null) {
			return;
		}
		if (source.attributes) {
			this.cloneAttributes(source.attributes, node);
		}
		for(var m = source.firstChild; m != null; m = m.nextSibling) {
			var nodeName = m.nodeName;
			switch (m.nodeType) {
			case 1:
				var elm = document.createElement(nodeName.toUpperCase());
				this.parseData( elm, m );
				break;
			case 8:
				// alert(m.nodeValue);
				break;
			default:
				if (m.nodeValue.length) {
					var elm = document.createTextNode(m.nodeValue);
				}
			}
			node.appendChild(elm);
		}
		return;
	}

	this.insertData = function() {
		this.unsetCurrent();
		this.s_base = this.s_item.cloneNode(false);
		this.s_base.removeAttribute("ondblclick");
		this.parseData(
			this.s_base,
			this.xmlr.firstChild
			);
		this.s_item.parentNode.replaceChild(this.s_base, this.s_item);
		this.setCurrent(this.s_base);
	}

	this.removeData = function (obj) {
		// this.unsetCurrent();
		obj.parentNode.removeChild(obj);
	}

	this.duplicateData = function () {
		if (this.s_item==null) {
			alert("Není co duplikovat. Zvolte poloľku.");
			return;
		}s
		this.s_base = this.s_item.cloneNode(true);
		this.s_base.setAttribute("id", "NEWITEM_"+Math.floor(Math.random() * 1000000));
		var target = this.s_item.nextSibling;
		if (target==null) {
			target = this.s_item;
		}
		this.s_item.parentNode.insertBefore(this.s_base, target);
		this.setCurrent(this.s_base);
	}

	this.addData = function () {
		if (this.s_item==null) {
			// this.unsetCurrent();
			this.s_item = getObj('list_first_row');
		}
		this.s_base = this.s_item.cloneNode(false);
		this.s_base.removeAttribute("ondblclick");
		this.parseData(
			this.s_base,
			this.xmlr.firstChild
			);
		if (this.s_item.nextSibling==null) {
			this.s_item.parentNode.appendChild(this.s_base);
		} else {
			this.s_item.parentNode.insertBefore(this.s_base, this.s_item.nextSibling);
		}
		this.setCurrent(this.s_base);

		// disable sort
		// getObj('dt_up').disabled = true;
		// getObj('dt_down').disabled = true;
	}

	this.upCurrent = function(element) {

		var node = this.s_item.parentNode;
		// tabulka ma radek s nadpisy
		if (this.s_item.rowIndex < 2) {
			return;
		}
		var  temp = null;
		if (this.s_item!==null) {
			temp = this.s_item.previousSibling;
			do {
				if (temp==null)
					break;
				if (temp.nodeName==element)
					break;
				temp = temp.previousSibling;
			} while(true);
			if (temp!=null) {
				buffnode_a = this.s_item.cloneNode(true);
				buffnode_b = temp.cloneNode(true);
				node.replaceChild(buffnode_a, temp);
				node.replaceChild(buffnode_b, this.s_item);
				this.setCurrent(buffnode_a);
			}
		}
	}

	this.downCurrent = function(element) {

		var node = this.s_item.parentNode;
		if (this.s_item!==null) {
			var temp = this.s_item.nextSibling;
			do {
				if (temp==null)
					break;
				if (temp.nodeName==element)
					break;
				temp = temp.nextSibling;
			} while(true);
			if (temp!=null) {
				buffnode_a = this.s_item.cloneNode(true);
				buffnode_b = temp.cloneNode(true);
				node.replaceChild(buffnode_a, temp);
				node.replaceChild(buffnode_b, this.s_item);
				this.setCurrent(buffnode_a);
			}
		}

	}

	this.xmlRequest = function () {
		this.xml.abort();
		if (this.xml.readyState == 0) {
			this.xml.open("GET", this.getUrl + '&rand='+Math.floor(Math.random() * 1000000), true);
			this.xml.onreadystatechange = this.receiveXMLcall;
			this.xml.send(null);
		} else {
			alert(this.xml.readyState);
		}
	}

	this.receiveXML = function () {
		if (this.xml.readyState == 4 && this.xml.responseText) {
			this.xmlr = null;
			if (this.xml.responseXML==null) {
				alert(this.xml.responseText);
				return false;
			}
			this.xmlr = this.xml.responseXML;
			if (this.debug) {
				alert(this.xml.responseText);
				return true;
			}
			switch (this.what) {
			case 'edit':
			case 'load':
				this.insertData();
				break;
			case 'duplicate':
			case 'add':
				this.addData();
				break;
			default:
				alert('Neznama akce: ' + this.what);
			}
		}
		return false;
	}

	this.xml = getHTTPObject();
	this.what = '';
	this.debug = false;
	this.getUrl = '';
	this.s_item = null;
	this.s_base = null;
	this.id = '';
	this.invd_price = '';
	this.invd_name = document.createTextNode("");
	this.invd_units = '';
	this.invd_vat_per = '';
	this.receiveXMLcall = createMethodReference(this, 'receiveXML');

};

var modalDialog = new requestParser();

modalDialog.get = function ( url ) {
	this.setCurrent(getObj('modaldialog'));
	this.s_item.style.display = 'block';
	this.what='load';
	this.getUrl = url;
	this.xmlRequest();
}

modalDialog.close = function ( ) {
	this.setCurrent(getObj('modaldialog'));
	this.s_item.style.display = 'none';
}