//-----------------------------------------------
// КЛАССЫ ДЛЯ ОПИСАНИЯ ДОСТАВКИ С ИСПОЛЬЗОВАНИЕМ КОНФИГУРАЦИЙ в XML
//-----------------------------------------------
//var DeliveryMaxInt=2000000000;
var DeliveryMaxInt=1000000;
// конструктор класса доставки, которые содержат конфигурации
function DeliveryObj()
{
	this.length=0;
	this.DeliveryArray = new Array();
	this.Add = AddDelivery;
	this.Get = GetDelivery;
	this.View4Basket = ViewDelivery4Basket;
	this.View4Order = ViewDelivery4Order;
	this.View4Goods = ViewDelivery4Goods;
	//this.GetCurDelivery = GetCurDelivery;
}

// конструктор класса элемента доставки из массива DeliveryArray класса DeliveryObj
function DeliveryItemObj(ShipID, ShipName, ShipPrice, ShipMinPrice, ShipMaxPrice, ShipFullName, ShipPrintInvoice)
{
	this.length = 0;
	this.ShipID = ShipID;
	this.ShipName = ShipName;
	this.ShipPrice = parseInt(ShipPrice);
	if (isNaN(this.ShipPrice)) this.ShipPrice = 0;
	this.ShipPriceStr = ((this.ShipPrice==0)?'Бесплатно':(this.ShipPrice + ' руб.'));
	this.ShipMinPrice = parseInt(ShipMinPrice);
	this.ShipMaxPrice = (parseInt(ShipMaxPrice)==1)?DeliveryMaxInt:parseInt(ShipMaxPrice);
	this.ShipFullName = ShipFullName.replace(";","<br/>");
	this.ShipPrintInvoice = ShipPrintInvoice;
}

// добавление доставки в массив DeliveryArray класса DeliveryObj
function AddDelivery(ShipID, ShipName, ShipPrice, ShipMinPrice, ShipMaxPrice, ShipFullName, ShipPrintInvoice)
{ 
	this.DeliveryArray[this.length++] = 
		new DeliveryItemObj(ShipID, ShipName, ShipPrice, ShipMinPrice, ShipMaxPrice, ShipFullName, ShipPrintInvoice);
}

// Получение элемента доставки по ИД
function GetDelivery(ShipID)
{ 
	var i;
	for(i=0;i<this.length;i++){
		if (this.DeliveryArray[i].ShipID==ShipID) return this.DeliveryArray[i]; 
	}
	return null;
}

//Вывод таблицы доступных доставок в корзине.
//Если сумма корзины попадает в интервал ShipMinPrice - ShipMaxPrice
function ViewDelivery4Basket(sBasketSumm)
{
	var BasketSumm = parseInt(sBasketSumm);
//alert("sBasketSumm = " + sBasketSumm);
	var retStr = '';
	var ShipID, ShipPrice;
	for(i=0;i<this.length;i++){
		if (BasketSumm>=this.DeliveryArray[i].ShipMinPrice && ((BasketSumm<=this.DeliveryArray[i].ShipMaxPrice && this.DeliveryArray[i].ShipMaxPrice!=DeliveryMaxInt) || this.DeliveryArray[i].ShipMaxPrice==DeliveryMaxInt)
			&& this.DeliveryArray[i].ShipPrice!=1){
			ShipID = this.DeliveryArray[i].ShipID;
//alert("ShipID=" + ShipID + ", ShipMinPrice=" + this.DeliveryArray[i].ShipMinPrice + ", ShipMaxPrice=" + this.DeliveryArray[i].ShipMaxPrice + ", ShipPrice=" + this.DeliveryArray[i].ShipPrice);
			retStr+='<tr>'
				+ '<td>' + this.DeliveryArray[i].ShipFullName + '</td>'
				+ '<td nowrap>' + this.DeliveryArray[i].ShipPriceStr + '</td>'
				+ '<td> <input type="radio" name="shippmode" value="'+ShipID+'" onclick="set_Delivery('+ShipID+',\''+sBasketSumm+'\');"></td> </tr>';
		}
	}
	document.write(retStr);
}


//Вывод таблицы доступных доставок в корзине.
//Если сумма корзины попадает в интервал ShipMinPrice - ShipMaxPrice
function ViewDelivery4Order(ShipID)
{
	var retStr = '';
	var DeliveryItem = this.Get(ShipID);
	if (DeliveryItem!=null)
	{
			retStr='<tr>'
				+ '<td>' + DeliveryItem.ShipFullName + '</td>'
				+ '<td align="center">' + DeliveryItem.ShipPriceStr + '</td>';
	}
	document.write(retStr);
}

//Вывод таблицы доступных доставок в корзине.
//Если сумма корзины попадает в интервал ShipMinPrice - ShipMaxPrice
function ViewDelivery4Goods(sSumm)
{
	var Summ = parseInt(sSumm);
	var retVal = '';
	var ShipID, ShipPrice=-1;

	for(i=0;i<this.length;i++){
		if (Summ>=this.DeliveryArray[i].ShipMinPrice && 
// Summ<=this.DeliveryArray[i].ShipMaxPrice
((Summ<=this.DeliveryArray[i].ShipMaxPrice && this.DeliveryArray[i].ShipMaxPrice!=DeliveryMaxInt) || this.DeliveryArray[i].ShipMaxPrice==DeliveryMaxInt)
			&& this.DeliveryArray[i].ShipPrice!=1
			&& this.DeliveryArray[i].ShipPrintInvoice
			&& this.DeliveryArray[i].ShipFullName.indexOf("России")!=-1
			)
		{
			return  this.DeliveryArray[i].ShipPrice;
		}
	}
}


//--------------------------------
// ОБЪЯВЛЕНИЕ ОБЪЕКТОВ
//--------------------------------
var Delivery = new DeliveryObj();


//--------------------------------
// ДОПОЛНИТЕЛЬНЫЙ ФУНКЦИОНАЛ
//--------------------------------
function set_form(v_choice)
{
	var regform=document.getElementById("regform");
	if (regform!=null) 
	{
		var shippmode = get_radio(regform.shippmode);
		if (shippmode=='' && regform.shippmode.value!=null) shippmode = regform.shippmode.value;
		if (v_choice!='' && shippmode=='' ) 
		{ 
			alert("Выберите, пожалуйста, способ доставки и оплаты."); 
			set_radio(regform.choice,"");
			return; 
		}

		set_radio(regform.choice,v_choice);
		regform.choice.value=v_choice;
		if (v_choice==1)
		{
			s_div('d_reg');h_div('d_login');s_div('d_order');clear_authorise_form();
			regform.c.value="";
		}
		else if (v_choice==2)
		{
			s_div('d_login');h_div('d_reg');s_div('d_order');
			regform.c.value="service";
		}
	}
}

function clear_authorise_form(){
	var regform=document.getElementById("regform");
	if (regform!=null)
	{
		//regform.orderlogin.value='';
		//regform.orderpass.value='';
		regform.login.value='';
		regform.pass.value='';
	}
}

function s_div(id_cur){
	var div_set = document.getElementById(id_cur);
	if (div_set!=null) div_set.style.display="block";
}

function h_div(id_cur){
	var div_set = document.getElementById(id_cur);
	if (div_set!=null) { div_set.style.display="none"; }
}

function set_Delivery(v_shippmode, sSumm)
{
	var infoD = document.getElementById("infoD");
	var summD = document.getElementById("summD");
	var strInfo = "Доставка не выбрана";
	var DeliveryItem = Delivery.Get(v_shippmode);
	var ShipPrice = 0;
	var regform=document.getElementById("regform");

	if (regform!=null) set_radio(regform.shippmode,v_shippmode);
	if (infoD!=null && summD!=null)
	{
		if (DeliveryItem!=null)
		{
			strInfo = "Итого с доставкой:";
			ShipPrice = parseInt(sSumm);
			if (isNaN(ShipPrice)) ShipPrice = 0;
			ShipPrice += DeliveryItem.ShipPrice;
			changeText(summD, roundprice2(ShipPrice) + " руб.");


			infoD = document.getElementById("bask_choose");
			if (infoD!=null)
			{
				infoD.setAttribute("className","bask_chooseGR");
				infoD.className = "bask_chooseGR";
			}
			else
				alert("ОШИБКА set_Delivery: не могу найти элемент по ид 'bask_choose'");
		}
		changeText(infoD, strInfo);
	}
}
