var platformMoz = (document.implementation && document.implementation.createDocument);
var platformIE6 = (!platformMoz && document.getElementById && window.ActiveXObject);
/* ************************************************************************** */

function loadGroupFromHandset(handset) {
    async_request_and_process_data ("c=handset&a=handset&handset="+handset, stsGroupFromHandset, error,true);
}

function stsGroupFromHandset(xml){
	Replace(ebId("stsGroupFromVendor"), xml);
	MakeDraggable();
}

function loadGroupFromVendor(vendor) {
    async_request_and_process_data ("c=handset&a=vendor&vendor="+vendor, stsGroupFromVendor, error,true);
}

function stsGroupFromVendor(xml){
	Replace(ebId("stsGroupFromHandset"), xml);
}


function loadHandsetsFromGroup(group) {
    async_request_and_process_data ("c=handset&a=group&group="+group, stsHandsetFromGroup, error,true);
}

function stsHandsetFromGroup(xml){
	Replace(ebId("stsHandsetFromGroup"), xml);
}

function loadCatalogues(partner, user){
	//alert(partner);
	async_request_and_process_data ("c=uma&a=getPartnerCatalogues&pid="+partner+"&uid="+user+"", fillCatalogues, error,true);
}

function fillCatalogues(xml){
//	alert(xml);
//	var zz =Transform(xml);
	//alert(zz.innerXml);
	Replace(ebId("serversScroller"),xml);
}

// *** Statistics ***
function stsLoadCatalogues(maker, seller){
	async_request_and_process_data ("c=sts&a=getCatalogues&seller=" + seller + "&maker=" + maker, stsFillCatalogues, error, true);
}

function stsFillCatalogues(xml){
//	alert(xml);
//	var zz =Transform(xml);
	//alert(zz.innerXml);
	Replace(ebId("stsCatalogues"), xml);
}

function stsLoadCarriers(country){
	async_request_and_process_data ("c=sts&a=getCarriers&country=" + country, stsFillCarriers, error, true);
}

function stsFillCarriers(xml){
//	alert(xml);
//	var zz =Transform(xml);
	//alert(zz.innerXml);
	Replace(ebId("stsCarriers"), xml);
}

function stsLoadSellers(maker){
	async_request_and_process_data ("c=sts&a=getSellers&maker=" + maker, stsFillSellers, error, true);
}

function stsFillSellers(xml){
//	alert(xml);
//	var zz =Transform(xml);
	//alert(zz.innerXml);
	Replace(ebId("stsSellers"), xml);
}

function stsLoadPayments(maker, seller){
	async_request_and_process_data ("c=sts&a=getPayments&seller=" + seller + "&maker=" + maker, stsFillPayments, error, true);
}

function stsFillPayments(xml){
//	alert(xml);
//	var zz =Transform(xml);
	//alert(zz.innerXml);
	Replace(ebId("stsPayments"), xml);
}

/* ************************************************************************** */
function finish(text){
}

function error(text){
	log("ERROR:"+text);
}

function switchVisibity(obj){
	if(obj.style.display != 'none'){
		obj.style.display = 'none';
		return true;
	}else{
		obj.style.display = 'block';
		return false;
	}
}

function ebId (id)
{
  if (document.getElementById)
  {
    return document.getElementById(id);
  }
  else if (document.all)
  {
    return document.all[id];
  }
  else if (document.layers)
  {
    return document.layers[id];
  }
  else
  {
    alert("Error:id '"+id+"' not found");
    return "";
  }
}

function appendit(text,id)
{
  ebId(id).innerHTML += text;
}

function GetXmlHttp () {
  var xmlhttp = false;
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      xmlhttp = false;
    }
  }
  if (!xmlhttp && typeof(XMLHttpRequest) != 'undefined') {
    xmlhttp = new XMLHttpRequest();
  }
  return xmlhttp;
}

//POST version
function async_post_and_process_data (query, post ,callback, failure){
  var xmlhttp = GetXmlHttp();
  var req = '?'+query+'&garbage='+Math.random();
//  alert(req);
//alert(post);
  xmlhttp.open ('POST', req, true);
  xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4) {
      if (xmlhttp.status != 200) {
        failure("Unexpected status: "+xmlhttp.status);
        callback(false);
      }
	  //if(alert)alert(xmlhttp.responseText);
      callback (xmlhttp.responseXML);
    }
  };
  xmlhttp.send (post);
}

function async_request_and_process_data (query, callback, failure){
	async_request_and_process_data (query,callback,failure,true);
}
//GET version
function async_request_and_process_data (query, callback, failure, aler) {
  var xmlhttp = GetXmlHttp();
  var req = '?'+query+'&garbage='+Math.random();
  //alert(req);
  xmlhttp.open ('GET', req, true);
  xmlhttp.setRequestHeader('Accept','text/xml');
  xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4) {
      if (xmlhttp.status != 200) {
        failure("Unexpected status: "+xmlhttp.status);
        callback(false);
      }
	  if(aler)(xmlhttp.responseText);
      callback (xmlhttp.responseText);
    }
  };
  xmlhttp.send (null);
}


	function Replace(target,source){
		if (platformMoz)
		{
			while (target.hasChildNodes())
				target.removeChild(target.childNodes[0]);
			target.innerHTML = source;
			//alert(source);
			//target.appendChild(source);
		}
		else if (platformIE6)
		{
			target.innerHTML = source;
		}
	}
