$(function () {
	var pageUrl = window.location;
	var urlString = pageUrl.toString();
	var tabContainers = $('div#contactforms > div');
	
	$('#confirmation_section').hide();
	
	if (urlString.match('confirmation')) {
		tabContainers.hide();
		$('#confirmation_section').show();
		setTimeout(function() {
			setActiveTab ();
			$('#confirmation_section').hide();
		}, 15000);
	}
	
	////////////// Textfield / Textarea Default Texts //////////////
	function defaultTextIdentifier (selectedField) {
		switch ($(selectedField).attr('name')) {
			case 'firstname':
				defaultText = 'First Name';
				break;
			case 'lastname':
				defaultText = 'Last Name';
				break;
			case 'company':
				defaultText = 'Company';
				break;
			case 'email':
				defaultText = 'Email';
				break;
			case 'phone':
				defaultText = 'Phone';
				break;
			case 'aboutcp':
				defaultText = 'How did you hear about Crushpad?';
				break;
			case 'comments':
				defaultText = 'Questions and comments:';
				break;
		}
		return defaultText;
	}
	
	////////////// Restrict Field to Numbers ony //////////////
	function restrictToNumbers (selectedField) {
		$(selectedField).bind("keydown",function(e) {
			var key = (e.which) ? e.which : e.keyCode;
			keychar = String.fromCharCode(key);
			if ((key==null) || (key==0) || (key==8) ||  (key==9) || (key==13) || (key==27) || (key==96) || (key==97) || (key==98) || (key==99) || (key==100)  || (key==101) || (key==102) || (key==103) || (key==104) || (key==105) || (key==107) || (key==108) || (key==109) || (key==110) ) {
			   return true;
			}
			else if ((("0123456789").indexOf(keychar) > -1)) {
			   return true;
			}
			else {
			   return false;
			}
		});
	}
	
	$('input.text').click(function() {
		var defaultText = defaultTextIdentifier(this);
		if ($(this).attr('value') == defaultText) {
			$(this).attr('value','');
			$(this).removeClass('error_state');
		}
		if ($(this).attr('name') == 'phone') {
			restrictToNumbers(this);
		}
	});
	$('input.text').focus(function() {
		var defaultText = defaultTextIdentifier(this);
		if ($(this).attr('value') == defaultText) {
			$(this).attr('value','');
			$(this).removeClass('error_state');
		}
		if ($(this).attr('name') == 'phone') {
			restrictToNumbers(this);
		}
	});
	$('input.text').blur(function() {
		var defaultText = defaultTextIdentifier(this);
		if ($(this).attr('value') == "") {
			$(this).attr('value',defaultText);
		};
	});
	
	
	$('textarea').click(function() {
		var defaultText = defaultTextIdentifier(this);
		if ($(this).attr('value') == defaultText) {
			$(this).attr('value','');
		}
	});
	$('textarea').focus(function() {
		var defaultText = defaultTextIdentifier(this);
		if ($(this).attr('value') == defaultText) {
			$(this).attr('value','');
		}
	});
	$('textarea').blur(function() {
		var defaultText = defaultTextIdentifier(this);
		if ($(this).attr('value') == "") {
			$(this).attr('value', defaultText);
		}
	});
	
	////////////// Required Fields Form Validation //////////////
	$('form').submit(function() {
		currentForm = $(this);
		currentFormID = currentForm.attr('id');
		that_first_name = $('#'+currentFormID+'_firstname').attr('value');
		that_last_name = $('#'+currentFormID+'_lastname').attr('value');
		that_email = $('#'+currentFormID+'_email').attr('value');
		
		if (that_first_name == '' || that_first_name == 'First Name') {
			$('#'+currentFormID+'_firstname').addClass('error_state');
			curfname = false;
		} else {
			curfname = true;
		}
		if (that_last_name == '' || that_last_name == 'Last Name') {
			$('#'+currentFormID+'_lastname').addClass('error_state');
			curlname = false;
		} else {
			curlname = true;
		}
		if (that_email == '' || that_email == 'Email') {
			$('#'+currentFormID+'_email').addClass('error_state');
			curemail = false;
		} else {
			curemail = true;
		}
		
		if (curfname == false || curlname == false || curemail == false) {
			alert('Please fill up all required fields');
			return false;
		} else {
			$(currentForm).submit();
		}
	});
});
