var zChar = new Array(' ', '(', ')', '-', '.');
var maxphonelength = 14;
var phonevalue1;
var phonevalue2;
var cursorposition;

$(document).ready(function() {
	$('[required=1]').each(function() {
		if (!$(this).next('[rel=Error]').length) {
			$(this).after('<div class="BadNews" rel="Error"><!-- --></div>');
		}
		$(this).bind('blur',function() {
			var FullString = $(this).val();
			var NewString = FullString.split(' ').join('');
			var curchr = NewString.length;
			//console.log($(this).attr('name')+" : "+curchr);
			$(this).removeClass('Invalid');
			$(this).next().html('');
			$(this).next().css({'height':'0px'});
			if (curchr < 1 || curchr == 'undefined') {
				$(this).addClass('Invalid');
				$(this).next().html('The '+$(this).attr('name').replace('_',' ')+' field is required');
				$(this).next().css({'height':'auto'});
			}
		});
	});
	$('[rel=Name]').each(function() {
		$(this).bind('blur',function() {
			CapitalizeFirstLetters($(this));
		});
	});
	$('[rel=Email]').each(function() {
		if (!$(this).next('[rel=Error]').length) {
			$(this).after('<div class="BadNews" rel="Error"><!-- --></div>');
		}
		$(this).bind('blur',function() {
			IsValidEmailAddress($(this));
		});
	});
	$('[rel=Phone]').each(function() {
		if (!$(this).next('[rel=Error]').length) {
			$(this).after('<div class="BadNews" rel="Error"><!-- --></div>');
		}
		$(this).bind('keyup',function(e) {
			FormatPhoneNumber($(this), e);
		});
		$(this).bind('keydown',function(e) {
			var curchr = $(this).val().length;
			//console.log(e.keyCode);
			if (curchr < 14 || e.keyCode == 9 || e.keyCode == 46 || e.keyCode == 8) {
				NumberOnly(e);
			} else {
				e.preventDefault();
			}
		});
		$(this).bind('blur',function() {
			var curchr = $(this).val().length;
			$(this).removeClass('Invalid');
			$(this).next().html('');
			$(this).next().css({'height':'0px'});
			if (curchr < 14) {
				$(this).addClass('Invalid');
				$(this).next().html('You must enter your full 10 digit phone number');
				$(this).next().css({'height':'auto'});
			}
		});
	});
	$('[rel=Password]').each(function() {
		if (!$(this).next('[rel=Error]').length) {
			$(this).after('<div class="BadNews" rel="Error"><!-- --></div>');
		}
		$(this).bind('blur',function() {
			var curchr = $(this).val().length;
			$(this).removeClass('Invalid');
			$(this).next().html('');
			$(this).next().css({'height':'0px'});
			if (curchr < 6) {
				$(this).addClass('Invalid');
				$(this).next().html('Your password must be at least 6 characters');
				$(this).next().css({'height':'auto'});
			}
		});
	});
	$('[rel=Error]').each(function() {
		$(this).css({'height':'0px'});
	});
});

function CheckRequiredFields(Form) {
	$('#'+Form).find('[required=1]').each(function() {
		if (!$(this).next('[rel=Error]').length) {
			$(this).after('<div class="BadNews" rel="Error"><!-- --></div>');
		}
		var FullString = $(this).val();
		var NewString = FullString.split(' ').join('');
		var curchr = NewString.length;
		//console.log($(this).attr('name')+" : "+curchr);
		$(this).removeClass('Invalid');
		$(this).next().html('');
		$(this).next().css({'height':'0px'});
		if (curchr < 1 || curchr == 'undefined') {
			//console.log($(this));
			$(this).addClass('Invalid');
			$(this).next().html('The '+$(this).attr('name').replace('_',' ')+' field is required');
			$(this).next().css({'height':'auto'});
		}
	});
}

function FormatPhoneNumber(Field, Event) {
	var curchr = Field.val().length;
	var curval = Field.val();
	if (curchr == 3) {
		Field.val("(" + curval + ") ");
	} else if (curchr == 9) {
		Field.val(curval + "-");
	}
}

function NumberOnly(Event) {
	//console.log(Event.keyCode);
	if (Event.keyCode != 46 && Event.keyCode != 8 && Event.keyCode != 9 ) {
		if (Event.keyCode < 95) {
			if (Event.keyCode < 48 || Event.keyCode > 57 ) {
				Event.preventDefault();
			}
		} else {
			if (Event.keyCode < 96 || Event.keyCode > 105 ) {
				Event.preventDefault();
			}
		}
	} 
}

function IsValidEmailAddress(Field) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	Field.next().html('');
	Field.removeClass('Invalid');
	if (!pattern.test(Field.val())) {
		Field.addClass('Invalid');
		Field.next().css({'height':'auto'});
		Field.next().html('Invalid Email Address');
	}
}

function CapitalizeFirstLetters(Field) {
	var mystring = Field.val();
	var sp = mystring.split(' ');
	var wl=0;
	var f ,r;
	var word = new Array();
	for (i = 0 ; i < sp.length ; i ++ ) {
		f = sp[i].substring(0,1).toUpperCase();
		r = sp[i].substring(1).toLowerCase();
		word[i] = f+r;
	}
	newstring = word.join(' ');
	Field.val(newstring);	
}
