webmeetup = { 
	
	//test 
	counter: 1,
	hue: 50,
	
	formValues: {},
	
	// change the background color
	changeColor: function() {
		$('body').css('background-color', 'rgb(' + webmeetup.hue + ',30,' + webmeetup.hue + ')');
		//$('body').css('background-color', 'hsl(' + webmeetup.hue + ',30%,30%)');
		//  $('body').css('background-color', '#7a7a7a'); 
		
		
		
    if (webmeetup.counter == 1) {
 webmeetup.hue++;
 if (webmeetup.hue == 130) webmeetup.counter = 2;
}
    
if (webmeetup.counter == 2) {
 webmeetup.hue--;
 if (webmeetup.hue == 50) webmeetup.counter = 1;
}

},
	
	
	
		
	
	
	
	
	
	
	// check if the url is valid
	isValidUrl: function(url) {
		var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
		return regexp.test(url);
	},
	
	
	strip_tags: function(str, allowed_tags) {
		// Strips HTML and PHP tags from a string
		// version: 905.3122
		// discuss at: http://phpjs.org/functions/strip_tags
		var key = '', allowed = false;
		var matches = [];
		var allowed_array = [];
		var allowed_tag = '';
		var i = 0;
		var k = '';
		var html = '';

		var replacer = function(search, replace, str) {
			return str.split(search).join(replace);
		};

		// Build allowes tags associative array
		if (allowed_tags) {
			allowed_array = allowed_tags.match(/([a-zA-Z]+)/gi);
		}

		str += '';

		// Match tags
		matches = str.match(/(<\/?[\S][^>]*>)/gi);

		// Go through all HTML tags
		for (key in matches) {
			if (isNaN(key)) {
				// IE7 Hack
				continue;
			}

			// Save HTML tag
			html = matches[key].toString();

			// Is tag not in allowed list? Remove from str!
			allowed = false;

			// Go through all allowed tags
			for (k in allowed_array) {
				// Init
				allowed_tag = allowed_array[k];
				i = -1;

				if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+'>');}
				if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+' ');}
				if (i != 0) { i = html.toLowerCase().indexOf('</'+allowed_tag)   ;}

				// Determine
				if (i == 0) {
					allowed = true;
					break;
				}
			}

			if (!allowed) {
				str = replacer(html, "", str); // Custom replace. No regexing
			}
		}

		return str;
	},
	
	
	// gather the form values
	initForm: function() {
		webmeetup.formValues.name = $.trim($('input:text[name=name]').val());
		webmeetup.formValues.url = $.trim($('input:text[name=url]').val());
		webmeetup.formValues.type =  $('input:radio[name=type]:checked').val();
		webmeetup.formValues.occupation = $.trim($('input:text[name=occupation]').val());
		webmeetup.formValues.course = $.trim($('input:text[name=course]').val());
	},
	
	//validate form
	validateForm: function() {
		
		var validForm = true;
		
		// name
		if(webmeetup.formValues.name.length == 0) {
			$('#name + label').addClass('input-error').attr('title', 'gelieve je naam in te vullen');
			validForm = false;
		}
		else if(webmeetup.formValues.name.length > 100) {
			$('#name + label').addClass('input-error').attr('title', 'je naam is te lang');
			validForm = false;
		}
		else $('#name + label').removeClass('input-error').removeAttr('title');
		
		// url
		if(webmeetup.formValues.url.length == 0 || webmeetup.formValues.url == 'http://') {
			$('#url + label').addClass('input-error').attr('title', 'gelieve je website in te vullen');
			validForm = false;
		}
		else if(!webmeetup.isValidUrl(webmeetup.formValues.url)) {
			$('#url + label').addClass('input-error').attr('title', 'dit is geen geldige website');
			validForm = false;
		}
		else $('#url + label').removeClass('input-error').removeAttr('title');

		// type
		if(webmeetup.formValues.type == 'professional') {
			if(webmeetup.formValues.occupation.length == 0) {
				$('#occupation + label').addClass('input-error').attr('title', 'gelieve je functie in te vullen');
				validForm = false;
			}
			else $('#occupation + label').removeClass('input-error').removeAttr('title');
		}
		else $('#occupation + label').removeClass('input-error').removeAttr('title');
		
		if(webmeetup.formValues.type == 'student') {
			if(webmeetup.formValues.course.length == 0) {
				$('#course + label').addClass('input-error').attr('title', 'gelieve je studierichting in te vullen');
				validForm = false;
			}
			else $('#occupation + label').removeClass('input-error').removeAttr('title');
		}
		else $('#course + label').removeClass('input-error').removeAttr('title');
		
		return validForm;
		
	},
	
	// handle the form
	handleForm: function() {
			
		// bind form using 'ajaxForm'
		$('#form').submit(function(evt) {
			
			evt.preventDefault();
			
			webmeetup.initForm();
					
			if(webmeetup.validateForm()) {
			
				$.ajax({
					   type: "POST",
					   url: "init.php",
					   data: webmeetup.formValues,
					   success: function(){
						 webmeetup.addSignup();
					   },
					   error: function(msg){
						   alert('Er was een probleem met je inschrijving.');
					   }
				});
			}   	
		});
		
		return false;
	},
	
	addSignup: function(values) {
		
		 var row =  $('table.datagrid tbody tr:has(td:empty):first');
		 
		 row.children('td:eq(1)').html(webmeetup.strip_tags(webmeetup.formValues.name));
		 
		 if(webmeetup.formValues.type == 'professional') row.children('td:eq(2)').html(webmeetup.strip_tags(webmeetup.formValues.occupation));
		 else if(webmeetup.formValues.type == 'student') row.children('td:eq(2)').html('student ' + webmeetup.strip_tags(webmeetup.formValues.course));
		 
		 row.children('td:eq(3)').html('<a href="' + webmeetup.strip_tags(webmeetup.formValues.url) + '" title="' + webmeetup.strip_tags(webmeetup.formValues.url) + '">⊕<span>Site</span></a>');
		 
		 // update counter
		 var counter =  $('#signupCount p span:first');
		 counter.text(parseInt(counter.text()) + 1);
		 
		 webmeetup.clearForms();
		 
		 webmeetup.scrollTo('h1.offScreen', 1500);
	},
	
	clearForms: function() {
		$('input:text').val('');
		$('input:radio[value=professional]').attr('checked', 'checked');
		$('input:radio[value=student]').removeAttr('checked');
		
		$('.input-error').removeClass('input-error');
	},
	
	// scroll to a point on the page
	scrollTo: function(location, speed) {
		
		var scrollValue = $(location).offset().top - $(location).height()
		$('html, body').animate({ scrollTop: scrollValue}, speed);
	}

}

jQuery(function($){

	setInterval("webmeetup.changeColor()", 80);

	webmeetup.handleForm();
	
	$('#bekijkAanwezigen').bind('click', function(evt) {
		evt.preventDefault();
		webmeetup.scrollTo('h1.offScreen', 1500);
	});	

});

$(document).ready(function() {

	//  Disable both radio --> input text fields
	$('#course, #occupation')
		.attr('disabled', 'disabled')
		.parent().parent().addClass('disabledLine');

	if ($('#type_student').is(':checked')) {
		$('#course')
			.attr('disabled', '')
			.parent().parent().removeClass('disabledLine');
	} else {
		$('#occupation')
			.attr('disabled', '')
			.parent().parent().removeClass('disabledLine');
	}

	$('#type_student, #type_professional').bind('change', function(event) {
		
		//  Disable both radio --> input text fields
		$('#course, #occupation')
			.attr('disabled', 'disabled')
			.parent().parent().addClass('disabledLine');

		if ($('#type_student').is(':checked')) {
			$('#course')
				.attr('disabled', '')
				.parent().parent().removeClass('disabledLine');
		} else {
			$('#occupation')
				.attr('disabled', '')
				.parent().parent().removeClass('disabledLine');
		}

	});
	
});

