var currentElement = null;
var changeData = null;
var delID = null;
var elementType = null;
var timeHandler = null;
var lockHandler = null;

function setCurrentElement(id) {
	currentElement = document.getElementById(id).value;
}

function delayInput(id) {
	clearTimeout(timeHandler);
	setTimeout("checkDiff('"+id+"')", "2000");
}

function checkDiff(id) {
	clearTimeout(timeHandler);
	if(document.getElementById(id).value == "" || isNaN(document.getElementById(id).value)) {
		if(lockHandler) {
			lockHandler = 0;
			return;
		}
		lockHandler = 1;		
		alert("Моля въведете количество");
		document.getElementById(id).value = currentElement;
	} else {
		if(currentElement != document.getElementById(id).value) {
			if(!changeData) {
				changeData = new ChangeAmount();
			}
			if(parseInt(document.getElementById(id).value) < 1) {
				if(lockHandler) {
					lockHandler = 0;
					return;
				}
				lockHandler = 1;					
				if(confirm("Сигурни ли сте, желаете да изтриете този продукт?")) {
					changeData.delProductData(id);
				} else {
					document.getElementById(id).value = currentElement;
				}
			} else {
				changeData.setProductData(id, parseInt(document.getElementById(id).value));
			}
		}
	}
}

function deleteProduct(id) {
	if(confirm("Сигурни ли сте, че желаете да изтриете този продукт?")) {
		if(!changeData) {
			changeData = new ChangeAmount();
		}
		changeData.delProductData(id);
	}
}

function ChangeAmount() {};
ChangeAmount.constructor = new ChangeAmount();
ChangeAmount.prototype = new DXMLHttpRequestShell();

ChangeAmount.prototype.setProductData = function (id, num) {
	delID = id;
	this.init();
	try {
		this.disableStep2(id);
		this.fastGetUrl(BASE_URL + "basket/count/" + id + "/" + num + ".html");
	} catch (e) {
		throw e;
	}	
}


ChangeAmount.prototype.onLoad = function () {
	document.getElementById("backetContainer").innerHTML = this.getResponseText();
	this.enableStep2();
}

ChangeAmount.prototype.delProductData = function (id) {
	delID = id;
	elementType = 1;
	this.init();
	try {
		this.disableStep2();
		this.fastGetUrl(BASE_URL + "basket/delete/" + id + ".html");
	} catch (e) {
		throw e;
	}			
}

ChangeAmount.prototype.disableStep2 = function () {
	if(document.getElementById("stepTwoButton")) {
		document.getElementById("stepTwoButton").style.display = "none";
		document.getElementById("stepTwoLoad").style.display = "block";
	}
}

ChangeAmount.prototype.enableStep2 = function () {
	if(document.getElementById("stepTwoButton")) {
		document.getElementById("stepTwoButton").style.display = "block";
		document.getElementById("stepTwoLoad").style.display = "none";	
	}
}

/*
document.onkeypress = function(e) {
	var kC  = (window.event) ?    // MSIE or Firefox?
            event.keyCode : e.keyCode;
	var Esc = (window.event) ?   
           27 : e.DOM_VK_ESCAPE // MSIE : Firefox
           
    if(kC == Esc) {
    	closeWarningWindow();
    }
}
*/