﻿function PhoneValidationCallback(result, phone)
{
	var currentPhone = $("#ctl00_ContentPlaceHolder1_Phone")[0].value;
	if(currentPhone != phone)
		return;
		
	if(result == "")
	{	//OK
		$("#ctl00_ContentPlaceHolder1_PhoneValidator")[0].innerHTML = "&nbsp;";
	}
	else
	{
		$("#ctl00_ContentPlaceHolder1_PhoneValidator")[0].innerHTML = result;
		$("#ctl00_ContentPlaceHolder1_PhoneValidator")[0].style.visibility = "visible";
	}
}

/**
 * jQuery TextBox
 * Version 0.1 - 18/03/2008
 * @author Dale Harvey <harveyd@gmail.com>
 *
 * A combination of a text input and a drop down
 * select box, used by
 * http://code.google.com/p/jqueryspreadsheet/
 *
 **/
(
	function($)
	{
		
		//конструктор объекта
		$.fn.textbox = function(options) 
		{
			// Add items to the list
			var addItem = function(tb,list,item)
			{
				var normNumber = item;
				var dispNumber = item;
				if(item.indexOf("|") != -1)
				{
					normNumber = item.substr(0, 11);
					dispNumber = item.substr(12);
				}

				var textBox = tb;
				var li = $("<li id='li" + normNumber + "' />").append($("<div style='overflow:hidden; padding:3px;'><div style='float:left; text-align:left;'><a id='num" + normNumber + "' href='#'>" + dispNumber + "</a></div> <div style='float:right; vertical-align:middle;'>&nbsp;<a id='del" + normNumber + "' href=\"#\"><img src='img/del.png' alt='' style='border:0' onmouseover=\"this.src='img/del_hov.png'\" onmouseout=\"this.src='img/del.png'\" /></a></div></div>"));

				//<a href='#'>"+item+"</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </div>"));
				//li.append($(""));
				list.append(li);

				//debugger;
				$("#num" + normNumber).mousedown(function(e)
				{
					//debugger;
					list.hide();
					textBox.val($(e.target).text());
					    
					if(typeof opts.onSelect == "function")
						 opts.onSelect($(e.target).text());
				});

				$("#del" + normNumber).mousedown(function(e)
				{
					deleteItem = true;
					//debugger;
					$("#li" + normNumber).fadeOut("normal");
					//list.remove();
					//delete phone number from cookies
					var recentPhonesCookie = $.cookie("RecentPhones");
					var recentPhones = recentPhonesCookie.split(",");
					var newRecentPhones = new Array();
					for(var i = 0; i < recentPhones.length; i++)
					{
						if(recentPhones[i].substr(0, 11) != normNumber)
						{
							newRecentPhones.push(recentPhones[i]);
						}
					}
					
					var newRecentPhonesStr = newRecentPhones.join(",");
					if(newRecentPhonesStr.length == 0)
					{
						newRecentPhonesStr = null;
					}
					var date = new Date();
                    date.setTime(date.getTime() + (200 * 24 * 60 * 60 * 1000));
                    $.cookie("RecentPhones", newRecentPhonesStr, { path: '/', expires: date });
				});
			};

			$.fn.textbox.defaults = 
			{
				items:      [],     // Default list
				onSelect:   null,   // Callback for item selected
				onChange:   null    // Callback for text changed
			};

			// default options used on initialisation
			// and arguments used on later calls
			var opts = $.extend({}, $.fn.textbox.defaults, options);
			var args = arguments;
			var deleteItem = false;

			/**
			 * Entry point
			 */
			//debugger;
			return this.each(function() 
				{
					// Initialisation
					if(typeof $.data(this,"textbox") == "undefined")
					{
						var $t   = $(this);
						var height = this.offsetHeight;
						var width  = this.offsetWidth;

						$t.addClass("textbox");

						// The drop down list
						var list = $("<ul class='textboxlist' />").insertAfter($t).width(width);

//            .mousedown(function(e)
//            {
//                list.hide();
//                $t.val($(e.target).text());
//                    
//                if(typeof opts.onSelect == "function")
//                     opts.onSelect($(e.target).text());
//            });       
            
			// The arrow that shows the list onclick
			/*var arrow = $("<div class='textboxarrow'/>"
			).insertAfter($t).bind('mousedown',function()
			{
				list.toggle();
				return false; // prevent wierd opera 
			});               // context menu
			*/
						$(window).resize(function()
							{
								var top  = $t.offset().top;
								var left = $t.offset().left;

								//arrow.css("left",((left+width)-16)+"px"
								//).css("top",((top+height)-16)+"px");
							    
								list.css("left",(left)+"px"
								).css("top",(top+height)+"px");
							});

						$t.click(function()
							{
								// Make sure the text is selected so
								// users can type to overwrite current text
								//this.select();
							}
						).focus(function()
							{
								if(opts.items.length == 0)
								{
									return;
								}
								
								list.show();
								var val = $(this).val();
								//this.select();
							
								// Run callback if the user has typed
								// something new
								$(this).bind('blur',function textboxBlur()
									{
										if(deleteItem)
										{
											deleteItem = false;
											return;
										}
										list.hide();
										$(this).unbind("blur", textboxBlur).unbind("keyup");

										if(typeof opts.onChange == "function"
											&& $(this).val() != val
											&& $(this).val() != "")
										{
											opts.onChange($(this).val());
										}
									}
								).bind('keyup',function(e)
									{
										// When the user presses return, lose focus
										if(e.keyCode == 13)
										{
											$(this).unbind("keyup");
											$(this).blur();
										}
									}
								);
							}
						);

						// Store ths list so it can
						// be added to in later calls
						$.data(this,"textbox",{list:list});

						// Setup the initial list
						$.each(opts.items,function(i)
						{
							addItem($t, list,this);
						});

						// bit ugly, safari renders 
						// too fast and misplaces stuff 
						// everywhere
						var fun = function()
						{
							//arrow.css("display","block");
							$(window).resize();
						};
						window.setTimeout(fun,50);
					}

					// The plugin has already been created on this object
					// must be an external call to modify
					else if(args[0] == "add")
						addItem($.data(this,"textbox").list,args[1]);
				}
			);
		};
	}
)(jQuery);