/* 
 *  calgarydj.js
 *  copyright 2007-2008 Jim E Sontag
 *
 *  routines common to event.php, eventSchedule.php, weddingPlanner.php
 *  jukebox.php
 *
 *  $Revision: 1.4 $
 *  $Date: 2008/12/06 00:11:35 $
 *
 *************************************************************/

function bookMarkIt( eventTitle )
{
  var msExploder = navigator.appName.match( /Microsoft/ ) ? true : false;  
  if( msExploder ) {
    window.external.AddFavorite( window.location, eventTitle )
  } else {
    alert( 'Type Ctrl+D to bookmark this page with the password included...' )
  }
}

function pcalc()
{
  var form = document.getElementById('booking')

  // start with the base price
  var price = 450
  
  // we have to create js date objects 
  // transform 2007-02-24 17:30 to new Date(2007,02,24,17,30,00)
  var b = form.day.value + ',' + form.stime.value + ',00'
  var b1 = b.replace( /[-:]/g, ',' )
  var e = form.day.value + ',' + form.etime.value + ',00'
  var e1 = e.replace( /[-:]/g, ',' )

  var begin = eval( 'new Date(' + b1 +')' )
  var end = eval( 'new Date(' + e1 +')' )
  
  // take care of midnite wrap around
  if( end.getTime() < begin.getTime() ) {
    end = new Date( end.getTime() + (24*3600*1000) )
  } 
  
  // the base is the # of hours included the base price
  var base = 4   
  
  // either of these options adds 1 hour to the hour base
  base += form.dinnerMusic[1].checked ? 1 : 0
  base += form.dinnerMusic[2].checked ? 1 : 0
  
  // calculate the total time in milliseconds
  var span = end - begin
  
  // convert millisecond span into hours:min format
  var hours = Math.floor(span/(60*60*1000))
  var minutes = span/(60*1000) % 60

  // calc the # of overtime half hours to the nearest 1/2 hour
  // increment price by # of overtime units, to nearest 1/2 hour 
  var ot = Math.max(Math.round(span/(30*60*1000)) - (base*2),0)
  
  //alert ( 'ot is ' + ot )  
  // price over 4 hours is $60 per hour 
  price += ot * 30
  
  // add in fixed price options
  price +=  form.dinnerMusic[1].checked ? 200 : 0
  price +=  form.dinnerMusic[2].checked ? 200 : 0
  
  price += form.wireless.checked ? 50 : 0
  price += form.lights.checked ? 100 : 0
  price += form.bigSound.checked ? 100 : 0
  price += form.senior.checked  ? 50 : 0
  price += form.earlySetup.checked ? 25 : 0
  price += form.outTown.checked ? (parseInt(form.travel.value) * 1.40) : 0

  var str = '<i>New estimate: $' + price + '</i>'
  document.getElementById('newPrice').innerHTML = str
  
  document.getElementById('formBtnDiv').style.visibility = 'visible'
}

function buttonsOn()
{
  document.getElementById('saveBtn').style.visibility = 'visible'
}

/* these next 2 funcs monitor the message window, when it closes
   a page rfresh occurs so the latest window is displayed */ 
  
var mwin
function postMessage( params )
{
  mwin = window.open( params, "msgwin", "height=287,width=440,screenX=300,screenY=400,top=300,left=400" )
  setTimeout( 'checkWin()', 500 )
}

function checkWin()
{
  if( mwin ) {
    if( !mwin.closed ) {
      setTimeout( 'checkWin()', 500 )
    } else {
      document.getElementById('cancelBtn').click()
    }
  }
}
