//----- APPLY FORM -----//
function sendDataApply()
{
	var _location = 0;
	/*
	if($("#liverpool:checked").val())
		_location = 1;
	if($("#manchester:checked").val())
		_location = 2;
	if($("#liverpool:checked").val() && $("#manchester:checked").val())
		_location = 3;
		*/
	
	$.post(
			"ajax/addParticipant.php",
			{
				/*type : ($("#rankin_by_you:checked").val())?(2):(0),*/
				type : 0,
				firstname : $("#firstname").val(), lastname : $("#lastname").val(), email : $("#email").val(),
				year : 0, month : 0, day : 0,
				address_1 : $("#address_1").val(), address_2 : "",
				postcode : $("#postcode").val(), city : $("#city").val(), country : "United Kingdom", nationality : $("#nationality").val(),
				sex : $("#sex").val(),
				image : $("#image_preview_input").val(),
				question_1 : $("#question_1").val(), question_2 : $("#question_2").val(), question_3 : $("#question_3").val(), question_4 : ($("#question_4:checked").val())?(1):(0),
				location : _location
			},
			getResultFormApply
		);
}
//check apply here form
function checkFormApply()
{
	//firstname
	var firstname = new BaseInput("firstname");
	firstname.addError("firstname_error","Required");
	firstname.verifyEmpty();
	
	//lastname
	var lastname = new BaseInput("lastname");
	lastname.addError("lastname_error","Required");
	lastname.verifyEmpty();
	
	//email
	var email = new BaseInput("email");
	email.addError("email_error","Incorrect Email");
	email.verifyEmail();
	
	//address_1
	var address_1 = new BaseInput("address_1");
	address_1.addError("address_1_error","Required");
	address_1.verifyEmpty();
	
	//postcode
	var postcode = new BaseInput("postcode");
	postcode.addError("postcode_error","Required");
	postcode.verifyEmpty();
	
	//city
	var city = new BaseInput("city");
	city.addError("city_error","Required");
	city.verifyEmpty();
	
	//nationality
	var nationality = new BaseInput("nationality");
	nationality.addError("nationality_error","Required");
	nationality.verifyEmpty();
	
	//sex
	var sex = new BaseInput("sex");
	sex.addError("sex_error","Required");
	sex.verifyEmpty();
	
	//image
	var image = new BaseInput("image_preview_input");
	image.addError("image_error","&nbsp;&nbsp;Image Required");
	if($("#image_preview_input").val() == "file to big" || $("#image_preview_input").val() == "an error occured" || $("#image_preview_input").val() == "undefined")
		image.displayMessage(false);
	else
		image.verifyEmpty();
	
	//question_1
	var question_1 = new BaseInput("question_1");
	question_1.addError("question_1_error","Required");
	question_1.verifyEmpty();
	
	//question_2
	var question_2 = new BaseInput("question_2");
	question_2.addError("question_2_error","Required");
	question_2.verifyEmpty();
	
	//question_3
	var question_3 = new BaseInput("question_3");
	question_3.addError("question_3_error","Required");
	question_3.verifyEmpty();
	
	//terms
	var terms = new CheckBox("terms");
	terms.addError("terms_error","Required");
	terms.verify();
	
	if(
		firstname.valid && lastname.valid &&
		address_1.valid && postcode.valid && city.valid && nationality.valid && sex.valid &&
		question_1.valid && question_2.valid && question_3.valid &&
		image.valid && terms.valid
	)
	{
		$("#general_error").css({'display':'none'});
		sendDataApply();
		/*
		$.post(
				"ajax/addParticipant.php",
				{
					type : ($("#rankin_by_you:checked").val())?(2):(0),
					firstname : firstname.value, lastname : lastname.value, email : email.value,
					year : 0, month : 0, day : 0,
					address_1 : address_1.value, address_2 : "",
					postcode : postcode.value, city : city.value, country : "United Kingdom", nationality : nationality.value,
					image : image.value,
					question_1 : question_1.value, question_2 : question_2.value, question_3 : question_3.value, question_4 : ($("#question_4:checked").val())?(1):(0)
				},
				getResultFormApply
			);
		*/
	}
	else
	{
		$("#general_error").css({'display':'block'});
	}
}


//----- RANKIN BY YOU -----//
function ukParticipant()
{
	if($("#country").val() == "United Kingdom")
		$("#uk").show();
}
function checkRankinByYouForm()
{
	//firstname
	var firstname = new BaseInput("firstname");
	firstname.addError("firstname_error","Required");
	firstname.verifyEmpty();
	
	//lastname
	var lastname = new BaseInput("lastname");
	lastname.addError("lastname_error","Required");
	lastname.verifyEmpty();
	
	//email
	var email = new BaseInput("email");
	email.addError("email_error","Incorrect Email");
	email.verifyEmail();
	
	//address_1
	var address_1 = new BaseInput("address_1");
	address_1.addError("address_1_error","Required");
	address_1.verifyEmpty();
	
	//postcode
	var postcode = new BaseInput("postcode");
	postcode.addError("postcode_error","Required");
	postcode.verifyEmpty();
	
	//city
	var city = new BaseInput("city");
	city.addError("city_error","Required");
	city.verifyEmpty();
	
	//nationality
	var nationality = new BaseInput("nationality");
	nationality.addError("nationality_error","Required");
	nationality.verifyEmpty();
	
	//sex
	var sex = new BaseInput("sex");
	sex.addError("sex_error","Required");
	sex.verifyEmpty();
	
	//nationality
	var country = new BaseInput("country");
	country.addError("country_error","Required");
	country.verifyEmpty();
	
	//image
	var image = new BaseInput("image_preview_input");
	image.addError("image_error","&nbsp;&nbsp;Image Required");
	if($("#image_preview_input").val() == "file to big" || $("#image_preview_input").val() == "an error occured" || $("#image_preview_input").val() == "undefined")
		image.displayMessage(false);
	else
		image.verifyEmpty();
	
	//terms
	var terms = new CheckBox("terms");
	terms.addError("terms_error","Required");
	terms.verify();
	
	if(
		firstname.valid && lastname.valid &&
		address_1.valid && postcode.valid && city.valid && nationality.valid && country.valid &&
		image.valid && terms.valid
	)
	{
		$("#general_error").css({'display':'none'});
		sendDataRankinByYou();
		/*
		var type_form = 1;
		if($("#country").val() == "United Kingdom")
			type_form = ($("#rankin_by_you:checked").val())?(2):(1);
		$.post(
				"ajax/addParticipant.php",
				{
					type : type_form,
					firstname : firstname.value, lastname : lastname.value, email : email.value,
					year : 0, month : 0, day : 0,
					address_1 : address_1.value, address_2 : "",
					postcode : postcode.value, city : city.value, country : country.value, nationality : nationality.value,
					image : image.value,
					question_1 : "", question_2 : "", question_3 : "", question_4 : ($("#question_4:checked").val())?(1):(0)
				},
				getResultFormApply
			);
			*/
	}
	else
	{
		$("#general_error").css({'display':'block'});
	}
}

function isUK() {
	if($("#country").val() == "United Kingdom")
		$("#rankin_by_you_div").fadeIn("fast");
	else
		$("#rankin_by_you_div").fadeOut("fast");
}

function sendDataRankinByYou()
{
	var type_form = 1;
	if($("#country").val() == "United Kingdom")
		type_form = ($("#rankin_by_you:checked").val())?(2):(1);
	$.post(
			"ajax/addParticipant.php",
			{
				type : type_form,
				firstname : $("#firstname").val(), lastname : $("#lastname").val(), email : $("#email").val(),
				year : 0, month : 0, day : 0,
				address_1 : "", address_2 : "",
				postcode : $("#postcode").val(), city : "", country : $("#country").val(), nationality : "",
				sex : $("#sex").val(),
				image : $("#image_preview_input").val(),
				question_1 : "", question_2 : "", question_3 : "", question_4 : 0
			},
			getResultFormApply
		);
}

//----- UTILS -----//
//get the result
function getResultFormApply(data)
{
	//hide form
	$("#form").css({display : "none"});
	
	//show result
	if(data == "success")
		$("#form_success").css({display : "block"});
	else
		$("#form_error").css({display : "block"});
}