// JavaScript Document //----------------------------------------------------------------    var interval_id;   var doRefresh = false;   var clockStarted = false;   var secondsSinceLastWrite = 0; // COUNTER USED TO REWRITE CLOSINGS (TO REMOVE YELLOW BACKGROUND) EVERY THIS MANY SECONDS   var currentlyRefreshing = false; // FLAG TO STOP MULTIPLE REFRESHES    //----------------------------------------------------------------   function refreshClosings()   {	// MAKE USER WAIT THE LENGTH OF THE SYNC DELAY CYCLE, NOT THE TIME WHEN THE NEXT CYCLE IS RUN	// IF WE REFRESH EVERYONE EXACTLY WHEN THE NEXT CYCLE OCCURS, IT WOULD CREATE UNNECESSARY LOAD	if(timePassed < syncDelay)	 {	  var nextUpdate = syncDelay - timePassed;	  window.alert('No new closings. The next update cycle will occur in ' + nextUpdate + ' seconds. Your list will automatically update at that time with any new closings.');	  doRefresh = true;	 } // END IF	else 	 {	  doRefresh = true;	  reloadClosings();	 } // END ELSE DO IMMEDIATE REFRESH   } // END FUNCTION  //----------------------------------------------------------------   function reloadClosings()    {	 if(currentlyRefreshing) return;	 if(!doRefresh) return;	 currentlyRefreshing = true;	 doRefresh = false;	 clearInterval(interval_id);	 location.reload();	} // END FUNCTION //----------------------------------------------------------------      function tickTock()    {	 currentTime += 1;	 secondsSinceLastWrite++;	 timePassed = currentTime - originalTime;     if(timePassed > syncDelay) reloadClosings();	 // REFRESH THE LIST ON THE CLIENT MACHINE TO REMOVE YELLOW BACKGROUNDS WHERE NECESSARY	 if(secondsSinceLastWrite > 10) writeClosings();	} // END FUNCTION    //----------------------------------------------------------------    function startClock()    {     interval_id = setInterval('tickTock()', 1000);	 clockStarted = true;	} // END FUNCTION  //----------------------------------------------------------------   function decodeString(theString)    {	 return theString;	} // END FUNCTION   //-----------------------------------------------------------------      function sortClosings()    {	 var sort_by = document.getElementById('sort_by');	 sort_by = sort_by.options[sort_by.options.selectedIndex].value;	 var arrayLength = closings.length;	 var d = arrayLength;	 var flag = 1;	 var temp;	 var newValue;	 var thisValue;	      while( flag || (d>1))      // boolean flag (true when not equal to 0)     {         flag = 0;           // reset flag to 0 to check for future swaps         d = parseInt((d+1) / 2);         for (i = 0; i < (arrayLength - d); i++)        {        switch(sort_by)        {        case 'closing_type_id':        newValue = closings[i + d].closing_type_id;        thisValue = closings[i].closing_type_id;            break            case 'org_type_id':        newValue = closings[i + d].org_type_id;        thisValue = closings[i].org_type_id;           break        case 'org_name':        newValue = closings[i + d].org_name;        thisValue = closings[i].org_name;           break        case 'org_city':        newValue = closings[i + d].org_city;        thisValue = closings[i].org_city;           break        case 'org_state':        newValue = closings[i + d].org_state;        thisValue = closings[i].org_state;           break        case 'county':        newValue = closings[i + d].county;        thisValue = closings[i].county;           break        case 'closing_start_date':        newValue = closings[i + d].closing_start_date;        thisValue = closings[i].closing_start_date;           break        case 'closing_end_date':        newValue = closings[i + d].closing_end_date;        thisValue = closings[i].closing_end_date;           break        case 'closing_effective_day':        newValue = closings[i + d].closing_effective_day;        thisValue = closings[i].closing_effective_day;          break        case 'closing_entered_date':        newValue = closings[i + d].closing_entered_date;        thisValue = closings[i].closing_entered_date;           break        } // END SWITCH             if (newValue > thisValue)              {                      temp = closings[i + d];      // swap items at positions i+d and d                      closings[i + d] = closings[i];                      closings[i] = temp;                      flag = 1;                  // indicate that a swap has occurred              }         }     }	 	} // END FUNCTION         //-----------------------------------------------------------------         function closing(theData)    {	 a = theData.split("~~");		 this.closing_type_id = decodeString(a[0]);	 this.org_type_id = decodeString(a[1]);	 this.user_id = decodeString(a[2]);	 this.closing_short_details = decodeString(a[3]);	 this.closing_long_details = decodeString(a[4]);	 this.org_name = decodeString(a[5].toUpperCase());	 this.org_city = decodeString(a[6].toUpperCase());	 this.org_state = decodeString(a[7]);	 this.county = decodeString(a[8]);	 this.closing_description = decodeString(a[9]);	 this.org_type_description = decodeString(a[10]);	 this.closing_start_date = decodeString(a[11]);	 this.closing_end_date = decodeString(a[12]);	 this.closing_effective_day = decodeString(a[13]);	 this.closing_entered_date = decodeString(a[14]);	 this.org_id = decodeString(a[15]);	 this.closing_id = decodeString(a[16]);	 	 return this;		} // END FUNCTION	   //-----------------------------------------------------------------      function closingEffectiveDay(theDay)    {	 if(theDay == '1') return 'Sunday';	 else if(theDay == '2') return 'Monday';	 else if(theDay == '3') return 'Tuesday';	 else if(theDay == '4') return 'Wednesday';	 else if(theDay == '5') return 'Thursday';	 else if(theDay == '6') return 'Friday';	 else if(theDay == '7') return 'Saturday';	 else return '';	} // END FUNCTION      //-----------------------------------------------------------------   function writeClosings()    {	 if(!clockStarted) startClock();	 secondsSinceLastWrite = 0;			 var closing_type_filter = document.getElementById('closing_type').value;	 var org_type_filter = document.getElementById('org_type').value;	 var nc = closings.length;	 var toWrite = '<table border=1 cellpadding=1 cellspacing=0 width=100%>';	 var c;	 var closingAge = 0;	 var bgColor;	 var ced; // CLOSING EFFECTIVE DAY	 	 // IF NO CLOSINGS	 if(nc < 1) toWrite += '<tr><td><center><strong><p>No closings found for this organization.</p></strong></center></td></tr>';	 for(var i = nc - 1; i >= 0; i--)	  {	   c = closings[i];	   closingAge = currentTime - c.closing_entered_date;	   // FILTER CLOSINGS IF A FILTER WAS SELECTED	   if(closing_type_filter != 'all')	    {		 // IF LATE START WAS SELECTED, IT COULD BE ANY OF THREE TYPES		 if(closing_type_filter == 'late') 		  {		   if(c.closing_type_id != 'la10' && c.closing_type_id != 'la2h' && c.closing_type_id != 'laot') continue;		  } // END IF LATE		 // IF CLOSED WAS SELECTED, IT COULD BE ANY OF THREE TYPES		 else if(closing_type_filter == 'clos') 		  {		   if(c.closing_type_id != 'clos' && c.closing_type_id != 'cloe' && c.closing_type_id != 'cloh') continue;		  } // END IF CLOSED         else 		  {		   if(closing_type_filter !=  c.closing_type_id) continue; // ELSE MAKE SURE THAT THE CURRENT CLOSING TYPE MATCHES THE SELECTED FILTER EXACTLY		  } // END ELSE NOT LATE START        } // END IF CLOSING TYPE FILTER	   if(org_type_filter != 'all')	    {	     if(org_type_filter !=  c.org_type_id) continue; // MAKE SURE THAT THE CURRENT ORG TYPE MATCHES THE SELECTED FILTER		} // END IF ORG TYPE FILTER	   // MAKE EFFECTIVE DAY HUMAN READABLE	   ced = closingEffectiveDay(c.closing_effective_day);	   // YELLOW IF LESS THAN 15 MINUTES OLD	   bgColor = closingAge < 900 ? '#FFFF00' : '#FFFFFF';	          toWrite += '<tr valign="middle"><td bgcolor=' + bgColor + '><span class="closing_text"><strong>' + c.org_name + '</strong></span> <span class="closing_text">' + c.org_city + ',' + c.org_state + '&nbsp;&nbsp;</span>';	   toWrite += '</td><td bgcolor=' + bgColor + '><span class="closing_text"><strong>' + c.closing_description + ' ' + ced + '</strong> - ' + c.closing_long_details + '</td></tr>';	  } // END LOOP	  	  toWrite += '</table>';	 document.getElementById('closingsBox').innerHTML = toWrite;	} // END FUNCTION