function JSend()
{
	var _this = this;
	this.uid = 'jSend';
	this.options = {};
	this.bSend = false;
	this.url = null;
	this.data = null;
	this.callback = null;
	this.method = null;
	this.idTimeout = 0;
	this.timeout = 2000

	this.shiftObj = function(obj){
		var arr=[];
		if(_this.sizeObj(obj)){
			var i = null;
			for(i in obj){break}
			arr = obj[i]
			delete obj[i]
		}
		return arr;
	}

	this.sizeObj = function(obj){
		var n=0;
		for(var i in obj){n++;}
		return n;
	}

	this.count = function()	{
		return _this.sizeObj(_this.options)
	}

	this.send = function(){
		if(!_this.bSend && _this.sizeObj(_this.options) ){
			_this.bSend = true
			showImgLoader();
			var arrJ = _this.shiftObj(_this.options)
			_this.url      = arrJ[0];
			_this.data     = arrJ[1];
			_this.callback = arrJ[2];
			_this.method   = arrJ[3];
			$.ajax({
				type: _this.method,
				url:  _this.url,
				data: _this.data,
				cache: false,
				success:_this.callback
			});
		}

		if(_this.sizeObj(_this.options)){
			clearTimeout(_this.idTimeout)
			_this.idTimeout = setTimeout(_this.send, 150)
		}
	}

	this.setOptions = function(url, data, callback, method)	{
		_this.options[_this.uid] = [url,data,callback,method]
		//_this.options.push([url,data,callback,method])
	}

	this.setUID = function(uid)	{
		_this.uid = (uid?uid:'jSend');
	}

	this.sendData = function(url, params, callback, method, uid){
		_this.setUID(uid)
		_this.setOptions(url, params, callback, method)
		_this.sendTimeout()
	}

	this.sendTimeout = function(time) {
		clearTimeout(_this.idTimeout)
		var timeout = _this.timeout
		if(time){
			timeout = time;
		}
		_this.idTimeout = setTimeout(_this.send, timeout)
	}
}

jcart = new JSend()

function converting(my, id, params)
{
	$('#region').attr('innerHTML', '');
	$('#delivery').attr('innerHTML', '');
	$('#dservice').attr('innerHTML', '');
	$('#payment').attr('innerHTML', '');
	$('#user').attr('innerHTML', '');

	var count = parseInt(number(my.value));
	if (isNaN(count) || count<1) {
		jcart.timeout = 5000
		count = 1;
	} else {
		jcart.timeout = 2000
	}

	var uid ='';
	var urlParams = '';
	var i = null;
	for(i in params){
		urlParams += i + '/' + params[i] + '/'
		uid += '_' + params[i]
	}

	httpURL = '/ajax/shop/order/operation/change/id/'+ id + '/count/' + count + '/' + urlParams;
	jcart.sendData(httpURL,null,reConverting,'GET','converting'+id+uid)
}

function reConverting(html)
{
	removeImgLoader()
	jcart.bSend = false
	var myObject = Json.evaluate(html);
	var id        = myObject['id'];
	var accessory = myObject['accessory'];
	var sufix = (accessory?'_'+accessory:'');
	$('#count').text(myObject['count']);
	$('#count'+id+sufix).val(myObject['countItem']);
	$('#price').text(myObject['money']);
	$('#ajax_total').text(myObject['money']);
	$('#ajax_coast'+id+sufix).text(myObject['price']);
	
	var soMultiple = myObject['so_multiple'];

		$.each(soMultiple,function($key,$row){
			var $uni = myObject['id']+'_'+$row['goods_id'];
			$('#ajax_price_so_multi_'+$uni).text($row['price']);
			$('#ajax_count_so_multi_'+$uni).text($row['count']);
			$('#ajax_cost_so_multi_'+$uni).text($row['cost']);
		});

	var soMultiple = myObject['so_unmultiple'];
	if(soMultiple){
		$('#ajax_price_so_unmulti_'+myObject['id']).text(soMultiple['price']);
		$('#ajax_count_so_unmulti_'+myObject['id']).text(soMultiple['count']);
		$('#ajax_cost_so_unmulti_'+myObject['id']).text(soMultiple['cost']);
	}
}

function isNumeric(my)
{
	if(isNaN(parseInt(my.value)) || parseInt(my.value)==0) {
		my.value = 1;
	}
}

function loadDelivery(my)
{
	$('#delivery').attr('innerHTML', '');
	$('#dservice').attr('innerHTML', '');
	$('#payment').attr('innerHTML', '');
	$('#user').attr('innerHTML', '');

	if(my.value > 0 ) {
		var str = 'id=' + my.value;
		createCookie('city_id', my.value, false);
		//createCookie('city_id', 10, false);

		var httpURL = '/ajax/shop/order/operation/delivery/';
		showImgLoader();
		$.post(httpURL, {id: my.value}, reDelivery, "json");
	}
}

//Способ доставки:
function reDelivery(data)
{
	removeImgLoader();

	if (data['count']==0) {
		var httpURL = '/ajax/shop/order/operation/user/';
		$.post(httpURL, {payment: '0'}, reUser);
	} else {
		$('#delivery').attr('innerHTML', data['delivery']);
		if(data['count']==1){
			loadDeliveryService(data['id']);
		}
	}
	lastAnchor();
}

function loadDeliveryService(id)
{
	$('#dservice').attr('innerHTML', '');
	$('#payment').attr('innerHTML', '');
	$('#user').attr('innerHTML', '');

	var reg = $('#region_id').attr('value');
	if (id > 0 && reg > 0)	{
		var str = 'delivery=' + id + '&region=' +reg
		var httpURL = '/ajax/shop/order/operation/service/';
		showImgLoader();
		$.post(httpURL, {delivery:id, region:reg}, reService,'json');
	}
}

//Служба доставки:
function reService(data)
{
	removeImgLoader();
	$('#dservice').attr('innerHTML',data.html);
	if(data['count']==1) {
		loadPayment(data['id'], data['sid']);
	}
	lastAnchor();
}

function loadPayment(id, sid)
{
	$('#user').attr('innerHTML', '');
	createCookie('servise_id',id);

	var payment = document.getElementById('payment');
	if (payment) {
		//var str = 'service=' + id + '&dservice=' + sid;
		var httpURL = '/ajax/shop/order/operation/payment/';
		showImgLoader();
		$.post(httpURL, {service:id, dservice:sid}, rePayment, "json");
	}
}

function rePayment(data)
{
	removeImgLoader();
	$('#payment').attr('innerHTML',data.html).blur()
	if(data['count']==1) {
		loadUser(data['id']);
	}
	lastAnchor();
}

function loadUser(id)
{
	$('#user').attr('innerHTML', '');

	showPresentForm('present_id');
	if (id) {
		var httpURL = '/ajax/shop/order/operation/user/';
		showImgLoader();
		$.post(httpURL, {payment:id}, reUser);
	}
}

function reUser(html)
{
	removeImgLoader();
	$('#user').attr('innerHTML', html);
	lastAnchor();
}

function lastAnchor()
{
	var ajax = null;
	if(ajax = document.getElementById("TB_ajaxContent")) {
		ajax.scrollTop = 1000;
	}
}

function loadAuth(name, login)
{
	showImgLoader();
	var httpURL = '/ajax/shop/order/operation/auth/';
	$.post(httpURL, {user_email:name, user_password:login}, reAjaxAuth, "json");
}

function reAjaxAuth(data)
{
	removeImgLoader();
	if (data['auth']) {
		//loadRegion('first');
		$('#login_form').hide();
		$('#user_name').text(data['name'])
		$('#auth').show();
		loadCard('first');
	} else {
		$('#login_order_message').css('display','none');
		$('#login_order_error').css('display','block');
	}
}

function loadCard(first)
{
	showImgLoader();
	$('#card').empty();
	$('#region').empty();
	$('#dservice').empty();
	$('#delivery').empty();
	$('#payment').empty();
	$('#user').empty();
	
	var httpURL = '/ajax/shop/order/operation/card/';
	$.post(httpURL, {pay:first}, reCard);
}

function reCard(html)
{
	removeImgLoader();
	$('#card').html(html);
	lastAnchor();
}

function loadRegion(first)
{
	showImgLoader();
	$('#card').empty();
	$('#region').empty();
	$('#dservice').empty();
	$('#delivery').empty();
	$('#payment').empty();
	$('#user').empty();

	var httpURL = '/ajax/shop/order/operation/region/';
	$.post(httpURL, {pay:first}, reRegion);
}

function reRegion(html)
{
	removeImgLoader();
	$('#region').html(html);
	lastAnchor();
}

var parse_basket = 0;
function resetForm()
{
	$('#order_user_surname').hide();
	$('#order_user_name').hide();
	$('#order_user_tel_code').hide();
	$('#order_user_email').hide();
	$('#order_user_address_delivery').hide();
	$('#order_user_house').hide();
	$('#order_user_flat').hide();
	$('#order_user_company').hide();
	$('#order_user_zkpo').hide();
	////////////////////////////////////////////////////////////////
	$('#order_user_surname_who').hide();
	$('#order_user_name_who').hide();
	$('#order_user_tel_code_who').hide();
	$('#order_user_email_who').hide();
	$('#order_user_address_delivery_who').hide();
	$('#order_user_house_who').hide();
	$('#order_user_flat_who').hide();
	////////////////////////////////////////////////////////////////
	$('#order_user_surname_anyone').hide();
	$('#order_user_name_anyone').hide();
	$('#order_user_tel_code_anyone').hide();
	$('#order_user_email_anyone').hide();
	$('#order_user_address_delivery_anyone').hide();
	$('#order_user_house_anyone').hide();
	$('#order_user_flat_anyone').hide();
	////////////////////////////////////////////////////////////////
	$('#order_user_index').hide();
	$('#order_user_oblast').hide();
	$('#order_user_city').hide();
	$('#order_user_floor').hide();
	$('#order_user_floor_who').hide();
	$('#order_user_floor_anyone').hide();
}

var validator=new Validator();

function sendOrderForm(my)
{
	var id = null;

	resetForm();

	if ('undefined'!=typeof(my.surname) && (my.surname.value == "" || my.surname.value=='Фамилия')) {
		$('#order_user_surname').show();
		return false;
	}

	if('undefined'!=typeof(my.name) && (my.name.value == "" || my.name.value=='Имя')) {
		$('#order_user_name').show();
		return false;
	}

	//if('undefined'!=typeof(my.tel_code) && (my.tel_code.value=="" || isNaN(parseInt(my.tel_code.value)) || my.tel.value == "" || my.tel.value=='Телефон' || !validator.isDigits(my.tel_code.value) || my.tel_code.length<3 || !validator.isDigits(my.tel.value) || my.tel.length<3)) {

	if('undefined'!=typeof(my.tel_code) && my.tel_code.value.length<3 && my.tel.value.length<4 /*&& (!validator.isDigits(my.tel_code.value) || my.tel_code.value.length<3 || !validator.isDigits(my.tel.value) || my.tel.value.length<3)*/) {
		$('#order_user_tel_code').show();
		return false;
	}

	if('undefined'!=typeof(my.email) && (my.email.value.length < 6 || my.email.value.indexOf('@') < 1 || my.email.value.indexOf(".") < 1)) {
		$('#order_user_email').show();
		return false;
	}

	if('undefined'!=typeof(my.index) && (my.index.value=="" || my.index.value=='Индекс')) {
		$('#order_user_index').show();
		return false;
	}
	/*
	if('undefined'!=typeof(my.oblast) && (my.oblast.value=="")) {
		$('#order_user_oblast').show();
		return false;
	}*/
	/*
	if('undefined'!=typeof(my.city) && (my.city.value=="" || my.city.value=='Индекс')) {
		$('#order_user_city').show();
		return false;
	}*/

	if ('undefined'!=typeof(my.address_delivery) && (my.address_delivery.value=="" || my.address_delivery.value=='Улица, проспект, бульвар')) {
		$('#order_user_address_delivery').show();
		return false;
	}

	if ('undefined'!=typeof(my.house) && (my.house.value=="" || my.house.value=='Дом, корпус')) {
		$('#order_user_house').show();
		return false;
	}

	if('undefined'!=typeof(my.flat) && (my.flat.value=="" || my.flat.value=='Квартира/оффис')) {
		$('#order_user_flat').show();
		return false;
	}

	//if('undefined'!=typeof(my.floor) && (my.floor.value=="" || my.floor.value=='Этаж')) {
	if('undefined'!=typeof(my.floor) && !validator.isDigits(my.floor.value)) {
		$('#order_user_floor').show();
		return false;
	}
	if ('undefined'!=typeof(my.company) && (my.company.value == "" || my.company.value=='Компания')) {
		$('#order_user_company').show();
		return false;
	}
	if ('undefined'!=typeof(my.zkpo) && (my.zkpo.value == "" || my.zkpo.value=='ЗКПО')) {
		$('#order_user_zkpo').show();
		return false;
	}


	var str = '';

	if('undefined'!=typeof(my.surname)) {
		if(str!='') {str += '&';}
		str +=  "surname=" + my.surname.value;
	}

	if('undefined'!=typeof(my.name)) {
		if(str!='') {str += '&';}
		str += "name=" + my.name.value;
	}

	if('undefined'!=typeof(my.patronymic) && my.patronymic.value!='' && my.patronymic.value!='Отчество') {
		if(str!='') {str += '&';}
		str += "patronymic=" + my.patronymic.value;
	}

	if('undefined'!=typeof(my.tel_code) && 'undefined'!=typeof(my.tel)) {
		if(str!='') {str += '&';}
		str += "tel=" + my.tel_code.value + '|' + my.tel.value;
	}

	if('undefined'!=typeof(my.tel_mob_code) && 'undefined'!=typeof(my.tel_mob) && my.tel_mob_code.value!='' && my.tel_mob_code.value!='Код' && my.tel_mob.value!='' && my.tel_mob.value!='Моб. Телефон') {
		if(str!='') {str += '&';}
		str += "mobile=" + my.tel_mob_code.value + '|' + my.tel_mob.value;
	}

	if('undefined'!=typeof(my.tel_code_fax) && 'undefined'!=typeof(my.tel_fax) && my.tel_code_fax.value!='' && my.tel_code_fax.value!='Код' && my.tel_fax.value!='' && my.tel_fax.value!='FAX') {
		if(str!='') {str += '&';}
		str += "fax=" + my.tel_code_fax.value + '|' + my.tel_fax.value;
	}

	if('undefined'!=typeof(my.email) && my.email.value!='' && my.email.value!='E-mail') {
		if(str!='') {str += '&';}
		str += "email=" + my.email.value;
	}

	if('undefined'!=typeof(my.index)) {
		if(str!='') {str += '&';}
		str += "index=" + my.index.value;
	}
	/*
	if('undefined'!=typeof(my.oblast)) {
		if(str!='') {str += '&';}
		str += "oblast=" + my.oblast.value;
	}

	if('undefined'!=typeof(my.city)) {
		if (str!='') {str += '&';}
		str += "city=" + my.city.value;
	}
	*/
	if('undefined'!=typeof(my.address_delivery)) {
		if(str!='') {str += '&';}
		str +=  "address_delivery=" + my.address_delivery.value;
	}

	if('undefined'!=typeof(my.house)) {
		if(str!='') {str += '&';}
		str +=  "house=" + my.house.value;
	}

	if('undefined'!=typeof(my.flat)) {
		if(str!='') {str += '&';}
		str +=  "flat=" + my.flat.value;
	}

	if('undefined'!=typeof(my.floor) && my.floor.value!='' && my.floor.value!='Этаж') {
		if(str!='') {str += '&';}
		str +=  "floor=" + my.floor.value;
	}

	if('undefined'!=typeof(my.comments_user) && my.comments_user.value!='' && my.comments_user.value!=='Дополнительные ориентиры для нахождения адреса, код подъезда, пожелания по времени доставки') {
		if(str!='') {str += '&';}
		str +=  "comments_user=" + my.comments_user.value;
	}

	if('undefined'!=typeof(my.zakaz)) {
		if(str!='') {str += '&';}
		str +=  "zakaz=" + my.zakaz.value;
	}
	if('undefined'!=typeof(my.company)) {
		if(str!='') {str += '&';}
		str +=  "company=" + my.company.value;
	}
	if('undefined'!=typeof(my.zkpo)) {
		if(str!='') {str += '&';}
		str +=  "zkpo=" + my.zkpo.value;
	}
	my.submit();
}

function showPresentForm(id, idout, idnew)
{
	var my = document.getElementById(id);

	if(my && my.checked) {
		createCookie('present_form', 1);
		$('#kiev').hide();
		$('#kiev_present').show();
	} else {
		createCookie('present_form', 0);
		$('#kiev').show();
		$('#kiev_present').hide();
	}

	var idnew = $('#'+idnew);
	var idout = $('#'+idout);
	idnew.attr('innerHTML',idout.attr('innerHTML'));

	if(my && my.checked) {
		idnew.attr('innerHTML',idout.attr('innerHTML'));
	} else {
		idnew.attr('innerHTML', '&nbsp;');
	}
}

var removeImgLoad = 0;
function showImgLoader()
{
	removeImgLoad = 1;
	$("body").append("<div id='TB_load'><img src='/img/js/loadingAnimation.gif' onload='is_showImgLoader();'/></div>");//add loader to the page
	$('#TB_load').show();//show loader
}

function is_showImgLoader()
{
	if(removeImgLoad==0) {
		removeImgLoader();
	}
}

function removeImgLoader()
{
	removeImgLoad = 0;
	$("#TB_load").remove();
}

function zakazat(caption, url, imageGroup, urlADD)
{
	window.focus();
	if (url) {
		tb_show(caption, url, imageGroup);
	}
	if (urlADD) {
		addItem(urlADD);
	}
}

function compare(url)
{
	window.focus();
	if (url) {
		open_window(url,1000,800);
	}
}