/*
 * Copyright 2004, Consorzio di Bioingegneria e Informatica Medica.
 *
 * CVS $Id: xmlhttp.js 561 2006-03-17 09:54:39Z daniele $
 */

var xmlhttp;
var ieBrowser = false;
var supportsNamespaces = (document.getElementsByTagNameNS != null);
var NS = "http://siv-ars.it/sita/1.0";
var PREFIX = "sita";

if (window.XMLHttpRequest) {
  xmlhttp = new XMLHttpRequest();
// branch for IE/Windows ActiveX version
} else if (window.ActiveXObject) {
  ieBrowser = true;
  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

function doXmlHttpRequest(url, func) {
    if (xmlhttp.readyState != 0 && xmlhttp.readyState != 4) {
        // Another request is already running
        return;
    }
    xmlhttp.open("GET", url, true);
    xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
            document.body.style.cursor = "default";
			func.apply(this, [xmlhttp]);
		}
    }
    document.body.style.cursor = "wait";
    xmlhttp.send(null);
}

function doXmlHttpRequestPost(url, func, data, idProdotto) {
    if (xmlhttp.readyState != 0 && xmlhttp.readyState != 4) {
        // Another request is already running
        return;
    }
	xmlhttp.open('POST', url, true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
            document.body.style.cursor = "default";
			func.apply(this, [xmlhttp]);
		}
    }
    var markup;
    if (ieBrowser) {
    	markup = encodeURIComponent(data.xml);
    } else {
		var xmlSerializer = new XMLSerializer();
	  	markup = encodeURIComponent(xmlSerializer.serializeToString(data));
  	}
    xmlhttp.send('xmlDoc=' + markup + '&id=' + idProdotto);
}

function doSyncXmlHttpRequest(url, func) {
    document.body.style.cursor = "wait";
    if (xmlhttp.readyState != 0 && xmlhttp.readyState != 4) {
        // Another request is already running
        return;
    }
    xmlhttp.open("GET", url, false);
    xmlhttp.send(null);
	if (xmlhttp.readyState == 4) {
        document.body.style.cursor = "default";
		func.apply(this, [xmlhttp]);
	}
}

function copyDomTree(fromNode, toNode) {
    var doc = toNode.ownerDocument;
    switch (fromNode.nodeType) {
        case 1: // Element
            var newNode = doc.createElement(fromNode.nodeName);
            var attributes = fromNode.attributes;
            for (var i = 0 ; i < attributes.length ; ++i) {
                var attrName = attributes.item(i).nodeName;
                var attr = doc.createAttribute(attrName);
                attr.nodeValue = attributes.item(i).nodeValue;
            	newNode.setAttributeNode(attr);
            }
            toNode.appendChild(newNode);
            var children = fromNode.childNodes;
            for (var i = 0 ; i < children.length ; ++i) {
                copyDomTree(children.item(i), newNode);
            }
            break;
        case 3: // Text
            var newNode = doc.createTextNode(fromNode.nodeValue);
            toNode.appendChild(newNode);
            break;
        default:
            //alert("Nodo sconosciuto" + fromNode);
            break;
    }
}


function swapDomTree(fromNode, toNode) {
	var parent = toNode.parentNode;
	parent.removeChild(toNode);
	copyDomTree(fromNode, parent);
}

//
//addWaitOption: aggiunge ad un dato elemento SELECT una option di "attesa" caricamento dati
//
function addWaitOption(select, text) {
    document.body.style.cursor = "wait";
    if (select != null) {
	 	var waitOption = document.createElement("option");
		waitOption.value = "";
		waitOption.text = text;
		select.options.add(waitOption, 0);
    }
}

//
//removeWaitOption: rimuove da un dato elemento SELECT una option di "attesa" caricamento dati
//
function removeWaitOption(select, opts) {
    document.body.style.cursor = "default";
	if (select != null) {
		select.removeChild(opts.item(0));
    }
}

//
//addWaitMessage: aggiunge ad un dato elemento uno SPAN contenente un messaggio di attesa
//
function addWaitMessage(elem, message) {
    document.body.style.cursor = "wait";
	var span = document.createElement("span");
	span.id = WAIT_MESSAGE_ID;
	span.appendChild(document.createTextNode(message));
	elem.appendChild(span);
    document.body.style.cursor = "wait";
}

//
//removeWaitMessage: rimuove da un dato elemento lo SPAN [waitMessage]
//
function removeWaitMessage(elem) {
    document.body.style.cursor = "default";
	var span = document.getElementById(WAIT_MESSAGE_ID);
	if (span != null) {
		elem.removeChild(span);
	}
}

//
//getElementsByTagName: ritorna un elemento con un namespace
//
function getElementsByTagName(node, local) {
    if (supportsNamespaces) {
        return node.getElementsByTagNameNS(NS, local);
    }
    else {
        return node.getElementsByTagName(PREFIX + ":" + local);
    }
}

function getDisplayProperty() {
    if(ieBrowser) {
        return "block";
    }
    else {
        return "table-cell";
    }
}

function initHtmlArea(elementId) {
	// inititalize editor
	editor = new HTMLArea(elementId);
    // retrive the config object
    var config = editor.config;
    // do custom configuration
    config.toolbar = [
		[
	  	  "bold", "italic", "underline","separator",
	  	  "undo", "redo", "paste", "separator",
	      "justifyleft", "justifycenter", "justifyright", "separator",
	      "insertorderedlist", "insertunorderedlist", "outdent",
          "indent","createsitalink"
        ]
    ];
	HTMLArea.replace(elementId, config);
}
