// toggles default field values (focus/blur), performs validation and submits form

// requires validationHelpers.js
// requires jquery.sysmessage.js
// requires jquery.cookie.js for non-ajax form submission
// requires ieHover.js to append .hover state class for IE

(function($) { 
        
        // plugin definition 
        $.fn.validate = function(options) { 
        
			// build main options before element iteration 
			var opts = $.extend({}, $.fn.validate.defaults, options); 
	
			// iterate and reformat each matched element 
			return this.each(function() { 
				$this = $(this); 
				// build element specific options 
				var o = $.metadata ? $.extend({}, opts, $this.metadata()) : opts; 

				//functionality
				
				//array to store default field vals
				var toggleVals = [];
			
				//Store the default value for each field
				$('input[type=text]').each(function() {
					toggleVals[$(this).attr('id')] = $(this).attr('value');
				});

				toggleText();
								
				$this.submit(function(){
					var valid = true;	
					var emailValid = true;
					$("#Error").remove();
					
					$("input[type=text]").each(function(){
						if ($(this).hasClass("required")){
							if($(this).val() == document.getElementById($(this).attr("id")).defaultValue || isEmpty($(this).val())){
								addAlert(this);
								valid = false;
							}
						}
						
						if ($(this).hasClass("email")){
							if(!isValidEmail($(this).val(), true)) {					
								addAlert(this);
								emailValid = false;
							}
						}	
						
						/*if ($("#nobot").val()!=''){
							valid = false; 	
						}*/
					
						
					});
					
					if (!valid) {	
						$(o.submitId).after("<div id='Error'><p>* Please fill in the required fields.</p></div>");
						return false; 
					}
					else if (!emailValid){
						$(o.submitId).after("<div id='Error'><p>* Please provide a valid email address.</p></div>");
						return false; 		
					}
					else {

						$.cookie("success", "true", 1);
						
						return true;
					}
				});
				
				function toggleText(){
					//Add focus and blur events to set or clear the value
					$('input[type=text]').focus(function() {
						if($(this).val() == toggleVals[$(this).attr("id")] || $(this).hasClass(o.alertClass)) {
							$(this).val('');
							if($(this).hasClass(o.alertClass)){
								toggleCss(this, false);
							}
						}
					});
					
					$('input[type=text]').blur(function() {
						if($(this).val() == '') {
							//persist error if it existed
							if($(this).hasClass("alerted")){
								$(this).addClass(o.alertClass);
								toggleCss(this, true);
							}
							$(this).val(toggleVals[$(this).attr('id')]);
						}
					});
				};
				
				//Remove error class onFocus
				$('input[type=text]').focus(function() {
					if($(this).hasClass(o.alertClass)) {
						$(this).removeClass(o.alertClass);
					}
				});
				
				function addAlert(elem){
					$(elem).addClass(o.alertClass + " alerted");
					toggleCss(elem, true);
				};
				
				function toggleCss(elem, error){
					if(error){
						$(elem).css({ 
							paddingLeft: '14px',
							width: '161px'
						});
					}
					else{
						$(elem).css({ 
							paddingLeft: '5px',
							width: '170px'
						});
					}
				}
				
				//target < IE7 to append .hover to submit button
				if ($.browser.msie && $.browser.version < 7) {	
					 toggleSubmit(o.submitId);
				}

				///
			}); 
        }; 
        
        // plugin defaults 
        $.fn.validate.defaults = { 
				alertClass: 'alert',
				formAction: '#',
				formMethod: 'POST',
				submitId: '#Submit',
				formParentId: '#Secondary'
        }; 
})(jQuery); 