var xmlHTTP_VERSIONS = new Array('MSXML2.XMLHTTP.6.0', 'MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHttp');
var ajax_path = "https://www.iahperd.org/planner/";
var ERR_IMG = '<img src=' + ajax_path + 'IMAGES/error.gif' + ' border=0 title="Error">';
var EXP_IMG = '<img src=' + ajax_path + 'IMAGES/expired.gif' + ' border=0>';
var OK_IMG = '<img src=' + ajax_path + 'IMAGES/ok.gif' + ' border=0 title="Ok/Saved">';
var DUP_IMG = '<img src=' + ajax_path + 'IMAGES/dup.gif' + ' border=0>';
var WRK_IMG = '<img src=' + ajax_path + 'IMAGES/loading.gif' + ' border=0 width="40" height="10">';
var LOADING_IMG = '<img src=' + ajax_path + 'IMAGESloading.gif' + ' border=0>';
// #####################################################################################################################################
// Boolean Error Control
var ERR_Personal = false;
var ERR_Program_Title = false;
var ERR_Program_Description = false;
var ERR_Speakers = false;
var ERR_Category = false;
var ERR_Age = false;
var ERR_Length = false;
var ERR_Day_Time = false;
var ERR_Type = false;
var ERR_Students = false;
var ERR_Students_Needs = false;
var ERR_Req = false;
var ERR_Email = false;
var AJAXERROR = false;

// ####################################################################################################################################
function ajax_CreateForm(_class)
{
	//alert(_class);
	if ( typeof _class == "undefined") 
	{
		$('#main').html("<center><div class='ui-state-error'> <h2>Please select a valid classification. </h2></div></center>");
		// vriable is set and isnt falsish so it can be used;
		  return;
	} 
	$('#main').html("<center>" + WRK_IMG + " Loading Form .... </center>");
	var NEW_FORM = $.ajax({
					  		// Define ajax url
							url: "./wrk/doCreateForm.php", 
							
							// Define ajax method
							type: "POST",
							
							// Prevent caching
							cache: false,
							
							// Pass paramaters to ajax object
							data: "class=" + _class,
							
							// Execute when ajax call was successful
							success: function(ajax)
										{
											
											$('#main').html(ajax);
											$("#accordion").accordion({ header: "h3", icons: icons });
												var icons = {
															header: "ui-icon-circle-arrow-e",
															headerSelected: "ui-icon-circle-arrow-s"	
															};
											js__applyClass(_class);
											$("input[type=text]").bind("keypress keyup blur", 
														function(e)
															{
																$(this).removeClass("ui-state-error");
															}
													);
											js__Wrapper();
											$("#m_Class").val(_class);
											
										},
							// Execute when ajax call failed
							error: function(ajax)
										{
											alert('Error ' + ajax.responseText);	
										}
					});
					   	
}
// ####################################################################################################################################
function ajax_SubmitApplication()
{
	var params = "";
	// Start loop through all input objects with name=LIST

	$('.sql').each(function()
						{ 
								if($(this).val().length == 0)
								 {
									AJAXERROR = true;
									var pDIV = $(this).parents('div:eq(0)').attr('id');
//alert(pDIV);
									$(this).addClass("ui-state-error");
									$('#status').html(ERR_IMG + " Missing one or more required field in the <span style='font-weight: bold; font-size: 16px; color: red'>" + pDIV + " </span>section. Please check that section and look for fields marked with red.");
									setTimeout(function()
															{
																$(this).removeClass("ui-state-error");
																$('#status').html("");
															}, 10000);
									return false; 
								 }
								
								// 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();	 
								 }
								 AJAXERROR = false;
					}); 
	
	if(AJAXERROR)
	{
		return;
	}
	
	$("#btunSubmit").hide("slow");
	$('#status').html(WRK_IMG + " Please Wait ....");
	var SUBMIT_APPLICATION = $.ajax({
					  		// Define ajax url
							url: "./wrk/doSubmitForm.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)
										{
											switch(ajax)
											 {
												case "EXPIRED":
													$('#status').html(ERR_IMG + " The credit card you provided had expired.");
													setTimeout(function()
																		{
																			$("#btunSubmit").show("slow");
																		}, 3000);

												break;
												
												case "INVALID":
													$('#status').html(ERR_IMG + " The credit card you provided is invalid.");
													setTimeout(function()
																		{
																			$("#btunSubmit").show("slow");
																		}, 3000);
												break;
												
												case "ERR":
													$('#status').html(ERR_IMG + ' Unexpected Error');
												break;
												
												default:
													$('#form').html("<h2>" + ajax + "</h2>");
												break;
											 }
										},
							// Execute when ajax call failed
							error: function(ajax)
										{
											alert('Error ' + ajax.responseText);	
										}
					});
}
// ####################################################################################################################################
function ajax_Support()
{
		var params = "";
	// Start loop through all input objects with name=LIST

	$('*[name=support]').each(function()
						{ 
								if($(this).val().length == 0)
								 {
									AJAXERROR = true;
									$('#support_status').html('Missing one or more required field.');
									return false; 
								 }
								
								// 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();	 
								 }
								 AJAXERROR = false;
					}); 
	
	if(AJAXERROR)
	{
		return;
	}
	
	$('#support_status').html("Sending ....");
	//$('#status').html(WRK_IMG + " Please Wait ....");
	var SUBMIT_APPLICATION = $.ajax({
					  		// Define ajax url
							url: "./wrk/doSupport.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)
										{
											if(ajax == "OK")
											 {
												$('#support_status').html('Thank you, your comments will be reviewed within 24 hours');
												$('*[name=support]').val("");
											 }
											 else
											 {
												 $('#support_status').html('Unexpected error. Please try again');
											 }
										},
							// Execute when ajax call failed
							error: function(ajax)
										{
											alert('Error ' + ajax.responseText);	
										}
					});

}
