/* All form and other validations for MeritBuilder */


$(document).ready(function(){ 


	// overriding the default options,
	// specifying the element type and class for the counter,
	// custom text for the counter message, disabling
	// the pulse effect, and specifying a delay for the
	// counter update






	
	// Verification validations
	 $('form[name=validateStep1]').validate({
	  rules: {
	    fname: {
	      required: true
	    },
		lname: {
			required: true
		}
	  },
	   messages: {
			fname: {
				required: 'Please enter your first name.'
			},
			lname: {
				required: 'Please enter your last name.'
			}
		} 
	  
	 });
	
	
	
	
		/* add skill validation */
		 $('form[name=addSkill]').validate({
		  rules: {
		    label: {
		      required: true
		    }
		  },
		   messages: {
				label: {
					required: 'Please enter a name for your skill. You cannot change this.'
				}
			} 
		  
		 });
		
		
		/* send Merit validations */
		 $('form[name=sendMeritForm]').validate({
		  rules: {
		    toIdentificationInfo: {
		      required: true,
		      email: true
		    },
		    messagebody: {
		      required: true
		    },
		    ammount: {
		      number: true
		    }    
		  }
		 });
		
		 
		
		/* Profile_top send merit  */
		$('form[name=sendMeritFromProfile]').validate({
		  rules: {
		    fromIdentificationInfo: {
		      required: true,
		      email: true
		    },
		    messagebody: {
		      required: true
		    },
		    ammount: {
		      number: true
		    }  
		  }
		 });
		 
		 
		 
		/* add new email form validation  */
		$('form[name=newEmail]').validate({
		  rules: {
		    email: {
		      required: true,
		      email: true
		    }    
		  },
		  
		  messages: {
				email: {
					required: 'Please enter an e-mail address.',
					email: 'Please enter valid email.', 
					remote: jQuery.format("Email {0} is already used.")	
				}
			} 
		
		  
		  
		 });
		 
		 
		 /* add new space form validation  */
		$('form[name=profileNew]').validate({
		  rules: {
		    label: {
		      required: true
		  	 }    
		  },
		  
		  messages: {
				label: {
					required: 'Please enter a name for your space.'
				}
			}   
		 });
		  
		
		
		/* Signup validation */
		$('form[name=signup]').validate({
			messages: {
				username: {
					required: 'Please enter your username.',
					remote: jQuery.format("Username {0} is already taken, please enter a different username."), 
					minlength: "Your username must consist of at least 3 characters"
				}, 
				email: {
					required: 'Please enter your email.',
					email: 'Please enter valid email.', 
					remote: jQuery.format("Email {0} is already used.")	
				}, 
				ccnum : {
					required: 'Valid cc num required.',
					creditcard: 'This is not a valid credit card number.'
				}, 
				captcha: {
					required: 'Please enter captcha code.',
					remote: 'Invalid captcha code.'
				}
			} 
			
			
		});
		
		$('form[name=signup] #username').keyup($.fn.specialChar);
		$('form[name=signup] #username').keyup(function(){
			var x = $(this).val();
			$('#usernameEnter').html(x).css({'font-weight':'bold'});
		});
		
		
		
		
		
		$.fn.specialChar = function() {
			var x = $(this).val().replace(/[^a-z_\u00D1\u00F10-9]*/ig,'');
			$(this).val(x);
		};
		
		
		
		
		/**
		 *
		 * Copyright (c) 2007 Tom Deater (http://www.tomdeater.com)
		 * Licensed under the MIT License:
		 * http://www.opensource.org/licenses/mit-license.php
		 * 
		 */

			/**
			 * attaches a character counter to each textarea element in the jQuery object
			 * usage: $("#myTextArea").charCounter(max, settings);
			 */

			$.fn.charCounter = function (max, settings) {
				max = max || 100;
				settings = $.extend({
					container: "<span></span>",
					classname: "charcounter",
					format: "(%1 characters remaining)",
					pulse: true,
					delay: 0
				}, settings);
				var p, timeout;

				function count(el, container) {
					el = $(el);
					if (el.val().length > max) {
					    el.val(el.val().substring(0, max));
					    if (settings.pulse && !p) {
					    	pulse(container, true);
					    };
					};
					if (settings.delay > 0) {
						if (timeout) {
							window.clearTimeout(timeout);
						}
						timeout = window.setTimeout(function () {
							container.html(settings.format.replace(/%1/, (max - el.val().length)));
						}, settings.delay);
					} else {
						container.html(settings.format.replace(/%1/, (max - el.val().length)));
					}
				};

				function pulse(el, again) {
					if (p) {
						window.clearTimeout(p);
						p = null;
					};
					el.animate({ opacity: 0.1 }, 100, function () {
						$(this).animate({ opacity: 1.0 }, 100);
					});
					if (again) {
						p = window.setTimeout(function () { pulse(el) }, 200);
					};
				};

				return this.each(function () {
					var container = (!settings.container.match(/^<.+>$/)) 
						? $(settings.container) 
						: $(settings.container)
							.insertAfter(this)
							.addClass(settings.classname);
					$(this)
						.bind("keydown", function () { count(this, container); })
						.bind("keypress", function () { count(this, container); })
						.bind("keyup", function () { count(this, container); })
						.bind("focus", function () { count(this, container); })
						.bind("mouseover", function () { count(this, container); })
						.bind("mouseout", function () { count(this, container); })
						.bind("paste", function () { 
							var me = this;
							setTimeout(function () { count(me, container); }, 10);
						});
					if (this.addEventListener) {
						this.addEventListener('input', function () { count(this, container); }, false);
					};
					count(this, container);
				});
			};
			
			
			
			//initialize the about counter

			$("#about").charCounter(150);
		
		
		
});