var DialogIsAnchor = false;
var eDialog;
function addEvent(obj, evType, fn, useCapture) {
    if (obj.addEventListener) {
        obj.addEventListener(evType, fn, useCapture);
        return true;
    } else if (obj.attachEvent) {
        var r = obj.attachEvent("on" + evType, fn);
        return r;
    } else {
        //alert("Handler could not be attached");
    }
}
addEvent(window, 'resize', function() { _shiftFade(); }, false);
addEvent(window, 'scroll', function() { _shiftFade(); }, false);

function sendConfirm(EmailAddy) {

    var sendConfirm = document.getElementById('sendConfirm');

    if (sendConfirm.value == 'true') {
        Dialog('../Scripts/send_confirm.asp?type=Error', true, null, true, 'Resending activation code');
    } else {
        sendConfirm.value = 'true';
        Dialog('../Scripts/send_confirm.asp?email=' + EmailAddy, true, null, true, 'Resending activation code');
    }
}
function Fade(element, _shift) {

    fadePanel = parent.document.getElementById('FadePanel');
    switch (_shift) {
        case true:
            //Fade On
            switch (element) {
                case "Screen":

                    //Cover Entire Page
                    FadeIsScreen = true;


                    elLeft = document.documentElement.scrollLeft;
                    elTop = document.documentElement.scrollTop;
                    elWidth = document.documentElement.clientWidth;
                    elHeight = document.documentElement.clientHeight;

                    if (isIE) {
                        elWidth = getPageSize('Horizontal') + getScrollPosition('Horizontal');
                        elHeight = getPageSize('Vertical') + getScrollPosition('Vertical');
                    }
                    
                    fadePanel.style.display = 'block';
                    fadePanel.style.left = elLeft + 'px';
                    fadePanel.style.top = elTop + 'px';
                    fadePanel.style.width = elWidth + 'px';
                    fadePanel.style.height = elHeight + 'px';

                    break;
                default:
                    //Cover Selected Element
                    FadeIsScreen = false;
                    break;
            }
            break;
        case false:
            //Fade Off
            FadeIsScreen = false;
            fadePanel.style.width = 0 + 'px';
            fadePanel.style.height = 0 + 'px';
            fadePanel.style.top = 0 + 'px';
            fadePanel.style.left = 0 + 'px';
            if (document.getElementById('IsMinimised')) {
                document.getElementById('IsMinimised').value = 'false';
            } else {
                parent.document.getElementById('IsMinimised').value = 'false';
            }
            break;
    }
    if (DialogIsAnchor) {
        CenterDialog();
    }
}


function _shiftFade() {
    if (document.getElementById('FadePanel').offsetWidth > 0) {
        switch (FadeIsScreen) {
            case (true):
                fadePanel = document.getElementById('FadePanel');
                elLeft = document.documentElement.scrollLeft;
                elTop = document.documentElement.scrollTop;

                elWidth = document.documentElement.clientWidth;
                elHeight = document.documentElement.clientHeight;

                if (isIE) {
                    elWidth = getPageSize('Horizontal') + getScrollPosition('Horizontal');
                    elHeight = getPageSize('Vertical') + getScrollPosition('Vertical');
                }

                fadePanel.style.left = elLeft + 'px';
                fadePanel.style.top = elTop + 'px';
                fadePanel.style.width = elWidth + 'px';
                fadePanel.style.height = elHeight + 'px';
                break;
            default:
                break;
        }
        if (DialogIsAnchor) {
            CenterDialog();
        }
    }
}

//GetPage = Include File For Main Section Of Dialog.
//Anchor = Boolen - If the page should remain centered when page is resized or on scroll.
//Icon = Specify an icon to use in the top left corner, if not specified the default icon will be used. 
//(Default size is 10px by 10px).e.g. /images/icon/default.png
//Heading = The heading of the Dialog.


//

function Dialog(getPage, Anchor, Icon, CanClose, Heading) {
    switch (Anchor) {
        case true:
            DialogIsAnchor = true;
            break;
        case false:
            DialogIsAnchor = false;
            break;
    }
    eDialog = document.getElementById('WindowPanel');
    eDialog.style.display = 'Block';
    var DialogIcon = parent.document.getElementById('WindowIcon');
    var DialogTitle = parent.document.getElementById('WindowHeading');
    var DialogCancel = parent.document.getElementById('WindowClose');
    if (CanClose == false) {
        DialogCancel.style.visibility = 'hidden';
    }
    if (Icon) {
        DialogIcon.innerHTML = '<img src="' + Icon + '" />';
    } else {
        //DialogIcon.innerHTML = '<img src="/images/icons/default.png" />';
        DialogIcon.innerHTML = '&nbsp;';
    }
    if (Heading) {
        DialogTitle.innerHTML = Heading;
    } else {
        DialogTitle.innerHTML = 'Dialog Title';
    }
    var URL = getPage;
    document.getElementById('InnerWindow').src = '../Dialogs/Default.asp?URL=' + URL;

    CenterDialog();
}

function getPageSize(Direction) {


    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;
    }
    switch (Direction) {
    case 'Horizontal':
        return myWidth;
        break;
    case 'Vertical':
        return myHeight;
        break;  
        
    }  

}

function getScrollPosition(Direction) {

    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;
    }

    switch (Direction) {
        case 'Horizontal':
            return scrOfX;
            break;
        case 'Vertical':
            return scrOfY;
            break;  
        
    }

}

function GetBrowserSize(Direction) {
    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;
    }

    switch (Direction) {
        case 'Horizontal':
            return myWidth;
            break;
        case 'Vertical':
            return myHeight;
            break;

    }
    
}


function CenterDialog() {
    
    eDialog = parent.document.getElementById('WindowPanel');
    elLeft = getScrollPosition('Horizontal');
    elTop = getScrollPosition('Vertical');


    elWidth = GetBrowserSize('Horizontal');
    elHeight = GetBrowserSize('Vertical');


    
    
    var dialogWidth, dialogHeight
    dialogWidth = eDialog.offsetWidth;
    dialogHeight = eDialog.offsetHeight;
    
    if (parent.document.getElementById('IsMinimised').value == 'false') {
        //alert('Top: ' + elTop + ' Height: ' + elHeight);
  
        elTop = ((elTop + (elHeight / 2)) - (dialogHeight / 2)) + 'px';
    } else {
        elTop = (Number(elTop) + Number(document.getElementById('DialogTop').value)) + 'px';
    }
    eDialog.style.top = elTop;
    elLeft = ((elLeft + (elWidth / 2)) - (dialogWidth / 2)) + 'px';
    eDialog.style.left = elLeft;
}

function hideDialog() {
    eDialog = parent.document.getElementById('WindowPanel');
    eDialog.style.left = 0 + 'px';
    eDialog.style.top = 0 + 'px';
    eDialog.style.display = 'none';
    document.getElementById('InnerWindow').src = '../Dialogs/Default.asp?type=DontLoad';
    if (document.getElementById('btMinimise')) {
        document.getElementById('btMinimise').src = '../images/icons/Minimise_Normal.gif';
    }
    if (parent.document.getElementById('btMinimise')) {

    }
}