$(document).ready(function() {
	
	$("#submitForm").click(function() {
		
		$(".error").html("");
		$(".info").html("");
		$("#spinner1").show();
		
		$.ajax({
			type: "POST",
			url: "submit",
			data: {
				lastname: $("input[name=lastname]").val(),
				firstname: $("input[name=firstname]").val(),
				email: $("input[name=email]").val(),
				postal: $("input[name=postal]").val(),
				mobile: $("input[name=mobile]").val(),
				checkRingtones: $("input[name=checkRingtones]:checked").val() ? true : false,
				checkRingtonesPopular: $("input[name=checkRingtonesPopular]:checked").val() ? true : false,
				checkRingtonesFeature: $("input[name=checkRingtonesFeature]:checked").val() ? true : false,
				checkMusic: $("input[name=checkMusic]:checked").val() ? true : false,
				checkMusicPopular: $("input[name=checkMusicPopular]:checked").val() ? true : false,
				checkMusicFeature: $("input[name=checkMusicFeature]:checked").val() ? true : false,
				checkContest: $("input[name=checkContest]:checked").val() ? true : false,
				checkWallpaper: $("input[name=checkWallpaper]:checked").val() ? true : false,
				checkVideo: $("input[name=checkVideo]:checked").val() ? true : false,
				requestType: "xml"
			},
			success: function(xml) {
				
				if($("result", xml).text() == "ok") {
					$("#txtmsgsent-info").html($("info", xml).text());
					
					if ($("category", xml) != null) {
						
						$("input[type=checkbox]").attr('checked', false);
						
						$("category", xml).each(function() {
							if ($(this).text() == 'RINGTONES_POPULAR') {
								$("input[name=checkRingtones]").attr('checked', true);
								$("input[name=checkRingtonesPopular]").attr('checked', true);
							}
							
							if ($(this).text() == 'RINGTONES_FEATURE') {
								$("input[name=checkRingtones]").attr('checked', true);
								$("input[name=checkRingtonesFeature]").attr('checked', true);
							}
						});
					}
				}
				else {
					if ($("errLastname", xml).text() != "") {
						$("#lastnameError").html($("errLastname", xml).text());
					}
					
					if ($("errFirstname", xml).text() != "") {
						$("#firstnameError").html($("errFirstname", xml).text());
					}
					
					if ($("errEmail", xml).text() != "") {
						$("#emailError").html($("errEmail", xml).text());
					}
					
					if ($("errPostal", xml).text() != "") {
						$("#postalError").html($("errPostal", xml).text());
					}
					
					if ($("errMobile", xml).text() != "") {
						$("#mobileError").html($("errMobile", xml).text());
					}
					
					if ($("errCheckbox", xml).text() != "") {
						$("#checkboxError").html($("errCheckbox", xml).text());
					}
				}
			},
			error: function() {
				$("#checkboxError").html("Internal Server Error");
			},
			timeout: function() {
				$("#checkboxError").html("Connection Timed Out");
			},
			complete: function() {
				$("#spinner1").hide();
			}
		});
		
		return false;
	});
	
	/*$("#confirmPin").click(function() {
		
		$(".error").html("");
		$(".info").html("");
		$("#spinner2").show();
		
		$.ajax({
			type: "POST",
			url: "confirmpin",
			data: {
				pin: $("input[name=pin]").val(),
				requestType: "xml"
			},
			success: function(xml) {
				
				if($("result", xml).text() == "ok") {
					$("#success-info").html($("info", xml).text());
				}
				else {
					if ($("errPin", xml).text() != "") {
						$("#pinError").html($("errPin", xml).text());
					}
				}
			},
			error: function() {
				$("#pinError").html("Internal Server Error");
			},
			timeout: function() {
				$("#pinError").html("Connection Timed Out");
			},
			complete: function() {
				$("#spinner2").hide();
			}
		});
		
		return false;
	});*/
	
	$("#modifySubmitPhone").click(function() {
		
		$(".error").html("");
		$(".info").html("");
		$("#spinner1").show();
		
		$.ajax({
			type: "POST",
			url: "sendmodifypin",
			data: {
				mobile: $("input[name=mobile]").val(),
				requestType: "xml"
			},
			success: function(xml) {
				
				if($("result", xml).text() == "ok") {
					$("#txtmsgsent-info").html($("info", xml).text());
					
				}
				else {
					
					if ($("errMobile", xml).text() != "") {
						$("#mobileError").html($("errMobile", xml).text());
					}
					
					if ($("errCheckbox", xml).text() != "") {
						$("#mobileError").html($("errCheckbox", xml).text());
					}
				}
			},
			error: function() {
				$("#checkboxError").html("Internal Server Error");
			},
			timeout: function() {
				$("#checkboxError").html("Connection Timed Out");
			},
			complete: function() {
				$("#spinner1").hide();
			}
		});
		
		return false;
	});
	
	$("#validatePin").click(function() {
	
		$(".error").html("");
		$(".info").html("");
		$("#spinner2").show();
		
		$.ajax({
			type: "POST",
			url: "validatepin",
			data: {
				pin: $("input[name=pin]").val(),
				requestType: "xml"
			},
			success: function(xml) {
				
				if($("result", xml).text() == "ok") {
					
					$("#modifyStep1").hide();
					$("#checkboxes").show();
					
					if ($("category", xml) != null) {
						
						$("input[type=checkbox]").attr('checked', false);
						
						$("category", xml).each(function() {
							if ($(this).text() == 'RINGTONES_POPULAR') {
								$("input[name=checkRingtones]").attr('checked', true);
								$("input[name=checkRingtonesPopular]").attr('checked', true);
							}
							
							if ($(this).text() == 'RINGTONES_FEATURE') {
								$("input[name=checkRingtones]").attr('checked', true);
								$("input[name=checkRingtonesFeature]").attr('checked', true);
							}
							
							if ($(this).text() == 'MUSIC_POPULAR') {
								$("input[name=checkMusic]").attr('checked', true);
								$("input[name=checkMusicPopular]").attr('checked', true);
							}
							
							if ($(this).text() == 'MUSIC_FEATURE') {
								$("input[name=checkMusic]").attr('checked', true);
								$("input[name=checkMusicFeature]").attr('checked', true);
							}
							
							if ($(this).text() == 'PROMOTIONS') {
								$("input[name=checkContest]").attr('checked', true);
							}
							
							if ($(this).text() == 'WALLPAPERS') {
								$("input[name=checkWallpaper]").attr('checked', true);
							}
							
							if ($(this).text() == 'VIDEOS') {
								$("input[name=checkVideo]").attr('checked', true);
							}
						});
					}
				}
				else {
					if ($("errPin", xml).text() != "") {
						$("#pinError").html($("errPin", xml).text());
					}
				}
			},
			error: function() {
				$("#pinError").html("Internal Server Error");
			},
			timeout: function() {
				$("#pinError").html("Connection Timed Out");
			},
			complete: function() {
				$("#spinner2").hide();
			}
		});
		
		return false;
	});
	
	$("#updateSubscription").click(function() {
		
		$(".error").html("");
		$(".info").html("");
		$("#spinner3").show();
		
		$.ajax({
			type: "POST",
			url: "updateSubscription",
			data: {
				mobile: $("input[name=mobile]").val(),
				checkRingtones: $("input[name=checkRingtones]:checked").val() ? true : false,
				checkRingtonesPopular: $("input[name=checkRingtonesPopular]:checked").val() ? true : false,
				checkRingtonesFeature: $("input[name=checkRingtonesFeature]:checked").val() ? true : false,
				checkMusic: $("input[name=checkMusic]:checked").val() ? true : false,
				checkMusicPopular: $("input[name=checkMusicPopular]:checked").val() ? true : false,
				checkMusicFeature: $("input[name=checkMusicFeature]:checked").val() ? true : false,
				checkContest: $("input[name=checkContest]:checked").val() ? true : false,
				checkWallpaper: $("input[name=checkWallpaper]:checked").val() ? true : false,
				checkVideo: $("input[name=checkVideo]:checked").val() ? true : false,
				requestType: "xml"
			},
			success: function(xml) {
				
				if($("result", xml).text() == "ok") {
					
					self.location="thankyou";
				}
				else {
					if ($("errCheckbox", xml).text() != "") {
						$("#checkboxError").html($("errCheckbox", xml).text());
					}
				}
			},
			error: function() {
				$("#checkboxError").html("Internal Server Error");
				$("#spinner3").hide();
			},
			timeout: function() {
				$("#checkboxError").html("Connection Timed Out");
				$("#spinner3").hide();
			},
			complete: function() {
				
			}
		});
		
		return false;
	});
});
