function getBasket(id, delay)
{
	
	//$("#linebasketDiv").hide();
	if( delay == '0' ) {
	$("#linebasketDiv").load("/bitrix/includes/getlinebasketajax.php", {id: id, delay: delay}, function(){ $.prompt("Товар добавлен в корзину", {buttons:{}, timeout:2000}); });
	} else {
	$("#linebasketDiv").load("/bitrix/includes/getlinebasketajax.php", {id: id, delay: delay}, function(){ $.prompt("Товар добавлен в избранное", {buttons:{}, timeout:2000}); });
	}
	//$("#linebasketDiv").show("slow");
	return false;
	
}
function getBasketP(id, delay, color, size, model)
{
	
	//$("#linebasketDiv").hide();
	var gift = $('[name=S_GIFT]').val();
	if(!gift)
	gift=0;
	if( delay == '0' ) {
	$("#linebasketDiv").load("/bitrix/includes/getlinebasketajax.php", {id: id, delay: delay, size: size, color: color, gift: gift, model:model}, function(){ $.prompt("Товар добавлен в корзину", {buttons:{}, timeout:2000}); });
	} else {
	$("#linebasketDiv").load("/bitrix/includes/getlinebasketajax.php", {id: id, delay: delay, size: size, color: color, gift: gift, model: model}, function(){ $.prompt("Товар добавлен в избранное", {buttons:{}, timeout:2000}); });
	}
	//$("#linebasketDiv").show("slow");
	return false;
	
}
function delBasket(item, num)
{
	$.get("/bitrix/includes/delbasket.php", {id: item}, function()
	{
		$("#linebasketDiv").load("/bitrix/includes/getlinebasketajax.php");
		$.prompt("Товар удален из корзины", {buttons:{}, timeout:2000});
		$(num).remove();
		
	});
}
function delBasketOrderIn(item, num)
{
	$.get("/bitrix/includes/delbasket.php", {id: item}, function()
	{
		$.prompt("Товар удален из корзины", {buttons:{}, timeout:2000});
		$(num).remove();
		var cityid = $("#DELIVERY_LOCATION").val();
		//alert(cityid);
		$("#linebasketDiv").load("/bitrix/includes/getlinebasketajax.php");
		$.getJSON('/bitrix/components/inakrin/sale.ajax.locations/templates/order/getdeliver.php', {id: cityid}, function(data)
		 {
		
		      var idsel = $('[name=DELIVERY_ID]:checked').attr("id");
		      $("#deliverytable").show();
		      $('#DELIVERY_ID').html('');
		      for (i in data)
		      {
			$('#DELIVERY_ID').append('<input type="radio" id="deliver' + data[i].ID + '" onclick="loadBasketDelivery(this.value);" name="DELIVERY_ID" value="' + data[i].ID + '"><label for="deliver' + data[i].NAME + '">' + data[i].NAME + '</label><br/>');
			//varvar = '<tr> <td width="0%" valign="top"><input type="radio" checked="false" value="6" name="DELIVERY_ID" id="ID_DELIVERY_ID_' + data[i].ID + '"/></td><td width="100%" valign="top"><label for="ID_DELIVERY_ID_' + data[i].ID + '"><b>' + data[i].NAME + '</b></label></td></tr>';
			//$("#deliverytable").append(varvar);
		      }
		      $("#" + idsel).attr("checked", true);
		      loadBasketDelivery($("#" + idsel).val());
		});
		
	});
}
function toWBasket(item, num)
{
	$.get("/bitrix/includes/towbasket.php", {id: item}, function()
	{
		$.prompt("Товар перемещен в вишлист", {buttons:{}, timeout:2000});
		$(num).remove();
	});
}
function delBasketDelay()
{
	var spl =  new Array();
	for(i=0;i<$("[rel=deliverydelay]").length;i++)
	{
		
		if($("[rel=deliverydelay]")[i].checked==true)
		{
			//alert($("[rel=deliverydelay]")[i].name);
			spl.push($("[rel=deliverydelay]")[i].name.split("_")[1]);
			
			//
			
		}
		
	}
	$.get("/bitrix/includes/delbasket.php", {'id[]': spl}, function(){$("#linebasketDiv").load("/bitrix/includes/getlinebasketajax.php");});
	for(i=0;i<spl.length;i++)
	{
	  $("#"+spl[i]+'_TD').remove();
	}
	$.prompt("Отмеченные товары удалены", {buttons:{}, timeout:2000});
}

/*Array.prototype.in_array = function(p_val) {
	for(var i = 0, l = this.length; i < l; i++)	{
		if(this[i] == p_val) {
			return true;
		}
	}
	return false;
}
*/
function in_array(needle, haystack, strict) {	// Checks if a value exists in an array
	// 
	// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)

	var found = false, key, strict = !!strict;

	for (key in haystack) {
		if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
			found = true;
			break;
		}
	}

	return found;
}

function loadCityDelivery(cityid)
{
	var a = ['352','438','439','441','442','443','444','445','446','447','448','449','450','451','452','453','454','455','456','457','458','459','460','461','462','463','464','465','466','467','468','469','470','471','472','473'];
	
	if (cityid > 1){
		$("#orderbasketdiv").hide();
		$.getJSON('/bitrix/components/inakrin/sale.ajax.locations/templates/order/getdeliver.php', {id: cityid}, function(data)
		{
			//var varvar="";
			//alert(123);
			//$("#deliverytable > tbody > *:gt(0)").remove();
			$("#deliverytable").show();
			$('#DELIVERY_ID').html('');
			
			for (i in data)
			{
				if (
					(in_array(cityid, ['352','438','439','441','442','443','444','445','446','447','448','449','450','451','452','453','454','455','456','457','458','459','460','461','462','463','464','465','466','467','468','469','470','471','472','473']) == true && (data[i].ID == 11 || data[i].ID == 12 || data[i].ID == 13 || data[i].ID == 14 || data[i].ID == 15))
					|| 
					(in_array(cityid, ['261','325']) == true && (data[i].ID == 23 || data[i].ID == 24 || data[i].ID == 25 || data[i].ID == 26 || data[i].ID == 27 || data[i].ID == 33 || data[i].ID == 34 || data[i].ID == 2 || data[i].ID == 5 || data[i].ID == 6 || data[i].ID == 7 || data[i].ID == 8 || data[i].ID == 9))
				){
					
				} else{
					$('#DELIVERY_ID').append('<input type="radio" id="deliver' + data[i].ID + '" onclick="loadBasketDelivery(this.value);" name="DELIVERY_ID" value="' + data[i].ID + '"><label for="deliver' + data[i].NAME + '">' + data[i].NAME + '</label><br/>');
				//varvar = '<tr> <td width="0%" valign="top"><input type="radio" checked="false" value="6" name="DELIVERY_ID" id="ID_DELIVERY_ID_' + data[i].ID + '"/></td><td width="100%" valign="top"><label for="ID_DELIVERY_ID_' + data[i].ID + '"><b>' + data[i].NAME + '</b></label></td></tr>';
				//$("#deliverytable").append(varvar);
				}
			}
		});
	} else {
		$("#deliverytable").hide();
		$('#DELIVERY_ID').html('');
	}
}
function loadBasketDelivery(delid)
{
  var cityid =$("#DELIVERY_LOCATION").val();
  $("#orderbasketdiv").hide("slow");  
  $("#orderbasketdiv").load("/bitrix/includes/getdeliverybasket.php", {id: delid, locationid:cityid}, function(){
		$("#orderbasketdiv").show("slow").find('.gifts').next('ul').css({'display':'none'});
		$('.gifts').click(function(){
			$(this).next('ul').slideToggle('fast');
		});
	});
}
function subscribeU(id)
{
	$.getJSON('/bitrix/includes/subscribe.php', {id: id}, function(data)
	{
		if(data.ok==1){
		$.prompt("Bы подписаны на рассылку", {buttons:{}, timeout:2000});
		$("#subscr" + id).text("Отписаться");
		}else if(data.ok==0)
		{
		$.prompt("Bы отписаны от рассылки", {buttons:{}, timeout:2000});
		$("#subscr" + id).text("Подписаться");
		}
	});
	
}
function subscribeUall()
{
	$.getJSON('/bitrix/includes/subscribe.php', {id: "all", s: 1}, function(data)
	{
		$.prompt("Bы подписаны на все рассылки", {buttons:{}, timeout:2000});
		$("[rel=subscr]").text("Отписаться");
	});
}
function unsubscribeUall()
{
	$.getJSON('/bitrix/includes/subscribe.php', {id: "all", s: 0}, function(data)
	{
		$.prompt("Bы отписаны от всех рассылок", {buttons:{}, timeout:2000});
		$("[rel=subscr]").text("Подписаться");
	});
}
function beforeSubmitAuth(form)
{
	//alert(form["USER_PASSWORD"].value);
	//return false;
	$.getJSON('/bitrix/includes/jsonauth.php', {login: form["USER_LOGIN"].value, password: form["USER_PASSWORD"].value}, function(data)
	{
		if(data.auth==true)
		{
			form.submit();
		}else
		{
			$("[rel=regfalse]").show();
			return false;
		}
	});
	return false;
	
	
}

	

function unCheckAllBIO(cbox, checkedt, baskid)
{
	
	var giftid = 0;
	var cityid =$("#DELIVERY_LOCATION").val();
	$("[rel=basketgift]").attr("checked", false);
	if(checkedt){
	cbox.checked = true;
	giftid=cbox.value;
	}
	$.getJSON('/bitrix/components/inakrin/sale.ajax.locations/templates/order/getdeliver.php', {id: cityid, basket:baskid, gift:giftid}, function(data)
	{
		
		//alert(123);
		//$("#deliverytable > tbody > *:gt(0)").remove();
		var idsel = $('[name=DELIVERY_ID]:checked').attr("id");
		var idval = $('[name=DELIVERY_ID]:checked').val();
		var cityid =$("#DELIVERY_LOCATION").val();
		$("#deliverytable").show();
		$('#DELIVERY_ID').html('');
		for (i in data)
		      {
			$('#DELIVERY_ID').append('<input type="radio" id="deliver' + data[i].ID + '" onclick="loadBasketDelivery(this.value);" name="DELIVERY_ID" value="' + data[i].ID + '"><label for="deliver' + data[i].NAME + '">' + data[i].NAME + '</label><br/>');
		      }
		      $("#" + idsel).attr("checked", true);
		$("#orderbasketdiv").hide("slow");  
  $("#orderbasketdiv").load("/bitrix/includes/getdeliverybasket.php", {id: idval, locationid:cityid}, function(){
		$("#orderbasketdiv").show("slow").find('.gifts').next('ul').css({'display':'none'});
		$('.gifts').click(function(){
			$(this).next('ul').slideToggle('fast');
		});
	});
	});
}
function unCheckAllBIOSimple(cbox, checkedt, baskid)
{
	
	var giftid = 0;
	var cityid =$("#DELIVERY_LOCATION").val();
	$("[rel=basketgift]").attr("checked", false);
	if(checkedt){
	cbox.checked = true;
	giftid=cbox.value;
	}
}
function unCheckAllBIOSimpleOrder(cbox, checkedt)
{
	
	var giftid = 0;
	//var cityid =$("#DELIVERY_LOCATION").val();
	$("[rel=ordergift]").attr("checked", false);
	if(checkedt){
	cbox.checked = true;
	giftid=cbox.value;
	}
}
function handleRadio(rd) {
  if(rd.preval) rd.checked=false;
  return true;
}

function selRegion(regtype)
{
  if(regtype=="reg")
  {
      $("#id9").hide();
      return;
  }
  $.getJSON("/bitrix/includes/getlocations.php", {id:regtype}, function(data){
    var sel = document.getElementById("id9");
    sel.options.length = 0;
    //alert(sel);
    $("#id9").show();
    for(i in data.cities)
    {
	sel.options[sel.options.length] = new Option(data.cities[i].NAME);
    }
});
}


function selRegionInOrder(regtype)
{
  if(regtype=="reg")
  {
      $("#DELIVERY_LOCATION").hide();
	  //$("#id5").attr('disabled','disabled');
      return;
  }
  $.getJSON("/bitrix/includes/getlocations.php", {id:regtype}, function(data){
    var sel = document.getElementById("DELIVERY_LOCATION");
    sel.options.length = 0;
    //alert(sel);
    $("#DELIVERY_LOCATION").show();
    for(i in data.cities)
    {
		if (data.cities[i].ID == '333')
			{sel.options[sel.options.length] = new Option(data.cities[i].NAME, data.cities[i].ID, false, true);}
		else
			{sel.options[sel.options.length] = new Option(data.cities[i].NAME, data.cities[i].ID);}
    }
	
    loadCityDelivery($("#DELIVERY_LOCATION").val());
	
	//if($('#DELIVERY_LOCATION').val() <= 0 || $('#DELIVERY_LOCATION').val() == ''){ $("#id5").attr('disabled','disabled');} else {$("#id5").removeAttr('disabled');}
});
}