/////////////////// candidate_step2.php /////////////////////

function validateCandidateStep2Fields() {
	var myForm = document.frm_candidate_step2;
	var requiredTextFields = new Array("first_name","last_name","user_mail", "phone_number", "profession", "workplace",  "captcha");
	var requiredSelectFields = new Array("birth_year", "origin_country", "family_status", "health_status", "citizenship", "years_of_education", "studies_field", "education_type", "mother_tongue");
	var requiredRadioFields = new Array("gender", "valid_passport", "driving_license", "convicted_criminal",
		"conviction", "police_investigation", "prison", "community_service");
	// situation
	var isSomethingMissing = false;
	isSomethingMissing = validateRequiredFields(myForm, requiredTextFields, requiredSelectFields, requiredRadioFields);
	displayErrorMsg(isSomethingMissing);
	return !isSomethingMissing;
}

function doCandidateStep2Submit() {
	var isOK = validateCandidateStep2Fields();
	if(isOK) {	
		startProcessAnimation('send_report');
		document.frm_candidate_step2.submit(); 
	} else {	
		stopProcessAnimation('send_report');		
	}
}



/////////////////// petition/join_form.php /////////////////////

function validateJoinFormFields() {
	var myForm = document.frm_join_form;
	var requiredTextFields = new Array("first_name", "last_name", "user_mail", "birth_year", "msg_content", "phone_number", "phone_number_2", "situation", "qualify", "captcha");
	
	var requiredRadioFields = new Array("gender", "valid_passport");
		
	var requiredSelectFields = new Array("birth_year", "citizenship", "native", "years_of_education", "occupation");
		
		
	var isSomethingMissing = false;
	isSomethingMissing = validateRequiredFields(myForm, requiredTextFields);
	displayErrorMsg(isSomethingMissing);
	return !isSomethingMissing;
}

function doJoinFormSubmit() {
	var isOK = validateJoinFormFields();
	if(isOK) {	
		startProcessAnimation('send_join_form');
		document.frm_join_form.submit(); 
	} else {	
		stopProcessAnimation('send_join_form');		
	}
}




/////////////////// petition/contact_us.php /////////////////////

function validateContactUsFields() {
	var myForm = document.frm_contact_us;
	var requiredTextFields = new Array("first_name","last_name","user_mail","subject","msg_content", "captcha");
	var isSomethingMissing = false;
	isSomethingMissing = validateRequiredFields(myForm, requiredTextFields);
	displayErrorMsg(isSomethingMissing);
	return !isSomethingMissing;
}

function doContactUsSubmit() {
	var isOK = validateContactUsFields();
	if(isOK) {	
		startProcessAnimation('send_contact_us');
		document.frm_contact_us.submit(); 
	} else {	
		stopProcessAnimation('send_contact_us');		
	}
}






/////////////////// petition/comments.php /////////////////////

function validateCommentsFields() {
	var myForm = document.frm_comments;
	var requiredTextFields = new Array("nick_name","email","comment"); // "captcha"
	var isSomethingMissing = false;
	isSomethingMissing = validateRequiredFields(myForm, requiredTextFields);
	
	// check if an avatar was selected
	if(myForm["avatar"].value == "") { // hidden field for avatar not selected
		isSomethingMissing = true;
		$("#avatars").addClass("field_error");
	} else { // hidden field for avatar selected
		$("#avatars").removeClass("field_error");
	}
	
	displayErrorMsg(isSomethingMissing);
	return !isSomethingMissing;
}

function doCommentsSubmit() {
	var myForm = document.frm_comments;
	var isOK = validateCommentsFields();
	if(isOK) {	
		startProcessAnimation('send_comment');
		insertComment();
	} else {	
		stopProcessAnimation('send_comment');		
	}
}

function insertComment() {
	//alert(escape(document.getElementById("comment").value));
	$("#error_msg").hide(); // don't show previous error msgs
	$.ajax({
		type: "POST",
		url: "ajax/insert_comment.php",
		data: "nick_name=" + encodeURI(document.getElementById("nick_name").value) +
				"&email=" + encodeURI(document.getElementById("email").value) +
				"&comment=" + encodeURI(document.getElementById("comment").value) +
				"&avatar=" + document.getElementById("avatar").value +				
				"&ip_address=" + document.getElementById("ip_address").value,
		dataType: "json",
		success : function(data){
			if (data.result == "ok") { // saved ok
				stopProcessAnimation('send_comment');
				$("#comment_form").hide(); 
				$("#comment_result").show(); 				
			} else {
				//alert(data.result);
			}
		},		
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			 $("#error_msg").text("Error occurred while saving your comment");
			 $("#error_msg").show(); 	
			 stopProcessAnimation('send_comment');
		}
	});	
}

/////////////////// petition/index.php /////////////////////

function saveSignature() {
	$("#error_msg").hide(); // don't show previous error msgs
	$.ajax({
		type: "POST",
		url: "ajax/save_signature.php",
		data: "first_name=" + document.getElementById("first_name").value +
				"&last_name=" + document.getElementById("last_name").value +
				"&user_mail=" + document.getElementById("user_mail").value +
				"&ip_address=" + document.getElementById("ip_address").value,
		dataType: "json",
		success : function(data){
			if (data.result == "already_exist") { // this user's email already exist
				$("#error_msg").text("Sorry, you have already signed this petition");		
				$("#error_msg").show(); 
				stopProcessAnimation('send_sign');
			} else if(data.result == "sending_error") { // problem sending email
				stopProcessAnimation('send_sign');
				$("#error_msg").text("Sorry, there was a problem sending your confirmation email");		
				$("#error_msg").show(); 
			} else { // saved ok
				stopProcessAnimation('send_sign');
				$("#sign_form").hide(); 
				$("#sign_result").show(); 				
			}
		},		
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			 /*
			 alert(XMLHttpRequest);		
			 alert(XMLHttpRequest.readyState);	 
			 alert(textStatus);
			 */
			 $("#error_msg").text("Error occurred while saving your signature");
			 $("#error_msg").show(); 	
			 stopProcessAnimation('send_sign');
		}
	});	
}


function validateSignFields() {

	var myForm = document.frm_sign;
	var requiredTextFields = new Array("first_name","last_name","user_mail");
	var isSomethingMissing = false;
	isSomethingMissing = validateRequiredFields(myForm, requiredTextFields);
	displayErrorMsg(isSomethingMissing);
	return !isSomethingMissing;
}

function doSignSubmit() {
	var isOK = validateSignFields();
	if(isOK) {	
		startProcessAnimation('send_sign');
		saveSignature();
	} else {	
		stopProcessAnimation('send_sign');		
	}
}



/////////////////////// donate.php /////////////////////

function validateDonationFields() {
	var myForm = document.frm_donation;
	var requiredTextFields = new Array("donation_amount","cc_num","cvv_num","cc_owner_name","id_num","user_mail","phone_number","receipt_name");
	var requiredSelectFields = new Array("cc_exp_year","cc_exp_month");
	var isSomethingMissing = false;
	
	isSomethingMissing = validateRequiredFields(myForm, requiredTextFields, requiredSelectFields);

	// check if read_toc is checked
	/*
	if($("#read_toc:checked").length == 0) { // read_toc not checked
		isSomethingMissing = true;
		$("#sp_read_toc").addClass("field_error");
	} else { // read_toc is checked
		$("#sp_read_toc").removeClass("field_error");
	}
	*/
	displayErrorMsg(isSomethingMissing);
	return !isSomethingMissing;
}

function doDonationSubmit() {
	var isOK = validateDonationFields();
	if(isOK) {	
		prepareTranzilaParams();
		startProcessAnimation('send_donation');
		document.frm_donation.submit(); 
	} else {	
		stopProcessAnimation('send_donation');		
	}
}

function prepareTranzilaParams() {
	var REGULAR_CREDIT = 1;
	var PAYMENTS_CREDIT = 8;
	var myForm = document.frm_donation;
	var numPayments = parseInt(myForm.donation_duration.options[myForm.donation_duration.selectedIndex].value);
	
	if(numPayments == 1) {
		myForm.cred_type.value = REGULAR_CREDIT;
		myForm.total_donation_amount.value = myForm.donation_amount.value;
	} else { // monthly payment
		myForm.cred_type.value = PAYMENTS_CREDIT;
		myForm.fpay.value = myForm.donation_amount.value;
		myForm.spay.value = myForm.donation_amount.value;
		myForm.npay.value = numPayments - 1;
		myForm.total_donation_amount.value = numPayments * parseInt(myForm.donation_amount.value);		
	}
	/*
	alert("cred_type = " + myForm.cred_type.value);
	alert("fpay = " + myForm.fpay.value);
	alert("spay = " + myForm.spay.value);
	alert("npay = " + myForm.npay.value);
	alert("total_donation_amount = " + myForm.total_donation_amount.value);
	*/
}

