jQuery.noConflict();


jQuery(document).ready(function(){  
	id = 0;
	jQuery(".openTime").click(function(){
			jQuery(this).prev(".openingTimes").append("<div class='formRow clearFix'><label for='newday" + id + "'>Day:</label><span class='formValue'><select id='newday" + id + "' name='newday" + id + "'><option value='monday'>Monday</option><option value='tuesday'>Tuesday</option><option value='wednesday'>Wednesday</option><option value='thursday'>Thursday</option><option value='friday'>Friday</option><option value='saturday'>Saturday</option><option value='sunday'>Sunday</option></select></span><label for='newopen" + id + "'>Open:</label><span class='formValue'><input id='newopen" + id + "' type='text' name='newopen" + id + "' size='50' maxlength='255' /></span><label for='newclose" + id + "'>Close:</label><span class='formValue'><input id='newclose" + id + "' type='text' name='newclose" + id + "' size='50' maxlength='255' /></span><label class='closed' for='newclosed" + id + "'>Closed?:</label><span class='formValue'><input id='newclosed" + id + "' type='checkbox' class='checkbox' name='newclosed" + id + "' /></span></div>");
			id = (id + 1);	
			jQuery(this).next(".timeCount").val(id);	
	});
	
	cid = 0;
	jQuery(".addCategory").click(function(){
			jQuery(this).next("#categorys").append("<div class='formRow clearFix'><label for='newCategory" + cid + "'>Category:</label><span class='formValue'><select id='newCategory" + cid + "' name='newCategory" + cid + "'><option value='Anxiety/Phobias'>  Anxiety/Phobias </option><option value='Carers'> Carers </option><option value='Crisis'> Crisis </option><option value='Depression'> Depression </option><option value='Eating Disorders'> Eating Disorders </option><option value='Faith'> Faith </option><option value='Families and Young People'> Families and Young People </option><option value='General Mental Health'> General Mental Health </option><option value='Hearing Voices'> Hearing Voices </option><option value='Minority/Ethnic Groups'> Minority/Ethnic Groups </option><option value='Post Traumatic Stress'> Post Traumatic Stress </option><option value='Schizophrenia'> Schizophrenia </option><option value='Women'> Women </option></select></span></div>");
			cid = (cid + 1);	
			jQuery(this).prev("#newCategoryCount").val(cid);	
			
			jQuery(this).next(".timeCount").val(id);	
	});

	function isValidEmailAddress(emailAddress) {
		var pattern = new RegExp(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/);
		return pattern.test(emailAddress);
	}

  	var error = false;
	/** 
	 * Character Counter for inputs and text areas 
	 */  
	jQuery('.word_count').each(function(){  
	    // get current number of characters  
	    var length = jQuery(this).val().length;  
	    // get current number of words  
	    //var length = jQuery(this).val().split(/\b[\s,\.-:;]*/).length;  
	    // update characters  
	    jQuery(this).parent().find('.counter').html( length );  
	    // bind on key up event  
	    jQuery(this).keyup(function(){  
	        // get new length of characters  
	        var new_length = jQuery(this).val().length;  
	        // get new length of words  
	        //var new_length = jQuery(this).val().split(/\b[\s,\.-:;]*/).length;  
	        // update  
	        jQuery(this).parent().find('.counter').html( new_length );  
	        
	        if (jQuery(this).hasClass("limit")) {
	        	if(new_length > 81) {
	        		jQuery(this).addClass("error");	        	
		    	}
		    	else {
		    		jQuery(this).removeClass("error");
		    	}
	        }
	        
	    }); 
	  
	});  
	
	jQuery('.errorMsg').hide();
	
	
	jQuery(function() {
        jQuery(".required").blur(function() {      
     		if (jQuery(this).val() == "") { 
       			jQuery(this).addClass("error").next(".errorMsg").html("This field is required").show();
     		} 
     		else if (jQuery(this).hasClass("requiredEmail")) {
     			jQuery(".requiredEmail").blur(function(){
					var email = jQuery(this).val();
					if(!isValidEmailAddress(email)) {
						jQuery(this).addClass("error").next(".errorMsg").html("This is not a proper email format").show();
		 			}
		 			else if (jQuery(this).val() == "") {
		 				jQuery(this).next(".errorMsg").html("This field is required");
		 			}
		 			else {
		 				jQuery(this).removeClass("error").next(".errorMsg").hide();
		 				error = 0;
		 			}
				});
     		}
     		else if (jQuery(this).hasClass("password")) {
     			var password = jQuery("#password").val()
				var cpassword = jQuery("#cPassword").val()
     			
     			
				if (jQuery(this).val().length < 6) {
     				jQuery(this).addClass("error").next(".errorMsg").html("Password is too short").show();
     			}     			
     			else if (password !== cpassword) {
     				jQuery(this).addClass("error").next(".errorMsg").html("Passwords do not match").show();
     			}
     			else {
     				jQuery(this).removeClass("error").next(".errorMsg").hide();
     				jQuery(".password").removeClass("error").next(".errorMsg").hide();
     				error = 0;
     			}
     		}
     		else {
     				jQuery(this).removeClass("error").next(".errorMsg").hide();
     				error = 0;
     		}
    	});
     });  
     
     	
	
	jQuery("form").submit(function() {
		jQuery(".required").each(function() {
	    	if (jQuery(this).hasClass("error")) {
		    	error = 1;
		    	jQuery(this).addClass("error").next(".errorMsg").show();
	    	}
	    });
	    jQuery(".word_count").each(function() {
	    	if (jQuery(this).hasClass("error")) {
		    	error = 1;
		    	jQuery(this).addClass("error").next(".errorMsg").show();
	    	}
	    });
	    if (error == 1) {
	    	alert("Some information is missing or incorrect. Please ammend the highlighted fields.");
	    	return false;
	    }
	});
				
}); 



					
				