﻿var HiddenFromOnline = false;
var My_Member_ID = 0;
var My_Username = "";
var My_Site_ID = 0;
var My_Member_Type = 0;
var Default_Country = "";

var WS_JSon = "";  // my_member_id, my_member_type, my_site_id, ws_hash, ws_time (datetime_as_string);

var Have_Self_Guided = false;
var Have_Courses = false;
var Have_Coaches = false;
var My_Privacy = 0;
var Have_Email = false;

var isPopUp = false;

var div_j = "#div_journal_form ";
var form_redirect = '';


function ToggleDiv(div_name) {

    if (DivIsBlock(div_name))
        setToNone(div_name);
    else
        setToBlock(div_name);

}
function SwitchHeader(PartialDivName) {

    SwitchArrow('img_' + PartialDivName);

    if (DivIsBlock('div_' + PartialDivName))
        setToNone('div_' + PartialDivName);
    else
        setToBlock('div_' + PartialDivName);

}

function CleanForJSON(val) {



    if (val == null || val == "" || val == undefined) {
 
        return val;
    }
    else {
        // return val.toString().replace(/\+/g, ' ');
         return val.toString().split("\\").join("\\\\").split("'").join("\\'");
    }
 
}

function is_int(value) {
    if ((parseFloat(value) == parseInt(value)) && !isNaN(parseInt(value))) {
        return true;
    } else {
        return false;
    }
}

function AlwaysReturnBool(val) {

    if (val == true || val == 1 || val == "true" || val == "True")
        return true;
    else
        return false;
}

function AlwaysReturnString(val) {
    if (val == null || val == "" || val == undefined)
        return "";
    else
        val.toString();
}
function AlwaysReturnInt(val) {
    if (is_int(val))
        return parseInt(val.toString());
    else
        return 0;
}

function OpenHeader(PartialDivName) {

    OpenArrow('img_' + PartialDivName);
    setToBlock('div_' + PartialDivName);

}
function SwitchArrow(img_name) {
    var div = document.getElementById(img_name);

    if (div != null) {
        if (div.className.match("Arrow_Right") != null)
            div.className = 'Arrow_Down';
        else
            div.className = 'Arrow_Right';
    }
}
function CloseArrow(img_name) {
    var div = document.getElementById(img_name);
    if (div != null) {
        div.className = 'Arrow_Right';
    }
}
function OpenArrow(img_name) {
    var div = document.getElementById(img_name);
    if (div != null) {
        div.className = 'Arrow_Down';
    }
}
function Boolean_ToQueryString(bool_int_value)
{
    return bool_int_value + 10;
}

function popWinCustom(url, width, height) {
    window.open(url, "popWindow", "width=" + width + ",height=" + height + ",toolbar=0,directories=0,menubar=0,status=0,location=0,scrollbars=0,resizable=1");
}

function setToBlock(divName) {
    if (document.getElementById(divName) != null) {
        document.getElementById(divName).style.display = "block";
    }
}
function setToNone(divName) {
    if (document.getElementById(divName) != null) {
        document.getElementById(divName).style.display = "none";
    }
}

function DivIsBlock(divName) {
    if (document.getElementById(divName) != null) {
        if (document.getElementById(divName).style.display == "block")
            return true;
        else
            return false;
    }
}

function BooleanToInteger(bool_value) {
    if (bool_value)
        return 1;
    else
        return 0;
}
function IntegerToBoolean(int_value) {
    if (int_value == 1)
        return true;
    else
        return false;
}


function ShowMore(PartialDivName) {
    setToNone('Summary_' + PartialDivName);
    setToBlock('Complete_' + PartialDivName);
}

function ShowDiv(PartialDivName) {

    if (DivIsBlock('sdiv_' + PartialDivName))
        setToNone('sdiv_' + PartialDivName);
    else
        setToBlock('sdiv_' + PartialDivName);
}

function HighLight(objectID) {

    var i = objectID.className.indexOf('HighLight');
    var j = objectID.className.indexOf('Selected');

    if (j == -1) {
        if (i != -1) {
            objectID.className = objectID.className.substring(0, i);
        } else {
            objectID.className = objectID.className + 'HighLight';
        }
    }
    return true;
}

function HighLightUsingName(objectName) {

    var objectID = document.getElementById(objectName);
    var i = objectID.className.indexOf('HighLight');
    var j = objectID.className.indexOf('Selected');
    if (j == -1) {
        if (i != -1) {
            objectID.className = objectID.className.substring(0, i);
        } else {
            objectID.className = objectID.className + 'HighLight';
        }
    }
    return true;
}

function Get_DLL_Value(ddl_id) {

    selected_value = 0;
    var ddl_object = document.getElementById(ddl_id);
    if (ddl_object != null) {
        selected_value = AlwaysReturnInt(ddl_object.options[ddl_object.selectedIndex].value);
    }
    return selected_value;
}
function Fill_DLL(ddl_id, listitems) {

    var select_value = 0;
    // var s = TurnIntoIDSelector(ddl_id, true);
    // var i = 0;

    // $(s + ">option").remove();

    var ddl_object = document.getElementById(ddl_id);

    // document.getElementById(ddl_id).length = 0;

    // Delete all entries in the cities list box
    for (i = ddl_object.length; i > 0; i--) {
        ddl_object.options[i - 1] = null
    }


    for (i = 0; i < listitems.length; i++) {

        var IsSelected = listitems[i]["Selected"];
        ddl_object.options.add(new Option(listitems[i]["Text"], listitems[i]["Value"], IsSelected, IsSelected));
        if (IsSelected) {

            select_value = listitems[i]["Value"];
        }
    }

    return select_value;
}

function Fill_DLL_Text(ddl_id, listitems) {

    // var _selected_txt = "";
    var s = TurnIntoIDSelector(ddl_id, true);
    var i = 0;

    $(s + ">option").remove();

    for (i = 0; i < listitems.length; i++) {
        document.getElementById(ddl_id).options.add(new Option(listitems[i]["Text"], listitems[i]["Value"], listitems[i]["Selected"], listitems[i]["Selected"]));
        if (listitems[i]["Selected"]) {
            // _selected_txt = listitems[i]["Value"];
        }
    }
    // return _selected_txt;
}

function pause(milliseconds) {
    var dt = new Date();
    while ((new Date()) - dt <= milliseconds) { /* Do nothing */ }
}

function AssignPill(parent_div_name, pill_css) {

    var s = TurnIntoIDSelector(parent_div_name, false);
    $(s + ' a.pretty-ui-pill-select').removeClass('pretty-ui-pill-select');

    s = $(s + ' .' + pill_css).attr('id');
    $(TurnIntoIDSelector(s, false)).addClass('pretty-ui-pill-select');

}

function SwitchPills(parent_div_name, obj) {

    var s = TurnIntoIDSelector(parent_div_name, false);
    $(s + ' a.pretty-ui-pill-select').removeClass('pretty-ui-pill-select');

    s = $(obj).attr('id');
    $(TurnIntoIDSelector(s, false)).addClass('pretty-ui-pill-select');

}

function AddTimeToUrl(url) {

    var u = url.toString();
    var fakeNum = (new Date()).getTime() % 1000000000;
    if (u.indexOf("?", 1) != 0)
        return u + "&fn=" + fakeNum;
    else
        return u + "?fn=" + fakeNum;


}
function RemoveViewstate(text) {
    return text.toString().replace("VIEWSTATE", "x").replace("aspnetForm", "xForm");
}
function AddPoundSign(div_name) {

    if (div_name != null && div_name.toString().length > 2) {
        if (div_name.toString().indexOf("#") == -1)
            return "#" + div_name;
        else
            return div_name;
    } else
        return "";

}
function LoadAjaxIntoDiv(url, div_name) {


    $.get(AddTimeToUrl(url), function(data) {

        $(AddPoundSign(div_name)).html(RemoveViewstate(data));
        // Set_Up_Page(div_name);
    });
}


function Show_Comment_Form(content_id, reply_to_comment_id) {
    setToBlock('div_comment_box_' + CombinedDivName(content_id, reply_to_comment_id));
}
function Show_Comment_Submit(content_id, reply_to_comment_id) {
    setToBlock('div_comment_post_' + CombinedDivName(content_id, reply_to_comment_id));
}


function RelocateCategory(destination_div, topic_text) {
    $("#div_category_text").html(topic_text);
    $("#" + destination_div).before($("#div_category").show()).remove();
}

function RelocateDiv(source_div_id, destination_div_id) {

    $("#" + destination_div_id).before($("#" + source_div_id).show()).remove();
}


function TurnIntoIDSelector(div_name, add_space_to_end) {

    var s = AddPoundSign(div_name).toString();
    if (add_space_to_end) {
        if (s.length > 2)
            return s + ' ';
        else
            return s;
    } else
        return s;
}








var Url = {

    // public method for url encoding
    encode: function(string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode: function(string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode: function(string) {
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode: function(utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while (i < utftext.length) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if ((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}


var isPop = false;

// CENTER WINDOW
var popup_blocker_shown = 0;
var xMax; var yMax;
if (document.all) {
    xMax = screen.width; yMax = screen.height;
} else {
    if (document.layers) {
        xMax = window.outerWidth; yMax = window.outerHeight;
    } else { var xMax = 640; yMax = 480; }
}


function popTab(url) {

    var fakeNum = (new Date()).getTime() % 1000000000;
    var windowName = "wintabc" + fakeNum;
    var features = "";
    // features += "scrollbars=yes,status=no,resizable=yes,location=no,menubar=no";
    var mywin;

    eval('my' + 'wi' + 'n = ' + 'w' + 'i' + 'nd' + 'ow.o' + 'pe' + 'n(ur' + 'l' + ', windowName, features)' + ';');

    if (mywin != null) {
        if (mywin.opener == null) { mywin.opener = self; }
        mywin.focus();
    } else {
        if (popup_blocker_shown == 0) {
            popup_blocker_shown = 1;
            alert("This site uses pop-ups (profiles, alerts, etc..). Please turn off your pop-up blocker.");
        }
    }
    return mywin;


}

function popWin(url, popWidth, popHeight) {

    var fakeNum = (new Date()).getTime() % 1000000000;
    var windowName = "winpopc" + fakeNum;

    var features = "width=" + popWidth + ",height=" + popHeight + ",";

    var xOffset = (xMax - popWidth) / 2, yOffset = (yMax - popHeight) / 2;

    if (xOffset > 0 && yOffset > 0) {
        features += "screenX=" + xOffset + ",left=" + xOffset + ",";
        features += "screenY=" + yOffset + ",top=" + yOffset + ",";
    }


    features += "scrollbars=yes,status=no,resizable=yes,location=no,menubar=no";


    var mywin;

    eval('my' + 'wi' + 'n = ' + 'w' + 'i' + 'nd' + 'ow.o' + 'pe' + 'n(ur' + 'l' + ', windowName, features)' + ';');

    if (mywin != null) {
        if (mywin.opener == null) { mywin.opener = self; }
        mywin.focus();
    } else {
        if (popup_blocker_shown == 0) {
            popup_blocker_shown = 1;
            alert("This site uses pop-ups (profiles, alerts, etc..). Please turn off your pop-up blocker.");
        }
    }
    return mywin;


}


function ReverseCheckAll(CheckBoxID) {

    re = new RegExp(CheckBoxID + '$')  //generated control name starts with a colon

    for (i = 0; i < document.forms[0].elements.length; i++) {

        elm = document.forms[0].elements[i]

        if (elm.type == 'checkbox') {

            if (re.test(elm.name)) {

                var checkVal = elm.checked;
                elm.checked = !checkVal

            }
        }
    }
}
function CheckAll(CheckBoxID, checkVal) {

    re = new RegExp(CheckBoxID + '$')  //generated control name starts with a colon

    for (i = 0; i < document.forms[0].elements.length; i++) {

        elm = document.forms[0].elements[i]

        if (elm.type == 'checkbox') {

            if (re.test(elm.name)) {

                elm.checked = checkVal

            }
        }
    }
}
