
// 
//	File: 		noticesInclude.js
//	Description:	This page uses javascript written by Douglas rice
//
// 	Changes:	Added code to support a pop up box with full details in ShowRow()

// ------------------------
// Sec 1.0 Global Variables
// ------------------------

  var editMode = ( location.search == "?edit" )
  var row=0

  var count = 0
  var gbA = new Array()
  var ReasonForActionA = new Array()

  var firstPastRowFlag = true

// ------------------------
// Sec 2.0 Functions - 
// ------------------------

function stopRefresh(){
  // The page refreashes itself every minute, if you click on a form field increas this to 5 min.
  clearInterval( timerID )
  timerID=setInterval('location.reload(true)',300*1000)
}

// ------------------------
// Sec 2.0 Form Functions - 
// ------------------------

function validate(){
     // Validate the form data and prevent submit if invalid

     var F1 = window.document.forms["F1"]
     F1.action=""

     var str = ""
     var foundBlank = false

     for ( var cnt=0; cnt < 8 ; cnt++ ){
       fieldValue = F1["row_1_col_"+cnt].value	
       str = str + fieldValue +"|"
       if ( fieldValue == "" ) {
         foundBlank = true
       }	
     }
     //var foundBlank = false

     if ( foundBlank ) {
	alert( ' please fill in all fields ')
	return false
     }	

     F1.userfield2.value = str
     dateA = F1["row_1_col_1"].value.split("/")
     timeA = F1["row_1_col_2"].value.split(":")

     if ( ( dateA.length >0 ) && ( dateA.length >0 ) ) {	
       var noticesDate  = new Date( dateA[2]*1,dateA[1]*1-1,  dateA[0]*1, timeA[0]*1, timeA[1]*1,0,0 )
       if ( isNaN( noticesDate )) {
 	  alert( "invalid date format\n use DD/MM/YYYY  HH:MM" )
	  return false
       }
       // Do a check for Y2K entered dates - DD/MM/YY
       now = new Date()
       var deltaTime = noticesDate.valueOf()- (now.valueOf() - 24.0*3600.0*7.0*1000)
       if ( deltaTime < 0 ){
 	  alert( "requested date, "+noticesDate.toString()+" " + deltaTime + " " + noticesDate.valueOf() + " " + now.valueOf() + " is more than 7 days in the past \n, please use date format: DD/MM/YYYY  HH:MM" )
	  return false
       }

     } else {
	  return false
     }

     var date = new Date() 
     if ( confirm( 'Confirm update\n'+str ) ) { 	
        F1.action  = "http://ccgi.dougrice.plus.com" 
        F1.userfield5.value = "uf5 "+ date.toString() 
        F1.action += "/cgi-bin/guestbook.pl" 
        return true
     } else {
       return false
     }
}

function formField( row,col,size ){
  // this function saves the typing
  var opStr = 
	'<P class="MsoNormal c16">'+
	'<input type="text" name="row_'+row+'_col_'+col+'" value="" size="'+size+'" onclick="stopRefresh()"> '+
	'</P>'
  return opStr 
}



function showFormRow( p1 ){
  colA = p1.split("|")

  row =  1

	currentRow =
	'<TR class="c28">'+
	'  <TD width="75" valign="top" class="c17">'+
		formField( row,0,10 )+
	'  </TD>'+
	'  <TD width="140" valign="top" class="c20">'+
	formField( row,1,10 )+formField( row,2,10 )+
	'  </TD>'+
	'  <TD width="96" valign="top" class="c22">'+
	formField( row,3,10 )+
	'  </TD>'+
	'  <TD width="129" valign="top" class="c23">'+
	formField( row,4,20 )+
	'  </TD>'+
	'  <TD width="135" valign="top" class="c24">'+
	formField( row,5,14 )+
	'  </TD>'+
	'  <TD width="287" valign="top" class="c25">'+
	formField( row,6,20 )+
	'  </TD>'+
	'  <TD width="76" valign="top" class="c27">'+
	formField( row,7,10 )+
	'   <input type="submit" name="B1" value="Submit" >'+
	'  </TD>'+
	'</TR>'+
	''
  self.document.write( currentRow )
}

// ------------------------
// Sec 3.0 Functions - Table row generating function
// ------------------------

function showRow( p1 ){

  // write a function to split the string
  //showRow( "Hall1|01/11/2007|15:30|Union Meeting|Doug Rice|01234 5678910|diversity|20min|" )

  colA = p1.split("|")
  row = row + 1
  paragraphClass = "c16" 
  var spanStyle  = 'c15'
  var now      = new Date()
  var today    = now
      today.setHours( 1 )
      today.setMinutes( 0 )
  
  var lastWeek   = new Date()
      lastWeek.setDate(  now.getDate() - 7 )		

  var tommorrow   = new Date()
      tommorrow.setDate(  now.getDate() +1 )		
      tommorrow.setHours( 1 )
      tommorrow.setMinutes( 0 )


  dateA = colA[1].split("/")

  var noticesDateStr  = new Date( dateA[2]*1,dateA[1]*1-1,  dateA[0]*1, 2, 0,0,0 )

  // Here we pick the row formatting based upon the date

  spanStyle = 'c15today'
  pastRow = ""

  if  ( lastWeek.valueOf() > noticesDateStr.valueOf()  ) {
    // too old to display
    return
  }

  if  ( today.valueOf() > noticesDateStr.valueOf()  ) {
    spanStyle = 'c15past'
    if ( firstPastRowFlag ) {
      firstPastRow = false
      pastRow =
        '<TR class="c28">'+
        '  <TD width="827" colspan="8" valign="top" class="c30">'+
        '    <P >'+
        '      <STRONG><SPAN class="c29">what happened:-</SPAN></STRONG>'+
        '    </P>'+
        '  </TD>'+
        '</TR>'
      firstPastRowFlag = false
    } 
  }

  if ( noticesDateStr.valueOf() > tommorrow.valueOf() ) {
    spanStyle = 'c15future'
  }


  // a bit of code to allow truncation of displayed info and a pop up box if you click the link

  var ReasonForAction = colA[6]
  var showNchars = 30
  if ( ReasonForAction.length > showNchars ) {
    // save the full string in an array for display latter
    ReasonForActionA[ row ] = colA[6]
    ReasonForAction = 	" <A href='javascript:alert( ReasonForActionA[ " + row + " ] )' > <IMG border =0 src=./notices_files/magGlass.gif alt='"+colA[6]+"' ></A> " +
		ReasonForAction.substr(0,showNchars )+
		"..."
  }


  currentRow =
	'      <TR class="c28">'+
	'  <TD width="75" valign="top" class="c17">'+
	'    <P >'+
	'      <STRONG><SPAN class='+spanStyle+'>'+colA[0]+'</SPAN></STRONG>'+
	'    </P>'+
	'  </TD>'+
	'  <TD width="140" valign="top" class="c20">'+
	'    <P >'+
	'      <STRONG><SPAN class="'+spanStyle+'"><SPAN class="c18">'+colA[1]+'<BR>'+colA[2]+'</SPAN></SPAN></STRONG>'+
	'    </P>'+
	'  </TD>'+
	'  <TD width="96" valign="top" class="c22">'+
	'    <P >'+
	'      <STRONG><SPAN class="'+spanStyle+'">'+colA[3]+'</SPAN></STRONG>'+
	'    </P>'+
	'  </TD>'+
	'  <TD width="129" valign="top" class="c23">'+
	'    <P >'+
	'      <STRONG><SPAN class="'+spanStyle+'">'+colA[4]+'</SPAN></STRONG>'+
	'    </P>'+
	'  </TD>'+
	'  <TD width="135" valign="top" class="c24">'+
	'    <P >'+
	'      <STRONG><SPAN class="'+spanStyle+'">'+colA[5]+'</SPAN></STRONG>'+
	'    </P>'+
	'  </TD>'+
	'  <TD width="287" valign="top" class="c25">'+
	'    <P >'+
	'      <STRONG><SPAN class="'+spanStyle+'">'+ReasonForAction+'</SPAN></STRONG>'+
	'    </P>'+
	'  </TD>'+
	'  <TD width="76" valign="top" class="c27">'+
	'    <P >'+
	'      <STRONG><SPAN class="'+spanStyle+'">'+colA[7]+'</SPAN></STRONG>'+
	'    </P>'+
	'  </TD>'+
	'</TR>'+
	''
	// alert ( currentRow )
  self.document.write( pastRow+currentRow )
}

// ------------------------
// Sec 4.0 Functions - Guestbook functions
// ------------------------


// Process the notice requests that are in the Guestbook gbookFnotices.js

function gbF( name,email,postedOn,IPaddress,userfield1,userfield2,userfield3,userfield4,comments){
  // this function is called once per notice entry
  
  // do a bit of validation
  userfield2A = userfield2.split("|") 

  var r = new Object()
  if ( userfield2A.length > 2  ){

     var r = new Object()
     // USERFIELD2 CONTAINS THE DATA	
     var colA = userfield2.split("|")
     var today      = new Date()
     dateA = colA[1].split("/")
     timeA = colA[2].split(":")

     var noticesDate  = new Date( dateA[2]*1,dateA[1]*1-1,  dateA[0]*1, timeA[0]*1, timeA[1]*1,0,0 )
 
     if ( ! isNaN( noticesDate )) {
	// ONLY INCUDE DATA IF THE DATE IS VALID
	r.name       = name
	r.email      = email
	r.postedOn   = postedOn
	r.IPaddress  = IPaddress
	r.userfield1 = userfield1
	r.userfield2 = userfield2
	r.userfield3 = userfield3
	r.userfield4 = userfield4
	r.comments   = comments
        // add extra fields
        r.noticesDate = noticesDate
        r.noticesDateValue = noticesDate.valueOf()
        
	r.count      = count
        gbA[ count ] = r
        count = count + 1
     }
  }
}

function sortFN( a,b ){
 return ( b.noticesDateValue - a.noticesDateValue )
}


function generateTableRows(){
  // this function displays   the table rows.

  gbA.sort( sortFN )

  if ( editMode ){
    showFormRow( "" )
  }

  for ( cnt=0; cnt < gbA.length; cnt++ ){
    showRow( gbA[ cnt ].userfield2 )
  }
}


// end of file
