// JavaScript Document
/* ----------------------------------------------------------------------------
 * Copyright (c) 2010 Moblico Solutions, LLC. All Rights Reserved
 *
 * This software is the property of Moblico Solutions, LLC and contains HIGHLY 
 * CONFIDENTIAL and PROPRIETARY INFORMATION. Use, disclosure, reproduction, 
 * modification, distribution or other use of this software and information is 
 * permitted only in strict compliance with an express written agreement with 
 * Moblico Solutions, LLC.
 * ----------------------------------------------------------------------------
 */
$(document).ready(function(event) {
	
	var emailOn = window.location.search.indexOf("email_on") > 0 ? "y" : "";
	var phoneOn = window.location.search.indexOf("phone_on") > 0 ? "y" : "";
	
	$("#phone_on").attr("checked", false);
	
	if(phoneOn || (!phoneOn && !emailOn)) {
		$("#phone_on").attr("checked", true);
	}
	
	if(emailOn) {
		$("#email_on").attr("checked", true);
	}
	
	Moblico.regDisableCheckbox(".disablecheckbox", true);
	
	$("#mainform").validate({
		rules : {
			phone : {
				required : function() {
					return $("#phone_on").is(":checked");
				},
				moblicophonenumber : true
			},
			email_address : {
				required : function() {
					return $("#email_on").is(":checked");
				},
				email : true
			},
			first_name : {
				validatenameformat : true
			},
			last_name : {
				validatenameformat : true
			},
			message_permission : {
				required : true	
			}
		},
		submitHandler : function(form) {
			
			// check age
			if(document.getElementById("agefld")) {
				if(document.getElementById("agefld").value.length > 0) {
					var ageval = parseInt(document.getElementById("agefld").value, 10);
					if(!Moblico.validateAge(ageval, null, null)) {
						alert("Please enter an age within 12 and 120.");
						return false;
					}
				}
			}
			
			var emailon = $("#email_on").is(":checked");
			var phoneon = $("#phone_on").is(":checked");
			
			if(!emailon && !phoneon) {
				alert("Please enter at least one method of receiving notifications.");
				return;
			}
			
			$("#message_permission").val("YES");
			
			var delprefs = new Array();
			$("#email_permission").val("YES");
			if(emailon) {
				// $("#email_permission").val("YES");
				delprefs.push("email");
			} else {
				// $("#email_permission").val("NO");
			}
			if(phoneon) {
				delprefs.push("sms");
			}
			$("#delivery_preferance").val(delprefs.join(","));
			// alert($("#message_permission").val() + " : " + $("#email_permission").val());
			// alert($("#delivery_preferance").val());
			// return;
			form.submit();	
		},
		errorClass : "errorField",
		errorPlacement : function(error, elem) {
			if($(elem).attr("id") == "message_permission") {
				$(".markrequiredmarker").after(error);
			} else {
				Moblico.errorPlacement(error, elem);
			}
		}
	});						   
});