﻿function packageselectobj(name){
	//alert(name);
	var obj	= document.getElementById(name);
	if(obj.options.length==0)return "";
	var i=0;
	var str="";
	while(i<obj.options.length){
		if(obj.options[i].selected)str	= str+obj.options[i].value+",";
		i++;
	}
	return str;
}
function replaceImage(type,pid){
	if(type == 0){
		$(".cartag").removeClass("tobuycar");
		$(".cartag").addClass("okbuycar");
	}else if(type == 1){
		$("#cartag"+pid).attr("src","/public/images/is_okbuycar.gif");
	}else if(type == 2){
		$(".cartag").removeClass("tobuycar");
	  $(".cartag").addClass("okbuycar");
	}
	
}

function addcar(pid){
	//window.location.href	= "/index/ajax/q_buy_pid_"+pid+"_r_"+Math.random();
	$.get("/index/ajax/q_buy_pid_"+pid+"_r_"+Math.random(),function(data){
		replaceImage(0,pid);
		if(data=='1'){
			//alert("白痴成功加入購物車,請您繼續購物");
			getcarinfo('car');
			return true;
		}else{
			if(data=='2'){
				//alert("物品已經在購物車中,請您繼續購物");
					
				return true;
			}
			alert("未知錯誤,請確定您的瀏覽器支持COOKIE.");
			return false;
		}
	});



	return true;
}
//#0:nothing
//#1:replace pic
//#2:replace css 
function addcar2(pid,type){
	//window.location.href	= "/index/ajax/q_buy_pid_"+pid+"_r_"+Math.random();
	$.get("/index/ajax/q_buy_pid_"+pid+"_r_"+Math.random(),function(data){
		replaceImage(type,pid);
		if(data=='1'){
			//alert("白痴成功加入購物車,請您繼續購物");
			getcarinfo('car');
			return true;
		}else{
			if(data=='2'){
				//alert("物品已經在購物車中,請您繼續購物");
					
				return true;
			}
			alert("未知錯誤,請確定您的瀏覽器支持COOKIE..");
			return false;
		}
	});



	return true;
}

function getcarinfo(id){
	$.get("/index/ajax/q_carlist_r_"+Math.random(),function(data){
		document.getElementById(id).innerHTML	= data;
		return true;
	});
	return true;
}



function delAllcar(){

	$.get("/index/ajax/q_delAll_r_"+Math.random(),function(data){
		$(".cars").each(function(){
			$(this).remove();
  	});
		countMoney();
		return true;
	});
	return true;

}

function countMoney(){
	var total = 0;
	$(".cars").each(function(){
			var id = $(this).attr("id").substr(3);
			var money = $("#money_"+id).text();
			var num = $("#num"+id).val();
			var count = money*num;
			total += count;
  	});
  	$("#total").text(total);
  	$("#all").text(total+150);
}

function delcar(id,pid,cid){
	

	$.get("/index/ajax/q_del_pid_"+id+"_r_"+Math.random(),function(data){
		document.getElementById(pid).removeChild(document.getElementById(cid));
		countMoney();
		return true;
	});

	return true;
}
function sendorder(){
	var orderstr	= document.getElementById("orderstr").value;
	if(orderstr==''){
		alert('錯誤');
		return 0;
	}
	var rname	= document.getElementById("rname").value;
	var email	= document.getElementById("email").value;
	var phone	= document.getElementById("phone").value;
	var qq	= document.getElementById("qq").value;
	var address	= document.getElementById("address").value;
	var quhao	= document.getElementById("quhao").value;//區號
	var remark	= document.getElementById("remark").value;
	//var gift	= packageselectobj("gift");
	if(rname==""){
		alert('請填寫姓名');
		return 0;
	}
	if(email==""){
		alert('請填寫EMAIL');
		return 0;
	}
	if(phone==""){
		alert('請填寫電話');
		return 0;
	}
	if(quhao==""){
		alert('請填寫區號');
		return 0;
	}
	if(address==""){
		alert('請填寫地址');
		return 0;
	}

	var code	= document.getElementById("code").value;
	if(code==""){
		alert('請填寫驗證碼');
		return 0;
	}
	$.get("/index/checkCode/ucode_"+code+"_r_"+Math.random(),function(data){

		if (data == "ok"){
			$.post("/index/ajax",{order:'1',orderstr:orderstr,rname:rname,email:email,phone:phone,address:address,qq:qq,quhao:quhao,remark:remark},function(data){
				document.getElementById("mycar").innerHTML	= data;
				return true;		
			});
			reorder();
			document.getElementById("mycar").innerHTML	= "<table width=500px align=center><tr><td width=100% height=300px align=center><img src='/public/images/load.gif' width=300 border=0><br>请稍等，正在提交定单...</td></tr></table>";
			return true;
		
		}else{
			alert('驗證碼錯誤');
			return 0;
		}

	});

	
}


function checkorder(){
	$("#mark").show();
	$("#check_div").show();
	
	var rname	= document.getElementById("rname").value;
	var email	= document.getElementById("email").value;
	var phone	= document.getElementById("phone").value;
	var qq	= document.getElementById("qq").value;
	var address	= document.getElementById("address").value;
	var quhao	= document.getElementById("quhao").value;//區號
	var remark	= document.getElementById("remark").value;
	$("#check_user").html("<ul><li>姓名:"+rname+"</li><li>電話:"+phone+"</li><li>EMAIL:"+email+"</li><li>MSN:"+qq+"</li><li>地址:("+quhao+")"+address+"</li><li>備註:"+remark+"</li></ul>");
}

function reorder(){
	$("#mark").hide();
	$("#check_div").hide();
}

