
var baseUrl = '';

$(function()
{
	
	var myloc = window.location.href;
	var locarray = myloc.split("/");
	delete locarray[(locarray.length-1)];
	baseUrl = locarray.join("/");	
	
	$('.all_curve').corner('5px');
	$('ul.ul_li_curve li.select').corner('top 5px');
	//$('input[type="button"],input[type="submit"]').corner('5px');
	
	$(document).ajaxStart(function(){
		$('body').prepend('<div id="loading"><img src="application/statics/images/ajax-loader.gif"/></div>')
	});
	$(document).ajaxComplete(function(event, xhr, settings) {
		$('body').find('#loading').remove();
	});
	
	$("input[id*=date]").datepicker({
		showOn: 'button',
		buttonImage: 'application/statics/images/calendar.png',
		buttonImageOnly: true,
		dateFormat:'yy/mm/dd'
	}); 
	$("input[id*=date]").attr('autocomplete','off')
	
	var c1 = $('.in_last div:first').height();
	var c2 = $('.in_last div:last').height();
	
	if(parseInt($('.in_last div:first').height()) > parseInt($('.in_last div:last').height()))
	{
		$('.in_last div:last').height($('.in_last div:first').height()+'px');
	}
	else
	{
		$('.in_last div:first').height($('.in_last div:last').height()+'px');
	}

});

// async ajax class
function c_aajax(pPa,pPd)
{

	this.pa = pPa;
	this.pd = pPd;

	this.c_aajax_f = function()
	{

		var rv = false;
		var data = $.ajax({
			type : "POST",
			dataType: "json",
			url : baseUrl + this.pa,
			async:true,
			data : this.pd,
			cache :false,
			success : function(data)
			{
			},
			error : function(XMLHttpRequest, textStatus, errorThrown)
			{
				alert(errorThrown);
				//switch(data.s)
				//{
				//case 'e':
				//message('alert',data.m) ;
				//break;
				//default:
				//message('alert',data.m + "XMLHttpRequest:
				// "+XMLHttpRequest+"\n"+"textStatus:"+textStatus+"\n"+"errorThrown:
				// "+errorThrown);
				//break;
				//}
			}
		});
		return data;
	};
}
// end async ajax class

function send_request(page,data)
{
	var ajx_obj = new c_aajax(page,data);
	return ajx_obj.c_aajax_f();	
}

function prepare_search()
{
	var data = '{';
	$('div.search_2col:visible').find('input[type!="submit"][type!="button"],select').each(function()
	{
		data += '"'+$(this).attr('id')+'":"'+$(this).val()+'",';
	});
	data = data.substring(0,data.length-1);
	data += '}';
	return data;
}
var timer = null;
function auto_complete(pThis,pAction)
{
	if($(pThis).val().length > 1)
	{
		$(pThis).dblclick( function()
		{
			$(this).trigger('onkeyup')
		});
		var txt = $(pThis).val();
		if(timer)
		{
			clearTimeout(timer);
		}
		timer = setTimeout
		( function()
		{
			var rv = false;
			var data = {keyword:txt} 
			$.when(send_request(pAction,data)).then( function(response)
			{
				if(response !== false && response != '')
				{
					$(pThis).next().remove()
					$(pThis).after('<div class="autocomplete" style="min-width:'+$(pThis).innerWidth()+'px;right:'+$(pThis).prev().width()+'px"><ul></ul></div>');
					$.each(response['rows'], function(index,value)
					{
						$(pThis).next().find('ul').append('<li>'+value[0]+'</li>').find('li:last').click( function()
						{
							$(this).parent().parent().hide();
							$(pThis).val($(this).text());
						});
					});
					$("body").click( function(e)
					{
						if(e.target.className !== "autocomplete")
						{
							$("div.autocomplete").hide();
						}
					});
				}
			});
		}
		,1000);
	}
}

