// 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))) {
        
            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 showOpenOrder(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';
        }
    }
    //DP Order item
    else if (objType == 'dpo')
    {
        openElementUrl = openElementPathCorrector + 'webrebrand/modules/Documents/DocumentViewProperties.aspx?guid=' + id + '&objType=' + objType + '&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;
        inviteHeight = 530;
//    }
//    else {
//        inviteWidth = 870;
//        //inviteHeight = 593;
//        inviteHeight = 598;
//    }
    
    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) {
                var btn = document.getElementById(buttonName);
                try {
                    // Only works in IE 
                    btn.click();
                } catch (e) {
                    // FireFox Safari Fix 
                    var q = btn.href;
                    if (q.indexOf("javascript") >= 0) {
                        var f = q.substring(11, q.length);
                        eval(f);
                    }                                
                }                         
            } 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);
}

function smartGuideTemplate(guid, dptTemplate ) {

    var width = 1000;
    var height = 680;

    var posTop = ((screen.height - height) / 2);
    var posLeft = ((screen.width - width) / 2);
    var openElementUrl = 'modules/DynamicPublisher/OrderPublication-Rebrand.aspx?ItemId=choice:' + guid + '&ScreenWidth=' + width + '&ScreenHeight=' + height + '&openTemplate=' + guid +'&template=' + guid + '&page=OrderPublication-Rebrand.aspx&edit=true&header=false&topbar=false&dpttemplate=' + dptTemplate + '&popup=true';


    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) {

    // 14102010 this if statement has been commented out because of an issue,
    // where the original pictures was not switched with the clear gif.
    // This issue seemed to only appear in IE.
    //if (document.getElementById(imageId).src.indexOf("clear.gif") > 0) {
    var imageControl = document.getElementById(imageId);
    if (imageControl != null) {
        imageControl.src = imageUrl;
    }    
    //}
}

function jscriptLoadImageDelayed(imageId, imageUrl, timeout) {
    setTimeout("jscriptLoadImage('" + imageId + "','" + imageUrl + "')", timeout);
}

//

function objectIdRefConvert(objectRef) 
{

    var intPos = objectRef.indexOf(":");
    var strId = objectRef.substring(intPos + 1, objectRef.length);
    return parseInt(strId, 16);

}

// Navigator / Browser identifications

var BROWSER_FIREFOX = "Firefox"; // Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13
var BROWSER_SAFARI = "Safari"; // ** Safari/**
var BROWSER_EXPLORER = "MSIE"; // Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.3; .NET4.0E)
var BROWSER_CHROME = "Chrome";  //Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10

var OS_WINDOWS = "Windows";
var OS_MAC = "Machintosh";


function BrowserOsIs(browserType, OSType) {
    if (BrowserIs(browserType) && OsIs(OSType)) return true;
    return false;
}

function BrowserIs(browserType) {
    var valid = (navigator.userAgent).indexOf(browserType);

    // Smaller validation fix 
    // Chrome and Safari share the same userAgent-string beside "Chrome"
    if ((browserType == BROWSER_SAFARI) && (valid > 0)) {
        valid = -((navigator.userAgent).indexOf(BROWSER_CHROME));
    }
    if (valid > 0) return true;
    return false;
}

function OsIs(OSType) {
    var valid = (navigator.userAgent).indexOf(OSType);
    if (valid > 0) return true;
    return false;
}

function BrowserString() {
    return navigator.userAgent;
}

