var KaExternal  = {};
KaExternal.Info = {};

var asID = 111703;


/* -- Functions -- */
function getKaExternal(){
    var userid = (Ka.Info.PROFILEID == "") ? Ka.Info.PROFILEID : Ka.Info.PROFILEID;
    var url = 'http://hosting.kicknetwork.com/foodlovers/userFunctions.php?task=getUserInfo&userId=' + userid + '&callback=setUserInfo';
    aObj = new JSONscriptRequest(url);
    aObj.buildScriptTag();
    aObj.addScriptTag();
}

function addQuestionsToPage(id){
    var d = new Date();
    var curr_year = d.getFullYear();

    var month = ["January","February","March","April","May","June", "July","August","September","October","November","December"];
    program_start  = "<select id=\"program_start_month\" name=\"program_start_month\"><option value=''>-</option>";
    for (var i = 0; i < month.length;++i){
        program_start += "<option value='" + (i + 1) + "'>" + month[i] + "</option>";
    }
    program_start += "</select>&nbsp;";

    program_start += "<select id=\"program_start_day\" name=\"program_start_day\"><option value=''>-</option>";
    for(i = 1; i <= 31; i++){
        program_start += "<option value='" + i + "'>" + i + "</option>";
    }
    program_start += "</select>&nbsp;";
    program_start += "<select id=\"program_start_year\" name=\"program_start_year\"><option value=''>-</option>";
    for(i = curr_year; i >= 1910; i--){
        program_start += "<option value='" + i + "'>" + i + "</option>";
    }
    program_start += "</select>";



    /* Beginning Weight */

    $j(id).append("<label for=\"gender\">Gender:</label><select name=\"gender\" id=\"gender\"><option value=\"\">-</option><option value=\"M\">Male</option><option value=\"F\">Female</option></select>");
    $j(id).append("<br/><label for=\"beginning_weight\">Beginning Weight:</label><input id=\"beginning_weight\" type=\"text\" value=\"\" size=\"30\" name=\"beginning_weight\"/>");

    /* Current Weight */
    $j(id).append("<label for=\"current_weight\">Current Weight:</label><input id=\"current_weight\" type=\"text\" value=\"\" size=\"30\" name=\"current_weight\"/>");

    /* Goal Weight */
    $j(id).append("<label for=\"goal_weight\">Goal Weight:</label><input id=\"goal_weight\" type=\"text\" value=\"\" size=\"30\" name=\"goal_weight\"/>");

    /* Total Weight Lost */
    $j(id).append("<label for=\"total_weight_lost\">Total Weight Lost (calculated based on start weight and current weight):</label><input id=\"total_weight_lost\" type=\"text\" value=\"\" size=\"4\" name=\"total_weight_lost\" readonly/>");

    /* Total Weight To Lose */
    $j(id).append("<label for=\"total_weight_to_lose\">Total Weight To Lose (calculated based on start weight and goal weight):</label><input id=\"total_weight_to_lose\" type=\"text\" value=\"\" size=\"4\" name=\"total_weight_to_lose\" readonly/>");

    /* Program Start */
    $j(id).append("<label for=\"program_start\">Program Start:</label>"+program_start);

}

function setUserInfo(data){
    var json = JSON.stringify(data);
    if(data.USERID){
        userid = data.USERID;
    }else if(Ka.Info.PROFILEID){
        userid = Ka.Info.PROFILEID;
    }else{
        return false;
    }

    /* Clear old info **/
    eraseCookieSS('ka_external_' + userid);
    KaExternal.Info = null;

    /* Put new info **/
    createCookieSS('ka_external_' + userid, json);
    KaExternal.Info = data;
    return true;
}


function checkUser(data){
    if(data.status >= 1){
        eraseCookieSS('fl_register');
        eraseCookieSS('fl_beginning_weight');
        eraseCookieSS('fl_current_weight');
        eraseCookieSS('fl_goal_weight');
        eraseCookieSS('fl_total_weight_to_lose');
        eraseCookieSS('fl_program_start');
        eraseCookieSS('fl_ka_username');
        eraseCookieSS('fl_birthday');
        eraseCookieSS('fl_gender');

        /* Update the KaExternal Object */
        KaExternal.Info = null;
        eraseCookieSS('ka_external_' + Ka.Info.PROFILEID);
        getKaExternal();

        return true;
    }else{
        alert("Error saving some data\nError: " + data.msg);
        return false;
    }
}

function storeInfo(){
    createCookieSS('fl_register',1,5);

    var begin  = ($j('#beginning_weight').val() > 0) ? $j('#beginning_weight').val() : 0;
    var curr   = ($j('#current_weight').val() > 0)   ? $j('#current_weight').val()   : 0;
    var goal   = ($j('#goal_weight').val() > 0)      ? $j('#goal_weight').val()      : 0;
    var gender = ($j('#gender').val() > 0)           ? $j('#gender').val()           : 0;

    createCookieSS('fl_beginning_weight', begin, 5);
    createCookieSS('fl_current_weight',   curr,  5);
    createCookieSS('fl_goal_weight',      goal,  5);
    createCookieSS('fl_gender',           gender,5);

    /** Format the birthday **/
    birth_month = $j('#dobMonth').val();
    birth_day   = $j('#dobDay').val();
    birth_year  = $j('#dobYear').val();
    birthday    = birth_year + "-" + birth_month + "-" + birth_day + " 00:00:00";
    createCookieSS('fl_birthday',birthday,5);
    
    /** Format the start date **/
    start_month = $j('#program_start_month').val();
    start_day   = $j('#program_start_day').val();
    start_year  = $j('#program_start_year').val();
    program_start = start_year + "-" + start_month + "-" + start_day + " 00:00:00";
    createCookieSS('fl_program_start',program_start,5);

    if($j("#RegisterUser").length > 0){
        createCookieSS('fl_ka_username',$j('#username').val(),5);
        setTimeout('document.RegisterUser.submit()', 1000);
    }else{
        createCookieSS('fl_ka_username',Ka.Info.USERNAME,5);
    }
}

function CoppaCheck(form,it){
    Ka.formValidation.actions.disableSubmit(form);
    storeInfo();
}

function createCookieSS(name,value,days) {
    var expires = '';
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = "; expires="+date.toGMTString();
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookieSS(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0) === ' '){
                    c = c.substring(1,c.length);
                    if (c.indexOf(nameEQ) === 0){return c.substring(nameEQ.length,c.length);}
                }
	}
	return null;
}

function eraseCookieSS(name) {
	createCookieSS(name,"",-1);
}

function addForumBadge(data){
    for (var idx in data) {
        var ele = $j('.ka_forumDetails li.ka_userName a:eq('+idx+')');
        if(typeof data[idx] == 'object' && data[idx].badge > 0){
            ele.parent().parent().append("<li class='ka_userBadge'>" + data[idx].badge + "</li>");
        }else if(typeof data[idx] == 'object' && data[idx].badge <= 0){
            ele.parent().parent().append("<li class='ka_userBadgeStart'>&nbsp;</li>");
        }
    }
}

function addProfileBadge(data){
    if(data[0].badge > 0){
        $j('#ka_profileLocItem').after('<li class="ka_userBadge">' + data[0].badge.trim() + '</li>');
    }else{
        $j('#ka_profileLocItem').after('<li class="ka_userBadgeStart">&nbsp;</li>');
    }
}

function profilePage(){
    var myregexp         = /(\d+)/im;
    var strong           = /<strong>([\s\S]*)<\/strong>/im;

    var beginning_weight = 0;
    var current_weight   = 0;
    var goal_weight      = 0;
    var badge            = 0;

    /** Loop through extra params and get/verify data **/
    $j("#ka_profileAboutMeExt p").each(function(idx){
        var match = strong.exec($j("#ka_profileAboutMeExt p:nth-child(" + idx + ")").html());
        result = (match != null) ? match[1] : "";

        var stuff = myregexp.exec($j("#ka_profileAboutMeExt p:nth-child(" + idx + ")").html());

        switch(result){
            case "Beginning Weight":
                beginning_weight = stuff[0];
                break;
            case "Current Weight":
                current_weight   = stuff[0];
                break;
            case "Goal Weight":
                goal_weight      = stuff[0];
                break;
        }
    });

    exists = checkUserExists(Ka.Info.PROFILEID, current_weight, beginning_weight, goal_weight);

    if(current_weight > 0){
        badge = Math.floor(parseInt((beginning_weight - current_weight))/5)*5
    }else{
        badge = 0;
    }

    var b_weight = false;
    var c_weight = false;
    var g_weight = false;

    $j("#ka_profileAboutMeExt p").each(function(idx){
        var match = strong.exec($j("#ka_profileAboutMeExt p:nth-child(" + idx + ")").html());
        result = (match != null) ? match[1] : "";

        switch(result){
            case "Beginning Weight":
                b_weight = true;
                $j("#ka_profileAboutMeExt p:nth-child(" + idx + ")").html('<strong>Beginning Weight</strong><br>' + beginning_weight);
                break;
            case "Current Weight":
                c_weight = true;
                $j("#ka_profileAboutMeExt p:nth-child(" + idx + ")").html('<strong>Current Weight</strong><br>' + current_weight);
                break;
            case "Goal Weight":
                g_weight = true;
                $j("#ka_profileAboutMeExt p:nth-child(" + idx + ")").html('<strong>Goal Weight</strong><br>' + goal_weight);
                break;
            case "Total Weight to Lose":
                total_weight_to_lose = 0;
                if(goal_weight > 0 && current_weight > 0){
                    total_weight_to_lose = current_weight - goal_weight;
                }

                $j("#ka_profileAboutMeExt p:nth-child(" + idx + ")").html('<strong>Total Weight to Lose</strong><br>' + total_weight_to_lose);
                break;
        }
    });


    /** If current weight or beginning weight isn't listed on the page add it **/
    if(c_weight != true){ $j("#ka_profileAboutMeExt").prepend('<p><strong>Current Weight</strong><br>' + current_weight + '</p>'); }
    if(b_weight != true){ $j("#ka_profileAboutMeExt").prepend('<p><strong>Beginning Weight</strong><br>' + beginning_weight + '</p>'); }
    if(g_weight != true){ $j("#ka_profileAboutMeExt").prepend('<p><strong>Goal Weight</strong><br>' + goal_weight + '</p>'); }

    if(badge > 0){
        $j('#ka_profileLocItem').after('<li class="ka_userBadge">' + badge + '</li>');
    }else{
        $j('#ka_profileLocItem').after('<li class="ka_userBadgeStart">&nbsp;</li>');
    }

    $j('#ka_subPointsNow').after('<li class="biggestLosers"><a href="http://affiliate.kickapps.com/service/openCustomPage.kickAction?as=' + asID + '&myParam=biggestlosers&bodyClass=biggestlosers&title=Food%20Lovers%20-%20Biggest%20Losers">Biggest Losers</a></li>');
    $j('#ka_profileLocItem').hide();
}

function forumPage(){
    pageUsers = [];

    $j('.ka_forumDetails li.ka_userName a').each(function(index){
        href   = $j(this).attr('href');
        idx    = $j(this).attr('href').indexOf('u=');
        userid = href.substr((idx + 2), href.length).trim();
        pageUsers.push({index : index, userid : userid});
    });

    url = 'http://hosting.kicknetwork.com/foodlovers/userFunctions.php?task=getUserBadge&json='+JSON.stringify(pageUsers)+'&callback=addForumBadge';
    aObj = new JSONscriptRequest(url);
    aObj.buildScriptTag();
    aObj.addScriptTag();

}

function profileQuestionPage(){

    /** Do the update **/
    if(readCookieSS('fl_register') == "1"){
        url  = 'http://hosting.kicknetwork.com/foodlovers/userFunctions.php?task=updateUser&ka_userid='+Ka.Info.USERID+'&ka_username='+readCookieSS('fl_ka_username');
        url += '&beginning_weight='+readCookieSS('fl_beginning_weight')+'&current_weight='+readCookieSS('fl_current_weight')+'&goal_weight='+readCookieSS('fl_goal_weight');
        url += '&program_start='+readCookieSS('fl_program_start')+'&birthday='+readCookieSS('fl_birthday')+'&gender='+readCookieSS('fl_gender')+'&callback=checkUser';

        aObj = new JSONscriptRequest(url);
        aObj.buildScriptTag();
        aObj.addScriptTag();

        eraseCookieSS('fl_register');
        eraseCookieSS('fl_beginning_weight');
        eraseCookieSS('fl_current_weight');
        eraseCookieSS('fl_goal_weight');
        eraseCookieSS('fl_total_weight_to_lose');
        eraseCookieSS('fl_program_start');
        eraseCookieSS('fl_ka_username');
        eraseCookieSS('fl_birthday');
        eraseCookieSS('fl_gender');

        getKaExternal();
    }

    var programStart = ["0", "0", "0"];
    var programYear  = "0000";
    var programMonth = "00";
    var programDay   = "00";

    /** Get the current values **/
    beginWeight  = (typeof KaExternal.Info.BEGINNINGWEIGHT != 'undefined') ? KaExternal.Info.BEGINNINGWEIGHT : $j("#PQ1").val();
    currWeight   = (typeof KaExternal.Info.CURRENTWEIGHT != 'undefined')   ? KaExternal.Info.CURRENTWEIGHT   : $j("#PQ2").val();
    goalWeight   = (typeof KaExternal.Info.GOALWEIGHT != 'undefined')      ? KaExternal.Info.GOALWEIGHT      : $j("#PQ3").val();

    if($j("PQ5").length > 0){
        programStart = $j("#PQ5").val().split("-");
    }else{

        programStart = (typeof KaExternal.Info.PROGRAMSTART != 'undefined') ? KaExternal.Info.PROGRAMSTART.split("-") : '[0, 0, 0]';
    }

    birthStart   = (typeof KaExternal.Info.BIRTHDAY != 'undefined' && typeof KaExternal.Info.BIRTHDAY != 'null') ? KaExternal.Info.BIRTHDAY.split("-") : ['0', '0', '0 00:00:00'];
    maleSelect   = (typeof KaExternal.Info.GENDER != 'undefined' && typeof KaExternal.Info.GENDER != 'null' && KaExternal.Info.GENDER == 'M') ? "selected" : "";
    femaleSelect = (typeof KaExternal.Info.GENDER != 'undefined' && typeof KaExternal.Info.GENDER != 'null' && KaExternal.Info.GENDER == 'F') ? "selected" : "";

    if(typeof kaExternal != 'undefined' && KaExternal.TOTALLOSS != ""){
        totalWeightLost   = KaExternal.Info.TOTALLOSS;
        totalWeightToLose = KaExternal.Info.TOTALTOLOSE;
    }else{
        totalWeightLost   = (beginWeight > currWeight) ? (beginWeight - currWeight) : 0;
        totalWeightToLose = (currWeight > goalWeight) ? (currWeight - goalWeight) : 0;
    }

    programYear  = (typeof programStart[0] != 'undefined' && programStart[0] != 0) ? programStart[0] : 0;
    programMonth = (typeof programStart[1] != 'undefined' && programStart[1] != 0) ? programStart[1] : 0;
    programDay   = (typeof programStart[2] != 'undefined' && programStart[2] != 0) ? programStart[2].substr(0, 2).replace(/^\s*/, "").replace(/\s*$/, "") : 0;


    /* Program Start Month **/
    var month = ["January","February","March","April","May","June", "July","August","September","October","November","December"];
    program_start  = "<select id=\"program_start_month\" name=\"program_start_month\"><option value=''>-</option>";
    for (var i=0; i < month.length;++i){
        program_start += "<option value='" + (i + 1) + "'";
            if(programMonth == (i + 1)){program_start += " selected";}
        program_start += ">" + month[i] + "</option>";
    }
    program_start += "</select>&nbsp;";

    /* Program Start Day **/
    program_start += "<select id=\"program_start_day\" name=\"program_start_day\"><option value=''>-</option>";
    for(i = 1; i <= 31; i++){
        program_start += "<option value='" + i + "'";
            if(programDay == i){program_start += " selected";}
        program_start += ">" + i + "</option>";
    }
    program_start += "</select>&nbsp;";

    /* Program Start Year **/
    var d = new Date();
    var curr_year = d.getFullYear();
    program_start += "<select id=\"program_start_year\" name=\"program_start_year\"><option value=''>-</option>";
    for(i = curr_year; i >= 1910; i--){
        program_start += "<option value='" + i + "'";
            if(programYear == i){program_start += " selected";}
        program_start += ">" + i + "</option>";
    }
    program_start += "</select>";

    birthYear  = birthStart[0];
    birthMonth = birthStart[1];
    birthDay   = birthStart[2].substr(0, 2).replace(/^\s*/, "").replace(/\s*$/, "");

    birth  = "<select id=\"dobMonth\" name=\"dobMonth\"><option value=''>-</option>";
    for (i = 0; i < month.length;++i){
        birth += "<option value='" + (i + 1) + "'";
            if(birthMonth == (i + 1)){birth += " selected";}
        birth += ">" + month[i] + "</option>";
    }
    birth += "</select>&nbsp;";

    birth += "<select id=\"dobDay\" name=\"dobDay\"><option value=''>-</option>";
    for(i = 1; i <= 31; i++){
        birth += "<option value='" + i + "'";
            if(birthDay == i){birth += " selected";}
        birth += ">" + i + "</option>";
    }
    birth += "</select>&nbsp;";
    birth += "<select id=\"dobYear\" name=\"dobYear\"><option value=''>-</option>";
    for(i = curr_year; i >= 1910; i--){
        birth += "<option value='" + i + "'";
            if(birthYear == i){birth += " selected";}
        birth += ">" + i + "</option>";
    }
    birth += "</select>";


    /* Beginning Weight */
    $j('#PQ1').parent().parent().hide();
    $j('#PQ1').parent().parent().parent().append("<label for=\"gender\">Gender:</label><select name=\"gender\" id=\"gender\"><option value=\"\">-</option><option value=\"M\" " + maleSelect + ">Male</option><option value=\"F\" " + femaleSelect + ">Female</option></select>");
    $j('#PQ1').parent().parent().parent().append("<br/><label for=\"beginning_weight\">Beginning Weight:</label><input id=\"beginning_weight\" type=\"text\" value=\"" + beginWeight + "\" size=\"30\" name=\"beginning_weight\"/>");

    /* Current Weight */
    $j('#PQ2').parent().parent().hide();
    $j('#PQ2').parent().parent().parent().append("<label for=\"current_weight\">Current Weight:</label><input id=\"current_weight\" type=\"text\" value=\"" + currWeight + "\" size=\"30\" name=\"current_weight\"/> Update Frequently");

    /* Goal Weight */
    $j('#PQ3').parent().parent().hide();
    $j('#PQ3').parent().parent().parent().append("<label for=\"goal_weight\">Goal Weight:</label><input id=\"goal_weight\" type=\"text\" value=\"" + goalWeight + "\" size=\"30\" name=\"goal_weight\"/>");

    /* Total Weight Lost */
    $j('#PQ3').parent().parent().parent().append("<label for=\"total_weight_lost\">Total Weight Lost:</label><input id=\"total_weight_lost\" type=\"text\" value=\"" + totalWeightLost + "\" size=\"4\" name=\"total_weight_lost\" readonly/>");

    /* Total Weight To Lose */
    $j('#PQ4').parent().parent().hide();
    $j('#PQ4').parent().parent().parent().append("<label for=\"total_weight_to_lose\">Total Weight To Lose:</label><input id=\"total_weight_to_lose\" type=\"text\" value=\"" + totalWeightToLose + "\" size=\"4\" name=\"total_weight_to_lose\" readonly/>");

    /* Program Start */
    $j('#PQ5').parent().parent().hide();
    $j('#PQ5').parent().parent().parent().append("<label for=\"program_start\">Program Start:</label>"+program_start);

    /* Birthday */
    $j('#PQ5').parent().parent().parent().append("<label for=\"birthday\">Birthday:</label>"+birth);

    $j('#ProfileQuestions').submit(function(){return false;});

    $j('#ka_editProfileSubmit').click(function(event){
        event.preventDefault();
        event.stopPropagation();
        start_month   = $j('#program_start_month').val();
        start_day     = $j('#program_start_day').val();
        start_year    = $j('#program_start_year').val();
        program_start = start_year + "-" + start_month + "-" + start_day + " 00:00:00";

        $j("#PQ1").val($j("#beginning_weight").val());
        $j("#PQ2").val($j("#current_weight").val());
        $j("#PQ3").val($j("#goal_weight").val());
        $j("#PQ4").val($j("#total_weight_to_lose").val());
        $j("#PQ5").val(program_start);
        storeInfo();
        return false;
    });

}

function checkUserExists(id){
    var myregexp         = /(\d+)/im;
    var strong           = /<strong>([\s\S]*)<\/strong>/im;

    var beginning_weight = 0;
    var current_weight   = 0;
    var goal_weight      = 0;
    var exists           = true;

    $j.getJSON('http://hosting.kicknetwork.com/foodlovers/userFunctions.php?task=checkUserExists&callback=?', {userID : id}, function(data){
        
        /* User doesn't exist in database */
        if(data.status < 0){
            exists = false;

            /** Loop through extra params and get/verify data **/
            if($j("#ka_profileAboutMeExt p").length > 0){

                $j("#ka_profileAboutMeExt p").each(function(idx){
                    var match = strong.exec($j("#ka_profileAboutMeExt p:nth-child(" + idx + ")").html());
                    result = (match != null) ? match[1] : "";

                    var stuff = myregexp.exec($j("#ka_profileAboutMeExt p:nth-child(" + idx + ")").html());

                    switch(result){
                        case "Beginning Weight":
                            beginning_weight = (stuff[0] > 0) ? stuff[0] : 0;
                            break;
                        case "Current Weight":
                            current_weight = (stuff[0] > 0) ? stuff[0] : 0;
                            break;
                        case "Goal Weight":
                            goal_weight = (stuff[0] > 0) ? stuff[0] : 0;
                            break;
                    }
                });
                
            }

        /* User exists in database, so just update there information with the information on the page */
        }else{

            /** Loop through extra params and get/verify data **/
            if($j("#ka_profileAboutMeExt p").length > 0){

                $j("#ka_profileAboutMeExt p").each(function(idx){

                    var match = strong.exec($j("#ka_profileAboutMeExt p:nth-child(" + idx + ")").html());
                    result = (match != null) ? match[1] : "";

                    var stuff = myregexp.exec($j("#ka_profileAboutMeExt p:nth-child(" + idx + ")").html());
                    
                    switch(result){
                        case "Beginning Weight":
                            beginning_weight = (stuff[0] > 0) ? stuff[0] : 0;
                            if(beginning_weight > 80 || beginning_weight == 0){
                                url = "http://hosting.kicknetwork.com/foodlovers/userFunctions.php?task=setProfileQuestion&what=beginning_weight&value=" + beginning_weight + "&id=" + id + "&callback=?";
                                $j.getJSON(url, function(data){ });
                            }
                            break;
                        case "Current Weight":
                            current_weight = (stuff[0] > 0) ? stuff[0] : 0;
                            if(current_weight > 80 || current_weight == 0){
                                url = "http://hosting.kicknetwork.com/foodlovers/userFunctions.php?task=setProfileQuestion&what=current_weight&value=" + current_weight + "&id=" + id + "&callback=?";
                                $j.getJSON(url, function(data){ });
                            }
                            break;
                        case "Goal Weight":
                            goal_weight = (stuff[0] > 0) ? stuff[0] : 0;
                            if(goal_weight > 80 || goal_weight == 0){
                                url = "http://hosting.kicknetwork.com/foodlovers/userFunctions.php?task=setProfileQuestion&what=goal_weight&value=" + goal_weight + "&id=" + id + "&callback=?";
                                $j.getJSON(url, function(data){ });
                            }
                            break;
                    }
                });
            }
        }
        
    });

    return exists;
}


/* -- ONLOAD EVENTS -- */
$j(document).ready(function(){
    if(Ka.Util.getLocationParam('STATUS') == 'ACTIVE'){
        //alert("Your activating your account");
    }

    /* -- Put the external variables in an object -- */
    if(Ka.Info.PROFILEID != ""){
        var kaInfo = readCookieSS('ka_external_' + Ka.Info.PROFILEID);
        if(kaInfo !== null && typeof kaInfo != 'undefined'){
            KaExternal.Info = JSON.parse(kaInfo);
        }else{
            getKaExternal();
        }
    }

    /* -- Profile Page -- */
    if (Ka.Info.PAGE == 'pages/kickPlaceServerSide.jsp'){
        profilePage();
    }

    /* -- Search Page -- */
    if (Ka.Info.PAGE == "search/searchPage.jsp" && Ka.Util.getLocationParam('mediaType') === 'user'){
        $j('body.user #ka_subPointsNow').after('<li class="biggestLosers"><a href="http://affiliate.kickapps.com/service/openCustomPage.kickAction?as=' + asID + '&myParam=biggestlosers&bodyClass=biggestlosers&title=Food%20Lovers%20-%20Biggest%20Losers">Biggest Losers</a></li>');
    }

    /* -- Forum Page -- */
    if(Ka.Info.PAGE == 'pages/forum.jsp' || Ka.Info.PAGE == 'pages/discussionThread.jsp'){
        forumPage();
    }


    /* -- Update User Profile Page -- */
    if(Ka.Info.PAGE == 'pages/manageProfileQuestions.jsp'){
        profileQuestionPage();
    }
    
    /* -- Register User Page -- */
    if (Ka.Info.PAGE != 'login/registerUser.jsp'){
        var ha = readCookieSS('fl_register');
        if(typeof(ha)!='undefined'&& ha && ha == 1){
            url  = 'http://hosting.kicknetwork.com/foodlovers/userFunctions.php?task=registerUser&ka_userid='+Ka.Info.USERID+'&ka_username='+readCookieSS('fl_ka_username');
            url += '&beginning_weight='+readCookieSS('fl_beginning_weight')+'&current_weight='+readCookieSS('fl_current_weight')+'&goal_weight='+readCookieSS('fl_goal_weight');
            url += '&program_start='+readCookieSS('fl_program_start')+'&birthday='+readCookieSS('fl_birthday')+'&gender='+readCookieSS('fl_gender')+'&callback=checkUser';

            aObj = new JSONscriptRequest(url);
            aObj.buildScriptTag();
            aObj.addScriptTag();
        }
    }else{
        addQuestionsToPage('#ka_joinOptional');
    }

    /* -- Format Total Lost and Total to Lose -- */
    $j("#current_weight").blur(function(){
        totalLost = ($j("#beginning_weight").val() > $j(this).val()) ? ($j("#beginning_weight").val() - $j(this).val()) : 0;
        $j("#total_weight_lost").val(totalLost);
    });

    /* -- Format Total Lost and Total to Lose -- */
    $j("#goal_weight").blur(function(){
        totalToLose = ($j("#current_weight").val() > $j(this).val()) ? ($j("#current_weight").val() - $j(this).val()) : 0;
        $j("#total_weight_to_lose").val(totalToLose);
    });

    /** Only allow 3 digit numbers to be entered */
    $j("#beginning_weight").mask("999");
    $j("#current_weight").mask("999");
    $j("#goal_weight").mask("999");
});
