voxox.myaccount = function(){};

$(function() {

$('#smsContentResendMessage').hide();
$('#showSmsResendMessage').click(function (){ $('#smsContentResendMessage').toggle("slow"); });

voxox.myaccount.buildsmsverify = function(payment) {
	if (!voxox.myaccount.smsverify.loaded) {
		// set country code drop down behaviour and default the selection
		/*
		$("#smsccdropdown").change(function() {
			$("#smscc").val($("#smsccdropdown option:selected:first").val());
		});
		$("#smsresendccdropdown").change(function() {
			$("#resmscc").val($("#smsresendccdropdown option:selected:first").val());
		});
		$("#smsccdropdown").change();
		$("#smsresendccdropdown").change();	
		*/
		// set up all the SMS dialogs
		var validationAttempts = 0;
		var sendAttempts = 0;
		$('#smsDialog').jqm(
			{overlay:80,modal:true,onShow:function(h) {h.w.hide(); h.w.css("visibility","visible"); h.w.fadeIn("fast");},
			onHide:function(h) {h.w.fadeOut("fast");h.w.css("visibility","hidden");h.w.show();if(h.o)h.o.fadeOut("slow");}}
		);
		$('#smsCode').jqm(
			{overlay:80,modal:true,onShow:function(h) {h.w.hide(); h.w.css("visibility","visible"); h.w.fadeIn("fast");},
			onHide:function(h) {h.w.fadeOut("fast");h.w.css("visibility","hidden");h.w.show();if(h.o)h.o.fadeOut("slow");}}
		);
		$('#smsFailed').jqm(
			{overlay:80,modal:true,onShow:function(h) {h.w.hide(); h.w.css("visibility","visible"); h.w.fadeIn("fast");},
			onHide:function(h) {h.w.fadeOut("fast");h.w.css("visibility","hidden");h.w.show();if(h.o)h.o.fadeOut("slow");}}
		);
		$('#smsMessageSent').jqm(
			{overlay:80,modal:true,onShow:function(h) {h.w.hide(); h.w.css("visibility","visible"); h.w.fadeIn("fast");},
			onHide:function(h) {h.w.fadeOut("fast");h.w.css("visibility","hidden");h.w.show();if(h.o)h.o.fadeOut("slow");}}
		);
		// define the functions for button clicks (should probably split these functions out better)
		$("#sendSMS").click(function(){
			var c = $("#CC_SMS_Verify").val().replace(/\+/g,"");
			var p = $("#P_SMS_Verify").val().replace(/\-/g,"");
			var number = c+p;
			if(p != "Enter Number" && p.length >= 6) {
				if (sendAttempts == 3) {
					$("#smsCode").jqmHide();
					$("#smsFailed").jqmShow();
					return;
				}
				if (number) {
					number = number.match(/\d/g).join('');
					$("#resmsnumber").val(($("#smsnumber").val()));
					$("#resmscc").val($("#smscc").val());
					voxox.callGateway("smsverify","sendCode",[voxox.userdata.voxox_id,number],function(data) {
						//sendAttempts++;
						$.cookie('smssent',sendAttempts);
						$("#smsDialog").jqmHide();
						if (data.success)
							$("#smsCode").jqmShow();
						else
							voxox.alert("sms_error","Error",data.result,{}, function(){$("#smsDialog").jqmShow();});
					});
				}
			}
		});
		$("#resendSMSCode").click(function(){
			var c = $("#CC_SMS_Resend").val().replace(/\+/g,"");
			var p = $("#P_SMS_Resend").val().replace(/\-/g,"");
			var number = c+p;
			if(p != "Enter Number" && p.length >= 6) {
				if (sendAttempts == 1) {
					$("#smsCode").jqmHide();
					$("#smsFailed").jqmShow();
					return;
				}
				if (number) {
					number = number.match(/\d/g).join('');
					voxox.callGateway("smsverify","sendCode",[voxox.userdata.voxox_id,number],function(data) {
						//sendAttempts++;
						$.cookie('smssent',sendAttempts);
						$("#smsCode").jqmHide();
						if (data.success)
							voxox.alert("sms_error","Resent","Text message resent.",{},
							 function(){$("#smsCode").jqmShow();$("#smsresendblock").hide();});
						else
							voxox.alert("sms_error","Error",data.result,{}, function(){$("#smsCode").jqmShow();});
					});
				}	
			}
		});
		$("#sendSMSCode").click(function(){
			var code = $("#smscode").val();
			if (code) {
				code = code.match(/\d/g).join('');
				if (validationAttempts == 2) {
					$("#smsCode").jqmHide();
					voxox.alert("sms_error","Error","You have exceeded the allowed number of validation attempts.",{}, function(){$("#smsFailed").jqmShow();});
					return;
				}
				voxox.callGateway("smsverify","validateCode",[voxox.userdata.voxox_id,code],function(data) {
					$("#smsCode").jqmHide();
					if (data.success) {
						voxox.alert("sms_error","Activated!","Please relaunch VoxOx to start placing calls. Your VoxOx Phone has been activated.");//TODO: show confirmation page
						voxox.userdata.hasPaymentValidated = true;
						voxox.userdata.hasValidated = true;
					}
					else {
						validationAttempts++;
						voxox.alert("sms_error","Error",data.result,{}, function(){$("#smsCode").jqmShow();});
					}
				});
			}
		});
		$(".showSmsAlt").click(function(){
			$("#smsDialog").jqmHide();
			$("#smsFailed").jqmShow();
			$("#smsCode").jqmHide();
			$("#smsMessageSent").jqmHide();
		});
		
		$(".cancelSms").click(function(){
			$("#smsDialog").jqmHide();
			$("#smsFailed").jqmHide();
			$("#smsCode").jqmHide();
			$("#smsMessageSent").jqmHide();
		});
		
		$(".cancelSmsVerify").click(function(){
			$("#smsDialog").jqmHide();
			$("#smsFailed").jqmHide();
			$("#smsCode").jqmHide();
			$("#smsMessageSent").jqmHide();
			
			$('#SMSVerifyCancel').jqm(
				{overlay:80,modal:true,onShow:function(h) {h.w.fadeIn("fast");},onHide:function(h) {h.w.fadeOut("fast");if(h.o)h.o.fadeOut("slow");}}
			).draggable();
			$('#SMSVerifyCancel').jqmShow();
			$.cookie("smsCancelled","true");
		});
		
		$("#sendAltMessage").click(function(){
			var message = $("#smsaltmessage").val();
			voxox.callGateway("smsverify","sendSupportEmail",[voxox.userid,voxox.username,message],function(data) {
				$("#smsFailed").jqmHide();
				$("#smsMessageSent").jqmShow();
			});
		});

		voxox.myaccount.smsverify.loaded = true;
        
	}
	if (voxox.userdata.hasCode)
		$("#smsCode").jqmShow();
	else
		$("#smsDialog").jqmShow();
		$("#CC_SMS_Verify").blur();
		$("#P_SMS_Verify").focus();
}
voxox.myaccount.smsverify = function(){};

});
