function IIf(lTest, trueValue, falseValue) {
	if (lTest) {
		return trueValue
	} else {
		return falseValue
	}
}

function SelectValue(oSelect) {
	return oSelect.options[oSelect.selectedIndex].value
}

function SelectText(oSelect) {
	return oSelect.options[oSelect.selectedIndex].text
}

function BoyGirl(oSelect) {
	return (oSelect.options[oSelect.selectedIndex].value == "M") ? "boy" : "girl"
}

function HeShe(oSelect) {
	return (oSelect.options[oSelect.selectedIndex].value == "M") ? "he" : "she"
}

function HimHer(oSelect) {
	return (oSelect.options[oSelect.selectedIndex].value == "M") ? "him" : "her"
}

function HisHer(oSelect) {
	return (oSelect.options[oSelect.selectedIndex].value == "M") ? "his" : "her"
}

function ManLady(oSelect) {
	return (oSelect.options[oSelect.selectedIndex].value == "M") ? "man" : "lady"
}

function ManWoman(oSelect) {
	return (oSelect.options[oSelect.selectedIndex].value == "M") ? "man" : "lady"
}

function MotherFather(oSelect) {
	return (oSelect.options[oSelect.selectedIndex].value == "M") ? "father" : "mother"
}

function DadMum(oSelect) {
	return (oSelect.options[oSelect.selectedIndex].value == "M") ? "dad" : "mum"
}

function BrotherSister(oSelect) {
	return (oSelect.options[oSelect.selectedIndex].value == "M") ? "brother" : "sister"
}

function InitVowel(string) {
	return ("AEIOUH".indexOf(string.substr(0, 1).toUpperCase()) > -1)
}

function CapIt(string) {
   return string.charAt(0).toUpperCase() + string.substring(1, string.length)
}

function AllTrim(string) {
   return string
}

function ParseList(string, delimiter) {
   var array = string.split(delimiter)
   var returnVal = AllTrim(array[0])
   for (i = 1; i < array.length - 1; i++) {
      returnVal += ", "
      returnVal += AllTrim(array[i])
   }
   return returnVal + " and " + AllTrim(array[array.length - 1])
}

function MakeArray(n) {
   this.length = n
   return this
}

monthNames = new MakeArray(12)
monthNames[1] = "January"
monthNames[2] = "February"
monthNames[3] = "March"
monthNames[4] = "April"
monthNames[5] = "May"
monthNames[6] = "June"
monthNames[7] = "July"
monthNames[8] = "August"
monthNames[9] = "September"
monthNames[10] = "October"
monthNames[11] = "November"
monthNames[12] = "December"

dayNames = new MakeArray(7)
dayNames[1] = "Sunday"
dayNames[2] = "Monday"
dayNames[3] = "Tuesday"
dayNames[4] = "Wednesday"
dayNames[5] = "Thursday"
dayNames[6] = "Friday"
dayNames[7] = "Saturday"

function customDateString(oDate) {
   var theDay = dayNames[oDate.getDay() + 1]
   var theMonth = monthNames[oDate.getMonth() + 1]
   var theYear = oDate.getYear() + 1900
   return theDay + ", " + theMonth + " " + oDate.getDate() + ", " + theYear
}

function Single(string) {
	return (string == "1" || string.toUpperCase() == "ONE") ? true : false
}

function Plural(string) {
   if (string.toUpperCase() == "MOOSE" || string.toUpperCase() == "FISH" || string.toUpperCase() == "DEER" || string.toUpperCase() == "YOU" || string.toUpperCase() == "SHEEP") {
      return string
   }
   if (string.toUpperCase() == "GOOSE") {
      return "geese"
   }
   if (string.toUpperCase() == "MOUSE") {
      return "mice"
   }
   if (string.toUpperCase() == "KNIFE") {
      return "knives"
   }
   if (string.toUpperCase() == "WIFE") {
      return "wives"
   }
   if (string.toUpperCase() == "LIFE") {
      return "lives"
   }

   var cBase = string.substring(0, string.length - 1)
   var cSecLast = string.charAt(string.length - 2)
   var cLast = string.charAt(string.length - 1)
   if (cLast == "s" || cLast == "x") {
      return string + "es"
   }
   if (cLast == "y" && "AEIOUaeiou".indexOf(cSecLast) < 0) {
      return cBase + "ies"
   }
   return string + "s"
}

function OrdinalFor(value) {
   var hundredRemainder = value % 100;
   var tenRemainder = value % 10;
   if (hundredRemainder - tenRemainder == 10) {
      return "th";
   }
 
   switch (tenRemainder) {
      case 1:
         return "st";
      case 2:
         return "nd";
      case 3:
         return "rd";
      default:
         return "th";
   }
}

function BuildStory(form) {
	var oWindow = window.open("","StoryBuilder")
	if (oWindow != null) {
		var cContent = '<HTML><HEAD><TITLE>' + document.title + '</TITLE></HEAD>\n'
		cContent += '<body bgcolor="#FFFFFF" onload="document.title = ' + document.title + ';">\n'
		cContent += '<table width="800" align="center">\n'
		cContent += '<tr align="center"><td><p><script language="JavaScript" type="text/javascript" src="/Ads/Flex.js"></script></p></td></tr>\n'
		cContent += '<tr align="center"><td><p><font size="6" color="#0000CC">' + document.title + '</font></p></td></tr>\n'
		cContent += '<tr><td><font size="4">\n\n'

		cContent += cStory(form)

		cContent += '\n\n</font></td><tr>\n'
		cContent += '<tr align="center"><td><p>~The End~</p></td></tr>\n'
		cContent += '<tr align="center"><td><p><font size="2"><i>www.SquiglysPlayhouse.com</i></font></p></td></tr>\n'
		cContent += '<tr align="right"><td><p><font size="2"><i><a href="javascript: self.close()">Close Window</a></i></font></p></td></tr>\n'
		cContent += '</table>\n'
		cContent += '</body></HTML>\n'
		oWindow.document.write(cContent)
		oWindow.document.close()
		oWindow.focus()
	} else {
		alert("Could not create story window.")
	}
}

/*
 * Title Caps
 * 
 * Ported to JavaScript By John Resig - http://ejohn.org/ - 21 May 2008
 * Original by John Gruber - http://daringfireball.net/ - 10 May 2008
 * License: http://www.opensource.org/licenses/mit-license.php
 */

(function(){
	var small = "(a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|v[.]?|via|vs[.]?)";
	var punct = "([!\"#$%&'()*+,./:;<=>?@[\\\\\\]^_`{|}~-]*)";
  
	this.titleCaps = function(title){
		var parts = [], split = /[:.;?!] |(?: |^)["Ò]/g, index = 0;
		
		while (true) {
			var m = split.exec(title);

			parts.push( title.substring(index, m ? m.index : title.length)
				.replace(/\b([A-Za-z][a-z.'Õ]*)\b/g, function(all){
					return /[A-Za-z]\.[A-Za-z]/.test(all) ? all : upper(all);
				})
				.replace(RegExp("\\b" + small + "\\b", "ig"), lower)
				.replace(RegExp("^" + punct + small + "\\b", "ig"), function(all, punct, word){
					return punct + upper(word);
				})
				.replace(RegExp("\\b" + small + punct + "$", "ig"), upper));
			
			index = split.lastIndex;
			
			if ( m ) parts.push( m[0] );
			else break;
		}
		
		return parts.join("").replace(/ V(s?)\. /ig, " v$1. ")
			.replace(/(['Õ])S\b/ig, "$1s")
			.replace(/\b(AT&T|Q&A)\b/ig, function(all){
				return all.toUpperCase();
			});
	};
    
	function lower(word){
		return word.toLowerCase();
	}
    
	function upper(word){
	  return word.substr(0,1).toUpperCase() + word.substr(1);
	}
})();
