function httpRequest(){
	if (self.XMLHttpRequest)
		this.Requester = new XMLHttpRequest()
	else{
		var xmlParsers = ['Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP']
		for (var i=0; i<xmlParsers.length; i++)
  			try {
  				this.Requester = new ActiveXObject(xmlParsers[i])
  			} 
  			catch(er){}
  			finally {
				if (this.Requester)
					break
  			}
	}
	this.useResponse = function(){
		alert(this.Requester.responseText)
	} 
	var _this = this
	this.getData = function getData(){
		if (_this.Requester.readyState == 4)
			if (_this.Requester.status == 200){
				_this.useResponse(_this.Requester.responseText)
				_this.finished = 1
			}
	}	
	this.queryParams = []
	this.setParam = function (param_name, param_value){
		this.queryParams[param_name] = param_value
	}
	this.clearParams = function (){
		this.queryParams = []
	}
	this.queryHeaders = []
	this.setHeader = function (header_name, header_value){
		this.queryHeaders[header_name] = header_value
	}
	this.clearHeaders = function (){
		this.queryHeaders = []
	}
	this.noCache = function (){
		this.queryHeaders["Cache-Control"] = "no-store, no-cache, must-revalidate"		
		this.queryHeaders["Pragma"] = "no-cache"
		this.queryHeaders["Expires"] = "0"
		this.setParam("nocache", Math.random())
	}
	this.exec = function (httpMethod, phpScript, isAsyn){
		var query_string = []
		for (key in this.queryParams) {
            if (!this.queryParams.hasOwnProperty(key)) continue;
			if (this.queryParams[key] != null) {
				query_string.push(key + "=" + this.queryParams[key])
			}
		}
		query_string = query_string.join("&")
		if (httpMethod == "GET"){
			if (query_string)
				phpScript += (phpScript.indexOf("?") > -1) ? "&" + query_string : "?" + query_string
			query_string = null
		}		
		this.Requester.open(httpMethod, phpScript, isAsyn)
		this.Requester.onreadystatechange = this.getData
		for (key in this.queryHeaders) {
            if (!this.queryHeaders.hasOwnProperty(key)) continue;

			if (this.queryHeaders[key] != null)
				this.Requester.setRequestHeader(key, this.queryHeaders[key])
	    }
		if (httpMethod == "POST" && !this.queryHeaders["Content-Type"])
			this.Requester.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
		this.Requester.send(query_string)
		if (this.Requester.readyState == 4 && !this.finished){
			this.getData()
			this.finished = 0
		}
	}	
	return true
}

function showFlavAddPopup(elem)
{
    if (typeof popup == 'undefined') {
        try {
            this.popup = document.getElementById('msg').cloneNode(false);
            popup.id = 'popup_clone';
            popup.style.visibility = 'visible';
            var body = document.body || document.documentElement;
            body.appendChild(popup);
        } catch (err) {
            alert(err);
        }
    }
    this.tulle = document.getElementById('tulle');
    var childElem = document.getElementById('ingr_successful_add');
    var popupHtml = childElem.innerHTML;
    popupHtml = popupHtml.replace(/<span>.+<\/span>/i, '<span>' + elem.title + '</span>');
    childElem.innerHTML = popupHtml;
    popup.appendChild(childElem);

    this.popupLifeTimeStartMs = getMilliseconds();
    
    popup.style.display = 'block';
    tulle.style.display = 'block';
    popup.style.top = getOffsetTop(popup) + 'px';
}

function showIngrAddPopup()
{
    if (countInredients() == 4) {
        document.getElementById('assignWrappingUrl').onclick();
    } else {
        var ingrAddSuccess = document.getElementById('ingrAddSuccess');
        document.getElementById('ingrName').innerHTML = self.option.name;

        getBody().appendChild(ingrAddSuccess);
        ingrAddSuccess.style.display = 'block';
        ingrAddSuccess.style.top = getOffsetTop(ingrAddSuccess) + 'px';
        getTulle().style.display = 'block';
    }

}

function hideIngrAddPopup()
{
    var ingrAddSuccess = document.getElementById('ingrAddSuccess');
    ingrAddSuccess.style.display = 'none';
    getTulle().style.display = 'none';
}

function hidePopupAfterIngrAdd()
{
    try {
        if (typeof popupLifeTimeStartMs != 'undefined') {
            var popupLifeTimeMs = getMilliseconds() - popupLifeTimeStartMs;
            if (popupLifeTimeMs < 1000) {
                pause(1000 - popupLifeTimeMs);
            }
            popupLifeTimeStartMs = undefined;
        }
        if (window.popup) {
            popup.style.display = 'none';
        }
        if (window.tulle) {
            tulle.style.display = 'none';
        }
    } catch (err) {
        alert(err);
    }
}

function registerGlobals()
{
    this.isCalledOnIngrAdd = true;
}

function fetchIngrIdFromElemId(elemId)
{
    var ingrId = elemId.replace(/btn_ingr_|point_/, '');
    return ingrId;
}

function getIngrNameById(ingrId)
{
    var elem = document.getElementById('btn_ingr_' + ingrId);
    var name = elem.title;
    return name;
}

function constructActionOnImageClick(elem)
{
    var ingrId = fetchIngrIdFromElemId(elem.id);
    if (isDisabledIngr(ingrId)) {
        return false;
    } else {
        emulateSelectButtonClick(ingrId);
    }
}

function isDisabledIngr(ingrId)
{
    var ingrTuller = document.getElementById('div_ingr_' + ingrId);
    if (isIe()) {
        if (ingrTuller.style.filter.length) {
            return true;
        }
    } else {
        if (ingrTuller.style.opacity.length) {
            return true;
        }
    }
    return false;
}

function emulateSelectButtonClick(ingrId)
{
    var selectButton = document.getElementById('btn_ingr_' + ingrId);
    selectButton.onclick();
}
