var subject_id = '';
var page_blur = ''; // when form submit - this will disable all paje controls
var wl_place = ''; // id of place for "wait, loading..." message


function handleHttpResponse() {
		if (http.readyState == 1) { // when request sended readyState = 1
		if (subject_id != '') {
			waitLayer(); // show "Loading..."
			if (page_blur == '1'){
				elemBlur();
			}
		}
	}

	if (http.readyState == 4) { // when OK result  readyState = 4
		//if (subject_id != '') 
		//{ // if layer for requested content exist
			// remove layer with "Loading..." entry
			if (document.getElementById('loadingpic')) { deleteLayer('loadingpic'); wl_place = ''; }
		    	if (document.getElementById('blurDiv')) { deleteLayer('blurDiv'); page_blur = ''; }
			// content from server
			var inTextAll = http.responseText;
			// place it to your layer with id='subject_id'
			document.getElementById(subject_id).innerHTML = inTextAll;
	/*
	 extract all javascript <script></script> or <script language="javascript"></script> etc.
	 from content, placed to innerHTML
	*/
	var inJs = new Array();
	inJs = inTextAll.split(/<script[^>]*>/);
	if (inJs && inJs.length >1) {
		for (var myivar=1; myivar<inJs.length; myivar++) {
			var inJs2 = new Array();
			inJs2 = inJs[myivar].match(/^([\s\S]*?\w*?[\s\S]*?)<\/script>/);
			if (inJs2[1]) 	{
			// and with 'eval' your scripts will work properly
			eval(inJs2[1]);
			}
		}
		}
	     //}
	}
}

function waitLayer() { 
//return
// this function show layer with "Loading..." entry and image 
// set parameters what you need
	if (document.getElementById('loadingpic')) deleteLayer('loadingpic');
	var wl = document.createElement('DIV');
//	wl.style.background = '#FFF';
	wl.style.whiteSpace = 'nowrap';
	wl.style.width = '16px';
	wl.style.height = '16px';

	wl.style.color = '#333';
	wl.style.position = 'absolute';
	wl.style.padding = '4px';
	wl.style.zIndex = '9999';
	wl.id = 'loadingpic';
	wl.innerHTML = '<span style="position: absolute; top: 0; left: 0; background:#fff;"><img src="/images/load.gif" width="16" height="16" alt="загрузка"/></span>';

	if (wl_place != '') { // move to target place 
		sl = document.getElementById(wl_place);
		wl.style.top = 0;
		wl.style.left = 0;
		sl.appendChild(wl);
		} else { // move to the body top 
		sl = document.getElementById(subject_id);
		wl.style.top = document.body.scrollTop+'px';
		wl.style.left = sl.offsetLeft+'px';
		document.body.appendChild(wl);
		window.onscroll = function() {
			wl.style.top = (window.scrollY) ? (window.scrollY +'px') : (document.body.scrollTop +'px');
		}

		}
}


function elemBlur(elem) {
// this function create layer for blur all controls 
	var value = 4;
	var hgt = document.body.scrollHeight + 'px';
	var wdt = document.body.scrollWidth + 'px';
	if (document.getElementById('blurDiv')) deleteLayer('blurDiv');
	var bl = document.createElement('DIV');
	bl.id = 'blurDiv';
	bl.style.width = wdt;
	bl.style.height = hgt;
	bl.style.position = 'absolute';
	bl.style.zIndex = '99';
	bl.style.top = 0;
	bl.style.left = 0;
	bl.style.opacity = value/10;
	bl.style.filter = 'alpha(opacity=' + value*10 + ')';
        bl.style.background = '#fff';
	document.body.appendChild(bl);
}




// basic crossbrowser XMLHttpRequest function
function getHTTPObject() {
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlhttp = false;
			}
		}
	@else
	xmlhttp = false;
	@end @*/

	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

 
 
// We create the HTTP Object
var http = getHTTPObject();


// this function make GET request
function httpGET(url, id) {
	subject_id = id;
	http.open("GET", url, true);
        
	http.onreadystatechange = handleHttpResponse;
	http.send(null);
}

var myhttp = getHTTPObject();

function myhttpGET(url, id) {
	subject_id2 = id;
	myhttp.open("GET", url, true);
	myhttp.onreadystatechange = myhandleHttpResponse;
	myhttp.send(null);
}

function myhandleHttpResponse() {
		if (myhttp.readyState == 1) { // when request sended readyState = 1
		if (subject_id2 != '') {
			waitLayer(); // show "Loading..."
			if (page_blur == '1'){
				elemBlur();
			}
		}
	}
	if (myhttp.readyState == 4) { // when OK result  readyState = 4
		if (subject_id2 != '') { // if layer for requested content exist
			// remove layer with "Loading..." entry
			if (document.getElementById('loadingpic')) { deleteLayer('loadingpic'); wl_place = ''; }
		    if (document.getElementById('blurDiv')) { deleteLayer('blurDiv'); page_blur = ''; }
			// content from server
			var inTextAll = myhttp.responseText;
			// place it to your layer with id='subject_id'
			document.getElementById(subject_id2).innerHTML = inTextAll;
	/*
	 extract all javascript <script></script> or <script language="javascript"></script> etc.
	 from content, placed to innerHTML
	*/
			var inJs = new Array();
			inJs = inTextAll.split(/<script[^>]*>/);
			if (inJs && inJs.length >1) {
				for (var myivar=1; myivar<inJs.length; myivar++) {
					var inJs2 = new Array();
					inJs2 = inJs[myivar].match(/^([\s\S]*?\w*?[\s\S]*?)<\/script>/);
					if (inJs2[1]) 	{
						// and with 'eval' your scripts will work properly
						eval(inJs2[1]);
					}
				}
			}
		}
	}
}

var newhttp = getHTTPObject();

function newhttpGET(url, id) {
	subject_id3 = id;
	newhttp.open("GET", url, true);
	newhttp.onreadystatechange = newhandleHttpResponse;
	newhttp.send(null);
}

function newhandleHttpResponse() {
		if (newhttp.readyState == 1) { // when request sended readyState = 1
		if (subject_id3 != '') {
			waitLayer(); // show "Loading..."
			if (page_blur == '1'){
				elemBlur();
			}
		}
	}
	if (newhttp.readyState == 4) { // when OK result  readyState = 4
		if (subject_id3 != '') { // if layer for requested content exist
			// remove layer with "Loading..." entry
			if (document.getElementById('loadingpic')) { deleteLayer('loadingpic'); wl_place = ''; }
		    	if (document.getElementById('blurDiv')) { deleteLayer('blurDiv'); page_blur = ''; }
			// content from server
			var inTextAll = newhttp.responseText;
			// place it to your layer with id='subject_id'
			document.getElementById(subject_id3).innerHTML = inTextAll;
	/*
	 extract all javascript <script></script> or <script language="javascript"></script> etc.
	 from content, placed to innerHTML
	*/
	var inJs = new Array();
	inJs = inTextAll.split(/<script[^>]*>/);
	if (inJs && inJs.length >1) {
		for (var myivar=1; myivar<inJs.length; myivar++) {
			var inJs2 = new Array();
			inJs2 = inJs[myivar].match(/^([\s\S]*?\w*?[\s\S]*?)<\/script>/);
			if (inJs2[1]) 	{
			// and with 'eval' your scripts will work properly
			eval(inJs2[1]);
			}
		}
		}
	     }
	}
}



// this function make POST request
function httpPOST(url, params, id) {
	subject_id = id;
	http.open("POST", url, true);
	http.setRequestHeader("Content-Type", "application/octet-stream");
	http.onreadystatechange = handleHttpResponse;
	http.send(params);
}



function getRequestParams(formID) {
    var params = new Array();
    if (document.getElementById(formID)) {
    	myform = document.getElementById(formID);
    } else {
    	myform = formID;
    }
    for (var i=0 ; i < myform.elements.length; i++) {
    	if (myform.elements[i].type != "checkbox" || (myform.elements[i].type == "checkbox" && (myform.elements[i].checked || myform.elements[i].checked == true))) {
        var sParam = encodeURIComponent(myform.elements[i].name);
        sParam += "=";
        sParam += encodeURIComponent(myform.elements[i].value);
        params.push(sParam);
        // this disable submit button from double click on send request
	if (myform.elements[i].type == "submit") {myform.elements[i].disabled='disabled';}
        }
    }
//    params.push(sParam);
    return params.join("&");
}


// this function remove any layer from HTML-document (from web-page in browser)
function deleteLayer(layerID) {
	var delLayer = document.getElementById(layerID);
	delLayer.innerHTML = '';
	delLayer.parentNode.removeChild(delLayer);
}

// this function create new layer in HTML-document (in web-page in browser)
function createLayer(newLayerID, parentLayerID) {
        var parentLayer = (parentLayerID != '') ? document.getElementById(parentLayerID):document.body;
	var newLayer = document.createElement('DIV');
	newLayer.id = newLayerID;
        parentLayer.appendChild(newLayer);
}



function fadeIn(o,p) {
	for (var i=0; i<11; i++) {
		setTimeout('setOpacity('+i+','+o+','+p+')',50*i);
		}
	return false;
} 

function fadeOut(o,p) {
var k = 0;
fadeSet = 'out';
	for (var i=11; i>0; i--) {
		k++;
		setTimeout('setOpacity('+i+','+o+','+p+')',50*k);
		}
	return false;
}

function setOpacity(v,o,p) {
	o.style.opacity = v/10;
	o.style.filter = 'alpha(opacity=' + v*10 + ')';
	if (p) {
		(fadeSet=='out' && v==1) ? (p.style.display = 'none') : (p.style.display = 'block')
	}
}


function getXmlHttp(){
  var xmlhttp;
  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;
}

function sendBannerRequest(){
	var xmlhttp = getXmlHttp();
	var obj = document.getElementById('inputarea_id');
	var option = obj.options[obj.selectedIndex];
	var param = '';
	if(option.className.indexOf('city')>=0)
		param = 'city=';
	else
		param = 'region=';
	
	param += option.value;
	
	param += '&doc-url=' + document.getElementById('doc-url').value;
	
	param += '&flo=' + Math.random();
	
	var url = '/search-banners.xml?' + param;
	//alert(url);
	xmlhttp.open('GET', url, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if(xmlhttp.status == 200) {
				var text = xmlhttp.responseText;
				text = text.replace('<root>', '');
				text = text.replace('</root>', '');
				eval (text);
				
				//alert(text);
			}
		}
	};
	xmlhttp.send(null);
}

var oldBannerTop, oldBannerLeftBottom, oldBannerCenterBottom, oldBannerRightBottom;
function changeBanners(bannerPlaceId, bannerId, bannerTitle, bannerImage, bannerFlash, bannerLink){
	var td;
	if(bannerPlaceId == '740605')
	{
		td = document.getElementById('BigHorizBanner');
		wi = 1001;
		if(!oldBannerTop && oldBannerTop != '')
			oldBannerTop = td.innerHTML;
	}
	else if (bannerPlaceId == '740606')
	{
		td = document.getElementById('LeftBottomBanner');
		wi = 248;
		if(!oldBannerLeftBottom && oldBannerLeftBottom != '')
			oldBannerLeftBottom = td.innerHTML;
	}
	else if (bannerPlaceId == '740607')
	{
		td = document.getElementById('CenterBottomBanner');
		wi = 248;
		if(!oldBannerCenterBottom && oldBannerCenterBottom != '')
			oldBannerCenterBottom = td.innerHTML;
	}
	else if (bannerPlaceId == '740608')
	{
		td = document.getElementById('RightBottomBanner');
		wi = 248;
		if(!oldBannerRightBottom && oldBannerRightBottom != '')
			oldBannerRightBottom = td.innerHTML;
	}
	
	if(bannerId){
		if(bannerFlash.indexOf('.swf')>0){
			td.innerHTML = '<div id="flash_'  + bannerPlaceId + '" style="padding: 3px 0;"></div>'
			var flashURL = bannerFlash + '?'  + bannerLink;
			var so = new SWFObject(flashURL, 'flash_' + bannerPlaceId +  '_swf', wi, '88', '8', '');
			so.addVariable('flashVarText', 'this is passed in via FlashVars for example only');
			so.addParam('scale', 'noscale');
			so.addParam('wmode', 'transparent');
			so.write('flash_' + bannerPlaceId);
	
		}else{
			var a = document.createElement ('a');
			var img = new Image();
			img.alt = bannerTitle;
			a.href = '/goto.xml?bid=' + bannerId;
			img.src = bannerImage;
			a.appendChild (img);
			td.innerHTML = '';
			td.appendChild (a);
		}
	}else{
		if(bannerPlaceId == '740605')
			td.innerHTML = '';
		else if(bannerPlaceId == '740606')
			td.innerHTML = '';
		else if(bannerPlaceId == '740607')
			td.innerHTML = '';
		else if(bannerPlaceId == '740608')
			td.innerHTML = '';
		
	}
}
