// File: birthday.js
// Author: William Howard
//
// Function: Provide a series of scripts to help with the creation of and processing of the birthday card lab system.
//
// Global vars
strStates = new Array;
strStates[0] = 'AL,Alabama';
strStates[1] = 'AK,Alaska';
strStates[2] = 'AZ,Arizona';
strStates[3] = 'AR,Arkansas';
strStates[4] = 'CA,California';
strStates[5] = 'CO,Colorado';
strStates[6] = 'CT,Connecticut';
strStates[7] = 'DE,Delaware';
strStates[8] = 'DC,District of Columbia';
strStates[9] = 'FL,Florida';
strStates[10] = 'GA,Georgia';
strStates[11] = 'HI,Hawaii';
strStates[12] = 'ID,Idaho';
strStates[13] = 'IL,Illinois';
strStates[14] = 'IN,Indiana';
strStates[15] = 'IA,Iowa';
strStates[16] = 'KS,Kansas';
strStates[17] = 'KY,Kentucky';
strStates[18] = 'LA,Louisiana';
strStates[19] = 'ME,Maine';
strStates[20] = 'MD,Maryland';
strStates[21] = 'MA,Massachusetts';
strStates[22] = 'MI,Michigan';
strStates[23] = 'MN,Minnesota';
strStates[24] = 'MS,Mississippi';
strStates[25] = 'MO,Missouri';
strStates[26] = 'MT,Montana';
strStates[27] = 'NE,Nebraska';
strStates[28] = 'NV,Nevada';
strStates[29] = 'NH,New Hampshire';
strStates[30] = 'NJ,New Jersey';
strStates[31] = 'NM,New Mexico';
strStates[32] = 'NY,New York';
strStates[33] = 'NC,North Carolina';
strStates[34] = 'ND,North Dakota';
strStates[35] = 'OH,Ohio';
strStates[36] = 'OK,Oklahoma';
strStates[37] = 'OR,Oregon';
strStates[38] = 'PA,Pennsylvania';
strStates[39] = 'RI,Rhode Island';
strStates[40] = 'SC,South Carolina';
strStates[41] = 'SD,South Dakota';
strStates[42] = 'TN,Tennessee';
strStates[43] = 'TX,Texas';
strStates[44] = 'UT,Utah';
strStates[45] = 'VT,Vermont';
strStates[46] = 'VA,Virginia';
strStates[47] = 'WA,Washington';
strStates[48] = 'WV,West Virginia';
strStates[49] = 'WI,Wisconsin';
strStates[50] = 'WY,Wyoming';

var strMonths = new Array();
strMonths[0] = "January";
strMonths[1] = "February";
strMonths[2] = "March";
strMonths[3] = "April";
strMonths[4] = "May";
strMonths[5] = "June";
strMonths[6] = "July";
strMonths[7] = "August";
strMonths[8] = "September";
strMonths[9] = "October";
strMonths[10] = "November";
strMonths[11] = "December";

var strDayOfMonth = new Array();
for(iDoMCount=1;iDoMCount<=31;iDoMCount++) {
  if (iDoMCount < 10 ) {
    strDayOfMonth[iDoMCount] = "0" + iDoMCount;
  } else {
    strDayOfMonth[iDoMCount] = "" + iDoMCount;
  }
}

var strFullYear = new Array();
var iBeanCount = 0;
// Yes, I know that I could have the top end of iFYCount be based on the system year date, but for now I'm just
// going to hard code it for 2007.
for(iFYCount=1930;iFYCount<=2007;iFYCount++) {
  strFullYear[iBeanCount] = "" + iFYCount;
  iBeanCount++;
}


function validateForm(formID) {
  retVal = false;
  
  // Run through all of the form fields and see if any of them are not valid.
  if (checkName(document.birthday.name) && 
      checkChildSex() &&
      checkCity(document.birthday.city) &&
      checkState(document.birthday.state) &&
      checkZip(document.birthday.zip) &&
      checkTelephone(document.birthday.tele) &&
      checkBirthday() && checkShow(document.birthday.show) ) {
    retVal = true;
  }
  
  // DEBUG:
  if (retVal) {
    //alert("Form Valid");
  } else {
    //alert("Something Wrong with Form");
  }
  return retVal;	
}


//
// ------------------------------------------------------------------------------------------
// function: addGoogleMapLink
//     Purpose: Check the address field information to see if it is valid.
//       passed to function: Nothing
//       returned from function: Nothing
//
//  Notes: 
function addGoogleMapLink() {

  // Really this should only show when we have good data.
  var lAddress = document.birthday.add1.value;
  var lCity = document.birthday.city.value;
  var lWhichState = document.birthday.state.selectedIndex;
  if (lWhichState > 0) {
  var lState = strStates[lWhichState-1].substr(0,2);
  } else {
    var lState = "";
  }
  var lZip = document.birthday.zip.value;
  var linkString = new String;
  var replaceMySpaces = lAddress.replace(/\ /g,"+");
  replaceMySpaces += ",+";
  replaceMySpaces += lCity.replace(/\ /g,"+");
  replaceMySpaces += ",+";
  replaceMySpaces += lState+",+"+lZip+",United+States";
  // http://maps.google.com/maps?f=q&hl=en&q=1+Arlen+Rd,+White+Marsh,+Baltimore,+Maryland+21236,+United+States
  
  if((lAddress.length > 0) && (lCity.length > 0) && (lState.length > 0) && (lZip.length == 5)){
    linkString = "<a href=\"http://maps.google.com/maps?f=q&hl=en&q="
    linkString += replaceMySpaces;
    linkString += "\" target=\"_map\">Link to a map to your address.</a>";
	  document.getElementById("googlemaplink").innerHTML = linkString;
  } else {
    document.getElementById("googlemaplink").innerHTML = "";
  }
}

//
// ------------------------------------------------------------------------------------------
// function: checkAddress
//     Purpose: Check the address field information to see if it is valid.
//       passed to function: address field from form
//       returned from function: true/false (true is it is a good data, false is bad data)
//
//  Notes: 
function checkAddress(addressField) {
	var response2Show = new String();
	var numberStartRegExp = /^\d/;
	var poboxStartRegExp = /^P.*O.* Box/i;
	var yesNoGoodAddress = false;
	var retVal = false;
	// Check to see if it starts with a number.
	if(addressField.value.search(numberStartRegExp) >= 0) {
		yesNoGoodAddress = true;
	}
	// Check to see if it starts with PO Box
	if(addressField.value.search(poboxStartRegExp) >= 0) {
		yesNoGoodAddress = true;
	}
	if(yesNoGoodAddress) {
		document.getElementById("youraddress").innerHTML = "Thanks for your address!";
    // what about adding in a link to Google Maps for this address?  Or after we get the state?
		retVal = true;
	} else {
		document.getElementById("youraddress").innerHTML = "Your address doesn't look right.";
		retVal = false;
	}
	
	return retVal;
}
//
// ------------------------------------------------------------------------------------------
// function: checkBirthday
//     Purpose: Check the birthday field information to see if it is valid.
//       passed to function: nothing, I have the fields hardcoded.
//       returned from function: true/false (true is it is a good data, false is bad data)
//
//  Notes: 
function checkBirthday() {
  var nYear = document.birthday.birthyear.value;
  var nLeapYear = Mod(nYear,4);
  var nMonth = document.birthday.birthmonth.selectedIndex;
  var nDayOfMonth = document.birthday.birthdayofmonth.value;
  var nMaxDays = 31;
	var dtCurrentTime = new Date();
  var retVal = false;

  if(nMonth == 1){
    // March
    // If LeapYear then 29 days, otherwise just 28.
    if (nLeapYear == 0) {
      nMaxDays = 29;
    } else {
      nMaxDays = 28;
    }
  } else {
    if(nMonth == 0 || nMonth == 2 || nMonth == 4 || nMonth == 6 || nMonth == 7 || nMonth == 9 || nMonth == 11) {
      // Jan, May,July,Aug,Oct,Dec all == 31 days.
      nMaxDays = 31;
    } else {
      // The rest of the months are 30 days.
      nMaxDays = 30;
    }  
  }

  // Don't forget that your birthday can't be before today...!!!!
	var dtPassedBirthday = new Date((nMonth+1)+"/"+nDayOfMonth+"/"+nYear);
//	alert ("dtPassedBirthday is ["+dtPassedBirthday.toString()+"]");
  if (dtPassedBirthday.getTime() > dtCurrentTime.getTime()){
    retVal = false;
    document.getElementById("yourbirthday").innerHTML = "Your birthday doesn't look right."
	} else {
	
    if(nDayOfMonth > nMaxDays) {
    // Day is too big for this month.
    retVal = false;
    document.getElementById("yourbirthday").innerHTML = "Your birthday doesn't look right."
  } else {
    retVal = true;
    document.getElementById("yourbirthday").innerHTML = "Thanks for telling me your birthday.";
  }
	}
  return retVal;
  
}
//
// ------------------------------------------------------------------------------------------
// function: checkChildSex
//     Purpose: Check the sex/gender field information to see if it is valid.
//       passed to function: nothing.  The field is a radio button and hard-coding it seemed to be the easiest approach when I built the script.
//       returned from function: true/false (true is it is a good data, false is bad data)
//
//  Notes: 
function checkChildSex() {
  var oneChecked = false;
  var retVal = false;
  if(document.birthday.sex[0].checked) {
	  // They checked they are a boy.
	  document.getElementById("yoursex").innerHTML = "You are a boy.";
    // What about putting in here where we have an image to go with this?
	  retVal = true;
  } else {
	  if(document.birthday.sex[1].checked) {
		  // They checked they are a girl.
		  document.getElementById("yoursex").innerHTML = "You are a girl.";
      // What about putting in a girls image?
		  retVal = true;
	  } else {
		  document.getElementById("yoursex").innerHTML = "You didn't answer the Boy / Girl question.";
      // And we can put in another image 
	  }
  }

  return retVal;
}
//
// ------------------------------------------------------------------------------------------
// function: checkCity
//     Purpose: Check the city field information to see if it is valid.
//       passed to function: city field from form
//       returned from function: true/false (true is it is a good data, false is bad data)
//
//  Notes: 
function checkCity(addressField) {
	var lengthOfAddressField = 0;
	var retVal = false;
	lengthOfAddressField = addressField.value.length;
	
	if(addressField.value.length <1) {
		document.getElementById("yourcity").innerHTML = "You didn't fill in your city field.";
	} else {
		document.getElementById("yourcity").innerHTML = "Thanks for telling me the city you live in.";
		retVal = true;
	}

	return retVal;
}
//
// ------------------------------------------------------------------------------------------
// function: checkName
//     Purpose: Check the name fields information to see if it is valid.
//       passed to function: one of the name fields field from form
//       returned from function: true/false (true is it is a good data, false is bad data)
//
//  Notes: 
function checkName(nameField) {
  var spaceFound = new Number;
  var response2Show = "";
  var arrBrokenName = new Array;
  var spaceFound = new Number;
  var tempField = new String;
  var firstName = new String;
  var lastName = new String;
  var otherNameStuff = new String;
  var numberOfNames = new Number;
  var placeHolder = new String;
  var retVal = false;
   
  // Also, shouldn't I pull out any non-alpha (like numbers and such) when I'm doing the count/space check?
  tempField = nameField.value.replace(/[^a-z -.]/gi,"");
  spaceFound = tempField.indexOf(" ");
  arrBrokenName = tempField.split(" ");
  numberOfNames = arrBrokenName.length -1;
  
  // Now, make the first letter of the first part of the name and the last part of the name cap.
  // - First name.
  firstName = arrBrokenName[0].substr(0,1).toUpperCase()+arrBrokenName[0].substr(1);
  
  // - Last name.
  lastName = arrBrokenName[numberOfNames].substr(0,1).toUpperCase()+arrBrokenName[numberOfNames].substr(1);

  // set tempField to be the full name (that is put the name back together with the upper cases)
  tempField = "";
  for(whichPart in arrBrokenName){
    if (whichPart == "0") {
        placeHolder = firstName + " ";
    } else {
      if(whichPart == numberOfNames){
        placeHolder = lastName;
      } else {
        placeHolder = arrBrokenName[whichPart] + " ";
      }
    }
    tempField += placeHolder;
  }
  
  // Need to check for at least one space in the name.
  if (spaceFound < 0 ) {
	  // No space found.  Let user know.
	  response2Show = "Your name doesn't seem to be filled in correctly.";
  } else {
	  // A space was found.  Let user know.
	  response2Show = "Hello " + tempField + "!";
    nameField.value = tempField;
	  retVal = true;
  }

  // Set the response to the screen.
  document.getElementById("yourname").innerHTML = response2Show;

  // don't forget to set the field value back to new "name" if we are good.

  return retVal;
}
//
// ------------------------------------------------------------------------------------------
// function: checkShow
//     Purpose: Check the state field information to see if they selected a state
//       passed to function: state field from form
//       returned from function: true/false (true is it is a good data, false is bad data)
//
//  Notes: 
function checkShow(showField) {
  var showSelected = showField.selectedIndex;
  var retVal = false;
  if (showSelected > 0 ) {
    document.getElementById("yourshow").innerHTML = "So, you like " + showField.options[showSelected].text +".";
  	retVal = true;
  } else {
	document.getElementById("yourshow").innerHTML = "You didn't select a show.";
  }

  return retVal;
}
//
// ------------------------------------------------------------------------------------------
// function: checkState
//     Purpose: Check the state field information to see if they selected a state
//       passed to function: state field from form
//       returned from function: true/false (true is it is a good data, false is bad data)
//
//  Notes: 
function checkState(stateField) {
  var stateSelected = stateField.selectedIndex;
  var retVal = false;
  if (stateSelected > 0 ) {
    document.getElementById("yourstate").innerHTML = "So, you live in " + strStates[stateSelected -1].substr(3) + "(" + strStates[stateSelected-1].substr(0,2)+")";
  	retVal = true;
  } else {
	document.getElementById("yourstate").innerHTML = "You didn't select a state.";
  }

  return retVal;
}
//
// ------------------------------------------------------------------------------------------
// function: checkTelephone
//     Purpose: Check the telephone field information to see if it is valid.
//       passed to function: telephone from form
//       returned from function: true/false (true is it is a good data, false is bad data)
//
//  Notes: 
function checkTelephone(phoneNumber) {
  var validPhoneRegExp = /^\(\d{3}\) \d{3}-\d{5}/;
  var validJustNumbersRegExp = /^\d{10}/;
  var justNumbers = phoneNumber.value.replace(/\D/g,"");
  var retVal = false;
  if(phoneNumber.value.search(validPhoneRegExp) >= 0 ) {
    document.getElementById("yourtelephone").innerHTML = "Your phone number looks good.";
	retVal = true;
  } else {
	// For now just yell.  Later let's see if we can fix it for them.
	if(justNumbers.length==10) {
	  var formattedPhonenumber = "(" + justNumbers.substr(0,3) + ") " + justNumbers.substr(3,3) + "-"+justNumbers.substr(6);
	  phoneNumber.value = formattedPhonenumber;
	  document.getElementById("yourtelephone").innerHTML = "Now your phone number looks good.";
	  retVal = true;
	} else {
	  document.getElementById("yourtelephone").innerHTML = "Your phone number should be in this format: (999) 999-9999" ;
    }
  }

  return retVal;
}
//
// ------------------------------------------------------------------------------------------
// function: checkZip
//     Purpose: Check the zip field information to see if it is valid.
//       passed to function: one of the zip field from form
//       returned from function: true/false (true is it is a good data, false is bad data)
//
//  Notes: 
function checkZip(zipField) {
  var validZipRegExp = /^\d{5}/;
  var retVal = false;
  if(zipField.value.search(validZipRegExp) >= 0 ) {
	document.getElementById("yourzipcode").innerHTML = "Your ZIP Code looks good.";  
	retVal = true;
  } else {
	document.getElementById("yourzipcode").innerHTML = "Your ZIP should be 5 digits!";
  }

  return retVal;
}
//
// ------------------------------------------------------------------------------------------
// function: displayBirthdaySelects
//     Purpose: Build the screen selections for the date fields for the Birthday section of the form
//       passed to function: nothing
//       returned from function: nothing
//
//  Notes: 
function displayBirthdaySelects() {
  var retVal = new String();
  var myDate = new Date();
  var intMonth = myDate.getMonth();
  var localDayOfMonth = myDate.getDate();
  var localFullYear = myDate.getFullYear();

  retVal = "<select name=\"birthyear\" >";
  for(whichYear in strFullYear) {
    if (strFullYear[whichYear] == localFullYear) {
      retVal += "<option value=\"" + strFullYear[whichYear] + "\" selected>"+strFullYear[whichYear]+"</option>";
    } else {
      retVal += "<option value=\"" + strFullYear[whichYear] + "\">"+strFullYear[whichYear]+"</option>";
    }
  }
  retVal += "</select>";
  // Month.
  retVal += "&nbsp;<select name=\"birthmonth\"  >";
  for(whichMonth in strMonths) {
    // Hard coding the month for now.  Come back later and have this based on the current month.
    //if(strMonths[whichMonth] == "March") {
    if(whichMonth == intMonth) {
      retVal += "<option value=\"" + strMonths[whichMonth] + "\" selected>"+strMonths[whichMonth]+"</option>";
    } else {
      retVal += "<option value=\"" + strMonths[whichMonth] + "\" >"+strMonths[whichMonth]+"</option>";
    }
  }
  retVal += "</select>";
  // Day of Month.
  retVal += "&nbsp;<select name=\"birthdayofmonth\" >";
  for(whichDayOfMonth in strDayOfMonth) {
    // Again, hard coding the day of month for now.
    //if (strDayOfMonth[whichDayOfMonth] == "20") {
    if(whichDayOfMonth == localDayOfMonth){
      retVal += "<option value=\"" +strDayOfMonth[whichDayOfMonth] + "\" selected>" + strDayOfMonth[whichDayOfMonth] + "</option>";
    } else {
      retVal += "<option value=\"" +strDayOfMonth[whichDayOfMonth] + "\" >" + strDayOfMonth[whichDayOfMonth] + "</option>";
    }
  }
  retVal += "</select>";
  
  return retVal;
  
}

//
// ------------------------------------------------------------------------------------------
// function: displayStateSelects
//     Purpose: Build the screen selections for the state select section of the form
//       passed to function: nothing
//       returned from function: nothing
//
//  Notes: 
function displayStateSelect() {
	var revVal = new String();
	retVal = "<select name=\"state\" onBlur=\"checkState(this)\">";
	retVal += "<option value=\"-1\">  </option>";
	for(whichState in strStates) {
		var stateCode = new String();
		var stateName = new String();
		stateCode = strStates[whichState].substr(0,2);
		stateName = strStates[whichState].substr(3);
		retVal += "<option value=\"" + stateCode + "\">"+stateName+"</option>";
	}
	retVal += "</select>";
	return retVal;
}
//
// ------------------------------------------------------------------------------------------
// Just a quick function to do the Mod for a number.  I don't know if I'm still using it or not.  I was going to use 
// this function for a leapyear check.
function Mod(X, Y) {
    return X - Math.floor(X / Y) * Y;
}



//
// ------------------------------------------------------------------------------------------
// End of script.


// Notes:  
// When I was looking for something about looking at the value of a passed parm it talked about:
//               location.search.substring()
//
