// jsToolPack.js
//
// a collection af JavaScript Functions that couldt be used several places 
//
// 
//

var openElementPathCorrector = "";

// used on onkeypress event on input-elements.
//
// arguments
//
//  functionName :  The name of the function to be run.
//  returnFalse  :  used, if the page gets submitted instead of running the JavaScript.
//

/*global variables used to switch height and width in openinviteboard depending on borders being rounded or square*/
var inviteWidth;
var inviteHeight;
var squareBorders = false;

function onEnterDoJavaScriptFunction(e, functionName, returnFalse ) {
    var charCode;

    var out = fixEventjsToolsPack(e);

    if (out.event && out.event.which) {// All others
        charCode = out.event.which;
    } else if (window.event) {// IE
    out.event = window.event;
    charCode = out.event.keyCode;
    }
    
//    if (out.event.which == null) {

    if (out.event && ((charCode == 13) || (out.event == 9))) {
        alert('så er der da enten en tab eller en enter knap');
            eval(functionName + "();");
            if (returnFalse) {
                if (window.event) {
                    out.event.keyCode = 10;
                }
                return !returnFalse;
            }
            else {
                return returnFalse;
            }
        }
        else {
            return false;
        }
    //}
    //else if (out.event.which > 0) {                  

//        if (out.event && ((out.event.which == 13) || (out.event.which == 9))) {
//            eval(functionName + "();");
//            if (returnFalse) {
//                if (window.event) {
//                    out.event.which = 10;
//                }
//                return !returnFalse;
//            }
//            else {
//                return returnFalse;
//            }
//        }
//        else {
//            return false;
//        }
//    }
}

function fixEventjsToolsPack(e) 
{
    
    var out = new Object();
    if (!e && window.event ) {
        
        out = window;    
    } else {
        
        out.event = e;
    }    
    return out;
}

function showHidePanel(element)
{
    var panel = document.getElementById(element);
    
    if( panel.style.visibility == "hidden") 
        { 
           panel.style.visibility = "visible";
        }
    else
        {
          panel.style.visibility = "hidden"; 
        }
}

function showHideDiv(element)
{
    var panel = document.getElementById(element);
    
    if( panel.style.display == "none") 
        { 
           panel.style.display = "block";
        }
    else
        {
          panel.style.display = "none"; 
        }
}

var ovdp = false;

function showOpenElement(itemId,edit, shopping)
{
    //    var url = "modules/OpenElement/OpenElement.aspx?itemId="+itemId+"&edit="+edit;
    //    window.open(url,"OpenElement","Width=980, Height=700");    
    //    &width=980&height=700&edit=true
    if (ovdp) {
        openDocumentViewProperties(itemId);
    }  else {
    openRebrandedElement(itemId, shopping);
    }        
}
//Bruges for at finde ud af om det er DP container
var objType;
function showOpenDpContainer(itemId,obj)
{
    objType = obj;
    openDocumentViewProperties(itemId)
}


function openDocumentViewProperties(id) {
    var width = 980;
    var height = 725;

    var posTop = ((screen.height - height) / 2);
    var posLeft = ((screen.width - width) / 2);
    var openElementUrl = openElementPathCorrector + 'modules/Documents/DocumentViewProperties.aspx?itemId=' + id + '&width=' + width + '&height=' + height + '&edit=true';
    if (objType == null)
    {
        if (parseInt(id)) {
            openElementUrl = openElementPathCorrector + 'modules/Documents/DocumentViewProperties.aspx?itemId=' + id + '&width=' + width + '&height=' + height + '&edit=true';
        } else {
        openElementUrl = openElementPathCorrector + 'modules/Documents/DocumentViewProperties.aspx?itemId=' + id + '&width=' + width + '&height=' + height + '&edit=true';
        }
    }
    else
    {
        openElementUrl = openElementPathCorrector + 'modules/Documents/DocumentViewProperties.aspx?itemId=' + id + '&objType=' + objType + '&width=' + width + '&height=' + height + '&edit=true';
    }

    //	if ( openElementInvitation.length) 
    //	{	    		
    //	    openElementUrl += '&invitation=' + openElementInvitation;
    //	    
    //	}   
    var positionstring = 'toolbar=0,location=0,scrollbars=0,directories=0,status=0,menubar=0,resizable=0,minimize=0,width=' + width + ',height=' + height + ',left=' + posLeft + ',top=' + posTop;
    window.open(openElementUrl, 'OrderItem', positionstring);
    //window.open(openElementUrl , 'OrderItem','toolbar=0,location=0,scrollbars=0,directories=0,status=0,menubar=0,resizable=0,minimize=0,width=' + width + ',height=' + height + ',left=' + posLeft + ',top=' + posTop+'');
}




function openInviteBoard(boardId) 
{
	var inviteWidth = 850;
	var inviteHeight = 445;

	var invitePosTop = ((screen.height - inviteHeight) / 2);
	var invitePosLeft = ((screen.width - inviteWidth) / 2);

	window.open('MO-InviteBoard_OpenBoard.aspx?boardId=' + boardId+'&width='+inviteWidth+'&height='+inviteHeight,'inviteWindow','toolbar=0,location=0,scrollbars=0,directories=0,status=1,menubar=0,resizable=0,minimize=0,width=' + inviteWidth + ',height=' + inviteHeight + ',left=' + invitePosLeft + ',top=' + invitePosTop + '');
}

function newOpenInviteBoard(boardId) {
    
    //inviteWidth and inviteHeight declared globally in top of file
    if (squareBorders == true) {
        inviteWidth = 838;
        inviteHeight = 515;
    }
    else {
        inviteWidth = 870;
        inviteHeight = 593;
    }
    
    var invitePosTop = ((screen.height - inviteHeight) / 2);
    var invitePosLeft = ((screen.width - inviteWidth) / 2);
    var url = 'Modules/OpenInviteboard/OpenInviteBoard.aspx?boardId=' + boardId + '&width=' + inviteWidth + '&height=' + inviteHeight;
    
	  window.open(url,'inviteWindow','toolbar=0,location=0,scrollbars=0,directories=0,status=1,menubar=0,resizable=0,minimize=0,width=' + inviteWidth + ',height=' + inviteHeight + ',left=' + invitePosLeft + ',top=' + invitePosTop + '');
}

function InviteBoardOpenElement(id, boardId,invitation)
{
	var openwidth = 980;
	var openheight = 700;

	var openposTop = ((screen.height - openheight) / 2);
	var openposLeft = ((screen.width - openwidth) / 2);

   	var url = "modules/OpenElement/OpenElement.aspx?inviteBoard="+ boardId + "&itemId=" + id + "&width=" + openwidth + "&height=" + openheight + "&edit=true";
	if ( invitation != '') {
	    url += "&invitation=" + invitation 
	}
	window.open(url,'OpenElement' + id,'toolbar=0,location=0,scrollbars=0,directories=0,status=0,menubar=0,resizable=0,minimize=0,width=' + openwidth + ',height=' + openheight + ',left=' + openposLeft + ',top=' + openposTop + '');
}      

function StartSearch(event, buttonName)
{
    if (!event) {
        event = fixEventjsToolsPack(event);         
    }    
    if(event.which || event.keyCode)
    {
        if ((event.which == 13) || (event.keyCode == 13)) {
            if (document.getElementById(buttonName) != null) {
                document.getElementById(buttonName).click();
            } else {
                document.getElementById(document.getElementById('ctl00_searchButtonClick').value).click();
            }
            return false;
        }
    } 
    else 
    {
        return true;
    } 
}

// Return false if EnterKey is hit
function disableEnterKey(e)
{
     var key;     
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox     

     return (key != 13);
}

// returns an object with available screenWidth and screenHeight 
// This is also fitted to match every browser.
function returnScreenSize() 
{
        var myWidth = 0;
        var 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;
          }	
          retval  = new SizeObject(myWidth , myHeight );
          return retval;
}


// Object representing a set of width and height.

function SizeObject(widthVal, heightVal) 
{
    this.width = widthVal;
    this.height = heightVal;
}
function editTemplate(guid,basketId, name, objectRefId)
{

     var width = 1000;
    var height = 680;

    var posTop = ((screen.height - height) / 2);
    var posLeft = ((screen.width - width) / 2);
    var openElementUrl = 'modules/DynamicPublisher/EditChoice.aspx?Id=' + guid + '&basketId=' + basketId + '&name=' + name + '&width=' + width + '&height=' + height + '&edit=true&objectRefId=' + objectRefId;

    
    var positionstring = 'toolbar=0,location=0,scrollbars=0,directories=0,status=0,menubar=0,resizable=0,minimize=0,width=' + width + ',height=' + height + ',left=' + posLeft + ',top=' + posTop;
    window.open(openElementUrl, 'OrderItem', positionstring);
}

//
// Delayed imageLoad
//

function jscriptLoadImage(imageId, imageUrl) {

    if (document.getElementById(imageId).src.indexOf("clear.gif") > 0) {
        document.getElementById(imageId).src = imageUrl;
    }
}

function jscriptLoadImageDelayed(imageId, imageUrl, timeout) {
    setTimeout("jscriptLoadImage('" + imageId + "','" + imageUrl + "')", timeout);
}

//
