var currentAction = '';
var currentLang = 1;
var ajaxUrl = "AJAXServices.aspx";
var req;
var cursor = false;
var offerType = 0;
var useTopOffers = false;
var useCatalog = false;

	//////////////////////////////// Test case ///////////////////////////////
	var objToSet = null;
	function loadXMLDoc(url) {
		req = false;
		// branch for native XMLHttpRequest object
		if(window.XMLHttpRequest) {
			try {
				req = new XMLHttpRequest();
			} catch(e) {
				req = false;
			}
		// branch for IE/Windows ActiveX version
		} else if(window.ActiveXObject) {
			try {
				req = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					req = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) {
					req = false;
				}
			}
		}
		if(req) {
			if(document.getElementById('loading'))
			{
				document.getElementById('loading').style.display = 'inline';
			}
			
			req.onreadystatechange = processReqChange;
			req.open("GET", url, true);
			req.send("");
		}
	}

	function processReqChange() {
		// only if req shows "loaded"
		if (req.readyState == 4) {
			// only if "OK"
			if (req.status == 200) {
				if(req.responseText.length > 0)
				{
					if(objToSet)
					{
						objToSet.innerHTML = req.responseText;
						if(objToSet.tagName == 'select')
						{
							objToSet.selectedIndex = 0;
						}
						
						objToSet = null;
					}
				}
				
				if(!nextCommand)
				{
					if(document.getElementById('loading'))
					{
						document.getElementById('loading').style.display = 'none';
					}
				}
				else
				{
					setTimeout(nextCommand, 5);
				}
			}
		}
	}
	//////////////////////////////// Test case ///////////////////////////////

	function loadData(url)
	{
		// branch for native XMLHttpRequest object
		if (window.XMLHttpRequest) 
		{
			req = new XMLHttpRequest();
		} 
		else if (window.ActiveXObject) 
		{
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}

		if(req)		
		{
			req.open("GET", url, false);
			req.send(null);
			
			var txt = req.responseText;
			if( txt.length > 0 )	
			{
				return txt;
			}
		}
		
		return '';
	}
	
	function initAXAJ(lang, l_useTopOffers, l_useCatalog)
	{
		useTopOffers = l_useTopOffers;
		useCatalog = l_useCatalog;
		currentAction = 'init';
		currentLang = lang;
		
		if(useTopOffers)
			document.getElementById('divTypes3').style.display = 'none';
			
		if(useTopOffers || useCatalog)
		{
			getOffers(lang);
		}
	}
	
	function getLocations(lang)
	{
		// document.body.style.cursor = 'wait';
		// cursor = true;
		
		// var otype = document.getElementById('sOfferType').value;
		var otypes = document.getElementsByName('sOfferType');
		otype = otypes[0].checked ? otypes[0].value : otypes[1].value;
		if(otype.length == 0)
			otype = '-1';
			
		var typesObj = document.getElementById('divTypes');
		var typeSelect = typesObj.childNodes.item(0);
		typeSelect.innerHTML = '';
		
		objToSet = document.getElementById('divLocations');
		loadXMLDoc(ajaxUrl + '?lang=' + lang + '&action=locations' + '&otype=' + otype + ((useTopOffers) ? "&top=1" : "") + '&p=1&ssiidd=' + Math.random()*10000);
		var types3Obj = document.getElementById('divTypes3');
		var type3Select = types3Obj.childNodes.item(0);
		type3Select.innerHTML = '';
		/*
		var lselect = loadData(ajaxUrl + '?lang=' + lang + '&action=locations' + '&otype=' + otype + ((useTopOffers) ? "&top=1" : "") + '&p=1');
		if( lselect.length > 0 )
		{
			var locationsObj = document.getElementById('divLocations');
			locationsObj.innerHTML = lselect;
			locationsObj.selectedIndex = 0;
			
			var typesObj = document.getElementById('divTypes');
			var typeSelect = typesObj.childNodes.item(0);
			typeSelect.innerHTML = '';
			
			var types3Obj = document.getElementById('divTypes3');
			var type3Select = types3Obj.childNodes.item(0);
			type3Select.innerHTML = '';
		}
		document.body.style.cursor = 'default';
		cursor = false;
		*/
	}
	
	function getTypes(lang, location)
	{
		/*
		var setCursor = false;
		if(!cursor)
		{
			cursor = true;
			setCursor = true;
			document.body.style.cursor = 'wait';
		}
		*/
		
		if(location == '-1')
			location = document.getElementById(varLocationId).childNodes.item(0).value;
		if(location.length == 0)		
			location = "-1";
			
		// var otype = document.getElementById('sOfferType').value;
		var otypes = document.getElementsByName('sOfferType');
		otype = otypes[0].checked ? otypes[0].value : otypes[1].value;
		if(otype.length == 0)
			otype = '-1';
			
		objToSet = document.getElementById('divTypes');
		loadXMLDoc(ajaxUrl + '?lang=' + lang + '&action=types&location=' + location + '&otype=' + otype + ((useTopOffers) ? "&top=1" : "") + '&p=1&ssiidd=' + Math.random()*10000);
		var types3Obj = document.getElementById('divTypes3');
		var type3Select = types3Obj.childNodes.item(0);
		type3Select.innerHTML = '';
		/*
		var tselect = loadData(ajaxUrl + '?lang=' + lang + '&action=types&location=' + location + '&otype=' + otype + ((useTopOffers) ? "&top=1" : "") + '&p=1');
		if( tselect.length > 0 )
		{
			var typesObj = document.getElementById('divTypes');
			typesObj.innerHTML = tselect;
			typesObj.selectedIndex = 0;
			
			if(!useTopOffers)
			{
				var typObj = document.getElementById(typesObj.childNodes.item(0).id);
			}
			
			var types3Obj = document.getElementById('divTypes3');
			var type3Select = types3Obj.childNodes.item(0);
			type3Select.innerHTML = '';

		}
		
		if(setCursor)
		{
			cursor = false;
			setCursor = false;
			document.body.style.cursor = 'default';
		}
		*/
		
		currentAction = '';
	}
	
	function getTypes3(lang, location, parentType)
	{
		/*
		var setCursor = false;
		if(!cursor)
		{
			cursor = true;
			setCursor = true;
			document.body.style.cursor = 'wait';
		}
		*/
		
		if(parentType.length == 0)		
			parentType = "-1";
			
		// var otype = document.getElementById('sOfferType').value;
		var otypes = document.getElementsByName('sOfferType');
		otype = otypes[0].checked ? otypes[0].value : otypes[1].value;
		if(otype.length == 0)
			otype = '-1';

		objToSet = document.getElementById('divTypes3');
		loadXMLDoc(ajaxUrl + '?lang=' + lang + '&action=types3&location=' + location + '&parent=' + parentType + '&otype=' + otype + ((useTopOffers) ? "&top=1" : "") + '&p=1&ssiidd=' + Math.random()*10000);
		/*			
		var tselect = loadData(ajaxUrl + '?lang=' + lang + '&action=types3&location=' + location + '&parent=' + parentType + '&otype=' + otype + ((useTopOffers) ? "&top=1" : "") + '&p=1');
		if( tselect.length > 0 )
		{
			document.getElementById('divTypes3').innerHTML = tselect;
		}
		
		if(currentAction == 'init' &&
			(useTopOffers || useCatalog))
		{
			getOffers(lang);
		}
		
		if(setCursor)
		{
			cursor = false;
			setCursor = false;
			document.body.style.cursor = 'default';
		}
		*/
		
		currentAction = '';
	}
	
	function submitOffers(lang)
	{
		// var strCompare = (lang == 1) ? " оферта N:": " offer N:";
		// if(document.getElementById('txtSearchInput').value.length > 0 &&
		// 	document.getElementById('txtSearchInput').value != strCompare)
		if(document.getElementById('txtSearchInput').value.length > 0)
		{
			getOfferDetails(lang, 1);
			
			return;
		}
		
		getOffers(lang);
	}
	
	function getOffers(lang, errString)
	{
		var otable = '';
		var requestUrl = '';
		var selected = false;
		
		var strQuery = '?lang=' + lang + '&action=offers&f=1';
		
		var locationsObj = document.getElementById('divLocations').childNodes.item(0);
		var typesObj = document.getElementById('divTypes').childNodes.item(0);
		var types3Obj = document.getElementById('divTypes3').childNodes.item(0);
		
		var locationValue = locationsObj.value;
		if(locationValue.length > 0)
		{
			strQuery += '&location=' + locationValue;
			selected = true;
		}
			
		var typeValue = typesObj.value;
		if(typeValue.length > 0)
		{
			strQuery += '&type=' + typeValue;
			selected = true;
		}
			
		var type3Value = types3Obj.value
		if(type3Value.length > 0)
		{
			strQuery += '&type3=' + type3Value;
			selected = true;
		}
		
		// var otype = document.getElementById('sOfferType').value
		var otypes = document.getElementsByName('sOfferType');
		otype = otypes[0].checked ? otypes[0].value : otypes[1].value;
		if(otype.length > 0)
		{
			strQuery += '&otype=' + otype;
			selected = true;
		}
		
		if(!selected)
		{
			alert(errString);
			
			return;
		}

		requestUrl = ajaxUrl + strQuery + ((useTopOffers) ? "&top=1" : "") + '&ssiidd=' + Math.random()*10000;
		
		objToSet = document.getElementById('divContent');
		loadXMLDoc(requestUrl);
		/*
		otable = loadData(requestUrl);
		
		document.getElementById('divContent').innerHTML = otable;
		*/
	}

	function getOfferDetails(lang, offerId)
	{
		var otable = '';
		var requestUrl = '';
		
		if(offerId == 1)
		{
			if(document.getElementById('txtSearchInput').value == '' ||
				isNaN(parseInt(document.getElementById('txtSearchInput').value)))
			{
				if(lang == 1)
				{
					alert('Моля въведете валиден номер на оферта.');
				}
				else
				{
					alert('Please enter valid offer number');
				}
				return;
			}

			var otypes = document.getElementsByName('sOfferType');
			otype = otypes[0].checked ? otypes[0].value : otypes[1].value;
			
			requestUrl = ajaxUrl + '?lang=' + lang + '&action=details&onum=' + document.getElementById('txtSearchInput').value + '&otype=' + otype + '&ssiidd=' + Math.random()*10000;
		}
		else
		{
			// requestUrl = ajaxUrl + '?lang=' + lang + '&action=details&oid=' + offerId + '&otype=' + document.getElementById('sOfferType').value;
			requestUrl = ajaxUrl + '?lang=' + lang + '&action=details&oid=' + offerId + '&ssiidd=' + Math.random()*10000;
		}
		
		objToSet = document.getElementById('divContent');
		loadXMLDoc(requestUrl);
		/*
		otable = loadData(requestUrl);
		if(document.getElementById('divContent'))
		{
			document.getElementById('divContent').innerHTML = otable;
		}
		*/
	}

	function setPage(lang, page)
	{
		objToSet = document.getElementById('divContent');
		var strUrl = ajaxUrl + '?lang=' + lang + '&action=page&p=' + page + '&ssiidd=' + Math.random()*10000;
		loadXMLDoc(strUrl);
		/*
		var otable = loadData(ajaxUrl + '?lang=' + lang + '&action=page&p=' + page);
		document.getElementById('divContent').innerHTML = otable;
		*/
	}

	function sortOffers(lang, sortColumn)
	{
		var requestUrl = ajaxUrl + '?lang=' + lang + '&action=sort&s=' + sortColumn + '&ssiidd=' + Math.random()*10000;
		
		objToSet = document.getElementById('divContent');
		loadXMLDoc(requestUrl);
		/*
		otable = loadData(requestUrl);
		
		document.getElementById('divContent').innerHTML = otable;
		*/
	}
	
	function getOfferImage(lang, imageIndex)
	{
		var requestUrl = ajaxUrl + '?lang=' + lang + '&action=image&ii=' + imageIndex + '&ssiidd=' + Math.random()*10000;
		
		objToSet = document.getElementById('imageDiv');
		loadXMLDoc(requestUrl);
		/*
		otable = loadData(requestUrl);
		
		document.getElementById('imageDiv').innerHTML = otable;
		*/
	}
	
	function getOffersQuarter(lang, quarter)
	{
		var requestUrl = ajaxUrl + '?lang=' + lang + '&action=offers&p=1&q=' + quarter + '&ssiidd=' + Math.random()*10000;
		
		objToSet = document.getElementById('divContent');
		loadXMLDoc(requestUrl);
	}
	
	function getOffersCategory(lang, category)
	{
		var otypes = document.getElementsByName('sOfferType');
		otype = otypes[0].checked ? otypes[0].value : otypes[1].value;
		
		var requestUrl = ajaxUrl + '?lang=' + lang + '&action=offers&p=1&f=1&c=' + category + '&otype=' + otype + '&ssiidd=' + Math.random()*10000;
		
		objToSet = document.getElementById('divContent');
		loadXMLDoc(requestUrl);
	}
	
	function getCategories(lang)
	{
		nextCommand = null;
		
		var otypes = document.getElementsByName('sOfferType');
		otype = otypes[0].checked ? otypes[0].value : otypes[1].value;
		
		var requestUrl = ajaxUrl + '?lang=' + lang + '&action=categories&otype=' + otype + '&ssiidd=' + Math.random()*10000;
		
		objToSet = document.getElementById('categories');
		loadXMLDoc(requestUrl);
	}

