function ajax_AddSpeaker(_mode)
{
	//alert(_mode);
	// Initialize parameters container
	
	var DynamicURL = "";
	// Start loop through all input objects with name=LIST
	
	if(_mode == "init")
	 {
		var params = ""; 
		DynamicURL="wrk/doStartReg.php";
		$('*[name=DATA_FIELD_1]').each(function()
									{ 
										
										// Check if the paramater is empty; this indicates it's the first item in the loop
										if(params.length == 0)
										 {
											// Create the first parameter
											params = $(this).attr('id') + "=" + $(this).val();	  
										 }
										else
										 {
											// Concat the next item in the loop to the parameter 
											params += "&" + $(this).attr('id') + "=" + $(this).val();	 
										 }
             				}); 
							
							// Get security field
							
							params += "&SID=" + Math.random();
							//alert(params);
							// Initilize ajax object
							var ADD_SPEAKER = $.ajax({
													// Define ajax url
													
													url: DynamicURL, 
													
													// Define ajax method
													type: "POST",
													
													// Prevent caching
													cache: false,
													
													// Pass paramaters to ajax object
													data: params,
													
													// Execute when ajax call was successful
													success: function(ajax)
																{
																	//alert(ajax);
																	setTimeout('ajax_ListProgramSpeakers()', 500);
																	if(ajax == "OK")
																	 {
																		  setTimeout('ajax_ListProgramSpeakers()', 500);
																	 }
																},
													// Execute when ajax call failed
													error: function(ajax)
																{
																		$('#dialog').html('<p>' + ajax.responseText + '</p>');
																		$('#dialog').dialog('open');
																}
											});
	 }
	 else
	 {
		DynamicURL="wrk/doAddSpeaker.php";  
		var params = "";
		$('*[name=DATA_FIELD_2]').each(function()
										{ 
											
											// Test if a required field has no data then set the error flag to true and exit the loop.
											if($(this).attr('class') == "field_sub" && $(this).val().length == 0)
											 {
												ERROR = true;
												//$(this).after('<small>This field is required.</small>');
												$(this).addClass("required_error");
												 $('#dialog').html('<p>Missing field(s)</p>');
												$('#dialog').dialog('open');
												setTimeout('$(\'#dialog\').dialog(\'close\');', 3000);
												return false;
											 }

										if(params.length == 0)
										 {
											// Create the first parameter
											params = $(this).attr('id') + "=" + $(this).val();	  
										 }
										else
										 {
											// Concat the next item in the loop to the parameter 
											params += "&" + $(this).attr('id') + "=" + $(this).val();	 
										 }
											 
								}); 

				if(ERROR)
				{
					return false; 
				}
				
				params += "&PROG_VP=" + $("#PROG_VP").val() + "&SID=" + Math.random();
				//alert("Add speaker " + params);
				
					// Initilize ajax object
				var ADD_SPEAKER = $.ajax({
										// Define ajax url
										
										url: DynamicURL, 
										
										// Define ajax method
										type: "POST",
										
										// Prevent caching
										cache: false,
										
										// Pass paramaters to ajax object
										data: params,
										
										// Execute when ajax call was successful
										success: function(ajax)
													{
														if(ajax == "OK")
														 {
															setTimeout('ajax_ListProgramSpeakers()', 500);
															$('#dialog').html('<p>Speaker added successfully.</p>');
															$('#dialog').dialog('open');
															setTimeout('$(\'#dialog\').dialog(\'close\');', 2000);
															$('*[name=DATA_FIELD_2]').each(function(){$(this).val('')});
															
														 }
														 else
														 {
															$('#dialog').html('<p>' + ajax + '</p>');
															$('#dialog').dialog('open');
														 }
														 setTimeout('ajax_ListProgramSpeakers()', 500);
														
													},
										// Execute when ajax call failed
										error: function(ajax)
													{
														alert('Error ' + ajax.responseText);	
													}
								});

	}
}

function ajax_ListProgramSpeakers()
{
	$("#PROGRAM_SPEAKERS").load("wrk/doListProgramSpeakers.php");
}


function ajax_RemoveProgramSpeaker(_id)
{
	$("#row_" + _id).addClass('delete');
	var REMOVE_SPEAKER = $.ajax({
						// Define ajax url
						
						url: 'wrk/doRemoveProgramSpeaker.php', 
						
						// Define ajax method
						type: "POST",
						
						// Prevent caching
						cache: false,
						
						// Pass paramaters to ajax object
						data: 'SYS_ID=' + _id,
						
						// Execute when ajax call was successful
						success: function(ajax)
									{
										if(ajax == "OK")
										 {
											
											$("#row_" + _id).remove();
											//setTimeout('ajax_ListProgramSpeakers()', 500);
										 
										 }
										 else
										 {
											$('#dialog').html('<p>' + ajax + '</p>');
											$('#dialog').dialog('open');
										 }
										 //setTimeout('ajax_ListProgramSpeakers()', 500);
										
									},
						// Execute when ajax call failed
						error: function(ajax)
									{
										alert('Error ' + ajax.responseText);	
									}
				});

}

function ajax_InvalidDelete()
{
	$('#dialog').html('<p>You can not delete the primary user for this program.(s)</p>');
	$('#dialog').dialog('open');
}

function ajax_CompileProgramData(_step, _index)
{
	var params = "";
	//DATA_FIELD_3
		$('*[name=' + _step + ']').each(function()
										{ 
											
											// Test if a required field has no data then set the error flag to true and exit the loop.
											if($(this).attr('class') == "field" || $(this).attr('class') == "field_sub")
											 {
												if(params.length == 0)
												 {
													// Create the first parameter
													params = $(this).attr('id') + "=" + $(this).val();	  
												 }
												else
												 {
													// Concat the next item in the loop to the parameter 
													params += "&" + $(this).attr('id') + "=" + $(this).val();	 
												 }
											 }
								}); 
		
		params += "&PROG_VP=" + $("#PROG_VP").val() + "&STEP=" + _index + "&SID=" + Math.random();
		//alert(params);
		var COMPILE_DATA = $.ajax({
						// Define ajax url
						
						url: 'wrk/doCompileProgramData.php', 
						
						// Define ajax method
						type: "POST",
						
						// Prevent caching
						cache: false,
						
						// Pass paramaters to ajax object
						data: params,
						
						// Execute when ajax call was successful
						success: function(ajax)
									{
											//alert(ajax);
											//$('#dialog').html('<p>' + ajax + '</p>');
											//$('#dialog').dialog('open');
									},
						// Execute when ajax call failed
						error: function(ajax)
									{
										alert('Error ' + ajax.responseText);	
									}
				});
		
}

function ajax_Submit()
{
	
	$("#div_Final").toggle('fast');
	$("#div_Final").html('Please wait ......');
	//$("#btnSubmit").hide();
	
	var COMPILE_DATA = $.ajax({
						// Define ajax url
						
						url: 'wrk/doSubmit.php', 
						
						// Define ajax method
						type: "POST",
						
						// Prevent caching
						cache: false,
						
						// Pass paramaters to ajax object
						data: null,
						
						// Execute when ajax call was successful
						success: function(ajax)
									{
											if(ajax == "OK")
											 {
												$("#div_Final").html('Thank you, your application was submitted successfull. You will be redirected shortly');
												//$("#div_Final").toggle('fast');
												setTimeout('location.reload(true)', 3000);
											 }
											 else
											 {
												$("#btnSubmit").show();
												$('#div_Final').html('<p>' + ajax + '</p>');
												//$('#dialog').dialog('open');
											 }
									},
						// Execute when ajax call failed
						error: function(ajax)
									{
										alert('Error ' + ajax.responseText);	
									}
				});
		
	
	//$("#div_Final").load("wrk/doSubmit.php");	
}