// ==================================================================================================================================
// CHECK LOADING
// alert("Loading code04.js");
// ==================================================================================================================================

// ################################## MAKE DATELINE ##################################

	function makeDateline(upper)							// IF THERE'S A CASE, MAKE IT ALL CAPS
		{
		month = new Array(12)
		month[0] = "January";
		month[1] = "February";
		month[2] = "March";
		month[3] = "April";
		month[4] = "May";
		month[5] = "June";
		month[6] = "July";
		month[7] = "August";
		month[8] = "September";
		month[9] = "October";
		month[10] = "November";
		month[11] = "December";
	
		day = new Array(7);
		day[0] = "Sunday";
		day[1] = "Monday";
		day[2] = "Tuesday";
		day[3] = "Wednesday";
		day[4] = "Thursday";
		day[5] = "Friday";
		day[6] = "Saturday";
	
		now = new Date;
		theDay = day[now.getDay()];
		theMonth = month[now.getMonth()];
		theDate = now.getDate();
		theYear = now.getYear();
		theYear = "" + theYear;									// MAKE DATE OBJECT A STRING
		theYear=theYear.substring (theYear.length-2, theYear.length);		// FIX THE YEAR '100' PROBLEM
		yearPrefix = "20";
		theDateline = theDay + ", " + theMonth + " " + theDate + ", " + yearPrefix + theYear;
		
		if(upper)											// MAKE IT UPPERCASE, IF PARAMETER
			{
			theDateline = theDateline.toUpperCase();
			}
		document.write(theDateline);
		}



// ==================================================================================================================================
// CHECKS TIME AND DATE ON OIL SUBMISSION
// ==================================================================================================================================

function checkOil()
	{
	if(document.getElementById("collectionDay"))
		{

		dontAskAgain = true;

		collection_day = document.getElementById("collectionDay").value;

		switch (collection_day)
			{
			case 	"Monday" :
				dayBefore = 5 ;
				break;
			case 	"Tuesday" :
				dayBefore = 1 ;
				break;
			case 	"Wednesday" :
				dayBefore = 2 ;
				break;
			case 	"Thursday" :
				dayBefore = 3 ;
				break;
			case 	"Friday" :
				dayBefore = 4 ;
				break;
			default :
				dayBefore = "none";
			}

		if(dayBefore == "none"){return (1);}

		yourDay = "tomorrow";					// YOUR COLLECTION DAY, FOR WARNING MESSAGE
		if(collection_day == "Monday")
			{
			yourDay = "Monday";				// ACCOUNTS FOR THE WEEKEND
			}

		justnow = new Date();

		thisDay = justnow.getDay();
		thisTime = justnow.getHours();

		if(thisDay==6) { thisDay = 5; }			// SATURDAY IS TREATED THE SAME AS FRIDAY
		if(thisDay==0) { thisDay = 5; }			// SUNDAY IS TREATED THE SAME AS FRIDAY

		if(thisDay==dayBefore)					// this is the last day. is it too late?
			{
			if(thisTime > 15)					// TOO LATE
				{
				if(confirm("PLEASE NOTE!\n\nOil pickups must be requested by 4:00 pm the workday before\nyour normal trasn pickup day.\n\nIt's too late to schedule an oil pickup for " + yourDay + ".\n\nIf you want to schedule the pickup for a week later, click \"OK\".\n\nOtherwise, Cancel."))
					{
					return  1;
					}
				else	{
					return  0;
					}
				}
			}
		else	{
			return 1;
			}

		}
	else	{
		return 1
		}

	}



// ==================================================================================================================================
// REVERSES THE DISPLAY VALUE OF A DIV
// ==================================================================================================================================

function showHideDiv(whatDiv)
	{
	theState = document.getElementById(whatDiv).style.display;
	if(theState=="block")
		{
		theState="none";
		}
	else	{
		theState = "block";
		}
	document.getElementById(whatDiv).style.display = theState;
	}			


// ==================================================================================================================================
// SUBMIT AND VERIFY FORM --------------------------------------------------------------------------------------------------------
// ==================================================================================================================================

// NOTES:

// The CGI script may required specific INPUT NAMES. See 'form_emailer_04.cgi'.
// The CGI script may use TEXT, TEXTAREA and SELECT NAMES as labels for output. See 'form_emailer_04.cgi'.
// The CGI script may use CHECKBOX or RADIO  VALUES as output. See 'form_emailer_04.cgi'.

// The FORM tag must contain this: onSubmit = "return submitForm(this.form)

// Required input fields must have names prefaced by "_required_".
// Other input field names must be prefaced with an underscore.
// Button names must be prefaced with "_button_".

// If a required text or textarea input is empty, focus will set to it after the error message.
// If a required radio button or checkbox is empty, the error message will contain the name of the object, so name appropriately.



/* ADDED ON JULY 25, 2007 - "checkOil()"
   Function to check whether oil pickup was scheduled before 4 pm the day before the indicated collection day.
   The first line in this function calls checkOil() to see if we should proceed or not.   
*/


function submitForm(theForm,formNumber)
	{

	if(checkOil()==0){return(false);}						// checkOil FUNCTION SAY SAYS WE'RE TOO LATE. SEE NOTE ABOVE.

	showElements = 0;									// MAKE THIS -1 TO SHOW ALERT BOX WITH ELEMENTS FOR TESTING
	if(formNumber == null)								// THERE *IS* A FORM NUMBER PARAMETER PASSED TO SCRIPT
		{
		formID = "document.forms[0]";						// NO FORM NUMBER PASSED TO SCRIPT. ASSUME FORM NUMBER IS 0.
		}
	else	{
		if(isNaN(formNumber))
			{
			formID="document." + formNumber;				// FORM HAS A NAME. USE FOR ID
			}
		else	{
			formID="document.forms[" + formNumber + "]";		// FORM HAS NO NAME. USE NUMBER
			}
		}
	if(showElements) {text="";}

	temp = eval(formID + ".elements.length");					// NUMBER OF ELEMENTS IN THE FORM
	for(i=0;i<temp;i++)										// LOOP THROUGH ELEMENTS
		{
		thisElement = eval(formID + ".elements[" + i + "]");
		if(showElements) {text += thisElement.name + " - " + thisElement.value + " - " + thisElement.checked + " - " + thisElement.selected + "\n";}
			
		if(thisElement.type  == "text")							// THIS IS A TEXT BOX. TRIM LEADING AND TRAILING SPACES.
			{
			thisElement.value = spaceTrim(thisElement.value);			// YES, TRIM SPACES
			}
		if(thisElement.type  == "text" || thisElement.type == "textarea") 	// VALIDATE TEXT OR TEXTAREA FIELD FOR CONTENT
			{
			// IF THIS IS A REQUIRED FIELD, AND THERE'S NOTHING IN IT...
			if(thisElement.value.length < 1 && thisElement.name.indexOf("_required")>-1)
				{
				alertMissingInfo(formNumber,i,thisElement.name);					// ERROR MESSAGE, SET FOCUS
				return(false);
				break;
				}
			}						
		if(thisElement.type=="select-one")					// IT'S A LIST
			{
			if(thisElement.value == "Select..." &&  thisElement.name.indexOf("_required")>-1)	// IT'S REQUIRED
				{
				alertMissingInfo(formNumber,i,thisElement.name);					// ERROR MESSAGE, SET FOCUS
				return(false);
				break;
				}
			}				

		if(thisElement.type == "checkbox")							// VALIDATE RADIO BUTTONS FOR SELECTED
			{
			if(!thisElement.checked  && thisElement.name.indexOf("_required")>-1)	// REQUIRED CHECK NOT CHECKED. 
				{
				alertMissingInfo(formNumber,i,thisElement.name);					// ERROR MESSAGE, SET FOCUS
				return(false);
				break;
				}
			}
		if(thisElement.type == "radio" && thisElement.name.indexOf("_required")>-1)	// VALIDATE RADIO BUTTONS FOR SELECTED
			{
			areAnyChecked = 0;									// TOGGLES WHEN A CHECKED RADIO BUTTON IS FOUND
			// radioObject = eval("document.forms[" + formNumber + "]." + thisElement.name);	// GET THE RADIO OBJECT
			radioObject = eval(formID + "." + thisElement.name);	// GET THE RADIO OBJECT
			for(j=0;j<radioObject.length;j++)						// GO THROUGH ALL BUTTONS IN THIS RADIO OBJECT
				{
				areAnyChecked += radioObject[j].checked;				// IF 'CHECKED' IS TRUE FOR ANY, THIS WILL BECOME '1'
				}
			if(areAnyChecked == 0)								// STILL 0, NONE ARE CHECKED
				{
				alertMissingInfo(formNumber,i,thisElement.name);		// ERROR MESSAGE, SET FOCUS
				return(false);
				break;
				}					
			}
		}
	if(showElements) {alert(text);}
	return (true);
	}

	function alertMissingInfo(formNumber,whichElement,objectName)
		{
		text = "\n\nRequired information is missing...";
		if(objectName)
			{
			temp = rightString(objectName,objectName.length-10);			// REMOVE '_required_''
			if(temp.substring(0,8)=="textarea") { temp = rightString(temp,temp.length-8); }
			if(temp.substring(0,4)=="text") { temp = rightString(temp,temp.length-5); }
			if(temp.substring(0,5)=="radio") { temp = rightString(temp,temp.length-6); }
			temp = replaceChar(temp,"_"," ");						// REMOVE UNDERSCORES
			text += "\n\n" + temp.toUpperCase();
			}
		self.alert(text);
		thisElement.focus();
		}


// ==================================================================================================================================
// LINK FOR HITS AND ONE-PIXEL TRANSPARENT GIF -----------------------------------------------------------------------------------
// ==================================================================================================================================

	function recordHit(page)
		{
		// MAKE PAGE INFORMATION
		file_info = location.href;						// PAGE HREF
		file = filenameOnly(file_info);					// GET FILE PART
		file =  file.substring(0,file.lastIndexOf ('.'));		// DELETE FILE EXTENSION
		folder = pathOnly(file_info);						// GET FOLDER INFO
	 	folder = folder.substring(0,folder.lastIndexOf ('/'));	// DROP LAST SLASH FROM FOLDER INFO
		folder = filenameOnly(folder);					// DROP PATH FROM FOLDER
		page = folder + ":_" + file;						// MAKE PAGE INFO

		// LOCATE THE CGI
		hitsCGI = "http://www.longbeach-recycles.org/cgi/hits04.cgi?";
		if(location.hostname == "127.0.0.1") { hitsCGI = "/cgi/hits04.cgi?"; }
		cgiCall = "<img src='" + hitsCGI + page + "'>";
		eval(document.write(cgiCall));
		// alert(cgiCall);
		}


// ==================================================================================================================================
// THANK YOU ALERT ---------------------------------------------------------------------------------------------------------------
// ==================================================================================================================================

	// Shows a 'thank you' alert if the document's search parameter is 'thankyou'

	function thanks()
		{
		if(location.search)
			{
			temp=location.search;
			temp=temp.substring(1,temp.length);
			if(temp=="thankyou")
				{
				alert("\n____________________________\n\nTHANK YOU.\n\nYour message has been received,\nand we will review it shortly.\n____________________________");
				self.location.search="";
				}
			}
		}

// ==================================================================================================================================
// PICKUP DAY --------------------------------------------------------------------------------------------------------------------
// ==================================================================================================================================

	function testit()
		{
		alert("testing");
		}

	function newDayAlert(msg)
		{
		alert(msg);
		document.lookup.address.focus();
		return (false);
		}

	function newDay(theform)
		{
		address=spaceTrim(document.lookup.address.value);
		if(address == "")
			{
			newDayAlert('Please enter an address.');
			return (false);
			}
		spaceIsAt = address.indexOf(" ");
		if(spaceIsAt==-1)
			{
			newDayAlert('Please enter both a number and street name.');
			return (false);
			}
		streetnumber = address.substring(0,spaceIsAt);
		
		if("0123456789".indexOf(streetnumber.substring(0,1)) == -1)
			{
			newDayAlert('Please enter both a number and street name.');
			return (false);
			}
		streetname = address.substring(spaceIsAt+1,address.length);

		streetname=escape(streetname);
		querystring="community/refuseschedule/refuseschedule.cfm?ADDR=" + streetnumber + "&STREET=" + streetname + "&Submit=Submit";

		if(location.href.indexOf("wmirror") > -1)
			{
			querystring = "http://wmirror.longbeach.gov:8000/" + querystring;
			win=window.open(querystring, "popup", "toolbar=yes,location=yes,directories=yes,status=YES,menubar=yes,scrollbars=yes,resizable=yes,height=480,width=700");
			return (false);
			}

		whichserver = "" + location.hostname;
		if(whichserver.indexOf(".us") > -1 || whichserver.indexOf(".gov") > -1) { whichserver = "www.longbeach-recycles.org"; }
		whichdir = "/home/";
		if(location.href.indexOf("development") > -1) { whichdir = "/development/"; }
		baseHREF = "http://" + whichserver + whichdir + "quicklinks/new_days/index.htm";

		if(location.hostname == "127.0.0.1" || location.hostname == "www.longbeach-recycles.org")
			{
			querystring = "/cgi/route_04.cgi?streetnumber=" + streetnumber + "&streetname=" + streetname + "&getday=Get%20Day&basehref=" + baseHREF;
			}
		else	{
			querystring = "http://www.longbeach-recycles.org/cgi/route_04.cgi?streetnumber=" + streetnumber + "&streetname=" + streetname + "&getday=Get%20Day&basehref=" + baseHREF;
			}
		popWindow(querystring);
		return (false);
		}



// ==================================================================================================================================
// POP UP WINDOW FUNCTION ----------------------------------------------------------------------------------------------
// ==================================================================================================================================

function popWindow(file,mywindow)
	{
	win=window.open(file, "popwindow",'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,height=320,width=580')
	win.focus();
	}	


// ==================================================================================================================================
// QUICK LINKS ----------------------------------------------------------------------------------------------
// ==================================================================================================================================

function quicklinks(file,mywindow)
	{
	temp=location.href;
	alert(temp);
return;

	win=window.open(file, "popwindow",'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,height=320,width=580')
	win.focus();
	}	


// ==================================================================================================================================
// HELPERS -------------------------------------------------------------------------------------------------
// ==================================================================================================================================

function rightString (InString, num)
	{
	OutString=InString.substring (InString.length-num, InString.length);
	return (OutString);
	}


function replaceChar (InString, oldChar, newChar)
	{
	OutString="";
	for (Count=0; Count < InString.length; Count++)
		{
		TempChar=InString.substring (Count, Count+1);
		if (TempChar!=oldChar)
			OutString += TempChar;
		else OutString += newChar;
		}
	return (OutString);
	}

function spaceTrim(InString)
	{
	var LoopCtrl=true;
	while (LoopCtrl)
		{
		if (InString.indexOf("  ") != -1)
			{
			Temp = InString.substring(0, InString.indexOf("  "))
			InString = Temp + InString.substring(InString.indexOf("  ")+1, InString.length)
			}
		else	{
			LoopCtrl = false;
			}
		}
	if (InString.substring(0, 1) == " ") InString = InString.substring(1, InString.length)
	if (InString.substring (InString.length-1) == " ") InString = InString.substring(0, InString.length-1)
	return (InString)
	}

function pathOnly (InString)
	{
	InString = "" + InString;
	temp=InString.lastIndexOf ('/', InString.length-1);
	OutString=InString.substring  (0, temp+1);
	return (OutString);	
	}

function stripChar (InString, StripThis)
	{
	OutString="";
	for (Count=0; Count < InString.length; Count++)
		{
		TempChar=InString.substring (Count, Count+1);
		if (TempChar!=StripThis)
			OutString=OutString+TempChar;
		}
	return (OutString);
	}

	function replaceChar(InString, replaceThis, withThis)
		{
		OutString="";
		for (Count=0; Count < InString.length; Count++)
			{
			TempChar=InString.substring (Count, Count+1);
			if (TempChar==replaceThis)
				{
				OutString=OutString+withThis;
				}
			else	{
				OutString=OutString+TempChar;
				}
			}
		return (OutString);
		}

	function stripPunctuation(InString)
		{
		OutString="";
		RefString="!$()-_;:,.<>[]&{}";
		for (Count=0; Count < InString.length; Count++)
			{
			TempChar=InString.substring (Count, Count+1);
			if(RefString.indexOf(TempChar)<0)
				{
				OutString=OutString+TempChar;
				}
			}
		return (OutString);
		}

function filenameOnly(InString)
	{
	LastSlash=InString.lastIndexOf ('/', InString.length-1)
	OutString=InString.substring  (LastSlash+1, InString.length)
	return (OutString);	
	}

