﻿/* Petena_Globals.js (c) Petena AB 1993-2009, http://www.petena.se

  Global repository of javascript functionality
 
DEVELOPMENT HISTORY: ******************************************************************************
  20071205 PiS -> Added popupwin, centered window, assign url, how, width, height
  20071205 PiS -> Added Show/HideEditBlock
  20081118 PiS -> Added functions SetZoom, Smaller, Larger
  20081119 PiS -> Corrected smaller-larger and added bypercent value
  20090603 PiS -> Added imgWidth height and hiRes to caller contentEdit
  20090827 PiS -> PopupWin, added window.open for other browsers than ie

BUGS & NOTES: *********** ( !=Warning, +=Additions to be made, -=Comment ) ************************
 - Use larger and smaller like this:
 - onmouseover="larger(this,160,157)" onmouseout="smaller(this,160,157)
 
--- code not in use -----
function OpenChild() 
{
var ParmA = retvalA.value;
var ParmB = retvalB.value;
var ParmC = retvalC.value;
var MyArgs = new Array(ParmA, ParmB, ParmC);
var WinSettings = "center:yes;resizable:no;dialogHeight:300px"
// ALTER BELOW LINE - supply correct URL for Child Form
var MyArgs = window.showModalDialog("http://localhost/ModalWin/ChildWebForm.aspx", MyArgs, WinSettings);
if (MyArgs == null)
{
window.alert("Nothing returned from child. No changes made to input boxes");
}
else
{
retvalA.value=MyArgs[0].toString();
retvalB.value=MyArgs[1].toString();
retvalC.value=MyArgs[2].toString();
}
}
</script>
</HEAD>
<body>
<P><INPUT id="retvalA" type="text" value="AAA"></P>
<P><INPUT id="retvalB" type="text" value="BBB"></P>
<P><INPUT id="retvalC" type="text" value="CCC"></P>
<P><BUTTON onclick="OpenChild()" type="button">Open Child Window</BUTTON></P>
</body>
</HTML>

7. In the above code, locate the line saying //ALTER BELOW LINE
8. Supply the correct URL for your Child Webform
9. Open the HTML surface for the ChildWebForm
10. Locate the yellow line
11. Delete all code EXCEPT the yellow line
12. Insert the following HTML below the existing yellow line

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Child Webform</TITLE>
<script language="javascript">
function Done() {
var ParmA = tbParamA.value;
var ParmB = tbParamB.value;
var ParmC = tbParamC.value;
var MyArgs = new Array(ParmA, ParmB, ParmC);
window.returnValue = MyArgs;
window.close();
}
function doInit() {
var ParmA = "Aparm";
var ParmB = "Bparm";
var ParmC = "Cparm";
var MyArgs = new Array(ParmA, ParmB, ParmC);
MyArgs = window.dialogArguments;
tbParamA.value = MyArgs[0].toString();
tbParamB.value = MyArgs[1].toString();
tbParamC.value = MyArgs[2].toString();
}
</script>
</HEAD>
<BODY onload="doInit()">
<P>Param A:<INPUT id="tbParamA" TYPE="text"></P>
<P>Param B:<INPUT ID="tbParamB" TYPE="text"></P>
<P>Param C:<INPUT ID="tbParamC" TYPE="text"></P>
<BUTTON onclick="Done()" type="button">OK</BUTTON>
</BODY>
</HTML>



COMPILER CONDITIONALS  ***************************************************************************/

function popupwin(url,how,width,height) 
{
 var left   = (screen.width  - width)/2;
 var top    = (screen.height - height)/2;
 var params = 'width='+width+', height='+height;
 params += ', top='+top+', left='+left;
 params += ', directories=no';
 params += ', location=no';
 params += ', menubar=no';
 params += ', resizable=yes';
 params += ', scrollbars=no';
 params += ', status=no';
 params += ', toolbar=no';
 var features = 'dialogHeight:' + height + 'px; dialogWidth:' + width + 'px; status: no; location: no;';
 //newwin=window.open(url,how,params);
 var ie = document.all;
 if (ie) {
     newwin = window.showModalDialog(url, '', features);
     location.reload(true);
 }
 else {
     var theWin = window.open(url, 'theWin', params);
     theWin.moveTo(300, 300);
     theWin.focus();
 } 
 // if (window.focus) {newwin.focus()}
 return false;
}

function ShowEditBlock(block) 
{	
  block.className = "ContentBorderOn";
  Tip('Dubbelklicka för att redigera innehållet.');
}

function HideEditBlock(block) 
{
  block.className = "ContentBorderOff";
  UnTip();
}

function EditContent(UID, Nr, Name, CultureName, BelongsTo_UID, imgWidth, imgHeight, hiRes) {
    popupwin('/Common/Pages/ContentEditor.aspx?UID=' + UID +
    '&Nr=' + Nr + '&Name=' + Name + '&CultureName=' +
    CultureName + '&BelongsTo_UID=' + BelongsTo_UID +
    '&imgWidth=' + imgWidth + '&imgHeight=' + imgHeight + '&hiRes=' + hiRes, 
    '_Child', 880, 600);
}

function EditBlock(contentID, contentName, UID) 
{
  popupwin('/Common/Pages/ContentEditor.aspx?id=' + contentID + '&contentName=' + contentName + '&UID=' + UID,'_Child',800,500);
}
function EditBlockData(contentID,TableName) 
{
  popupwin('/Common/Pages/ContentEditor.aspx?id=' + contentID + '&Table=' + TableName,'_Child',600,700);
}

function setZoom(img, dir, width, height, margin, zIndex, delay) {
    setTimeout(function() {
        if (img.dir == dir) {
            img.style.width = width;
            img.style.height = height;
            img.style.margin = margin;
//            img.style.zIndex = zIndex;
//            img.parentNode.parentNode.style.zIndex = zIndex;
        }
    }, delay);
}

function larger(img, width, height, bypercent) {
    img.dir = 'rtl';
    var w = width;
    var ws = (width*(bypercent/100)) / 10;
    var h = height;
    var hs = (height*(bypercent/100)) / 10;
    var m = (width * (bypercent/2)/100) / 10;
    for (var i = 1; i <= 10; i++) {
        w1 = w + (ws*i) + 'px';
        h1 = h + (hs*i) + 'px';
        m1 = '0 ' + (-i*m) + 'px 0 ' + (-i*m) + 'px';
        setZoom(img, 'rtl', w1, h1, m1, i, 40 * (i));
    }
}

function smaller(img, width, height, bypercent) {
    img.dir = 'ltr';
    var w = width;
    var ws = (width * (bypercent / 100)) / 10;
    var h = height;
    var hs = (height * (bypercent / 100)) / 10;
    var m = (width * (bypercent / 2)/100) / 10;
    for (var i = 10; i >= 0; i--) {
        w1 = w + (ws * i) + 'px';
        h1 = h + (hs * i) + 'px';
        m1 = '0 ' + (-i * m) + 'px 0 ' + (-i * m) + 'px';
        setZoom(img, 'ltr', w1, h1, m1, i, 40 * (10-i));
    }
}

function Querystring(qs) { // optionally pass a querystring to parse
    this.params = {};

    if (qs == null) qs = location.search.substring(1, location.search.length);
    if (qs.length == 0) return;

    // Turn <plus> back to <space>
    // See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
    qs = qs.replace(/\+/g, ' ');
    var args = qs.split('&'); // parse out name/value pairs separated via &

    // split out each name=value pair
    for (var i = 0; i < args.length; i++) {
        var pair = args[i].split('=');
        var name = decodeURIComponent(pair[0]);

        var value = (pair.length == 2)
			? decodeURIComponent(pair[1])
			: name;

        this.params[name] = value;
    }
}

Querystring.prototype.get = function(key, default_) {
    var value = this.params[key];
    return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
    var value = this.params[key];
    return (value != null);
}


/**
*
*  Simple Context Menu
*  http://www.webtoolkit.info/
*
**/

var SimpleContextMenu = {

    // private attributes
    _menus: new Array,
    _attachedElement: null,
    _menuElement: null,
    _preventDefault: true,
    _preventForms: true,


    // public method. Sets up whole context menu stuff..
    setup: function(conf) {

        if (document.all && document.getElementById && !window.opera) {
            SimpleContextMenu.IE = true;
        }

        if (!document.all && document.getElementById && !window.opera) {
            SimpleContextMenu.FF = true;
        }

        if (document.all && document.getElementById && window.opera) {
            SimpleContextMenu.OP = true;
        }

        if (SimpleContextMenu.IE || SimpleContextMenu.FF) {

            document.oncontextmenu = SimpleContextMenu._show;
            document.onclick = SimpleContextMenu._hide;

            if (conf && typeof (conf.preventDefault) != "undefined") {
                SimpleContextMenu._preventDefault = conf.preventDefault;
            }

            if (conf && typeof (conf.preventForms) != "undefined") {
                SimpleContextMenu._preventForms = conf.preventForms;
            }

        }

    },


    // public method. Attaches context menus to specific class names
    attach: function(classNames, menuId) {

        if (typeof (classNames) == "string") {
            SimpleContextMenu._menus[classNames] = menuId;
        }

        if (typeof (classNames) == "object") {
            for (x = 0; x < classNames.length; x++) {
                SimpleContextMenu._menus[classNames[x]] = menuId;
            }
        }

    },


    // private method. Get which context menu to show
    _getMenuElementId: function(e) {

        if (SimpleContextMenu.IE) {
            SimpleContextMenu._attachedElement = event.srcElement;
        } else {
            SimpleContextMenu._attachedElement = e.target;
        }

        while (SimpleContextMenu._attachedElement != null) {
            var className = SimpleContextMenu._attachedElement.className;

            if (typeof (className) != "undefined") {
                className = className.replace(/^\s+/g, "").replace(/\s+$/g, "")
                var classArray = className.split(/[ ]+/g);

                for (i = 0; i < classArray.length; i++) {
                    if (SimpleContextMenu._menus[classArray[i]]) {
                        return SimpleContextMenu._menus[classArray[i]];
                    }
                }
            }

            if (SimpleContextMenu.IE) {
                SimpleContextMenu._attachedElement = SimpleContextMenu._attachedElement.parentElement;
            } else {
                SimpleContextMenu._attachedElement = SimpleContextMenu._attachedElement.parentNode;
            }
        }

        return null;

    },


    // private method. Shows context menu
    _getReturnValue: function(e) {

        var returnValue = true;
        var evt = SimpleContextMenu.IE ? window.event : e;

        if (evt.button != 1) {
            if (evt.target) {
                var el = evt.target;
            } else if (evt.srcElement) {
                var el = evt.srcElement;
            }

            var tname = el.tagName.toLowerCase();

            if ((tname == "input" || tname == "textarea")) {
                if (!SimpleContextMenu._preventForms) {
                    returnValue = true;
                } else {
                    returnValue = false;
                }
            } else {
                if (!SimpleContextMenu._preventDefault) {
                    returnValue = true;
                } else {
                    returnValue = false;
                }
            }
        }

        return returnValue;

    },


    // private method. Shows context menu
    _show: function(e) {

        SimpleContextMenu._hide();
        var menuElementId = SimpleContextMenu._getMenuElementId(e);

        if (menuElementId) {
            var m = SimpleContextMenu._getMousePosition(e);
            var s = SimpleContextMenu._getScrollPosition(e);

            SimpleContextMenu._menuElement = document.getElementById(menuElementId);
            SimpleContextMenu._menuElement.style.left = m.x + s.x + 'px';
            SimpleContextMenu._menuElement.style.top = m.y + s.y + 'px';
            SimpleContextMenu._menuElement.style.display = 'block';
            return false;
        }

        return SimpleContextMenu._getReturnValue(e);

    },


    // private method. Hides context menu
    _hide: function() {

        if (SimpleContextMenu._menuElement) {
            SimpleContextMenu._menuElement.style.display = 'none';
        }

    },


    // private method. Returns mouse position
    _getMousePosition: function(e) {

        e = e ? e : window.event;
        var position = {
            'x': e.clientX,
            'y': e.clientY
        }

        return position;

    },


    // private method. Get document scroll position
    _getScrollPosition: function() {

        var x = 0;
        var y = 0;

        if (typeof (window.pageYOffset) == 'number') {
            x = window.pageXOffset;
            y = window.pageYOffset;
        } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
            x = document.documentElement.scrollLeft;
            y = document.documentElement.scrollTop;
        } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
            x = document.body.scrollLeft;
            y = document.body.scrollTop;
        }

        var position = {
            'x': x,
            'y': y
        }

        return position;

    }

}