 $(document).ready(function() {
 //only run on the page with post
 if ($("#update-directory-form").length > 0){
	//Add collapsible areas
	$('fieldset.sub').hide();
	$('.button').button();
	$('.subheader').click(function(){
		if($.browser.msie){
			$(this).next('.sub').show();
		}
		else{
			$(this).next('.sub').slideToggle();
		}
		
	});	
	//Setup add another contact	
	$( "#othercontactbutton" ).button({icons: {primary: "ui-icon-plusthick"}});
	$("#othercontactbutton").click(function(){
		var othercontactNum = $('.othercontact').length;
		var newOthercontactNum = othercontactNum+1;
		if(newOthercontactNum > 30){alert('You cannot add more than 30 users at a time, please resubmit the form if you need to add more.');}
		else{
			var addContactHTML = '<br /><br /><input id="add'+newOthercontactNum+'" type="radio" name="Title'+newOthercontactNum+'" value="Add" />Add&nbsp;&nbsp;&nbsp;<input id="change'+newOthercontactNum+'" type="radio" name="Title'+newOthercontactNum+'" value="Change" />Change&nbsp;&nbsp;&nbsp;<input id="remove'+newOthercontactNum+'" type="radio" name="Title'+newOthercontactNum+'" value="Remove" />Remove<br /><br />Provide: Title, name, Telephone, and Email.<br /><textarea id="details'+newOthercontactNum+'" name="Title1details'+newOthercontactNum+'" rows="4" class="othercontact" cols="36"></textarea><br /><br />'
			var curhtml = $('#othercontactWrapper').html();
			$("#othercontactWrapper").html(curhtml+addContactHTML);	
		}		
	});
	//Add default text to EDIContact textarea
	var EDIContactdefault_value = "Name, Phone ,Email";
	$("#EDIContact").val(EDIContactdefault_value)		
	$("#EDIContact").css('color', '#888')
		.focus(function() {
			if($(this).val() == EDIContactdefault_value) {
				$(this).val('').css('color', '#252525');
			}
		})
		.blur(function() {
			if($(this).val() == '') {
				$(this).val(EDIContactdefault_value).css('color', '#888');
			}
		});	
	//Submit
	$('#submit').click(function(event){
		if($("select[name='institute']").val() == ''){
			$("select[name='institute']").after('<p style="color:red;"><strong>This filed is required</strong></p>');
			 event.preventDefault();
		}	
		if($("input[name='submit_by']").val() == ''){
			$("input[name='submit_by']").after('<p style="color:red;"><strong>This filed is required</strong></p>');
			event.preventDefault();
		}	
	});
	//add FFF
	$("select[name='institute']").focus();
	
	}
});
