
function showInvioceEditor() {
	var top = document.body.scrollTop ;
	$('invoice_editot').style.top = top + 300 + "px";
	$show('invoice_editot');
}

function hideLoadingIndicator() {
	$hide('loading_bar');
}


//buyStuff('', 'http://ved/', 'Страховочная система <Navaho Boss> ', 'Цена руб.', '12200', '', '', 161, 'cnt_161')

function buyStuff( options, serv, name_external, prise_sense, discount_sense, entity_id, field_name, sid, hs){
//alert (' buyStuff Work   ');
   	showLoadingIndicator();
	if (options) {
		if (options.error > 0) {
   			hideLoadingIndicator();
			alert ('Ошибка' + options.error_desc);
		} else {
   			hideLoadingIndicator();
			alert ('Ващ товар: ' + options.stuff + ' добавлен в карзину ');
		}
		return;
	}
	var countStuff = parseFloat($(field_name).value);
	if ( !countStuff ) {
		alert ('введите, пожалуйста, количество товара. ');
		return;
	}
//	alert (' entity_id =  ' + entity_id + ' field_name =  ' + field_name  + ' countStuff =  ' +  countStuff );
    SendRequestByAjax( serv+'tools.php', 'prc=buy&ne='+ name_external + '&ps=' + prise_sense + '&ds=' + discount_sense + '&id=' + entity_id + '&cnt=' + countStuff + '&sid=' + sid + '&hs=' + hs, buyStuff);
}

function stuffInvoice( options, serv, sid, hs){
   	showLoadingIndicator();
//	alert (' stuffInvoice work Now serv = ' + serv );
	if (options) {
		if (options.error > 0) {
   			hideLoadingIndicator();
			alert ('Ошибка!  ' + options.error_desc);
		} else {
//			alert ('work Now serv = ' + options.serv );
   			hideLoadingIndicator();
			invoiceWindow( options.serv, 'Счет Form ', options.tableForm );
			calculateCost($('stuffCount').value);
		}
		return;
	}

    SendRequestByAjax( serv+'tools.php', 'prc=invoice&sid=' + sid + '&hs=' + hs + '&srv=' + serv, stuffInvoice);
}


function calculateCost(sCnt) {
	var maxCou = parseInt(sCnt);
	var i = 1;
	var fullSum = 0;
	if (sCnt >0) {
		do {
	//alert (' row_ = ' +  $('row_'+(i)).style.display.toString() );
			if ($('row_'+(i)).style.display != 'none') {
	//alert (' row_ = ' +  'row_'+(i) );
				var ssumm = $('prise_'+(i)).value * $('stuffCount_'+(i)).value;
	//alert (' ssumm = ' +  ssumm );
				var disc = $('discount_'+(i)).value;
	//alert (' disc = ' +  disc );
				var sum_1 = (ssumm-ssumm/100*disc)*100;
	//alert (' sum_1 = ' +  sum_1 );
				var outSumm = Math.round(sum_1)/100;
	//alert (' outSumm = ' +  outSumm );
				fullSum = fullSum + outSumm;
	//alert (' fullSum = ' +  fullSum );
				$('sum_'+(i)).innerHTML = outSumm;
			}
			i++;
	//alert (' maxCou = ' +  maxCou + '; i= ' + i  );
		} while (i <= maxCou)
	//alert (' fullSum = ' +  fullSum );
		$('fullSum').innerHTML = Math.round(fullSum*100)/100 + ' руб';
	}
//	return fullSum;
} // function picWindow


function invoiceWindowClose() {
	$hide('info_bar');
}

function invoiceWindow( serv, title, value ) {
//	var top = document.body.scrollTop ;
//	var x = parseInt( getBodyScrollLeft() );
	var y = parseInt(getBodyScrollTop() ) ;
	$('info_bar').style.top = y + 50 + "px";
	$('info_bar').style.left = 200 + "px";
	$show('info_bar');
//	$('info_bar').innerHTML = '<div id="invoiceForm">'+value+ '</div>';
	$('info_bar').innerHTML = '<table border="0" cellpadding="0" cellspacing="0"><tr><td><div id="invoiceForm">'+value+ '</div></td></tr></table>';
	var x_size_body = parseInt($('invoiceForm').offsetWidth) + 10;
	$('info_bar').style.width = x_size_body + "px";
	if (bIE==1) {
		var y_size_body = parseInt($('invoiceForm').offsetHeight);
//alert ( 'invoiceWindow:: y_size_body ' + y_size_body );
		$('info_bar').style.height = y_size_body + "px";
	}

} // function invoiceWindow

function stuffInvoice2( options, serv, sid, hs){
	//alert ( 'stuffInvoice2 begin ' );
   	showLoadingIndicator();
	if (options) {
		if (options.error > 0) {
   			hideLoadingIndicator();
			alert ('Ошибка!  ' + options.error_desc);
		} else {
   			hideLoadingIndicator();
			$('invoiceForm').innerHTML = options.tableForm;
			$('info_bar').doScroll("pageDown");
		}
		return;
	}

   	sSnt = parseInt($('stuffCount').value);
	dataArray = new Array();
	if (sSnt >0) {
		var i=0;
		do {
	//alert (' stuffInvoice2 name_1 = ' + name + '; $(name).value = ' +  $(name).value );
			dataArray[i] = new Array();
			dataArray[i][0] = i;
			stuffCount = 'stuffCount_'+(i+1);
			if ($('row_'+(i+1)).style.display == 'none') {
				dataArray[i][1] = 0;
			} else {
				dataArray[i][1] = $(stuffCount).value;
			}
			stuffId = 'stuffId_'+(i+1);
			dataArray[i][2] = $(stuffId).value;
			i++;
		} while (i < sSnt)
	}


	var dataJson = Json.toString(dataArray);
//alert (' stuffInvoice2 work Now serv = ' + serv + ' ' + sid +  ' ' + hs + ' ' + dataJson.toString());
    SendRequestByAjax( serv+'tools.php', 'prc=invoice2&sid=' + sid + '&hs=' + hs + '&srv=' + serv + '&cnt='+sSnt+ '&dta=' + dataJson , stuffInvoice2);
}

function stuffInvoiceSend( options, serv, sid, hs){
//alert ( 'stuffInvoiceSend Begin ' );
   	showLoadingIndicator();
	if (options) {
		if (options.error > 0) {
   			hideLoadingIndicator();
			alert ('Ошибка!  ' + options.error_desc);
		} else {
   			hideLoadingIndicator();
			$('invoiceForm').innerHTML = options.tableForm;
		}
		return;
	}


   	sSnt = parseInt($('stuffCount').value);
//alert (' stuffInvoice2 sSnt = ' + sSnt );
	dataArray = new Array();
	if (sSnt >0) {
		var i=0;
		do {
	//alert (' stuffInvoice2 name_1 = ' + name + '; $(name).value = ' +  $(name).value );
			dataArray[i] = new Array();
			dataArray[i][0] = i;
			stuffCount = 'stuffCount_'+(i+1);
			if ($('row_'+(i+1)).style.display == 'none') {
				dataArray[i][1] = 0;
			} else {
				dataArray[i][1] = $(stuffCount).value;
			}
			stuffId = 'stuffId_'+(i+1);
			dataArray[i][2] = $(stuffId).value;
			i++;
		} while (i < sSnt)
	}

	var dataJson = Json.toString(dataArray);

	var customerDataArray = new Array();
	var errorStr = '';

//alert (' stuffInvoice2  errorStr = ' + errorStr.toString());

	customerDataArray[0] =$('customerName').value;
	if (customerDataArray[0].length == 0) {
		errorStr = '- имя покупателя <br>';
	}
	customerDataArray[1] =$('get_contact_phon').value;
	customerDataArray[2] =$('get_contact_fax').value;
	customerDataArray[3] =$('get_contact_e_mail').value;
	if ((customerDataArray[3].length == 0) || (!isValidEmail(customerDataArray[3]))) {
		errorStr += '- e-mail покупателя <br>';
	}
	customerDataArray[4] =$('paid_form').value;
	if (customerDataArray[4].length == 0) {
		errorStr += '- форму оплаты покупки <br>';
	}
	customerDataArray[5] =$('delivery_form').value;
	if (customerDataArray[5].length == 0) {
		errorStr += '- метод доставки <br>';
	}
	customerDataArray[6] =$('ph_addr').value;
	if (customerDataArray[6].length == 0) {
		errorStr += '- адрес доставки <br>';
	}
	customerDataArray[7] =$('get_wrk_term').value;
	customerDataArray[8] =$('get_comments').value;

//alert (' stuffInvoice2  customerDataArray = ' + customerDataArray.toString());
//alert (' stuffInvoice2  errorStr = ' + errorStr.toString());

	if (sSnt == 0) {
		hideLoadingIndicator();
		$('ErorrZone').innerHTML = 'Вы не выбрали ни одного товара для покупки. <br>';
		return false;
	}
	if (errorStr.length > 0) {
		hideLoadingIndicator();
		$('ErorrZone').innerHTML = 'введите, пожалуйста:<br>' + errorStr;
		return false;
	}


	var customerDataJson = Json.toString(customerDataArray);
//alert (' stuffInvoice2 work Now serv = ' + serv + ' ' + sid +  ' ' + hs + ' ' + dataJson.toString());
    SendRequestByAjax( serv+'tools.php', 'prc=invoiceOut&sid=' + sid + '&hs=' + hs + '&srv=' + serv + '&cnt='+sSnt+ '&csr=' + customerDataJson+ '&sj=' + dataJson , stuffInvoice2);
}

function delRowStuff( stuffId, order) {

   	showLoadingIndicator();
//	$('stuffCount_'+order).innerHTML = 0;
	$hide('row_'+order);
	calculateCost($('stuffCount').value);
	hideLoadingIndicator();
	return false;

} // function delRowStuff


//reviewItems( '', 'http://<!--{$SERVER_PATCH}-->', 'info_bar', '1', '0',  '<!--{$dt1}-->', '<!--{$hs1}-->' );
/**
 * divTargetName - куда отдаем полученное 
 * itemsId - ИД
 */
function reviewItems( options, servReq, itemsId, dt, hs ) {
	showLoadingIndicator();
	if (options) {
		hideLoadingIndicator();
		if (options.error > 0) {
			alert ('Ошибка!  ' + options.error_desc);
		} else {
//alert ('make remove_focus_select!!!!  options.tableForm = ' + options.caseFieldForm );
			var top = document.body.scrollTop ;
//			$('invoiceForm').innerHTML = options.itemBody;
			invoiceWindow( servReq, '123', options.itemBody );
		}
		return;
	}
//alert ('make remove_focus_select!!!!  serv = ' + serv)
	var requestServ = 'http://' + servReq+'/tools.php';
	var requestParam = 'prc=gits&sid=' + itemsId +  '&dt=' + dt  + '&hs='+ hs;
//alert ('make  ' + requestServ + '?' +  requestParam);
	SendRequestByAjax( requestServ, requestParam, reviewItems);
}

