// -----------------------------------------------------------------------------
// rvorder.js		(Copyright 2003-2005 Rick Harvey)
//
// Requires: rhlib.js, rvsearch.js
// -----------------------------------------------------------------------------

var familyAll = new Array() // familyAll[i]=option:code       // all "-"
var titleAll = new Array()  // titleAll[i]=option:code:block  // no "-"
var selectedTitle = ""
var countryCurrency = ""
var countryName = ""
var countryRegion = 0
var countryFormat = ""
var countryButton = "+postage"
var protoTable
var orderTable

// called after page has finished loading via <body onload="rvOrderInit()">
function rvOrderInit()
{
  if (rhOldBrowser() && oldBrowser)  	// oldBrowser is a global setup by rhlib
    return
  showObjId("rvdb", false)
  showObjId("searchForm", true)
  showObjId("orderForm", true)
  protoTable = document.getElementById('protoTable')
  orderTable = document.getElementById('orderTable')
  initOrderSets()
  comboFamilyFill()
  rvOrderFamily()
  document.orderForm.countryInfo.selectedIndex = 0
  rvOrderCountryInfo("")
  updateFields()
}

function rvOrderFamily()
{
  if (oldBrowser)
    return
  var vfamily = document.searchForm.vfamily
  var block = vfamily[vfamily.selectedIndex].value
  comboTitleFill(block)
  // don't want History of Spaniels to be first
  if (vfamily.selectedIndex == 0)
  {
    var vtitle = document.searchForm.vtitle
    if (vtitle.options.length)
      vtitle.selectedIndex = vtitle.options.length - 1
  }
  rvOrderTitle()
}

function rvOrderTitle()
{
  if (oldBrowser)
    return
  var vfamily = document.searchForm.vfamily
  var vtitle = document.searchForm.vtitle
  var code = vtitle[vtitle.selectedIndex].value
  // set media
  comboMediaFill(vfamily.selectedIndex, code)
  // set copies
  document.searchForm.vcopies.selectedIndex = 0
  rvOrderSameTitle()
}

function rvOrderSameTitle()
{
  if (oldBrowser)
    return
  var vtitle = document.searchForm.vtitle
  var code = vtitle[vtitle.selectedIndex].value
  // code exists?
  for (var i=orderTable.rows.length-1; i>0; i--)
    if (orderTable.rows[i].cells)	// Safari rows[] are buggy!
      if (code == orderTable.rows[i].cells[2].innerHTML)
        break;
  if (i)
    document.searchForm.vbutton.value = "Update Title"
  else
    document.searchForm.vbutton.value = "Add Title"
}

function rvoAddTitle()
{
  var vtitle = document.searchForm.vtitle
  var title = vtitle[vtitle.selectedIndex].text
  var code = vtitle[vtitle.selectedIndex].value
  var vcopies = document.searchForm.vcopies
  var copies = vcopies[vcopies.selectedIndex].value
  var vmedia = document.searchForm.vmedia
  var media = vmedia[vmedia.selectedIndex].value
  // code exists?
  for (var i=orderTable.rows.length-1; i>0; i--)
    if (orderTable.rows[i].cells)	// Safari rows[] are buggy!
      if (code == orderTable.rows[i].cells[2].innerHTML)
        break;
  if (i) // update row
  {
    orderTable.rows[i].cells[1].innerHTML = copies
    orderTable.rows[i].cells[3].innerHTML = media
  }
  else  // add new row
  {
    var row = protoTable.rows[0].cloneNode(true)
    orderTable.rows[0].parentNode.appendChild(row)
    row.cells[0].innerHTML = title
    row.cells[1].innerHTML = copies
    row.cells[2].innerHTML = code
    row.cells[3].innerHTML = media
  }
  updateFields()
}

function rvoDelTitle(rowIndex)
{
  orderTable.deleteRow(rowIndex)
  updateFields()
}

function rvOrderCountryInfo(code, text)
{
  if (oldBrowser)
    return
  if (code.length == 6)
  {
    countryCurrency = code.substr(0,3)
    countryName = text
    countryRegion = code.charAt(4)
    countryFormat = (code.charAt(5) == "N") ? "NTSC" : "PAL"
    countryButton = "convert"
  }
  else
  {
    countryCurrency = ""
    countryName = ""
    countryRegion = 0
    countryFormat = ""
    countryButton = "+postage"
  }
  updateFields()
}

function rvOrderConvert(code)
{
  if (oldBrowser)
    return
  if (!countryCurrency)
  {
    alert("Please select country")
    return
  }
  var amount = document.orderForm.vtotalprice.value
  var addr = "http://www.xe.com/pca/input.cgi?&Amount="+amount+"&From=AUD&ToSelect="+countryCurrency
  var popWin = window.open(addr,'convert','width=700,height=200');
  return false;
}

function rvOrderOldBrowserWarning()
{
  if (oldBrowser)
    document.write("Sorry, your browser is unable to run the JavaScript ordering functions.<br>"
      + " Please request our videos via our <a href=\"contact.php\">Contact Us</a> page.<br>&nbsp;")
}

// -----------------------------------------------------------------------------
// initialisation
// -----------------------------------------------------------------------------

function initOrderSets()
{
  var divSet = document.getElementsByTagName("div")
  for (i = 0; i < divSet.length; i++)
  {
    var obj = divSet[i]
    initAttrs(obj)
    if (obj.video)
    {
      var code = obj.video
      var block = (obj.block) ? obj.block : ""
      vSet[code] = obj
      if (code.charAt(0) == "-")
        familyAll[familyAll.length] = trimHTML(obj.innerHTML) + ":" + code
      else
        titleAll[titleAll.length] = trimHTML(obj.innerHTML) + ":" + code + ":" + block
    }
    if (obj.ruler)
      rSet[obj.ruler] = obj
  }
  titleAll.sort()
}

function updateFields()
{
  document.orderForm.vcount.value = vlistCount()
  document.orderForm.vtotalprice.value = priceTotal()
  document.orderForm.videoList.value = vlistString()
  document.orderForm.vformat.value = countryFormat
  document.orderForm.vtotalconvert.value = countryButton
  document.orderForm.countryName.value = countryName
}

// -----------------------------------------------------------------------------
// combo box routines
// -----------------------------------------------------------------------------

function comboFamilyFill()
{
  var opt = document.searchForm.vfamily.options
  comboClear(opt)
  for (var i = 0; i < familyAll.length; i++)
  {
    var items = familyAll[i].split(":")
    opt[opt.length] = new Option(items[0], items[1])
  }
  opt.selectedIndex = 0;
  comboTitleFill(opt[0].value)
}

function comboTitleFill(block)
{
  if (block == selectedTitle)
    return
  var previousTitle = ""  // World dogshows parts 1 & 2 have the same name
  var opt = document.searchForm.vtitle.options
  comboClear(opt)
  for (var i = 0; i < titleAll.length; i++)
  {
    var items = titleAll[i].split(":")
    var itemBlock = items[2]
    var itemCode  = items[1]
    var itemTitle = items[0]
    if (block == itemBlock && itemTitle != previousTitle)
    {
      opt[opt.length] = new Option(itemTitle, itemCode)
      previousTitle = itemTitle
      //alert("previous title = " + previousTitle)
    }
  }
  selectedTitle = opt[0].value
}

function comboMediaFill(familyIndex, code)
{
  var opt = document.searchForm.vmedia.options
  var hasDVD = false
  comboClear(opt)
  // all educational videos are on DVDs
  //if (familyIndex == 0)
  //  hasDVD = true
  // all dog show videos from 2005 onwards are on DVD
  //var year = code.replace(/\D*([0-7][0-9]+).*/,'$1')
  //if (year >= "05" && year <= "80")
  //  hasDVD = true
  //if (hasDVD)
  //  opt[opt.length] = new Option("DVD", "DVD")
  //opt[opt.length] = new Option("Video", "Video")
  opt[opt.length] = new Option("DVD", "DVD")
}

function comboOrderMsg(msg)
{
  comboFamilyMsg(msg)
  comboTitleMsg(msg)
}

function comboFamilyMsg(msg)
{
  var family = document.searchForm.vfamily
  comboMsg(family, msg)
}

function comboTitleMsg(msg)
{
  var sub = document.searchForm.vtitle
  comboMsg(sub, msg)
}

// -----------------------------------------------------------------------------
// video list routines
// -----------------------------------------------------------------------------

function vlistCount()
{
  var total = 0
  for (var i=1; i<orderTable.rows.length; i++)
    if (orderTable.rows[i].cells)	// Safari rows[] are buggy!
      total += parseInt(orderTable.rows[i].cells[1].innerHTML)
  return total
}

function vlistString()
{
  var vcount = 0
  var vlist = ""
  for (var i=1; i<orderTable.rows.length; i++)
  {
    if (orderTable.rows[i].cells)	// Safari rows[] are buggy!
    {
      var count = orderTable.rows[i].cells[1].innerHTML
      var code  = orderTable.rows[i].cells[2].innerHTML
      var media = orderTable.rows[i].cells[3].innerHTML
      vlist = vlist + " " + code + ":" + count + ":" + media
    }
  }
  return vlist
}

// -----------------------------------------------------------------------------
// Pricing routines
// -----------------------------------------------------------------------------

function priceTotal()
{
  if (orderTable.rows.length == 0)
    return ""
  var total = 0;
  var countVideo = 0
  var countDVD = 0
  // DVD/VIDEO price
  for (var i=1; i<orderTable.rows.length; i++)
  {
    if (orderTable.rows[i].cells)	// Safari rows[] are buggy!
    {
      var count = parseInt(orderTable.rows[i].cells[1].innerHTML)
      var code  = orderTable.rows[i].cells[2].innerHTML
      var media = orderTable.rows[i].cells[3].innerHTML
      var price = priceVideo(code, media)
      if (media == "Video")
        ++countVideo
      else
        ++countDVD
      total += count*price
	}
  }
  // POSTAGE
  // if a mix of DVD and Videos approximate postage as 2 DVDs = 1 Video
  if (countryRegion)
  {
    var postage
    if (countDVD == 0)
      postage = pricePostageVideo(countVideo)
    else if (countVideo == 0)
      postage = pricePostageDVD(countDVD)
    else
      postage = pricePostageVideo(countVideo + parseInt(1+countDVD/2))
    total += postage
  }
  return total + ".00"
}

// Educationals: 50 or 40 or 30
// Dog Shows: 35 each
// Add $10 for NTSC video
// Add $10 per DVD
// Add another $10 if DVD is a show (3hr/4hr shows = 2 DVDs)
function priceVideo(code, media)
{
  var price = 0
  if (vSet[code])
  {
    var block = vSet[code].block
    // Educationals: 50 or 40 or 30
    if (block == "-v1")
    {
      if (code == "ttn02")
        price = 40
      else if (code == "judge99" || code == "hos99" || code == "tt02")
        price = 30
      else
        price = 40
    }
    // Dog Shows: 40 each
    else
    {
      price = 40
    }
    if (media == "Video" && countryFormat == "NTSC")
    {
        price += 10
    }
    else if (media == "DVD")
    {
      price += 10
      // double DVD title?
      if (code == "breed00" || code == "ttn02")
        price += 10
      // Shows always span 2 DVDs (3hr/4hr video = 2 DVDs)
      //if (block != "-v1")
      //  price += 10
    }
  }
//  alert("priceVideo("+code+","+media+")="+price)
  return price
}

var postageVideo = new Array(6)
postageVideo[1] = new Array(0,  7, 11, 11, 11, 11) // Region 1: Within Australia
postageVideo[2] = new Array(0, 11, 14, 23, 26, 29) // Region 2: New Zealand
postageVideo[3] = new Array(0, 13, 17, 25, 34, 37) // Region 3: Asia
postageVideo[4] = new Array(0, 15, 20, 30, 40, 45) // Region 4: USA and Canada
postageVideo[5] = new Array(0, 17, 23, 35, 47, 53) // Region 5: UK, Europe and other

function pricePostageVideo(count)
{
  var region = countryRegion
  if (region < "1" || region > "5")
    region = 5
  var price = 0
  while (count > 5)
  {
    price += postageVideo[region][5]
    count -= 5
  }
  if (count < 0 || count > 5)
    return 0
  price += postageVideo[region][count]
//  alert("pricePostageVideo("+count+")="+price)
  return price
}

var postageDVD = new Array(6)
postageDVD[1] = new Array(0,  6,  7,  7, 11, 11, 11) // Region 1: Within Australia
postageDVD[2] = new Array(0,  8, 11, 11, 15, 15, 18) // Region 2: New Zealand
postageDVD[3] = new Array(0,  9, 13, 13, 18, 18, 22) // Region 3: Asia
postageDVD[4] = new Array(0, 10, 15, 15, 21, 21, 26) // Region 4: USA and Canada
postageDVD[5] = new Array(0, 12, 18, 18, 24, 24, 30) // Region 5: UK, Europe and other

function pricePostageDVD(count)
{
  var region = countryRegion
  if (region < "1" || region > "5")
    region = 5
  // just charge for 6 DVDs
  if (count > 6)
    count = 6
  var price = postageDVD[region][count]
//  alert("pricePostageDVD("+count+")="+price)
  return price
}

