		
function formWarning(id,content) {
	$("#"+id).html(content);
	$("#"+id).show();
	setTimeout(function() {
		$("#"+id).slideUp();
		$("#"+id).html("");
	},5000);
}
function processSignupForm() {
	if($("#signupFormInProgress").val() == "0") {
		$("#signupFormInProgress").val("1");
		var d = $("#signupForm").serialize();
		$("#ws_signupFormButton").html("جاري الإرسال ..");
		$("#ws_signupFormButton").attr("onclick","return false;");			
		$.ajax({ type: "POST", cache: false, url: "../api/?ac="+Math.random(), data: d,
			success: function(json,status,xhr){
				if(xhr.status == 200) {
					if(json["status"] == "success") {
					// Start
						$("#ws_signupFormSuccessEmail").html(json["response"]["Email"]);
						$("#ws_signupForm").hide();
						$("#ws_signupFormSuccessContainer").show();
					// End
					} else { formWarning("ws_signupFormWarning",json["status_description"]); }
				} else { formWarning("ws_signupFormWarning","البيانات التي أرسلها الخادم غير صحيحة."); }
				$("#signupFormInProgress").val("0");
				$("#ws_signupFormButton").html("ارسال النموذج");
				$("#ws_signupFormButton").attr("onclick","processSignupForm();");
			},
			error: function() {
				formWarning("ws_signupFormWarning","لا يمكن التواصل مع الخادم، يرجى التأكد من الإتصال بالشبكة.");
				$("#signupFormInProgress").val("0");
				$("#ws_signupFormButton").html("ارسال النموذج");
				$("#ws_signupFormButton").attr("onclick","processSignupForm();");
			}
		});
	}
}
function processRecoveryForm() {
	if($("#recoveryFormInProgress").val() == "0") {
		$("#recoveryFormInProgress").val("1");
		var d = $("#recoveryForm").serialize();
		$("#ws_recoveryFormButton").html("جاري الإرسال ..");
		$("#ws_recoveryFormButton").attr("onclick","return false;");			
		$.ajax({ type: "POST", cache: false, url: "../api/?ac="+Math.random(), data: d,
			success: function(json,status,xhr){
				if(xhr.status == 200) {
					if(json["status"] == "success") {
					// Start
						$("#ws_recoveryFormSuccessEmail").html(json["response"]["Email"]);
						$("#ws_recoveryForm").hide();
						$("#ws_recoveryFormSuccessContainer").show();
					// End
					} else { formWarning("ws_recoveryFormWarning",json["status_description"]); }
				} else { formWarning("ws_recoveryFormWarning","البيانات التي أرسلها الخادم غير صحيحة."); }
				$("#recoveryFormInProgress").val("0");
				$("#ws_recoveryFormButton").html("ارسال النموذج");
				$("#ws_recoveryFormButton").attr("onclick","processRecoveryForm();");
			},
			error: function() {
				formWarning("ws_recoveryFormWarning","لا يمكن التواصل مع الخادم، يرجى التأكد من الإتصال بالشبكة.");
				$("#recoveryFormInProgress").val("0");
				$("#ws_recoveryFormButton").html("ارسال النموذج");
				$("#ws_recoveryFormButton").attr("onclick","processRecoveryForm();");
			}
		});
	}
}
function processActivatesForm() {
	var d = $("#activatesForm").serialize();			
	$.ajax({ type: "POST", cache: false, url: "../api/?ac="+Math.random(), data: d,
		success: function(json,status,xhr){
			if(xhr.status == 200) {
				if(json["status"] == "success") {
					// Start
					$("#activatesSuccessEmail").html(json["response"]["Email"]);
					$("#activatesProcessingContainer").hide();
					$("#ws_activatesSuccessContainer").show();
					// End
				} else { $("#activatesProcessingContainer").html(json["status_description"]); }
			} else { $("#activatesProcessingContainer").html("البيانات التي أرسلها الخادم غير صحيحة."); }
		},
		error: function() {
			$("#activatesProcessingContainer").html("لا يمكن التواصل مع الخادم، يرجى التأكد من الإتصال بالشبكة.");
		}
	});
}
function processActivatepForm() {
	var d = $("#activatepForm").serialize();			
	$.ajax({ type: "POST", cache: false, url: "../api/?ac="+Math.random(), data: d,
		success: function(json,status,xhr){
			if(xhr.status == 200) {
				if(json["status"] == "success") {
					// Start
					$("#activatepSuccessEmail").html(json["response"]["Email"]);
					$("#activatepProcessingContainer").hide();
					$("#ws_activatepSuccessContainer").show();
					// End
				} else { $("#activatepProcessingContainer").html(json["status_description"]); }
			} else { $("#activatepProcessingContainer").html("البيانات التي أرسلها الخادم غير صحيحة."); }
		},
		error: function() {
			$("#activatepProcessingContainer").html("لا يمكن التواصل مع الخادم، يرجى التأكد من الإتصال بالشبكة.");
		}
	});
}
function livesortingFormProcess() {
	if($("#livesortingFormInProgress").val() == "0") {
		$("#livesortingFormInProgress").val("1");
		var d = $("#livesortingForm").serialize();
		$.ajax({ type: "POST", cache: false, url: "../api/?ac="+Math.random(), data: d,
			success: function(json,status,xhr){
				if(xhr.status == 200) {
					if(json["status"] == "success") {
						if(json["response"].length > 0) {
							var isdefault = "";
							var o = "<div class=\"ws_candidateContainer\" style=\"border-top: 0px;\">";
							for(var i=0; i<json["response"].length; i++) {										
								if(json["response"][i]["IsDefault"] == "1") {
									isdefault = "style=\"background-color: #f9f9f9\"";
								} else {
									isdefault = "";
								}																
								o += "	<div class=\"ws_candidateBox\" "+isdefault+">";
								o += "		<div class=\"ws_candidateRank\">"+(i+1)+"</div>";
								o += "		<div class=\"ws_candidatePhoto\"><img src=\"https://abdullahalkandari.com/"+json["response"][i]["Photo"]+"\" class=\"ws_candidateAvatar\" /></div>";
								o += "		<div class=\"ws_candidateVotes\"><img src=\"html/images/blank.png\" class=\"votedIcon\" /> "+parseInt(json["response"][i]["TotalVotes"]).toLocaleString()+" صوت</div>";
								o += "		<div class=\"ws_candidateName\">"+json["response"][i]["Name"]+"</div>";
								o += "	</div>";
							}
							o += "</div>";
							$("#livesortingResult").html(o);
							setTimeout(function() { livesortingFormProcess(); },60000);
						} else {
							$("#livesortingResult").html("خطأ أثناء استدعاء البيانات. يرجى إعادة تشغيل التطبيق والمحاولة مرة أخرى.");				
						}
					} else {
						$("#livesortingResult").html(json["status_description"]);
					}
				} else { 
					$("#livesortingResult").html("البيانات التي أرسلها الخادم غير صحيحة.");
				}
				$("#livesortingFormInProgress").val("0");
			},
			error: function() {
				$("#livesortingFormInProgress").val("0");
				$("#livesortingResult").html("لا يمكن التواصل مع الخادم، يرجى التأكد من الإتصال بالشبكة.");
			}
		});
	}	
}
function livevotingFormProcess() {
	if($("#livevotingFormInProgress").val() == "0") {
		$("#livevotingFormInProgress").val("1");
		var d = $("#livevotingForm").serialize();
		$.ajax({ type: "POST", cache: false, url: "../api/?ac="+Math.random(), data: d,
			success: function(json,status,xhr){
				if(xhr.status == 200) {
					if(json["status"] == "success") {
						if(json["response"].length > 0) {
							var o = "<div class=\"ws_candidateContainer\" style=\"border-top: 0px; padding-top: 0px; padding-bottom: 0px;\">";
							var TotalRatio = (parseInt(json["response"][0]["TotalVoters"]) < 1 || isNaN(parseInt(json["response"][0]["TotalVoters"]))) ? "0%" : (100*parseInt(json["response"][0]["TotalVoted"])/parseInt(json["response"][0]["TotalVoters"])).toFixed(0).toString() + "%";							
							var TotalMaleRatio = (parseInt(json["response"][0]["MaleTotalVoters"]) < 1 || isNaN(parseInt(json["response"][0]["MaleTotalVoters"]))) ? "0%" : (100*parseInt(json["response"][0]["MaleTotalVoted"])/parseInt(json["response"][0]["MaleTotalVoters"])).toFixed(0).toString() + "%";							
							var TotalFemaleRatio = (parseInt(json["response"][0]["FemaleTotalVoters"]) < 1 || isNaN(parseInt(json["response"][0]["FemaleTotalVoters"]))) ? "0%" : (100*parseInt(json["response"][0]["FemaleTotalVoted"])/parseInt(json["response"][0]["FemaleTotalVoters"])).toFixed(0).toString() + "%";							
							o += "		<div class=\"ws_candidateRank\">"+parseInt(json["response"][0]["TotalVoted"]).toLocaleString()+" ("+TotalRatio+") صوتا</div>";
							o += "		<div class=\"ws_candidateName\">من "+parseInt(json["response"][0]["TotalVoters"]).toLocaleString()+" صوتا</div>";									
							o += "		<div class=\"ws_candidateName\" style=\"margin-bottom: 20px;\"><img src=\"html/images/blank.png\" class=\"ws_maleIconB\" /> ذكور "+parseInt(json["response"][0]["MaleTotalVoted"]).toLocaleString()+" ("+TotalMaleRatio+") من "+parseInt(json["response"][0]["MaleTotalVoters"]).toLocaleString()+" صوتا<br /><img src=\"html/images/blank.png\" class=\"ws_femaleIconB\" /> إناث "+parseInt(json["response"][0]["FemaleTotalVoted"]).toLocaleString()+" ("+TotalFemaleRatio+") من "+parseInt(json["response"][0]["FemaleTotalVoters"]).toLocaleString()+" صوتا</div>";										
							var elbc = "";
							var TotalAreaRatio = 0;
							TotalMaleRatio = TotalFemaleRatio = 0;
							var TotalCommitteeRatio = [];
							for(var i=0; i<json["response"][0]["Committees"].length; i++) {
								o += "<div class=\"ws_livevotingarearecordContainer\">";
								TotalAreaRatio = (parseInt(json["response"][0]["Committees"][i]["TotalVoters"]) < 1 || isNaN(parseInt(json["response"][0]["Committees"][i]["TotalVoters"]))) ? "0%" : (100*parseInt(json["response"][0]["Committees"][i]["TotalVoted"])/parseInt(json["response"][0]["Committees"][i]["TotalVoters"])).toFixed(0).toString() + "%";
								o += "<div class=\"ws_livevotingarea\"><table cellpadding=\"0\" cellspacing=\"0\" class=\"width100pr borderBox\"><tr><td class=\"width50pr righted\">"+json["response"][0]["Committees"][i]["Area"]+"</td><td class=\"width50pr lefted\">"+parseInt(json["response"][0]["Committees"][i]["TotalVoted"]).toLocaleString()+" ("+TotalAreaRatio+") من "+parseInt(json["response"][0]["Committees"][i]["TotalVoters"]).toLocaleString()+"</td></tr></table></div>";
								if(json["response"][0]["Committees"][i]["Male"] != undefined) {
									TotalMaleRatio = (parseInt(json["response"][0]["Committees"][i]["Male"]["TotalVoters"]) < 1 || isNaN(parseInt(json["response"][0]["Committees"][i]["Male"]["TotalVoters"]))) ? "0%" : (100*parseInt(json["response"][0]["Committees"][i]["Male"]["TotalVoted"])/parseInt(json["response"][0]["Committees"][i]["Male"]["TotalVoters"])).toFixed(0).toString() + "%";
									o += "<div class=\"ws_livevotinggender\"><table cellpadding=\"0\" cellspacing=\"0\" class=\"width100pr borderBox\"><tr><td class=\"width50pr righted\"><img src=\"html/images/blank.png\" class=\"ws_maleIconB\" /> لجان الذكور</td><td class=\"width50pr lefted\">"+parseInt(json["response"][0]["Committees"][i]["Male"]["TotalVoted"]).toLocaleString()+" ("+TotalMaleRatio+") من "+parseInt(json["response"][0]["Committees"][i]["Male"]["TotalVoters"]).toLocaleString()+"</td></tr></table></div>";
								}
								if(json["response"][0]["Committees"][i]["Female"] != undefined) {
									TotalFemaleRatio = (parseInt(json["response"][0]["Committees"][i]["Female"]["TotalVoters"]) < 1 || isNaN(parseInt(json["response"][0]["Committees"][i]["Female"]["TotalVoters"]))) ? "0%" : (100*parseInt(json["response"][0]["Committees"][i]["Female"]["TotalVoted"])/parseInt(json["response"][0]["Committees"][i]["Female"]["TotalVoters"])).toFixed(0).toString() + "%";
									o += "<div class=\"ws_livevotinggender\"><table cellpadding=\"0\" cellspacing=\"0\" class=\"width100pr borderBox\"><tr><td class=\"width50pr righted\"><img src=\"html/images/blank.png\" class=\"ws_femaleIconB\" /> لجان الإناث</td><td class=\"width50pr lefted\">"+parseInt(json["response"][0]["Committees"][i]["Female"]["TotalVoted"]).toLocaleString()+" ("+TotalFemaleRatio+") من "+parseInt(json["response"][0]["Committees"][i]["Female"]["TotalVoters"]).toLocaleString()+"</td></tr></table></div>";
								}
								o += "</div>";
							}
							o += "</div>";
							o += "<div class=\"ws_footerDisclaimer\">الإجمالي وإجمالي الذكور والإناث والمناطق مبني على إجمالي الاقتراع وليس الفرز.</div>";
							$("#livevotingResult").html(o);
							setTimeout(function() { livevotingFormProcess(); },60000);
						} else {
							$("#livevotingResult").html("خطأ أثناء استدعاء البيانات. يرجى إعادة تشغيل التطبيق والمحاولة مرة أخرى.");				
						}
					} else {
						$("#livevotingResult").html(json["status_description"]);
					}
				} else { 
					$("#livevotingResult").html("البيانات التي أرسلها الخادم غير صحيحة.");
				}
				$("#livevotingFormInProgress").val("0");
			},
			error: function() {
				$("#livevotingFormInProgress").val("0");
				$("#livevotingResult").html("لا يمكن التواصل مع الخادم، يرجى التأكد من الإتصال بالشبكة.");
			}
		});
	}	
}
