function delete_product_image(product_id, image_id) {

	jQuery.ajax({
	  type: "POST",
	  url: "/ajax/delete_product_picture.php",
	  data: {product_id : product_id, image_id : image_id},
	  success: function(msg){
	    for(i=image_id; i<=5; i++) {
	    	if (jQuery('#product_image_' + (i+1)).length == 0) {
	    		jQuery('#product_image_' + i).remove();
	    		jQuery('#delete_picture_link_' + i).remove();
	    	} else {
	    		jQuery('#product_image_' + i).attr('src', jQuery('#product_image_' + (i+1)).attr('src'));
	    	}
	    }
	  }
 	});
	return false;
}

function changeProductsCategory() {

	productsIds = "";
	var productsIdsArray = new Array();
	i=0;
	jQuery("#pageMain :checked").each( function () {
		productsIds += jQuery(this).val() + ",";
		productsIdsArray[i] = jQuery(this).val();
		i++;
	});

	selectedCategory = jQuery("#category_id option:selected").val();

	if (selectedCategory != 0) {

	jQuery.ajax({
	  type: "POST",
	  url: "/ajax/change_products_category.php",
	  data: {products_ids : productsIds, category_id : selectedCategory},
	  success: function(msg){
	  	alert('Moved ' + productsIdsArray.length + ' products.');
	  	for (index in productsIdsArray) {
	  		product_id = productsIdsArray[index];
	  		jQuery('#product_datarow_empty_' + product_id).remove();
	  		jQuery('#product_datarow_' + product_id).remove();
	  	}
	  }
 	});
 	} else {
 		alert("Please select category");
 	}

	return false;
}


function changeProductsBrandPrice(categoryId, brandId) {
	pricePercent = jQuery("#brand_products_price_percent").val();
	//brandId = jQuery("#brand_id").val();

	jQuery.ajax({
	  type: "POST",
	  url: "/ajax/change_products_price.php",
	  data: {category_id : categoryId, brand_id: brandId, price_percent: pricePercent},
	  success: function(msg){
	  	/*alert('Moved ' + productsIdsArray.length + ' products.');
	  	for (index in productsIdsArray) {
	  		product_id = productsIdsArray[index];
	  		jQuery('#product_datarow_empty_' + product_id).remove();
	  		jQuery('#product_datarow_' + product_id).remove();
	  	}*/
	  	return main_request('part=search;brand=' + brandId + ';name=;category=' + categoryId + ';sort_by=name;sort_type=desc')
	  }
 	});
 	return false;
}

function showPropertyEditWindow(id) {
	element = jQuery("#property_name_" + id);
	element.toggle();
	element.css("height", "40px");
	return false;
	}

function savePropertyName(id) {
	propertyName = jQuery("#property_name_value_" + id).val();

	jQuery.ajax({
	  type: "POST",
	  url: "/ajax/save_property_name.php",
	  data: {property_name_id : id, property_name: propertyName},
	  success: function(msg){
	  	return main_request('part=products_properties');
	  }
 	});
 	return false;
}

function showPropertyValueAddWindow(id) {
	element = jQuery("#property_value_add_window_" + id);
	element.toggle();
	element.css("height", "40px");
	return false;
	}

function showPropertyValueEditWindow(id) {
	element = jQuery("#property_value_edit_window_" + id);
	element.toggle();
	element.css("height", "40px");
	return false;
	}

function savePropertyValue(id, saveType) {
	if (saveType == 'add') {
		propertyValue = jQuery("#property_value_" + id).val();
	}
	if (saveType == 'change') {
		propertyValue = jQuery("#property_value_value_" + id).val();
	}

	jQuery.ajax({
	  type: "POST",
	  url: "/ajax/save_property_value.php",
	  data: {identifier : id, property_value: propertyValue, save_type: saveType},
	  success: function(msg){
	  	return main_request('part=products_properties');
	  }
 	});
 	return false;
}

function findValuesForProperty() {

	propertyNameId = jQuery("#property_name_select option:selected").val();


	/*jQuery.ajax({
	  type: "POST",
	  url: "/ajax/find_properties_values.php",
	  data: {property_name_id : propertyNameId},
	  success: function(msg){
	  	//alert(jQuery.deserialize(msg));
	  	jQuery.getJSON(msg, function(data) {
	  		alert(data);
	  	})
	  }
 	});*/

 	element = jQuery('#property_value_container');
	jQuery('#property_value_container_value').remove();
 	jQuery('#property_value_container_select').remove();

 	//element.append('<select id="property_value_container_select"></select>');


	var options = new Array();

 	jQuery.getJSON('/ajax/find_properties_values.php?property_name_id=' + propertyNameId, function(data) {
 			//alert(data.length);
 			if (data.length == 0) {
 				element.append('<input type="text" name="property_value_container_value" id="property_value_container_value"></select>');
 			}
 			else {
 				element.append('<select name="property_value_container_select" id="property_value_container_select"></select>');
 				elementSelect = jQuery('#property_value_container_select');
				jQuery.each(data, function() {
					elementSelect.append('<option value="' + jQuery(this).attr('id') + '">' + jQuery(this).val() + '</option>');
					//options[jQuery(this).attr('id')] = jQuery(this).val();
				});
			}
	});

/*	alert(options);

	for (id in options) {
		alert(id);
		alert(options[id]);
	}*/

 	return false;
}


function findCategoryForBrand() {

	brandId = jQuery("#brand_id option:selected").val();

 	element = jQuery('#category_select_container');
	jQuery('#category_select').remove();
	jQuery('#no_data_info').remove();


 	jQuery.getJSON('/ajax/find_categories_for_brand.php?brand_id=' + brandId, function(data) {
 			//alert(data.length);
 			if (data.length == 0) {
				element.append('<p id="no_data_info">No categories found for this brand</p>');
 			}
 			else {
 				element.append('<select name="category_select" id="category_select"></select>');
 				elementSelect = jQuery('#category_select');
				jQuery.each(data, function() {

				id = jQuery(this).attr('id');
				if (id.length == 2) {
					elementSelect.append('<option value="' + jQuery(this).attr('id') + '">' + jQuery(this).val() + '</option>');
				}
				if (id.length == 4) {
					elementSelect.append('<option value="' + jQuery(this).attr('id') + '">&nbsp;&nbsp;&nbsp;&nbsp;' + jQuery(this).val() + '</option>');
				}


				});
			}
	});

 	return false;
}


/************ CATEGORIES GROUPS *******************/

function showCategoryGroupEditWindow(id) {
	element = jQuery("#category_group_name_" + id);
	element.toggle();
	element.css("height", "40px");
	return false;
}

function showCategoriesListWindow(id) {
	element = jQuery("#categories_for_group_" + id);
	elementList = jQuery("#categories_for_group_list_" + id);
	element.toggle();
	element.css("height", "auto");

	jQuery.ajax({
	  type: "GET",
	  url: "/ajax/category_group/load_categories_list.php",
	  success: function(msg){
	  	elementList.html(msg);
	  }
 	});

	return false;
}

function saveCategoryGroupName(id) {
	categoryGroupName = jQuery("#category_group_name_editing_" + id).val();

	jQuery.ajax({
	  type: "POST",
	  url: "/ajax/category_group/save_category_group_name.php",
	  data: {category_group_name_id : id, category_group_name: categoryGroupName},
	  success: function(msg){
	  	return main_request('part=categories_groups');
	  }
 	});
 	return false;
}

function addCategoriesToGroup(id){
	element = jQuery('#categories_for_group_' + id);

	categoriesIds = "";
	var categoriesIdsArray = new Array();
	i=0;
	jQuery("#categories_for_group_" + id + " :checked").each( function () {
		categoriesIds += jQuery(this).val() + ",";
		//categoriesIdsArray[i] = jQuery(this).val();
		//i++;
	});

	if (categoriesIds.length != 0) {

	jQuery.ajax({
	  type: "POST",
	  url: "/ajax/category_group/add_categories_to_group.php",
	  data: {categories_ids : categoriesIds, group_id : id},
	  success: function(msg){
	  	//alert('Moved ' + productsIdsArray.length + ' products.');
	  	//for (index in productsIdsArray) {
	  		//product_id = productsIdsArray[index];
	  		//jQuery('#product_datarow_empty_' + product_id).remove();
	  		//jQuery('#product_datarow_' + product_id).remove();
	  	//}
	  	return main_request('part=categories_groups');
	  }
 	});
 	} else {
 		alert("Please select categories");
 	}

	return false;
}

function deleteFromCategoryGroup(id) {
	jQuery.ajax({
	  type: "POST",
	  url: "/ajax/category_group/delete_category_from_group.php",
	  data: {category_id : id},
	  success: function(msg){
	  	return main_request('part=categories_groups');
	  }
 	});
	return false;
}

function deleteCategoryGroup(id) {
	if (confirm("Are you sure to delete category group? \n(categories will be not deleted but will be with no group)")) {
		jQuery.ajax({
		  type: "POST",
		  url: "/ajax/category_group/delete_category_group.php",
		  data: {category_group_id : id},
		  success: function(msg){
		  	return main_request('part=categories_groups');
		  }
	 	});
 	}
 	return false;
}

function changeGroupOrder(id, orderDirection) {
	jQuery.ajax({
	  type: "POST",
	  url: "/ajax/category_group/change_group_order.php",
	  data: {id : id, order_dir : orderDirection},
	  success: function(msg){
	  	return main_request('part=categories_groups');
	  }
 	});
 	return false;
}

function removeAllCategoriesFromGroup() {
	if (confirm("Are you sure to remove all categories from groups? \n(categories will be not deleted but will be with no group)")) {
		jQuery.ajax({
		  type: "POST",
		  url: "/ajax/category_group/remove_categories_from_group.php",
		  data: {},
		  success: function(msg){
		  	return main_request('part=categories_groups');
		  }
	 	});
 	}
 	return false;
}

function goToPromotion() {
	productsIds = "";
	var productsIdsArray = new Array();
	i=0;
	jQuery("#pageMain :checked").each( function () {
		if (jQuery(this).val() > 0) {
			productsIds += jQuery(this).val() + ",";
		}
		//productsIdsArray[i] = jQuery(this).val();
		//i++;
	});
	document.location.href = '/?part=add_promotions&products_ids=' + productsIds;
}

function selectAll() {
	//jQuery('#products_list :checkbox').attr('selected', 'selected');

	/*elements = jQuery('#products_list :tr');
	elements.each( function () {
		identifier = jQuery(this).attr("id");

		jQuery('#' + identifier + ':checkbox').css('checked', 'checked');
	});*/

	jQuery('#products_list INPUT[@name="products"][type="checkbox"]').attr('checked', 'checked');


	//alert(jQuery('#products_list :input'));

	return false;
}

function unSelectAll() {
	jQuery('#products_list INPUT[@name="products"][type="checkbox"]').attr('checked', '');
	return false;
}

function goForHideProducts(parameter) {
	n = jQuery("#products_list input:checked").length;
	if (n == 0) {
		alert('No products have been selected');
	} else {

		productsIds = "";
		var productsIdsArray = new Array();
		i=0;
		jQuery("#products_list :checked").each( function () {
			if (jQuery(this).val() > 0) {
				productsIds += jQuery(this).val() + ",";
			}
			//productsIdsArray[i] = jQuery(this).val();
			//i++;
		});
		document.location.href = '/special_actions.php?products_ids=' + productsIds +'&parameter=' + parameter + '&qs=' + jQuery('#query_string').val();
	}
	return false
}

function toggleAddressField() {
	elementCheckbox = document.getElementById("different_user_address");
	element = document.getElementById("invoice_temp_address");
	if (elementCheckbox.checked) {
		element.disabled = false;
	} else {
		element.disabled = true;
	}
}

function changeFrakt(cartId) {
	value = jQuery('#basket_frakt_new_value').val();
	jQuery.ajax({
	  type: "POST",
	  url: "/ajax/change_frakt.php",
	  data: {cart_id : cartId, value : value},
	  success: function(msg){
	  	return main_request('part=basket;cart_id=' + cartId);
	  }
 	});
 	return false;
}

function changeFraktShowWindow() {
	jQuery('.basket_frakt_change_window').show();
	return false;
}
function changeFraktHideWindow() {
	jQuery('.basket_frakt_change_window').hide();
	return false;
}
