function getExistingMealplans(whichControl) {
    //retrieve existing meal plans using jquery getJSON
    //if successful, populate the content of the 'existingMealplanDetails' container
    // and enable the 'go' button
    jQuery.getJSON("urlOfGetExistingMealplanDetails?mealplanID="+jQuery(whichControl).find("option:selected").val(),
        function(data){
            jQuery.each(data.items, function(i,item){
            //iterate through the returned data and add to the appropriate dropdown controls (course/day)
          });
        },
        function(data, textStatus){
            //this is the callback function
            // textStatus will be one of the following values:
            //   "timeout","error","notmodified","success","parsererror"
            switch(textStatus){
                case "success":
                    //enable the 'go' button
                    break;
                default:
                    //do something
            }
        });
}

function addToExistingMealplan(){
    //add the current recipe to the selected item from the 'dd_existing_mealplans' control (i.e. the targeted meal plan)
}

function addToNewMealplan(){
    //add the current recipe to a new meal plan
}

function showErrorMessage(forWhichField, message) {
    jQuery(forWhichField).parent().find("span.errorMessage:first").html(message);
}

function clearErrorMessage(forWhichField) {
    jQuery(forWhichField).parent().find("span.errorMessage:first").empty();
}

function isValidEmail(thisEmail) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(thisEmail);
}

function validateSelectOptionChosen(whichField, context) {
	
	if (jQuery(whichField).val() == "") {
		jQuery(whichField).focus();
        showErrorMessage(whichField, lcloGlobalMessages["shoppinglist.you_must_select.message"] +" "+ context);
        return false;
	} else {
        clearErrorMessage(whichField);
        return true;
    }
}

function validateIntegerField(whichField, context) {
	
    if (jQuery(whichField).val().length < 1 || !isNumeric(jQuery(whichField).val()) || jQuery(whichField).val() < 1 ) {
        jQuery(whichField).focus();
        showErrorMessage(whichField, shoppingListMessages["common.please_enter_valid.message"] +" "+ context);
        return false;
    } else {
        clearErrorMessage(whichField);  
        return true;  
    }
}

function isNumeric(form_value) 
{ 
    if (form_value.match(/^\d+$/) == null) {
        return false; 
    } else { 
        return true;
    } 
} 

function validateTextfield(whichField, context) {
	
    //generic validation - checks that the length is >= 0 characters
    if (jQuery.trim(jQuery(whichField).val()).length < 1) {
        jQuery(whichField).focus();
        showErrorMessage(whichField, shoppingListMessages["common.please_enter_valid.message"] +" "+ context);
        return false;
    } else {
    	isValid = true;
        clearErrorMessage(whichField);  
        return true;  
    }
}
function validateShoppingListNameTextfield(whichField, context) {
	
    //generic validation - checks that the length is >= 0 characters
    if (jQuery.trim(jQuery(whichField).val()).length < 1) {
        jQuery(whichField).focus();
        showErrorMessage(whichField, shoppingListMessages["common.please_enter_valid.message"] + " "+context);
        return false;
    } else if (jQuery.trim(jQuery(whichField).val()).length >80) {
        jQuery(whichField).focus();
        showErrorMessage(whichField, shoppingListMessages["shoppinglist.use_less_80_char.message"]);
        return false;
    
    } else {
    	isValid = true;
        clearErrorMessage(whichField);  
        return true;  
    }
}

function validateEmailfield(whichEmail, context) {

    //email validation - checks that the length is >= 0 characters
	if (isValidEmail(jQuery(whichEmail).val())) {
        clearErrorMessage(whichEmail);
        return true;
    } else {
        showErrorMessage(whichEmail, lcloGlobalMessages["common.please_enter_valid.message"] +" "+ context);
        return false;
    }
}

function validateReviewForm(){
    //validate the title & rating fields
    var boolFormIsValid = true;
    if (jQuery.trim(jQuery("#tb_review_title").val()) < 1) {
        boolFormIsValid = false;
        showErrorMessage(jQuery("#tb_review_title"), "Please enter a review title");
    }
    if (jQuery("#overlayRating input:hidden").val() == "undefined") { //star rating uses a hidden field to store the temp value of the rating
        boolFormIsValid = false;
        showErrorMessage(jQuery("#overlayRatingControl"), "Please enter a rating");
    }
    //console.log(boolFormIsValid);
  
    return boolFormIsValid;
}

function validateShoppingListCreateNewForm(){
    //validate the title field
    var boolFormIsValid = true;
    if (jQuery.trim(jQuery("#tb_shoppinglist_title").val()) < 1) {
        boolFormIsValid = false;
        showErrorMessage(jQuery("#tb_shoppinglist_title"), shoppingListMessages["shoppinglist.title_missing.message"]);
    
    }else if (jQuery.trim(jQuery("#tb_shoppinglist_title").val()).length > 80) {
        boolFormIsValid = false;
        showErrorMessage(jQuery("#tb_shoppinglist_title"), shoppingListMessages["shoppinglist.use_less_80_char.message"]);
    }

    return boolFormIsValid;
}

function validateShoppingListAddItemForm(){
//for now we validate the two mandotory fields only
   var boolFormIsValid = true;
    if (jQuery.trim(jQuery("#tb_shoppinglist_item_name").val()) < 1) {
        boolFormIsValid = false;
        showErrorMessage(jQuery("#tb_shoppinglist_item_name"), shoppingListMessages["shoppinglist.item.name_missing.message"]);
    } else if (jQuery.trim(jQuery("#tb_shoppinglist_item_quantity").val()) < 1 ||!isNumeric(jQuery("#tb_shoppinglist_item_quantity").val())){
    boolFormIsValid = false;
        showErrorMessage(jQuery("#tb_shoppinglist_item_quantity"), shoppingListMessages["shoppinglist.item.quantity_wrong.message"]);
        
    }
    return boolFormIsValid;

}

function validateShoppingListAddProductItemForm(){
//for now we validate the two mandatory fields only
   var boolFormIsValid = true;

    if (jQuery('.scrollingListBox label input:radio:checked').length < 1) {
        boolFormIsValid = false;
        showErrorMessage(jQuery("#scrollingListBox"), shoppingListMessages["shoppinglist.item.select_products.message"]);
    } else if (jQuery.trim(jQuery("#tb_shoppinglist_item_quantity2").val()) < 1){
    	boolFormIsValid = false;
    	clearErrorMessage(jQuery("#scrollingListBox"));     
        showErrorMessage(jQuery("#tb_shoppinglist_item_quantity2"), shoppingListMessages["shoppinglist.item.quantity_wrong.message"]);
     
    } else {
    
    clearErrorMessage(jQuery("#tb_shoppinglist_item_quantity2"));
  
    }
    return boolFormIsValid;
}


jQuery(document).ready(function(){
    if (jQuery("#overlayRating").length > 0) {
	    jQuery("#overlayRating").children().not(":input, .title").hide();
	    jQuery("#overlayRating").stars({
	        cancelShow: false
	    });
    }
    jQuery("fieldset div.row input").focus(function(){
		jQuery(this).parent().find("div.instructional").show();
	});
	jQuery("fieldset div.row input").blur(function(){
		jQuery(this).parent().find("div.instructional").hide();
	});
    jQuery("fieldset div.row textarea").focus(function(){
		jQuery(this).parent().find("div.instructional").show();
	});
	jQuery("fieldset div.row textarea").blur(function(){
		jQuery(this).parent().find("div.instructional").hide();
	});
	
	if (jQuery("#overlayWidgetTabNavigationContainer").length > 0){
	    jQuery("#overlayWidgetTabNavigationContainer").tabs();
	}
	
	if (jQuery("div#shoppingListsWidget input.spinner").length > 0) {
		jQuery("div#shoppingListsWidget input.spinner").spin({imageBasePath:"images/",max:100,min:0});
		jQuery("div#shoppingListsWidget input.spinner").numeric();
		jQuery("div#shoppingListsWidget input.spinner").change(function(){
			if (jQuery(this).val() > 0){
				jQuery(this).parent().find("input[type='checkbox']").attr("checked", "checked");
			} else {
				jQuery(this).parent().find("input[type='checkbox']").removeAttr("checked");
			}
		});
		
		jQuery("div#shoppingListsWidget div#tab2 fieldset:eq(1) input[type='checkbox']").attr("disabled", "disabled");
	}
	jQuery("div#shoppingListsWidget input[name='shoppinglist_additemtype']").click(function(){
		if (jQuery(this).val() == "recipe"){
			jQuery("div#shoppingListsWidget div#tab2 fieldset:eq(1) input[type='checkbox']").removeAttr("disabled");
			jQuery("div#shoppingListsWidget div#tab2 fieldset:eq(0) input[type='checkbox']").attr("disabled", "disabled");
		} else {
			jQuery("div#shoppingListsWidget div#tab2 fieldset:eq(1) input[type='checkbox']").attr("disabled", "disabled");
			jQuery("div#shoppingListsWidget div#tab2 fieldset:eq(0) input[type='checkbox']").removeAttr("disabled");
		}
	});
	
	jQuery("#dd_delete_account_reason").change(function(){
		if (jQuery("#dd_delete_account_reason option:selected").val() == "other") {
			jQuery("#otherReason").css("display", "block");
		} else {
			jQuery("#otherReason").css("display", "none");
		}
	});
});
