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><option value='bank holidays'>Bank Holidays</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><label class='moreinfo' for='newlabel" + id + "'>More info:</label><span class='formValue moreinfo'><input id='newlabel" + id + "' type='text' name='newlabel" + id + "' size='50' maxlength='255' /></span><label class='override' for='newoverride" + id + "'>Override Day?:</label><span class='formValue'><input id='newoverride" + id + "' type='checkbox' class='checkbox' name='newoverride" + 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='Addictive behaviour'>Addictive behaviour</option><option value='All' selected='selected'>All</option><option value='Anxiety disorders (including panic attacks and OCD)'>Anxiety disorders (including panic attacks and OCD)</option><option value='Bereavement'>Bereavement</option><option value='Carers/Young Carers'>Carers/Young Carers</option><option value='Crisis'>Crisis</option><option value='Depression (including bi-polar and post natal)'>Depression (including bi-polar and post natal)</option><option value='Eating Disorders'>Eating Disorders</option><option value='Faith'>Faith</option><option value='Families'>Families</option><option value='General Mental Health'>General Mental Health</option><option value='Men'>Men</option><option value='Minority and Ethnic Groups (BME)'>Minority and Ethnic Groups (BME)				        	</option><option value='Post Traumatic Stress'>Post Traumatic Stress</option><option value='Schizophrenia (including hearing voices and disturbing thoughts)'>Schizophrenia (including hearing voices and disturbing thoughts)</option><option value='Self Harm'>Self Harm</option><option value='Stress and emotional distress'>Stress and emotional distress</option><option value='Suicide'>Suicide</option><option value='Women'>Women				        	</option><option value='Young People'>Young People</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;
	    }
	});
				
}); 



					
				
