

$(document).ready(function() {       
   function ClearErrors()
   {
        $('#RecipeBlankCredentials').hide();
        $('#ShareRecipeErrors').hide();
        
        $('#RecipeUsername').css('background-color','');
        $('#RecipePassword').css('background-color','');
        $('#RecipePhoto').css('background-color','');
        $('#RecipeName').css('background-color','');
        $('#RecipeIngredients').css('background-color','');
        $('#RecipeInstructions').css('background-color','');
        $('#RecipeMainIngredient').css('background-color','');
        $('#RecipeProduct').css('background-color','');
        $('#Recipe').css('background-color','');
        $('#RecipeChillTime').css('background-color','');
        $('#RecipeCookTime').css('background-color','');
        $('#RecipeTotalServings').css('background-color','');
        $('#RecipePrepTime').css('background-color','');
   }
   
   $('#ShareSignIn').click(function (e) {
        var value = $('#RecipeUsername').val().replace(/^\s\s*/, '').replace(/\s\s*$/, '');
        var success = true;
        ClearErrors();
        if(value == "")
        {
        $('#RecipeUsername').css('background-color','#FEB1A3');
            success = false; 
        } 
        value = $('#RecipePassword').val().replace(/^\s\s*/, '').replace(/\s\s*$/, '');
        if(value == "")
        {
         $('#RecipePassword').css('background-color','#FEB1A3');
            success = false; 
        }
        
        if(!success)
        {
            $('#RecipeBlankCredentials').show();
            e.preventDefault();
        }
        else
        {
            ClearErrors();
        }
   });
   
   $('#SubmitRecipe').click(function(e) {
        var errors = "";
        var success = true;
        ClearErrors();
		
        if($('#RecipePhoto').val() != "" && !$('#RecipePhoto').val().match(/^.*\.(jpg|jpeg)$/i))
        {
            $('#RecipePhoto').css('background-color','#FEB1A3');
            errors += "You must upload an image with the extension .jpg or .jpeg\n";
            success = false;
        }
        if($('#RecipeName').val() == "")
        {
            $('#RecipeName').css('background-color','#FEB1A3');
            //errors+= "You must supply a recipe name.\n";
            success = false;
        }
        if($('#RecipeIngredients').val() == "")
        {
            $('#RecipeIngredients').css('background-color','#FEB1A3');
            //errors+= "You must supply recipe ingredients.\n";
            success = false;
        }
        if($('#RecipeInstructions').val() == "")
        {
            $('#RecipeInstructions').css('background-color','#FEB1A3');
            //errors+= "You must supply recipe instructions.\n";
            success = false;
        }
        if($('#RecipeMainIngredient').val() == "")
        {
            $('#RecipeMainIngredient').css('background-color','#FEB1A3');
            //errors+= "You must supply a main ingredient.\n";
            success = false;
        }
        if($('#RecipeProduct').val() == "")
        {
            $('#RecipeProduct').css('background-color','#FEB1A3');
            //errors+= "You must supply a recipe product.\n";
            success = false;
        }
        
        if($('#Recipe').val() == "")
        {
            $('#Recipe').css('background-color','#FEB1A3');
            //errors+= "You must supply a recipe product.\n";
            success = false;
        }
        
        var value = $('#RecipeChillTime').val().replace(/^\s\s*/, '').replace(/\s\s*$/, ''); 
        if(value != "" && !IsNumeric(value))
        { 
            $('#RecipeChillTime').css('background-color','#FEB1A3');
            //errors+= "Recipe Chill Time must be numeric.\n"; 
            success = false; 
        } 
        
        value = $('#RecipeCookTime').val().replace(/^\s\s*/, '').replace(/\s\s*$/, '');
        if(value != "" && !IsNumeric(value))
        { 
            $('#RecipeCookTime').css('background-color','#FEB1A3');
            //errors+= "Recipe Cook Time must be numeric.\n"; 
            success = false; 
        } 
        
        value = $('#RecipeTotalServings').val().replace(/^\s\s*/, '').replace(/\s\s*$/, '');
        if(value != "" && !IsNumeric(value))
        { 
            $('#RecipeTotalServings').css('background-color','#FEB1A3');
            //errors+= "Recipe Total Servings must be numeric.\n"; 
            success = false; 
        } 
        
        value = $('#RecipePrepTime').val().replace(/^\s\s*/, '').replace(/\s\s*$/, '');
        if(value != "" && !IsNumeric(value))
        { 
            $('#RecipePrepTime').css('background-color','#FEB1A3');
            //errors+= "Recipe Prep Time must be numeric.\n"; 
            success = false; 
        }        
        
        if(success && $('#IAgree:checked').val() == null)
        {
            alert('You must agree to the terms and conditions.');
            $('#IAgree').css('background-color','#FEB1A3');
            success = false;
        }
		
		if (errors.length > 0) {
			alert(errors);
			sucess = false;
		}
        
        if(!success)
        {
            $('#ShareRecipeErrors').show();			
            e.preventDefault();
        }
    });
    
    function IsNumeric(input) 
    { 
       return (input - 0) == input && input.length > 0; 
    }   
});
