﻿<!--Для работы с кнопками Product Contener с селектом-->
function inner(id, html)
{
var name_input = document.getElementById(id);
name_input.innerHTML = html;
}

function onSelectChange(selectEl) {
	onOptionsChange(selectEl.id.substring(6));
}

function onAmountChange(amountEl) {
	onOptionsChange(amountEl.id.substring(6));
}

function onCountMinus(countEl) {
	onCountChangeMinus(countEl.id.substring(12));
	onOptionsChange(countEl.id.substring(12));
}

function onCountPlus(countEl) {
	onCountChangePlus(countEl.id.substring(12));
	onOptionsChange(countEl.id.substring(12));
}

function onCountChangeMinus(id){
	if (document.getElementById( "amount" + id ).value>0)
	{document.getElementById( "amount" + id ).value=document.getElementById( "amount" + id ).value-1;}
	else
	{document.getElementById( "amount" + id ).value=0;}
}

function onCountChangePlus(id){
	document.getElementById( "amount" + id ).value=+(document.getElementById( "amount" + id ).value)+1;
}

function onOptionsChange(id){
	var selectEl=document.getElementById( "select" + id );
	var price = getElemByVal( selectEl,selectEl.value ).getAttribute("xprice");
	var amount = document.getElementById( "amount" + id ).value;
	document.getElementById( "price" + id ).innerHTML = price;
	var costEl = document.getElementById( "cost" + id )
	if ( costEl ) { costEl.innerHTML = amount * price; }
}
function getElemByVal(parent,value){
	var els = parent.getElementsByTagName("*");
	var elsLen = els.length;
	var El=null;
	for ( var i = 0; i < elsLen ; i++ ) {
		if (els[i].value == value) {
			El = els[i]; 
			i = elsLen;
		}
	}
	return El;
}
<!------------------------------------------------------------------------------------>

<!--Для работы с кнопками Product Contener без селекта-->
function CountMinus(countEl) {
	onCountChangeMinus(countEl.id.substring(12));
	costChange(countEl.id.substring(12));
}
function CountPlus(countEl) {
	onCountChangePlus(countEl.id.substring(12));
	costChange(countEl.id.substring(12));
}
function AmountChange(amountEl) {
	costChange(amountEl.id.substring(6));
}
function costChange(id) {
	var price = $('#price'+id).text();
	var amount = $('#amount'+id).val();
	var costEl = document.getElementById( "cost" + id )
	if ( costEl ) { costEl.innerHTML = amount * price; }
}
<!------------------------------------------------------------------------------------>

function findPosx( a ) {
	var b = {};
	b.x = 0;
	b.y = 0;
	if (a.offsetParent) {
		while (a.offsetParent) {
			b.y += a.offsetTop;
			b.x += a.offsetLeft;
			a = a.offsetParent;
		}
	} else {
		if (a.x)
			b.x += a.x;
		if (a.y)
			b.y += a.y
	}
	return b;
}

function addToCart(prodId, prodKol, buttonEl){
	var msgEl = document.getElementById( "msgcart" );
	data='<img width="25px" height="25px" src="/template/images/loader.gif" />';
	msgEl.innerHTML = data;
	var pos = findPosx( buttonEl );
	msgEl.style.left = (pos.x - 40)+'px';
	msgEl.style.top = (pos.y - 70)+'px';
	msgEl.style.visibility = "visible";
	data='<b>Товар добавлен в корзину</b>';
	data+='<table width="0%" border="0" cellpadding="2px" cellspacing="5px"><tr>';
    data+='<td width="90px" align="center" style="border: 1px outset; font-weight: bold; cursor: pointer; background-color: rgb(232, 253, 215);" onclick="document.location.href=' + "'/internet_magazin/cart/'" + '">Оформить заказ</td>';
    data+='<td width="90px" align="center" style="border: 1px outset; font-weight: bold; cursor: pointer; background-color: rgb(232, 253, 215);" onclick="msgcartHide()">Продолжить выбор</td></tr></table>';
	$.ajax({
	url: '/internet_magazin/addToCart.php?prod_id='+prodId+'&prod_kol='+prodKol,
    dataType : "html",
    success: function (d, textStatus) {
		msgEl.innerHTML = data;
		$('.cost_total').html(d);
    }
	});
}

function changeCart(input) {
	var prodId=input.id.substring(7);
	<!--Формируем строку в которой оставляем только цифры-->
	str=input.value;
	var prodKol='';
	for ($i=0; $i<str.length; $i++) {
		if ('0123456789'.indexOf(str.charAt($i))!=-1) {prodKol+=str.charAt($i)}
	}
	input.value=prodKol;
	<!------------------------------------------------------>
	var cost=document.getElementById('cost_'+prodId);
	data='<img width="20px" height="20px" src="/template/images/loader.gif" />';
	cost.innerHTML = data;
	$.ajax({
	url: '/internet_magazin/changeCart.php?prod_id='+prodId+'&prod_kol='+prodKol,
    dataType : "html",
    success: function (d, textStatus) {
		cost.innerHTML = $('#price_'+prodId).text()*prodKol;
		$('.cost_total').html(d);
    }
	});
}

function addToCartSelect(buttonEl){
	var id=buttonEl.id.substring(6);
	var selectEl=document.getElementById( "select" + id );
	var optionsEl=getElemByVal( selectEl,selectEl.value );
	var prodId = optionsEl.getAttribute("xprodId");
	var amountEl = document.getElementById( "amount" + id );
	addToCart(prodId, amountEl.value, buttonEl);
}

function addToCartAmount(buttonEl){
	var id=buttonEl.id.substring(6);
	var amountEl = document.getElementById( "amount" + id );
	addToCart(id, amountEl.value, buttonEl);
}

function msgcartHide() {
	var msgEl = document.getElementById( "msgcart" );
	msgEl.style.visibility = "hidden";
}

function deleteRaw(delrow) {
	var prodId=delrow.id.substring(4);
	var delrow=document.getElementById( "raw_" + prodId );
	delrow.parentNode.removeChild(delrow);
	if ($('.Raw_prod').length==0) {$('#cart_div').html('');}
	$.ajax({
	url: '/internet_magazin/changeCart.php?prod_id='+prodId+'&prod_kol=0',
	dataType : "html",
    success: function (d, textStatus) {
		$('.cost_total').html(d);
    }
	});
}

function checkForm() {
	if (!$('input[name=order_fio]').val()) {alert('Не заполнено поле "Ваше имя"'); } else {
		if (!$('input[name=order_phone]').val()) {alert('Не заполнено поле "Контактный телефон"'); } else {
			if (!$('input[name=order_email]').val()) {alert('Не заполнено поле "Контактный email"'); } else {
				$('#orderForm').submit();
			};
		};
	};
}

function ustanovka_cen() {
	$.ajax({
	url: '/admin/ustanovka_cen.php',
	dataType : "html",
    success: function (d, textStatus) {
		alert('Цены на композиции заполнены');
    }
	});
}

function raschet_dostavki() {
<!--Чтобы не вводили числа больше 23-->
if (Number($('#eITDbegintime').val().substring(0,2))>23) {$('#eITDbegintime').val('__:__');}
if (Number($('#eITDendtime').val().substring(0,2))>23) {$('#eITDendtime').val('__:__');}
	if (($('#eITDbegintime').val().indexOf('_')==-1)&($('#eITDendtime').val().indexOf('_')==-1)&($('#eITDbegintime').val().length!=0)&($('#eITDendtime').val().length!=0)) {
		time_begin=Number($('#eITDbegintime').val().substring(0,2))*60 + Number($('#eITDbegintime').val().substring(3));
		time_end=Number($('#eITDendtime').val().substring(0,2))*60 + Number($('#eITDendtime').val().substring(3));
		dostavka_date=$('#datepicker').val();
		$.ajax({
		url: '/dostavka/raschet_dostavki.php?time_begin='+time_begin+'&time_end='+time_end+'&dostavka_date='+dostavka_date,
    	dataType : "html",
    	success: function (d, textStatus) {
			$('#costDostavka').val(d);
    	}
		});
	}
	else {$('#costDostavka').val('')}
}

function dostavka_podrobnee(el) {
	dostavka_date=$('#datepicker').val();
	var msgEl = document.getElementById( "dostavka_podrobnee" );
	data='<img width="20px" height="20px" src="/template/images/loader.gif" />';
	msgEl.innerHTML = data;
	var pos = findPosx(el);
	msgEl.style.left = (pos.x + 20)+'px';
	msgEl.style.top = (pos.y)+'px';
	msgEl.style.visibility = "visible";
	$.ajax({
	url: '/dostavka/dostavka_podrobnee.php?dostavka_date='+dostavka_date,
    dataType : "html",
    success: function (d, textStatus) {
		$('#dostavka_podrobnee').html(d);
    }
	});
}

function element_hidden(id) {
	var El = document.getElementById(id);
	El.style.visibility = "hidden";
}

function callme() {
if ($("#callme_table").css('display')=='none') {
	var x = '';
	for (var i=1; i<=64; i++) {
		rnd = Math.random()*15;
		rnd = Math.round(rnd);
		rnd = Number(rnd).toString(16);
		x = x + rnd;
	}
	$("[name=captcha_value]").val('');
	$("[name=phone]").val('');
	$("#callme_pic").html("<img width='80px' height='40px' src='http://universe.uiscom.ru/centrex/callme/get_captcha_image/?key="+x+"' />");
	$("[name=captcha_key]").val(x);
	$("#callme_message").css('display', 'none');
	$("#callme_table").css('display', 'block');
}
else {callme_close('callme_table');}
}

function callme_close(div_id) {$("#"+div_id).css('display', 'none');}
function callme_open(div_id) {$("#"+div_id).css('display', 'block');}

function callme_send() {
var h = '_X8ffbsgkwI9o4G51OGnncZiDgn7s24rYaHcm0tHGbP4eu9fhSDF4BXGUkMllPNg';
var captcha_key = $("[name=captcha_key]").val();
var captcha_value = $("[name=captcha_value]").val();
var phone = $("[name=phone]").val();
$.ajax({
    url: '/myservice/callme_send.php?captcha_key='+captcha_key+'&captcha_value='+captcha_value+'&h='+h+'&phone='+phone,
    dataType : "html",
    success: function (data, textStatus) {
		inner('callme_message',data);
		callme_open('callme_message');
		if (data=='Код введен не верно.') {callme_close('callme_table'); callme(); $("#callme_message").css('display', 'block');}
		if (data=='Ожидайте звонок') {setTimeout("callme_close('callme_message'); callme_close('callme_table')",10000);}
    }
});
}

