var MP_EDIT_ITEM = {}

//12
/*
function getEditItemPopup()
{
	var urlEditItem = '/fr_ca/products/ajax_response/menuplanner_response.php';
	
	$.post(
		urlEditItem,
		{edititem},
		formResponseMenuPlanner,
		"json"		
	);
}
*/


function editThisItem( dayNameShort, mealType, row )
{
	var mealsList    = new Array('','break', 'morning_snack', 'lunch', 'afternoon_snack', 'dinner', 'other_snack');
	var dayNamesList = new Array('sun','mon', 'tues', 'wed', 'thurs', 'fri', 'sat');
	
	
	MP_EDIT_ITEM[ 'childId' ] 	  = $( '#child_id' ).val();
	MP_EDIT_ITEM[ 'plannerId' ]   = $( '#' + dayNameShort + '_' + mealType + '_' + row).val();	
	MP_EDIT_ITEM[ 'productId' ]   = $( '#' + dayNameShort + '_' + mealType + '_productid_' + row).val();	
	MP_EDIT_ITEM[ 'productName' ] = $( '#' + dayNameShort + mealType + 'name' + row).html();	
	MP_EDIT_ITEM[ 'weekDayId' ]   = array_search( dayNameShort, dayNamesList );
	MP_EDIT_ITEM[ 'mealTypeId' ]  = array_search( mealType, mealsList );
	MP_EDIT_ITEM[ 'row' ]         = row;
	//saveEditedItem( );
	var popRequest2ndArg = '';
	if( MP_EDIT_ITEM[ 'productId' ] == '-1' )
	{
		popRequest2ndArg = MP_EDIT_ITEM[ 'productName' ];
	}
	sendPopupRequest( 'edititem', popRequest2ndArg );
	//var productId = 107;
	
	
		
}


function saveEditedItem()
{
	var error_text = "";
	
	var categories_selected_index      = $('#categories_additem :selected').val();
	var categories_selected_value      = $('#categories_additem :selected').val();
	var category_groups_selected_value = $('#category_groups_additem :selected').val();
	var product_selected_value         = $('#products_additem :selected').val();
	var product_selected_text          = $('#products_additem :selected').text();	  
	var other_product                  = $('#other_product_additem').val();	
	//var DBmenuplaner                   = $('#menuplannerID').val();	
	
	var isCategoriesSelected 	= true
	var isCategoryGroupSelected = true
	var isProductSelected 		= true
	
	var mealsList    = new Array('','break', 'morning_snack', 'lunch', 'afternoon_snack', 'dinner', 'other_snack');
	var dayNamesList = new Array('sun','mon', 'tues', 'wed', 'thurs', 'fri', 'sat');
	var dayNameShort = dayNamesList[MP_EDIT_ITEM[ 'weekDayId' ]];
	var mealTypeId   = mealsList[MP_EDIT_ITEM[ 'mealTypeId' ]];
	
	/*
	if( MP_EDIT_ITEM[ 'productId' ] != '-1' )
	{*/
		if(categories_selected_value == "sel" && other_product.replace( ' ', '' ) == ''){
				error_text = " Please select a category<br/>"
				isCategoriesSelected = false
		}
		if(category_groups_selected_value == "sel" && other_product.replace( ' ', '' ) == ''){
			error_text = error_text + " Please select a group<br/>"
			isCategoryGroupSelected = false
		}
		
		if(product_selected_value == "sel" && other_product.replace( ' ', '' ) == ''){
			error_text = error_text + " Please select a product<br/>"
			isProductSelected = false
		}
		
		/*if(((isCategoriesSelected && isCategoryGroupSelected && isProductSelected)==true)){
			error_text = error_text + "Please either select a product with the drop down or manually type a product<br/>"
		}*/
	/*}
	else
	{*/
		if( other_product.replace( ' ', '' ) == '' &&  product_selected_value == "sel")
		{
			error_text = "Please manually type a product or select a product from the list<br>";
		}
	//}
	
	//
	//check errors and stuff
	//
	if(error_text != ""){
		$('#menuplanner_error_text').html(error_text)
		showMenuPlannerError()
	}
	else{
		//MP_EDIT_ITEM[ 'menuId' ]	  = DBmenuplaner;
		/*if( MP_EDIT_ITEM[ 'productId' ] != '-1' )
		{
			MP_EDIT_ITEM[ 'productId' ]   = product_selected_value;	
			MP_EDIT_ITEM[ 'productName' ] = $('#products_additem :selected').text();
		}
		else
		{
			MP_EDIT_ITEM[ 'productName' ] = $('#other_product_additem').val();
		}*/
		
		
		if( product_selected_value != 'sel' )
		{
			MP_EDIT_ITEM[ 'productId' ]   = product_selected_value;	
			MP_EDIT_ITEM[ 'productName' ] = '';
			//MP_EDIT_ITEM[ 'productName' ] = $('#products_additem :selected').text();			
		}
		else
		{
			MP_EDIT_ITEM[ 'productId' ]   = '-1';
			MP_EDIT_ITEM[ 'productName' ] = $('#other_product_additem').val();
		}	
		//MP_EDIT_ITEM[ 'dbmenuplannerid' ] = DBmenuplaner; 
		
		$.post( "/fr_ca/products/ajax_response/edit_menuplanner_response.php", MP_EDIT_ITEM, EditResponseMenuPlanner, "json" );
		
		/* legacy simon section*/
		//$( '#' + dayNameShort + mealTypeId + 'name' + MP_EDIT_ITEM[ 'row' ]).html( MP_EDIT_ITEM[ 'productName' ] );
		//$( '#' + dayNameShort + '_' + mealTypeId + '_' + MP_EDIT_ITEM[ 'row' ]).val( MP_EDIT_ITEM[ 'plannerId' ] );
		
		//MP_EDIT_ITEM = {};
	}
	
	

}

function EditResponseMenuPlanner(data){
	
	if(data[0] == "true"){
		//refresh menu with updated info //
		var mealsList    = new Array('','break', 'morning_snack', 'lunch', 'afternoon_snack', 'dinner', 'other_snack');
		var dayNamesList = new Array('sun','mon', 'tues', 'wed', 'thurs', 'fri', 'sat');
		var dayNameShort = dayNamesList[MP_EDIT_ITEM[ 'weekDayId' ]];
		var mealTypeId   = mealsList[MP_EDIT_ITEM[ 'mealTypeId' ]];
   /*$response[1] = $plannerId;
   $response[2] = $productName;
   $response[3] = $productId;		*/
		$( '#' + dayNameShort + mealTypeId + 'name' + MP_EDIT_ITEM[ 'row' ]).html( data[2] );
		$( '#' + dayNameShort + '_' + mealTypeId + '_' + MP_EDIT_ITEM[ 'row' ]).val( data[1] );
		$( '#' + dayNameShort + '_' + mealTypeId + '_productid_' + MP_EDIT_ITEM[ 'row' ]).val( data[3] );
		
		//Display confirmation table//
		$('#menuplanner_error_text').html("Les renseignements ont &eacute;t&eacute; ajout&eacute;s avec succ&egrave;s au planificateur de menus de votre enfant.");
		$('#menuplanner_edit_error_button').html("<img src='/fr_ca/products/includes/images/helpful_tools/bt_ok.jpg' onclick=\"javascript:BAM_AD_hide();\" width='70' height='26' >");
		showMenuPlannerError()
		sendRequestPopualteMenuPlanner(MP_EDIT_ITEM[ 'weekDayId' ],MP_EDIT_ITEM[ 'childId' ]);
		MP_EDIT_ITEM = {};			
	}	
	else{
		//This condition is met if the user updates to a product with already exists

		
		var error_text_full = "Cet &eacute;l&eacute;ment a d&eacute;j&agrave; &eacute;t&eacute; ajout&eacute; au menu du repas et du jour surlign&eacute;s."
		$('#menuplanner_error_text').html(error_text_full)
		showMenuPlannerError()	
	}		
}

function array_search(str, array)
{
	//return first position
	var posCount = "posCount";
	for(var i=0; i<array.length;i++)
	{
		if(array[i] == str)
		{
			if(posCount == "posCount")
			{
				posCount = i;
			}
		}
	}
	if(posCount == "posCount")
	{
		return false;
	}
	else
	{
		return posCount;
	}
}

function showMenuPlannerError()
{
  //this function shows the menu planner error and hides the main content area
  $('#menuplanner_error_content').show()
  $('#menuplanner_content').hide()
}

function showRemoveChild( child_position_id )
{
	BAM_AD_show()
    var closeBtn = '<div class="error_button"><img src="/fr_ca/babyclub/images/bt_close.gif" width="70" height="28" onclick="javascript:BAM_AD_hide()" /></div>';
	var message  = '<div class="error_text"><span style="color:#008176;font-size:13px;font-weight:bold">Confirmation</span><br><br>To remove the child\'s profile, click on "Continue". <br><br><a href="javascript:removeChild('+ child_position_id+')"><img src="/fr_ca/babyclub/images/bt_continue.gif" border="0" /></a></div>';
	$('.error_box_middle').html(closeBtn+message);
			var sizes       = getPageSize();
			//Static height of the popup
			var popupHeight = $('#error_box').height();
			var topCenter   = Math.round( ( sizes[3] - popupHeight ) / 2 ) + $( window ).scrollTop();			
			$( '#BAM_AD_content' ).css( 'top', topCenter+'px');		
}		

function removeChild( child_position_id )
{
	$.post( "/fr_ca/babyclub/ajax_response/remove_child_profile.php", { 'child_id':child_position_id }, showRemoveChildThanks, "json" );
}					  

function showRemoveChildThanks( data )
{
	if( data[ 0 ] == 'true' )
	{
		var closeBtn = '<div class="error_button"><img src="/fr_ca/babyclub/images/bt_close.gif" width="70" height="28" onclick="javascript:window.location.href=\'/fr_ca/babyclub/\'" /></div>';
		var message  = '<div class="error_text">Your child profile has been removed.</div>';
		$('.error_box_middle').html(closeBtn+message);		
			var sizes       = getPageSize();
			//Static height of the popup
			var popupHeight = $('#error_box').height();
			var topCenter   = Math.round( ( sizes[3] - popupHeight ) / 2 ) + $( window ).scrollTop();			
			$( '#BAM_AD_content' ).css( 'top', topCenter+'px');			
	}
	else
	{
		window.location.href= '/fr_ca/babyclub/';
	}
}
