if (Drupal.jsEnabled) {
  $(document).ready(function() {
    // Strip the host name down, removing subdomains or www.
    var host = window.location.host.replace(/^(([^\/]+?\.)*)([^\.]{4,})((\.[a-z]{1,4})*)$/, '$3$4');
    var subdomain = window.location.host.replace(/^(([^\/]+?\.)*)([^\.]{4,})((\.[a-z]{1,4})*)$/, '$1');

    // Determine what subdomains are considered internal.
    if (Drupal.settings.extlink.extSubdomains) {
      var subdomains = "([^/]*)?";
    }
    else if (subdomain == 'www.' || subdomain == '') {
      var subdomains = "(www\.)?";
    }
    else {
      var subdomains = subdomain.replace(".", "\.");
    }

    // Build regular expressions that define an internal link.
    var internal_link = new RegExp("^https?://" + subdomains + host, "i");

    // Find all links which are NOT internal and begin with http (as opposed
    // to ftp://, javascript:, etc. other kinds of links.
    // When operating on the 'this' variable, the host has been appended to
    // all links by the browser, even local ones.
    // In jQuery 1.1 and higher, we'd us a filter method here, but it is not
    // available in jQuery 1.0 (Drupal 5 default).
    var external_links = new Array();
    var mailto_links = new Array();
    /*$("a").each(function(el) {
      try {
        var url = this.href.toLowerCase();
        if (url.indexOf('http') == 0 && !url.match(internal_link)) {
          external_links.push(this);
        }
        else if (url.indexOf('mailto:') == 0) {
          mailto_links.push(this);
        }
      }*/
    var White_list = Drupal.settings.extlink.extWhiteList;
    if (White_list != '') 
    {
	  wlist = White_list;
	  White_list = '';
	  White_list_arr = wlist.split(",");
	  for (var i=0;i<White_list_arr.length;i++ )
	  {
		White_list_arr[i] += "/";
	  }
	  White_list = White_list_arr.join();
    }

    $("a").each(function(el) {
      try {
        var url = this.href.toLowerCase();
        White_list = White_list.toLowerCase();
        if (White_list != '') {
          if (White_list.indexOf(url) < 0 && (url.indexOf('http') == 0 && !url.match(internal_link))) {
            external_links.push(this);
          }
        }
		else {
		  if (url.indexOf('http') == 0 && !url.match(internal_link)) {
            external_links.push(this);
          }
		}
      }

      // IE7 throws errors often when dealing with irregular links, such as:
      // <a href="node/10"></a> Empty tags.
      // <a href="http://user:pass@example.com">example</a> User:pass syntax.
      catch(error) {
        return false;
      }
    });
//alert(external_links);
    if (Drupal.settings.extlink.extClass) {
      // Apply the "ext" class to all links not containing images.
      if (parseFloat($().jquery) < 1.2) {
        $(external_links).not('[img]').addClass(Drupal.settings.extlink.extClass);
      }
      else {
        $(external_links).not($(external_links).find('img').parents('a')).addClass(Drupal.settings.extlink.extClass);
      }
    }
    $(external_links).click( function() {
      if (Drupal.settings.extlink.extPromptType == '0') {
        confirmed = window.confirm(Drupal.settings.extlink.extTarget_Msg); if(!confirmed) { return false; }
      }
      else if (Drupal.settings.extlink.extPromptType == 1) {
        // Apply the target attribute to all links.
          extlink_window(Drupal.settings.extlink.path, escape(this));
          return false;
      }
      else {
          window.location.href = Drupal.settings.extlink.path+'&url='+this;
          return false;

      }
    } );

	// Code to display the configurations in Edit mode
	if (Drupal.settings.extlink.extPromptType == 0) {
	  $('#extlink-prompt-formats').hide();
	  $('#extlink-prompt-buttons').hide();
	}
	else if (Drupal.settings.extlink.extPromptType == 1)
	{
	  if (Drupal.settings.extlink.extPromptDhtml == 'dhtml_button') {
	    $('#extlink-prompt-buttons').hide();
		  $('#extlink-dhtml-image').hide();
		  $('#extlink-prompt-web').hide();
	  }
	  else 
    { 
	    $('#extlink-prompt-buttons').show();
		  $('#extlink-prompt-web').hide();
		  $('#extlink-dhtml-image').show();
		  $('#extlink-web-image').hide();
	  }
	}
	else if (Drupal.settings.extlink.extPromptType == 2)
	{
	  if (Drupal.settings.extlink.extPromptWeb == 'web_button') {
	    $('#extlink-prompt-buttons').hide();
		  $('#extlink-web-image').hide();
		  $('#extlink-prompt-dhtml').hide();
	  }
	  else 
    {
	    $('#extlink-prompt-buttons').show();
		  $('#extlink-web-image').show();
		  $('#extlink-prompt-dhtml').hide();
		  $('#extlink-dhtml-image').hide();
	  }
	}
  });
}

/**
 * Show/hide System Prompt options.
 */
Drupal.behaviors.extlink = function() {
  $("input[name='extlink_prompt_type'][value=2]").click(function() {
	$('#extlink-prompt-formats').show('slow');
	$('#extlink-prompt-web').show('slow');
	$('#extlink-prompt-dhtml').hide('slow');
	$('#extlink-prompt-buttons').hide('slow');
	
  });

  $("input[name='extlink_prompt_type_web'][value='web_button']").click(function() {
    $('#extlink-prompt-buttons').hide('slow');
  });

  $("input[name='extlink_prompt_type_web'][value='web_image']").click(function() {
    $('#extlink-prompt-buttons').show('slow');
	$('#extlink-web-image').show('slow');
    $('#extlink-dhtml-image').hide('slow');
  });

  $("input[name='extlink_prompt_type'][value=1]").click(function() {
	$('#extlink-prompt-formats').show('slow');
	$('#extlink-prompt-web').hide('slow');
	$('#extlink-prompt-dhtml').show('slow');
	$('#extlink-prompt-buttons').hide('slow');
  });

  $("input[name='extlink_prompt_type_dhtml'][value='dhtml_button']").click(function() {
    $('#extlink-prompt-buttons').hide('slow');
  });

  $("input[name='extlink_prompt_type_dhtml'][value='dhtml_image']").click(function() { 
    $('#extlink-prompt-buttons').show('slow');
	$('#extlink-dhtml-image').show('slow');
    $('#extlink-web-image').hide('slow');
  });

  $("input[name='extlink_prompt_type'][value=0]").click(function() {
	$('#extlink-prompt-formats').hide('slow');
	$('#extlink-prompt-buttons').hide('slow');
  });

};

function cancel_fn(admin_choice) {
  if (admin_choice == 1)
    document.getElementById('divExtLink').innerHTML="";
    parent.ext_window.hide();
}


function ok_fn(url, admin_choice) {
  if (admin_choice == '1') {
    document.getElementById('divExtLink').innerHTML="";
   parent.ext_window.hide();
   window.open(url,this.target);
  }
}