

  function checkButtonEnabled(buttonName){
  	if($(buttonName).hasClass('ui-state-disabled')){
  		return false;
  	}

  	return true;
  }


  function enabledButton(buttonName){
  	buttonName = "#"+buttonName;
  	$(buttonName).removeClass("ui-state-disabled");
	$(buttonName).addClass("ui-state-default");
  }
  
  function disableButton(buttonName){
	buttonName = "#"+buttonName;
	$(buttonName).addClass("ui-state-disabled");
	$(buttonName).removeClass("ui-state-default");
  }  
  
  /*deprecated*/
  function AGP_addValueToClassName(ClassName,Value)
  {
      /*if (ClassName.indexOf(Value) == -1);
        ClassName = ClassName + " "+Value;*/
      return ClassName;
  }
  
  /*deprecated*/
  function AGP_RemoveValueFromClassName(ClassName,Value)
  {
      /*eval("ClassName = ClassName.replace(/"+Value+"/gi,\"\");");*/
      return ClassName;
  }

  function setFocusFirstField()
  {
    allInputFocusFields = document.body.getElementsByTagName("INPUT");
    for (i=0;i<allInputFocusFields.length;i++)
    {

        if (allInputFocusFields[i].type == "text" && !allInputFocusFields[i].isDisabled)
        {
         	allInputFocusFields[i].select();
            allInputFocusFields[i].focus();
            break;
        }
	}
  }

  function addOnloadFocus()
  {
        var oldonload = document.body.onload;
        if (typeof document.body.onload != 'function') 
        {
            window.onload = setFocusFirstField;
        }
        else
        {
            window.onload = function()
            {
                oldonload();
                setFocusFirstField();
            }
        }
  }

  function reloadmenu()
  {
        topWindow = window.top;
        if (topWindow != null)
        {
            menuIframe = topWindow.document.getElementById("navigationiframe");
            if (menuIframe != null)
                menuIframe.src = "logic.system_menu.htm";
        }

  }

    function AGP_openInRightFrame(url)
    {
        if(parent != null)
        {
            contentIframe = parent.document.getElementById("contentiframe");
            if (contentIframe != null)
                contentIframe.src = url;
        }
    }


    function AGP_openInWindow(url)
    {
        /*if(parent != null)
        {
            smallIframe = parent.document.getElementById("smallwindowiframe");
            bluritall = parent.document.getElementById("blurall");
            if (smallIframe != null)
            {
                bluritall.style.display = "inline";
                smallIframe.src = url;
                titel = parent.document.getElementById("smallwindowheadertitel");
                titel = smallIframe.document.title;
            }
        }*/

    	if(top!=window)
    	{
    		top.AGP_openInWindow(url);
    		return;
    	}
         
   
    	openDialog(url);
      }


    function openDialog(url)
    {
    	//remove all old data
    	AGP_closeSmallWindow();
    	
    	$('<div id="AGP_Dialog" align="center" style="width: 100%; height: 100%"><iframe frameBorder="0" class="externalSite AGP_Dialog" height="100%" width="90%" style="height: 100%; width:90%; border: 0px none;" src="' + url + '" /></div>', parent.document).dialog({
                title: '',
                autoOpen: true,
                modal: true,
                resizable: false,
                autoResize: false
            });
    }
    
    function AGP_openInContentFrameFromContext(url)
    {

        if(parent != null)
        {
             scrollY = 0;

            smallIframe = document.getElementById("AGP_GRID");
            if(smallIframe!=null)
                scrollY = smallIframe.scrollTop;

             url = url.replace(".htm",".scrollY."+scrollY+".htm");

        }


        addNumber = false;
        Element = LastContextElement;

        while (Element.tagName.toUpperCase() != "BODY")
        {
            if(Element.getAttribute("number") != null)
            {
                addNumber = true;
                break;
            }
            Element = Element.parentElement;
        }

        if (addNumber != false)
        {
            url = url.replace(".htm",".number."+Element.getAttribute("number")+".htm");
            if (Element.getAttribute("cmspart") != null)
                url = url.replace(".htm",".cmspart."+Element.getAttribute("cmspart")+".htm");
        }
        document.location = url;
    }

    
    
    function AGP_openEditorFromContext()
    {
        url = "editor.editor.type.page.htm";
        addNumber = false;
        Element = LastContextElement;

        while (Element.tagName.toUpperCase() != "BODY")
        {
            if(Element.getAttribute("number") != null)
            {
                addNumber = true;
                break;
            }
            Element = Element.parentElement;
        }

        if (addNumber != false)
            url = url.replace(".htm",".number."+Element.getAttribute("number")+".htm");

        window.open(url,'','width='+(screen.availWidth)+',height='+(screen.availHeight-50)+',status=1,scrollbars=yes,resizable=no,titlebar=0,top=0,left=0');
    }

    function AGP_openReadyToPublishEditorFromURL(number)
    {
        url = "editor.editor.type.pagePublish.htm";
        url = url.replace(".htm",".number."+number+".htm");
        window.open(url,'','width='+(screen.availWidth)+',height='+(screen.availHeight-50)+',status=1,scrollbars=yes,resizable=no,titlebar=0,top=0,left=0');
    }
    

    function AGP_openEmailEditorFromContext(part)
    {
        url = "logic.module_mail_mail_editor.htm";
        addNumber = false;
        Element = LastContextElement;

        while (Element.tagName.toUpperCase() != "BODY")
        {
            if(Element.getAttribute("number") != null)
            {
                addNumber = true;
                break;
            }
            Element = Element.parentElement;
        }

        if (addNumber != false)
            url = url.replace(".htm",".number."+Element.getAttribute("number")+".htm");

		if (part != false)
            url = url.replace(".htm",".cmspart."+part+".htm");
        
		window.open(url,'','width='+(screen.availWidth)+',height='+(screen.availHeight-50)+',status=1,scrollbars=yes,resizable=no,titlebar=0,top=0,left=0');
    }


    function AGP_openProductInEditorFromContext()
    {
        url = "editor.editor.type.product.htm";
        addNumber = false;
        Element = LastContextElement;

        while (Element.tagName.toUpperCase() != "BODY")
        {
            if(Element.getAttribute("productdisplayid") != null)
            {
                addNumber = true;
                break;
            }
            Element = Element.parentElement;
        }

        if (addNumber != false)
            url = url.replace(".htm",".number."+Element.getAttribute("productdisplayid")+".htm");

        window.open(url,'','width='+(screen.availWidth)+',height='+(screen.availHeight-50)+',status=1,scrollbars=no,resizable=no,titlebar=0,top=0,left=0');
    }

    function AGP_openProductInEditorFromGrid(url)
    {
        url = "editor.editor.type.product.htm";
        addNumber = false;
        Element = LastContextElement;

        while (Element.tagName.toUpperCase() != "BODY")
        {
            if(Element.getAttribute("productdisplayid") != null)
            {
                addNumber = true;
                break;
            }
            Element = Element.parentElement;
        }

        if(parent != null)
        {
            contentIframe = parent.document.getElementById("contentiframe");
            if (contentIframe != null)
            {
                if (addNumber != false)
                    url = url.replace(".htm",".number."+Element.getAttribute("productdisplayid")+".htm");
                window.open(url,'','width='+(screen.availWidth)+',height='+(screen.availHeight-50)+',status=1,scrollbars=yes,resizable=no,titlebar=0,top=0,left=0');

            }
        }
    }


    function AGP_openEditorFromGrid(url)
    {
    	url = "editor.editor.type.page.htm";
        addNumber = false;
        //Element = window.event.srcElement;
        Element = LastContextElement;
        while (Element.tagName.toUpperCase() != "BODY")
        {
            if(Element.getAttribute("number") != null)
            {
                addNumber = true;
                break;
            }
            Element = Element.parentElement;
        }

        if(parent != null)
        {
            contentIframe = parent.document.getElementById("contentiframe");
            if (contentIframe != null)
            {
                if (addNumber != false)
                    url = url.replace(".htm",".number."+Element.getAttribute("number")+".htm");
                window.open(url,'','width='+(screen.availWidth)+',height='+(screen.availHeight-50)+',status=1,scrollbars=yes,resizable=no,titlebar=0,top=0,left=0');

            }
        }
    }


    //scroll the grid to a y position scrollY
    function AGP_scrollGridToPos(scrollY)
    {

        if(scrollY==null)
               return;
        smallIframe = document.getElementById("AGP_GRID");
        if(smallIframe!=null)
        {
             smallIframe.scrollTop = scrollY;
        }
    }      
    

    function AGP_openInWindowFromContext(url)
    {

        
    	if(top!=window)
    	{
  
    	  	if(parent != null)
            {       
                 scrollY = 0;

                smallIframe = document.getElementById("AGP_GRID");
                if(smallIframe!=null)
                    scrollY = smallIframe.scrollTop;

                 url = url.replace(".htm",".scrollY."+scrollY+".htm");

            }

            addNumber = false;
            Element = LastContextElement;
            
            while (Element.tagName.toUpperCase() != "BODY")
            {
                if(Element.getAttribute("number") != null)
                {
                    addNumber = true;
                    break;
                }
                Element = Element.parentElement;
            }
    		
    		if (addNumber != false)
            {
                url = url.replace(".htm",".number."+Element.getAttribute("number")+".htm");
                if (Element.getAttribute("cmspart") != null)
                     url = url.replace(".htm",".cmspart."+Element.getAttribute("cmspart")+".htm");
            }
    		
    		top.AGP_openInWindowFromContext(url);
    	}else{
    		
    		openDialog(url);
            //smallIframe.contentWindow.document.title
    	}
    	
/*
        if(parent != null)
        {
            smallIframe = parent.document.getElementById("smallwindowiframe");
            bluritall = parent.document.getElementById("blurall");
            if (smallIframe != null)
            {
                if (addNumber != false)
                {
                    url = url.replace(".htm",".number."+Element.getAttribute("number")+".htm");
                    if (Element.getAttribute("cmspart") != null)
                        url = url.replace(".htm",".cmspart."+Element.getAttribute("cmspart")+".htm");
                }
                bluritall.style.display = "inline";
                smallIframe.src = url;
            }
        }
        */
   


    }


    function AGP_openInBlankWindowFromContext(url)
    {
        addNumber = false;
        Element = LastContextElement;

        while (Element.tagName.toUpperCase() != "BODY")
        {
            if(Element.getAttribute("number") != null)
            {
                addNumber = true;
                break;
            }
            Element = Element.parentElement;
        }
        if (addNumber != false)
        {
            url = url.replace(".htm",".number."+Element.getAttribute("number")+".htm");
            if (Element.getAttribute("cmspart") != null)
                url = url.replace(".htm",".cmspart."+Element.getAttribute("cmspart")+".htm");
        }
        window.open(url);
    }
    
    function AGP_openInContentFrameFromGrid(url)
    {
       
    	addNumber = false;
        Element = LastContextElement;

        while (Element.tagName.toUpperCase() != "BODY")
        {
            if(Element.getAttribute("number") != null)
            {
                addNumber = true;
                break;
            }
            Element = Element.parentElement;
        }

        if(parent != null)
        {
            contentIframe = parent.document.getElementById("contentiframe");
            if (contentIframe != null)
            {
                if (addNumber != false)
                {
                    url = url.replace(".htm",".number."+Element.getAttribute("number")+".htm");
                    if (Element.getAttribute("cmspart") != null)
                        url = url.replace(".htm",".cmspart."+Element.getAttribute("cmspart")+".htm");
                }
                contentIframe.src = url;
            }
        }
        

    }


    function AGP_openInWindowFromGrid(url)
    {
        
   	
    	/* 
        if(parent != null)
        {
             scrollY = 0;

            smallIframe = document.getElementById("AGP_GRID");
            if(smallIframe!=null)
                scrollY = smallIframe.scrollTop;

             url = url.replace(".htm",".scrollY."+scrollY+".htm");
        }*/

    	if(top!=window)
    	{
    		
    		addNumber = false;
	        Element = LastContextElement;
	        
	       
	        
	        while (Element.tagName.toUpperCase() != "BODY")
	        {
	        	if(Element.getAttribute("number") != null)
	            {
	                addNumber = true;
	                break;
	            }
	        	 alert(Element.tagName);
	        	Element = Element.parentElement;
	            alert(Element.tagName);
	        }
	        
	        if (addNumber != false)
	        {
	            url = url.replace(".htm",".number."+Element.getAttribute("number")+".htm");
	            if (Element.getAttribute("cmspart") != null)
	                url = url.replace(".htm",".cmspart."+Element.getAttribute("cmspart")+".htm");
	        }

	        
    		top.AGP_openInWindowFromGrid(url);
    		return;
    	}
        
        
        
    	openDialog(url);
        
    }

    function AGP_openInBlankWindowFromGrid(url)
    {

    	addNumber = false;
        Element = LastContextElement;

        while (Element.tagName.toUpperCase() != "BODY")
        {
            if(Element.getAttribute("number") != null)
            {
                addNumber = true;
                break;
            }
            Element = Element.parentElement;
        }

        if (addNumber != false)
        {
            url = url.replace(".htm",".number."+Element.getAttribute("number")+".htm");
            if (Element.getAttribute("cmspart") != null)
                url = url.replace(".htm",".cmspart."+Element.getAttribute("cmspart")+".htm");
        }
        window.open(url);
    }


    
    function AGP_openInContentFromULR(url)
    {
        if(parent != null)
        {
            contentIframe  = parent.document.getElementById("contentiframe");
            if (contentIframe  != null)
            {
                contentIframe.src = url;
            }
        }
    }
    
    function AGP_openInWindowFromULR(url)
    {
    	openDialog(url);
    }
    



    function AGP_confirmOKClick()
    {
      //window.frames['ContentWindow'].AGP_confirmOKClickAction();
      // if(document.getElementById("ContentWindow"))
    //	   document.getElementById("ContentWindow").AGP_confirmOKClickAction();
      parent.ContentWindow.AGP_confirmOKClickAction();
      //AGP_closeSmallWindow();
    }

    function AGP_confirmOKClickAction()
    {
        this.location = AGP_confirmOKPath;
        
    }

    function AGP_confirmGetText()
    {
        return AGP_confirmDisplayText;
    }


    var AGP_confirmOKPath;
    var AGP_confirmDisplayText;
    function AGP_confirmFromContext(displaytext,okpath)
    {

    	/*if(top!=window)
    	{
    		top.AGP_confirmFromContext(displaytext,okpath);
    		return;
    	}*/
    	
    	AGP_confirmOKPath = okpath;
        addNumber = false;
        Element = LastContextElement;
        if(parent != null)
        {
             scrollY = 0;

            smallIframe = document.getElementById("AGP_GRID");
            if(smallIframe!=null)
                scrollY = smallIframe.scrollTop;

             AGP_confirmOKPath = AGP_confirmOKPath.replace(".htm",".scrollY."+scrollY+".htm");

        }


	   if(Element!=null){
	        while (Element.tagName.toUpperCase() != "BODY")
	        {
	
	            if(Element.getAttribute("number") != null)
	            {
	                addNumber = true;
	                break;
	            }
	            Element = Element.parentElement;
	        }
		}
		
        if(parent != null)
        {
            //bluritall = parent.document.getElementById("blurall");
            if (addNumber != false)
            {
                   AGP_confirmOKPath = AGP_confirmOKPath.replace(".htm",".number."+Element.getAttribute("number")+".htm");
                   if (Element.getAttribute("cmspart") != null)
                       AGP_confirmOKPath = AGP_confirmOKPath.replace(".htm",".cmspart."+Element.getAttribute("cmspart")+".htm");
                   //bluritall.style.display = "inline";
            }
        }
    	
    	
    	$(function() {
    		// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
    		$("#dialog").dialog("destroy");
    	
    		var newDialog = $("#dialog-confirm");
    		
    		$(newDialog).attr("title", "Bevestiging");
    		$(newDialog).find('p:first-child').append(displaytext);
    		
    		$(newDialog).dialog({
    			resizable: false,
    			height:140,
    			modal: true,
    			buttons: {
    				'Nee': function() {
    					AGP_cancelClick();
    					$(this).dialog('close');
    				},
    				'Ja': function() {
    					AGP_confirmOKClick();
    					$(this).dialog('close');
    				}
    			}
    		});
    	});

    }
    
    function AGP_confirmJSCallBackOKClick(booleanOK)
    {
		AGP_CallBackFunction(booleanOK);
        AGP_cancelClick();
    }

	var AGP_CallBackFunction;
    function AGP_confirmBackToJs(displaytext,CallBack)
    {
       	
    	AGP_CallBackFunction = CallBack;
    	
    	$(function() {
    		// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
    		$("#dialog").dialog("destroy");
    	
    		var newDialog = $("#dialog-confirm");
    		
    		$(newDialog).attr("title", "Bevestiging");
    		$(newDialog).find('p:first-child').append(displaytext);
    		
    		$(newDialog).dialog({
    			resizable: false,
    			height:140,
    			modal: true,
    			buttons: {
    			'Nee': function() {
					AGP_cancelClick();
					$(this).dialog('close');
				},
    				'Ja': function() {
    			//alert(CallBack);
    			AGP_confirmJSCallBackOKClick(true);
    					//window[CallBack];
    					//AGP_confirmOKClick();
    					//window[function_name]
    					$(this).dialog('close');
    				}    				
    			}
    		});
    	});
    	
    	
    	
    	/*
    	AGP_CallBackFunction = CallBack;
        AGP_confirmDisplayText =displaytext;
        url = "logic.system_confirmjs.htm";
        addNumber = false;

        if(parent != null)
        {
            smallIframe = parent.document.getElementById("smallwindowiframe");
            bluritall = parent.document.getElementById("blurall");
            if (smallIframe != null)
            {
                bluritall.style.display = "inline";
                smallIframe.src = url;
            }
        }
        */
    }

    function AGP_setSmallWindowSize(width, height)
    {
    	$( ".AGP_Dialog" ).css("width", (width-50)+"px");
      	$( ".AGP_Dialog" ).css("height", (height-20)+"px");
      	
    	$( ".ui-dialog" ).css("width", width+"px");
      	$( ".ui-dialog" ).css("height", height+"px");

      	$( "#AGP_Dialog" ).dialog( "option", "position", ['center','center']);
    }
    
    function AGP_closeSmallWindow()
    {
    	$(".AGP_Dialog").dialog("close");
    	$("#AGP_Dialog").dialog("close");
   		$("#AGP_Dialog").remove();
    	
    	//$("#AGP_Dialog").dialog("destroy");
    	/*if(parent != null)
        {
            smallIframe = parent.document.getElementById("smallwindowiframe");
            bluritall = parent.document.getElementById("blurall");
            if (smallIframe == null)
                smallIframe = document.getElementById("smallwindowiframe");
            if (bluritall == null)
                bluritall = document.getElementById("blurall");
            if (bluritall != null)
                bluritall.style.display = "none";
            if (smallIframe != null)
                smallIframe.src = "about:blank";
        }*/
    }

    function AGP_refreshContenWindow()
    {            	
    	contentIframe = window.top.document.getElementById("contentiframe");
        if (contentIframe != null)
        {
               contentIframe.src = contentIframe.src;
        }
    }


    function AGP_refreshContenWindowGridPos(yPos)
    {
        return; 
    	contentIframe = parent.document.getElementById("contentiframe");
        if (contentIframe != null)
        {
                newScr = contentIframe.src;
                newScr = newScr.replace(".htm",".scrollY."+yPos+".htm");
                contentIframe.src = newScr;
         }
    }



    function setSmallWindowTitle()
    {
        
    	return;
    	smalltitel = document.getElementById("smallwindowheadertitel");
        smallIframe = parent.document.getElementById("smallwindowiframe");
        smalltitel.innerHTML = smallIframe.contentWindow.document.title;
        
        containerdiv = smallIframe.contentWindow.document.getElementById("formcontainerdiv");
        if (containerdiv != null)
        {
            if (containerdiv.offsetHeight < (document.body.offsetHeight-50))
               smallIframe.height = containerdiv.offsetHeight+20;
            else
                smallIframe.height = document.body.offsetHeight-50;

        }
        else
        {
            smallIframe.height = document.body.offsetHeight-50;
        }
        if (smallIframe.offsetHeight < 100)
            smallIframe.height = 100;
    }

    function AGP_rewriteUsername(data)
    {
        UserPanelvar = document.getElementById("UserPanel");
        UserPanelvar.lastChild.innerText = data;
    }

    function AGP_cancelClick()
    {
    	AGP_closeSmallWindow();
    	//$("#dialog").dialog("close");
    	/*if(parent != null)
        {
            bluritall = parent.document.getElementById("blurall");
            if (bluritall == null)
                bluritall = document.getElementById("blurall");
            if (bluritall != null)
                if (bluritall.style.display == "inline")
                    AGP_closeSmallWindow();
            if (parent.CurrentSelection!=null)
	           parent.CurrentSelection.select();
        }*/
    }


    /**
     * Make sure the current page loads in the frame
     */
    function checkForLoadInFrame()
    {

    	if (this.location == top.location)
        {
           newPath = this.location.pathname;
           var pathsArray = newPath.split("/");
           newPath = pathsArray[pathsArray.length-1];
           indexOfDot = newPath.indexOf('.');
           while (indexOfDot != -1)
           {
               newPath = newPath.replace(".","|");
               indexOfDot = newPath.indexOf('.');
           }
           top.location = "./logic.system_layout.reload."+newPath+".htm";
        }
    }

    function WindowHide(sWindowReturnValue)
	{
	    
    	/*var iframes = document.body.getElementsByTagName("IFRAME");
	    for (i=0;i<iframes.length;i++)
        {
            if (iframes[i].contentWindow.returnFunction)
                iframes[i].contentWindow.returnFunction(sWindowReturnValue);
        }*/
    	if (parent.ContentWindow.returnFunction)
    		parent.ContentWindow.returnFunction(sWindowReturnValue);
    	AGP_cancelClick();

    }

document.onkeydown = function (e)
{
	if (!e) var e = window.event;

	if (e.keyCode == 27)
	   AGP_cancelClick();
};

document.onkeypress = onkeyup = function (e)
{
	if (!e) var e = window.event;

	if (e.keyCode == 27)
    {
    	e.cancelBubble = true;
	    e.returnValue = false;
    	return false;
	}
};


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

