 $(document).ready(function(){
    $("#searchfoodfrm").validate({
        rules: {
            srchfood: {
                required: true,
                minlength: 3
            }
        },
        messages: {
            srchfood: {
                required: 'Please enter food name',
                minlength: 'Name must be three char long'
            }
        },
            errorPlacement: function(error, element) {
            
            if (element.attr("name") == "srchfood")
            error.insertAfter("#search_btn");
            else
            error.insertAfter(element);

    }
    });
  });
 $(document).ready(function(){
    $("#searchcatfrm").validate({
        rules: {
            category: {
                required: true
            }
        },
        messages: {
            category: {
                required: ''
            }
        }       
    });
  });
$(document).ready(function(){
    $("#nl_frm").validate({
        rules: {
            nl_emailid: {
                required: true,
                email: true
            }
        },
        messages: {
            nl_emailid: {
                required: 'Please enter your Email-id',
                email : 'Please enter valid Email-id'
            }
        }
    });
  });
    
$(document).ready(function(){
$('#sender_name').alpha({allow:" "});
    $("#sharefrm").validate({
        rules: {
            sender_name: {
                       required: true
            },
            sender_email: {
                required: true,
                email: true
            },
            rec_email: {
                required: true,
                email: true
            }
        },
        messages: {
            sender_name: {
                required: 'Please enter your name'
            },
            sender_email: {
                required: 'Please enter your Email-id',
                email : 'Please enter valid Email-id'
            },
            rec_email: {
                required: "Please enter friend's Email-id",
                email : 'Please enter valid Email-id'
            }
        }
    });
  });
function updateCal() {

    form = document.forms;
    val = form.foods.qty.value;
    weight=form.foods.weight.value;
    calorie1=form.foods.calorie1.value;
    kJs1=form.foods.kJs1.value;
    protein1 = form.foods.protein1.value;
    carbohydrates1 = form.foods.carbohydrates1.value;
    fiber1 = form.foods.fiber1.value;
    saturated1 =form.foods.saturated1.value;
    polyunsaturated1 =form.foods.polyunsaturated1.value;
    monounsaturated1 =form.foods.monounsaturated1.value;
    suger1 = form.foods.suger1.value;
    sodium1 = form.foods.sodium1.value;
    vitaminA1 = form.foods.vitaminA1.value;
    vitaminC1 = form.foods.vitaminC1.value;
    calcium1 = form.foods.calcium1.value;
    iron1 = form.foods.iron1.value;
    cholesterol1 = form.foods.cholesterol1.value;
    fat1 =form.foods.fat1.value;
    food_id =form.foods.food_id.value;
    if(food_id==100004 || food_id==100003 || food_id==100002 || food_id==100001) {
        calorie = Math.round(calorie1 * weight * val * 10)/10;
        kJs = Math.round(kJs1 * weight * val* 10)/10;
        protein = Math.round(protein1 * weight * val* 10)/10;
        carbohydrates = Math.round(carbohydrates1 * weight * val* 10)/10;
        fiber = Math.round(fiber1 * weight * val* 10)/10;
        saturated = Math.round(saturated1 * weight * val* 10)/10;
        polyunsaturated = Math.round(polyunsaturated1 * weight * val* 10)/10;
        monounsaturated = Math.round(monounsaturated1 * weight * val* 10)/10;
        suger = Math.round(suger1 * weight *  val* 10)/10;
        sodium = Math.round(sodium1 * weight * val* 10)/10;
        vitaminA = Math.round(vitaminA1 * weight * val* 10)/10;
        vitaminC = Math.round(vitaminC1 * weight * val* 10)/10;
        calcium = Math.round(calcium1 * weight * val* 10)/10;
        iron = Math.round(iron1 * weight * val* 10)/10;
        cholesterol = Math.round(cholesterol1 * weight * val* 10)/10;
        fat = Math.round(fat1 * weight * val* 10)/10;
    } else {
        calorie = Math.round(calorie1 * weight * val);
        kJs = Math.round(kJs1 * weight * val);
        protein = Math.round(protein1 * weight * val);
        carbohydrates = Math.round(carbohydrates1 * weight * val);
        fiber = Math.round(fiber1 * weight * val);
        saturated = Math.round(saturated1 * weight * val);
        polyunsaturated = Math.round(polyunsaturated1 * weight * val);
        monounsaturated = Math.round(monounsaturated1 * weight * val);
        suger = Math.round(suger1 * weight *  val);
        sodium = Math.round(sodium1 * weight * val);
        vitaminA = Math.round(vitaminA1 * weight * val);
        vitaminC = Math.round(vitaminC1 * weight * val);
        calcium = Math.round(calcium1 * weight * val);
        iron = Math.round(iron1 * weight * val);
        cholesterol = Math.round(cholesterol1 * weight * val);
        fat = Math.round(fat1 * weight * val);
    }
    document.getElementById("_calorie").innerHTML=calorie;
    document.getElementById("_protein").innerHTML='<b>'+protein+' g</b>';
    document.getElementById("_carbohydrates").innerHTML='<b>'+carbohydrates+' g</b>';
    document.getElementById("_fiber").innerHTML=fiber+' g';
    document.getElementById("_saturated").innerHTML=saturated+' g';
    document.getElementById("_polyunsaturated").innerHTML=polyunsaturated+' g';
    document.getElementById("_monounsaturated").innerHTML=monounsaturated+' g';
    document.getElementById("_fat").innerHTML='<b>'+fat+' g</b>';
//  document.getElementById("_suger").innerHTML=suger+' g';
    document.getElementById("_sodium").innerHTML='<b>'+sodium+' mg</b>';
    document.getElementById("_cholesterol").innerHTML='<b>'+cholesterol+' mg</b>';

    total = fat+protein+carbohydrates;
    
    //caculating the calories in nutrient
    protein_cl=protein*4;
    fat_cl=fat*9;
    saturated_cl=saturated*9;
    cholesterol_cl=cholesterol*9;
    carbohydrates_cl=carbohydrates*4;
    suger_cl=suger*4;
    sodium_cl=sodium*4;
    vitaminA_cl = vitaminA;

    total_cals=protein_cl+fat_cl+carbohydrates_cl+suger_cl;

    //percentage in calories
    if(!total_cals){total_cals=1;}
    total_fat_pr = Math.round((fat_cl / total_cals ) * 100);
    total_saturated_pr = Math.round((saturated_cl / total_cals ) * 100);
    total_cholesterol_pr = Math.round((cholesterol_cl / total_cals ) * 100);
    total_protein_pr =Math.round((protein_cl / total_cals ) * 100);
    total_suger_pr = Math.round((suger_cl / total_cals ) * 100);
    total_carbs_pr = Math.round(((carbohydrates_cl / total_cals ) * 100 + total_suger_pr));
    total_sodium_pr = Math.round((sodium_cl / total_cals ) * 100);
    total_vitaminA_pr = Math.round((vitaminA_cl / total_cals ) * 100);
    total_vitaminC_pr = Math.round((vitaminC / total_cals ) * 100);
    total_calcium_pr = Math.round((calcium / total_cals ) * 100);
    total_iron_pr = Math.round((iron / total_cals ) * 100);

    document.getElementById("_fat_pr").innerHTML='<b>'+total_fat_pr+' %</b>';
    document.getElementById("_saturated_pr").innerHTML='<b>'+total_saturated_pr+' %</b>';
    document.getElementById("_cholesterol_pr").innerHTML='<b>'+total_cholesterol_pr+' %</b>';
    document.getElementById("_sodium_pr").innerHTML='<b>'+total_sodium_pr+' %</b>';
    document.getElementById("_carbs_pr").innerHTML='<b>'+total_carbs_pr+' %</b>';
    document.getElementById("_vitaminA_pr").innerHTML='<b>'+total_vitaminA_pr+' %</b>';
    document.getElementById("_vitaminC_pr").innerHTML='<b>'+total_vitaminC_pr+' %</b>';
    document.getElementById("_calcium_pr").innerHTML='<b>'+total_calcium_pr+' %</b>';
    document.getElementById("_iron_pr").innerHTML='<b>'+total_iron_pr+' %</b>';
}
function getUniqeId() {
 todayDate=new Date();
 nowSeconds=todayDate.getSeconds();
 return '/usid/'+nowSeconds*Math.random();
}
function changeWeight() {
    var sel_weight = document.foods.unit.options[document.foods.unit.selectedIndex].value;
    var weight=new Array();
    weight = sel_weight.split('_');
    document.foods.weight.value=weight[1];
    updateCal();
}
$(document).ready(function(){
$('#firstname').alpha({allow:"'"});
$('#lastname').alpha({allow:"'"});
$('#state').alpha({allow:" "});
$('#city').alpha({allow:" "});
$('#zipcode').alphanumeric({allow:""});
$("#registerfrm").validate({
        rules: {
            emailid: {
                required: true,
                email: true
            },
            password: {
                required: true,
                minlength: 6,
                maxlength: 20
            },
            cpassword: {
                required: true,
                equalTo: "#password"
            },
            firstname: {
                required: true,
                minlength: 2,
                maxlength: 20
            },
            lastname: {
                required: true,
                minlength: 2,
                maxlength: 20
            },
            city: {
                minlength: 4
            },
            state: {
                minlength: 4
            },
            zipcode: {
                minlength: 4
            },
            website: {
                minlength: 4,
                url: true
            },
            captcha: {
                required: true
            },
            terms_of_use: {
                required: true
            }
        },
        messages: {
            emailid: {
                required: 'Please enter your emailid',
                email: 'Please enter valid emailid'
            },
            password: {
                required: 'Please enter password',
                minlength: 'Password must be 6 characters long',
                maxlength: 'Password cannot be more than 20 characters'
            },
            cpassword: {
                required: 'Please enter confirm password',
                equalTo: 'Please enter the same password as above'
            },
            firstname: {
                required: 'Please enter your first name',
                minlength: 'Firstname must be atleast 2 characters long',
                maxlength: 'Firstname cannot be more than 20 characters'
            },
            lastname: {
                required: 'Please enter your last name',
                minlength: 'Lastname must be atleast 2 characters long',
                maxlength: 'Lastname cannot be more than 20 characters'
            },
            city: {
                minlength: 'City must be atleast 4 characters long'
            },
            state: {
                minlength: 'State must be atleast 4 characters long'
            },
            zipcode: {
                minlength: 'Zipcode must be atleast 4 characters long'
            },
            website: {
                minlength: 'Website must be atleast 4 characters long',
                url: 'Please enter valid url'
            },
            captcha: {
                required: 'Please enter the text in the image above'
            },
            terms_of_use: {
                required: 'Please check the terms of use'
            }
        }
    });
  });
$(document).ready(function(){
$("#loginfrm").validate({
        rules: {
            emailid: {
                required: true,
                email: true
            },
            password: {
                required: true
            }
        },
        messages: {
            emailid: {
                required: 'Please enter your emailid',
                email: 'Please enter valid emailid'
            },
            password: {
                required: 'Please enter password'
            }
        }
    });
  });
$(document).ready(function(){
$("#changepassfrm").validate({
        rules: {
            old_password: {
                required: true,
                minlength: 6,
                maxlength: 20
            },
            password: {
                required: true,
                minlength: 6,
                maxlength: 20
            },
            confirm_password: {
                required: true,
                minlength: 6,
                maxlength: 20,
                equalTo: "#password"
            }
        },
        messages: {
            old_password: {
                required: 'Please enter old password',
                minlength: 'Password must be 6 characters long',
                maxlength: 'Password cannot be more than 20 charaters'
            },
            password: {
                required: 'Please enter new password',
                minlength: 'Password must be 6 characters long',
                maxlength: 'Password cannot be more than 20 charaters'
            },
            confirm_password: {
                required: 'Please enter confirm password',
                minlength: 'Password must be 6 characters long',
                maxlength: 'Password cannot be more than 20 charaters',
                equalTo: 'Please enter the same password as above'
            }
        }
    });
  });
$(document).ready(function(){
$("#forgotfrm").validate({
        rules: {
            emailid: {
                required: true,
                email: true
            }
        },
        messages: {
            emailid: {
                required: 'Please enter your emailid',
                email: 'Please enter valid emailid'
            }
        }
    });
  });
$(document).ready(function(){
$("#profilephotofrm").validate({
        rules: {
            profilephoto: {
                required: true,
                accept: "jpeg|jpg|gif|png"
            }
        },
        messages: {
            profilephoto: {
                required: 'Please select an image',
                accept: 'Please upload images only'
            }
        }
    });
  });
$(document).ready(function(){
$('#caption').alpha({allow:" "});
$("#albumfrm").validate({
        rules: {
            caption: {
                required: true
            },
            albumphoto: {
                required: true,
                accept: "jpeg|jpg|gif|png"
            }
        },
        messages: {
            caption: {
                required: 'Please enter caption'
            },
            albumphoto: {
                required: 'Please select an image',
                accept: 'Please upload images only'
            }
        }
    });
  });
function deletechecked(message) {
    var answer = confirm(message)
    if (answer){
        return true;
    }
    return false;
} 
$(document).ready(function(){
    $("#fitnessfrm").validate({
        rules: {
            fitness: {
                required: true
            }
        },
        messages: {
            fitness: {
                required: 'Please enter your fitness goal'
            }
        }       
    });
});
$(document).ready(function(){
    // Add onclick handler to button w/id edit
    $("#edit").click(function(){
    //show the hidden div
    $("#myfitness").show('slow');
    $("#viewfitness").hide();
      });
    // Add onclick handler to button w/id cancel
    $("#cancel").click(function(){
    //show the hidden div
    $("#myfitness").hide();
    $("#viewfitness").show('slow');
    });
});
$(document).ready(function(){
    $("#searchmemberfrm").validate({
        rules: {
            searchmember: {
                required: true
            }
        },
        messages: {
            searchmember: {
                required: 'Please enter search value'
            }
        }       
    });
});
$(document).ready(function() {
    $("#unit").change(function() {
       switch($("#unit").val()) {
        case 'met' :
            $("#english_height").hide();
            $("#metric_height").show('fast');
            $("#english_weight").hide();
            $("#metric_weight").show('fast');
            break;
        case 'eng' :
            $("#metric_height").hide();
            $("#english_height").show('fast');
            $("#metric_weight").hide();
            $("#english_weight").show('fast');
            break;
       }
    });
});
$(document).ready(function() {
    $('#height_ft').numeric({allow:""});
    $('#height_in').numeric({allow:""});
    $('#weight_lb').numeric({allow:"."});
    $('#height_cm').numeric({allow:"."});
    $('#weight_kilo').numeric({allow:"."});
    $('#age').numeric({allow:"."});
    $("#calculate").click(function() {
    var gender = $("input[@name='gender']:checked").val();
    switch (gender) {
        case 'M' :
            switch($("#unit").val()) {
                case 'met' :
                var height1 = $("#height_ft").val();
                var height2 = $("#height_in").val(); 
                var height3 = parseFloat(height1 * 12) + parseFloat(height2);
                var weight = $("#weight_lb").val();
                var age = $("#age").val();
                if ((height1 == '') || (height2 == '') || (height3 == '') || (weight == '') || ((age == '') || (age == 0))) {
                    $("#BMR").hide();
                    $("#BMR_error").html('Insufficient data!'); 
                } else {
                    var result = Number (66 + ( 6.23 * weight ) + ( 12.7 * height3 ) - ( 6.8 * age )).toFixed(2);
                    switch (result) {
                        case 'NaN' :
                        $("#BMR").hide();
                        $("#BMR_error").html('Insufficient data!'); 
                        break;
                        default :
                        $("#BMR").show('fast');
                        $("#BMR_error").hide();
                        $("#result").html(result); 
                        }
                }
                break;
                case 'eng' :
                var height3 = $("#height_cm").val();
                var weight = $("#weight_kilo").val();
                var age = $("#age").val();
                if ((height1 == '') || (height2 == '') || (height3 == '') || (weight == '') || ((age == '') || (age == 0))) {
                    $("#BMR").hide();
                    $("#BMR_error").html('Insufficient data!'); 
                } else {
                    var result = Number (66 + ( 13.7 * weight ) + ( 5 * height3 ) - ( 6.8 * age )).toFixed(2);
                    switch (result) {
                        case 'NaN' :
                        $("#BMR").hide();
                        $("#BMR_error").html('Insufficient data!'); 
                        break;
                        default :
                        $("#BMR").show('fast');
                        $("#BMR_error").hide();
                        $("#result").html(result); 
                        }
                }
                break;
            }
            break;
        case 'F' :
            switch($("#unit").val()) {
                case 'met' :
                var height1 = $("#height_ft").val();
                var height2 = $("#height_in").val(); 
                var height3 = parseFloat(height1 * 12) + parseFloat(height2);
                var weight = $("#weight_lb").val();
                var age = $("#age").val();
                if ((height1 == '') || (height2 == '') || (height3 == '') || (weight == '') || ((age == '') || (age == 0))) {
                    $("#BMR").hide();
                    $("#BMR_error").html('Insufficient data!'); 
                } else {
                    var result = Number (655 + ( 4.35 * weight ) + ( 4.7 * height3 ) - ( 4.7 * age )).toFixed(2);
                    switch (result) {
                        case 'NaN' :
                        $("#BMR").hide();
                        $("#BMR_error").html('Insufficient data!'); 
                        break;
                        default :
                        $("#BMR").show('fast');
                        $("#BMR_error").hide();
                        $("#result").html(result);
                        }
                } 
                break;
                case 'eng' :
                var height3 = $("#height_cm").val();
                var weight = $("#weight_kilo").val();
                var age = $("#age").val();
                if ((height1 == '') || (height2 == '') || (height3 == '') || (weight == '') || ((age == '') || (age == 0))) {
                    $("#BMR").hide();
                    $("#BMR_error").html('Insufficient data!'); 
                } else {
                    var result = Number (655 + ( 9.6 * weight ) + ( 1.8 * height3 ) - ( 4.7 * age )).toFixed(2); 
                    switch (result) {
                        case 'NaN' :
                        $("#BMR").hide();
                        $("#BMR_error").html('Insufficient data!'); 
                        break;
                        default :
                        $("#BMR").show('fast');
                        $("#BMR_error").hide();
                        $("#result").html(result); 
                        } 
                }
                break;
            }
            break;
        }
    });
});

$(document).ready(function(){
$('#calorie').numeric({allow:"."});
$('#tags').alphanumeric({allow:", "});
$("#mealentryfrm").validate({
        rules: {
            food: {
                required: true,
                minlength: 2,
                maxlength: 150
            },
            calorie: {
                required: true,
                maxlength: 4
            },
            tags: {
                minlength: 2
            }
        },
        messages: {
            food: {
                required: 'Please enter food name',
                minlength: 'Food name must be 2 characters long',
                maxlength: 'Food name cannot be more than 150 charaters'
            },
            calorie: {
                required: 'Please enter calorie',
                maxlength: 'Calorie must be 4 characters long'
            },
            tags: {
                minlength: 'Tag must be 2 characters long'
            }
        }
    });
  });
$(document).ready(function(){
$('#calorie').numeric({allow:"."});
$('#tags').alphanumeric({allow:", "});
$("#editmealentryfrm").validate({
        rules: {
            food: {
                required: true,
                minlength: 2,
                maxlength: 150
            },
            calorie: {
                required: true,
                maxlength: 4
            },
            tags: {
                minlength: 2
            }
        },
        messages: {
            food: {
                required: 'Please enter food name',
                minlength: 'Food name must be 2 characters long',
                maxlength: 'Food name cannot be more than 150 charaters'
            },
            calorie: {
                required: 'Please enter calorie',
                maxlength: 'Calorie must be 4 characters long'
            },
            tags: {
                minlength: 'Tag must be 2 characters long'
            }
        }
    });
  });

function displayeditmode(i, day) {
    var editmode = "editmode"+i;
    var editmodetag = "editmodetag"+day;
    var viewmode = "viewmode"+i;
    //var viewmodetotal = "viewmodetotal"+i;

    document.getElementById(editmode).style.display = "block";
//     document.getElementById(editmodetag).style.display = "block";
    if (document.getElementById(editmode).style.display == "block") {
        document.getElementById(editmodetag).style.display = "block"
    } else {
     document.getElementById(editmodetag).style.display = "none";
    }
    document.getElementById(viewmode).style.display = "none";
//     document.getElementById(viewmodetotal).style.display = "none";
}
function displayviewmode(i, day) {
    var editmode = "editmode"+i;
    var editmodetag = "editmodetag"+day;
    var viewmode = "viewmode"+i;
   // var viewmodetotal = "viewmodetotal"+i;
//alert(editmodetag);
    document.getElementById(viewmode).style.display = "block";
//     document.getElementById(viewmodetotal).style.display = "block";
    document.getElementById(editmode).style.display = "none";

    if (document.getElementById(editmode).style.display == "block") {
        document.getElementById(editmodetag).style.display = "block"
    } else {
     document.getElementById(editmodetag).style.display = "none";
    }
}

$(document).ready(function(){
$('#calorie').numeric({allow:"."});
$('#tags').alphanumeric({allow:", "});
$("#exercisefrm").validate({
        rules: {
            exercise: {
                required: true,
                minlength: 2,
                maxlength: 150
            },
            calorie: {
                required: true,
                maxlength: 4
            },
            tags: {
                minlength: 2
            }
        },
        messages: {
            exercise: {
                required: 'Please enter exercise',
                minlength: 'Food name must be 2 characters long',
                maxlength: 'Food name cannot be more than 150 charaters'
            },
            calorie: {
                required: 'Please enter calorie',
                maxlength: 'Calorie must be 4 characters long'
            },
            tags: {
                minlength: 'Tag must be 2 characters long'
            }
        }
    });
  });


$(document).ready(function(){
$('#calorie').numeric({allow:"."});
$('#tags').alphanumeric({allow:", "});
$("#editexercisefrm").validate({
        rules: {
            exercise: {
                required: true,
                minlength: 2,
                maxlength: 150
            },
            calorie: {
                required: true,
                maxlength: 4
            },
            tags: {
                minlength: 2
            }
        },
        messages: {
            exercise: {
                required: 'Please enter exercise',
                minlength: 'Food name must be 2 characters long',
                maxlength: 'Food name cannot be more than 150 charaters'
            },
            calorie: {
                required: 'Please enter calorie',
                maxlength: 'Calorie must be 4 characters long'
            },
            tags: {
                minlength: 'Tag must be 2 characters long'
            }
        }
    });
  });

$(document).ready(function(){
$("#searchmealfrm").validate({
        rules: {
            searchmeal: {
                required: true,
                minlength: 2,
                maxlength: 150
            }
        },
        messages: {
            searchmeal: {
                required: 'Please enter food name',
                minlength: 'Food name must be 2 characters long',
                maxlength: 'Food name cannot be more than 150 charaters'
            }
        }
    });
  });

$(document).ready(function(){
$("#searchexercisefrm").validate({
        rules: {
            searchexercise: {
                required: true,
                minlength: 2,
                maxlength: 150
            }
        },
        messages: {
            searchexercise: {
                required: 'Please enter exercise',
                minlength: 'Food name must be 2 characters long',
                maxlength: 'Food name cannot be more than 150 charaters'
            }
        }
    });
  });

$(document).ready(function(){
    // Add onclick handler to button w/id edit
    $("#dispmeal").click(function(){
    //show the hidden div
    $(this).removeClass("un_selected").addClass("selected");
    $('#dispexercise').removeClass("selected").addClass("un_selected");
    $("#meal").show('fast');
    $("#exercise").hide();
      });
    // Add onclick handler to button w/id cancel
    $("#dispexercise").click(function(){
    //show the hidden div
    $(this).removeClass("un_selected").addClass("selected");
    $('#dispmeal').removeClass("selected").addClass("un_selected");
    $("#meal").hide();
    $("#exercise").show('fast');
    });
});

$(document).ready(function(){
    $("#country").change(function(){
    if ($("#country").val() != '222') {
        $("#other_state").show('fast');
        $("#us_state").hide();
    } else {
        $("#us_state").show('fast');
        $("#other_state").hide();
    } 
 });  
});
