$(document).ready(function() {
	$("#bb_checkout").click(function() {
		document.location.href='vos-billets';
	});
	
	bb_igniteForms();
	$("#div_motdepasse").css('display','none');
	$(".a_motdepasse").click(function() {
		$("#fCustomer").toggle();
		$("#div_motdepasse").toggle();
		return false;
	});
	// tweak temporaire
	$("#callback_f").addClass("callback_f");
	// affiche la webcam dans la modal
	$("#div_webcam").css('display','none');
	$("#a_showWebCam").click(function(){
	    $("#div_webcam").toggle();
	    return false;
	});
	// affiche le tableau avec les cartes 
	//$("#a_showCards").click(function(){
	//    $("#tableCard").toggle();
	//   return false;
	//});
	// grise et desactive les moyens de paiements
	$("#frmCmcic input[type='submit']").attr("value", ""); 
	$("#div_modepaiement input[type='submit']").attr("disabled", "disabled");  
	$("#div_modepaiement input[type='submit']").animate({opacity:"0.3"});
	$("#conditionGE").click(function(){
		if ($("#conditionGE").is(":checked")){
			$("#div_modepaiement input[type='submit']").animate({opacity:"1"});
			$("#div_modepaiement input[type='submit']").removeAttr("disabled");
		}  else {
			$("#div_modepaiement input[type='submit']").attr("disabled", "disabled");
			$("#div_modepaiement input[type='submit']").animate({opacity:"0.3"});
		}
	});
	$(".a_toggleCardForm").click(function() {
                  
		$(this).next().toggle();
                $(this).toggleClass("headbox");
		return false;
	});
	$("#deleteCustomer").click(function() {
		if(window.confirm('Etes-vous certain de vouloir supprimer votre compte?')) {
			$.post(bb_u(), {
				iAction: 'delete_customer'
			}, function(xml){
				if(!bb_getError(xml,true)) bb_closeModalAndReloadPage();
			});
		}
	});

	$("#a_deconnexion").click(function() {
		document.location.href='deconnexion';
	});
	$("#a_customer_modif").click(function() {
		document.location.href='modification-de-compte';
	});
	$("#content_gauche").css({
		position:'absolute',
		top:'150px',
		left:'20px'
	});
	
	$("#cartUpdated").hide();
	bb_cartRecapListener();
	bb_dDateListener();
	bb_makeCoffee();
	$('.phone').mask("+99 99 999 99 ?99");
	$('.dob').mask("99-99-9999");
});
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
function bb_makeCoffee() {
	$(this).stopTime('coffee');
	var response_delay = 20000;
	$(this).everyTime(response_delay, 'coffee', function(){
		bb_coffeeShot();
	});
}
function bb_coffeeShot() {
	$.post(bb_u(), {
		iAction:'coffee_shot'
	}, function(data){
		//$('<span style="color:#FFFFFF">Gave Coffee '+data+' </span>').appendTo('#extramenu');
	});
}
function bb_getAlertBox(s,t,w,h) {
	w=!w ? 350 : w;
	h=!h ? 100 : h;
	Shadowbox.open({
        content:    'modal-alert.html?s='+s,
        player:     "iframe",
        title:      t ? t : "Oops",
        height:     h,
        width:      w
    });
}
function bb_redAlert(data) {
	//alert(data);
	$("#alert").hide().html(data).fadeIn("fast",function() {
		$(this).stopTime('alert');
		var response_delay = 3000;
		$(this).everyTime(response_delay, 'alert', function(){
			$(this).fadeOut("fast");
		});
	});
}
function bb_displayCardSuccess() {
	bb_getCart();
	var w = !w ? 350 : w;
	var h = !h ? 300 : h;
	var t = t ? t : "Merci";
	Shadowbox.open({
		content: 'carte-ajoutee',
		player: "iframe",
		title: t,
		height: h,
		width: w
	});
}
function bb_displayAccredSuccess() {
	var w = !w ? 350 : w;
	var h = !h ? 300 : h;
	var t = t ? t : "Merci";
	Shadowbox.open({
		options: { 
			onClose:function(){ document.location.href='/'; }
		},
		content: 'accred-confirmed',
		player: "iframe",
		title: t,
		height: h,
		width: w
	});
}
function bb_displaySuccess(u,m,t,w,h) {
	if (m) {
		w = !w ? 350 : w;
		h = !h ? 100 : h;
		Shadowbox.open({
			content: u,
			player: "iframe",
			title: t ? t : "Merci",
			height: h,
			width: w
		});
		$(this).stopTime('success');
		var response_delay = 3000;
		$(this).oneTime(response_delay, 'success', function(){
			Shadowbox.close();
		});
	}
	else {
		$("#success").load(u,function() {
			$(this).stopTime('success');
			var response_delay = 3000;
			$(this).oneTime(response_delay, 'success', function(){
				$(this).fadeOut("fast");
			});
		});
	}
}
function bb_trim(str) {
	var trimmed = str.replace(/^\s+|\s+$/g, '');
	return trimmed;
}
function bb_passwordSent() {
	$("#alert").hide().load('mot-de-passe-envoye').fadeIn("fast",function() {
		$(this).stopTime('alert');
		var response_delay = 3000;
		$(this).oneTime(response_delay, 'alert', function(){
			$(this).fadeOut("fast");
			bb_closeModal();
		});
	});
}
function bb_cartRecapListener1() {
	$(".numeric").keyup(function(e){
		if(e.keyCode!=8 && e.keyCode!=0 && (e.keyCode<48 || e.keyCode>57)) return false;
	});
	$(".bb_cartRecapCount").keyup(function(event){
		$(this).stopTime('controlled');
		var response_delay = 500;
		if ($(this).val().length > 0) {
			$(this).everyTime(response_delay, 'controlled', function(){
				bb_refreshStatus($('#panier'));
				$(this).stopTime('controlled');
				$.post(bb_u(), {
					iAction: 'update_entry',
					iCommandEntryCode: $(this).attr('rel'),
					iCount: $(this).val()
				}, function(xml){
					if(!bb_getError(xml,true)) bb_getCartRecap();
				});
			});
		}
	});
}
function bb_cartRecapListener() {
	/*
	$(".bb_cartRecapCount").change(function(){
		if ($(this).val() > 0) {
			bb_refreshStatus();
			$.post(bb_u(), {
				iAction: 'update_entry',
				iCommandEntryCode: $(this).attr('rel'),
				iCount: $(this).val()
			}, function(xml){
				bb_getError(xml,true);
				bb_getCartRecap();
			});
		}
	});
	*/
	$(".bb_cartRecapCount").click(function(){
		var id=$(this).attr('id').replace(/add-/,'');
		var iCount=$('#iCount-'+id).val();
		//alert(iCount);
		if (iCount > 0) {
			bb_refreshStatus($('#panier'));
			$.post(bb_u(), {
				iAction: 'update_entry',
				iCommandEntryCode: $(this).attr('rel'),
				iCount: iCount
			}, function(xml){
				bb_getError(xml,true);
				bb_getCartRecap();
			});
		}
	});
}
function bb_dDateListener() {
	$("#divSeatsForm").hide();
	$("#dDate").change(function(){
		if ($(this).val() > 0) {
			//$(".dDateDependant").attr("disabled", false);
			var iDateCode=$(this).val();
			bb_updateSeats(iDateCode);
		}
	});
	$("#divSeatsFormRefresh").click(function(){
		if ($("#dDate").val() > 0) {
			//$(".dDateDependant").attr("disabled", false);
			var iDateCode=$("#dDate").val();
			//alert(iDateCode);
			bb_updateSeats(iDateCode);
		}
	});
}
function bb_updateSeats(iDateCode) {
	if (iDateCode) {
		bb_refreshStatus($("#divSeatsForm").fadeIn("fast"));
		$.post(bb_u(), {
			iAction: 'seats_update',
			iDateCode: iDateCode
		}, function(data){
			$("#divSeatsForm").fadeOut("fast",function() {
				$(this).html(data).fadeIn("fast");
			});
		});
	}
}
function bb_u() {
	return 'formActionAjaxPhp';
}
function bb_getError(data,modal) {
	//alert('bb_getError : '+data);
	if(bb_trim(data)=="") return false;
	var u='soap_custom_errors';
	//var err = $(data).find('faultstring').text();
	$.post(u, {
		//debug:1,
		msg: data
	}, function(data){
		modal ? bb_getAlertBox(data) : bb_redAlert(data);
	});
	return true;
}
function bb_igniteForms() {
	//bb_getCart();
	//bb_getCards();
	//bb_getCustomer();
	$("form.ajxfrm").submit(function(){
		var action = $(this).attr('action');
		action=="" ? action=bb_u() : "";
		var fid = $(this).attr('id');
		var els = {};
		$(this).find('.ajxfrm').each(function(){
			//alert($(this).attr('name') + ": "+$(this).val());
			var theName = $(this).attr('name');
			els[theName] = $(this).val();
		});
		var bModal=$("#bModalAlert").val();
		bModal=bModal=="1" ? true : false;
		// CALLBACK FUNCTION
		var f = $(this).find('.callback_f').val();
		$.post(action, els, function(data){
			//alert('fds');
			data=bb_trim(data);
			if (!bb_getError(data,bModal)) {
				if (f != "") 
 					(typeof els['callback_pg']!='undefined' ? window[f](data,els['callback_pg']) : window[f](data));
			}
		});
		return false;
	});
}
function bb_closeModal() {
	parent.Shadowbox.close();
}
function bb_closeModalAndReloadPage(data) {
	parent.location.reload();
	parent.Shadowbox.close();
}
function bb_closeModalAndGoToPage(data,pg) {
	parent.location.href=pg;
	parent.Shadowbox.close();
}
function bb_reloadPage(data) {
	document.location.reload();
}
function bb_gotoPage(data,page) {
	document.location.href=page;
}
function bb_msg(data) {
	bb_getError(data);
}
function bb_refreshStatus(el) {
	el.html('<div align="center"><img src="/2009/assets/images/billeterie/ajax-loader.gif" /></div>');
}
function bb_addExistingMembershipCardDone(data) {
	bb_displayCardSuccess();
}
function bb_updateCart(iAction, iCommandEntryCode, iCount, iCategoryCode, iDateCode, iTariffZoneCode, win){
	$("#cartUpdated").hide();
	win=win=="" ? win="self" : win;
	bb_refreshStatus($('#panier'));
	//alert('iAction: '+iAction+' \n iDateCode: '+iDateCode+' \niCategoryCode: '+ iCategoryCode+' \niTariffZoneCode '+iTariffZoneCode+' \niCount '+iCount);
	$.post(bb_u(), {
			iAction: iAction,
			iCommandEntryCode: iCommandEntryCode,
			iCategoryCode: iCategoryCode,
			iCount: iCount,
			iDateCode: iDateCode,
			iTariffZoneCode: iTariffZoneCode
		},function(xml){
			//alert(xml);
			if (!bb_getError(xml, true)) {
				win ? parent.bb_getCart() : bb_getCart();
				bb_updateSeats($("#dDate").val());
				$("#cartUpdated").fadeIn("fast",function() {
					$(this).stopTime('cartUpdated');
					var response_delay = 3000;
					$(this).everyTime(response_delay, 'cartUpdated', function(){
						$(this).fadeOut("fast");
					});
				});
			}
		}
	);
}
function bb_updateCartRecap(iAction, iCommandEntryCode, iCount, iCategoryCode, iDateCode, iTariffZoneCode){
	bb_refreshStatus($('#panier'));
	//alert('iAction: '+iAction+' \n iDateCode: '+iDateCode+' \niCategoryCode: '+ iCategoryCode+' \niTariffZoneCode '+iTariffZoneCode+' \niCount '+iCount+'\nu: '+u);
	$.post(bb_u(), {
			iAction: iAction,
			iCommandEntryCode: iCommandEntryCode,
			iCategoryCode: iCategoryCode,
			iCount: iCount,
			iDateCode: iDateCode,
			iTariffZoneCode: iTariffZoneCode
		},function(xml){
			bb_getError(xml,true);
			bb_getCartRecap();
		}
	);
}
function bb_createCustomer1(sFirstname, sLastname, sEmail, sPassword, sCellularPhone){
	$.post(bb_u(), {
			iAction: iAction,
			sFirstname:sFirstname,
			sLastname:sLastname,
			sEmail:sEmail,
			sPassword:sPassword,
			sCellularPhone:sCellularPhone
		},function(xml){
			bb_gotoPage(xml,'[~385~]');
		}
	);
}
function bb_createCustomer(s,iAction){
	//alert(s);
	$.post(bb_u(), {
			iAction: iAction,
			s:s
		},function(xml){
			if(!bb_getError(xml,true)) bb_gotoPage(xml,'inscription-valide');
		}
	);
}
function bb_getCustomer(data) {
	if(!bb_getError(data)) document.location.href='modification-de-compte';
}
function bb_getCustomer1(){
	$.post(bb_u(), {
		iAction: 'customer'
		},function(xml){
			$("#bb_customer").hide().html(xml).fadeIn();
			//$("#bb_customerConn").hide().html(xml).fadeIn();
		}
	);
	return true;
}
function bb_getCart(){
	$.ajax({
		type: "POST",
		url: bb_u(),
		data: {
			iAction: 'status'
		},
		error: function(xhr, ajaxOptions, thrownError){
			alert(xhr.status);
		},
		success: function(xml){
			//alert(xml);
			$('#bb_panier').fadeOut("fast",
											function() {
												$(this).html(xml).fadeIn("fast");
											})	
			/*
			$('#bb_panier').stop().animate({
										color: "#ED687E"
									},500, 
										function() {
											$(this).html(xml).stop().animate({
												color: "#FFFFFF"
											},200);
										}
									);*/
		}
	});
}
function bb_getCartRecap(){
	bb_getCart();
	$.ajax({
		type: "POST",
		url: bb_u(),
		data: {
			iAction: 'recap'
		},
		error: function(xhr, ajaxOptions, thrownError){
			alert(xhr.status);
		},
		success: function(xml){
			//alert(xml);
			$('#panier').fadeOut("fast",
							function() {
								$(this).html(xml).fadeIn("fast");
								bb_cartRecapListener();
							}
						);
		}
	});
}
function bb_getUserInfoCart(){
	$.ajax({
		type: "POST",
		url: bb_u(),
		data: {
			iAction: 'customer'
		},
		error: function(xhr, ajaxOptions, thrownError){
			alert(xhr.status);
		},
		success: function(xml){
			//alert(xml);
			$('#bb_customer').fadeOut("fast",
							function() {
								$(this).html(xml).fadeIn("fast");
							}
						);
		}
	});
}
function bb_membershipCardModified() {
	parent.bb_getUserInfoCart();
	parent.bb_getCartRecap();
	parent.Shadowbox.close();
}
function bb_getCards(){
	$.ajax({
		type: "POST",
		url: bb_u(),
		data: {
			iAction: 'membership_cards'
		},
		error: function(xhr, ajaxOptions, thrownError){
			alert(xhr.status);
		},
		success: function(xml){
			//alert(xml);
			$('#bb_cards').html(xml);
		}
	});
}
function bb_showMemberForm(element) {
	$(element).hide();
	form=$(element).parents('form');
	$(form).find('DIV').show();
}
function bb_membershipCardModif(iMembershipCardCode) {
	/*
	$.ajax({
		type: "POST",
		url: bb_u(),
		data: {
			iAction: 'membership_card_modif',
			iMembershipCardCode: iMembershipCardCode
		},
		error: function(xhr, ajaxOptions, thrownError){
			alert(xhr.status);
		},
		success: function(data) {
			if(data) bb_getError(data,true);
			else $('#bb_cards').html(xml);
		}
	});
	*/
	var u='modif-carte.html?iMembershipCardCode='+iMembershipCardCode;
	Shadowbox.open({
        content:    u,
        player:     "iframe",
        title:      "Modifier les informations de la carte",
        height:     490,
        width:      450
    });
}
function bb_showMembershipCardDates(iMembershipCardCode) {
	//$('#membership_dates').find('DIV').show();
	$.ajax({
		type: "POST",
		url: bb_u(),
		data: {
			iAction: 'membership_dates',
			iMembershipCardCode: iMembershipCardCode
		},
		error: function(xhr, ajaxOptions, thrownError){
			alert(xhr.status);
		},
		success: function(data) {
			if(data) bb_getError(data,true);
			else $('#bb_cards').html(xml);
		}
	});
	//$(this.form).down('DIV').hide();
	//$(this.form.showForm).show();
}
function bb_addMembershipCard(iCategoryCode, iShippingModeCode, sFirstname, sLastname) {
	$.ajax({
		type: "POST",
		url: bb_u(),
		data: {
			iAction: 'add_membership_card',
			iCategoryCode: iCategoryCode,
			iShippingModeCode: iShippingModeCode,
			sFirstname: sFirstname,
			sLastname: sLastname
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert('error: '+xhr.status);
		},
		success: function(data) {
			//alert(data);
			if(!bb_getError(data,true)) bb_getCartRecap();//bb_reloadPage();
		}
	});
}
function bb_addExistingMembershipCard(sMembershipCardCode) {
	$.ajax({
		type: "POST",
		url: bb_u(),
		data: {
			iAction: 'add_membership_card',
			sMembershipCardCode: sMembershipCardCode.trim()
		},
		error: function(xhr, ajaxOptions, thrownError){
			alert(xhr.status);
		},
		success: function(data) {
			if(!bb_getError(data,true)) bb_getCartRecap();//bb_reloadPage();
		}
	});
}
function bb_addMembershipCardEntry(iMembershipCardCode, iDateCode, iCategoryCode, iTariffZoneCode, iCount) {
	if (iCount>0) {
		$.ajax({
			type: "POST",
			url: bb_u(),
			data: {
				iAction: 'add_entry',
				iMembershipCardCode: iMembershipCardCode,
				iDateCode: iDateCode,
				iCategoryCode: iCategoryCode,
				iTariffZoneCode: iTariffZoneCode,
				iCount: iCount
			},
			error: function(xhr, ajaxOptions, thrownError){
				alert(xhr.status);
			},
			success: function(data) {
				if (!bb_getError(data, true)) {
					bb_updateSeats($("#dDate").val());
					$("#cartUpdated").fadeIn("fast", function(){
						$(this).stopTime('cartUpdated');
						var response_delay = 3000;
						$(this).oneTime(response_delay, 'cartUpdated', function(){
							$(this).fadeOut("fast");
						});
					});
				}//bb_reloadPage();
			}
		});
	}
}

// TIMERS
jQuery.fn.extend({
	everyTime: function(interval, label, fn, times, belay) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, times, belay);
		});
	},
	oneTime: function(interval, label, fn) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, 1);
		});
	},
	stopTime: function(label, fn) {
		return this.each(function() {
			jQuery.timer.remove(this, label, fn);
		});
	}
});

jQuery.extend({
	timer: {
		guid: 1,
		global: {},
		regex: /^([0-9]+)\s*(.*s)?$/,
		powers: {
			// Yeah this is major overkill...
			'ms': 1,
			'cs': 10,
			'ds': 100,
			's': 1000,
			'das': 10000,
			'hs': 100000,
			'ks': 1000000
		},
		timeParse: function(value) {
			if (value == undefined || value == null)
				return null;
			var result = this.regex.exec(jQuery.trim(value.toString()));
			if (result[2]) {
				var num = parseInt(result[1], 10);
				var mult = this.powers[result[2]] || 1;
				return num * mult;
			} else {
				return value;
			}
		},
		add: function(element, interval, label, fn, times, belay) {
			var counter = 0;
			
			if (jQuery.isFunction(label)) {
				if (!times) 
					times = fn;
				fn = label;
				label = interval;
			}
			
			interval = jQuery.timer.timeParse(interval);

			if (typeof interval != 'number' || isNaN(interval) || interval <= 0)
				return;

			if (times && times.constructor != Number) {
				belay = !!times;
				times = 0;
			}
			
			times = times || 0;
			belay = belay || false;
			
			if (!element.$timers) 
				element.$timers = {};
			
			if (!element.$timers[label])
				element.$timers[label] = {};
			
			fn.$timerID = fn.$timerID || this.guid++;
			
			var handler = function() {
				if (belay && this.inProgress) 
					return;
				this.inProgress = true;
				if ((++counter > times && times !== 0) || fn.call(element, counter) === false)
					jQuery.timer.remove(element, label, fn);
				this.inProgress = false;
			};
			
			handler.$timerID = fn.$timerID;
			
			if (!element.$timers[label][fn.$timerID]) 
				element.$timers[label][fn.$timerID] = window.setInterval(handler,interval);
			
			if ( !this.global[label] )
				this.global[label] = [];
			this.global[label].push( element );
			
		},
		remove: function(element, label, fn) {
			var timers = element.$timers, ret;
			
			if ( timers ) {
				
				if (!label) {
					for ( label in timers )
						this.remove(element, label, fn);
				} else if ( timers[label] ) {
					if ( fn ) {
						if ( fn.$timerID ) {
							window.clearInterval(timers[label][fn.$timerID]);
							delete timers[label][fn.$timerID];
						}
					} else {
						for ( var fn in timers[label] ) {
							window.clearInterval(timers[label][fn]);
							delete timers[label][fn];
						}
					}
					
					for ( ret in timers[label] ) break;
					if ( !ret ) {
						ret = null;
						delete timers[label];
					}
				}
				
				for ( ret in timers ) break;
				if ( !ret ) 
					element.$timers = null;
			}
		}
	}
});

if (jQuery.browser.msie)
	jQuery(window).one("unload", function() {
		var global = jQuery.timer.global;
		for ( var label in global ) {
			var els = global[label], i = els.length;
			while ( --i )
				jQuery.timer.remove(els[i], label);
		}
	});


// TIMERS />
// MASKED INPUT
(function(a){var c=(a.browser.msie?"paste":"input")+".mask";var b=(window.orientation!=undefined);a.mask={definitions:{"9":"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"}};a.fn.extend({caret:function(e,f){if(this.length==0){return}if(typeof e=="number"){f=(typeof f=="number")?f:e;return this.each(function(){if(this.setSelectionRange){this.focus();this.setSelectionRange(e,f)}else{if(this.createTextRange){var g=this.createTextRange();g.collapse(true);g.moveEnd("character",f);g.moveStart("character",e);g.select()}}})}else{if(this[0].setSelectionRange){e=this[0].selectionStart;f=this[0].selectionEnd}else{if(document.selection&&document.selection.createRange){var d=document.selection.createRange();e=0-d.duplicate().moveStart("character",-100000);f=e+d.text.length}}return{begin:e,end:f}}},unmask:function(){return this.trigger("unmask")},mask:function(j,d){if(!j&&this.length>0){var f=a(this[0]);var g=f.data("tests");return a.map(f.data("buffer"),function(l,m){return g[m]?l:null}).join("")}d=a.extend({placeholder:"_",completed:null},d);var k=a.mask.definitions;var g=[];var e=j.length;var i=null;var h=j.length;a.each(j.split(""),function(m,l){if(l=="?"){h--;e=m}else{if(k[l]){g.push(new RegExp(k[l]));if(i==null){i=g.length-1}}else{g.push(null)}}});return this.each(function(){var r=a(this);var m=a.map(j.split(""),function(x,y){if(x!="?"){return k[x]?d.placeholder:x}});var n=false;var q=r.val();r.data("buffer",m).data("tests",g);function v(x){while(++x<=h&&!g[x]){}return x}function t(x){while(!g[x]&&--x>=0){}for(var y=x;y<h;y++){if(g[y]){m[y]=d.placeholder;var z=v(y);if(z<h&&g[y].test(m[z])){m[y]=m[z]}else{break}}}s();r.caret(Math.max(i,x))}function u(y){for(var A=y,z=d.placeholder;A<h;A++){if(g[A]){var B=v(A);var x=m[A];m[A]=z;if(B<h&&g[B].test(x)){z=x}else{break}}}}function l(y){var x=a(this).caret();var z=y.keyCode;n=(z<16||(z>16&&z<32)||(z>32&&z<41));if((x.begin-x.end)!=0&&(!n||z==8||z==46)){w(x.begin,x.end)}if(z==8||z==46||(b&&z==127)){t(x.begin+(z==46?0:-1));return false}else{if(z==27){r.val(q);r.caret(0,p());return false}}}function o(B){if(n){n=false;return(B.keyCode==8)?false:null}B=B||window.event;var C=B.charCode||B.keyCode||B.which;var z=a(this).caret();if(B.ctrlKey||B.altKey||B.metaKey){return true}else{if((C>=32&&C<=125)||C>186){var x=v(z.begin-1);if(x<h){var A=String.fromCharCode(C);if(g[x].test(A)){u(x);m[x]=A;s();var y=v(x);a(this).caret(y);if(d.completed&&y==h){d.completed.call(r)}}}}}return false}function w(x,y){for(var z=x;z<y&&z<h;z++){if(g[z]){m[z]=d.placeholder}}}function s(){return r.val(m.join("")).val()}function p(y){var z=r.val();var C=-1;for(var B=0,x=0;B<h;B++){if(g[B]){m[B]=d.placeholder;while(x++<z.length){var A=z.charAt(x-1);if(g[B].test(A)){m[B]=A;C=B;break}}if(x>z.length){break}}else{if(m[B]==z[x]&&B!=e){x++;C=B}}}if(!y&&C+1<e){r.val("");w(0,h)}else{if(y||C+1>=e){s();if(!y){r.val(r.val().substring(0,C+1))}}}return(e?B:i)}if(!r.attr("readonly")){r.one("unmask",function(){r.unbind(".mask").removeData("buffer").removeData("tests")}).bind("focus.mask",function(){q=r.val();var x=p();s();setTimeout(function(){if(x==j.length){r.caret(0,x)}else{r.caret(x)}},0)}).bind("blur.mask",function(){p();if(r.val()!=q){r.change()}}).bind("keydown.mask",l).bind("keypress.mask",o).bind(c,function(){setTimeout(function(){r.caret(p(true))},0)})}p()})}})})(jQuery);
// MASKED INPUT />

