function doOnLoadFunctions() {
	setPromoDates();
	setTimeout("setADDSelects()",500);
	}

function calulateImpressions(element) {
	var thisObj = e(element);
	var ImpressionCount = 0;

	for( len = 0; len < thisObj.options.length; len++ ) {
		try {
			ImpressionCount += thisObj.options[len].text.match(/\((\d+)\)/)[1] * 1;
			}
		catch(e) {}
		}
	//alert(ImpressionCount);
	return ImpressionCount;
	}
function setImpressionsContent() {	
	var numFormat = new NumberFormat();
	numFormat.setPlaces(0);
	numFormat.setSeparators(true);
	
	numFormat.setNumber(calulateImpressions('listSelected'));
	var selectedLocations = numFormat.toFormatted();
	
	numFormat.setNumber(calulateImpressions('listAvailable'));
	var availableLocations = numFormat.toFormatted();
	
	//e('ImpressionsContent').innerHTML = 'There are ' + availableLocations + ' available impressions. You\'ve selected ' + selectedLocations + ' of them.';
	e('ImpressionsContent').innerHTML = selectedLocations;
	
	var availableObj = e('listAvailable');
	var selectedObj = e('listSelected');
	
	///This sizes the SELECT element to exactly how many options there are. 
	selectedObj.setAttribute("size",selectedObj.options.length);
	availableObj.setAttribute("size",availableObj.options.length);

	///These next two lines help reset the display of the select element in certain browsers
	availableObj.style.display='none';
	availableObj.style.display='block';
	selectedObj.style.display='none';
	selectedObj.style.display='block';
	}
	
function getCities() {
	var State = e('StateCode').value;
	e('CityDiv').style.display='block';
	DWRUtil.removeAllOptions("CitySelect");
	addOption('CitySelect','Loading Cities...',0);
	DWREngine._execute(_cfscriptRinktime, null, 'createCitySelect', State, handleCityResult);
	}
function handleCityResult(result) {
	var Success = -1;
	var Error = -1;
	var Data = -1;
	
	for ( var i=0, len=result.length; i<len; ++i ){
		matchValue = new RegExp("SUCCESS", "i");
		isMatch = result[i].KEY.match(matchValue);
		if (isMatch) {
			Success = i;
			continue;
			}
		matchValue = new RegExp("ERROR", "i");
		isMatch = result[i].KEY.match(matchValue);
		if (isMatch) {
			Error = i;
			continue;
			}
		matchValue = new RegExp("DATAARRAY", "i");
		isMatch = result[i].KEY.match(matchValue);
		if (isMatch) {
			Data = i;
			continue;
			}
		} 

	if (result[Success].VALUE == 1) {
		e('CityDiv').style.display='block';
		DWRUtil.removeAllOptions("CitySelect");
		addOption('CitySelect','- Select a City -',0);
		for ( var i=0, len=result[Data].VALUE.length; i<len; ++i ){
			<!--- alert(result[Data].VALUE[i]); --->
			var tmp = result[Data].VALUE[i].split("|");
			var PostalCode = tmp[0];
			var CityName = tmp[1];
			addOption('CitySelect',CityName,PostalCode);
			}
		}
	else {
		DWRUtil.removeAllOptions("CitySelect");
		addOption('CitySelect','Could not load city data.',0);
		DWRUtil.removeAllOptions("CitySelect");
		}
	}
function previewDescription() {
	var editor = FCKeditorAPI.GetInstance( "EventDescription" );
	sValue = editor.GetHTML();
	e('EventDescriptionPreview').value = sValue;
	e('form_EventDescriptionPreview').submit();
	}
function setPotalCode() {
	e('LookupPostalCode').value = e('CitySelect').value;
	doPostalLookup(0);
	}
function setPromoDates() {
	if (e('PromoEndDate').value=='' && e('EventStartDate').value != '') {
		e('PromoEndDate').value = e('EventStartDate').value;
		}
	if (e('PromoStartDate').value != '') {
		e('PromoStateDateTxt').innerHTML = e('PromoStartDate').value;
		}
	if (e('PromoEndDate').value != '') {
		e('PromoEndDateTxt').innerHTML = e('PromoEndDate').value;
		}
	setTimeout('setPromoDates()',1000)
	}
function disableAll(formName) {
	var thisForm = document.getElementById(formName);
	for(i=0; i<thisForm.elements.length; i++)	{
		thisForm.elements[i].disabled=true;
		}
	}
function enableAll(formName) {
	var thisForm = document.getElementById(formName);
	for(i=0; i<thisForm.elements.length; i++)	{
		thisForm.elements[i].disabled=false;
		}
	}
function userBoxSwitch(to) {
	document.getElementById('ExistingUser').style.display='none';
	document.getElementById('NewUser').style.display='none';
	document.getElementById('Login').style.display='none';
	document.getElementById(to).style.display='block';
	}
function initAjax()
	{
		DWRUtil.useLoadingMessage();
		DWREngine._errorHandler =  errorHandler;
	}
function doLogin(thisEmail,thisPassword) {
	DWREngine._execute(_cfscriptRinktime, null, 'ResourceLogin', thisEmail, thisPassword, handleResult);
	}
function doSignup() {
	var FirstName = e('FirstName');
	var LastName = e('LastName');
	var Email = e('Email');
	var Password1 = e('Password1');
	var Password2 = e('Password2');
	var MailingList = e('MailingList');

	DWREngine._execute(_cfscriptRinktime, null, 'NewResource', FirstName.value, LastName.value, Email.value, Password1.value, Password2.value, MailingList.value, handleSignupResult);	
	}
function addOption(elementName,displayName,value) {
	var opt = new Option(displayName,value);
	var ele = e(elementName);
	ele.options[ele.options.length] = opt;
	} 
function doPostalLookup(event) {
	if (event != 0 & event.type != 'click') var unicode=event.charCode? event.charCode : event.keyCode;
	else unicode = 48; //just a default value to allow execution of the IF statement below
	
	//Remove whitespace
	e('LookupPostalCode').value = e('LookupPostalCode').value.replace(/^\s*(\S*(\s+\S+)*)\s*$/, '$1');
	var PostalCode = e('LookupPostalCode').value;
	
	// IF PostalCode is either 5 or 6 chars AND keypress event is a-z, A-Z, or 0-9, then execute
	if ((PostalCode.length == 5 || PostalCode.length == 6) && (
			(unicode >= 48 && unicode <= 57) || //0-9
			(unicode >= 65 && unicode <= 90) || //A-Z
			(unicode >= 97 && unicode <= 122) //a-z
			)
		) {
		<!--- alert(PostalCode); --->
		e('SearchLink').innerHTML = '<span style="color: red;font-weight: bold;">Searching...</span>';
		DWRUtil.removeAllOptions("listAvailable");
		addOption('listAvailable','Loading locations for ' + PostalCode + '...','');
		DWREngine._execute(_cfscriptRinktime, null, 'createLocationSelect', PostalCode, 25, handleLocationSelectResult);
		}
	else e('InvalidPostalCode').style.display='none';
	return false;
	}
function handleSignupResult(result) {	
	var Success = -1;
	var Error = -1;
	var ResourceID = -1
	for ( var i=0, len=result.length; i<len; ++i ){
		matchValue = new RegExp("SUCCESS", "i");
		isMatch = result[i].KEY.match(matchValue);
		if (isMatch) {
			Success = i;
			continue;
			}
		matchValue = new RegExp("ERROR", "i");
		isMatch = result[i].KEY.match(matchValue);
		if (isMatch) {
			Error = i;
			continue;
			}
		matchValue = new RegExp("RESOURCEID", "i");
		isMatch = result[i].KEY.match(matchValue);
		if (isMatch) {
			ResourceID = i;
			continue;
			}
		} 

	if (result[Success].VALUE == 1) {
		<!--- alert(result[ResourceID].VALUE); --->
		doLogin(e('Email').value,e('Password1').value);
		}
	else {
		alert(result[Error].VALUE);
		}
	}
function handleResult(result) {	
	var Success = -1;
	var Error = -1;
	var ResourceEmail = -1;
	var RedirectURL = -1;
	for ( var i=0, len=result.length; i<len; ++i ){
		matchValue = new RegExp("SUCCESS", "i");
		isMatch = result[i].KEY.match(matchValue);
		if (isMatch) {
			Success = i;
			continue;
			}
		matchValue = new RegExp("ERROR", "i");
		isMatch = result[i].KEY.match(matchValue);
		if (isMatch) {
			Error = i;
			continue;
			}
		matchValue = new RegExp("RESOURCEEMAIL", "i");
		isMatch = result[i].KEY.match(matchValue);
		if (isMatch) {
			ResourceEmail = i;
			continue;
			}
		matchValue = new RegExp("REDIRECTURL", "i");
		isMatch = result[i].KEY.match(matchValue);
		if (isMatch) {
			RedirectURL = i;
			continue;
			}
		} 

	if (result[Success].VALUE == 1) {
		//e('ResourceEmail').innerHTML = ', ' + result[ResourceEmail].VALUE;
		if (RedirectURL != -1) {
			window.location = result[RedirectURL].VALUE;
			}
		else {
			userBoxSwitch('ExistingUser');
			}
		
		//enableAll('form_NewEvent');
		}
	else {
		<!--- alert('No dice. ' + result[Error].VALUE); --->
		e('LoginFailed').innerHTML = '<span class="warning">' + result[Error].VALUE + '</span>';
		e('LoginFailed').style.display = 'block';
		}
	}
function handleLocationSelectResult(result) {
	var Success = -1;
	var Error = -1;
	var Data = -1;
	for ( var i=0, len=result.length; i<len; ++i ){
		matchValue = new RegExp("SUCCESS", "i");
		isMatch = result[i].KEY.match(matchValue);
		if (isMatch) {
			Success = i;
			continue;
			}
		matchValue = new RegExp("ERROR", "i");
		isMatch = result[i].KEY.match(matchValue);
		if (isMatch) {
			Error = i;
			continue;
			}
		matchValue = new RegExp("DATAARRAY", "i");
		isMatch = result[i].KEY.match(matchValue);
		if (isMatch) {
			Data = i;
			continue;
			}
		} 

	if (result[Success].VALUE == 1) {
		e('InvalidPostalCode').style.display='none';
		e('SearchLink').innerHTML = '<a href="##" onClick="doPostalLookup(event);return void(0);">search</a>';
		DWRUtil.removeAllOptions("listAvailable");
		for ( var i=0, len=result[Data].VALUE.length; i<len; ++i ){
			<!--- alert(result[Data].VALUE[i]); --->
			var tmp = result[Data].VALUE[i].split("|");
			var LocationID = tmp[0];
			var LocationName = tmp[1];
			addOption('listAvailable',LocationName,LocationID);
			}
		setImpressionsContent();
		}
	else {
		e('InvalidPostalCode').style.display='block';
		DWRUtil.removeAllOptions("listAvailable");
		}
	}

function isEnterKey(e){
	var unicode=e.charCode? e.charCode : e.keyCode
	if (unicode == 13) { 
		return true;
		}
	else return false;
	}
function applyPromoCode() {
	var PromoCode = e('PromoCode').value;
	var TotalCost = e('TotalCostOrig').innerHTML;

	DWREngine._execute(_cfscriptRinktime, null, 'getPromoDiscount', TotalCost, PromoCode, handlePromoCodeResult);
	}
function handlePromoCodeResult(result) {
	var Success = -1;
	var Error = -1;
	var DiscountApplied = -1;
	var DiscountedTotal = -1;
	
	for ( var i=0, len=result.length; i<len; ++i ){
		matchValue = new RegExp("SUCCESS", "i");
		isMatch = result[i].KEY.match(matchValue);
		if (isMatch) {
			Success = i;
			continue;
			}
		matchValue = new RegExp("ERROR", "i");
		isMatch = result[i].KEY.match(matchValue);
		if (isMatch) {
			Error = i;
			continue;
			}
		matchValue = new RegExp("DISCOUNTAPPLIED", "i");
		isMatch = result[i].KEY.match(matchValue);
		if (isMatch) {
			DiscountApplied = i;
			continue;
			}
		matchValue = new RegExp("DISCOUNTEDTOTAL", "i");
		isMatch = result[i].KEY.match(matchValue);
		if (isMatch) {
			DiscountedTotal = i;
			continue;
			}
		} 
		
	if (result[Success].VALUE == 1) {
		e('DiscountRow').style.display='block';
		e('InvalidPromoCode').style.display='none';
		e('DiscountAmount').innerHTML = formatNumber(result[DiscountApplied].VALUE);
		e('TotalCost').innerHTML = formatNumber(result[DiscountedTotal].VALUE);		
		if (result[DiscountedTotal].VALUE == 0) {
			e('NoCardInfoNeeded').style.display='block';
			e('AdjustmentRow').style.display='none';
			}
		}
	else {
		e('InvalidPromoCode').style.display='block';
		e('DiscountRow').style.display='none';
		e('AdjustmentRow').style.display='block';
		e('NoCardInfoNeeded').style.display='none';
		e('DiscountAmount').innerHTML = '$0.00';
		e('TotalCost').innerHTML = formatNumber(e('TotalCostOrig').innerHTML);		
		}
	}
	
function getActivitiesByDescription(DescriptionCode,MultiSelect) {
	DWRUtil.removeAllOptions("ActivitySelect");
	try	{
		DWRUtil.removeAllOptions("DivisionSelect");
		e('DivisionSelectDiv').style.display = 'none';
		e('ActivitySelectDiv').style.display = 'block';
		}
	catch(err) {
		
		}
	addOption('ActivitySelect','Loading Activities...','');
	DWREngine._execute(_cfscriptRinktime, null, 'getActivityByDescription', DescriptionCode, 'true', handleActivityResult);
	}
function handleActivityResult(result) {
	var Success = -1;
	var Error = -1;
	var Data = -1;
	
	for ( var i=0, len=result.length; i<len; ++i ){
		matchValue = new RegExp("SUCCESS", "i");
		isMatch = result[i].KEY.match(matchValue);
		if (isMatch) {
			Success = i;
			continue;
			}
		matchValue = new RegExp("ERROR", "i");
		isMatch = result[i].KEY.match(matchValue);
		if (isMatch) {
			Error = i;
			continue;
			}
		matchValue = new RegExp("DATAARRAY", "i");
		isMatch = result[i].KEY.match(matchValue);
		if (isMatch) {
			Data = i;
			continue;
			}
		}
	if (result[Success].VALUE == 1) {
		DWRUtil.removeAllOptions("ActivitySelect");
		if (result[Data].VALUE.length == 0) addOption('ActivitySelect','No Available Activities',0);
		if (result[Data].VALUE.length > 1 && !e('ActivitySelect').multiple) addOption('ActivitySelect','- Select an Activity -','-1');
		for ( var i=0, len=result[Data].VALUE.length; i<len; ++i ){
			<!--- alert(result[Data].VALUE[i]); --->
			var tmp = result[Data].VALUE[i].split("|");
			var ActivityID = tmp[0];
			var ActivityName = tmp[1];
			addOption('ActivitySelect',ActivityName,ActivityID);
			}
		e('ActivitySelectDiv').style.display = 'block';
		}
	else {
		DWRUtil.removeAllOptions("ActivitySelect");
		/* addOption('ActivitySelect','Could not load activity data.',0); */
		e('ActivitySelectDiv').style.display = 'none';
		}
	}
function getDivisionByActivity(ActivityCode) {
	DWRUtil.removeAllOptions("DivisionSelect");
	e('DivisionSelectDiv').style.display = 'block';
	addOption('DivisionSelect','Loading Divisons...','');
	DWREngine._execute(_cfscriptRinktime, null, 'getDivisionByActivity', ActivityCode, handleDivisionResult);
	}
function handleDivisionResult(result) {
	var Success = -1;
	var Error = -1;
	var Data = -1;
	
	for ( var i=0, len=result.length; i<len; ++i ){
		matchValue = new RegExp("SUCCESS", "i");
		isMatch = result[i].KEY.match(matchValue);
		if (isMatch) {
			Success = i;
			continue;
			}
		matchValue = new RegExp("ERROR", "i");
		isMatch = result[i].KEY.match(matchValue);
		if (isMatch) {
			Error = i;
			continue;
			}
		matchValue = new RegExp("DATAARRAY", "i");
		isMatch = result[i].KEY.match(matchValue);
		if (isMatch) {
			Data = i;
			continue;
			}
		}
	if (result[Success].VALUE == 1) {
		DWRUtil.removeAllOptions("DivisionSelect");
		if (result[Data].VALUE.length == 0) e('DivisionSelectDiv').style.display = 'none';
		if (result[Data].VALUE.length > 1 && !e('DivisionSelect').multiple) addOption('DivisionSelect','- Select a Division -','-1');
		for ( var i=0, len=result[Data].VALUE.length; i<len; ++i ){
			<!--- alert(result[Data].VALUE[i]); --->
			var tmp = result[Data].VALUE[i].split("|");
			var DivisionID = tmp[0];
			var DivisionName = tmp[1];
			addOption('DivisionSelect',DivisionName,DivisionID);
			}
		if (result[Data].VALUE.length == 0) e('DivisionSelectDiv').style.display = 'none';
		else e('DivisionSelectDiv').style.display = 'block';
		}
	else {
		DWRUtil.removeAllOptions("DivisionSelect");
		/* addOption('ActivitySelect','Could not load activity data.',0); */
		e('DivisionSelectDiv').style.display = 'none';
		}
	}
	
function switchViews(to) {
	if (to == 'toAdmin') DWRUtil.removeAllOptions("listSelected");
	if (to == 'toSearch') uncheckRadio('EventLocations');
	Effect.toggle('LocationSearchDiv','blind');
	Effect.toggle('PreselectedLocationsDiv','blind');
	return false;
	}

function uncheckRadio(which) {
	var choice = document.getElementsByName(which);
	for (i = 0; i < choice.length; i++) {
		choice[i].checked = false; 
		}
	}
