﻿

function ShowLinkBuilder(strCtrl)
{  
  if (Ext.isReady) 
  { 
    //store the ctrl that will receive the Built Link
    AV.LinkBuilder.setCtrl(strCtrl);
  
    //store the ctrl that LinkBuilder will animate To/From
    AV.LinkBuilder.setAnimCtrl(strCtrl);
    
    //begin processing to show linkbuilder    
    AV.LinkBuilder.createHtml(); 
  }     
  else { alert('Ext not ready.  Please reload the page and try again.'); }
}

Ext.onReady(function(){ /*Add any initiialization code here */});


function Success(response, options)
{
  AV.LinkBuilder.setLinkBuilderHtml(response.responseText);
  AV.LinkBuilder.getWin().show(Ext.get(AV.LinkBuilder.getAnimCtrl()));
}
  
function Fail(response, options) { Alert.show("Failure", ""); }

AV.LinkBuilder = new function(){

  var link_builder_html;
  var tree_panel;
  var win;
  var ctrl;
  var animCtrl;
      
  function GetCtrl() { return ctrl; }
  
  function GetAnimCtrl() { return animCtrl; }
  
  return {
  
    setCtrl: function(strCtrl) { ctrl = strCtrl; },
    
    getCtrl: function() { return GetCtrl(); },
    
    setAnimCtrl: function(strCtrl) { animCtrl = strCtrl; },
    
    getAnimCtrl: function() { return GetAnimCtrl(); },
    
    setLinkBuilderHtml: function(html){ link_builder_html = html; },
    
    createHtml: function(){
    
      if(!link_builder_html)
      {
        Ext.Ajax.request({
         url: '/link/LinkBuilder.asp?hide_buttons=1',
         success: Success,
         failure: Fail
        });       
      }
      else //if the html already exists, show the window
       { this.getWin().show(Ext.get(GetAnimCtrl())); }            
    },
    
    getHtml: function(){
      if(!link_builder_html) { createHtml(); }
    },
    
    getTree: function(){
      
      if(!tree_panel)
      {
        tree_panel = new Ext.Panel({          
          region: 'west',                    
          margins:'3 0 3 3',
          cmargins:'3 3 3 3',  
          html: link_builder_html        
        });
      }        
    },
    
    getWin: function(){
      if(!win)
      {
        if(!tree_panel) { this.getTree(); }
              
        win = new Ext.Window({
          title: 'Link Builder',
          closable:true,
          width:425,         
          plain:true,          
          modal: true,
          resizable: false,
          autoDestroy: false,
          closeAction: 'hide',
          buttons:  [
                      {
                        text: 'Update Link',
                        handler:  function() { if(ctrl) { updateLink(ctrl); } }
                      },
                      {
                        text: 'Cancel',
                        handler:  function() { win.hide(); }
                      }
                    ],
          
          items: [tree_panel]
        });   
      } 
      else
      {
				mgr = tree_panel.getUpdater();
				mgr.update({ url: '/link/LinkBuilder.asp?hide_buttons=1' });
      }
           
      return win;      
    },
    
    hide : function() { win.hide(); }
		
  }
};


//Alert Dialog
var Alert = function(){
  return{
    show : function(title, message){
       Ext.MessageBox.alert(title, message);
    }
  };           
}(); 


var appVer = navigator.userAgent.toLowerCase();
var mac= (appVer.indexOf("mac") != -1);
var opera = (appVer.indexOf('opera') != -1);
var firefox = (appVer.indexOf('firefox') != -1);

function ShowHide(strID)
{
	//2008-01-02 TICKET: 29402
	if ((document.all || document.getElementById) && (!opera) && ((mac && firefox) || (!mac)))
	{
		var strSpan;
		var strFldr; 
		var strTree;
		
		if (document.getElementById)
		{
			strSpan = "document.getElementById('contents" + strID + "')";
			strFldr = "document.getElementById('imgFldr" + strID + "')";
			strTree = "document.getElementById('imgPlusMin" + strID + "')";
		}
		else
		{
			strSpan = "document.all['contents" + strID + "']";
			strFldr = "document.all['imgFldr" + strID + "']";
			strTree = "document.all['imgPlusMin" + strID + "']";
		}

		fldrItem = eval(strFldr);
		treeItem = eval(strTree);
		
		//Change the folder and plus minus images
		if (treeItem)
		{
			strTreeImgSrc = treeItem.src.toString();
	
			if (strTreeImgSrc.search("plus.gif")>=0) 
			{
				treeItem.src = "/link/images/min.gif";				
				fldrItem.src = "/images/icons/icon-folder-open.gif";
			}
			else 
			{ 
				treeItem.src = "/link/images/plus.gif";
				fldrItem.src = "/images/icons/icon-folder.gif";
			}
		}
			
		//Show or hide the span element if it exists
		spanItem = eval(strSpan);
		if (spanItem)
		{
			if (spanItem.style.display=='none') spanItem.style.display="";
			else spanItem.style.display="none";	
		}
	}
	else
	{
		var strUrl;
		strUrl = document.URL;

		if (strUrl.indexOf(strID) > 0)
		{
			strUrl = strUrl.replace(',e' + strID, '');
			strUrl = strUrl.replace('e' + strID, '');
		}
		else
		{
			if (strUrl.indexOf('?') == 0)
				strUrl = strUrl + '?exploded=e' + strID;
			else if (strUrl.indexOf('exploded') > 0)
				strUrl = strUrl + ',e' + strID;
			else
				strUrl = strUrl + '&exploded=e' + strID;
		}
		document.location.href = strUrl;	
	}
}

strBaseUrl = '';
function showLink(strLink, strUrl, strPopUpUrl, isLink)
{
  if (isLink && isLink == 1)
  {
    strUrl = strUrl.replace(/\%/g,'%25');//this must be first!
    strUrl = strUrl.replace(/\#/g,'%23');
    strUrl = strUrl.replace(/\?/g,'%3F');
    strUrl = strUrl.replace(/.asp%3F/g,'.asp?'); //replace the encoded value when it is part of a URL 
    strUrl = strUrl.replace(/\"/g,'%22');
    strUrl = strUrl.replace(/\&quot;/g,'%22');
    strUrl = strUrl.replace(/\.{2,}/g,'.');//replace 2 or more . with just one (invalid to have more than one consecutive in a url) TICKET 46847
  }

	if (strBaseUrl == '')
	{
		var strTemp = document.URL.substr(7);
		var arUrl = strTemp.split('/');
		strBaseUrl = 'http://' + arUrl[0];
	}
	
	// convert to absolut path if neccessary - commented TS 5/12/2008
	//if ((strUrl.substr(0, 7) != 'http://') && (strUrl.toLowerCase().substr(0, 10) != 'javascript'))
	//	strUrl = strBaseUrl + strUrl;
	
	// check for popup link
	if (strPopUpUrl) { document.link_builder.link_popup_url.value = strPopUpUrl; }
	else {document.link_builder.link_popup_url.value = ''; }  //*DA* 6/30/2008 clear popup value is a non Resource Center item is selected
				
	document.link_builder.link_name.value = strLink;
	document.link_builder.link_url.value = strUrl;
}

function updateLink(ctrl)
{      
	if (document.link_builder.link_url.value != '' && ctrl)
	{		
		var strControl = ctrl;
		var ctlToUpdate;
		var ctlPopupToUpdate;
		
		if (window.document.form1)
		{		  
			ctlToUpdate = eval('window.document.form1.' + strControl);
			ctlNormToUpdate = eval('window.document.form1.' + strControl + '_norm');
			ctlPopupToUpdate = eval('window.document.form1.' + strControl + '_popup');
			
			//Added for use with Editor
			if(!ctlToUpdate)
			   ctlToUpdate = window.document.getElementById(strControl);
			if(!ctlNormToUpdate)
			  ctlNormToUpdate = window.document.getElementById(strControl + '_norm');
			if(!ctlPopupToUpdate)
				ctlPopupToUpdate = window.document.getElementById(strControl + '_popup');
		}
		else
		{
		  ctlToUpdate = window.document.getElementById(strControl);
			ctlNormToUpdate = window.document.getElementById(strControl + '_norm');
			ctlPopupToUpdate = window.document.getElementById(strControl + '_popup');
		}
			
		
		//if (ctlToUpdate)
			//ctlToUpdate.value = document.link_builder.link_url.value;	
			
		if (ctlToUpdate)
		{
		  // *DA* 6/30/2008 if the LB was launched from the Hyperlink tool, the Open in a New Window check box
		  //is checked, and an item with a popup_url (e.g. a resource center item) is selected, 
		  //then display the popup code in the Address Input box
		  var chk = Ext.getDom("chkNewWin");
		  var is_checked = false;
		  if(chk) { is_checked = chk.checked; }
		  
		  if(is_checked && document.link_builder.link_popup_url.value != '')
		    {  ctlToUpdate.value = document.link_builder.link_popup_url.value; }
		  else
		    { ctlToUpdate.value = document.link_builder.link_url.value; }
		  	
	  }
		
		if (ctlNormToUpdate)
			ctlNormToUpdate.value = document.link_builder.link_url.value;
		
		if (ctlPopupToUpdate)
			ctlPopupToUpdate.value = document.link_builder.link_popup_url.value;
			
		AV.LinkBuilder.hide();
	}
	else
	  { Alert.show('Link Not Selected', 'Please select a link before clicking Update Link.');	}
}