// Jun 2003
// functions to check if given URL is accessible
// Both the cdinsert and itrans online interfaces form.php3 use this
// - they both have the Submit button named "webform.submitkey" for this to work

// to trigger this, add a img src pointing to the url to test, example:
//$img = "http://${remotehost}/favicon.ico";
//echo "<img src=\"$img\" width=1 height=1 onError=\"portBlocked(this.src);\">";
//
// add this line between <HEAD> ... </HEAD>
//<script type="text/javascript" src="/tools/portaccess.js"></script>
// Jan 2005:  portBlocked no longer works with Internet Explorer 6.0
// if img is a .ico file.
//---
// there was a bug in IE 5.0, where img and onError did not work:
// http://support.microsoft.com/default.aspx?scid=kb;en-us;251109
// "Local Files May Be Accessed By Using JavaScript Linked to Image Tag"
// But looks like in IE 6.0 the problem still exists for .ico files
// So, use a .gif file in the img (such as transpdot.gif)

//-------------------------------------------------------------------
// cookies.js:
/**
 * Read the JavaScript cookies tutorial at:
 *   http://www.netspade.com/articles/javascript/cookies.xml
 */

/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}
//-------------------------------------------------------------------
// Port checking - blocked or not

  var access_denied = false;
  var access_denied_text = "Outbound TCP Port may be blocked.\nBehind restrictive firewall?\nMay or may not work from your site (click on Submit to try it).\n";
  var access_src = "";
  var checked_cookie = "portIsBlocked";

  function portBlocked(port, picSrc) {
    var blockedParagraph = document.getElementById("blockedParagraph"); 
    access_denied = true;
    access_src = "Port " + port + " " + picSrc;
//----------
// commented out key disabling - Jan 2005 - bugs in IE can cause false alarms
// So, don't block the key, let the user be able to click it, just
// print the warning in case it does not work...
//  document.webform.submitkey.disabled = true;// true grays out, hard to read
//----------
    blockedParagraph.innerHTML = "<p><b>" + access_denied_text + "</p><p>" + access_src + "</b></p>";
    if (!getCookie(checked_cookie + port)) { 
       alert(access_denied_text + access_src);
       setCookie(checked_cookie + port, "true", null, "/", null);
    }
  }
  function checkAccess() {
    if (access_denied) {
      alert(access_denied_text + access_src);
      return false;
    }
    // if the code got up to here, we can submit the form
    return true;
  }
//-------------------------------------------------------------------
