//去除字串左邊的空白虛格
function ltrim(instr){
return instr.replace(/^[\s]*/gi,"");
}

//去除字串右邊的空白虛格
function rtrim(instr){
return instr.replace(/[\s]*$/gi,"");
}

//去除字串前後的空白虛格
function trim(instr){
instr = ltrim(instr);
instr = rtrim(instr);
return instr;
}

//---------------------------------------------------------------------------------
//檢查必輸入欄位
function check(tObj,tTitle)
{
	if(trim(tObj.value) == '')
	{
		
		if (tTitle == '')
		{
			return tObj.title + '\n';
		}
		else
		{
			return tTitle	+ '\n';
		}
	}
	else
	{
		return "";
	}
}


//---------------------------------------------------------------------------------
//檢查數字
function checkInt(tObj,tCheck,tMin)	//tObj-物件，tCheck-大於小於，tMin-最小值
{
	var tErrmsg	= "";
	if(tMin == "")
	{
		tMin = 0;
	}
	else
	{
		tMin = parseInt(tMin,10);	
	}
	
	
	if(isNaN(tObj.value))
	{
		tErrmsg	= tObj.title + '\n';
	}
	else
	{	
		tObj.value	= parseInt(tObj.value,10);
		
		switch(tCheck)
		{
			case ">": 	if(tObj.value <= tMin)
						{
							tErrmsg = 	tObj.title + '必須>' + tMin + '\n';
						}
						break;
			case ">=":	if(tObj.value < tMin)
						{
							tErrmsg = 	tObj.title + '必須>=' + tMin + '\n';
						}
						break;
			default:	
		}
	}
	return tErrmsg;
}


//---------------------------------------------------------------------------------
//資料處理: ListBox比對資料,回傳SelectedIndex到畫面上顯示
//      傳入: 欄位物件名稱
//          傳出: 對應ListBox的SelectedIndex值
function Fun_ListBox(obj,value)
{
    var i;
    var w_index=0;

    for(i=0; i< obj.length; i++)
        if (value == obj[i].value) {
            w_index = i
            break;}
    return  w_index;
}

function Is_ListBox_Check(obj)
{
    var i;
    var IsSelect=false;

    for(i=0; i< obj.length; i++)
        if (obj[i].checked) {
            IsSelect = true
            break;}
    return  IsSelect;
}


//---------------------------------------------------------------------------------
//檢查email
function validateEmail(email) {
regularExpression = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9])+$/;
	if (regularExpression.test(email)) {
		return true;
	}else{
		return false;
	}
} 
//---------------------------------------------------------------------------------


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function form_submit()
{
	$("#edit_form").submit();
}
function form_reset()
{
	document.edit_form.reset();
}
function login()
{
	var msg=""
	if($("#login_account").val()=="")
	{
		alert_msg("請輸入帳號","login_first");
		return false;
	}
	if($("#login_pwd").val()=="")
	{
		alert_msg("請輸入密碼","login_first");
		return false;
	}
	
	$.getJSON("ajax_member_login.php", {act:"login" , account: $("#login_account").val() , pwd:$("#login_pwd").val()}, 
		function(json){
			if(json.isPass)
			{
				window.location.reload();
			}
			else
			{
				alert_msg(json.msg);
				$("#login_account").focus();
			}
		}
	);
}
function bonus_search()
{
	$.getJSON("ajax_member_login.php", {act:"check_member"}, 
		function(json){
			if(json.isPass)
			{
				location="order.php";
			}
			else
			{
				alert_msg(json.msg,"login_first");
			}
		}
	);	
}
//前台頁面初始化
function page_initial()
{
	$(document).ready(function() {		
		//對話框
		var msg_box='<div id="dialog"  title="系統訊息" style="display:none;"><p><br><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span><span id="dialog_msg" style="float:left;" ></span></p></div>'
		$("body").append(msg_box);	

		//計算左右剩下的值
		var each = Math.floor((document.body.clientWidth-1000)/2);
	
		$("#follow_div").scrollFollow( {
			offset:350
        }).css("left",each+1000);
		
	});		
}

//取代掉原本的alert
function alert_msg(msg,type)
{
	//更新對話框內容
	$("#dialog_msg").html(msg);
	//釋放
	$("#dialog").dialog( 'destroy' );
	//重建
	$("#dialog").dialog( {
		bgiframe: true,
		modal: true,
		width:400,
		buttons: {
			"確認": function() {
				$(this).dialog("close");
				
				if(type=="login_first")
					$("#login_account").focus();
			}
		}
	});	
}

//---------------購物車---------------//
//產品加入購物車
function add_shopping_car(type)
{
	if($("#size").val()!="" )
	{
		$.getJSON("ajax_shopping_cart.php", {act:"add" ,size:$("#size").val() , element:$("#element").val() , type:type}, 
			function(json){
				if(json.isPass==1)
				{
					location="shopping_car.php";					
				}
				else if(json.isPass==2)
				{
					alert_msg("已售完!!");
				}
				else if(json.isPass=="login_first")
				{
					alert_msg("請先登入!!","login_first");
				}
				else if(json.isPass=="not_enough")
				{
					alert_msg("您擁有的紅利點數不足");
				}				
				else
				{
					alert_msg("加入失敗!!");
				}
			}
		);	
	}
}

//改變顯示樣式
function change_style(id,name)
{
	//$.blockUI( {  message: "<h1>加載中...請稍候</h1>" }); 
	$.getJSON("ajax_change_style.php", { id: id }, 
		function(json){	
			//$.unblockUI();
			if(json.isPass)
			{
				
				//抓取所有該商品的樣式圖比對
				$("#gallery a").each(function(){
					//找到，兩兩交換
					if(json.file==$(this).attr("href"))
					{
						$(this).attr("href",$("#product_pic_lightbox").attr("href"));
						$("#product_pic_lightbox").attr("href",json.file).attr("title",name+" [ "+json.style_name+" ]");
						
					}
				});
				//改變產品訊息
				$("#product_pic").attr("src",json.file);
				$("#product_color").html(json.style_name);


				
				if(! json.element )
					$("#product_sn").html("此樣式目前無資料");
				else
					$("#product_sn").html(json.element[0].sn);
				
				//清空選項
				$('#size').find('option').remove().end(); 					
				if(! json.element)
				{
					$("#add_txt").html("此樣式目前無資料");
					$("#add_txt2").html("此樣式目前無資料");
					$("#add_txt3").html("此樣式目前無資料");
				}
				else
				{
					if(json.element[0].element>0)
					{
						$("#add_txt").html("加入購物車");
						$("#add_txt2").html("全紅利兌換");
						$("#add_txt3").html("紅利配現金");
					}
					else
					{
						$("#add_txt").html("該尺寸已售完");
						$("#add_txt2").html("該尺寸已售完");	
						$("#add_txt3").html("該尺寸已售完");	
					}
					//改變尺寸選單
					for(i=0;i<json.element.length;i++)	
						$('#size').append('<option value='+json.element[i].id+ ',' + json.element[i].sn + ',' + json.element[i].element +'>'+json.element[i].name+'</option>'); 						
				}
				

			}
			else
			{
				$("#add_txt").html("AJAX錯誤!!!");
			}
		}
	);	
}

//改變產品編號
function change_sn()
{
	var select_val 	= $('select#size option:selected').val();
	var sn		   	= select_val.split(",")[1];
	var element		= select_val.split(",")[2];
	$("#product_sn").html(sn);
	
	if(element>0)
	{
		$("#add_txt").html("加入購物車");
		$("#add_txt2").html("全紅利兌換");
		$("#add_txt3").html("紅利配現金");
	}
	else
	{
		$("#add_txt").html("該尺寸已售完");
		$("#add_txt2").html("該尺寸已售完");	
		$("#add_txt3").html("該尺寸已售完");		
	}
	
}
//結帳檢查
function payment()
{
	if($("input[name=edit_pick]:checked").val()==1)
	{
		if($("#edit_county").val()=="")
		{
			alert("請選擇縣市。");	
			return;
		}
		if($("#edit_area").val()=="")
		{
			alert("請選擇鄉鎮市。");	
			return;
		}		
		if($("#edit_address").val()=="")
		{
			alert("請選擇送貨地址。");	
			return;		
		}		
	}
	else
	{
		if($("#edit_other_county").val()=="")
		{
			alert("請選擇送貨城市。");	
			return;
		}
		if($("#edit_other_address").val()=="")
		{
			alert("請選擇送貨地址。");	
			return;		
		}
	}

	$.getJSON("ajax_shopping_cart.php", {act:"pay",edit_other_county:$("#edit_other_county").val(),edit_other_area:$("#edit_other_area").val(),edit_zipcode:$("#edit_zipcode").val(),edit_area:$("#edit_area").val(),edit_county:$("#edit_county").val(),edit_address:$("#edit_address").val()}, 
		function(json){
			
			if(json.isPass==1)
			{
				location="shopping_car2.php";
			}
			else if(json.isPass=="login_first")
			{
				alert_msg("請先登入","login_first");
			}
			else if(json.isPass=="no_item")
			{
				alert_msg("你的購物車沒有東西喔，請選擇要購買的產品");
			}
			else if(json.isPass=="not_enough")
			{
				alert_msg(json.msg);
			}
		}
	);	
}
//改變購買數量
function change_element(id,type)
{
	var qty=$("#qty_"+type+"_"+id).val();
	$.getJSON("ajax_shopping_cart.php", {act:"change_element" ,element:qty , id:id ,type:type}, 
		function(json){
			if(json.isPass==1)
			{
//				$("#qty_"+type+"_"+id).attr("value",json.element);
//				$("#sub_total_"+type+"_"+id).html(json.sub_total);
//				$("#sub_bonus_"+type+"_"+id).html(json.sub_bonus);
//				$("#product_total").html(json.total);
//				$("#bonus").html(json.bonus);
//				$("#member_bonus").html(json.member_bonus);
//				$("#freight").html(json.freight);
//				$("#total").html(parseInt(json.total)+parseInt(json.freight));
				window.location.reload();
				
			}
			else if(json.isPass=="not_enough")
			{
				$("#qty_"+type+"_"+id).attr("value",json.element);
				alert_msg("您擁有的紅利點數不足");	
			}
			else
			{
				alert_msg("數量應為正整數。請確認");
			}
		}
	);	
}
//刪除產品
function del_product(id,type)
{


	//更新對話框內容
	$("#dialog_msg").html("確定要刪除此筆資料?");
	//釋放
	$("#dialog").dialog( 'destroy' );
	//重建
	$("#dialog").dialog( {
		bgiframe: true,
		modal: true,
		width:400,
		buttons: {
			"確認": function() {

				$.getJSON("ajax_shopping_cart.php", {act:"del" , id:id , type:type}, 
					function(json){
						if(json.isPass)
						{
//							$("#tr_"+type+"_"+id).remove();
//							$("#product_total").html(json.total);
//							$("#freight").html(json.freight);
//							$("#total").html(parseInt(json.total)+parseInt(json.freight));
//							$("#bonus").html(json.bonus);
//							$("#member_bonus").html(json.member_bonus);
							window.location.reload();
						}
						else
						{
							alert_msg("刪除失敗。請確認");
						}
					}
				);
				
				$(this).dialog("close");				
			},
			"取消": function() {
				$(this).dialog("close");				
			}			
		}
	});	
}
//---------------購物車結束---------------//

//--------------兌換 js------------------//
function coupon_send()
{
	if($("#coupon_sn").val()=="")
		$("#coupon_error").html("*請輸入序號");
	else 
	{
		$.getJSON("ajax_shopping_cart.php", {act:"check_coupon",id:$("#coupon_sn").val() }, 
			function(json){
				if(json.isPass=="1")
				{
					$("#coupon_error").html(json.msg).delay(1000);
					location="shopping_car.php";
				}
				else
				{
					$("#coupon_error").html(json.msg);
				}
			}
		);		
	}
}
function cancel_coupon()
{
	if(confirm("確定要取消兌換?"))
	{
		$.getJSON("ajax_shopping_cart.php", {act:"cancel_coupon"}, 
			function(json){
				if(json.isPass=="1")
				{
					window.location.reload();
				}
			}
		);			
	}
}
