﻿var z = 0;

var WindowArea = windowSize();
var OuterUpdatePanelWidth = 0;

function control(name) {
    if (baseID == "" || baseID == null) { return null; }
    return $get(baseID + "MyPersonPopup_" + name)
}

function sscontrol(name, id, subid) {
    if (baseID == "" || baseID == null) { return null; }
    if (subid == null) { subid = ""; }
    if (id != null) {
        return $get(baseID + "SlideShow" + id + "_" + name + subid);
    }
}

var NewProject = {
    Show: function(typeid, typename) {
        $get(NewProjectID).value = typeid;
        $get(NewProjectTypeName).innerHTML = typename;
        $get(NewProjectContainer).parentNode.style.display = "";

        $get(Type3ProductName).parentNode.style.display = "none";

        if (typeid == parseInt(FamilyTreeID)) {
            $get("ProjectMessage").innerHTML = "Join all members of your family into one big Family Tree.";
        } else if (typeid == parseInt(FamilyReunionID)) {
            $get("ProjectMessage").innerHTML = "Join married Siblings’ Family Trees (or Reunions) into one big Family Reunion. Unmarried Siblings without their own Family Tree can be added as a Single Person.";
        } else if (typeid == parseInt(InMemoryOfID)) {
            $get("ProjectMessage").innerHTML = "Create a poster or DVD in the Memory Of a single person or a couple who are dear to your heart.";
            $get(Type3ProductName).parentNode.style.display = "";
        }

        showModal(NewProjectContainer, true);
    },

    Hide: function() {
        showModal(NewProjectContainer, false);
    },

    Create: function() {
        if ($get(NewProjectName).value != '') {
            DoPostBack(replace(CreateProject, "_", "$"), '');
        } else {
            alert("Please enter a name for your new project.");
        }
    }
};

function setCookie(name, value, expires, path, domain, secure) {
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime(today.getTime());

    /*
    if the expires variable is set, make the correct
    expires time, the current script below will set
    it for x number of days, to make it for hours,
    delete * 24, for minutes, delete * 60 * 24
    */
    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date(today.getTime() + (expires));

    document.cookie = name + "=" + escape(value) +
                ((expires) ? ";expires=" + expires_date.toGMTString() : "") +
                ((path) ? ";path=" + path : "") +
                ((domain) ? ";domain=" + domain : "") +
                ((secure) ? ";secure" : "");

}

var visibleModal = "";
function showModal(ContainerID, show) {
    if ($get(ContainerID)) {
        if (show) {
            WindowArea = windowSize(false);

            WindowArea.Width -= getScrollerWidth();

            var WindowOffset = windowOffset();

            $get("GrayBackGround").style.width = WindowArea.Width + "px";
            $get("GrayBackGround").style.height = WindowArea.Height + "px";

            $get("GrayBackGround").style.left = WindowOffset.x + "px";
            $get("GrayBackGround").style.top = WindowOffset.y + "px";

            $get("GrayBackGround").style.display = "";
            $get(ContainerID).style.display = "";

            $get(ContainerID).style.left = ((WindowArea.Width - elementSize($get(ContainerID)).w) / 2 + WindowOffset.x) + "px";
            $get(ContainerID).style.top = ((WindowArea.Height - elementSize($get(ContainerID)).h) / 2 + WindowOffset.y) + "px";

            visibleModal = ContainerID;
        } else {
            $get("GrayBackGround").style.display = "none";
            $get(ContainerID).style.display = "none";

            visibleModal = "";
        }
    }
}

function DeleteProject(name, index) {
    if (confirm("Are you sure you want to delete \"" + name + "\"?")) {
        DoPostBack("DeleteProject", index);
    }
}

function ClearProject() {
    if (confirm("Are you sure you want to clear this project?")) {
        DoPostBack("ClearProject", "");
    }
}

window.onresize = windowResize;
function windowResize() {
    GetMinWidth();
    BannerFix();
    if (visibleModal != "") {
        showModal(visibleModal, true);
    }
}

function windowScroll() {
    if (visibleModal != "") {
        showModal(visibleModal, true);
    }
}

var AcceptShares = new Array();
var DeclineShares = new Array();
function AcceptShare(i) {
    i = parseInt(i);
    AcceptShares[AcceptShares.length] = requestShared[i][1];
    NextShare(i + 1);
}

function DeclineShare(i) {
    i = parseInt(i);
    DeclineShares[DeclineShares.length] = requestShared[i][1];
    NextShare(i + 1);
}

function NextShare(i) {
    try {
        if (i == null) i = 0;

        if (requestShared.length > i) {
            $get(ShareRequestPerson).innerHTML = requestShared[i][0];
            $get(ShareRequestProject).innerHTML = requestShared[i][2];
            $get(ShareRequestIndex).value = i;

            showModal(ShareRequestContainer, true);
        } else {
            showModal(ShareRequestContainer, false);
            if (AcceptShares.length > 0 || DeclineShares.length > 0) {
                DoPostBack("AcceptShares", AcceptShares + "|" + DeclineShares);
            }
        }
    } catch (ex) { }
}

function PageLoad() {
    NextShare();

    startLogoutTimer();

    GetMinWidth();

    if (location.href.indexOf("?") != -1) {
        if (querySt("setupdelegate", location.href.toLowerCase().split("?")[1]) == "true") {
            CreateDelegates.Show();
        } else if (querySt("newproject", location.href.toLowerCase().split("?")[1]) != "") {
            NewProject.Show();
        }
    }

    if (location.href.toLowerCase().indexOf("register.aspx") != -1) {
        startIntro();
    }

    if (mainUpdatePanel != "") {
        if ($get(mainUpdatePanel) != null) {
            $get(mainUpdatePanel).style.height = "100%";
        }
    }
}

function ViewMyProjects() {
    if ($get("MyProjects") != null) {
        location.href = location.href.replace("#MyProjects", "") + "#MyProjects"
    } else {
        var urlParts = location.href.replace("#MyProjects", "").split("?")[0].split("/");
        location.href = location.href.replace("#MyProjects", "").split("?")[0].replace(urlParts[urlParts.length - 1], "Default.aspx#MyProjects");
    }
}

function changeOpac(opacity, id) {
    var object = $get(id).style;

    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

function querySt(ji, li) {
    gy = li.split("&");
    for (var i = 0; gy.length > i; i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
    return 0
}

function replace(str, se, re) {
    if (str != null) {
        str = str + '';
        var stringsplit = str.split(se + '');
        var newstring = "";
        for (var i = 1; i <= stringsplit.length; i++) {
            if (i < stringsplit.length) {
                newstring += stringsplit[i - 1] + "" + re;
            } else if (i = stringsplit.length) {
                newstring += stringsplit[i - 1];
            }
        }
        return newstring;
    }

    return ""
}

function elementPosition(obj) {
    var curleft = 0, curtop = 0;

    if (obj.offsetParent) {
        curleft = obj.offsetLeft;
        curtop = obj.offsetTop;

        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop;
        }
    }
    return { x: curleft, y: curtop };
}

function elementSize(obj) {
    var curwidth = 0, curheight = 0;

    if (obj != null) {
        curwidth = obj.offsetWidth;
        curheight = obj.offsetHeight;
    }

    return { w: curwidth, h: curheight };
}

function forgotPassword() {
    if ($get(UserName) != null) {
        if ($get(UserName).value != "") {
            DoPostBack(replace(ForgotPassword, "_", "$"), "");
        } else {
            alert("Enter an Email and then click Forgot Password");
        }
    }
}

function addEvent(element, event, func) {
    if (element.addEventListener) {
        element.addEventListener(event, func, false);
    } else if (element.attachEvent) {
        element.attachEvent('on' + event, func);
    }
}

function removeEvent(element, event, func) {
    if (element.addEventListener) {
        element.removeEventListener(event, func, false);
    } else if (element.attachEvent) {
        element.detachEvent('on' + event, func);
    }
}

function GetMinWidth() {
    OuterUpdatePanelWidth = elementSize($get(OuterUpdatePanel)).w;
}

function windowSize(pageSize) {
    if (pageSize == null)
        pageSize = true;

    var myWidth = 0, myHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }

    myWidth = Math.max(myWidth, pageSize ? OuterUpdatePanelWidth : 0);

    return { Width: myWidth, Height: myHeight };
}

function windowOffset() {
    var scrOfX = 0, scrOfY = 0;
    if (typeof (window.pageYOffset) == 'number') {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return { x: scrOfX, y: scrOfY };
}

function ViewOrders(id) {
    location.href = "ViewOrders.aspx?ID=" + id;
}

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;
    }

}

function openSharedProjectInfo(originalOwnerIndex) {
    return "<table id=ToolTipTable><tr><td width=200><div style=&quot;color: #9F7830; font-family: arial,helvetica; font-size: 12pt; font-weight: bold;&quot;><h3 style=&quot;color: #666666; font-family: arial,helvetica; font-size: 12pt; font-weight: bold;margin-bottom: 0; margin-top: 0;&quot;>Shared Project Information</h3> </div><br />This project has been shared with you. The original owner is <b>" + originalOwner[originalOwnerIndex] + "</b></td> </tr> </table>"
}

var BrowserDetect = {
    init: function() {
        this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
        this.version = this.searchVersion(navigator.userAgent)
		            || this.searchVersion(navigator.appVersion)
		            || "an unknown version";
        this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function(data) {
        for (var i = 0; i < data.length; i++) {
            var dataString = data[i].string;
            var dataProp = data[i].prop;
            this.versionSearchString = data[i].versionSearch || data[i].identity;
            if (dataString) {
                if (dataString.indexOf(data[i].subString) != -1)
                    return data[i].identity;
            }
            else if (dataProp)
                return data[i].identity;
        }
    },
    searchVersion: function(dataString) {
        var index = dataString.indexOf(this.versionSearchString);
        if (index == -1) return;
        return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));
    },
    dataBrowser: [
	            { string: navigator.userAgent,
	                subString: "OmniWeb",
	                versionSearch: "OmniWeb/",
	                identity: "OmniWeb"
	            },
	            {
	                string: navigator.vendor,
	                subString: "Apple",
	                identity: "Safari"
	            },
	            {
	                prop: window.opera,
	                identity: "Opera"
	            },
	            {
	                string: navigator.vendor,
	                subString: "iCab",
	                identity: "iCab"
	            },
	            {
	                string: navigator.vendor,
	                subString: "KDE",
	                identity: "Konqueror"
	            },
	            {
	                string: navigator.userAgent,
	                subString: "Firefox",
	                identity: "Firefox"
	            },
	            {
	                string: navigator.vendor,
	                subString: "Camino",
	                identity: "Camino"
	            },
	            {		// for newer Netscapes (6+)
	                string: navigator.userAgent,
	                subString: "Netscape",
	                identity: "Netscape"
	            },
	            {
	                string: navigator.userAgent,
	                subString: "MSIE",
	                identity: "Explorer",
	                versionSearch: "MSIE"
	            },
	            {
	                string: navigator.userAgent,
	                subString: "Gecko",
	                identity: "Mozilla",
	                versionSearch: "rv"
	            },
	            { 		// for older Netscapes (4-)
	                string: navigator.userAgent,
	                subString: "Mozilla",
	                identity: "Netscape",
	                versionSearch: "Mozilla"
	            }
            ],
    dataOS: [
	            {
	                string: navigator.platform,
	                subString: "Win",
	                identity: "Windows"
	            },
	            {
	                string: navigator.platform,
	                subString: "Mac",
	                identity: "Mac"
	            },
	            {
	                string: navigator.platform,
	                subString: "Linux",
	                identity: "Linux"
	            }
            ]

};
BrowserDetect.init();