/* Copyright 1997 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/***************************************************/
/* File   : JavaScript.c                           */
/*                                                 */
/* Purpose: JavaScript support.                    */
/*                                                 */
/* Author : A.D.Hodgkinson                         */
/*                                                 */
/* History: 24-Jul-97: Created.                    */
/***************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "swis.h"

#include "HTMLLib.h" /* HTML library API, Which will include html2_ext.h, tags.h and struct.h */

#include "wimp.h"
#include "wimplib.h"
#include "event.h"

#include "svcprint.h"
#include "Global.h"
#include "Utils.h"

#include "Browser.h"
#include "FetchPage.h"
#include "Frames.h"
#include "Customer.h"
#include "URLutils.h"

#include "JavaScript.h"

/*************************************************/
/* javascript_body_onload()                      */
/*                                               */
/* When a page has finished fetching the main    */
/* page data, a BODY tag may specify some        */
/* JavaScript action. This functon should be     */
/* called to carry out that action.              */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             with a filled in 'onload' field   */
/*             describing the contents of the    */
/*             onLoad command in the HTML.       */
/*************************************************/

_kernel_oserror * javascript_body_onload(browser_data * b)
{
  _kernel_oserror * e = NULL;

  #ifdef TRACE
    if (tl & (1u<<24)) Printf("javascript_body_onload: Called\n");
  #endif

  #ifdef CUSTOMER_SPECIAL

    /* Only call customer_ functions if on the customer's site */

    if (strstr(browser_current_url(b), "www.customer.com"))
    {
      e = customer_body_onload(b);
    }

  #endif

  b->onload = NULL;

  return e;
}

/*************************************************/
/* javascript_href_onclick()                     */
/*                                               */
/* When something is clicked upon that has an    */
/* onClick attribute specified for it, this      */
/* function is called with the details of that   */
/* attribute.                                    */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the item clicked on;  */
/*                                               */
/*             Pointer to the token representing */
/*             that item;                        */
/*                                               */
/*             Pointer to an int, in which 1 is  */
/*             written if the contents of the    */
/*             HREF attribute in the link that   */
/*             held the onClick should be        */
/*             ignored (else 0 is written) -     */
/*             this may be NULL if you're not    */
/*             interested.                       */
/*************************************************/

_kernel_oserror * javascript_href_onclick(browser_data * b, HStream * t, int * ignore)
{
  _kernel_oserror * e = NULL;

  #ifdef TRACE
    if (tl & (1u<<24)) Printf("javascript_href_onclick: Called\n");
  #endif

  if (ignore) *ignore = 0;

  #ifdef CUSTOMER_SPECIAL

    /* Only call customer_ functions if on the customer's site */

    if (strstr(browser_current_url(b), "www.customer.com"))
    {
      e = customer_href_onclick(b, t, ignore);
    }

  #endif

  return e;
}

/*************************************************/
/* javascript_window_open()                      */
/*                                               */
/* Opens a given URL in a given target window.   */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the window_open call; */
/*                                               */
/*             1 if the specified target window  */
/*             must be found to proceed, else 0  */
/*             (if the target isn't found the    */
/*             URL is opened in the given        */
/*             browser instead);                 */
/*                                               */
/*             1 if the URL displayed in the     */
/*             target is to be recorded in the   */
/*             History before the new one is     */
/*             fetched, else 0;                  */
/*                                               */
/*             Pointer to the URL to fetch;      */
/*                                               */
/*             Pointer to the target (or NULL /  */
/*             a null string for the given       */
/*             browser).                         */
/*************************************************/

_kernel_oserror * javascript_window_open(browser_data * b, int must_find, int record, char * url, char * target)
{
  browser_data * targetted = b;

  #ifdef TRACE
    if (tl & (1u<<24)) Printf("javascript_window_open: Called for %p with '%s' and target '%s'\n",b,url,target);
  #endif

  /* Exit if no URL or base browser is given */

  if (!b || !url || !*url) return NULL;

  /* If a target is specified, try and find it */

  if (target && *target)
  {
    browser_data * ancestor = utils_ancestor(b);

    #ifdef TRACE
      if (tl & (1u<<24)) Printf("javascript_window_open: Proceeding with target '%s'\n",target);
    #endif

    targetted = frames_find_named(ancestor, target);

    #ifdef TRACE
      if (tl & (1u<<24)) Printf("javascript_window_open: targetted = %p\n",targetted);
    #endif

    /* If the target can't be found and the parameters specify it must be, */
    /* then exit; else target the base browser instead.                    */

    if (!targetted)
    {
      if (must_find) return NULL;
      else targetted = b;
    }
  }

  /* Open the URL */

  #ifdef TRACE
    if (tl & (1u<<24)) Printf("javascript_window_open: Opening to target %p\n",targetted);
  #endif

  /* Relativise the URL if necessary */

  if (!strstr(url, HTTPmethod ProtocolSeparator)) /* (URLutils.h) */
  {
    browser_data * ancestor = utils_ancestor(targetted);
    char         * r_url;

    #ifdef TRACE
      if (tl & (1u<<24)) Printf("javascript_window_open: Relativising URL\n");
    #endif

    /* Need to go down a sequence of options for which URL to relativise to! */

    r_url = browser_current_url(targetted);

    if (!r_url) r_url = browser_fetch_url(targetted);
    if (!r_url) r_url = browser_current_url(ancestor);
    if (!r_url) r_url = browser_fetch_url(ancestor);

    url = HtmlRelativiseURL(r_url, url, targetted->stream);

    #ifdef TRACE
      if (tl & (1u<<24)) Printf("javascript_window_open: Relativised URL is '%s'\n",url);
    #endif
  }

  return fetchpage_new(targetted, url, record, 0);
}