$(function() {
	$.wbs_msg = function(msg_text) {
		$('#ef_msg').hide();
		$('#ef_msg').html(msg_text);
		$('#ef_msg').animate({opacity:'show'}, 1000, function(){
			window.setTimeout(function(){
				//$('#ef_msg').animate({opacity:'hide'});
			}, 4000);
		});
	}
	var locations = '';
	$.validator_init = function() {
		$('#wbs_enrolment_form')[0].reset();
		$('input:hidden').val('');
		$('input').attr('alt','');
		$('#wbs_form_holder #ef_submit').live('click',function() {
			$('.input_multiselect').find(':input:hidden').val(locations);
			$('#wbs_form_holder input, #wbs_form_holder textarea').each(function(i) {
				if($(this).attr('type') != 'checkbox') {
					$.validator_check($(this));
				}
			});
			
			if($('#wbs_form_holder input[alt="error"],textarea[alt="error"]').length == 0) {
				//alert('return');
				return true;
			} else {
				$.wbs_msg('Please check fields and try again');
				window.scrollTo(0,0);
				return false;
			}
		});
		$('.ef_field input:not(:hidden)').live('keyup',function() {
			$.validator_check($(this));
		});
		$('.ef_field textarea').live('keyup',function() {
			$.validator_check($(this));
		});
	}
	$.validator_check = function(field) {
		var re_array = new Array();
		re_array[0] = /^[A-Za-z]+[A-Za-z\'\-\s]*$/; // alpha
		re_array[1] = /^([A-Za-z]+[A-Za-z\'\-\s]*)?$/; // alpha_opt
		re_array[2] = /^[0-9\s\(\)\-\+]+$/; // number
		re_array[3] = /^([0-9\s\(\)\-\+]+)?$/; // number_opt
		re_array[4] = /^\w+[\/\-\,\\\(\)\.\&\'\#\?\$\!\|\:\w\s]*$/; // multi
		re_array[5] = /^(\w+[\/\-\,\\\(\)\.\&\'\#\?\$\!\|\:\w\s]*)?$/; // multi_opt
		re_array[6] = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,5})+$/; // email
		re_array[7] = /^(\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,5})+)?$/; // email_opt
		re_array[8] = /^[A-Za-z0-9]+[A-Za-z0-9\s]*$/; // alphanumeric
		re_array[9] = /^([A-Za-z0-9]+[A-Za-z0-9\s]*)?$/; // alphanumeric_opt
		
		
		var re_id = parseInt(field.attr('id').substr(field.attr('id').length-1));
		//alert(re_id);
		if(!isNaN(re_id)) {
		
			var re_val = field.val();
			
			if(field.parent().parent().attr('class') == 'ef_select_box') {
				field = field.parent().parent();
			}
			if(re_val.match(re_array[re_id])) {
				if((re_id==1||re_id==3||re_id==5||re_id==7||re_id==9)&&(re_val.length==0)/*&&(field.parent().parent().attr('class')!='ef_select_box')*/) {
					field.css({borderColor:'#CCCCCC', backgroundImage:'url(images/wbs_form_bg.gif)'});
					field.parent().find('input:not(:checkbox), textarea').attr({alt:'empty'});
					field.parent().find('.ef_icon').html('');
				} else {
					field.css({borderColor:'#99CC00', backgroundImage:'url(images/wbs_form_acc_bg.gif)'});
					field.parent().find('input:not(:checkbox), textarea').attr({alt:'ok'});
					if(field.parent().find('input').length > 1) {
						if(field.parent().find('input[alt="error"]').length==0) {
							field.parent().find('.ef_icon').html('<img src="images/ef_icon_tick.gif" />');
						}
					} else {
						field.parent().find('.ef_icon').html('<img src="images/ef_icon_tick.gif" />');
					}
				}
			} else {
				field.css({borderColor:'#CC0000', backgroundImage:'url(images/wbs_form_err_bg.gif)'});
				field.parent().find('input:not(:checkbox), textarea').attr({alt:'error'});
				field.parent().find('.ef_icon').html('<img src="images/ef_icon_error.gif" />');
			}
		}
	}
	
	$.selectbox_init = function() {
		$('.input_select .ef_select_box .ef_select_btn').live('click',function() {
			var tdt_selectbox = $(this).parent().find('.ef_select_options');
			tdt_selectbox.toggle();
		});
		$('.input_select .ef_select_options a').live('click',function() {
			$(this).parent().parent().find('.ef_select_value').html($(this).html()).css({color:'#333333'});
			$(this).parent().find('input').val($(this).attr('id'));
			$(this).parent().toggle();
			$.validator_check($(this).parent().find('input'));
			return false;
		});
	}
	
	$.selectbox_init();
	$.validator_init();
	
	$.tickets = function() {
		var currRows = [];
		var zi = 100;
		var thisRow = 1;
		$('#addTickets').live('click',function() {
			var i = $('.input_tickets').size();
			if(i != 5) {
				currRows[thisRow] = thisRow;
				var ticketRowLeft = '<div class="ef_field input_select ef_req" style="z-index:'+zi+';"><label>Ticket Type</label><div class="ef_select_box" id="selc_1"><span class="ef_select_value">Select...</span><a class="ef_select_btn"></a><div class="ef_select_options"><a href="#" id="festival">Festival ticket &ndash; all weekend $30</a><a href="#" id="festival_lady">Festival ticket &ndash; all weekend and Lady Sings The Blues Special Show $40</a><a href="#" id="day_sat">Day ticket &ndash; Saturday only $20</a><a href="#" id="day_sun">Day ticket &ndash; Sunday only $20</a><a href="#" id="lady">Lady Sings The Blues Special Show only $25</a><input type="hidden" class="input_tickettype" id="ef_tickettype-'+thisRow+'_4" name="ef_tickettype-'+thisRow+'_4" value="" /></div></div><div class="ef_icon"><img src="images/ef_icon_req.gif" /></div></div>';
				var ticketRowRight = '<div class="ef_field input_select ef_req" style="z-index:'+zi+';"><label>Tickets #</label><div class="ef_select_box" id="selc_1"><span class="ef_select_value">Select...</span><a class="ef_select_btn"></a><div class="ef_select_options"><a href="#" id="1">1</a><a href="#" id="2">2</a><a href="#" id="3">3</a><a href="#" id="4">4</a><a href="#" id="5">5</a><a href="#" id="6">6</a><a href="#" id="7">7</a><a href="#" id="8">8</a><a href="#" id="9">9</a><a href="#" id="10">10</a><input type="hidden" class="input_tickets" id="ef_tickets-'+thisRow+'_4" name="ef_tickets-'+thisRow+'_4" value="" /></div></div><div class="ef_icon"><img src="images/ef_icon_req.gif" /></div><div id="'+thisRow+'" class="remove"><img src="images/icon_minus.gif" width="16" height="16" /><span>Remove</span></div></div>';
				$(this).siblings('.ef_form_group:not(.marginLeft)').append(ticketRowLeft);						   
				$(this).siblings('.ef_form_group.marginLeft').append(ticketRowRight);
				thisRow++;
			} else {
				alert('Maximum ticket types reached.');
			}
			zi-=5;
		});
		$('.remove').live('click',function() {
			var thisID = $(this).attr('id');
			var removeLeft = '#ef_tickettype-'+thisID+'_4';
			var removeRight = '#ef_tickets-'+thisID+'_4';
			$(this).parents('#tickets').find(removeLeft).parents('.ef_field').remove();
			$(this).parents('#tickets').find(removeRight).parents('.ef_field').remove();
			currRows[thisID] = '';
		});
	}
	$.tickets();
	
});
