﻿//| ah_sitedata.js (for angelohornak.co.uk site)
//|------------------------------------------------------------------------------
//| Copyright 2005 by Mark Anderson for Shoantel Limited
//| Site's catalogue content is copyright Angelo Hornak, but templates and support
//| JS/NP files are copyright Shoantel (except where attributed) and  my not be
//| copied or re-used without prior permission
//|------------------------------------------------------------------------------
//| Language               : JavaScript
//| Author                 : Mark Anderson (mark@shoantel.com)
//|------------------------------------------------------------------------------
//| Description:
//|   Defines utility data variables/functions for Portfolio NetworkPublish site.
//|
//| History:
//| 13 Jan 2005          : 1.0 
//| 18 Jan 2005          : 1.01
//| 25 Jan 2005          : 1.02
//| 08 Sep 2005          : 1.03
//| 19 May 2006          : 1.04 (Validates) Current version.
//|------------------------------------------------------------------------------
//*
//-------------------------------------------------------------------------------
// Site variables:
//
// replace all [] sections with actual data unless var not in use
//
// cookie duration info
var cookiePath = '/'; // scope of cookies persistence (String)
var myCollection = 10; // # of days set in NP's server.properties 'general.collectionExpires' for collection persistence (Number)
var myDuration = 24 * myCollection; // time to persist cookies (Number)
// site alias
var mySite = 'hornak'; // NP site alias as in site.properties and also name of NP site OS folder (Sting)
// catalogue alias
var myCatalog = 'catalog'; // name of the project's catalogue alias in alias.properties (NP default is 'catalog') (String)
// template mappings
var myResults = 'resultslinks.np'; // name of Results template (String)
var myResultscanned = 'resultslinkscanned.np'; // name of Canned Results template (String)
var myResultsPerPage = 15; // number of results per results page (Number) **must match template's settings**
var myLightbox = 'lightboxlinks.np'; // name of Lightbox template (String)
var myLightboxPerPage = 15; // number of results per lightbox page (Number) **must match template's settings**
var myLinkList = 9; // (odd) number of links to create in result/lightbox page link list (number)
var myOrderForm = 'orderform.np'; // name of Order Form template (String)
var myBounce = 'bounce.np'; // template for 'bounce' when adding/deleting items (String)
var myDetails = 'detailscombi.np'; // template for 1-up record details (String)
var myPreview = 'previewcombi.np'; // template for 1-up record details (String)
// template related
var myPrevSize = '450'; //long side in pixels (String)
var myThumbSize = '112'; // thumbnail long side in pixels for results/lightbox(String)
var myBigThumbSize = '256'; // thumbnail long side for details page in pixel (String)
var mySortField = 'Priority'; // optional field for 'sorton' Find parameter; app's default is 'Cataloged', i.e. RID. [case sensitive] 
var myOrderSortField = 'Filename'; // optional field for order form 'sorton' Find parameter; app's default is 'Cataloged', i.e. RID. [case sensitive](String)
var mySortOrder = '1'; // optional field for ascending' Find parameter; app's default is '1' (1 = A-Z, 0 = Z-A)  (String)
var myLightboxName = 'Lightbox'; // The on-screen name for your cart/collection/lightbox (String)
// site folder structure
var myNPSiteName = '/res/sites/' + mySite +'/'; //The relative path to the site root if based in the NP site folder (String)
var myNPLoc = '/netpub/server.np?'; // The relative path to the NP app (for dynamic queries, etc.) (String)
var myCSS = 'siteport.css'; // name of CSS file in /resources/ folder (String)
var myJS = 'ah_sitedata.js'; // name of this file in /libraries/ folder (String)
//var myHome = 'http://netpublish.blacknight.co.uk/res/sites/hornak/index.html'; // no domain
var myHome = 'http://www.angelohornak.co.uk/index.html'; // site's home page (String)
var myIndex = 'index.html'; // site's home page (String)
//var mySearchTpl = 'searchah.np'; // name of NP search templates
//var mySearch = myNPLoc + 'base&site=' + mySite + '&template=' + mySearchTpl + '&catalog=' + myCatalog; // site's search page (String)
//var mySearch = myNPSiteName + 'searchah.htm'; // site's search page (String)
var mySearch = 'http://www.angelohornak.co.uk/searchah.htm'; // site's search page (String)
var mySearchnew = 'http://www.angelohornak.co.uk/searchahnew.htm'; // site's new (empty) search page (String)
var myHelp = myNPSiteName + 'info_helpah.htm'; // site's help page (String)
var myPrev = myNPSiteName + 'previewsAH/'; // The relative path to the site's sub-folder holding Previews (String)
var myMedia = myNPSiteName + 'media/'; // The relative path to the site's sub-folder holding 'media' - e.g. logos, branding (String)
var myOrig = myNPSiteName + 'originals/'; // The relative path to the site's sub-folder holding Originals (String)
var myRes = myNPSiteName + 'resources/'; // The relative path to the site's sub-folder holding 'resources', e.g. CSS (String)
var myLibs = myNPSiteName + 'libraries/'; // The relative path to the site's sub-folder holding 'libraries', e.g. JS (String)
var myTemps = myNPSiteName + 'templates/'; // The relative path to the site's sub-folder holding  NP templates (String)
// names/emails for template customisation
var mySiteName = 'Angelo Hornak Photograph Library'; // Name string used in TITLE (String)
var myFooterName = 'Angelo Hornak'; // Name string used in page footer (String)
var myAddress = 'angelo@angelohornak.co.uk'; // addressee for order form output (String)
// form related
//var myFormLoc = 'http://netpublish.blacknight.co.uk/res/sites/hornak/nporder.lasso'; // old ver - no domain
var myFormLoc = 'http://www.angelohornak.co.uk/nporder.lasso'; // URL location of form-to-email script (String)
var myFormUser = 'The Angelo Hornak Photograph Library'; //short user name for use in form results (String)
var myReturnPage = 'http://www.angelohornak.co.uk/index.html'; // URL of page to which user returns after order submission (String)
// client OS - detect Preview bug
var myClientOS = 'Mac'; // OS of client used to create previews (values 'Mac' or 'PC') (String)
//
// End project variables
// Below are utility functions for the site
//-----------------------------------------------------------------
//
//-----------------------------------------------------------------
// Purpose: Used to write correct IMG rag for thumbnails
// Used by: results, lightbox, detailscombi
//-----------------------------------------------------------------
function setThumb(id, w, h, alt, theThumbSize) {
  if (id === null) {
    return;
  }
  var preset = parseInt(theThumbSize,10);
  if(alt === '') {
    alt = "Click to enlarge image";
  }
  else {
    alt += " - Click to enlarge";
  }
  var nw = parseInt(w,10);
  var nh = parseInt(h,10);
  var sw = '';
  var sh = '';
  if (w==h) {
    sw = preset;
    sh = preset;
  }
  else if (w === "" | h === "") {
    sw = preset;
    sh = preset;
  }
  else if (nw >nh) {
    sw = preset;
    sh = Math.round(nh*(preset/nw));
  }
  else if (nh > nw) {
    sh = preset;
    sw = Math.round(nw*(preset/nh));
  }
  var writeStr = '<img src="'+myNPLoc+'thumbnail='+id+'&amp;site='+mySite+'&amp;catalog='+myCatalog+'" alt="'+alt+'" width="'+sw+'" height="'+sh+'" border="0">';
    return writeStr;
}

//---------------------------------------------------------------------------
// Purpose: for returning to N-up page from 1-up or toggling preview/details
// Used by: detailsboxes, detailslboxes, preview, previewl, details, detailsl
//---------------------------------------------------------------------------
function setNewURL(sourceURL,reqOffset,theTemplate){
//test and clean out submit, #, etc.
  var newSourceURL ='';
  var reA = /&submit[^&]+/;
  var mA = reA.exec(sourceURL);
  if (mA !== null) {
    newSourceURL = sourceURL.replace(mA,'');
  }
  if (newSourceURL !== '') {
    sourceURL = newSourceURL;
  }
  //set new offset
  var reB = /&offset=\d+/;
  var mB = reB.exec(sourceURL);
  if (mB !== null) {
    var theOffset = '&offset=' + reqOffset.toString(10);
    newSourceURL = sourceURL.replace(mB,theOffset);
  }
  if (newSourceURL !== '') {
    sourceURL = newSourceURL;
  }
  //set new template
  var reC = /&template[^&]+/;
  var mC = reC.exec(sourceURL);
  if (mC !== null) {
    theTemplate = '&template=' + theTemplate;
    newSourceURL = sourceURL.replace(mC,theTemplate);
  }
  location.href = newSourceURL;
}

//--------------------------------------------------------
// Purpose; find the N-up 'more' page for a 1-up page item
// Used by: detailsboxes, detailslboxes, preview, previewl, details, detailsl
//--------------------------------------------------------
function findResultPage(theOffset,howMany) {
  if (theOffset === 0) {
    return theOffset;
  } else {
  var newOffset = Math.floor(theOffset/(howMany));
  if (newOffset === 0) {
    return newOffset;
    } else if (newOffset > 0) {
    newOffset = (newOffset * howMany);
    return newOffset;
    }
  }
}

//-----------------------------------------------------------
// Used to display preview images in corectly defined IMG tag
// Used by: preview, previewl
//-----------------------------------------------------------
function showPic(src,rid,w,h,res_w,res_h,myClientOS) {
// if using previews with actual filenames set filename for  'src' and 'rid'
// otherwise use filename for 'src' and Item ID for 'rid'
  if (src === null) {
    return;
  }
  // if rid = filename use RID otherwise construct preview filename
  if (src !== rid) {
    var myZeros = '0000000000';
    var lengthRID = rid.length;
    myZeros = myZeros.slice(lengthRID);
    rid = 'p' + myZeros + rid + '.jpg';
  }
  var preset = parseInt(myPrevSize,10);
  res_w = parseInt(res_w,10);
  res_h = parseInt(res_h,10);
  var nw = parseInt(w,10);
  var nh = parseInt(h,10);
  if (myClientOS == 'Win') {
    var unw = nw*(72/res_w);
    var unh = nh*(72/res_h);
  }
  var sw = '';
  var sh = '';
  if (w==h) {
    if (unw < preset) {
      sw = Math.round(unw);
      sh = Math.round(unh);
    } else {
      sw = preset;
      sh = preset;
    }
  }
  else if (w === '' | h=== '') {
    sw = preset;
    sh = preset;
  }
  else if (nw >nh) {
    if (unw < preset) {
      sw = Math.round(unw);
      sh = Math.round(unh);
    } else {
      sw = preset;
      sh = Math.round(nh*(preset/nw));
    }
  }
  else if (nh > nw) {
    if (unh < preset) {
      sw = Math.round(unw);
      sh = Math.round(unh);
    } else {
      sh = preset;
      sw = Math.round(nw*(preset/nh));
    }
  }
  // **LOCAL AMENDMENT** adding 'p_' to front of the rid value
  var writeStr = '<img src="'+myPrev+'p_'+rid+'" alt="'+src+'" width="'+sw+'" height="'+sh+'" border="0">';
  return writeStr;
}

//---------------------------------------------------------------------------------
// Purpose: master function for moves between lightbox/results and their sub-pages
// Used by: results, lightbox   [Note: 1-up pages use previewSubmit() for this purpose]
//---------------------------------------------------------------------------------
function goWhere(linkName,theNextURL,task,theForm,theOffset) {
// if offset not needed in call simply set to zero(0 number not '0' string)
  theNextURL = decodeURIComponent(theNextURL);
  var target = '';
  if (anyCheck(theForm) === true) {
    target = constructURL(linkName,theNextURL,theOffset);
    // write the 'nextPage' cookie for bounce template
    setNextPageCookie(target);
    if ((task == 'ADD') || (task == 'DEL')) {
      theForm.template.value = myBounce;
      theForm.submit();
    }
  } else {
    target = constructURL(linkName,theNextURL,theOffset);
    location.href = target;
  }
}

//--------------------------------------------------
// Purpose; subfunction of goWhere(), sets next URL
// Used by: results, lightbox
//-------------------------------------------------
function constructURL(linkName,theNextURL,theOffset) {
  // add 'categories of search by adding cases below
  // process assumes 'linkName' is a keyword in the catalogue
  switch(linkName) {
    case 'CURRENT' :
      var myPage = myGetCookie('lastResultsPage');
      if ((myPage===null) || (myPage==='')) {
        myPage = myNPLoc + 'quickfind\&catalog=' + myCatalog + '\&site=' + mySite + '\&template=' + myResults + '\&sorton=' + mySortField + '\&ascending=' + mySortOrder + '\&offset=0';
      }
      theNextURL = myPage;
      return theNextURL;
      //break;
    case 'RESULTS_PREVIEW' :
      theNextURL = previewLayout(theNextURL,myPreview,'results',theOffset);
      return theNextURL;
    case 'RESULTS_DETAILS' :
      theNextURL = previewLayout(theNextURL,myDetails,'results',theOffset);
      return theNextURL;
    case 'BOX_PREVIEW' :
      theNextURL = previewLayout(theNextURL,myPreview,'box',theOffset);
      return theNextURL;
    case 'BOX_DETAILS' :
      theNextURL = previewLayout(theNextURL,myDetails,'box',theOffset);
      return theNextURL;
    default:
      return theNextURL;
  }
}
//---------------------------------------------------------------------------------
// Purpose: Set approriate preview for preview source type (result v.s lightbox)
// Used by: results, lightbox
//---------------------------------------------------------------------------------
function previewLayout (sourceURL,theTemplate,theLayout,reqOffset) {
  var newSourceURL ='';
  // clean out existing  layout values.
  var reA = /&layout[^&]+/;
  var mA = reA.exec(sourceURL);
  if (mA !== null) {
    newSourceURL = sourceURL.replace(mA,'');
  }
  if (newSourceURL !== '') {
    sourceURL = newSourceURL;
  }
  //set new offset
  var reB = /&offset=\d+/;
  var mB = reB.exec(sourceURL);
  if (mB !== null) {
    var theOffset = '&offset=' + reqOffset.toString(10);
    newSourceURL = sourceURL.replace(mB,theOffset);
  }
  if (newSourceURL !== '') {
    sourceURL = newSourceURL;
  }
  //set new template
  var reC = /&template[^&]+/;
  var mC = reC.exec(sourceURL);
  if (mC !== null) {
    theNewTemplate = '&template=' + theTemplate + '&layout='+theLayout;
    newSourceURL = sourceURL.replace(mC,theNewTemplate);
  }
  return newSourceURL;
}

//--------------------------------------------------------------------
// Purpose: Checks for any ticked items before moving to the next page
// Used by: results, lightbox
//--------------------------------------------------------------------
function anyCheck(theForm) {
// returns true if one or more boxes is ticked
  if (theForm.itemid) {
    //checkbox exists but 1 or many?
    if (theForm.itemid.length) {
      // >1 checkbox
      var arrMax = theForm.itemid.length;
      var found = 0;
      for (var idx = 0; idx < arrMax; idx++) {
        if (theForm.itemid[idx].checked) {
          found = 1;
          return true;
        }
      }
    } 
    else {
      // only one checkbox
      if (theForm.itemid.checked) {
        return true;
      }
      else {
        return false;
      }
    }
  }
  else {
  return false;
  }
}

//-------------------------------------------------------
// Purpose: Add/delete call from preview/detail templates
// Used by: detailsboxes, detailslboxes, preview, previewl, details, detailsl
//-------------------------------------------------------
function previewSubmit(myForm, myAction, myNext) {
  if (myAction == 'ADD') {
    setNextPageCookie(myNext);
    myForm.submit();
  }
  else if (myAction == 'DEL'){
    setNextPageCookie(myNext);
    myForm.submit();
  }
}

//----------------------------------------------------
// Purpose: sub-function for previewSubmit and goWhere
// Used by: resultslinks, lightboxlinks, detailsboxes, detailslboxes, preview, previewl, details, detailsl
//----------------------------------------------------
function setNextPageCookie(myURL) {
  myDeleteCookie('nextPage');
  var expDate = new Date();
  expDate.setTime( expDate.getTime() + ( myDuration * 60 * 60 * 1000 ) );
  mySetCookie('nextPage', myURL, expDate, cookiePath);
}

//---------------------------------------------------------------------------
// Purpose: Open lightbox & orderform directly  from preview/detail templates
// Used by: detailsboxes, detailslboxes, preview, previewl, details, detailsl
//---------------------------------------------------------------------------
function makeLink(theDest) {
  var nextPage = '';
  if (theDest == 'LIGHTBOX') {
    nextPage = myGetCookie('lastBox');
    if ((nextPage===null) || (nextPage==='')) {
      nextPage = myNPLoc + 'show\&catalog=' + myCatalog + '\&site=' + mySite + '\&template=' + myLightbox + '\&sorton=' + mySortField + '\&ascending=' + mySortOrder + '\&offset=0';
    } else {
    // convert lightbox add/delte calls to a simple 'show' command
    nextPage = nextPage.replace(/\?remove/,'?show');
    nextPage = nextPage.replace(/\?add/,'?show');
    }
    location.href = nextPage;
  } else if (theDest == 'ORDER') {
    location.href = myNPLoc + 'show\&catalog=' + myCatalog + '\&site=' + mySite + '\&template=' + myOrderForm + '\&sorton=' + myOrderSortField + '\&ascending=' + mySortOrder + '\&offset=0';
  } else if (theDest == 'RESULTS') {
    nextPage = myGetCookie('lastResultsPage');
    if ((nextPage===null) || (nextPage==='')) {
      nextPage = myNPLoc + 'quickfind\&catalog=' + myCatalog + '\&site=' + mySite + '\&template=' + myResults + '\&sorton=' + mySortField + '\&ascending=' + mySortOrder + '\&offset=0';
    }
    location.href = nextPage;
  }
}

//---------------------------------------------------------------------------
// Purpose: Tick or un/tick all items on page
// Used by: results, lightbox
//---------------------------------------------------------------------------
function doTicks(myField) {
  if (!myField) {
    return;
  }
  if (checkflag == "false") {
    if (myField.length >=1){
      for (i = 0; i < myField.length; i++) {
        myField[i].checked = true;
      }
    } else {
      myField.checked = true;
    }
    checkflag = "true";
  } else {
    if (myField.length >=1) {
      for (i = 0; i < myField.length; i++) {
        myField[i].checked = false;
      }
    } else {
      myField.checked = false;
    }
    checkflag = "false";
  }
}

//---------------------------------------------------------------------------
// Purpose: Check ticks are present before subitting
// Used by: results, lightbox
//---------------------------------------------------------------------------
function checkForTicks(myForm,myField) {
  if (myField.length >=1){
    for (i = 0; i < myField.length; i++) {
      if (myField[i].checked === true) {
        myForm.submit();
        return true;
      }
    }
    alert('You must tick at least one item!');
    return false;
  } else {
    if(myField.checked===true) {
        myForm.submit();
        return true;
    } else {
    alert('You must tick at least one item!');
    return false;
    }
  }
}

/////////////////////////////////////////////////////
// Cookies
/////////////////////////////////////////////////////
// section below copyright as per original header
//*
//| was - [cookie.js]
//|------------------------------------------------------------------------------
//| Copyright 2001 by GalaSoft Laurent Bugnion
//|------------------------------------------------------------------------------
//| Website                : www.benoitlange.ch
//| Language               : JavaScript
//| Author                 : Laurent Bugnion (galasoft-LB@bluewin.ch)
//|------------------------------------------------------------------------------
//| Description:
//|   Defines the cookie functions.
//|
//| History:
//|   06.03.2000 Lbu : Created in this version.
//|------------------------------------------------------------------------------
//*
// Cookie functions -------------------------------------------------------------
// Thanx a lot to Jerry Aman, Optima System
// and Bill Dortch, hIdaho Design for the free use of their code.
// Found on http://www.cookiecentral.com/js_cookie8.htm

function myGetCookieVal( offset ) {
  var endstr = document.cookie.indexOf ( ";", offset );
  if ( endstr == -1 ) {
    endstr = document.cookie.length;
  }
  return unescape( document.cookie.substring( offset, endstr ) );
}

function myGetCookie( name ) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while ( i < clen ) {
    var j = i + alen;
    if ( document.cookie.substring( i, j ) == arg ) {
      return myGetCookieVal ( j );
    }
    i = document.cookie.indexOf( " ", i ) + 1;
    if ( i === 0 ) {
      break;
    }
  }
  return null;
}

function mySetCookie( name, value ) {
  var argv = mySetCookie.arguments;
  var argc = mySetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
  document.cookie = name + "=" + escape (value) +
    ( ( expires === null ) ? "" : ( "; expires=" + expires.toGMTString() ) ) +
    ( ( path === null ) ? "" : ( "; path=" + path ) ) +
    ( ( domain === null ) ? "" : ( "; domain=" + domain ) ) +
    ( ( secure === true ) ? "; secure" : "" );
}

function myDeleteCookie ( name ) {
  var exp = new Date();
  exp.setTime ( exp.getTime() - 1 );  
  mySetCookie( name, "", exp, cookiePath );
}

function myTestCookie() {
  var expDate = new Date();
  //valid one minute
  expDate.setTime( expDate.getTime() + ( 60 * 1000 ) );
  setCookie( "testCookie", "OK", expDate );
  testing = myGetCookie( "testCookie" );
  if ( testing == "OK" ) {
    return true;
  }
  else {
    return false;
  }
}
