function submitEventForm() {
	$('endDate1').disabled = false;
	$('endDate1Hours').disabled = false;
	$('endDate1Minutes').disabled = false;
	$('endDate1AMPM').disabled = false;
	selectAll($('list2'));
	return true;
}

// select all items in a multiple select list
function selectAll(list) {
    for(var i = 0; i < list.length; i++) {
        list[i].selected = true;
    }
}

// count characters, check for max
function countChars(field, max)
{
  if (field) {
      if (field.value.length > max){
          field.value = field.value.substr(0,max);
      }
    var name = field.name + "Count";
    if ($(name)) {
      $(name).innerHTML = field.value.length;
    }
  }
}

function removeEventPd(box) {
    var i;
    var notSelectedArray = new Array();
    var notSelectedNum;

    // find all of the categories that aren't scheduled for removal
    for(i=0, notSelectedNum = 0; i<box.options.length; i++){
        if( ( ! box.options[i].selected) && (box.options[i].value != "") ){
            notSelectedArray[notSelectedNum] = box[i];
            notSelectedNum++;
        }
    }

    // copy the remaining categories back
    box.length = 0;
    for(i=0; i<notSelectedNum; i++){
        box[i] = notSelectedArray[i];
    }
}

function copyEvent(fbox, tbox)
{
    var arrFbox = new Array();
    var arrTbox = new Array();
    var arrLookup = new Array();
    var i;
    var isDup = false;
    var fLength = 0;
    var tLength = 0;

    for(i=0; i<tbox.options.length; i++)
    {
        arrLookup[tbox.options[i].text] = tbox.options[i].value;
        arrTbox[i] = tbox.options[i].text;
    }

    tLength = arrTbox.length; // number of categories already in event

    for(i=0; i<fbox.options.length; i++)
    {
        arrLookup[fbox.options[i].text] = fbox.options[i].value;

        if(fbox.options[i].selected && fbox.options[i].value != "")
        {
            // check for duplicates
            var j;
            var text = fbox.options[i].text;
            for(j=0; j<tbox.length; j++)
            {
                if (text == tbox.options[j].text)
                {
                    isDup = true;
                }

                // check for trimmed version as well
                text = text.replace(/^\s*|\s*$/g, "");

                if (text == tbox.options[j].text)
                {
                    isDup = true;
                }
            }

            // only add the category if it does not already exist
            if( ! isDup )
            {
                arrTbox[tLength] = fbox.options[i].text;
                tLength++;
            }
            // already had the chance to copy event, reset isDup
            isDup = false;
        }
        else
        {
              arrFbox[fLength] = fbox.options[i].text;
              fLength++;
        }
    }

     // move selected items into Categories for Event
     tbox.length = 0;
     var c;
     for(c=0; c<arrTbox.length; c++)
     {
         // trim lead and end whitespace
         var category = arrTbox[c];
         category = category.replace(/^\s*|\s*$/g, "");

     	 var no = new Option();
     	 no.value = arrLookup[arrTbox[c]];
     	 no.text = category;
     	 tbox[c] = no;
     }
}

function selectAll(list) {
	for(var i = 0; i < list.length; i++) {
    	list[i].selected = true;
	}
}


var changeAttribPars='';
function buildParString(par) {
	changeAttribPars = changeAttribPars + "&" + par.key + "=" + par.value;
}

function getValueByKey(key) {
	for ( var i=100;i<200;i++) {
		if (document.submitEvent[key+i]) {
			return document.submitEvent[key+i].value;
			break;
		}
	}
}

function getAgeValues() {
	var ageVal='';
	var ageInputs = $$('#ages input');
	ageInputs.each( function(item) { if (item.checked) { ageVal = ageVal + '&' + item.name + '=' + item.value; } } );
	return ageVal;
}

function buildParMapFromInputs() {
	var newMap = {};
	newMap['parking100'] = newMap['parking120'] = newMap['parking140'] = newMap['parking160'] = newMap['parking180'] = getValueByKey('parking');
	newMap['family101'] = newMap['family121'] = newMap['family141'] = newMap['family161'] = newMap['family181'] = getValueByKey('family');
	newMap['ages102'] = newMap['ages122'] = newMap['ages142'] = newMap['ages162'] = newMap['ages182'] = getAgeValues();
	newMap['directions103'] = newMap['directions123'] = newMap['directions143'] = newMap['directions163'] = newMap['directions183'] = getValueByKey('directions');
	newMap['registration105'] = newMap['registration125'] = newMap['registration145'] = newMap['registration165'] = newMap['registration185'] = getValueByKey('registration');
	newMap['sponsors106'] = newMap['sponsors126'] = newMap['sponsors146'] = newMap['sponsors166'] = newMap['sponsors186'] = getValueByKey('sponsors');
	newMap['neighborhood107'] = newMap['neighborhood127'] = newMap['neighborhood147'] = newMap['neighborhood167'] = newMap['neighborhood187'] = getValueByKey('neighborhood');

	return newMap;
}

// switch out which type attribs are shown
function changeAttribs(typeId,parMap) {
	if ( !parMap ) {
		parMap = buildParMapFromInputs();
	}
	var url = '/stdevents/includes/submit_type_attribs.jsp';
	changeAttribPars = 'typeId=' + typeId;

	// passes through parameters so the fields hold their values
	$H(parMap).each( buildParString );
	var ageString = getAgeValues();
	changeAttribPars = changeAttribPars.replace(/ages1[0-9]2=.*?&/g,"");
	changeAttribPars = changeAttribPars + ageString.replace(/ages1[0-9]2/g,"ages102");
	changeAttribPars = changeAttribPars + ageString.replace(/ages1[0-9]2/g,"ages122");
	changeAttribPars = changeAttribPars + ageString.replace(/ages1[0-9]2/g,"ages142");
	changeAttribPars = changeAttribPars + ageString.replace(/ages1[0-9]2/g,"ages162");
	changeAttribPars = changeAttribPars + ageString.replace(/ages1[0-9]2/g,"ages182");
	changeAttribPars = changeAttribPars + '&ages102Submit=true&ages122Submit=true&ages142Submit=true&ages162Submit=true&ages182Submit=true';

	var venueCall = new Ajax.Updater(
		{success:'eventTypeAttribs'},
		url,
		{ method: 'get', parameters: changeAttribPars, onFailure: attribError } );

}

function attribError() {
	$('eventTypeAttribs').innerHTML = 'Failed to get type attributes, please try again.';
}

// check for an enter keypress
function checkEnter(e){
	var characterCode;

	if(e && e.which) { //if which property of event object is supported (NN4)
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	} else {
		e = event;
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}

	if(characterCode == 13) { //if generated character code is equal to ascii 13 (if enter key)
		return false;
	} else {
		return true;
	}
}

// get the value of a radio group
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

//
// Adds event to window.onload without overwriting currently
// assigned onload functions.
function addLoadEvent(func)
{
    var oldonload = window.onload;
    if (typeof window.onload != 'function')
    {
        window.onload = func;
    }
    else
    {
        window.onload = function()
        {
            oldonload();
            func();
        }
    }
}

var venueSearchState = '';
var venueSearchKeyword = '';

// venue search is in an error state
// show the error message and allow the user to try again.
function venueError() {
	$('venueSelector').hide();
	$('venueLoading').hide();
	$('venueError').show();
	$('venueSearch').show();
	venueSearchState = 'error';
}

// perform the venue search using an ajax call
// takes a keyword and a result start number
function venueSearch(keywords,start) {
	$('venueHelp').hide();
	$('venueSearch').hide();
	$('venueError').hide();
	$('venueInput').hide();
	$('venueSelected').hide();
	if ( start <= 1 ) {	$('venueLoading').show(); }

	var url = '/stdevents/includes/venue_search.jsp';
	var pars = 'keywords=' + keywords + '&sNum=' + start;

	venueSearchState = 'searching';
	venueSearchKeyword = keywords;
	var venueCall = new Ajax.Request(
		url+'?'+pars,
		{ method: 'get',
		  onSuccess: function(transport) {
		  	if ( venueSearchState == 'searching' &&
		  	     venueSearchKeyword == keywords ) {
			  	$('venueSelector').update(transport.responseText);
			  	$('venueLoading').hide();
				$('venueSelector').show();
			}
		  },
		  onFailure: venueError
		}
	);

	//var venueCall = new Ajax.Updater(
	//	{success:'venueSelector'},
	//	url,
	//	{ method: 'get', parameters: pars, onFailure: venueError } );
}

// venue search cancel button pressed, reset divs
function venueCancel() {
	venueSearchState = 'cancel';
	venueSearchKeyword = '';
	$('venueHelp').show();
	$('venueSelector').hide();
	$('venueSelected').hide();
	$('venueInput').hide();
	$('venueError').hide();
	$('venueLoading').hide();
	$('venueSearch').show();
}

// venue search user has selected an item
function venueSelect(venueId) {

	$('venueSelector').hide();
	$('venueError').hide();

	if ( venueId == '0' || venueId=='' ) {
		$('venueInput').show();
		$('siteId').value = '0';
	} else {
		$('venueSelected').show();
		$('siteId').value = venueId;

		var url = '/stdevents/includes/venue_selected.jsp';
		var pars = 'wId=' + venueId;
		var venueCall = new Ajax.Updater(
			{success:'venueSelected'},
			url,
			{ method: 'get', parameters: pars, onFailure: venueError } );
	}
}
