sajax={};
sajax.DEBUG=0;
var ldr=null;
sajax.document = document;
var LoadImg = new Image();
LoadImg.src = "/i/loading3.gif";

sajax.selector=function()
{
	var A=null;
	try{A=new ActiveXObject("Msxml2.XMLHTTP")}
	catch(e){try{A=new ActiveXObject("Microsoft.XMLHTTP")}
	catch(oc){A=null}}
	if(!A&&typeof XMLHttpRequest!="undefined") {A=new XMLHttpRequest()}
	return A
}

sajax.XMLCmd= function (url,data, doc)
{

 if(doc)sajax.document = doc;

        //load and show loader-pic
	//showModal(sajax.document);
//	xDoc = sajax.document.getElementById('main');
//	if(!sajax.document.getElementById('iceLoader'))
//	{
//		xDIV = sajax.document.createElement('div');
//		xDIV.innerHTML = "<img src='/i/loading3.gif' width='141' height='141' alt='Loading...'>";
//		xDIV.setAttribute("id", "iceLoader");
//		xDIV.style.display='block';
//		xDoc.appendChild(xDIV);
//	}else{
  //      	sajax.document.getElementById('iceLoader').style.display='block';
//	}
	//


 if(ldr&&ldr.readyState!=0) { ldr.abort() }
 ldr=sajax.selector();
 if(ldr)
 {
 	name.length = 0;
 	var now = new Date();
 	ldr.open("POST",url+"?t="+now.getSeconds(),true);
 	ldr.setRequestHeader("Method", "POST " + url + " HTTP/1.1");
	ldr.setRequestHeader("Content-Type", "text/plain");
	ldr.onreadystatechange=function()
	{
	 if(ldr.readyState==4 && ldr.responseXML)
	 {
		sajax.ProcessXml(ldr);
	 }
	};
	if(data.toJSONString=== undefined)
		if(data.tagName && "FORM" == data.tagName.toUpperCase()) data=sajax.getFormValues(data);
	ldr.send(data.toJSONString());
 }
 return false;
}


sajax.ProcessXml= function (request)
{
 var responseXML = request.responseXML;
 if (responseXML.documentElement && responseXML.documentElement.localName!="parsererror") 
 {
	var child = responseXML.documentElement.firstChild;
	while (child) 
	{
	 if(sajax.command[child.nodeName]) sajax.command[child.nodeName](child);
/*	 if('replace'== child.nodeName)
	 {
	   var dest=child.getElementsByTagName("id")[0].firstChild.data;
	   $(dest).innerHTML=child.getElementsByTagName("data")[0].firstChild.data;
	 }
	 else if('remove'== child.nodeName)
	 {
	   sajax.dom.remove($$(child,"id")[0].firstChild.data);
	 }*/
	 child = child.nextSibling;
	}
 }
 else
 {
  if(sajax.DEBUG) sajax.Error('no valid xml',request.responseText);
 }
 // delete loaded loader-pic
//	if(sajax.document.getElementById('iceLoader'))sajax.document.getElementById('iceLoader').style.display='none';
	//hideModal(sajax.document);
 //

}

sajax.command = [];

sajax.command['replace']= function(child) {
	   var dest=$$(child,"id")[0].firstChild.data;
	   $(dest).innerHTML=$$(child,"data")[0].firstChild.data;
}

sajax.command['add']= function(child) {
	   var dest=$$(child,"id")[0].firstChild.data;
	   $(dest).innerHTML=$(dest).innerHTML+$$(child,"data")[0].firstChild.data;
}

sajax.command['remove']= function(child) {
	sajax.dom.remove($$(child,"id")[0].firstChild.data);
}

sajax.command['insert']= function(child) {
	   var xDV = sajax.document.createElement('div');
	   xDV.innerHTML=$$(child,"data")[0].firstChild.data;
	   var dest=$$(child,"id")[0].firstChild.data;
	   $(dest).parentNode.insertBefore(xDV,$(dest));
}

sajax.command['hide']= function(child) {
	   var id=$$(child,"id")[0].firstChild.data;
	   $(id).style.display='none';
}

sajax.command['show']= function(child) {
	   var id=$$(child,"id")[0].firstChild.data;
	   $(id).style.display='block';
}

sajax.command['javascript']= function(child) {
	   eval(child.firstChild.data);
}

sajax.Error=function (name,txt)
{
 var obj =sajax.document.createElement('div');
 obj.setAttribute('id', 'sajax_error');
 obj.style.position='absolute';
 obj.style.top='10px';
 obj.style.left='10px';
 obj.style.border='1px solid red';
 obj.style.backgroundColor='#ffffff';
 obj.style.padding='10px';
 obj.style.width='700px';
 sajax.document.body.appendChild(obj);
 obj.innerHTML='<b>ERROR:'+name+'</b><textarea style="width:100%;height:400px">'+txt+'</textarea>';
}

sajax.dom={};

sajax.dom.remove = function(element) {
	if ('string' == typeof (element))
		element = $(element);
	
	if (element && element.parentNode && element.parentNode.removeChild)
		element.parentNode.removeChild(element);

	return true;
}

function $(sId)
{
	if (!sId) {
		return null;
	}
	var returnObj = sajax.document.getElementById(sId);
	if (!returnObj && sajax.document.all) {
		returnObj = sajax.document.all[sId];
	}
	if (sajax.DEBUG & !returnObj) {
		  sajax.Error('$()',"Element with the id \"" + sId + "\" not found.");
	}
	return returnObj;
}

function $$(obj,tagname)
{
	if (!tagname) {
		return null;
	}
	var returnObj = obj.getElementsByTagName(tagname);
	if (sajax.DEBUG & (!returnObj || returnObj.length==0)) {
		  sajax.Error('$$()',"Element with the tagname\"" + tagname + "\" not found.");
	}
	return returnObj;
}

//-- json
Array.prototype.toJSONString = function () {
    var a = ['['], b, i, l = this.length, v;
    for (i = 0; i < l; i += 1) {
        v = this[i];
        switch (typeof v) {
        case 'undefined':
        case 'function':
        case 'unknown':
            break;
        default:
            if (b) {
                a.push(',');
            }
            a.push(v === null ? "null" : v.toJSONString());
            b = true;
        }
    }
    a.push(']');
    return a.join('');
};

Boolean.prototype.toJSONString = function () {
    return String(this);
};

Date.prototype.toJSONString = function () {

    function f(n) {
        return n < 10 ? '0' + n : n;
    }

    return '"' + this.getFullYear() + '-' +
            f(this.getMonth() + 1) + '-' +
            f(this.getDate()) + 'T' +
            f(this.getHours()) + ':' +
            f(this.getMinutes()) + ':' +
            f(this.getSeconds()) + '"';
};

Number.prototype.toJSONString = function () {
    return isFinite(this) ? String(this) : "null";
};

Object.prototype.toJSONString = function () {
var obj=this;
if(obj.tagName && "FORM" == obj.tagName.toUpperCase()) obj=sajax.getFormValues(this);

    var a = ['{'], b, i, v;
    for (i in obj) {
        if (obj.hasOwnProperty(i)) {
            v = obj[i];
            switch (typeof v) {
            case 'undefined':
            case 'function':
            case 'unknown':
                break;
            default:
                if (b) {
                    a.push(',');
                }
		if(v.toJSONString=== undefined)
		if(v.tagName && "FORM" == v.tagName.toUpperCase()) v=sajax.getFormValues(v);

                a.push(i.toJSONString(), ':',
                        v === null ? "null" : v.toJSONString());
                b = true;
            }
        }
    }
    a.push('}');
    return a.join('');
};

String.prototype.parseJSON = function () {
    try {
        if (/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(this)) {
            return eval('(' + this + ')');
        }
    } catch (e) {
    }
    throw new SyntaxError("parseJSON");
};

(function () {
    var m = {
        '\b': '\\b',
        '\t': '\\t',
        '\n': '\\n',
        '\f': '\\f',
        '\r': '\\r',
        '"' : '\\"',
        '\\': '\\\\'
    };

    String.prototype.toJSONString = function () {
        if (/["\\\x00-\x1f]/.test(this)) {
            return '"' + this.replace(/([\x00-\x1f\\"])/g, function(a, b) {
                var c = m[b];
                if (c) {
                    return c;
                }
                c = b.charCodeAt();
                return '\\u00' +
                    Math.floor(c / 16).toString(16) +
                    (c % 16).toString(16);
            }) + '"';
        }
        return '"' + this + '"'; //escape()
    };

})();

// -- getForm
sajax.getFormValues = function(element) {
	var submitDisabledElements = false;
	if (arguments.length > 1 && arguments[1] == true)
		submitDisabledElements = true;
	
	var prefix="";
	if(arguments.length > 2)
		prefix = arguments[2];
	
	if ("string" == typeof(element))
		element = $(element);
	
	var aXml = new Object;
	if (element && element.tagName && "FORM" == element.tagName.toUpperCase()) {
		var formElements = element.elements;
		for (var i = 0; i < formElements.length; ++i) {
			var child = formElements[i];
			if (!child.name)
				continue;
			if (prefix != child.name.substring(0, prefix.length))
				continue;
			if (child.type && (child.type == 'radio' || child.type == 'checkbox') && child.checked == false)
				continue;
			if (child.disabled && true == child.disabled && false == submitDisabledElements)
				continue;
			var name = child.name;
			if (name) {
				if('select-multiple' == child.type) {
					if (name.substr(name.length-2, 2) != '[]') { name += '[]'; aXml[name]= new Array;}
					for (var j = 0; j < child.length; ++j) {
						var option = child.options[j];
						if (true == option.selected) {
							aXml[name].push(option.value);
						}
					}
				} 
				else if(name.substr(name.length-2, 2) == '[]') {
				if(!aXml[name]) aXml[name]= new Array;
				aXml[name].push(child.value);
				}
				else {

					aXml[name]=child.value;
				}
			} 
		}
	}
	return aXml; 
}