// JavaScript Document

$(document).ready(function(){	

	$('div#hide-me').hide();
	// hide blogger / story buttons
	$("input:radio[@name=soldierSupporter]").click(function() {
		if ( $(this).val() == 'Supporter' ) {
			$('div#hide-me').slideUp('normal');
			$('#userType2').attr("checked", true); 
		} else {
			$('div#hide-me').slideDown('normal');
		}
	});
	
	// hide submit button 
	$('#submitStoryWrapper').hide();
	// show submit button if "agreeToTerms" is checked
	$('#agreeToTerms').click( function() {
		if($(this).is(':checked')) {
			$('#submitStoryWrapper').slideDown('normal');
		} else {
			$('#submitStoryWrapper').slideUp('normal');
		}
	});
	
/*
1	ROTC Cadet
2	Active Duty
3	Army Reserve
4	Army Veteran
5	National Guard	
6	Army Civilian	
7	West Point Cadet
*/
	
	// hide branch and badge if affiliate = ROTC
	$('select#affiliationID').change( function() {
		selectedVal = $(this).val();
		if(selectedVal == '1' || 
			selectedVal == '4' ||
			selectedVal == '7' || 
			selectedVal == '6') {
			$('div#fieldsToHide').slideUp('normal');
		} else {
			$('div#fieldsToHide').slideDown('normal');
		}
	});

	// if other branch is selected display other input
	$('select#branchID').change( function() {
		selectedVal = $(this).val();
		if(selectedVal == 'Other') {
			$('label#otherBranchLabel').slideDown('normal');
		}else {
			$('label#otherBranchLabel').slideUp('normal');				
		}
	});
	$('label#otherBranchLabel').hide();

	// if other race is selected display other input
	$('select#raceID').change( function() {
		selectedVal = $(this).val();
		if(selectedVal == 'Other') {
			$('label#otherRaceLabel').slideDown('normal');
		}else {
			$('label#otherRaceLabel').slideUp('normal');				
		}
	});
	$('label#otherRaceLabel').hide();
	
	// check username availability
	$("#email").blur(function() {
		//remove all the class add the messagebox classes and start fading
		fieldVal = $(this).val();
		if (fieldVal !== '') {
			if (checkEmail(fieldVal)) {
				$("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
				//check the username exists or not from ajax
				$.post("/action/action_checkEmail.cfm",{ email:$(this).val() } ,function(data) {
				if(data=='false'){ //if username not avaiable
					$("#msgbox").fadeTo(200,0.1,function(){ //start fading the messagebox
						//add message and change the class of the box and start fading
						$(this).html('This email already is already in use').addClass('messageboxerror').fadeTo(900,1);
					});
				} else {
					$("#msgbox").fadeTo(200,0.1,function(){  //start fading the messagebox
					//add message and change the class of the box and start fading
					$(this).html('Email available to register').addClass('messageboxok').fadeTo(900,1);
					});
				}
				});
			} else {
				$("#msgbox").removeClass().addClass('messagebox').text('A valid email address is required').fadeIn("slow");
			}
		} else {
			$("#msgbox").removeClass().text('');
		}
	});
	
	// check displayname availability
	$("#displayName").keyup(function() {
		//remove all the class add the messagebox classes and start fading
		fieldVal = $(this).val();
		if (fieldVal !== '') {
			$("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
			//check the displayname exists or not from ajax
			$.post("/action/action_checkDisplayName.cfm",{ displayName:$(this).val() } ,function(data) {
			if(data=='false'){ //if username not avaiable
				$("#msgbox").fadeTo(200,0.1,function(){ //start fading the messagebox
					//add message and change the class of the box and start fading
					$(this).html('Display name is already in use').addClass('messagebox').fadeTo(900,1);
				});
			} else {
				$("#msgbox").fadeTo(200,0.1,function(){  //start fading the messagebox
				//add message and change the class of the box and start fading
				$(this).html('Display name is available').addClass('messageboxok').fadeTo(900,1);
				});
			}
			});
		} else {
			$("#msgbox").removeClass().text('');
		}
	});
	
	
	$('textarea.maxchar').keyup(function(){
		var max = parseInt($(this).attr('maxlength'), 10);
		if($(this).val().length > max){
			$(this).val($(this).val().substr(0, $(this).attr('maxlength')));
		}
		$('#charsRemaining').html('You have ' + (max - $(this).val().length) + ' characters remaining');
		if((max - $(this).val().length) < 100) {
			$("#charsRemaining").addClass('alert');
		} else {
			$("#charsRemaining").removeClass('alert');
		}
	});
	

	
	//////////////////////////////////////////////
	// video or written
	$('a.choose-story').click(function(e) {
		e.preventDefault();
		myDiv = $(this).attr('href');
		$('#bottom-form').show();
		if (myDiv == 'written-story-form') {
			$('#default-story-form').remove();
			$('#submit-story').removeClass('contribute_page3-tab-off').addClass('contribute_page3-tab-on');
			$('#submit-video').removeClass('contribute_page3-tab-on').addClass('contribute_page3-tab-off');
			$('#video-story-form').hide();
			$('#'+myDiv).show();
			$('input#storyType').val('written');
		} else {
			$('#default-story-form').remove();
			$('#submit-video').removeClass('contribute_page3-tab-off').addClass('contribute_page3-tab-on');	
			$('#submit-story').removeClass('contribute_page3-tab-on').addClass('contribute_page3-tab-off');				
			$('#written-story-form').hide();
			$('#'+myDiv).show();	
			$('input#storyType').val('video');
		}
	});
	
	$('#written-story-form').hide();
	$('#video-story-form').hide();
	$('#bottom-form').hide();


	// open window for file upload 
	$('a.new-window').click(function(){
		//e.preventDefault();
		w = window.open($(this).attr('href'), 'mywindow','toolbar=0,location=0,menubar=0,resizable=1,width=426,height=400');
		w.focus();
		return false;
	});
	

	$.fn.preventDoubleSubmit = function() {
		var $that = $(this);
		$(this).submit(function() {
			if (this.beenSubmitted) {
				return false;
			} else {
				this.beenSubmitted = true;
				$that.find("input[type='submit']").attr('disabled', 'true');
			}
		});
	};
	$('form.styledForm').preventDoubleSubmit();


	if ($('#tag-tooltip').length) {
		$('a#tag-tooltip').tooltip({
			//position: 'bottom center',
		  	offset: [880,34],
			relative: true,
		  	onBeforeShow: function(event, position){
	  			//this.getTip();
				this.getTip().appendTo('#container');
			}
		});
  	}
	if ($('#tag-tooltip1').length) {
		$('a#tag-tooltip1').tooltip({
			position: 'bottom center',
		  	//offset: [880,34],
			relative: true,
		  	onBeforeShow: function(event, position){
	  			//this.getTip();
				this.getTip().appendTo('#container');
			}
		});
  	}
	
	if ($('#blogger-tooltip').length) {
		$('a#blogger-tooltip').tooltip({
			position: 'bottom center',
			relative: true,
		  	onBeforeShow: function(event, position){
				this.getTip().appendTo('#container').css('z-index','9999');
			}
		});
  	}
	if ($('#story-tooltip').length) {
		$('a#story-tooltip').tooltip({
			position: 'bottom center',
			relative: true,
		  	onBeforeShow: function(event, position){
				this.getTip().appendTo('#container').css('z-index','9999');
			}
		});
  	}
	
	$('input.goBackConfirm').click(function() {
		var page = $(this).attr('name');
		var answer = confirm('The data on this page has not be saved. Click "OK" to continue or "Cancel" to remain on this page');
		if (answer){
			window.location = '/tell-your-story/' + page + '/';
		}
	});
	$('.goBackBtn').click(function() {
		var page = $(this).attr('name');
		window.location = '/tell-your-story/' + page + '/';
	});

});

function checkEmail(myEmail) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myEmail)){
	//if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.mil)+$/.test(myEmail)){
		return (true);
	} else {
		return (false);
	}
}



