jQuery.createSelection = function (field, start, end) {
	if( field.createTextRange ) {
		var selRange = field.createTextRange();
		selRange.collapse(true);
		selRange.moveStart('character', start);
		selRange.moveEnd('character', end);
		selRange.select();
	} else if( field.setSelectionRange ) {
		field.setSelectionRange(start, end);
	} else if( field.selectionStart ) {
		field.selectionStart = start;
		field.selectionEnd = end;
	}
	field.focus();
}

$(function () {
	/*	
	$('input[type=tel]').bind('keypress',function(e){
	var el = $(this);
	var key = !e.which ? e.keyCode : e.which;
	var val = el.val().replace(/\D/g,'');
	if(val.length > 6)
	el.val(val.substring(0,3) + '-' + val.substring(3,6) + '-' + val.substring(6));
	else if(val.length > 3)
	el.val(val.substring(0,3) + '-' + val.substring(3));
	else
	el.val(val);
	});
	*/
	$('input[type=email]').bind('keyup', function (e) {
		var key = !e.which ? e.keyCode : e.which;
		var el = $(this);
		var movementKeys = [8, 17, 18, 20, 35, 36, 37, 38, 39, 40];
		var rec = ['gmail.com', 'yahoo.com', 'ymail.com', 'hotmail.com', 'aol.com', 'live.com'];
		var acceptKey = true;
		for (var i = 0; i < movementKeys.length; i++) if (movementKeys[i] == key) acceptKey = false;
		if (acceptKey && el.val().match(/^[\w._-]+@[\w._-]+$/)) {
			var val = el.val();
			var domain = val.split('@')[1];
			if (domain.length > 1) {
				for (var i = 0; i < rec.length; i++) {
					if (rec[i].substring(0, domain.length) == domain) {
						el.val(val.split('@')[0] + '@' + rec[i]);
						$.createSelection(this, val.length, (val.length) + (rec[i].length - domain.length));
					}
				}
			}
		}
	})
	$('input.citybox').each(function (idx) {
		$('<ul id="' + $(this).attr('id') + '_list" style="display:none; min-width:' + $(this).innerWidth() + 'px; left:' + $(this).offset().left + 'px; top:' + ($(this).offset().top + $(this).outerHeight() - 1) + 'px" class="cityboxlist"></ul>').appendTo('body');
		this.htmllist = $('#' + $(this).attr('id') + '_list').first();
	}).bind('keyup', function (e) {
		var ele = $(this);
		var eleCode = $('#' + ele.attr('id') + '_code');
		var eleList = $('#' + ele.attr('id') + '_list');
		var key = !e.which ? e.keyCode : e.which;
		var blockedKeys = [38, 40, 13];
		var notBlocked = true;

		for (var i = 0; i < blockedKeys.length; i++)
			if (blockedKeys[i] == key)
				notBlocked = false;

		if (notBlocked && $(this).val().length >= 3) {
			$.ajax({
				url: '/js2/atsctrl/citybox/air.ashx',
				data: { 'v': $(this).val() }, //$(this).val().indexOf(';') == -1 ? $(this).val() : $(this).val().substring($(this).val().lastIndexOf(';') + 1, $(this).val().length)
				success: function (data) {
					var o;
					eval('o=' + data);
					ele.get(0).list = o.List;
					eleList.html('');
					for (var i = 0; i < o.List.length; i++) {
						eleList.append('<li>' + o.List[i].Name + '<small>IATA code: ' + o.List[i].Code + '</small></li>');
					}
					eleList.children('li').each(function (idx) {
						$(this).bind('click', { eleData: o.List[idx] }, function (e) {
							ele.val(e.data.eleData.Name);
							eleCode.val(e.data.eleData.Code);
							eleList.hide();
						}).bind('mouseenter mouseleave', function () {
							$(this).siblings('li.selected').removeClass('selected');
							$(this).toggleClass('selected');
						});
					});
					if (eleList.children('li').length > 0) {
						if (ele.get(0).isFocused) {
							eleList.fadeIn(100);
						} else {
							var itemInfo = o.List[0];
							ele.val(itemInfo.Name);
							$('#' + ele.attr('id') + '_code').val(itemInfo.Code);
						}
					} else
						eleList.hide();
				}
			});
		}
		else if ($(this).val().length == 0) {
			eleList.hide();
		}
	}).bind('keydown', function (e) {
		var key = !e.which ? e.keyCode : e.which;
		//console.info(key);
		//38 up, 40 down, 9 tab, 13 enter
		var ele = $(this);
		var eleCode = $('#' + ele.attr('id') + '_code');
		var eleList = $('#' + ele.attr('id') + '_list');
		function setInfoToSelected() {
			if (eleList.children('li.selected').length > 0) {
				var children = eleList.children('li');
				for (var i = 0; i < children.length; i++)
					if ($(children.get(i)).is('.selected')) {
						var itemInfo = ele.get(0).list[i];
						ele.val(itemInfo.Name);
						$('#' + ele.attr('id') + '_code').val(itemInfo.Code);
						break;
					}
			}
		}
		switch (key) {
			case 38:
				if (eleList.is(':visible') && eleList.children('li.selected').length > 0) {
					if (eleList.children('li').first().hasClass('selected')) {
						eleList.children('li').first().removeClass('selected');
						eleList.children('li').last().addClass('selected');
					}
					else {
						var sel = eleList.children('li.selected').first();
						sel.removeClass('selected');
						sel.prev('li').addClass('selected');
					}
				}
				else {
					eleList.children('li:last-child').addClass('selected');
				}
				setInfoToSelected();
				break;
			case 40:
				if (eleList.is(':visible') && eleList.children('li.selected').length > 0) {
					if (eleList.children('li').last().hasClass('selected')) {
						eleList.children('li').last().removeClass('selected');
						eleList.children('li').first().addClass('selected');
					}
					else {
						var sel = eleList.children('li.selected').first();
						sel.removeClass('selected');
						sel.first().next('li').addClass('selected');
					}
				}
				else {
					eleList.children('li:first-child').addClass('selected');
				}
				setInfoToSelected();
				break;
			case 13:
				setInfoToSelected();
				eleList.hide();
				break;
		}
	}).bind('blur', function (e) {
		this.isFocused = false;
		if (this.htmllist.is(':visible')) {
			var children = this.htmllist.children('li');
			var item = -1;
			for (var i = 0; i < children.length; i++) {
				if ($(children.get(i)).is('.selected')) {
					item = i;
					break;
				}
			}
			if (item == -1 && $(this).get(0).list.length > 0) item = 0;
			if (item != -1) {
				var itemInfo = $(this).get(0).list[item];
				$(this).val(itemInfo.Name);
				$('#' + $(this).attr('id') + '_code').val(itemInfo.Code);
			}
			this.htmllist.hide();
		}
	}).bind('focus', function (e) {
		this.isFocused = true;
	});

	$('input[alt]').after('<label class="hint"></label>').each(function () {
		$(this).parent().css('position', 'relative');
		var lbl = $(this).next('label.hint');
		var pos = $(this).position();
		pos.left += parseInt($(this).css('margin-left').replace('px', '')) + parseInt($(this).css('padding-left').replace('px', '')) + 3;
		pos.top += parseInt($(this).css('margin-top').replace('px', '')) + parseInt($(this).css('padding-top').replace('px', '')) - 2;
		lbl.text($(this).attr('alt'));
		lbl.css('top', (pos.top + 3) + 'px');
		lbl.css('left', pos.left + 'px');
		lbl.css('position', 'absolute');
		lbl.attr('for', $(this).attr('id'));
		if ($(this).val() == '') lbl.show();
		else lbl.hide();
	}).bind('keydown', function (e) {
		var key = !e.which ? e.keyCode : e.which;
		var ignoredKeys = [8, 9, 16, 17, 18, 20, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46];
		var cont = true;
		for (var i = 0; i < ignoredKeys.length; i++) {
			if (ignoredKeys[i] == key) {
				cont = false;
				break;
			}
		}
		if (!cont) return;
		$(this).next('label.hint').hide();
	}).bind('keyup', function (e) {
		if ($(this).val() == '')
			$(this).next('label.hint').show();
		else
			$(this).next('label.hint').hide();
	});
});
