var params, objTmp;

function confirmAppointment(id)
{
	$("#buttons"+id).hide();
	$("#loading"+id).show();
	$.ajax({
		type: "POST",
		url: "/ajax/confirm_appointment.php",
		data: "id=" + id,
		success: function(msg) {
			if (msg != "success")
			{
				$("#loading"+id).hide();
				$("#buttons"+id).show();
				alert(msg);
			}
			else
			{
				$("#pending"+id).animate( { height: "hide", opacity: "hide", paddingTop: 0, paddingBottom: 0 }, 400, function() {
					$("#reserved"+id).animate( { height: "show", opacity: "show" }, 400 );
				});
			}
		}
	});
}

function rejectAppointment(id)
{
	if ( confirm("WARNING: This will permanently delete this appointment application.\n\nDo you wish to continue?") )
	{
		$("#buttons"+id).hide();
		$("#loading"+id).show();
		$.ajax({
			type: "POST",
			url: "/ajax/reject_appointment.php",
			data: "id=" + id,
			success: function(msg) {
				if (msg != "success")
				{
					$("#loading"+id).hide();
					$("#buttons"+id).show();
					alert(msg);
				}
				else
				{
					$("#pending"+id).animate( { height: "hide", opacity: "hide", paddingTop: 0, paddingBottom: 0 }, 400, function() {
						$("#available"+id).animate( { height: "show", opacity: "show" }, 400 );
					});
				}
			}
		});
	}
}

function removeAppointment(id)
{
	if ( confirm("WARNING: This will permanently remove this appointment application.\n\nDo you wish to continue?") )
	{
		$("#buttons"+id).hide();
		$("#loading"+id).show();
		$.ajax({
			type: "POST",
			url: "/ajax/reject_appointment.php",
			data: "id=" + id,
			success: function(msg) {
				if (msg != "success")
				{
					$("#loading"+id).hide();
					$("#buttons"+id).show();
					alert(msg);
				}
				else
				{
					$("#reserved"+id).animate( { height: "hide", opacity: "hide", paddingTop: 0, paddingBottom: 0 }, 400, function() {
						$("#available"+id).animate( { height: "show", opacity: "show" }, 400 );
					});
				}
			}
		});
	}
}

function confirmRequest(id)
{
	$("#buttons"+id).hide();
	$("#loading"+id).show();
	$.ajax({
		type: "POST",
		url: "/ajax/confirm_request.php",
		data: "exhibitor_id=" + getElement("exhibitor_id").value +
			  "&department=" + getElement("department_field").options.selectedIndex + 
			  "&day=" + getElement("day_field").options.selectedIndex + 
			  "&slot_id=" + id +
			  "&fname=" + getElement(id+"name_field").value +
			  "&sname=" + getElement(id+"surname_field").value +
			  "&email=" + getElement(id+"email_field").value +
			  "&telephone=" + getElement(id+"telephone_field").value +
			  "&company=" + getElement(id+"company_field").value +
			  "&comments=" + getElement(id+"comments_field").value,
		success: function(msg) {
			if (msg != "success")
			{
				$("#loading"+id).hide();
				$("#buttons"+id).show();
				alert(msg);
			}
			else
			{
				$("#request"+id).animate( { height: "hide", opacity: "hide", paddingTop: 0, paddingBottom: 0 }, 400, function() {
					$("#pending"+id).animate( { height: "show", opacity: "show" }, 400 );
				});
			}
		}
	});
}

function showConfirm(id)
{
	$("#available"+id).animate( { height: "hide", opacity: "hide" }, 150, function() {
		$("#request"+id).animate( { height: "show", opacity: "show" }, 300, function() {
			$("#"+id+"name_field").focus();
		});
	});	
}

function hideConfirm(id)
{
	$("#request"+id).animate( { height: "hide", opacity: "hide" }, 300, function() {
		$("#available"+id).animate( { height: "show", opacity: "show" }, 150 );
	});	
}

function saveProfile(id,logo)
{
	getElement("name_field").disabled = true;
	getElement("logo_field").disabled = true;
	getElement("url_field").disabled = true;
	getElement("telephone_field").disabled = true;
	getElement("description_field").disabled = true;
	$.ajax({
		type: "POST",
		url: "/ajax/save_profile.php",
		data: "id=" + id +
			  "&name=" + escape(getElement("name_field").value) +
			  "&logo=" + escape(logo) +
			  "&url=" + escape(getElement("url_field").value) +
			  "&telephone=" + escape(getElement("telephone_field").value) +
			  "&description=" + escape(getElement("description_field").value),
		success: function(msg) {
			getElement("name_field").disabled = false;
			getElement("logo_field").disabled = false;
			getElement("url_field").disabled = false;
			getElement("telephone_field").disabled = false;
			getElement("description_field").disabled = false;
			if (msg != "success")
			{
				alert("Error saving profile: " + msg);
			}
			else
			{
				$("#name_text").html(displayParagraph(getElement("name_field").value));
				document.title = getElement("name_field").value+" Profile : a4uexpo London, 14th - 15th October 2008";
				$("#company_url")[0].href = getElement("url_field").value;
				$("#company_url").html(getElement("url_field").value);
				$("#company_logo")[0].href = getElement("url_field").value;
				$("#description_text").html(displayParagraph(getElement("description_field").value));
				hideEditProfile('company_profile');
			}
		}
	});
}

function ajaxFileUpload(id)
{
	if ( $("#logo_field").val() )
	{
		$.ajaxFileUpload
		(
			{
				url:'/ajax/save_profile.php',
				secureuri:false,
				fileElementId:'logo_field',
				dataType: 'json',
				success: function (data, status)
				{
					if(typeof(data.error) != 'undefined')
					{
						if(data.error != '')
						{
							alert( data.error );
						}
						else
						{
							if (id)
							{
								$("#company_logo_img")[0].src = "/images/"+data.msg;
								$("#logo_field").val("");
								$("#logo_text").html("/images/"+data.msg);
								saveProfile(id,data.msg);
							}
							else
							{
								alert( data.msg );
							}
						}
					}
				},
				error: function (data, status, e)
				{
					alert('Very sorry '+e);
				}
			}
		)
	}
	else
	{
		saveProfile(id);
	}
	return false;
}

function ajaxStaffFileUpload(id,exhibitor_id,picture_field)
{
	if ( $("#"+picture_field).val() )
	{
		$.ajaxFileUpload
		(
			{
				url:'/ajax/save_staff_profile.php?field='+picture_field,
				secureuri:false,
				fileElementId:picture_field,
				dataType: 'json',
				success: function (data, status)
				{
					if(typeof(data.error) != 'undefined')
					{
						if(data.error != '')
						{
							alert( data.error );
						}
						else
						{
							if (id)
							{
								saveStaffProfile(id,exhibitor_id,data.msg);
							}
							else
							{
								alert( data.msg );
							}
						}
					}
				},
				error: function (data, status, e)
				{
					alert('Very sorry '+e);
				}
			}
		)
	}
	else
	{
		saveStaffProfile(id,exhibitor_id);
	}
	return false;
}

function saveStaffProfile(staff_id,exhibitor_id,picture)
{
	getElement("st_name_field_"+staff_id).disabled = true;
	getElement("st_surname_field_"+staff_id).disabled = true;
	getElement("st_job_field_"+staff_id).disabled = true;
    getElement("st_picture_field_"+staff_id).disabled = true;
	getElement("st_description_field_"+staff_id).disabled = true;
	//$("#loading"+id).show();
	$.ajax({
		type: "POST",
		url: "/ajax/save_staff_profile.php",
		data: "staff_id=" + staff_id +
			  "&exhibitor_id=" + exhibitor_id +
			  "&fname=" + escape(getElement("st_name_field_"+staff_id).value) +
			  "&sname=" + escape(getElement("st_surname_field_"+staff_id).value) +
			  "&job_title=" + escape(getElement("st_job_field_"+staff_id).value) +
              "&picture=" + escape(picture) +
			  "&description=" + escape(getElement("st_description_field_"+staff_id).value),
		success: function(msg) {
			getElement("st_name_field_"+staff_id).disabled = false;
			getElement("st_surname_field_"+staff_id).disabled = false;
			getElement("st_job_field_"+staff_id).disabled = false;
            getElement("st_picture_field_"+staff_id).disabled = false;
			getElement("st_description_field_"+staff_id).disabled = false;
            if ( getElement("staff_profiles_header").style.display == "none" ) showVisibleSlide("staff_profiles_header");
			if (staff_id == 0)
            {
                $("#staff_profiles").append(msg);
                $("#st_name_field_0").val("");
                $("#st_surname_field_0").val("");
                $("#st_job_field_0").val("");
                $("#st_picture_field_0").val("");
                $("#st_description_field_0").val("");
            }
            else
            {
                if (picture != undefined) $("#staff_picture_"+staff_id)[0].src = "/images/"+picture;
                $("#st_picture_field_"+staff_id).val("");
                $("#logo_text_"+staff_id).html("/images/"+picture);
                $("#st_name_text_"+staff_id).html($("#st_name_field_"+staff_id).val()+" "+$("#st_surname_field_"+staff_id).val());
                $("#st_job_text_"+staff_id).html($("#st_job_field_"+staff_id).val());
                $("#st_description_text_"+staff_id).html(displayParagraph($("#st_description_field_"+staff_id).val()));
            }
            hideEditProfile("st_profile_"+staff_id);
		}
	});
}
