var ua = navigator.userAgent;
var isIE = (ua.indexOf('MSIE') >= 0);
var isIE6 = navigator.appVersion.match(/MSIE 6.0/);
var isWebKit = /WebKit/.test(ua);
var isOpera = typeof window.opera !== "undefined";

/*var load_div;*/



function callServer(url, data, callback, customData, responseType) {
  return _callServer('GET', url, data, callback, customData, responseType);
}

function callServerPost(url, data, callback, customData, responseType) {
  return _callServer('POST', url, data, callback, customData, responseType);
}


function _callServer(method, url, data, callback, customData, responseType) {
  var xmlReq = new XMLHttpRequest;
  if (xmlReq) {
    xmlReq.open(method, url, true);
    xmlReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    xmlReq.onreadystatechange = function() {
      // only if xmlReq shows "loaded"
//      if ((xmlReq.readyState == 4) && (xmlReq.status == 200 || xmlReq.status == 201)) {
	if (this.readyState == XMLHttpRequest.DONE) {
		if (responseType == 'text') {
			callback(xmlReq.responseText, customData);
		} else {
			callback(xmlReq.responseXML, customData);
		}
      }
    }

    xmlReq.send(data);

    return false;
  }
  return true;
}



function callSrv(url, data, callback, customData, loading, responseType) {
  if (loading) loading();

  var re = /sid=(\w+)/;
  var sid = re.exec(document.URL);  

  if (sid) {
	url = url.indexOf('?') > -1 ? url + '&' : url + '?';
	url = url + 'sid=' + sid[1];	
  }

  callServer(url, data, callback, customData, responseType);
}

function callSrvPost(url, data, callback, customData, loading, responseType) {
  if (loading) loading();

  var re = /sid=(\w+)/;
  var sid = re.exec(document.URL);  

  if (sid) {
	if (data) data = data + '&';
	data = data + 'sid=' + sid;
  }

  callServerPost(url, data, callback, customData, responseType);
}


function def_loading(parent) {
/*  var parent;
  if (p) {
	parent = p
  } else {
	if (load_div) parent = document.getElementById(load_div)
  }*/

  if (parent) {
	var msg = document.createElement('div');
	msg.id = 'progress';
	msg.innerHTML = '<p>Загрузка, подождите...</p>';

	var top = parent.offsetTop + 50;
	var left = parent.offsetLeft + 125;

	msg.style.top = top + 'px';
	msg.style.left = left + 'px';

	parent.insertBefore(msg, parent.firstChild);
  }
}




function insertScript(script, targetNode){
  try {
	var el = document.createElement('script');
	el.type="text/javascript";
	/*el.innerHTML = "<!--//--><![CDATA[\n"+script+"\n//]]>";*/
	el.innerHTML = script;
	targetNode.appendChild(el);
  } catch (e) {
	var el = document.createElement('span');
	targetNode.appendChild(el);
	el.innerHTML = "<br /><scr"+"ipt type='text/javascript' defer='defer'>"+script+"</script" + ">";
  }
}



function xsltProcessValid(xml, xslt) {
  var content =  xsltProcessDoc(xml, xslt);

  if (content && typeof(content) != "object") {
	content = content.replace(/&amp;/g, "&");
	content = content.replace(/value=\"-1\"/g, "value=\"\"");
	content = content.replace(/<(div|textarea)([^<]+?)\/>/g, "<$1 $2></$1>");
  }

  return content;
}



function processFormSetScript(xmlObj, xslt, destination, category) {
  var content = xsltProcessValid(xmlObj, xslt);

  var js_target = document.getElementById('scripts');
  if (js_target) js_target.innerHTML = '';

  if (isWebKit) content = content.xml; /* иначе если через DOM - очень много геморроя */

  if (typeof(content) != 'object') {
	var re_script = /<script[\"\w\s\=\/\':\.]*>([\W\w]+?)<\/script>/g;
	var sc;
	var scripts = [];
	while (sc = re_script.exec(content)) {
		scripts.push(sc[1]);
	}

	content = content.replace(re_script, '');

	var newEl = destination.cloneNode(false);
	newEl.innerHTML = content;
	destination.parentNode.replaceChild(newEl, destination);

	for (l=0;l<scripts.length;l++) {
		insertScript(scripts[l], js_target);
	}
  } else {

	destination.innerHTML = '';

	while (content.firstChild) {
		var element = content.firstChild;
		destination.appendChild(element);
	}

	if (isOpera) recurseScript(destination);

  }

  if (category && document.getElementById('iform')) document.getElementById('iform').cat.value = category;
}

function recurseScript(parent) {
	var el = parent.firstChild;
	while (el) {
		if (el.nodeName.toUpperCase() == 'SCRIPT') {
			var js_target = document.getElementById('scripts');
			var s = el.innerHTML;
			el.parentNode.removeChild(el);
			insertScript(s, js_target);
		} else {
			recurseScript(el);
		}
		el = el.nextSibling;
	}
}


function formParams(form) {
  var params = '';
  for (var i=0;i<form.elements.length;i++) {
	if ((form.elements[i].type!='radio' || form.elements[i].checked) && form.elements[i].name) {
		if (params) params = params + '&';
		params = params + form.elements[i].name + '=' + encodeURIComponent(form.elements[i].value);
	}
  }
  return params;
}




function _xmlGetOneChild(parent, name) {
  var result = parent.getElementsByTagName(name);
  if (result) {
	return result.item(0);
  }
}

function _xmlGetOneChildValue(parent, name) {
  var result = _xmlGetOneChild(parent, name);
  if (result) {
	var valueNode = result.firstChild;
	if (valueNode) return valueNode.nodeValue;
  }
}

function eraseDOMChildren(parent) {
  if (parent) {
	while (parent.hasChildNodes()) {
		parent.removeChild(parent.firstChild);
	}
  }
}

function addAttrValue(parent, name, value) {
  var attr = document.createAttribute(name);
  attr.nodeValue = value;
  parent.setAttributeNode(attr);
}


function submitForm(sender) {
  if (sender) {
	var result = sender.onsubmit ? sender.onsubmit() : true;
	if (result) sender.submit();
  }
}



function xmlValue(node) {
  if (!node) {
	return '';

  }

  var ret = '';
  if (node.nodeType == 3 ||
 node.nodeType == 4) {
	ret += node.nodeValue;


  } else if (node.nodeType == 2) {

	if (isIE6) {
		ret += xmlValueIE6Hack(node);
	} else {
		ret += node.nodeValue;
	}
  } else if (node.nodeType == 1 || node.nodeType == 9 ||
 node.nodeType == 11) {

	for (var i = 0; i < node.childNodes.length; ++i) {

		ret += arguments.callee(node.childNodes[i]);

	}

  }
  return ret;

}

function xmlValueIE6Hack(node) {

  var nodeName = node.nodeName;

  var nodeValue = node.nodeValue;

  if (nodeName.length != 4) return nodeValue;
  if (!/^href$/i.test(nodeName)) return nodeValue;
  if (!/^javascript:/.test(nodeValue)) return nodeValue;

  return unescape(nodeValue);

} 