jQuery.fn.fadeToggle = function(speed, easing, callback) {
    return this.animate({opacity: 'toggle'}, speed, easing, callback);
};




//results per 'page' when there is paginated data
var perpage = 10;
var currentUrl = window.location.pathname;

$(document).ready(function(){
	setInterval(function(){ $(".success").fadeOut(500);}, 5000);
	$(document).pngFix();
	
	$("#fgtPwd").click(function(){
		$("#fgtPwdDiv").fadeToggle(250);
	return false; 
	})
	
	$(".fgtPwd").click(function(){
		$(".fgtPwdDiv").fadeIn(250);
		$("#emailPwdBtn").show();
		$("#emailActBtn").hide();
	return false; 
	})
	
	$(".fgtAct").click(function(){
		$(".fgtPwdDiv").fadeIn(250);
		$("#emailPwdBtn").hide();
		$("#emailActBtn").show();
	return false; 
	})
	
	$("#group").change(function(){
		if($("#group").val()=='none'){
			$("#cus_group, .cus_group").show();
		}else{
			$("#cus_group, .cus_group").hide();
		}
	return false; 
	})

	
	
	$(".hide").click(function(){
		$("#topControls").slideUp(700);	
		setTimeout(function(){ $(".hide").fadeOut(250);}, 700);
		setTimeout(function(){ $("#showHide").addClass('closed'); $(".show").fadeIn(250);}, 950);
		$.post("includes/sessions.php", {topControls: 1}, function(data){$("#welcome").html(data);});
	return false; 
	})
	
	$(".show").click(function(){
		$("#topControls").slideDown(700);	
		setTimeout(function(){ $(".show").fadeOut(250);}, 700);
		setTimeout(function(){ $("#showHide").removeClass('closed'); $(".hide").fadeIn(250);}, 950);
		$.post("includes/sessions.php", {topControls: 0}, function(data){ $("#welcome").html(data);/*alert("data " + data);*/ });
	return false; 
	})
	
	$(".accept").click(function(){
		$("#topControls").slideDown(700);	
		setTimeout(function(){ $(".show").fadeOut(250);}, 700);
		setTimeout(function(){ $("#showHide").removeClass('closed'); $(".hide").fadeIn(250);}, 950);
		$.post("includes/sessions.php", {topControls: 0}, function(data){ $("#welcome").html(data);/*alert("data " + data);*/ });
	return false; 
	})
	
	$("#fieldType").change(function(){
		var value = $(this).val();
		var question = $("#question").val();
		$("#fieldOptionsDiv").hide();
		
		$("#exampleField").empty();
		$("#exampleField").append('<span class="fieldLabel">'+ question +'</span>');
		if(value==1){
			$("#exampleField").append('<input class="field1" readonly="readonly" type="text" />');
		}else if(value==2){
			$("#exampleField").append('<textarea class="field2" style="width:470px;"></textarea>');
		}else if(value==3){
			$("#exampleField").append('<input type="radio" />Option 1 <input type="radio" />Option 2');
			$("#fieldOptionsDiv").fadeIn(250);
		}else if(value==4){
			$("#exampleField").append('<select class="field1" id="exampleSelect"><option>Please Select...</option></select>');
			$("#fieldOptionsDiv").fadeIn(250);
		}else if(value==5){
			$("#exampleField").append('<input type="checkbox" />Option 1 <input type="checkbox" />Option 2');
			$("#fieldOptionsDiv").fadeIn(250);
		}
	return false; 
	})
	
	$(".custom_field2").click(function(){
		
		var fieldtype = $("#fieldType").val();
		if(fieldtype==1){
			$("#exampleSelect").empty();
			
			$("#exampleField").empty();
			$("#exampleField").append('<span class="fieldLabel">'+ $("#question").val() +'</span>');
			$("#exampleField").append('<input class="field1" readonly="readonly" type="text" />');

		}else if(fieldtype==2){
			$("#exampleSelect").empty();
			$("#exampleField").empty();
			$("#exampleField").append('<span class="fieldLabel">'+ $("#question").val() +'</span>');
			$("#exampleField").append('<textarea class="field2" style="width:470px;"></textarea>');
		}else if(fieldtype==3){
			$("#exampleSelect").empty();
			var fieldOptions = $("#fieldOptions").val().split("\n");
			$("#exampleField").empty();
			$("#exampleField").append('<span class="fieldLabel">'+ $("#question").val() +'</span>');
			
			for(option in fieldOptions){
				$("#exampleField").append('<input type="radio" name=\"exampleRadio\" /> '+ fieldOptions[option] +'  ');	
			}
		}else if(fieldtype==4){
			$("#exampleField").empty();
			$("#exampleField").append('<span class="fieldLabel">'+ $("#question").val() +'</span>');
			$("#exampleSelect").empty();
			var fieldOptions = $("#fieldOptions").val().split("\n");
			$("#exampleField").append('<select class="field1" id="exampleSelect"><option>Please Select...</option>');
			
			for(option in fieldOptions){
				$("#exampleSelect").append('<option value="">'+ fieldOptions[option] +'</option>');	
			}
			$("#exampleSelect").append('</select>');
		}else if(fieldtype==5){
			$("#exampleSelect").empty();
			var fieldOptions = $("#fieldOptions").val().split("\n");
			$("#exampleField").empty();
			$("#exampleField").append('<span class="fieldLabel">'+ $("#question").val() +'</span>');
			
			for(option in fieldOptions){
				$("#exampleField").append('<input type="checkbox" /> '+ fieldOptions[option] +'  ');	
			}
		}
	})
	
	
	$("#toggleCurrency").click(function(){
		$("#currency").slideToggle(250);	
	return false; 
	})
	
	//$("#dobDay, #dobMonth, #dobYear").selectbox();
	//$("#network").selectbox2();
	
	
	////////////////
	/////LOGIN AND SIGNUP PAGE
	/////////////////////////////////////////////////////
	$("#emailLogin, #passwordLogin").focus(function(){
		$(this).removeClass('greytext');
	return false; 
	})
	
	$("#emailLogin").blur(function(){
		if($("#emailLogin").val()=="Email Address"){
			$(this).addClass('greytext');	
		}
	return false; 
	})
	$("#passwordLogin").blur(function(){
		if($("#passwordLogin").val()=="password"){
			$(this).addClass('greytext');	
		}
	return false; 
	})
	
	$(".field_short, .field_long").focus(function(){
		$(this).removeClass('greytext');	
	return false; 
	})
	
		
	$(".forgot").click(function(){
		$("#forgotPwd").slideToggle(250);
	return false; 
	})
	$("#signupBtn").click(function(){
		$("#login").slideUp(500);	
		setTimeout(function(){ $("#signup").slideDown(500);}, 500);
	return false; 
	})
	
	$(".jquery-selectbox").click(function(){
		$(".jquery-selectbox-item, .jquery-selectbox-currentItem").addClass('greytextdark');	
	return false; 
	})
	
	$(".jquery-selectbox2").click(function(){
		$(".jquery-selectbox2-item, .jquery-selectbox2-currentItem").addClass('greytextdark');	
	return false; 
	})
	
		
	
	$('tr.paginate').livequery(function(){ 

		$(this).hover(
		  function () {
			$(this).children("td").addClass("lightGreyBg");
	
		  }, 
		  function () {
			$(this).children("td").removeClass("lightGreyBg");
	
		  }
		);
	})
	
	$(".custom_field").hover(function () {
		$(this).find("div").show();

	  }, 
	  function () {
		$(this).find("div").hide();

	  }
	);
	
	$(".expandable .expLink").livequery('click', function(event){
		if($(this).parent().find("div:first").is(':hidden')){
			//expand
			var currentExpandable = $(this).parent();
			$(this).parent().find("div:first").slideDown('medium');
			$(this).parent().find("div .clearer").show();
			$(this).parent().find(".floatable, .floatable2").show();
			setTimeout(function(){ $.scrollTo(currentExpandable, 500, {offset:-20}); }, 500);
		}else{
			//collapse
			$(this).parent().find("div:first").slideUp('medium');
			setTimeout(function(){ $.scrollTo(0, 500); }, 800);
		}
		/*$(this).parent().find("div:first").slideToggle('medium');
		$(this).parent().find("div .clearer").show();
		$.scrollTo($(this).parent(), 500, {offset:-20});*/
		return false;
	});
	
	$(".bg, .addToForm").click(function () {
		if($(this).parent().find("input[type='checkbox']").is(':checked')){
			$(this).parent().find("input[type='checkbox']").attr('checked', false);
		}else{
			$(this).parent().find("input[type='checkbox']").attr('checked', true);
		}
		
		if($(":checkbox:checked").length > 0){
			//$(".multiple").fadeIn(250);
		}else{
			//$(".multiple").fadeOut(250);
		}
	})
	
	$("#selectAll").livequery('click', function(event){
		$("tr:visible input[type='checkbox'][class='checkedUser']").attr('checked', true);
		$("tr:visible input[type='checkbox'][class='cfSelected']").attr('checked', true);
		//$(".multiple").fadeIn(250);
		return false;
	})
	
	$("#unselectAll").livequery('click', function(event){
		$("tr:visible input[type='checkbox'][class='checkedUser']").attr('checked', false);
		$("tr:visible input[type='checkbox'][class='cfSelected']").attr('checked', false);
		//$(".multiple").fadeOut(250);
		return false;
	})
	
	

	$("td.tdSelect").livequery('click', function(event){
		
		if($(this).parent().find("input[type='checkbox']").is(':checked')){
			$(this).parent().find("input[type='checkbox']").attr('checked', false);
		}else{
			$(this).parent().find("input[type='checkbox']").attr('checked', true);
		}
		
		//alert($(":checkbox:checked").length);
		
		if($(":checkbox:checked").length > 0){
			//$(".multiple").fadeIn(250);
		}else{
			//$(".multiple").fadeOut(250);
		}
		
		
	})
	$("td input[class='checkedUser']").livequery('click', function(event){
		
		if($(this).parent().find("input[type='checkbox']").is(':checked')){
			$(this).parent().find("input[type='checkbox']").attr('checked', true);
		}else{
			$(this).parent().find("input[type='checkbox']").attr('checked', false);
		}
		
		//alert($(":checkbox:checked").length);
		
		if($(":checkbox:checked").length > 0){
			//$(".multiple").fadeIn(250);
		}else{
			//$(".multiple").fadeOut(250);
		}
		
		
	})
	
	$(".app_select_all").click(function () {	
		
		$(this).parent().parent().find("input[type='checkbox']").attr('checked', true);
		$(this).attr("checked", false);
		
		
	})
	
	$(".app_select_all_boxes").click(function () {	
		
		if($(this).is(':checked')){
			$('.expandable div').slideDown(250);
			$('.expandable').find("input[type='checkbox']").attr('checked', true);
		}else{
			$('.expandable').find("input[type='checkbox']").attr('checked', false);
		}
		
	})
	
	
	
	$("tr.paginate").quickpaginate({ perpage: perpage, showcounter: true, pager : $(".counter") });
	
	$(function() {
		$(".sortable").sortable({ opacity: 0.6, cursor: 'move', update: function() {
			var fieldWeight = $(this).sortable("serialize");
			
			$.post("/includes/ajax/order_questions.php", {fieldWeight:fieldWeight}, function(data){ /*alert(data);*/ });
		}
		});
	});
	
	$(function() {
		$(".sortable2").sortable({ opacity: 0.6, cursor: 'move', update: function() {
			
			var result = '';
			$("input[name='profile_order']").each( function () {
				result = result + $(this).val() + ":";
			});
			
			result = substr(result, 0, -1);
			
			
			
			$.post("/includes/ajax/order_profile_data.php", {fieldWeight:result}, function(data){ /*alert(data);*/ });
		}
		});
	});


});




function handleEnter(event, fn, id) {
 var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
 if (keyCode == 13) {
   
   if(id=="none"){
	   id = "";
   }
   
   fn(id);
   //alert("enter pressed");
 }
 //return false;
}

function substr (f_string, f_start, f_length) {
    // http://kevin.vanzonneveld.net
    // +     original by: Martijn Wieringa
    // +     bugfixed by: T.Wild
    // +      tweaked by: Onno Marsman
    // *       example 1: substr('abcdef', 0, -1);
    // *       returns 1: 'abcde'
    // *       example 2: substr(2, 0, -6);
    // *       returns 2: ''
 
    f_string += '';
 
    if (f_start < 0) {
        f_start += f_string.length;
    }
 
    if (f_length == undefined) {
        f_length = f_string.length;
    } else if (f_length < 0){
        f_length += f_string.length;
    } else {
        f_length += f_start;
    }
 
    if (f_length < f_start) {
        f_length = f_start;
    }
 
    return f_string.substring(f_start, f_length);
}

function not_applicable(field){
	var tickbox = $('#'+field+'_na');
	if(tickbox.is(':checked')){
		$('#'+field).val('na');
		$('#'+field).hide();
	}else{
		$('#'+field).val('');
		$('#'+field).show();
	}
}

function not_applicable_2(field){
	var tickbox = $('#ed_'+field+'_na');
	if(tickbox.is(':checked')){
		
		//set fields to na
		$('#ed_'+field).val('na');
		$('#ed_'+field+'_state').val('na');
		$('#'+field+'_div input[type="radio"]').attr('checked', false);
		$('#ed_'+field+'_study').val('na');
		$('#ed_'+field+'_comp option:eq(0)').attr('selected', 'selected');
		
		
		//hide fields
		$('#'+field+'_div').hide();
	}else{
		$('#'+field+'_div').show();
	}
}

function expandCollapse(){
	
	
	$(this).children().hide();

}

function save_question(int){
	
	$("#questionDiv").fadeOut(250);
	setTimeout(function(){ $("#saveStatus").append('<p>Saving Question<img src="/images/loading.gif" /></p>'); }, 350);
	var question = $("#question").val();
	var fieldtype = $("#fieldType").val();
	var fieldoptions = $("#fieldOptions").val();
	var group = $("#group").val();
	var cus_group = $("#cus_group").val();
	
	$.post("/includes/ajax/save_question.php", {question: question, fieldtype: fieldtype, fieldoptions:fieldoptions, group:group, cus_group:cus_group}, function(data){ 
		setTimeout(function(){$("#saveStatus").empty(); $("#saveStatus").append("<p>"+ data + "</p>"); }, 400);
		setTimeout(function(){
			$("#saveStatus").empty();
			$("#question").val('');
			$("#fieldOptions").val('');
			$("#fieldType option:eq(0)").attr("selected", "selected");
			$("#group option:eq(0)").attr("selected", "selected");
			$("#exampleField").empty();
			$("#questionDiv").fadeIn(250);
			
			if(int==1){
				$("#question").parent().parent().submit();	
			}
			
		}, 1400);
	});
	return false; 

}

function check_question(){
	var question = $("#question").val();
	
	if(question!=''){
		//alert("hi");
		var con =  confirm('You haven\'t saved your custom question, would you like to save now and proceed?');
		if(con==false){
			$("#question").focus();
		}else{
			save_question(1);			
		}
	}else{
		$("#question").parent().parent().submit();
	}
	
}

function scrollPage(id){
	setTimeout(function(){ $.scrollTo($('#'+id), 500, {offset:-20}); }, 500);
	
}

function save_image(){
	
	$("#loading").fadeIn(250);
	var x = $("#x").val();
	var y = $("#y").val();
	var h = $("#h").val();
	var w = $("#w").val();
	$.post("/includes/ajax/save_image.php", {x:x, y:y, w:w, h:h}, function(data){ 

			$("#loading").fadeOut(250);
			if(data=='error1'){
				window.location = "/homepage/customise/error1";
			}else if(data=='error2'){
				window.location = "/homepage/customise/error2";
			}else{
				window.location = "/homepage/customise";
			}
	});
	return false; 

}

function save_logo(){
	
	$("#loading").fadeIn(250);
	var x = $("#x").val();
	var y = $("#y").val();
	var h = $("#h").val();
	var w = $("#w").val();
	$.post("/includes/ajax/save_logo.php", {x:x, y:y, w:w, h:h}, function(data){ 

			$("#loading").fadeOut(250);
			if(data=='error1'){
				window.location = "/myapply/profile/error1";
			}else if(data=='error2'){
				window.location = "/myapply/profile/error2";
			}else{
				window.location = "/myapply/profile";
			}
	});
	return false; 

}

function update_profile(section, uid){
	$("#loading_"+section).fadeIn();
	
	var currentExpandable = $("#loading_"+section).parent().parent().parent();
	$.scrollTo(currentExpandable, 500, {offset:-20});
	
	var answer = confirm("Are you sure you'd like to update this section?");
	if(answer){
	
	
	if(section=="contact"){
		
		var email = $("#email").val();
		var add1 = $("#add1").val();
		var add2 = $("#add2").val();
		var town = $("#town").val();
		var state = $("#state").val();
		var zip_postal = $("#zip_postal").val();
		var country = $("#country").val();
		
		if(uid==1){
			var day_tel = $("#day_tel").val().split(' ').join('');
			var eve_tel = $("#eve_tel").val().split(' ').join('');
			if(isNaN(day_tel) || isNaN(eve_tel)){
				$('.success, .error').remove();
				$("#loading_"+section).parent().before('<div class="error">Please enter a numeric value for telephone numbers.</div>');	 
				$('.success, .error').fadeIn(100);
				$("#loading_"+section).fadeOut(250);
			}else{
				$.post("/includes/ajax/save_profile.php", {email:email, add1: add1, add2:add2, town:town, state:state, zip_postal:zip_postal, country:country, day_tel:day_tel, eve_tel:eve_tel, section:section}, function(data){ 
					if(data=='success'){
						$('.success, .error').remove();
						$("#loading_"+section).parent().before('<div class="success">Contact details successfully updated.</div>');
						$('.success, .error').fadeIn(100);
						setTimeout(function(){ $(".success, .error").fadeOut(500);}, 3000);
					}else{
						$('.success, .error').remove();
						$("#loading_"+section).parent().before('<div class="error">Contact details couldn\'t be updated.</div>');
						$('.success, .error').fadeIn(100);
						setTimeout(function(){ $(".success, .error").fadeOut(500);}, 3000);
					}
					$("#loading_"+section).fadeOut(250);
						
				});
			}
		}else if(uid==2){
			
		}else if(uid==3){
			var tel = $("#tel").val().split(' ').join('');
			
			if(isNaN(tel)){
				$('.success, .error').remove();
				$("#loading_"+section).parent().before('<div class="error">Please enter a numeric value for telephone number.</div>');	 
				$('.success, .error').fadeIn(100);
				$("#loading_"+section).fadeOut(250);
			}else{
				$.post("/includes/ajax/save_profile.php", {add1: add1, add2:add2, town:town, state:state, zip_postal:zip_postal, country:country, tel:tel, section:section}, function(data){ 
					if(data=='success'){
						$('.success, .error').remove();
						$("#loading_"+section).parent().before('<div class="success">Contact details successfully updated.</div>');
						$('.success, .error').fadeIn(100);
						setTimeout(function(){ $(".success, .error").fadeOut(500);}, 3000);
					}else{
						$('.success, .error').remove();
						$("#loading_"+section).parent().before('<div class="error">Contact details couldn\'t be updated.</div>');
						$('.success, .error').fadeIn(100);
						setTimeout(function(){ $(".success, .error").fadeOut(500);}, 3000);
					}
					$("#loading_"+section).fadeOut(250);
						
				});
			}
		}
		
		
		
		
		
		return false;
		
	}else if(section=="employ_info"){
		
		var ssn = $("#ssn").val();
		
		var emp_type = '';
		$("input[name='emp_type']:checked").each( function () {
		emp_type = emp_type + $(this).val() + "|";
		});
		emp_type = substr(emp_type, 0, -1);

		var work_from_sun = $("#work_from_sun").val();
		var work_to_sun = $("#work_to_sun").val();
		
		var work_from_mon = $("#work_from_mon").val();
		var work_to_mon = $("#work_to_mon").val();
		
		var work_from_tue = $("#work_from_tue").val();
		var work_to_tue = $("#work_to_tue").val();
		
		var work_from_wed = $("#work_from_wed").val();
		var work_to_wed = $("#work_to_wed").val();
		
		var work_from_thu = $("#work_from_thu").val();
		var work_to_thu = $("#work_to_thu").val();
		
		var work_from_fri = $("#work_from_fri").val();
		var work_to_fri = $("#work_to_fri").val();
		
		var work_from_sat = $("#work_from_sat").val();
		var work_to_sat = $("#work_to_sat").val();
		
		var overtime = $("input[name='overtime']:checked").val();
		var legal = $("input[name='legal']:checked").val();
		var certificate = $("input[name='certificate']:checked").val();
		var crime = $("input[name='crime']:checked").val();
		var conviction = $("#conviction").val();
		
		if(crime=="no"){
			var conviction = '';	
		}
		
		$.post("/includes/ajax/save_profile.php", {
			   ssn:ssn, emp_type: emp_type,
			   sun_from:work_from_sun, sun_to:work_to_sun,
			   mon_from:work_from_mon, mon_to:work_to_mon, 
			   tue_from:work_from_tue, tue_to:work_to_tue, 
			   wed_from:work_from_wed, wed_to:work_to_wed, 
			   thu_from:work_from_thu, thu_to:work_to_thu, 
			   fri_from:work_from_fri, fri_to:work_to_fri, 
			   sat_from:work_from_sat, sat_to:work_to_sat,
			   overtime:overtime, legal:legal, certificate:certificate, crime:crime, conviction:conviction, section:section}, function(data){ 
			$("#loading_"+section).fadeOut(250);
			if(data=='success'){
				$('.success, .error').remove();
				$("#loading_"+section).parent().before('<div class="success">Employment information successfully updated.</div>');
				$('.success, .error').fadeIn(100);
				setTimeout(function(){ $(".success, .error").fadeOut(500);}, 3000);
			}else{
				$('.success, .error').remove();
				$("#loading_"+section).parent().before('<div class="error">Employment information couldn\'t be updated.</div>');
				$('.success, .error').fadeIn(100);
				setTimeout(function(){ $(".success, .error").fadeOut(500);}, 3000);
			}
				
		});
		
		return false;
		
	}else if(section=="employ_hist"){
		
		var employer = $("#eh_employer").val();
		var position = $("#eh_position").val();
		var date_1 = $("#eh_date_1").val();
		var date_2 = $("#eh_date_2").val();
		var salary = $("#eh_salary").val();
		var per = $("#eh_per").val();
		var duties = $("#eh_duties").val();
		var supervisor = $("#eh_supervisor").val();
		var supervisor_tel = $("#eh_supervisor_tel").val();
		var supervisor_allow = $("input[id='eh_supervisor_allow']:checked").val();
		
		
		$.post("/includes/ajax/save_profile.php", {
			employer:employer,
			position:position,
			date_1:date_1,
			date_2:date_2,
			salary:salary,
			per:per,
			duties:duties,
			supervisor:supervisor,
			supervisor_tel:supervisor_tel,
			supervisor_allow:supervisor_allow,
			section:section
			}, function(data){ 
			$("#loading_"+section).fadeOut(250);
			if(data!='error'){
				$('.success, .error').remove();
				
				$("#loading_"+section).parent().parent().find('form').slideUp('medium');
				
				setTimeout(function(){
				$("#loading_"+section).parent().before('<div class="success">Employment history successfully updated.</div>');
				$('.success, .error').fadeIn(100);
					setTimeout(function(){$("#loading_"+section).parent().parent().find('form').slideDown('medium'); }, 2000);
				}, 1000);
				
				$("#eh_employer").val('');
				$("#eh_position").val('');
				$("#eh_date_1").val('');
				$("#eh_date_2").val('');
				$("#eh_salary").val('');
				$("#eh_per option:eq(0)").attr("selected","selected");
				$("#eh_duties").val('');
				$("#eh_supervisor").val('');
				$("#eh_supervisor_tel").val('');
				$("input[id='eh_supervisor_allow']:checked").attr("checked", false);
				
				$("#employ_hist").empty();
				$("#employ_hist").append(data);
				
				setTimeout(function(){ $(".success, .error").fadeOut(500);}, 3000);
			}else{	
				$('.success, .error').remove();
				
				$("#loading_"+section).parent().parent().find('form').slideUp('medium');
				setTimeout(function(){
					$("#loading_"+section).parent().before('<div class="error">Employment history couldn\'t be updated. Please make sure you have filled all fields in.</div>');
					$('.success, .error').fadeIn(100);
					setTimeout(function(){$("#loading_"+section).parent().parent().find('form').slideDown('medium'); }, 2000);
				}, 1000);
				
				setTimeout(function(){ $(".success, .error").fadeOut(500);}, 3000);
				return false;
			}
		
		});
		return false;
		
	}else if(section=="references"){
		
		var employer = $("#ref_employer").val();
		var referrer = $("#ref_referrer").val();
		var contact = $("#ref_referrer_tel").val().split(' ').join('');
		
		if(isNaN(contact)){
			$('.success, .error').remove();
			$("#loading_"+section).parent().before('<div class="error">Please enter a numeric value for Telephone numbers.</div>');	 
			$('.success, .error').fadeIn(100);
			$("#loading_"+section).fadeOut(250);
			return false;
		}else{		
			$.post("/includes/ajax/save_profile.php", {employer:employer, referrer:referrer, contact:contact, section:section}, function(data){ 
				$("#loading_"+section).fadeOut(250);
				if(data!='error'){
					$('.success, .error').remove();
					
					$("#loading_"+section).parent().parent().find('form').slideUp('medium');
					
					setTimeout(function(){
					$("#loading_"+section).parent().before('<div class="success">Employment history successfully updated.</div>');
					$('.success, .error').fadeIn(100);
						setTimeout(function(){$("#loading_"+section).parent().parent().find('form').slideDown('medium'); }, 2000);
					}, 1000);
					
					$("#ref_employer").val('');
					$("#ref_referrer").val('');
					$("#ref_referrer_tel").val('');
					
					$("#references").empty();
					$("#references").append(data);
					
					setTimeout(function(){ $(".success, .error").fadeOut(500);}, 3000);
				}else{	
					$('.success, .error').remove();
					
					$("#loading_"+section).parent().parent().find('form').slideUp('medium');
					setTimeout(function(){
						$("#loading_"+section).parent().before('<div class="error">Employment history couldn\'t be updated. Please make sure you have filled all fields in.</div>');
						$('.success, .error').fadeIn(100);
						setTimeout(function(){$("#loading_"+section).parent().parent().find('form').slideDown('medium'); }, 2000);
					}, 1000);
					
					setTimeout(function(){ $(".success, .error").fadeOut(500);}, 3000);
				}
			
			});
		
		}
		
		
		
	}else if(section=="edu_info"){
		
		var hs = $("#ed_hs").val();
		var hs_state = $("#ed_hs_state").val();
		var hs_grad = $("input[name='ed_hs_grad']:checked").val();
		var hs_study = $("#ed_hs_study").val();
		var hs_comp = $("#ed_hs_comp").val();
		var hs_enroll = $("input[name='ed_hs_enroll']:checked").val();
		
		var col = $("#ed_col").val();
		var col_state = $("#ed_col_state").val();
		var col_grad = $("input[name='ed_col_grad']:checked").val();
		var col_study = $("#ed_col_study").val();
		var col_comp = $("#ed_col_comp").val();
		var col_enroll = $("input[name='ed_col_enroll']:checked").val();
		
		var pcol = $("#ed_pcol").val();
		var pcol_state = $("#ed_pcol_state").val();
		var pcol_grad = $("input[name='ed_pcol_grad']:checked").val();
		var pcol_study = $("#ed_pcol_study").val();
		var pcol_comp = $("#ed_pcol_comp").val();
		var pcol_enroll = $("input[name='ed_pcol_enroll']:checked").val();
		
		
		$.post("/includes/ajax/save_profile.php", {
			   hs:hs, hs_state:hs_state, hs_grad:hs_grad, hs_study:hs_study, hs_comp:hs_comp, hs_enroll:hs_enroll,
			   col:col, col_state:col_state, col_grad:col_grad, col_study:col_study, col_comp:col_comp, col_enroll:col_enroll,
			   pcol:pcol, pcol_state:pcol_state, pcol_grad:pcol_grad, pcol_study:pcol_study, pcol_comp:pcol_comp, pcol_enroll:pcol_enroll,
			   section:section}, function(data){ 
			$("#loading_"+section).fadeOut(250);
			if(data=='success'){
				$('.success, .error').remove();
				$("#loading_"+section).parent().before('<div class="success">Education information successfully updated.</div>');
				$('.success, .error').fadeIn(100);
				setTimeout(function(){ $(".success, .error").fadeOut(500);}, 3000);
			}else{
				$('.success, .error').remove();
				$("#loading_"+section).parent().before('<div class="error">Education information couldn\'t be updated.</div>');
				$('.success, .error').fadeIn(100);
				setTimeout(function(){ $(".success, .error").fadeOut(500);}, 3000);
			}
				
		});
		return false;
		
	}
	
	}else{
		$("#loading_"+section).hide();
		var currentUrl = window.location.pathname;
		window.location = currentUrl;
		return false;	
	}
	
	
	return false; 

}

function favourite(obj, uid, jid){
	
	
	$.post("/includes/ajax/favourite.php", {uid:uid, jid:jid}, function(data){ 
		$("#applicants_tbl").replaceWith(data);
		//setTimeout(function(){ $("#applicants").empty(); $("#applicants").append(data); }, 500);
	});

}


function filter_applicants(af_id, jid){
	
	var str = $("#appFilters form").serialize({emptyCheckboxes:true, emptyValues:true});	
	$("#applicants").empty();
	$("#applicants").append('<p>Searching applicants <img src=\"/images/loading.gif\" /></p>');
	$.post("/includes/ajax/filter_applicants.php", {af_id:af_id, jid:jid, str:str}, function(data){ 
		setTimeout(function(){ $("#applicants").empty(); $("#applicants").append(data); }, 500);
	});

}

function save_job(obj, jid){
	
	$(obj).css({opacity:'0.5', filter:'alpha(opacity=50)', cursor:'default'});
	$.post("/includes/ajax/save_job.php", {jid:jid}, function(data){ 
		$('#saved_jobs').empty();
		$('#saved_jobs').append(data);
		$(obj).removeAttr('onclick');
	});

}

function save_all_jobs(apply){
	
	var jobs = $('#allJobs').val();
	jobs = jobs.split(',');
	
	for(job in jobs){
		$('#save_'+jobs[job]).css({opacity:'0.5', filter:'alpha(opacity=50)', cursor:'default'});
		$('#save_'+jobs[job]).removeAttr('onclick');
		$.post("/includes/ajax/save_job.php", {jid:jobs[job]}, function(data){ 
			if(apply==1){
				window.location='/application-form.php?jid='+jobs[0];	
			}
		
		});
		
	}
	
	/*$.post("/includes/ajax/save_job.php", {jid:jid}, function(data){ 
		$('#saved_jobs').empty();
		$('#saved_jobs').append(data);
		$(obj).removeAttr('onclick');
	  });*/

}

function remove_job(obj, jid){
	
	$(obj).css({opacity:'0.5', filter:'alpha(opacity=50)', cursor:'default'});
	$.post("/includes/ajax/remove_job.php", {jid:jid}, function(data){ 
		
		$('#saved_jobs').empty();
		$('#saved_jobs').append(data);
		$('#save_'+jid).bind('click', function(){save_job(this, jid); return false;});

		$('#save_'+jid).css({opacity:'1', filter:'alpha(opacity=100)', cursor:'pointer'});
	});
	
	return false;

}

function delete_reference(rfid){
	$.post("/includes/ajax/delete_reference.php", {rfid:rfid}, function(data){ 
		
		$('#references').replaceWith(data);
	});
}

function delete_employ_hist(eh_id){
	$.post("/includes/ajax/delete_employ_hist.php", {eh_id:eh_id}, function(data){ 
		
		$('#employ_hist').replaceWith(data);
	});
}

function reset_filters(af_id, jid){
	
	$("#appFilters form").empty();	
	$("#appFilters form").append('<p>Resetting filters <img src=\"/images/loading.gif\" /></p>');
	$.post("/includes/ajax/filter_applicants.php", {af_id:af_id, jid:jid, reset_f:1}, function(data){ 
		setTimeout(function(){ $("#appFilters form").empty(); $("#appFilters form").append(data); }, 500);
	});

}

function fill_position(jid){
	$.post("/includes/ajax/fill_position.php", {action:0, jid:jid}, function(data){ 
		//$("#jobs_div").empty();
		$("#fill_position").append(data);
	});

}

function fill_position_email(jid, uid, obj){
	$.post("/includes/ajax/fill_position.php", {action:1, jid:jid, uid:uid}, function(data){ 
		//$("#jobs_div").empty();
		$("#myjobs").replaceWith(data);
		var notification = $(obj).parent().parent().parent();
		$(notification).fadeOut(500);
	});

}


function close_job(jid){
	var answer = confirm("By closing this job it will not be visible to any users, and all applicants will be informed that the job has been closed.\n\nCurrent applicants will be saved for future reference.\n\nClick OK to continue with closing this job.\nClick cancel to discard all actions.");
	if (answer){
		$.post("/includes/ajax/close_job.php", {jid: jid}, function(data){ 
			if(data!='error'){
				$("#jobs_div").empty();
				$("#jobs_div").append(data);
				
				//functionality to click if the job was filled by an online applicant.
				fill_position(jid);
			}
		});
	}

}

function open_job(jid){
	var answer = confirm("By opening this job all students in your network will be able to apply for this job.\n\nAll previous applicants will be notified that the job has been re-opened.\n\nClick OK to continue with opening this job.\nClick cancel to keep the job closed.");
	if (answer){
		$.post("/includes/ajax/open_job.php", {jid: jid}, function(data){ 
			if(data!='error'){
				$("#jobs_div").empty();
				$("#jobs_div").append(data);
				$("#notify-"+jid).remove();
			}
		});
	}

}

function remove_notification(obj){
	var notification = $(obj).parent().parent();
	
	if($(notification).attr('class')=='notify'){
		$(notification).fadeOut(500);
	}else{
		var notification = $(obj).parent();
		$(notification).fadeOut(500);
	}
	
	return false;

}

function job_filled_discard(obj, jfid){
	var notification = $(obj).parent();
	$.post("/includes/ajax/job_filled_discard.php", {jfid:jfid}, function(data){ 
		$(notification).fadeOut(500);
	});
}

function employ_status(status){
	
	var radio = this;
	if(status==0){
		var answer = confirm("Employers will not be able to view your applications, are you sure you wish to change your employment status?");	
		if (answer){
			$.post("/includes/ajax/employ_status.php", {status:status}, function(data){ 
			});
		}else{
			$('input[name=employ_status]').attr('checked', false);
			$('input[name=employ_status]:first').attr('checked', true);
		}
	}else if(status==1){
		$.post("/includes/ajax/employ_status.php", {status:status}, function(data){ 
		});		
	}
	
	
	


}

function delete_application_form_selected(){
	
	var result = "";
	$(".checkedUser:checked").each( function () {
	result = result + $(this).val() + ",";
	});
	result = substr(result, 0, -1);
	
	
	var answer = confirm("Are you sure you want to remove the selected application forms?");
	if (answer){
		$.post("/includes/ajax/delete_af.php", {af: result}, function(data){ 
			$(".checkedUser:checked").each( function () {
				if(data=='error'){
				}else{
				$("#app_form_" + $(this).val()).remove();	
				}
			})
			//$(".multiple").hide();
		});
	}

}


function delete_cafi_selected(){
	
	var result = "";
	$(".cfSelected:checked").each( function () {
	result = result + $(this).val() + ",";
	});
	result = substr(result, 0, -1);
	
	
	var answer = confirm("Are you sure you want to remove the selected custom application questions?");
	if (answer){
		$.post("/includes/ajax/delete_cafi.php", {cafi: result}, function(data){ 
			$(".cfSelected:checked").each( function () {
				if(data=='error'){
				}else{
				$("#custom_field_" + $(this).val()).remove();	
				}
			})
			//$(".multiple").hide();
		});
	}

}

function delete_jobs_selected(){
	
	var result = "";
	$(".checkedUser:checked").each( function () {
	result = result + $(this).val() + ",";
	});
	result = substr(result, 0, -1);
	
	
	var answer = confirm("Are you sure you would like to delete the selected job(s)? If you continue all received applications will also be deleted. \n\nClick OK to continue deleting job(s). \nClick Cancel to discard all actions\n\nTip: If you'd like to close and save the job post for future use, simply close the job by clicking on the red-arrow.");
	if (answer){
		$.post("/includes/ajax/delete_jobs.php", {jobs: result}, function(data){ 
			$(".checkedUser:checked").each( function () {
				if(data=='error'){
				}else{
					$("#job_" + $(this).val()).remove();	
				}
			})
			//$(".multiple").hide();
		});
	}

}

function showHide(elm){
	$("#"+elm).slideToggle(250);
	$(".hidden").hide();
	return false; 
}

function show(elm){
	$("#"+elm).fadeIn(250);
	return false; 
}

function hide(elm){
	$("#"+elm).fadeOut(250);
	return false; 
}

function highlight_questions(){
	
	var str = $("form").serialize({emptyCheckboxes:true, emptyValues:true});	
	
	$.post("/includes/ajax/submit_app.php", { str:str, highlight:1 }, function(data){ 
		$("li, td, tr, table, ul").removeClass('highlight');
		$(data).parent().addClass('highlight');
		scrollPage("application_form");	
	});	
}

function submit_application(update){
	
	$('#application_form ul').fadeOut(250);
	$('#application_form input[type="button"]').fadeOut(250);
	setTimeout(function(){ $('#application_form #loading').fadeIn(250); }, 500 );
	
	var str = $("form").serialize({emptyCheckboxes:false, emptyValues:true});	
	
	
	$.post("/includes/ajax/submit_app.php", { str:str, update:update }, function(data){ 
		setTimeout(function(){ 
			
			if(substr(data, 12, 3)=="suc"){
				$("form").remove();
				$("#status").empty();
				$("#status").append(data + "Please wait a few seconds to be redirected...");
				scrollPage("application_form");
				
				var jid = $('input[name=nextJob]').val();				
				if(jid!=''){
					setTimeout(function(){ window.location='/application-form.php?jid='+jid;}, 3500);
				}else{
					setTimeout(function(){ window.location='/myapply';}, 3500);
				}
			}else{
				$("#status").empty();
				$("#status").append(data);
				scrollPage("application_form");
				
				highlight_questions();
			}
		}, 500);
		
	});
	

}

function print_application(update){
	
	$('#application_form ul').fadeOut(250);
	$('#application_form input[type="button"]').fadeOut(250);
	setTimeout(function(){ $('#application_form #loading').fadeIn(250); }, 500 );
	
	var str = $("form").serialize({emptyCheckboxes:false, emptyValues:true});	
	
	
	$.post("/includes/ajax/print_app.php", { str:str, update:update }, function(data){ 
		setTimeout(function(){ 
			
			if(substr(data, 12, 3)=="suc"){
				
				$("tr, td, li, input, textarea, p").removeClass('highlight');
				window.print();
				//$("form").remove();
				$("#status").empty();
				$("#status").append(data + "Please wait a few seconds to be redirected...");
				scrollPage("application_form");
				setTimeout(function(){ window.location='/network';}, 4500);
				
			}else{
				$("#status").empty();
				$("#status").append(data);
				scrollPage("application_form");
				
				highlight_questions();
			}
		}, 500);
		
	});
	

}

function controls(id){
	value = $("#controls-"+id+"-val").val();
	$("#controls-"+id).slideToggle('fast');
	if(value==0 || value==''){
		$("#controls-"+id+"-val").val(1);
		$.post("/includes/sessions.php", {controls:id, value:0}, function(data){  });
	}else if(value==1){
		$("#controls-"+id+"-val").val(0);
		$.post("/includes/sessions.php", {controls:id, value:1}, function(data){  });
	}
	return false; 
}

function alterNate(elm){
if (!elm.base) elm.base = elm.value
if (elm.value == elm.base) elm.value = "";
else if (elm.value == "") elm.value = elm.base;
}

function network_request(uid, utype, nid, req){
	
	$("#netReq-"+uid+"-"+utype+" img").fadeIn(100);
	if(utype==1){
		$.post("/includes/ajax/network_request.php", {uid: uid, utype: utype, nid:nid, req:req}, function(data){ $("#netReq-"+uid+"-"+utype).remove() ;$(".stuReqCount").empty(); $(".stuReqCount").append(data); });
	}else if(utype==3){
		$.post("/includes/ajax/network_request.php", {uid: uid, utype: utype, nid:nid, req:req}, function(data){ $("#netReq-"+uid+"-"+utype).remove() ;$(".coReqCount").empty(); $(".coReqCount").append(data); });
	}

}

function job_approve(obj, jid, status){
	var tr = $(obj).parent().parent();
	$.post("/includes/ajax/job_approve.php", {jid: jid, status:status}, function(data){ 
		$(tr).fadeOut(500);
		setTimeout(function(){ $(tr).remove(); }, 500);
	});
	
	
}

function applicant_request(jid, req, uid){
	
	$("#jobtop"+" .btn").remove();
	$("#accept-reject").append('<img src="/images/loading.gif" />');
	
	$.post("/includes/ajax/applicant_request.php", {jid:jid, req:req, uid:uid}, function(data){ 
		if(data==1){
			$("#accept-reject").empty();
			$("#accept-reject").append('<img src="/images/bullets/accepted.jpg" width="9" height="9" /> Accepted');
		}else if(data==2){
			$("#accept-reject").empty();
			$("#accept-reject").append('<img src="/images/bullets/rejected.jpg" width="9" height="9" /> Rejected');
		}
		//$("#netReq-"+uid+"-"+utype).remove();
		//$(".stuReqCount").empty(); 
		//$(".stuReqCount").append(data); 
	});

}

function network_request_selected(utype, nid, req){
	
	var result = "";
	$(".checkedUser:checked").each( function () {
	result = result + $(this).val() + ",";
	});
	result = substr(result, 0, -1);
	
	
	$(".checkedUser:checked").each( function () {
			$("#netReq-" + $(this).val() + " img").fadeIn(100);							  
	})
	
	$.post("/includes/ajax/network_request.php", {users: result, nid:nid, req:req}, function(data){ 
		$(".checkedUser:checked").each( function () {
			$("#netReq-" + $(this).val()).remove();							  
		})
		
		if(utype==1){
			$(".stuReqCount").empty(); $(".stuReqCount").append(data);
		}else if(utype==3){
			$(".coReqCount").empty(); $(".coReqCount").append(data);
		}
		
		//$(".multiple").hide();
	});

}

function network_remove(uid, utype, nid, req){
	
	var answer = confirm("Are you sure you want to remove this user from your network?")
	if (answer){
		$("#netRem-"+uid+"-"+utype+" img").fadeIn(100);
		$.post("/includes/ajax/network_remove.php", {uid: uid, utype: utype, nid:nid, req:req}, function(data){ $("#netRem-"+uid+"-"+utype).remove(); $(".userCount").empty(); $(".userCount").append(data);/*alert("data " + data);*/ });
	}

}


function network_remove_selected(nid, req){
	
	var result = "";
	$(".checkedUser:checked").each( function () {
	result = result + $(this).val() + ",";
	});
	result = substr(result, 0, -1);
	
	
	var answer = confirm("Are you sure you want to remove the selected users?");
	if (answer){
		$(".checkedUser:checked").each( function () {
			$("#netRem-" + $(this).val() + " img").fadeIn(100);							  
		})
		$.post("/includes/ajax/network_remove.php", {users: result, nid:nid, req:req}, function(data){ 
			$(".checkedUser:checked").each( function () {
				$("#netRem-" + $(this).val()).remove();							  
			})
			$(".userCount").empty(); $(".userCount").append(data);
			//$(".multiple").hide();
		});
	}

}

function delete_messages_selected(){
	
	var result = "";
	$(".checkedUser:checked").each( function () {
	result = result + $(this).val() + ",";
	});
	result = substr(result, 0, -1);
	
	
	var answer = confirm("Are you sure you want to remove the selected messages?");
	if (answer){
		$(".checkedUser:checked").each( function () {
			$("#userMsg-" + $(this).val() + " img").fadeIn(100);							  
		})
		$.post("/includes/ajax/message_remove.php", {messages: result}, function(data){ 
			$(".checkedUser:checked").each( function () {
				$("#userMsg-" + $(this).val()).remove();							  
			})
			//$(".multiple").hide();
		});
	}

}

function lookup_contacts(uid, utype, div){
	value = $('#messageTo').val();
	utype = $('#utype').val();
	if(value==''){
		
	}else{
		$.post("/includes/ajax/lookup_contacts.php", {value:value, utype:utype}, function(data){ 
		$("#"+div+" ul").remove(); $("#"+div).append(data);/*alert("data " + data);*/
	});

	}
	


}

function fill(div, content){
	$('#'+div).val(content);
	$('#'+div).addClass('bold');
	$('#'+div).attr('disabled', 'disabled');	
	
	$('#utype').attr('disabled', 'disabled');
	$('.autoFill ul').hide();
	$('#changeRecipient').append('<a href="#" onclick="changeRecipient();" class="btn">Change Recipient</a>');
	$('#changeRecipient').show();
}

function fillRecipient(rid, r_utype){
	$('#rid').val(rid);
	$('#r_utype').val(r_utype);
}

function changeRecipient(){
	$('#messageTo').val('');
	$('#messageTo').removeAttr('disabled');
	$('#messageTo').removeClass('bold');
	
	$('#utype').removeAttr('disabled');
	
	$('#changeRecipient').empty();
}

function search_user(utype){
	
	$("#userTable").empty();
	if(utype==1){
		$("#stuSearchLoad").fadeIn(100);
		fname = $("#fname").val();
		lname = $("#lname").val();		
		$.post("/includes/ajax/search_user.php", {fname: fname, lname:lname, utype: utype}, function(data){ 
			$("#stuSearchLoad").fadeOut(250); 
			$("#userTable").append(data);/*alert("data " + data);*/ 
			$("tr.paginate").quickpaginate({ perpage: perpage, showcounter: true, pager : $(".counter") });
		});
	}else if(utype==3){
		$("#coSearchLoad").fadeIn(100);
		cname = $("#cname").val();
		$.post("/includes/ajax/search_user.php", {cname: cname, utype:utype}, function(data){ 
			$("#coSearchLoad").fadeOut(250);
			$("#userTable").append(data);/*alert("data " + data);*/ 
			$("tr.paginate").quickpaginate({ perpage: perpage, showcounter: true, pager : $(".counter") });
		});
	}

}

function show_restrict(uid, utype){
	
	$("#restrict-"+uid+"-"+utype).slideToggle(250);
	setTimeout(function(){ $(".restrict").hide(); }, 10);
}

function restrict(uid, utype, option, restriction){
	
	
	$("#netRem-"+uid+"-"+utype+" img").fadeIn(100);
	$.post("/includes/ajax/restrict.php", {uid: uid, utype: utype, option:option, restriction:restriction}, function(data){ 
	 	$("#netRem-"+uid+"-"+utype+" img").hide();/*alert("data " + data);*/ 
		//$("#restrict-"+uid+"-"+utype+" a").removeClass("bold");
		
		if(option==1 && restriction==1){
			$("#restrict-option1-1"+"-"+uid+"-"+utype).addClass("bold");
			$("#restrict-option1-0"+"-"+uid+"-"+utype).removeClass("bold");
		}
		
		if(option==1 && restriction==0){
			$("#restrict-option1-1"+"-"+uid+"-"+utype).removeClass("bold");
			$("#restrict-option1-0"+"-"+uid+"-"+utype).addClass("bold");
		}
		
		if(option==2 && restriction==1){
			$("#restrict-option2-1"+"-"+uid+"-"+utype).addClass("bold");
			$("#restrict-option2-0"+"-"+uid+"-"+utype).removeClass("bold");
		}
		
		if(option==2 && restriction==0){
			$("#restrict-option2-1"+"-"+uid+"-"+utype).removeClass("bold");
			$("#restrict-option2-0"+"-"+uid+"-"+utype).addClass("bold");
		}
	});
}



if(currentUrl=="/homepage/customise"){
	$(window).load(function(){
		$('#bgColor, #bgColor2, #maintextColor, #headingColor, #linkColor, #linkBgColor').colorPicker();	
		
		
		//update the homepage preview
		var bg1 = $("#bgColor").val().replace('#','');
			var bg2 = $("#bgColor2").val().replace('#','');
			$("#homepagePreview .gradient").remove();
			$('#homepagePreview').gradient({
							from:      bg1,
							to:        bg2,
							direction: 'horizontal'
			});	
		$("#homepageHeadingP").css({backgroundColor:$("#headingColor").val()});
		$("#homepageMainTextP").css({color:$("#maintextColor").val()});
		$("#homepageLinksP a").css({color:$("#linkColor").val(), backgroundColor:$("#linkBgColor").val()});
	})
}

if(currentUrl=="/myapply/profile"){
	$(window).load(function(){
		$('.datepicker').datepicker({ dateFormat: 'mm-dd-yy', buttonText: '&raquo;	' });
	})
}

function updateColor(div){
	
	if(div=="bg"){
		var bg1 = $("#bgColor").val().replace('#','');
		var bg2 = $("#bgColor2").val().replace('#','');
		$("#homepagePreview .gradient").remove();
		$('#homepagePreview').gradient({
						from:      bg1,
						to:        bg2,
						direction: 'horizontal'
		});
	}else if(div=='links'){
		//alert(div.toLowerCase().val());
		$("#homepageLinksP a").css({color:$("#linkColor").val(), backgroundColor:$("#linkBgColor").val()});
	}else if(div=='heading'){
		$("#homepageHeadingP").css({backgroundColor:$("#headingColor").val()});
	}
	else if(div=='maintext'){
		$("#homepageMainTextP").css({color:$("#maintextColor").val()});
	}
	
	
	var page_bg = $("#bgColor").val().replace('#', '');
	var page_bg2 = $("#bgColor2").val().replace('#', '');
	var headings = $("#headingColor").val().replace('#', '');
	var main_text = $("#maintextColor").val().replace('#', '');
	var link_color = $("#linkColor").val().replace('#', '');
	var link_bg_color = $("#linkBgColor").val().replace('#', '');
	
	$.post("/includes/ajax/save_colors.php", {page_bg: page_bg, page_bg2:page_bg2, headings:headings, main_text:main_text, link_color:link_color, link_bg_color:link_bg_color}, function(data){ 

	});
	
	
}

function homepage_content(uid){
	var content = $('#homepage_content').val();
	$.post("/includes/ajax/homepage_content.php", {uid:uid, content:content}, function(data){ 
		if(data=='success'){
			$('#homepage_content').before('<div class="success">Homepage content successfully updated.</div>');
		}else{
			$('#homepage_content').before('<div class="error">Homepage content could not be updated.</div>');
		}
		//$("#leftside .error, #leftside .success").remove();
		//$("#leftside").append(data);
	});
}

function fgt_pwd(){
	var email = $(".fgtPwdDiv #emailForgot").val();	
	$.post("/includes/ajax/forgot_password.php", {email:email}, function(data){ 
		$("#leftside .error, #leftside .success").remove();
		$("#leftside").append(data);
	});
	//alert(email);
}

function resend_act(){
	var email = $(".fgtPwdDiv #emailForgot").val();	
	$.post("/includes/ajax/resend_activation.php", {email:email}, function(data){ 
		$("#leftside .error, #leftside .success").remove();
		$("#leftside").append(data);
	});
	//alert(email);
}

function validate_form(form){
	if(form=='job_build_1'){
		var title = $('input[name=title]').val();	
		var salary = $('input[name=salary]').val();	
		var per = $('select[name=per]').val();	
		var jobtype = $('input[name=jobtype]:checked').val();	
		var description = $('textarea[name=description]').val();	
		
		var result = '';
		if(title==''){ result = result + 'You must enter a job title\n'; }
		if(salary==''){	result = result + 'You must enter a salary in numerical format\n'; }
		if(per=='none'){	result = result + 'You must enter a salary rate\n'; }
		if(!jobtype){	result = result + 'You must select a job type\n'; }
		if(description==''){	result = result + 'You must enter a job description\n'; }
		
		if(result!=''){
			alert(result);
		}else{
			$('#'+form).submit();	
		}

		
	}
}

function search_jobs(){
	var cat = $('#category').val();
	var salary = $('#salary').val();
	var jobtype = $('#jobtype').val();
	
	$('#searchResults').empty();
	$('#searchResults').append('<img src="/images/loading.gif" />');
	
	
	$.post("/includes/ajax/search_jobs.php", {cat:cat, salary:salary, jobtype:jobtype}, function(data){ 
		$("#searchResults").empty();
		$("#searchResults").append(data);
	});
	//alert(email);
}

function add_group(){
	var group = $('#newGroup').val();
	$.post("/includes/ajax/add_group.php", {group:group}, function(data){ 
		if(substr(data, 12, 3)=="suc"){
			$('#newGroup').val('');
			$('#messages_group').empty();
			$('#messages_group').append(data);
			update_groups();
		}else{
			$('#messages_group').empty();
			$('#messages_group').append(data);
		}
	});
}

function update_groups(){
	$.post("/includes/ajax/update_groups.php", {}, function(data){ 
		$('#currGroups').empty();
		$('#currGroups').append(data);
	});
}

function update_url(){
	var url = $('#url').val();
	
	if(url==''){
		alert("You didn't enter a URL");	
	}else{
	
		$.post("/includes/ajax/update_url.php", {url:url}, function(data){
			if(substr(data, 12, 3)=="suc"){
				$('#urlDiv').empty();
				$('#urlDiv').append('<span class="fieldLabel">URL:</span>www.applyintime.com/schools/'+url);
			}
			$('#urlMsg').empty();
			$('#urlMsg').append(data);
			
			setTimeout(function(){ $('.error').fadeOut(500); }, 2000);
		});
	}
}


function delete_group(gid){
	$.post("/includes/ajax/delete_group.php", {gid:gid}, function(data){ 

		$('#messages_group').empty();
		$('#messages_group').append(data);	
		update_groups();
	});
}

function group_selected(){
	
	var result = "";
	var gid = $('#group').val();
	$(".checkedUser:checked").each( function () {
	result = result + $(this).val() + ",";
	});
	result = substr(result, 0, -1);

		$.post("/includes/ajax/group_selected.php", {users:result, gid:gid}, function(data){ 
			load_users();
		});

}

function load_users(){
	$('#userTable').empty();
	$('#userTable').append('<tr><td>Loading...</td></tr>');
	$.post("/includes/ajax/load_users.php", {}, function(data){ 
		$('#userTable').empty();
		$('#userTable').append(data);
	});
}