   // convert all characters to lowercase to simplify testing
    var agt=navigator.userAgent.toLowerCase();

    // *** BROWSER VERSION ***
    var is_major = parseInt(navigator.appVersion);

    // Note: Opera and WebTV spoof Navigator.  We do strict client detection.
    // If you want to allow spoofing, take out the tests for opera and webtv.
    var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
                && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
                && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
    var is_nav4down = (is_nav && (is_major <= 4));

function preLoad()
{
	arrImages = new Array();
	for (i=0; i<preLoad.arguments.length; i++)
	{
		arrImages[i] 		= new Image();
		arrImages[i].src	= preLoad.arguments[i];
	}
}

function help(topic)
{
	window.open("help.html"+topic,"newWindow","height=150,width=400,scrollbars=yes");
}

function chooser()
{
	window.open("template_samples.html","newWindow","height=600,width=300,scrollbars=yes");
}

function preSelect(form,type,field,dbVal)
{
	var count;
	
	if (type == "radio") {
		count = eval("form.elements['"+field+"'].length");
		if (count) {
			for (i=0; i<count; i++) {
				if (eval("form.elements['"+field+"'][i].value == dbVal")) {
					eval("form.elements['"+field+"'][i].checked = true");
					break;
				}
			}
		} else {
			eval("form.elements['"+field+"'].checked = true");
		}
	} 
	else if (type == "checkbox") 
	{
		if (eval("form."+field+".value == dbVal"))
		{
			eval("form."+field+".checked = true");
		}		
	} 
	else if (type == "select") 
	{
		if (!eval("form."+field))
		{
			count = eval("document.forms[0]."+field+".options.length");

			for (i=0; i<count; i++)
			{
				if (eval("document.forms[0]."+field+".options[i].value == dbVal"))
				{
					eval("document.forms[0]."+field+".options[i].selected = true");
					break;
				}
			}
		} 
		else 
		{
			count = eval("form."+field+".options.length");

			for (i=0; i<count; i++)
			{
				if (eval("form."+field+".options[i].value == dbVal"))
				{
					eval("form."+field+".options[i].selected = true");
					break;
				}
			}
		
		}
		
	}
}

function preSelectMultiple(form,type,field,arrVals)
{
	var count;
	
	if ((type == "radio") || (type == "checkbox"))
	{
		count = eval("form.elements['"+field+"'].length");
		
		for (x=0; x<arrVals.length; x++)
		{
			for (i=0; i<count; i++)
			{
				if (eval("form.elements['"+field+"'][i].value == arrVals[x]"))
				{
					eval("form.elements['"+field+"'][i].checked = true");
					break;
				}
			}
		}
	
	} else if (type == "select") {
		count = eval("form."+field+".options.length");
		
		for (i=0; i<count; i++)
		{
			if (eval("form."+field+".options[i].value == arrVals"))
			{
				eval("form."+field+".options[i].selected = true");
				break;
			}
		}
	}
}

function checkRequired(form,required)
{
	var errlist = "";
	var currfield;

	if (form.email) {
		var valid = checkEmailAddress(form.email);
		
		if (!valid) {
			errlist = "Please enter a valid email address\r\n";
		}
	}
	
	if (form.confirm_email) {
		if (!compareValues(form,'email','email_confirm')) {
			errlist = "Email addresses do not match\r\n";
		}
	}

	for (fieldname in required)
	{
		var fieldtype = eval("form."+fieldname+".type");
		var fieldlength = eval("form."+fieldname+".length");

		if (fieldtype == "select-one" || fieldtype == "text" || fieldtype == "checkbox" || fieldtype == "textarea" || fieldtype == "password") {
			if (eval("form."+fieldname+".value") == "") {
				errlist += required[fieldname] + " is a required field\r\n";
			}
		} else if (fieldtype == "select-multiple") { 
	                var numOptions = eval("form."+fieldname+".options.length");
        	        var lastSelected=-1;
                	for(loop=numOptions-1;loop>=0;loop--) {
	                    if(eval("form."+fieldname+".options["+loop+"].selected") ) {
        	                lastSelected = loop;
                        	break;
	                    }
        	        }
                	if ( lastSelected < 0 ) {
				errlist += required[fieldname] + " is a required field\r\n";
                	}
		} else if ((fieldlength > 0) && (eval("form."+fieldname+"[0].type") == 'radio' || eval("form."+fieldname+"[0].type") == 'checkbox')) {
                	var isChecked=-1;
	                for (loop=0;loop <fieldlength;loop++) {
        	            if (eval("form."+fieldname+"["+loop+"].checked") ) {
                	        isChecked=loop;
                        	break; // only one needs to be checked
	                    }
        	        }

                	if (isChecked < 0) {
				errlist += required[fieldname] + " is a required field\r\n";
                	}
		}
	}

	if (form.freeclass) {
		if (form.freeclass.checked != true) {
			cost = parseFloat(form.cost.value);
			disc = parseFloat(form.discount.value);
			base = cost - disc;

			if ((cost < 29) || (base < 29)) {
				errlist += "Registration Fee cannot be less than $29.00 including discount";
			}
		}
	}
	
	if (form.certify) {
		return checkCertified();
	}
	
	if (errlist) {
		alert(errlist);
		return false;		
	} else {
		disableSubmit(form);
		return true;		
	}
}

function compareValues(form,field1,field2)
{
	var object1	= eval("form."+field1);
	var object2	= eval("form."+field2);
	
	if (object1.value == object2.value)
	{
		return true;	
	} 
	else 
	{
		return false;		
	}
}

function noPhoto()
{
	if (document.profile.image.value == "")
	{
		if (confirm("You haven't chosen an image to upload\r\nAre sure you want to continue?"))
		{
			return true;
		} 
		else 
		{
			return false;
		}
	}
}

function setMax(max)
{
	document.course.max_students.value = max;
}

function checkMax(max)
{
	if (max > 0)
	{
		if (parseInt(document.course.max_students.value) > max)
		{
			alert("Max students cannot exceed bridge capacity");
			document.course.max_students.value = max;
			return false;
		}
	}
}

function in_array(needle,haystack) 
{ 
	var bool = false; 

	for (var i=0; i<haystack.length; i++) 
	{ 
		if (haystack[i]==needle) 
		{ 
			bool=true; 
		} 
	} 
	
	return bool;
}

function disableOptions(type)
{
	ownbridge = document.bridgeoptions.elements["bridge_id"];
	tcibridge = document.bridgeoptions.elements["bridge_size"];
	
	if (type == "tci")
	{
		for (i=0; i<tcibridge.length; i++)
		{
			tcibridge[i].disabled = true;
			tcibridge[i].checked = false;
		}

		if (!ownbridge)
		{
			return false;
		}
		
		if (!ownbridge.length)
		{
			ownbridge.disabled = false;
		} 
		else 
		{
			for (i=0; i<ownbridge.length; i++)
			{
				ownbridge[i].disabled = false;
			}
		}
	} 
	else if (type == "own") 
	{
		if (ownbridge)
		{
			if (!ownbridge.length)
			{
				ownbridge.disabled = true;
				ownbridge.checked = false;
			} 
			else 
			{
				for (i=0; i<ownbridge.length; i++)
				{
					ownbridge[i].disabled = true;
					ownbridge[i].checked = false;
				}
			}
		}

		for (i=0; i<tcibridge.length; i++)
		{
			tcibridge[i].disabled = false;
		}
	}
}

function checkOption()
{
	if (document.bridgeoptions.bridge_option[0].checked == true)
	{
		size = document.bridgeoptions.elements["bridge_size"];

		for (i=0; i<size.length; i++)
		{
			if (size[i].checked == true) 
			{
				return true;			
			} 
			else 
			{
				chosen = 0;
			}
		}

		if (!chosen)
		{
			alert("You must choose a rental option");
			return false;
		}
	} 
	else 
	{
		id = document.bridgeoptions.elements["bridge_id"];

		if (!id)
		{
			alert("You do not have any bridges to choose from.\r\nPlease add a bridge, or choose the \"I need to rent a bridge\" option\r\n");
			return false;
		}

		if (!id.length)
		{
			if (id.checked == true)
			{
				return true;
			} 
			else 
			{
				chosen = 0;
			}		
		} 
		else 
		{
			for (i=0; i<id.length; i++)
			{
				if (id[i].checked == true)
				{
					return true;			
				} 
				else 
				{
					chosen = 0;
				}
			}
		}

		if (!chosen)
		{
			alert("You must choose a bridge from your list");
			return false;
		
		}
	}
}

function cancelEventCreation(cancel) {
	if (cancel == "1") {
		document.location = "cancel_course_creation.html?action=cancel";	
	} else {
		document.location = "index.html";
	}
}

function disableSubmit(form)
{
	try {
		form.submit.disabled = true;
	} catch (err) {
	}
}

function checkStrLen(strLen,limit)
{
	if (strLen > limit)
	{
		alert("You product description is too long\r\nPlease limit it to no more than 550 characters (including spaces)");
		return false;
	}
}

function checkSaved(form,arrDates)
{
	var boxes = document.calendar.elements["days[]"];

	for (i=0; i<boxes.length; i++)
	{
		if ((boxes[i].checked == true) && !in_array(boxes[i].value,arrDates))
		{
			unsaved = 1;
			break;		
		} 
		else 
		{
			unsaved = 0;
		}
	}

	if (unsaved)
	{
		alert("Please save your chosen dates before proceeding to another month\rTo save dates, click the STEP 2: Save Selected Dates button");
		return false;
	}
}

function checkCertified()
{
	if (document.payment.certify.checked != true)
	{
		alert("You must agree to the terms of the TeleLeader Agreement to proceed");
		document.payment.certify.checked = true;
		return false;
	} 
	else 
	{
		return true;		
	}
}

function requireCheckboxes()
{
	var fields = document.schedule.elements["times[]"];

	for (i=0; i<fields.length; i++)
	{
		if (fields[i].checked == true)
		{
			return true;
		}
	}

	alert("You must select at least one time to book");
	return false;
}

function disableFields(optIndex)
{
	if (document.conversion.format.options[optIndex].text.indexOf('CD') > -1)
	{
		document.conversion.cd_option.disabled = false;
	} 
	else 
	{
		document.conversion.cd_option.disabled = true;
	}
}

function fieldEnabler(condition, target)
{
	if (condition)
	{
		target.disabled = false;
	}
	else
	{
		target.disabled = true;
	}
}

function checkForPayPal(emailAddr)
{
	if (!emailAddr)
	{
		alert("You must provide a PayPal email address in your profile to use TEEM");
		document.location = "/leader/edit_profile.html";
	}
	else 
	{
		return true;
	}
}



function launchMailClient(userEmail) {
	var checks = document.students.elements["recipients[]"];

	var bcc = "";
	if (checks.length > 0) {
		for (i=0; i<checks.length; i++) {
			if (checks[i].checked) {
				if (bcc != "") {
					bcc += "," + checks[i].value;
				} else {
					bcc = checks[i].value;
				}
			}
		}
	} else {
		if (checks.checked) {
			bcc = checks.value;
		}
	}

	if (bcc == "") {
		alert("No participants selected.");
		return false;
	}

	var newURL = 'mailto:' + userEmail + '?bcc=' + bcc;
	mailWin = window.open(newURL,"newWin");
}

function toggleAll()
{
	var checks = document.students.elements["recipients[]"];

	if (checks.length > 0) {
		if (checks[0].checked == true) {
			boxstatus = "on";		
		} else {
			boxstatus = "off";
		}
		for (i=0; i<checks.length; i++) {
			if (boxstatus == "off") {
				checks[i].checked = true;		
			} else {
				checks[i].checked = false;
			}
		}
	} else {
		if (checks.checked == true) {
			boxstatus = "on";		
			checks.checked = false;
		} else {
			boxstatus = "off";
			checks.checked = true;
		}
	}
}

function popUpHelp(topic)
{
	// get objects
	help 	= document.getElementById('help_container');
	frm 	= document.getElementById('help_frame');
	
	// set attributes
	frm.src = "/help.html?topic=" + topic;
	help.style.visibility = "visible";
}

function closeHelp()
{
	help = document.getElementById('help_container');
	
	help.style.visibility = "hidden";
}

function buildMonthList(form)
{
	selectedMo 	= form.ccmo.options[form.ccmo.selectedIndex].value;
	form.ccmo.options.length = 0;
	currDate 	= new Date();
	currMonth 	= currDate.getMonth() + 1;
	currYear	= currDate.getFullYear();
	
	if (currYear == form.ccyr.options[form.ccyr.selectedIndex].value)
	{
		startMonth = currMonth;
	}
	else
	{
		startMonth = 1;
	}
	
	count = 0;
	for (i=startMonth; i<=12; i++)
	{
		form.ccmo.options[count] = new Option(i,i);
		count++;	
	}
	
	for (x=0; x<form.ccmo.options.length; x++)
	{
		if (form.ccmo.options[x].value == selectedMo)
		{
			form.ccmo.options[x].selected = true;
		}
	}
}

function buildYearList(form)
{
	form.ccyr.options.length = 0;	
	currDate 	= new Date();
	currYear	= currDate.getFullYear();
	
	for (i=0; i<10; i++)
	{
		form.ccyr.options[i] = new Option(currYear,currYear);
		currYear++;	
	}
}

function jumpTo(catIndex)
{
	category = document.categories.category[catIndex].value;
	
	if (category == "all")
	{
		document.location = "product_catalog.html";
		
	} else {
		document.location = "product_catalog.html?category="+category;
		
	}
}

function checkEmailAddress(field) 
{
	// Note: The next expression must be all on one line...
	//       allow no spaces, linefeeds, or carriage returns!
	var goodEmail = field.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
	
	if (goodEmail)
	{
		return true;
   
	} else {
		return false;
   
	}
}

    // fieldname, warningname, remainingname, maxchars
    function CheckFieldLength(fn,wn,rn,mc) {
      var len = fn.value.length;
      if (len > mc) {
        fn.value = fn.value.substring(0,mc);
        len = mc;
      }
      document.getElementById(wn).innerHTML = len;
      document.getElementById(rn).innerHTML = mc - len;
    }

    function dateCheckLength(objDate) {
      if (objDate.value.length != 10) {
        objDate.value = "";
      }
    }

    //Generic function that reads a dropdown box for its' value.
    function textSelect(selectObj)
    {
      var selectValue;
      if (is_nav4down) {
         selectValue = selectObj[selectObj.selectedIndex].text;
      } else {
         selectValue = selectObj.value;
      }

      return selectValue;
    }
