var ShowOvernightNB = function(iIndex){
//	alert(iIndex);
//	var i_id = oEl.id.replace(/^end_hour_([\d]*)$/, '$1');
	var i_id = iIndex;
	var s_html = $('overnight_' + i_id).get('html');
	var s_new_html_begin = '<span class="begin">' + $('begin_hour_' + i_id).value + 
		':' + $('begin_minute_' + i_id).value + '</span>';
	var s_new_html_end = '<span class="end">' + $('end_hour_' + i_id).value + 
		':' + $('end_minute_' + i_id).value + '</span>';
	
	if ($('end_hour_' + i_id).value >= '00' 
		&& $('end_hour_' + i_id).value <= '05')
	{
		if (s_html.test(/\[begin\]/) === true){
			s_html = s_html.replace(/\[begin\]/i, s_new_html_begin);
			s_html = s_html.replace(/\[end\]/i, s_new_html_end);
		} else {
			s_html = s_html.clean().replace(/<span class=("?)begin("?)>[\d]{2}:[\d]{2}<\/span>/i, s_new_html_begin);
			s_html = s_html.clean().replace(/<span class=("?)end("?)>[\d]{2}:[\d]{2}<\/span>/i, s_new_html_end);
		}
		$('overnight_' + i_id).set('html', s_html);
		$('overnight_' + i_id).removeClass('hidden');
	} else {
		s_html = s_html.clean().replace(/<span class=("?)begin("?)>[\d]{2}:[\d]{2}<\/span>/i, '[begin]');
		s_html = s_html.clean().replace(/<span class=("?)end("?)>[\d]{2}:[\d]{2}<\/span>/i, '[end]');
		$('overnight_' + i_id).set('html', s_html);
		$('overnight_' + i_id).addClass('hidden');
	}
};

var CheckEventData = function(){
	$$('.error').removeClass('error');
	
	if ($('name_nl') && $('name_nl').value.trim().length === 0){
		$('name_nl').focus();
		$('name_nl').addClass('error');
		alert($('error-name').get('text'));
		return false;
	}
	
	if ($('name_en') && $('name_en').value.trim().length === 0){
		$('name_en').focus();
		$('name_en').addClass('error');
		alert($('error-name').get('text'));
		return false;
	}
	
	if ($$('input[name="type[]"]:checked').length === 0){
		$$('input[name="type[]"]')[0].focus();
		alert($('error-type').get('text'));
		return false;
	}
	
	if ($('description_nl') && $('description_nl').value.trim().length === 0){
		$('description_nl').focus();
		$('description_nl').addClass('error');
		alert($('error-description').get('text'));
		return false;
	}
	
	if ($('description_en') && $('description_en').value.trim().length === 0){
		$('description_en').focus();
		$('description_en').addClass('error');
		alert($('error-description').get('text'));
		return false;
	}
	
	if ($('location') && $('location').value.trim().length === 0){
		$('location').focus();
		$('location').addClass('error');
		alert($('error-location').get('text'));
		return false;
	}
	
	if ($('location_address') && $('location_address').value.trim().length === 0){
		$('location_address').focus();
		$('location_address').addClass('error');
		alert($('error-address').get('text'));
		return false;
	}
	
	if ($('location_zipcode') && $('location_zipcode').value.trim().test(/^[\d]{4}[a-z]{2}$/i) === false) {
		$('location_zipcode').focus();
		$('location_zipcode').addClass('error');
		alert($('error-zipcode').get('text'));
		return false;
	}
	
	if ($('location_city') && $('location_city').value.trim().length === 0){
		$('location_city').focus();
		$('location_city').addClass('error');
		alert($('error-city').get('text'));
		return false;
	}
	
	if ($$('input[name="date[]"]:checked').length === 0){
		// no dates selected!
		$$('input[name="date[]"]')[0].focus();
		alert($('error-date').get('text'));
		return false;
	}
	
	// check if begintime < endtime
	var b_date_error = false;
	var a_dates = $$('input[name="date[]"]:checked');
	
	a_dates.each(function(oEl){
		var i_id = oEl.id.replace(/^date-([\d]*)/, '$1');
		var i_begin = $('begin_hour_' + i_id).value + '' + 
			$('begin_minute_' + i_id).value;
		var i_end = $('end_hour_' + i_id).value + '' + 
			$('end_minute_' + i_id).value;
			
		if (CheckTimes(i_begin, i_end) === false){
			oEl.focus();
			alert($('error-time').get('text'));
			a_dates.empty();
			b_date_error = true;
		}
	});
	if (b_date_error === true) return false;
	// end check times

	if ($('organizer') && $('organizer').value.trim().length === 0){
		$('organizer').focus();
		$('organizer').addClass('error');
		alert($('error-organizer').get('text'));
		return false;
	}
	
	if ($('contact_name') && $('contact_name').value.trim().length === 0){
		$('contact_name').focus();
		$('contact_name').addClass('error');
		alert($('error-contact_name').get('text'));
		return false;
	}
	
	if ($('contact_phone') && $('contact_phone').value.trim().length === 0){
		$('contact_phone').focus();
		$('contact_phone').addClass('error');
		alert($('error-contact_phone').get('text'));
		return false;
	}
	
	if ($('contact_email') && ($('contact_email').value.trim().length === 0
		|| $('contact_email').value.trim().test(/^[a-z0-9._\-]+@[a-z0-9\-.]+\.[a-z.]{2,5}$/i) === false))
	{
		$('contact_email').focus();
		$('contact_email').addClass('error');
		alert($('error-contact_email').get('text'));
		return false;
	}
	
	if ($('organizer_zipcode') && $('organizer_zipcode').value.trim().length > 0 && 
		$('organizer_zipcode').value.trim().test(/^[\d]{4}[a-z]{2}$/i) === false)
	{
		$('organizer_zipcode').focus();
		$('organizer_zipcode').addClass('error');
		alert($('error-organizer_zipcode').get('text'));
		return false;
	}

	return true;
};

var CheckTimes = function (sBegin, sEnd){
	if (sBegin >= '0500' && sBegin <= '2345'){
		if (sEnd < sBegin){
			if (sEnd >= '0515'){
				return false;
			}
		}
	} else if (sBegin >= '0000' && sBegin < '0500') {
		if (sEnd < sBegin || sEnd > '0600'){
			return false;
		}
	}
};

var ToggleTime = function(iIndex){
	if ($('begin_hour_' + iIndex)){
		if ($('begin_hour_' + iIndex).disabled == true){
			$('begin_hour_' + iIndex).disabled = false;
			$('begin_minute_' + iIndex).disabled = false;
			
			$('end_hour_' + iIndex).disabled = false;
			$('end_minute_' + iIndex).disabled = false;
			
			// check if there is another date active, if so, use begin- & endtime
			if ($$('input[name="date[]"]:checked').length > 1){
				if ($$('input[name="date[]"]:checked')[0].id.replace(/^date-([\d]*)$/, '$1') != iIndex){
					var i_other_active_index = $$('input[name="date[]"]:checked')[0].id.replace(/^date-([\d]*)$/, '$1');
					// use begin- & endtime from first active one
					$('begin_hour_' + iIndex).selectedIndex = 
						$('begin_hour_' + i_other_active_index).selectedIndex;
					$('begin_minute_' + iIndex).selectedIndex = 
						$('begin_minute_' + i_other_active_index).selectedIndex;

					$('end_hour_' + iIndex).selectedIndex = 
						$('end_hour_' + i_other_active_index).selectedIndex;
					$('end_minute_' + iIndex).selectedIndex = 
						$('end_minute_' + i_other_active_index).selectedIndex;
				} else {
					var i_other_active_index = $$('input[name="date[]"]:checked')[1].id.replace(/^date-([\d]*)$/, '$1');
					// use begin- & endtime from the second one 
					$('begin_hour_' + iIndex).selectedIndex = 
						$('begin_hour_' + i_other_active_index).selectedIndex;
					$('begin_minute_' + iIndex).selectedIndex = 
						$('begin_minute_' + i_other_active_index).selectedIndex;

					$('end_hour_' + iIndex).selectedIndex = 
						$('end_hour_' + i_other_active_index).selectedIndex;
					$('end_minute_' + iIndex).selectedIndex = 
						$('end_minute_' + i_other_active_index).selectedIndex;
				}
				ShowOvernightNB(iIndex);
			}
		} else {
			$('begin_hour_' + iIndex).disabled = true;
			$('begin_minute_' + iIndex).disabled = true;
			
			$('end_hour_' + iIndex).disabled = true;
			$('end_minute_' + iIndex).disabled = true;
			
			$('overnight_' + iIndex).addClass('hidden');
		}
	}
};

window.addEvent('domready', function(){
	
	if ($('add-event-form')){
		$('add-event-form').addEvent('submit', function(){
			return CheckEventData();
		});
	}
	
	$$('input[name="date[]"]').each(function(oEl){
		oEl.addEvent('click', function(){
			ToggleTime(oEl.id.replace(/^date-([\d]*)$/, '$1'));
		});
	});
});
