/***********************************************
 * Fundraiser functions
 **********************************************/

//reset specific form elements
function resetForm(){
	$("locationDropDowns").innerHTML = '';
	if( $("location1") ) $("location1").value = "";
	if( $("location2") ) $("location2").value = "";
	if( $("location3") ) $("location3").value = "";
	if( $('startTime') ) $('startTime').value = "";
	if( $('endTime') )  $('endTime').value = "";
	$("eventDate").value = "";
	$('fundraiserDate').hide();
	$('fundraiserTimes').hide();
	//$("seshdate").value = "";
	hideGroups();
}

//called when groupcount changes
function checkGroupCount() {
	var elem = $('multiple');
	if( parseInt($F('guestCount')) >= 250 ) {
		elem.disabled = false;
		document.getElementById("multiple_div").style.visibility = 'visible';
	} else {
		if (typeof userRole == 'undefined') {
		elem.disabled = true;
		}
		
		document.getElementById("multiple_div").style.visibility = 'hidden';
		if( elem.checked == true ) {
			$('multiple').checked = false;
			resetForm();
		}
	}
}

//called when groupcount changes for admin portal
function checkAdminGroupCount() {
	var elem = $('multiple');
	if( parseInt($F('guestCount')) >= 32 ) {
		elem.disabled = false;
		document.getElementById("multiple_div").style.visibility = 'visible';
	} else {
		elem.disabled = true;
		document.getElementById("multiple_div").style.visibility = 'hidden';
		if( elem.checked == true ) {
			$('multiple').checked = false;
			resetForm();
		}
	}
}

function showLocations() {
	var zcode = $F("locationZip");
	var zc = $("ziperror");
	var dist = $F("distance");
	
	$("eventCalendar").hide();
	
	if(zcode.length != 5 ){
		zc.show();
		resetForm();
	}else{
		zc.hide();
		$('locationDropDowns').innerHTML = '';
		getLocations(site_root + 'util/getLocations.php');
	}
}

function checkTimes(){
	if( $F('startTime') != '' && $F('endTime') != '' ){
		$('orgHeader').show();
		showGroups();
	}else{
		$('orgHeader').hide();
		hideGroups();
	}
}
function showTimes(){
	if($F('eventDate') != '') {
		$('fundraiserTimes').show();
		$('chooseTimeHeader').show();
		$('startTime').selectedIndex = 0;//value = '';
		$('endTime').selectedIndex = 0;//value = '';
		checkTimes();
	}
}
function hideGroups(){
	var groups = $$('.hidden');
	groups.invoke('hide');
}
function showGroups(){
	var groups = $$('.hidden');
	groups.invoke('show');
}

function padZeros(value, num){
	while( value.length < num ){
		value = '0' + value;
	}
	return value;
}
/***********************************************
 * Ajax functions
 **********************************************/
function getLocations(locationsgw) {
	if($("guestCount").value < 35){
		$("gcounterror").show();
		return;
	}else{
		$("gcounterror").hide();
	}
	$("eventDate").value = "";
	//$("eventCalendar").hide();
	scwDisabledDates = new Array();
	new Ajax.Request( locationsgw,	{
		method: 'post',
		parameters: $('fundform').serialize(),
		onSuccess: function(res) {
			var result = res.responseText;
			$('locationDropDowns').innerHTML = result;
		},
		onFailure: function (res) {
			alert(res.responseText);
		}
	});
}

function locationSelect() {

	$('eventDate').value = "";
	$('fundraiserDate').hide();
	$('fundraiserTimes').hide();
	hideGroups();

	//initialize calendar object
	getUnavailable();

	var showDate = false;
	if($('location1')) {
		if( $("location1").getValue() != "" ) {
			showDate = true;
			$('location1hours').style.visibility = 'visible';
			$('location1hours').href = 'http://rubios.know-where.com/rubios/cgi/site?site=' + padZeros($F('location1'), 5);
		} else {
			if( $('location1hours') )
				$('location1hours').style.visibility = 'hidden';
		}
	}

	if($('location2')) {
		if( $("location2").getValue() != "" ) {
			showDate = true;
			$('location2hours').style.visibility = 'visible';
			$('location2hours').href = 'http://rubios.know-where.com/rubios/cgi/site?site=' + padZeros($F('location2'), 5);
		} else {
			if( $("location2hours") )
				$('location2hours').style.visibility = 'hidden';
		}
	}

	if($('location3')) {
		if ( $("location3").getValue() != ""){
			showDate = true;
			$('location3hours').style.visibility = 'visible';
			$('location3hours').href = 'http://rubios.know-where.com/rubios/cgi/site?site=' + padZeros($F('location3'), 5);
		} else {
			if( $("location3hours") )
				$('location3hours').style.visibility = 'hidden';
		}
	}

	if( showDate ){
		$('fundraiserDate').show();
		$('chooseDateHeader').show();
	}else{
		$('fundraiserDate').hide();
		$('chooseDateHeader').hide();
	}
	
	$("eventCalendar").show();
}

function getUnavailable(){
	scwDisabledDates = new Array();
	new Ajax.Updater(
		'eventCalendar',
		site_root + 'util/getUnavailableDates.php',
		{
			method: 'post',
			parameters: $('fundform').serialize(),
			evalScripts: true,
			onSuccess: function(res) {
				checkHours();
			},
			onFailure: function (res) {
				alert(res.responseText);
			}
		}
	);
}
/*  we are using ajax just to swap out innerHTML, because swapping out with display styles
   retains separate startTime/endTime values in each div holding near-identical select dropdowns --
   when form is submitted, both divs with startTime/endTime dropdowns contain values
   (if the user went from weekend to weekday or vice versa) which causes the wrong values to be passed by POST
   the Ajax.Updater solves this problem
*/
function checkHours(){
	var cwh = $F("eventDate");
	var cwhH = new Date(cwh);
	var cwhD = cwhH.getDay();

	if(cwhD == 6 || cwhD == 0){
		var locationsgw1 = site_root + "util/getWeekendTimeDropDowns1.php";
		var locationsgw2 = site_root + "util/getWeekendTimeDropDowns2.php";
	}else{
		var locationsgw1 = site_root + "util/getTimeDropDowns1.php";
		var locationsgw2 = site_root + "util/getTimeDropDowns2.php";
	}
	new Ajax.Updater(
		'hourDropDowns1',
		locationsgw1,
		{
			method: 'post',
			parameters: { startTime: $F('startTime') },
			evalScripts: true,
			onSuccess: function(res) {
				//var result = res.responseText;
				//$(container).innerHTML = result;
			},
			onFailure: function (res) {
				alert(res.responseText);
			}
		}
	);

	new Ajax.Updater(
		'hourDropDowns2',
		locationsgw2,
		{
			method: 'post',
			parameters: { endTime: $F('endTime') },
			evalScripts: true,
			onSuccess: function(res) {
				//var result = res.responseText;
				//$(container).innerHTML = result;
			},
			onFailure: function (res) {
				alert(res.responseText);
			}
		}
	);
}



/**********************************************
 * Event Handlers
 *********************************************/
Event.observe(window, 'load', tick);
Event.observe(window, 'load', checkGroupCount);

//prevent return from submitting form
Event.observe(window, 'load', function() {
	Event.observe(window, 'keypress', function(event) {
		if (event.keyCode == 13) {
	    	Event.stop(event);
		}
	});
});
