// Grendelfly - A Division of SC&G Technology Solutions
// Form Validator Plugin // GRNDL V2.R1.2011.06
// Purpose: Validate all types of entries before submitting form.
// Requires: JQuery (jquery-1.3.2.min.js or above)
// (c) 2011 Grendelfly


$(function(){
$(document).ready(function() {

// Place (*) in label of required fields

	$('label').each( function(i,e){
							  
		var g_id = $(e).attr('for');
		var g_tx = $(e).html().replace(/:/g,'');
		var g_cs = $('#' + g_id).attr('class') + '';
		
		if( g_cs.indexOf('required') != -1 ){
			$(e).before('<span class="req">* </span>');
			$('form span.req').css({'font-weight':'bold','color':'#900','vertical-align':'text-top'});
			$('#' + g_id).attr({'rel':g_tx});
			}; // End if
			
	}); // End $('label').each


// Puts id on form if no id is already given

	$('form').each( function(i,e){
		if(e.id == null || e.id ==''){ $(e).attr({'id':'form_' + i}); };
	});


// Form Validation
	$('input[type="submit"]').click(function() {

		var g_frm_id = $(this).parents('form').attr('id'); // Get Form ID
		var ErrStr = "";
		var g_offset = 0;
		$('input[type="text"],select,textarea').css({'background-color':'white'}); // Reset all inputs to normal
	
		$('#' + g_frm_id + ' .required').each( function(i,e){
			var g_id  = e.id;
			var g_val = $(e).val();
			
			if( g_val == "" ){				
				ErrStr = ErrStr + 'Missing: ' + $(e).attr('rel') + '\n';
				$(e).css({'background-color':'pink'}); 
				if(g_offset == 0){g_offset = $(e).offset().top - 50;};
			};
				
		}); // End $('#' + g_frm_id + ' .required').each
		
		if(ErrStr != ""){
			alert("Error In Form!\nPlease fill out all required fields.");
			$('html:not(:animated),body:not(:animated)').animate({ scrollTop: g_offset-20}, 500 );
			return false;
			};

	}); // End $('input[type="submit"]').click

// Phone Validation
	$('.val_phn').change( function(){
		var g_val = $(this).val().replace(/\D/g,'');
		var g_3 = ''; // last 4
		var g_2 = ''; // 3
		var g_1 = ''; // area code
		var g_0 = ''; // 1
		
		if(g_val.length >= 7) { g_3 = g_val.slice(g_val.length-4); };
		if(g_val.length >= 7) { g_2 = g_val.slice(g_val.length-7,g_val.length-4);     if(g_2 != ""){g_2 = g_2 + '-'};          };
		if(g_val.length >= 10){ g_1 = g_val.slice(g_val.length-10,g_val.length-7);    if(g_1 != ""){g_1 = '(' + g_1 + ') '};   };
		if(g_val.length >= 11){ g_0 = g_val.slice(g_val.length-11,g_val.length-10);   if(g_0 != ""){g_0 = g_0 + ' '};          };
			
		$(this).val(g_0 + g_1 + g_2 + g_3);
		if(g_val.length < 7){ alert('Please supply a proper phone number.'); };
		
		});

// Zip Validation
	$('.val_zip').change( function(){
		var g_val = $(this).val().replace(/\D/g,'');		
		$(this).val(g_val);
		if(g_val.length < 4){ alert('Please supply a proper zip code.'); };
		});

// Email Validation
	$('.val_eml').change( function(){
		var g_val = $(this).val()
			.replace(/@/,'xXx')
			.replace(/\./,'xDx')
			.replace(/\W/g,'')
			.replace(/xXx/,'@')
			.replace(/xDx/,'.');		
		$(this).val(g_val);
		if(g_val.length < 6 || g_val.indexOf('@') == -1 || g_val.indexOf('.') == -1 ){ alert('Please supply a proper email address.'); };
		});

}); // $(document).ready(function()
}); // $(function()
