function submit_ajax_form(form_object, extraData, callback) {
	if (!extraData) extraData = new Array();
    	var options = { 
		success:       function(responseText, statusText, xhr, $form){
			if(typeof responseText != 'undefined' && responseText != 'null')
			{ 
				parsed_response = json_parse(responseText);
				if (parsed_response.form_html!='NULL') //null is a special output that indicates that currently existing form should be left untouched
				{
					$(form_object).replaceWith(parsed_response.form_html);
				}	
				$.triggerReady();
				if (typeof callback !== 'undefined')
				{	
					callback($(form_object), parsed_response);
				}
			}
		},  // post-submit callback 
			 
		// other available options: 
		url:       'ajax.php',    // override for form's 'action' attribute 
		data: extraData
   	};
	$(form_object).ajaxSubmit(options); 
}

function submit_search_agent_ajax_form(form_object, extraData, callback) {
	if (!extraData) extraData = new Array();
	$("div.sign_up_search_box form").css("display", "none");
	$("div.sign_up_search_box").append("<div class='ajax_loading_box ajax_loading_box_search_agent'><img src='images/working.gif'></div>");
	submit_ajax_form(form_object, extraData, callback); 
}

function sign_up_job_agent_callback(form, response)
{
	$("div.sign_up_search_box div.ajax_loading_box").remove();
	$("div.sign_up_search_box form").css("display", "block");
	if(typeof response.errors != 'undefined' && response.errors != 'null')
	{
		$("div.sign_up_search_box div.guest_agent_errors").html(response.errors);
	}
}

function wj_preload_multi_cities(state_selector, city_selector)
{	
	selected_states = $(state_selector+":checked").map(function () {
		return this.value;
	}).get();
	ajax_url = "ajax.php?page=search&action=load_cities_list&state_list="+(encodeURI(selected_states));
	jQuery.ajax({	
		url: ajax_url,
  		success: function(responseText) {
  			if(typeof responseText != 'undefined' && responseText != 'null')
  			{ 
				parsed_response = json_parse(responseText);
				currently_selected_city = $(city_selector).val();
				$(city_selector).html(parsed_response.city_list);
				$(city_selector).val(currently_selected_city);
			}	
	  	}
  	});

}

function initiate_search_form(previous_form) {
	$('div.search_results_left_tab_search legend').bind('click', function() {
		wj_search_tab_click($(this));
	});
	if (typeof previous_form !== 'undefined')
	{	
		previous_form.find('legend').each(function() {
			if ($(this).hasClass('search_tab_closed'))
			{
				$('div.search_results_left_tab_search fieldset.'+($(this).parent().attr('class'))+' legend').click();
			}
		});
	}
}

function wj_search_tab_click(target) {
	if (target.hasClass("search_tab_closed"))
	{
		//open section
		target.removeClass("search_tab_closed");
		target.parent().find('div').css('display', 'block');
	}
	else
	{
		//close section
		target.addClass("search_tab_closed");
		target.parent().find('div').css('display', 'none');
	}
}

$.getReadyList = function() {
	if(this.readyList != null) { this.myreadylist = [].concat(this.readyList); }
	return this.myreadylist;
};

$(document).ready(function() {
	readylist = $.getReadyList();
});

$.triggerReady = function() {
	$(readylist).each(function(){this();});
}


function process_delete_entry(link, refresh_table, custom_callback_f) {
	//this one is also used to move entries up/down and to delete photos/videos
	ajax_link = link.href;
	base_link = $('base').attr('href');
	ajax_link = ajax_link.replace(base_link, '');
	loading_seeker_section(link);
	
	$.ajax({
	        method: "get", 
		url: "ajax.php", 
		data: "_seo_url="+encodeURIComponent(ajax_link),
		success: function(responseText) {
			if(typeof responseText != 'undefined' && responseText != 'null')
			{ 		
				parsed_response = json_parse(responseText);
				clear_global_seeker_errors();
				if (refresh_table)
				{
					$(refresh_table).replaceWith(parsed_response['refresh_table']);
				}
				if (custom_callback_f)
				{
					custom_callback_f();
				}
			}	
		}
	});	
}

function process_edit_entry(link, form_object, what) {
	ajax_link = link.href?link.href:link;
	base_link = $('base').attr('href');
	ajax_link = ajax_link.replace(base_link, '');
	loading_seeker_section(link);

	$.ajax({
	    method: "get", 
		url: "ajax.php", 
		data: "_seo_url="+encodeURIComponent(ajax_link),
		success: function(responseText) {
			if(typeof responseText != 'undefined' && responseText != 'null')
			{
				parsed_response = json_parse(responseText);
				$("div."+what+"_main_box", document.register_seeker).replaceWith($("div."+what+"_main_box", parsed_response.form_html));
				clear_global_seeker_errors();
				$.triggerReady();
			}
		}
	});	
}

function form_section_do_hide(what, target) 
{
	display_field = 'input#display_'+what;
	$(display_field).val('0');
	loading_seeker_section(target);
	
	var options = { 
		success:       function(responseText, statusText, xhr, $form){
			if(typeof responseText != 'undefined' && responseText != 'null')
			{ 
				parsed_response = json_parse(responseText);
				$("div."+what+"_main_box", document.register_seeker).replaceWith($("div."+what+"_main_box", parsed_response.form_html));
				clear_global_seeker_errors();
				$.triggerReady();
			}
		},  // post-submit callback 
		url:       'ajax.php',    // override for form's 'action' attribute 
		data: {'action':'hide_section', display_field:'0'}
	};
	$(document.register_seeker).ajaxSubmit(options);
}

function profileSubsectionDeleteCallback() {
	/*is_profile_subsection_enabled("div.register_seeker2_div_experience", "div.experience_entries_list table tbody", "#display_position");
	is_profile_subsection_enabled("div.register_seeker2_div_education", "div.education_entries_list table tbody", "#display_education");*/
	/*if($('div.experience_entries_list table tbody').length==0)
	{
		$('div.experience_entries_list').css('display', 'none');
	}
	if($('div.education_entries_list table tbody').length==0)
	{
		$('div.education_entries_list').css('display', 'none');
	}*/
}

function submit_section_ajax_form(form_object, extraData, what, target) {
	if (!extraData) extraData = new Array();
	if(typeof target != 'undefined' && target != 'null')
	{
		loading_seeker_section(target);
	}
		
    var options = { 
		//target:        '#output1',   // target element(s) to be updated with server response 
		//beforeSubmit:  showRequest,  // pre-submit callback 
		success:       function(responseText, statusText, xhr, $form){
			if(typeof responseText != 'undefined' && responseText != 'null')
			{ 
				parsed_response = json_parse(responseText);
				$("div."+what+"_main_box", document.register_seeker).replaceWith($("div."+what+"_main_box", parsed_response.form_html));
				clear_global_seeker_errors();
				$.triggerReady();
			}
		},  // post-submit callback 
			 
		// other available options: 
		url:       'ajax.php',    // override for form's 'action' attribute 
		data: extraData
   	};
	$(form_object).ajaxSubmit(options); 
}

function clear_global_seeker_errors()
{
	$("div.edit_seeker_global_errors_box span.error").html("");
	$("div.edit_seeker_global_errors_box ol").remove();
}

function loading_seeker_section(target)
{
	if ($(target).closest("td").length)
	{
		//this is usually the case of edit/delete/move/cancel-edit
		$(target).closest("td").find("*").css("display", "none");
		$(target).closest("td").append("<div class='ajax_loading_box'><img src='images/working.gif'></div>");
	}
	else 
	{
		//this is usually add/cancel-add
		$(target).closest("div.register_seeker2_profile_div").css("display", "none");
		$("<div class='ajax_loading_box'><img src='images/working.gif'></div>").insertAfter($(target).closest("div.register_seeker2_profile_div"));		
	}	
}
