/**
 * 
 */

function sortList(criteria, orderName, orderType, page) {
	jQuery.ajax({
		type : "POST",
		url : "/offer/change-search-order/",
		data : "criteria=" + criteria + "&orderName=" + orderName + "&orderType=" + orderType + "&page=" + page,
		success : function(data) {
			jQuery('#offersSearch').html(data);
			jQuery('html, body').animate({
			    scrollTop: jQuery("#offersSearch").offset().top
			}, 500);
		}
	});
}

function registrationStepOne(offerId, tourneyId) {
	if(!tourneyId) {
		tourneyId = jQuery('#product-date-change-select').val();
	}
	jQuery.ajax({
		type : "POST",
		url : "/rejestracja/krok-1/",
		data : "tourneyId=" + tourneyId + "&offerId=" + offerId,
		success : function(data) {
			jQuery('#content').html(data);
			jQuery('html, body').animate({
			    scrollTop: jQuery("#content").offset().top
			}, 500);
		}
	});
}

function updateProductPrice(offer,tourney) {
	
	var url = "/oferta/aktualizuj-cene";
	jQuery.post(url, {offerId: offer, tourneyId: tourney}, function(data) {
		jQuery('#tourneyTime').html(data.time);
		jQuery('#product-price-value-container').html(data.price);
		jQuery('#product-date-change-form').removeClass('loading');
		jQuery('#product-date-change-form select').show();
	}, "json");

	jQuery('#product-date-change-form').addClass('loading');
	jQuery('#product-date-change-form select').hide();
}

function updateTransportPrice(offer,tourney) {
	var access = jQuery('#tourney-access-select').val();
	var back = jQuery('#tourney-back-select').val();
	var url = "/rejestracja/aktualizuj-cene-dojazdu";
	jQuery.post(url, {accessId: access, backId: back, offerId: offer, tourneyId: tourney}, function(data) {
		jQuery('#transport-price-container').html(data);
		jQuery('#transport-price-container-loader').removeClass('loading');
		jQuery('#transport-price-container-loader span').show();
		
	});

	jQuery('#transport-price-container-loader').addClass('loading');
	jQuery('#transport-price-container-loader span').hide();
}

function registrationStepTwo(form) {
	
	jQuery.ajax({
		type : "POST",
		url : "/rejestracja/krok-2/",
		data : jQuery(form).serialize(),
		success : function(data) {
			var obj = jQuery.parseJSON(data);
			if(obj.errors != null) {
				jQuery('#content').find('div.big-loader').remove();
				for(k in obj.errors) {
					jQuery(form).find('[name="' + k +'"]').addClass('error');
				}
			} else {
				jQuery('#content').html(obj.content);
				jQuery('html, body').animate({
				    scrollTop: jQuery("#breadcrumbs").offset().top
				}, 500);
			}

		}
	});
}

function registrationStepTwoBack() {
	jQuery.ajax({
		type : "POST",
		url : "/rejestracja/krok-2-p/",
		data : '',
		success : function(data) {
			var obj = jQuery.parseJSON(data);
			jQuery('#content').html(obj.content);
			jQuery('html, body').animate({
			    scrollTop: jQuery("#camperDataContainer").offset().top
			}, 500);
		}
	});
}

function registrationStepThree(form) {
	
	jQuery.ajax({
		type : "POST",
		url : "/rejestracja/krok-3/",
		data : jQuery(form).serialize(),
		success : function(data) {
			var obj = jQuery.parseJSON(data);
			jQuery('#content').find('div.big-loader').remove();
			if(obj.errors != null) {
				jQuery('input').removeClass('error');
				
				for(k in obj.errors) {
					if(k == 'email-exists') {
						alert('Podany adres email już istnieje w bazie');
						jQuery('input[name="user[email]"]').addClass('error');
					} else {
						jQuery(form).find('[name="' + k +'"]').addClass('error');
					}
					
				}
				jQuery('html, body').animate({
				    scrollTop: jQuery("#camperDataContainer").offset().top
				}, 500);
			} else {
				jQuery('#content').html(obj.content);
				jQuery('html, body').animate({
				    scrollTop: jQuery("#breadcrumbs").offset().top
				}, 500);
			}

		}
	});
	
}

function registrationStepFour() {
	jQuery.ajax({
		type : "POST",
		url : "/rejestracja/krok-4/",
		data : '',
		success : function(data) {
			var obj = jQuery.parseJSON(data);
			jQuery('#content').html(obj.content);
			jQuery('html, body').animate({
			    scrollTop: jQuery("#breadcrumbs").offset().top
			}, 500);
		}
	});
}

function addToClipboard(offerId) {
	jQuery.ajax({
		type : "POST",
		url : "/clipboard/add/",
		data : "offerId=" + offerId,
		success : function(data) {
			var obj = jQuery.parseJSON(data);
			jQuery('#clipboardInfo').html(obj.content);
			jQuery('html, body').animate({
			    scrollTop: jQuery("#breadcrumbs").offset().top
			}, 500);
		}
	});
}


function submitCatalogue(form) {
	jQuery.ajax({
		type : "POST",
		url : "/index/submit-catalogue/",
		data : jQuery(form).serialize(),
		success : function(data) {
			var obj = jQuery.parseJSON(data);
			jQuery('#content').find('div.big-loader').remove();
			if(obj.errors != null) {
				jQuery('input').removeClass('error');
				jQuery('#catalogueSelect').removeClass('error');
				
				for(k in obj.errors) {
					if(k == 'catalogueError') {
						jQuery('#catalogueSelect').addClass('error');
					} else {
						jQuery(form).find('[name="' + k +'"]').addClass('error');
					}
				}
				jQuery('html, body').animate({
				    scrollTop: jQuery("#productPage3").offset().top
				}, 500);
			} else {
				jQuery('#content').html(obj.content);
				jQuery('html, body').animate({
				    scrollTop: jQuery("#content").offset().top
				}, 500);
			}

		}
	});
}
