
var comparePresent = new Array;
comparePresent = {};

//Добавление товара в корзинку
function addToBasket(nom_id) {
//	LoadHTML('addToBasket', document.getElementById('short_basket'), 'short_basket', {'nom_id' : nom_id});
	var func = function(req, tag, env_par, dis, evl) {
		var s = req.responseJS[0];
		tag.innerHTML = s;
		var d = document.getElementById('bask_not');
		d.style.display = 'block';
		x = document.body.clientWidth - d.width;
		y = document.body.clientHeight + document.body.scrollTop - d.height;
		d.style.top = y;
		d.style.left = x;
		$("#bask_not").show("drop", { direction: "down" }, 1000);
	    setTimeout('$("#bask_not").hide("drop", { direction: "down" }, 1000)', 3000);
	}
	Load('/cgi-bin/Runs/dyna_loader.pl', {'par' : 'addToBasket', 'nom_id' : nom_id}, document.getElementById('short_basket'), func);
}

//Добавление товара к сравнению
function addCompareItem(itemInfo) {
	if(comparePresent[itemInfo['nID']]) {
		alert('Товар уже добавлен к сравнению');
		return;
	}

	var compTbl = document.getElementById('compTbl');
	if(! compTbl) {
		return;
	}
	var rows = compTbl.rows;
	var present = 0;
	for(var i in comparePresent) {
		present = 1;
	}
	if(! present) {
		compTbl.deleteRow(0);
	}
		
	var row = compTbl.insertRow(rows.length);
	var id = Math.random();

	var cellChk = row.insertCell(0);
	var cellText = row.insertCell(1);
	
	cellChk.innerHTML = '<input type="checkbox" name="compare[]" value="' + itemInfo['nID'] + '" />';
	cellText.innerHTML = itemInfo['name'];
	
	//compDiv.innerHTML += '<br /><a href="' + itemInfo['href'] + '" title="Добавить к сравнению">' + itemInfo['name'] + '</a>';
	
	//Добавление на сервере
	dynaExecute('addCompareItem', {'nID' : itemInfo['nID']});
	comparePresent[itemInfo['nID']] = itemInfo;
	
	alert('Товар добавлен в сравнительную таблицу');
	return false;
	
}

function saveValue(action, nom_id, value) {
	if(action == 'price') {
		dynaExecute('savePrice', {'script': '/cgi-bin/Runs/shop.cgi', 'price': value, 'nom_id': nom_id});
		return;
	}
	if(action == 'priceOld') {
		dynaExecute('savePrice', {'script': '/cgi-bin/Runs/shop.cgi', 'price_old': value, 'nom_id': nom_id});
		return;
	}
	var markup = 0;
	if (document.getElementById('markup_' + nom_id)) { 
		markup = parseFloat(document.getElementById('markup_' + nom_id).value);
	}
	var markup_add = 0;
	if (document.getElementById('markup_add_' + nom_id)) {
		markup_add = parseFloat(document.getElementById('markup_add_' + nom_id).value);
	}

	if(action == 'priceBase') {
		dynaExecute('savePriceBase', {'script': '/cgi-bin/Runs/shop.cgi', 'base': value, 'nom_id': nom_id});
		document.getElementById('price_' + nom_id).value = parseFloat(document.getElementById('base_' + nom_id).value / 100 * markup) + parseFloat(document.getElementById('base_' + nom_id).value) + markup_add;
		return;
	}
	if(action == 'priceMarkup') {
		dynaExecute('savePriceBase', {'script': '/cgi-bin/Runs/shop.cgi', 'markup': value, 'nom_id': nom_id});
		document.getElementById('price_' + nom_id).value = parseFloat(document.getElementById('base_' + nom_id).value / 100 * markup) + parseFloat(document.getElementById('base_' + nom_id).value) + markup_add;
		return;
	}
	if(action == 'priceMarkupAdd') {
		dynaExecute('savePriceBase', {'script': '/cgi-bin/Runs/shop.cgi', 'markup_add': value, 'nom_id': nom_id});
		document.getElementById('price_' + nom_id).value = parseFloat(document.getElementById('base_' + nom_id).value / 100 * markup) + parseFloat(document.getElementById('base_' + nom_id).value) + markup_add;
		return;
	}
	if(action == 'countWarehouse') {
		dynaExecute('updateWarehouse', {'script': '/cgi-bin/Runs/shop.cgi', 'value': value, 'nom_id': nom_id});
		return;
	}
	if(action == 'dateIncome') {
		dynaExecute('updateDateIncome', {'script': '/cgi-bin/Runs/shop.cgi', 'value': value, 'nom_id': nom_id});
		return;
	}
	dynaExecute('saveNomValue', {'script': '/cgi-bin/Runs/shop.cgi', 'value': value, 'field': action, 'nom_id': nom_id});
}

function sel_addr_tab(max_t, t) {
    for (i=1;i<=max_t;i++)
        if (document.getElementById('tab_addr_'+i))
            document.getElementById('tab_addr_'+i).style.display = 'none';
    document.getElementById('tab_addr_'+t).style.display = 'block';
}

function showEditNomComment(txt, itemID) {
        if(tIDn) {
                clearTimeout(tIDn);
        }
        var dv = document.getElementById('divNomComment');
        dv.style.width = 270;
        dv.style.left = parseInt(dv.style.left)-150;
        var tArea = '<textarea id="editNomComment" rows="6" cols="60" style="font-size:10px">' + txt + '</textarea><br /><div align="center"><input type="button" value="Сохранить" onclick="saveNomComment(document.getElementById(\'editNomComment\').innerHTML, ' + itemID + ')" />&nbsp;&nbsp;<input type="button" value="Отменить" onclick="closeNomComment()" /></div>';
        dv.innerHTML = tArea;
}

function saveNomComment(txt, itemID) {
        if(! itemID ) {
                return;
        }
        dynaExecute('saveNomComment', {'textValue': txt, 'itemID': itemID, 'script': '/cgi-bin/Runs/shop.cgi'});
        document.getElementById('sp_' + itemID).comment = txt;
	if (txt.length == 0) {
		txt = '<span style="color: #cccccc">--- нет примечания ---</span>';
	}
        document.getElementById('sp_' + itemID).innerHTML = txt;
        closeNomComment();
}

function showNomComment(txt, itemID) {
        if(tIDn) {
                clearTimeout(tIDn);
        }
	var X, Y, cX, cY;
	if (window.event) {
		X = d.body.scrollLeft; Y = d.body.scrollTop;
		cX = event.clientX; cY = event.clientY;
	} else {
		X = window.pageXOffset; Y = window.pageYOffset;
        cX = 200; cY = 200; // cX = Event.pageX; cY = Event.pageY;
	}
        
        document.getElementById('divNomComment').style.top = cY + Y + 5;
        document.getElementById('divNomComment').style.left = cX + X;
        document.getElementById('divNomComment').style.display = '';
        document.getElementById('divNomComment').comment = txt;
        txt = txt + '<br /><span style="font-size:9px">----------<br />*Двойной щелчок для редактирования</span>';
        document.getElementById('divNomComment').innerHTML = txt;
        document.getElementById('divNomComment').itemID = itemID;
}

function closeNomComment() {
        document.getElementById('divNomComment').style.width = 120;
        document.getElementById('divNomComment').style.display = 'none';
}

