function getElPoint(e)
{
     var x = e.offsetLeft;
     var y = e.offsetTop;
     while(e = e.offsetParent)
     {
         x += e.offsetLeft;
         y += e.offsetTop;
      }
      return {"x": x, "y": y};
}

function setCheckBox(ids)
{
	$("input[name*='" + ids + "']").each(function(){
		if(this.checked)
			this.checked = false;
		else
			this.checked = true;	
	});
}

function setCheckBoxNull(ids)
{
	$("input[name*='" + ids + "']").each(function(){
		this.checked = false;
	});
}

function picSize(ImgD,iwidth,iheight) {
     var image=new Image();
     image.src=ImgD.src;
     var image=new Image();
     image.src=ImgD.src;
	 ImgD.width = iwidth;
	 ImgD.height = iheight;
	 //return;	 
     if(image.width>0 && image.height>0){
        if(image.width/image.height>= iwidth/iheight){
           if(image.width>iwidth){
               ImgD.width=iwidth;
               ImgD.height=(image.height*iwidth)/image.width;
           }else{
                  ImgD.width=image.width;
                  ImgD.height=image.height;
                }
               //ImgD.alt=image.width+"?+image.height;
        }
        else{
                if(image.height>iheight){
                       ImgD.height=iheight;
                       ImgD.width=(image.width*iheight)/image.height;
                }else{
                        ImgD.width=image.width;
                        ImgD.height=image.height;
                     }
                //ImgD.alt=image.width+"?+image.height;
            }
    }
}	

//city list function
var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
function listReady()
{
	/*for(i = 0; i < str.length; i++)
	{
		$("#choice").append("<div class='sub' id='sub" + i + "'>" + str.substr(i, 1) + "</div>");
	}
	
	for(i = 0; i < str.length; i++)
	{
		$("#sub" + i).click(function(){
			$("#search").val($("#search").val() + this.innerHTML);
			changeData();
		});
	}
	
	$("#cls").click(function(){
		var val = $("#search").val();
		$("#search").val(val.substr(0, val.length-1));
		if(val.length >= 1)
		{
			changeData();
		}
	});
	*/
	
	
	$("#content").ajaxStart(function(){
		$(this).html("<font color='red'>Loading...</font>");
	});
	$("#content").ajaxError(function(){
		$(this).html("<font color='red'>Load error...</font>");
	});	
	
	$("#closeDiv").click(function(){
		$("#list").slideUp("slow");
	});	
	$("#list").blur(function(){
		$("#list").slideUp("slow");
	});	
}

function changeData(val)
{
	//alert($("#search").val());
	//var val = $("#search").val();
	$.post("http://localhost:90/u2unew/index.php/airport/search", {search:val}, callback);
	//$.post("http://localhost:90/test/list.php", {search:val}, callback);
}

function callback(data)
{
	//alert(data);
	//return;
	var obj = eval(data);
	$("#content").html("");
	if(obj.length < 1 || obj == "" || obj == null)
	{
		$("#content").html("<font color='red'>No Data</font>");
	}	
	for(i = 0; i < obj.length; i++)
	{
		var str = "<div id='content" + i + "' class='listcontent'>" + obj[i].code + "----(" +obj[i].enName + " | " +obj[i].cnName + ")" +"</div>";
		$("#content").append(str);
		$("#content" + i).mouseover(function(){$(this).addClass("mouseon");});
		$("#content" + i).mouseout(function(){$(this).removeClass("mouseon");});
		$("#content" + i).click(function(){
			$(this).removeClass("mouseon");$("#list").slideUp("slow");
			var value = $(this).text();
			$(name).val(value.substr(0, value.indexOf("-")));
		});
	}
}
	
/*    function getElPoint(e)
    {
    	if(e == "undefined" || e == "")
    		return null;
        var x = e.offsetLeft;
        var y = e.offsetTop;
        while(e = e.offsetParent)
        {
            x += e.offsetLeft;
            y += e.offsetTop;
        }
        return {"x": x, "y": y};
    }
*/	

function addlist(id)
{
	$("#"+id).click(function(){
		var val = $("#list");
		val.css("position", "absolute");     //absoult
		var xy = getElPoint(this);
		val.css("left", xy.x + "px");
		val.css("top", xy.y+20 + "px");
		val.slideDown("slow");
		name = "#" + this.id;	
		changeData($(this).val());
	});	
	
	$("#"+id).keyup(function(){
		changeData($(this).val());
	});
}	


function show_div(el, id, detail, title)
{
	var xy = getElPoint(el);			
	var obj = $("#big_product_id");
	obj.css("position", "absolute");
	obj.css("left", (xy.x - 100) + "px");
	obj.css("top", (xy.y - 150) + "px");
	$("#big_title").text($("#title_" + id).text());
	$("#big_info").text($("#info_" + id).text());
	$("#big_detail").attr("href", detail);
	$("#title_id").val(title);
	$("#big_pic").attr("src", el.src);
	obj.show();
	//obj.mouseout(function(){
		//$(this).hide();		
	//});
}

function remark(title)
{
	if(title.length  > 0 )
		$("#title_id").val(title);
	$("#remark_form").submit();
}

function set_province_city(now_province, now_city)
{
	var province = document.getElementById("province");
	var province_count = province.options.length;
	for(var i = 0; i < province_count; i++)
	{
		if(province.options[i].value == now_province)
		{
			province.options[i].selected = "selected";
			break;	
		}
	}
	
	/*$("#city").trigger("change");
	var city = document.getElementById("city");	
	var city_count = city.options.length;
	for(var j = 0; j < city_count; j++)
	{
		if(city.options[j].value == now_city)
		{
			city.options[j].selected = "selected";
			break;	
		}
	}*/
}

function change_address(el)
{
	var id= el.options[el.selectedIndex].value;
	if(id == "")
	{
		$("#user_name").val("");
		$("#post_num").val("");
		$("#tel").val("");
		$("#mobile").val("");
		$("#address").val("");
		return;
	}
	var url = url_encode({"action":"util", "do":"address", "id":id});
	var url2 = url_encode({"action":"util", "do":"address_content", "id":id});
	$.getJSON(url, function(data){
		if(data)
		{
			$("#user_name").val(data.user_name);
			$("#post_num").val(data.post_num);
			$("#tel").val(data.tel);
			$("#mobile").val(data.mobile);
			set_province_city(data.province, data.city);
		}
	});	
	$.get(url2, function(data){
		$("#address").val(data);
	});
}

//load post money
function load_post_money(el)
{
	var url = el.options[el.selectedIndex].value;
	if(url.length < 1)
		return;
	$.getJSON(url, function get_val(data){
		if(data)
		{
			$("#post_money").text(data.money);
			$("#post_id").val(data.id);
		}
	});	
}


//load payaway
function load_pay_way(el)
{
	var url = el.options[el.selectedIndex].value;
	if(url.length < 1)
		return;
	$.getJSON(url, function get_val(data){
		if(data)
		{
			$("#pay_title").text(data.title);
			$("#pay_id").val(data.id);
		}
	});		
}

/**
 * 添加到用戶收藏夾
 */
function add_mem_keep(id)
{
	if(user_flag != true)
	{
		alert("請先登錄后再操作");
		return;
	}
	if(id == "" || id <= 0)
	{
		alert("添加時出錯");
		return;
	}
	var url = url_encode({"action":"mem_keep", "do":"add", "id":id});
	$.get(url, function(data){
		if(data > 0)
			alert("已成功添加到您的收藏夾");
		else
			alert("添加失敗,請稍候再試");
	});
}



