// add all errors up here
var errors = {
	noTitle: 'Please enter the film\'s <strong>title.</strong>',
	noRunningTime: 'Please enter the film\'s <strong>running time.</strong>',
	noDateCompleted: 'Please enter the film\'s <strong>completion date.</strong>',
	invalidDateCompleted: 'Please enter a <strong>valid</strong> completion date (mm/dd/yyyy).',
	noStyle: 'Please enter the film\'s <strong>style.</strong>', 
	noDirector: 'Please enter the film\'s <strong>director.</strong>',
	noPhone: 'Please enter a <strong>contact phone number.</strong>',
	noEmail: 'Please enter a <strong>contact email address.</strong>',
	invalidEmail: 'Please enter a <strong>valid email address.</strong>',
	noAddress1: 'Please enter one <strong>address</strong> line.',
	noCity: 'Please enter your <strong>city.</strong>',
	noState: 'Please enter your <strong>state or country</strong> (non-USA only).',
	noPostal: 'Please enter your <strong>zip/postal code.</strong>',
	noSynopsis: 'Please enter the film\'s <strong>synopsis.</strong>',
	noSignature: 'Please <strong>sign</strong> the form.',
	noToday: 'Please <strong>date</strong> the form.', 
	invalidToday: 'Please enter a <strong>valid date</strong> for today (mm/dd/yyyy).'
}

$(document).ready(function() {

	$("#scrolltop").click(function(){
			$("html, body").animate({ scrollTop: 0 }, {
	        duration: 'slow',
	        easing: 'easeInOutQuad'
	    });
	    return false;
	}); 
		
	// generate error message array
	$('form#submissionForm').submit(function() {
		var filmTitle = $('#filmTitle').val();
		var runningTime = $('#runningTime').val();
/* 		var dateCompleted = $('#dateCompleted').val(); */
		var style = $('#style').val();
		
		var director = $('#director').val();
		var producer = $('#producer').val();
		var website = $('#website').val();
		var phone = $('#phone').val();
		var fax = $('#fax').val();
		var email = $('#email').val();
		var address1 = $('#address1').val();
		var address2 = $('#address2').val();
		var city = $('#city').val();
		var state = $('#state').val();
		var postal = $('#postal').val();
		
		var synopsis = $('#synopsis').val();
		var previous = $('#previous').val();
		var signature = $('#signature').val();
		var today = $('#today').val();
		
		var errorMessages = [];
		
		if(isEmpty(filmTitle)) {
			errorMessages.push(errors.noTitle);
		}
		
		if(isEmpty(runningTime)) {
			errorMessages.push(errors.noRunningTime);
		}
		
/*
		if(isEmpty(dateCompleted)) {
			errorMessages.push(errors.noDateCompleted);
		} else if(!isValidUSDate(dateCompleted)) {
			errorMessages.push(errors.invalidDateCompleted);
		}
*/
		
		if(isEmpty(style)) {
			errorMessages.push(errors.noStyle);
		}
		
		if(isEmpty(director)) {
			errorMessages.push(errors.noDirector);
		}
		
		if(isEmpty(phone)) {
			errorMessages.push(errors.noPhone);
		}
		
		if(isEmpty(email)) {
			errorMessages.push(errors.noEmail);
		} else if(!isEmail(email)) {
			errorMessages.push(errors.invalidEmail);
		}
		
		if(isEmpty(address1)) {
			errorMessages.push(errors.noAddress1);
		}
		
		if(isEmpty(city)) {
			errorMessages.push(errors.noCity);
		}
		
		if(isEmpty(state)) {
			errorMessages.push(errors.noState);
		}
		
		if(isEmpty(postal)) {
			errorMessages.push(errors.noPostal);
		}
		
		if(isEmpty(synopsis)) {
			errorMessages.push(errors.noSynopsis);
		}	
	
		if(isEmpty(signature)) {
			errorMessages.push(errors.noSignature);
		}
		
		if(isEmpty(today)) {
			errorMessages.push(errors.noToday);
		} else if(!isValidUSDate(today)) {
			errorMessages.push(errors.invalidToday);
		}
		// More tests for other values
		
		if(errorMessages.length > 0) {
			var html = generateErrorHTML(errorMessages);
			$('#error').html(html);
			$('#error').show();
			
			var yPos = $('#error').offset().top - 30; 
			$('html,body').animate({scrollTop: yPos}, 500);
			
			return false;
		}
	
	});
	$('#error').hide();
	$('#dateCompleted').preserveDefaultText();

});

function isEmpty(value) {
	return (value == '');	
}

function isEmail(email) {
	var emailRegEx=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	if(emailRegEx.test(email)) {
		return true;
	}
	return false;
}

function isValidUSDate( strValue ) {
	/************************************************
	DESCRIPTION: Validates that a string contains only
	valid dates with 2 digit month, 2 digit day,
	4 digit year. Date separator can be ., -, or /.
	Uses combination of regular expressions and
	string parsing to validate date.
	Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy
	
	PARAMETERS:
	strValue - String to be tested for validity
	
	RETURNS:
	True if valid, otherwise false.
	
	REMARKS:
	Avoids some of the limitations of the Date.parse()
	method such as the date separator character.
	*************************************************/
	var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
	
	//check to see if in correct format
	if(!objRegExp.test(strValue))
	return false; //doesn't match pattern, bad date
	else{
	var strSeparator = strValue.substring(2,3) 
	var arrayDate = strValue.split(strSeparator); 
	//create a lookup for months not equal to Feb.
	var arrayLookup = { '01' : 31,'03' : 31, 
	                    '04' : 30,'05' : 31,
	                    '06' : 30,'07' : 31,
	                    '08' : 31,'09' : 30,
	                    '10' : 31,'11' : 30,'12' : 31}
	var intDay = parseInt(arrayDate[1],10); 
	
	//check if month value and day value agree
	if(arrayLookup[arrayDate[0]] != null) {
		if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
		    return true; //found in lookup table, good date
	}
	
	//check for February (bugfix 20050322)
	//bugfix  for parseInt kevin
	//bugfix  biss year  O.Jp Voutat
	var intMonth = parseInt(arrayDate[0],10);
	if (intMonth == 2) { 
	   var intYear = parseInt(arrayDate[2]);
	   if (intDay > 0 && intDay < 29) {
	       return true;
	   }
	   else if (intDay == 29) {
	     if ((intYear % 4 == 0) && (intYear % 100 != 0) || 
	         (intYear % 400 == 0)) {
	          // year div by 4 and ((not div by 100) or div by 400) ->ok
	         return true;
	     }   
	   }
	}
	}  
	return false; //any other values, bad date
}

function generateErrorHTML(messages) {
	var html 	= '<h3>';
	if(messages.length > 1){
		html 	+= 'Oops! We encountered some errors:';
	} else {
		html 	+= 'Oops! We encountered an error:';
	}
	html 		+= '</h3>\n<ul>';
	$.each(messages,function(i, message){
		html 	+= '	<li>'+message+'</li>';
	});
	html 		+= '</ul>';
	
	return html;
}
