function AjaxObject()
{
  var ro = false;

  if (window.XMLHttpRequest) {  ro = new XMLHttpRequest(); }
  else {
    ro = new ActiveXObject('Msxml2.XMLHTTP');
    if(!ro) { ro = new ActiveXObject('Microsoft.XMLHTTP'); }
  }

  return ro;
}

var nAjaxWidth=250;
var nAjaxHeight=200;
var nAjaxXOffset=0;
var nAjaxYOffset=0;

var gotolink = '';

function AjaxSendRequest(action,vars,sObject,newWidth,newHeight,newXOffset,newYOffset) {

	if ((sObject == 'ajax1_1') || (sObject == 'ajax1')) {
		obj=SfindObj(sObject);
		if (parseInt(obj.style.left) > -4000)
			return;
	}
	
	http = AjaxObject();
	
	if (!sObject)
		sObject = '';
		
	if (vars)
		vars = vars + '&object=' + sObject;
	else
		vars = 'object=' + sObject;
	
	http.open('POST', action, true);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", vars.length);
	
	http.onreadystatechange = function(){handleResponse(http);};
    
    if (!newWidth)
    	newWidth = 347;
    if (!newHeight)
    	newHeight = 210;
    if (!newXOffset)
    	newXOffset = 0;
    if (!newYOffset)
    	newYOffset = 0;

    nAjaxWidth=newWidth;
	nAjaxHeight=newHeight;
	
	nAjaxXOffset=newXOffset;
	nAjaxYOffset=newYOffset;

	http.send(vars);
    
	obj=SfindObj('ajloading');
	obj.style.visibility='visible';
	obj.style.left=document.body.clientWidth-63+'px';
	obj.style.top=document.body.scrollTop+document.body.clientHeight-63+'px';
}


function AjaxSendPOST(action,form,sObject,newWidth,newHeight,newXOffset, newYOffset) {
	
	http = AjaxObject();
	
	if (!sObject)
		sObject = '';
	
	vars = '';
	for (i = 0; i < window.document.forms[form].elements.length; i++) 
	{
		elem = window.document.forms[form].elements[i];
		if((elem.type == 'checkbox') && elem.checked != true) continue;
		vars = vars + '&' + elem.name + '=' + encodeURI(elem.value);
	}
	
	if (vars)
		vars = vars + '&object=' + sObject;
	else
		vars = 'object=' + sObject;

	http.open('POST', action, true);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", vars.length);
    
    http.onreadystatechange = function(){handleResponse(http);};
    	
    if (!newWidth)
    	newWidth = 347;
    if (!newHeight)
    	newHeight = 210;
    if (!newXOffset)
    	newXOffset = 0;
    if (!newYOffset)
    	newYOffset = 0;

    nAjaxWidth=newWidth;
	nAjaxHeight=newHeight;
	
	nAjaxXOffset=newXOffset;
	nAjaxYOffset=newYOffset;
    	
    http.send(vars);
    
    if (sObject == 'ajax') {
	/*objajax=SfindObj('ajax');
    	objajax.style.visibility = 'hidden';
	objajax.innerHTML = '';*/
	HideClear('ajax');
    }

	obj=SfindObj('ajloading');
	obj.style.visibility='visible';
	obj.style.left=document.body.clientWidth-63+'px';
	obj.style.top=document.body.scrollTop+document.body.clientHeight-63+'px';
    
}


function handleResponse(http) {
	 //try {
	    if (http.readyState == 4) {
    		//alert(http.responseText);
			var xmldoc = http.responseXML;
			
			if (xmldoc)
				var root = xmldoc.getElementsByTagName('root')[0];
			
	        if (root) {
	        	/*xml response*/
				//var root = xmldoc.getElementsByTagName('root')[0];
				if (root) {
					for (var iNode = 0; iNode < root.childNodes.length; iNode++) {
						if (root.childNodes[iNode].nodeType == 1) {
							
							if (root.childNodes[iNode].tagName == 'ajax2_1') {
								obj=SfindObj('autosearch_title');if (obj) obj.style.visibility = 'hidden';
								obj=SfindObj('autosearch_manufacturer');if (obj) obj.style.visibility = 'hidden';
								obj=SfindObj('autosearch_model');if (obj) obj.style.visibility = 'hidden';
								obj=SfindObj('autosearch_fulltitle');if (obj) obj.style.visibility = 'hidden';
							}

							if (root.childNodes[iNode].tagName == 'goto') 
							{
//								document.URL = root.childNodes[iNode].firstChild.nodeValue;
								window.location.href = root.childNodes[iNode].firstChild.nodeValue;
//								document.location.href = root.childNodes[iNode].firstChild.nodeValue;
								//window.location.reload();

							}

							obj=SfindObj(root.childNodes[iNode].tagName);
							if (obj)
								obj.innerHTML = root.childNodes[iNode].firstChild.nodeValue;
							
						
							if (root.childNodes[iNode].tagName.indexOf('ajax') != -1) {
			           			/*obj.style.width = nAjaxWidth;*/
			           			/*obj.style.height = nAjaxHeight;*/
			           			CenterLayer (root.childNodes[iNode].tagName,nAjaxWidth,nAjaxHeight, 0, nAjaxXOffset, nAjaxYOffset);
							}
			           			
							if (root.childNodes[iNode].tagName != 'zametkadiv' && obj)
								obj.style.visibility = 'visible';
								
			           			if ((root.childNodes[iNode].firstChild.nodeValue.indexOf('<div class=ajaxi></div>') != -1) || (root.childNodes[iNode].firstChild.nodeValue.indexOf('<div class=ajaxi><!--') != -1))
									HideClear(root.childNodes[iNode].tagName);
								
							
							if (root.childNodes[iNode].firstChild.nodeValue.indexOf('<!-- fulltitle -->') != -1)
								sDisableSearch = 'fulltitle';

							if (root.childNodes[iNode].firstChild.nodeValue.indexOf('<!-- title -->') != -1)
								sDisableSearch = 'title';

							if (root.childNodes[iNode].firstChild.nodeValue.indexOf('<!-- country -->') != -1)
								sDisableSearch = 'country';

							if (root.childNodes[iNode].firstChild.nodeValue.indexOf('<!-- region -->') != -1)
								sDisableSearch = 'region';

							if (root.childNodes[iNode].firstChild.nodeValue.indexOf('<!-- town -->') != -1)
								sDisableSearch = 'town';
								
							if ((root.childNodes[iNode].tagName == 'loginbar') && gotolink)
						        	document.location.href = gotolink;
							
							CheckScrollers ();
							
							if (root.childNodes[iNode].tagName == 'zametkadiv')
								AjaxSendRequest('/blognotessearch?mode=ajax&id='+ window.document.forms['formzametka'].id.value +'&gid='+ window.document.forms['formzametka'].gid.value +'', '', null, 'ajax2_1', 250, 200, 350);
							if (root.childNodes[iNode].tagName.indexOf('message_') != -1) {
								y = findPosY(root.childNodes[iNode].tagName);
								window.scrollTo(0,y);
							}
						}
			        }
					
				obj=SfindObj('ajloading');
	           		obj.style.visibility='hidden';
	           		obj.style.left=document.body.clientWidth-63+'px';
	           		obj.style.top=document.body.scrollTop+document.body.clientHeight-63+'px';
			        
				}
	        	return;	
	        }
	    }
	     //}
        //catch(e) {
          //  alert('Connection error: ' + e.description);
        //}
}

