// JavaScript Document

//News tag System
//Checks to ensure the user adds a proper tag name
$(document).ready( function(){
      //set the focus on the first input element
	   $(':input:first').focus();
 
	  
	  //detect button click
     $("#add_tag").click(function() {
		 var tagName = $("input#new_tag").val();

		 if(tagName =='Add new tag'){
			 
			 $("#tag_alert").append("please enter a valid tag name");
		 
		 }else{
			//submit ajax
			   var tagname = $("input#new_tag").val();

			   var dataString = 'tagname='+tagname+'&action='+'addtag1';

			   $.ajax({
               type: "GET",
               url: "news/news.processnews.php",
               data: dataString,
               success: function(msg){
				   
				      $("#taglist").ajaxComplete(function(event,request, settings){
						        if(msg =='failure'){
					                       $("#tag_alert").append("Cannot complete your request!");
					             }else{
						   
						                  $(this).html(msg); 
					             }
                          
                        });
							   
			   }
			   
			   });
				 
				 
		 }
 	
	 });//end button click function


});//end document ready function

function clearDefault(){
	   $("#new_tag").val("");
	}	
	
	function clearSearchHint(){
	   $("#searchQuery").val("");
	   $("#searchQuery").css("color","#333")
	   
	}	
	
	function clearSearchHint2(){
	   $("#searchQuery2").val("");
	   $("#searchQuery2").css("color","#333")
	   
	}	
////////////////////////////////////////////////////////////////////
//process add user form
$(document).ready(function(){
   $('#adduser').submit(function(){
    //$('#username_error').append('This is a required field');
	
	//setting ajax variables
	 var  formData = $(this).serialize();
     var  formUrl  = 'user/user.processuser.php';  
        
		
		//do our ajax stuff here
        $.ajax({
               type: "GET",
               url: formUrl,
               data: formData,
               success: function(response){
				   
				   //setup variables  
                  var replyData = jQuery.parseJSON(response);
				   
				      if(replyData.result=='success'){
					        window.location = replyData.location;
				      }else{
						   					  
							$('#username_error').empty();
							$('#email_error').empty();
							$('#password_error').empty();
							if(replyData.error.username){
								$('#username_error').append('This is a required field');
								
								} 
								 if(replyData.error.emailrequired){
									$('#email_error').append('This is a required field');
								}
								 else if(replyData.error.emailinvalid){
									 $('#email_error').append('Please enter a valid email');
									
								}
								
								if(replyData.error.passwordrequired){
									$('#password_error').append('This is a required field');
								}
								else if(replyData.error.passwordnotmatched){
									$('#password_error').append('The passwords entered did not match');
								}
					
				}
						
						
							   
			   }
			   
			   }); 
		 
		 
		 
        //prevent form from submitting
        return false;
    })
})


////////////////////////////////////////////////////////////////////
//process edit user form
$(document).ready(function(){
   $('#edituser').submit(function(){
    //$('#username_error').append('This is a required field');
	
	//setting ajax variables
	 var  formData = $(this).serialize();
     var  formUrl  = 'user/user.processuser.php';  
        
		
		//do our ajax stuff here
        $.ajax({
               type: "GET",
               url: formUrl,
               data: formData,
               success: function(response){
				   
				   //setup variables  
                  var replyData = jQuery.parseJSON(response);
				   
				      if(replyData.result=='success'){
					        window.location = replyData.location;
				      }else{
						   					  
							$('#username_error').empty();
							$('#email_error').empty();
							$('#password_error').empty();
							if(replyData.error.username){
								$('#username_error').append('This is a required field');
								
								} 
								 if(replyData.error.emailrequired){
									$('#email_error').append('This is a required field');
								}
								 else if(replyData.error.emailinvalid){
									 $('#email_error').append('Please enter a valid email');
									
								}
								
								/*if(replyData.error.passwordrequired){
									$('#password_error').append('This is a required field');
								}*/
								else if(replyData.error.passwordnotmatched){
									$('#password_error').append('The passwords entered did not match');
								}
					
				}
						
						
							   
			   }
			   
			   }); 
		 
		 
		 
        //prevent form from submitting
        return false;
    })
})

///////////////////////////////////////////////////////////////////////////
function checkUser(){
	$('#username_error').append('This is a required field');
	}
	
//////////////////////////////////////////////////////////////////////////////////////	
//
$(document).ready(function() {
		
			$(".add").click(function() {
				$(".dept:first").clone(true).insertBefore("p.add");
				return false;
			});
			
			$(".remove").click(function() {
				$(this).parent().remove();
			});
		
        });	
		
//////////////////////////////////////////////////////////////////////////
//Show add department form on when editing contact
$(document).ready(function() {
		
			$("a#add_department").click(function() {
				$(".dept_form").show();
				
				return false;
			});
			
			
		
        });
//Show admin  slideshow preview
$(document).ready(function() {
		
			$("a.preview_slideshow").click(function() {
				$("#slideshow_preview").toggle();
				
				return false;
			});
			
			
		
        });
//////////////////////////////////////////////////////////////////////////
//Show add city form on when adding new career opportunity
//$(document).ready(function() {
function shownewcityform(){						   
		
			//$("a.add_new_city").click(function() {
				$("#addcityform").show();
				$('.add_new_city,#cityID ').hide();
				//$('#cityID').hide();
				$("input#new_city").val('');
				return false;
			//});
			
			
		
        };
///////////////////////////////////////////////////////////////////////////		
//Show add position form on when adding new career opportunity
//$(document).ready(function() {
function shownewpositionform(){		
			//$("a.add_new_position").click(function() {
				$('.add_new_position, #positionID').hide();
				$("#addpositionform").show();
				$("input#new_position").val('');
				/*$('#cityID').attr("disabled","disabled");*/
				return false;
			//});
			
			
		
        };
/////////////////////////////////////////////////////////////////////////
//Add new position to database		
		
//Show add new title form on when adding new contact
$(document).ready(function() {
		
			$("a#title_add").click(function() {
				$("#add_new_title").show();
				$('#contact_title, a#title_add').hide();
				/*$('#cityID').attr("disabled","disabled");*/
				return false;
			});
			
			
		
        });
//Cancel add title and show title dropdown
$(document).ready(function() {
		
			$("a#cancel_add_title").click(function() {
				$("#add_new_title").hide();
				$('#contact_title, a#title_add').show();
				/*$('#cityID').attr("disabled","disabled");*/
				return false;
			});
			
			
		
        });
								
///////////////////////////////////////////////////////////////////////////		
//Show map on locations_list template
$(document).ready(function() {
		
			$("a#viewmap").click(function() {
				$("#locations_map").slideToggle('slow');
				
				if($(this).text() =='View Map'){
					$(this).text('Hide Map');
					}else{
						
						$(this).text('View Map');
						}
				return false;
			});
			
			
		
        });
						
////////////////////////////////////////////////////////////////////////////					
//Add new location and department to contact
function add_department_location(){
	//clear previous alert message if any
	$("#tag_alert").empty();
	
	
	var location     = $("#locationID").val();
	var department   = $("#depID").val();
	var contact      = $("#contactID").val();
	var formData     =('locationID='+location+'&depID='+department+'&contactID='+contact+'&action=ajaxadddepartment');
	var formUrl      = 'contacts/contacts.processcontact.php';
	
	//do all the ajax stuff
	$.ajax({
               type: "GET",
               url: formUrl,
               data: formData,
               success: function(msg){
				   
				   //setup variables 
				  //$("#tag_alert").empty();  
                 $("#department_content").ajaxComplete(function(event,request, settings){
						        if(msg =='failure'){
									        $("#tag_alert").empty();
					                       $("#tag_alert").append("Cannot complete your request!");
					             }else{
						                  //$(".dept_form").hide();
										  $("#tag_alert").empty();
						                  $(this).html(msg); 
					             }
                          
                        });
				   
				      
						
						
							   
			   }
			   
			   }); 
	
	}
////////////////////////////////////////////////////////////////////////////////////////	
function deleteDepartment(contact_id,department_id, location_id){
	     //get confirmation for user before proceeding 
	     if(confirm("Are you sure you want to delete this entry?")){
			 
			        var location     = location_id;
					var department   = department_id;
					var contact      = contact_id;
					var formData     =('locationID='+location+'&depID='+department+'&contactID='+contact+'&action=ajaxdeletedepartment');
					var formUrl      = 'contacts/contacts.processcontact.php';
					
					//do all the ajax stuff
					$.ajax({
							   type: "GET",
							   url: formUrl,
							   data: formData,
							   success: function(msg){
								   
								   //setup variables 
								  //$("#tag_alert").empty();  
								 $("#department_content").ajaxComplete(function(event,request, settings){
												if(msg =='failure'){
															$("#delete_alert").empty();
														   $("#delete_alert").append("Cannot complete your request!");
												 }else{
														  //$(".dept_form").hide();
														  $("#delete_alert").empty();
														  $(this).html(msg); 
												 }
										  
										});
								   
									  
										
										
											   
							   }
							   
							   }); 
			 
			 }
	
	
	return false;
	}
	
////////////////////////////////////////////////////////////////////////////////////////	
function deleteContact(contact_id){
	     //get confirmation for user before proceeding 
	     if(confirm("Are you sure you want to delete this contact?")){
			 
			        var contact      = contact_id;
					var formData     =('contactID='+contact+'&action=ajaxdeletecontact');
					var formUrl      = 'contacts/contacts.processcontact.php';
					var deleteRow    = 'contact_'+contact;
					//do all the ajax stuff
					$.ajax({
							   type: "GET",
							   url: formUrl,
							   data: formData,
							   success: function(msg){
								   
								   //setup variables 
								  //$("#tag_alert").empty();  
								 $(".notification_box").ajaxComplete(function(event,request, settings){
												if(msg =='failure'){
															alert('A problem occured while trying to delete that contact. Please try again later.');
												 }else{
														  //$(".dept_form").hide();
														  $("#delete_alert").empty();
														  //$(this).html(msg); 
														  $(this).show();
														  $('#'+deleteRow).hide();
												 }
										  
										});
								   
									  
										
										
											   
							   }
							   
							   }); 
			 
			 }
	
	
	return false;
	}	
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/*function checkContactForm(){
	var firstName =$('#first_name').val();
	
	if(firstName ==''){
		  $('#form_error').append('Please enter a first name.');
		return false;
		}
	
	}	*/
//process add user form
function processcontact(){
	 var  contactData = $('#contactform').serialize();
     var  contactUrl  = 'processcontact.php';
	 
	 $.ajax({
		       type: "GET",
               url: contactUrl,
               data: contactData,
               success: function(response){
				   
				   
				   
				   }
               
	      });
}

////////////////////////////////////////////////////////////////////////////					
//Add new location and department to contact
function addNewTitle(){
	//clear previous alert message if any
	$("#tag_alert").empty();
	
	
	var title     = $("#new_title").val();
	var formData     =('newtitle='+title+'&action=addtitle');
	var formUrl      = 'contacts/contacts.processcontact.php';
	
	//do all the ajax stuff
	$.ajax({
               type: "GET",
               url: formUrl,
               data: formData,
               success: function(msg){
				   
				   //setup variables 
				  //$("#tag_alert").empty();  
                 $("#contact_title").ajaxComplete(function(event,request, settings){
						        if(msg =='failure'){
									        $("#tag_alert").empty();
					                       $("#tag_alert").append("Please enter a valid title!");
					             }else{
						                  $("#add_new_title").hide();
										  $("#tag_alert").empty();
										  $(this).show();
						                  $(this).html(msg); 
					             }
                          
                        });
				   
				      
						
						
							   
			   }
			   
			   }); 
	
	}
////////////////////////////////////////////////////////////////////////
// add highlight to table rows
//$(document).ready(function(){
//	$('.simplehighlight').hover(function(){
//		$(this).children().addClass('datahighlight');
//		},function(){
//			$(this).children().removeClass('datahighlight');
//		});
 //     });

/////////////////////////////////////////////////////////////////////
$(".simplehighlight td").hover(
  function () {
    $(this).addClass("datahighlight");
  },
  function () {
    $(this).removeClass("datahighlight");
  }
);


function limitChars(textarea, limit, infodiv)
{
	var text = textarea.value;	
	var textlength = text.length;
	var info = document.getElementById(infodiv);

	if(textlength > limit)
	{
		info.innerHTML = 'You cannot write more then '+limit+' characters!';
		textarea.value = text.substr(0,limit);
		return false;
	}
	else
	{
		info.innerHTML = 'You have '+ (limit - textlength) +' characters remaining.';
		return true;
	}
}
//Add new location in Employment
//Checks to ensure the user adds a proper tag name
$(document).ready( function(){
      //set the focus on the first input element
	   //$(':input:first').focus();
 
	  
	  //detect button click
     $("#add_city").click(function() {
		 var cityName = $("input#new_city").val();

		 if(cityName ==''){
			 
			 $("#city_alert").append("please enter a valid city name");
		 
		 }else{
			//submit ajax
			   var cityname = $("input#new_city").val();

			   var dataString = 'cityname='+cityname+'&action='+'addcity1';

			   $.ajax({
               type: "GET",
               url: "employment/employment.processjob.php",
               data: dataString,
               success: function(msg){
				   
				      $("#citylist").ajaxComplete(function(event,request, settings){
						        if(msg =='failure'){
					                       $("#city_alert").append("Cannot complete your request!");
					             }else{
						                  $('#addcityform').hide();
						                  $(this).html(msg); 
					             }
                          
                        });
							   
			   }
			   
			   });
				 
				 
		 }
 	
	 });//end button click function


});//end document ready function
//Add new position for employment
//Checks to ensure the user adds a proper tag name
$(document).ready( function(){
      //set the focus on the first input element
	   //$(':input:first').focus();
 
	  
	  //detect button click
     $("#add_position").click(function() {
		 var positionName = $("input#new_position").val();

		 if(positionName ==''){
			 
			 $("#position_alert").append("please enter a valid position name");
		 
		 }else{
			//submit ajax
			   //var cityname = $("input#new_city").val();

			   var dataString = 'positionname='+positionName+'&action='+'addposition1';

			   $.ajax({
               type: "GET",
               url: "employment/employment.processjob.php",
               data: dataString,
               success: function(msg){
				   
				      $("#positionlist").ajaxComplete(function(event,request, settings){
						        if(msg =='failure'){
					                       $("#position_alert").append("Cannot complete your request!");
					             }else{
						                  $('#addpositionform').hide();
						                  $(this).html(msg); 
					             }
                          
                        });
							   
			   }
			   
			   });
				 
				 
		 }
 	
	 });//end button click function


});//end document ready function

