var ajaxObjects = new Array();
var id_hist_cart = '0';
var cart_count = 0;
var add = ''
var remove = ''
var Firms = new Array(); // кол-во товаров у фирмы в корзине
var warning_cart = '';

function actGoods(id_web_user, id_hist_cart, id_service, id_city, id_firm, id_price, indexFirm)
{
	var im = document.getElementById('i_'+id_service+"_"+id_city+"_"+id_firm+"_"+id_price);
	if ((checksubstring(im.src,'unbuy')) || (checksubstring(im.src,'delcart')))
	{
		//alert('Удаляем');
		removeGoods(remove, id_web_user, id_hist_cart, id_service, id_city, id_firm, id_price, indexFirm);
	}
	else 
	{
		//alert('Добавляем');
		addGoods(add, id_web_user, id_hist_cart, id_service, id_city, id_firm, id_price, indexFirm);
	}
}

function addGoods(__action, id_web_user, id_hist_cart, id_service, id_city, id_firm, id_price, indexFirm)
{
	if (checkCookie()) {
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].method = "GET";
	ajaxObjects[ajaxIndex].requestFile = 'cart';	// Saving product in this file
	ajaxObjects[ajaxIndex].setVar('__action',__action);
	ajaxObjects[ajaxIndex].setVar('id_web_user',id_web_user);
	ajaxObjects[ajaxIndex].setVar('id_hist_cart',id_hist_cart);
	ajaxObjects[ajaxIndex].setVar('id_service',id_service);
	ajaxObjects[ajaxIndex].setVar('id_city',id_city);
	ajaxObjects[ajaxIndex].setVar('id_firm',id_firm);
	ajaxObjects[ajaxIndex].setVar('id_price',id_price);
	ajaxObjects[ajaxIndex].onCompletion = function(){ajaxValidateAddGoods(ajaxIndex, id_service, id_city, id_firm, id_price, indexFirm);};	// Specify function that will be executed after file has been found
	document.getElementById("i_"+id_service+"_"+id_city+"_"+id_firm+"_"+id_price).src='../../img/buy-loader.gif';
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function		
	}
}


function ajaxValidateAddGoods(ajaxIndex, id_service, id_city, id_firm, id_price, indexFirm)
{
	if(ajaxObjects[ajaxIndex].status=200)
	{	
		cart_count = 1+cart_count;
		id_hist_cart = ajaxObjects[ajaxIndex].response;
		document.cookie = "id_hist_cart"+"="+escape(id_hist_cart)+";";
		//document.getElementById("id_hist_cart").innerHTML = id_hist_cart;
		document.getElementById("cart_count").innerHTML = cart_count;
		//document.getElementById("id_hist_cart").style.display = 'None';
		document.getElementById("link_export_order").style.display = '';
		document.getElementById("i_"+id_service+"_"+id_city+"_"+id_firm+"_"+id_price).src='../../img/unbuy.gif';
 		if (document.getElementById("add_"+id_service+"_"+id_city+"_"+id_firm+"_"+id_price) != null) 
			{document.getElementById("add_"+id_service+"_"+id_city+"_"+id_firm+"_"+id_price).style.display = 'None';}
		if (document.getElementById("added_"+id_service+"_"+id_city+"_"+id_firm+"_"+id_price) != null)
			{document.getElementById("added_"+id_service+"_"+id_city+"_"+id_firm+"_"+id_price).style.display = '';}
		//alert('Добавлено в корзину! № '+id_hist_cart)
	}	
	else alert('Ошибка при добавлении в корзину! '+ajaxObjects[ajaxIndex].status);
	
}
  
function removeGoods(__action, id_web_user, id_hist_cart, id_service, id_city, id_firm, id_price, indexFirm)
{
	//HideRow(id_service+"_"+id_city+"_"+id_firm+"_"+id_price);
	//return;
	if (checkCookie()) {
	var ajaxObject;
	ajaxObject = new sack();
	ajaxObject.method = "GET";
	ajaxObject.requestFile = 'cart';	// Saving product in this file
	ajaxObject.setVar('__action',__action);
	ajaxObject.setVar('id_web_user',id_web_user);
	ajaxObject.setVar('id_hist_cart',id_hist_cart);
	ajaxObject.setVar('id_service',id_service);
	ajaxObject.setVar('id_city',id_city);
	ajaxObject.setVar('id_firm',id_firm);
	ajaxObject.setVar('id_price',id_price);
	ajaxObject.onCompletion = function(){ajaxValidateRemoveGoods(ajaxObject, id_service, id_city, id_firm, id_price, indexFirm);};	// Specify function that will be executed after file has been found
	document.getElementById("i_"+id_service+"_"+id_city+"_"+id_firm+"_"+id_price).src='../../img/unbuy-loader.gif';
	ajaxObject.runAJAX();		// Execute AJAX function		
	}
}

function ajaxValidateRemoveGoods(ajaxObject, id_service, id_city, id_firm, id_price, indexFirm)
{
	if(ajaxObject.status=200)
	{	
		cart_count = parseInt(ajaxObject.response); //cart_count = cart_count-1;
		document.getElementById("cart_count").innerHTML = cart_count;
		if (document.getElementById('del_'+id_service+"_"+id_city+"_"+id_firm+"_"+id_price) != null) 
		{
			if (id_price == 0) {
				Firms[indexFirm] = 0;
				document.getElementById('del_'+id_service+"_"+id_city+"_"+id_firm+"_"+id_price).style.display = 'None';
			}
			else {
				document.getElementById('del_'+id_service+"_"+id_city+"_"+id_firm+"_"+id_price).style.display = 'None';
				Firms[indexFirm] = Firms[indexFirm] - 1;
				if (Firms[indexFirm] == 0) {document.getElementById('f_'+indexFirm).style.display = 'None';}
			}
			if (cart_count == 0) 
			{
				document.getElementById('title').innerHTML = 'Ваша корзина пуста';
				document.getElementById('order').style.display = 'None';
			}
		}
		else {
			document.getElementById("i_"+id_service+"_"+id_city+"_"+id_firm+"_"+id_price).src='../../img/buy.gif';
			if (id_price != 0) {
				if (document.getElementById("add_"+id_service+"_"+id_city+"_"+id_firm+"_"+id_price)!=null){document.getElementById("add_"+id_service+"_"+id_city+"_"+id_firm+"_"+id_price).style.display = '';}
				if (document.getElementById("added_"+id_service+"_"+id_city+"_"+id_firm+"_"+id_price)!=null){document.getElementById("added_"+id_service+"_"+id_city+"_"+id_firm+"_"+id_price).style.display = 'None';}
			}
			//alert('Добавлено в корзину! № '+id_hist_cart)
		}
	}	
	else alert('Ошибка при удалении из корзины! '+ajaxObject.status);
	
}

function HideRow(id)
{
	var speed = 1
	var objName = document.getElementById(id);
	var objCost = document.getElementById('cost_'+id);
	var objImage = document.getElementById('i_'+id);
	var obj = document.getElementById('del_'+id);
	objName.innerHTML = '';
	objCost.innerHTML = '';
	objImage.style.height = (objImage.offsetHeight - speed)+'px';
	obj.style.height = (obj.offsetHeight - speed)+'px';
	//alert(obj.offsetHeight);
	if(obj.offsetHeight > 4)
		setTimeout(HideRow(id),200);
	else obj.style.display = 'None';
}

function imageover(im) {
	if (im.src.substring(im.src.indexOf('.gif')-5,im.src.indexOf('.gif')) == 'unbuy') im.src='../../img/unbuy_hover.gif';
	if (im.src.substring(im.src.indexOf('.gif')-3,im.src.indexOf('.gif')) == 'buy') im.src='../../img/buy_hover.gif';
}
function imageout(im) {
	if (im.src.substring(im.src.indexOf('.gif')-11,im.src.indexOf('.gif')) == 'unbuy_hover') im.src='../../img/unbuy.gif';
	if (im.src.substring(im.src.indexOf('.gif')-9,im.src.indexOf('.gif')) == 'buy_hover') im.src='../../img/buy.gif';
}

function checksubstring(str,needle) {

	if (str.indexOf(needle)!=-1) return true; else return false;

}

function orderGoods(__action)
{
	if (checkCookie()) {
	var ajaxObject = new sack();
	ajaxObject.method = "GET";
	ajaxObject.requestFile = 'cart';	// Saving product in this file
	ajaxObject.setVar('__action',__action);
	ajaxObject.onCompletion = function(){ajaxValidateOrderGoods(ajaxObject);};	// Specify function that will be executed after file has been found
	ajaxObject.runAJAX();		// Execute AJAX function		
	}
}

function ajaxValidateOrderGoods(ajaxObject)
{
	alert(ajaxObject.response); //cart_count = cart_count-1;
	document.getElementById("cart_count").innerHTML = ajaxObject.response;
}

function CheckOrder(f)
{
	var err='';
	if (f.fio.value=='') err+="- «Фамилия И. О.»\n";
	if (f.phone.value=='') err+="- «Телефон»\n";
	if (f.address.value=='') err+="- «Адрес доставки»\n";

	if (err!='') {
		alert("Перед отправкой формы заполните следующие поля:\n\n"+err);
		return false;
	}
	if (f.valid_text.value != f.check_text.value)
    {
		alert("Пожалуйста, введите корректно текст с картинки!\n или нажмите обновить");
        return false;
    }
	return true;	
}

function UpdateImage(__action, id_web_user, id_hist_cart, check_text)
{
	if (checkCookie()) {
	var ajaxObject = new sack();
	ajaxObject.method = "GET";
	ajaxObject.requestFile = 'order';	// Saving product in this file
	ajaxObject.setVar('__action',__action);
	ajaxObject.setVar('check_text',check_text);
	ajaxObject.onCompletion = function(){ajaxValidateUpdateImage(ajaxObject, id_web_user, id_hist_cart);};	// Specify function that will be executed after file has been found
	ajaxObject.runAJAX();		// Execute AJAX function		
	}
}

function ajaxValidateUpdateImage(ajaxObject, id_web_user, id_hist_cart)
{
	var image_text = document.getElementById('image_text');
	f_order.valid_text.value = ajaxObject.response;
	image_text.src='../../find/images/'+f_order.valid_text.value+id_hist_cart+id_web_user+'.gif';
}
