﻿/* ---------------------------- */
/* XMLHTTPRequest Enable */
/* ---------------------------- */
function createObject() 
{
	var request_type;
	var browser = navigator.appName;
	if (browser == "Microsoft Internet Explorer")
	{
		request_type = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		request_type = new XMLHttpRequest();
	}
	return request_type;
}

var http = createObject();
/* -------------------------- */
/* LOGIN */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */
var nocache = 0;
var nowpage = 1;
var pageno  = 1;
var pagetype = 1;
var typeid = 1;
var subtypeid = 1;
var nav = "";

function goPrePage(subtype){
	if(nowpage>1){
		nowpage--;
		gopage(nowpage,subtype);
	}
}

function goNextPage(total,subtype){
	if(nowpage<total){
		nowpage++;
		gopage(nowpage,subtype);
	}
}

function gopage(no,subtype){
//	alert(no);
	nowpage=no;
	subtypeid=subtype;
	http.open('get', 'getdata.php?pageno='+no+'&subtype='+subtype);
	http.onreadystatechange = loginReply;
	http.send(null);
}

function setup_new(pid,type){
	http.open('get', 'update_newpro.php?pId='+pid+'&type='+type);
	http.onreadystatechange = loginReply2;
	http.send(null);
}

function pro_save(pid,ptype){
	var message = (ptype==0)?"確定新增嗎？":"確定修改嗎？";
	var name  = encodeURI(eval("document.getElementById('pro_name_"+pid+"')").value);
	var desc1 = encodeURI(eval("document.getElementById('pro_desc_"+pid+"')").value);
	var price = encodeURI(eval("document.getElementById('pro_price_"+pid+"')").value);
	var stock = encodeURI(eval("document.getElementById('pro_stock_"+pid+"')").value);
	var pic   = encodeURI(eval("document.getElementById('pro_pic_"+pid+"')").value);
	var picb  = encodeURI(eval("document.getElementById('pro_picb_"+pid+"')").value);
	var desc2 = encodeURI(eval("document.getElementById('pro_describe_"+pid+"')").value);
	var type    = encodeURI(eval("document.getElementById('sel_type_"+pid+"')").value);
	var typesub = encodeURI(eval("document.getElementById('sel_subtype_"+pid+"')").value);
	pagetype  = ptype;
	pageno = (ptype==2)?document.getElementById('pageno').value:1;
	if(eval("document.getElementById('forSale_"+pid+"')").checked){
		var forSale = 1;
	}else{
		var forSale = 0;
	}
	if(name==''){
		alert('請輸入產品名稱！');
		return false;
	}
	if(price==''){
		price = 0;
	}
	if(stock==''){
		alert('請輸入產品存量！');
		return false;
	}
	if(type==0){
		alert('請選擇商品分類！');
		return false;
	}
	if(typesub==0){
		alert('請選擇商品子分類！');
		return false;
	}
	if(confirm(message)){
		http.open('get', 'product_save.php?pid='+pid+'&name='+name+'&desc1='+desc1+'&price='+price+'&stock='+stock+'&forSale='+forSale+'&pic='+pic+'&picb='+picb+'&type='+ptype+'&desc2='+desc2+'&typesub='+typesub);
		http.onreadystatechange = loginReply2;
		http.send(null);
	}
}

function changeOrderStatus(oid){
	var hname = eval("document.getElementById('handle_"+oid+"')").value;
	if(hname==""){
		alert('您還沒輸入處理人員帳號喔！');
		return false;
	}else{
		http.open('get', 'chgOrderStatus.php?oid='+oid+'&handler='+hname);
		http.onreadystatechange = loginReply3;
		http.send(null);
	}
}

function checkAccount(){
	var oAct = encodeURI(document.getElementById('oAct').value);
	var oPwd = encodeURI(document.getElementById('oPwd').value);
	var oPid = encodeURI(document.getElementById('pid').value);
	if(oAct==''){
		alert('請輸入帳號！');
		return false;
	}
	if(oPwd==''){
		alert('請輸入密碼！');
		return false;
	}
	http.open('get', 'chkAccount.php?oAct='+oAct+'&oPwd='+oPwd+'&oPid='+oPid);
	http.onreadystatechange = loginReply4;
	http.send(null);
}

function saveType(type){
	var tid = encodeURI(document.getElementById('tid').value);
	typeid = tid;
	var tsid = encodeURI(document.getElementById('tsid').value);
	var typename = encodeURI(document.getElementById('typename').value);
	var type_show = (type==2)?encodeURI(document.getElementsByName('type_show').value):1;
	if(typename==""){
		alert('您還沒輸入名稱喔！');
		return false;
	}else{
		http.open('get', 'product_type_save.php?type='+type+'&name='+typename+'&tid='+tid+'&tsid='+tsid+'&type_show='+type_show);
		http.onreadystatechange = loginReply5;
		http.send(null);
	}
}

function newType(){
	window.location.href = 'product_type_new.php';	
}

function newSubType(){
	window.location.href = 'product_type_sub_new.php';
}

function editType(id){
	window.location.href = 'product_type_edit.php?tid='+id;	
}

function editSubType(id){
	window.location.href = 'product_type_sub_new.php?tsid='+id;
}

function cancel(){
	window.location.href = 'product_type.php';
}

function loginReply()
{
	if(http.readyState == 4)
	{
		if(http.status==200){
			var response = http.responseText;
			var ary = response.split('$');
			totalpage = ary[1];
			json = "json:" + ary[0];
			json = eval(json);
			for(var i=0;i<9;i++){
				if(i<json.length){
					eval("document.getElementById('block"+i+"1')").style.display = 'block';
					eval("document.getElementById('block"+i+"2')").style.display = 'block';
				}else{
					eval("document.getElementById('block"+i+"1')").style.display = 'none';
					eval("document.getElementById('block"+i+"2')").style.display = 'none';
				}
			}
			for(var i=0,j=json.length;i<j;i++){
				eval("document.getElementById('PRONAME"+i+"')").innerHTML  = json[i].name;
				eval("document.getElementById('PROSTRING"+i+"')").innerHTML = json[i].desc;
				eval("document.getElementById('PROSTOCK"+i+"')").innerHTML = json[i].stock;
				eval("document.getElementById('PROPRICE"+i+"')").innerHTML = formatNumber(json[i].price);
				if(json[i].price==0){
					eval("document.getElementById('ROWPRICE"+i+"')").style.display = "none";
				}else{
					eval("document.getElementById('ROWPRICE"+i+"')").style.display = "inline-block";
				}
				eval("document.getElementById('PROPIC"+i+"')").innerHTML   = "<img src='images/"+json[i].pic+"' width='100' height='100' border='0' onClick='productdeatail("+json[i].Id+")' />";
				btnValue = (json[i].stock>0)?"我要兌換":"兌換完畢";
				btnClick = ((json[i].stock>0) && (json[i].name!=""))?"onClick='buyit("+json[i].Id+")'":"";
				eval("document.getElementById('PROBTN"+i+"')").innerHTML   = "<input id='PROBTN0' type='button' class='style39' value='"+btnValue+"' style='font-size: 8pt' "+btnClick+">";
			}
			//page_nav
			nav = '';
			for(k=1;k<=totalpage;k++){
				nav = nav + '<span onClick="gopage('+k+','+subtypeid+')" style="cursor:pointer;"> '+k+' </span>';  
			}
			nav_start = '<input type="button" value="上一頁" name="B6" style="color: #6699FF; font-size: 8pt; border: 1px solid #0066FF; background-color: #FFFFFF" onClick="goPrePage('+subtypeid+')"><font size="2" face="Tahoma" style="color:#FFFFFF">';
			nav_end   = '</font><input type="button" value="下一頁" name="B5" style="color: #6699FF; font-size: 8pt; border: 1px solid #0066FF; background-color: #FFFFFF" onClick="goNextPage('+totalpage+','+subtypeid+')">';
			document.getElementById("page_nav").innerHTML = (totalpage>0)?nav_start + nav + nav_end:"";
		}
		
	}
}

function loginReply2()
{
	if(http.readyState == 4)
	{
	  if(http.status==200){
		var response = http.responseText;
//		alert(response);
//		exit;
		if(response == 'a')
		{
			alert('新增成功！');
			window.location.href='list_normal.php';
		}
		else if(response == 'b')
		{
			alert('修改成功！');
			if(pagetype==1){
				window.location.href='list_new.php';
			}else if(pagetype==2){
				window.location.href='list_normal.php?pageno='+pageno;
			}else{
				window.location.href='list_normal.php';
			}
		}
		else if(response == 'c')
		{
			alert('刪除成功！');
			window.location.href='list_normal.php';
		}
		else if(response == 'd')
		{
			alert('更新成功！');
			window.location.reload();
		}
		else
		{
			alert('作業失敗！');
			return false;
		}	
	  }
	}
}

function loginReply3()
{
	if(http.readyState == 4)
	{
	  if(http.status==200){
		var response = http.responseText;
//		alert(response);
//		exit;
		if(response == 'x')
		{
			alert('處理失敗！');
			return false;
		}
		else
		{
			alert('處理完成！');
			var handler_name = eval("document.getElementById('handle_"+response+"')").value;
			eval("document.getElementById('tr_"+response+"')").style.backgroundColor = '#CCCCCC';
			eval("document.getElementById('hbtn_"+response+"')").innerHTML  = '已處理';
			eval("document.getElementById('htext_"+response+"')").innerHTML = handler_name;
		}	
	  }
	}
}

function loginReply4()
{
	if(http.readyState == 4)
	{
	  if(http.status==200){
		var response = http.responseText;
		//alert(response);
		//exit;
		if(response == 'x')
		{
			alert('請輸入正確的帳號密碼！');
			return false;
		}
		else if(response == 'z')
		{
			alert('您的發達幣不足！請選擇其他產品！');
			location.href = 'shopping.php';
		}
		else
		{
			confirmBuyit();
		}	
	  }
	}
}

function loginReply5()
{
	if(http.readyState == 4)
	{
	  if(http.status==200){
		var response = http.responseText;
//		alert(response);
//		exit;
		if(response == 'a')
		{
			alert('新增成功！');
			window.location.href='product_type.php?tid='+typeid;
		}
		else if(response == 'b')
		{
			alert('修改成功！');
			window.location.href='product_type.php?tid='+typeid;
		}
		else if(response == 'c')
		{
			alert('刪除成功！');
			window.location.href='product_type.php?tid='+typeid;
		}
		else if(response == 'd')
		{
			alert('更新成功！');
			window.location.href='product_type.php?tid='+typeid;
		}
		else
		{
			alert('作業失敗！');
			return false;
		}	
	  }
	}
}

function formatNumber(str) {   
    if(str.length <= 3){   
        return str;   
    } else {   
        return formatNumber(str.substr(0,str.length-3))+','+str.substr(str.length-3);    
    }   
}   

//檢查身分證
function cert_check(cert) {
   var acc = 0;
   var d0 = cert.charAt(0);
   var d1 = cert.charAt(1);
   var d2 = cert.charAt(2);
   var d3 = cert.charAt(3);
   var d4 = cert.charAt(4);
   var d5 = cert.charAt(5);
   var d6 = cert.charAt(6);
   var d7 = cert.charAt(7);
   var d8 = cert.charAt(8);
   var d9 = cert.charAt(9);
   if ((d0 == 'A') || (d0 == 'a')) { acc = 10; }
   else if ((d0 == 'B') || (d0 == 'b')) { acc = 11; }
   else if ((d0 == 'C') || (d0 == 'c')) { acc = 12; }
   else if ((d0 == 'D') || (d0 == 'd')) { acc = 13; }
   else if ((d0 == 'E') || (d0 == 'e')) { acc = 14; }
   else if ((d0 == 'F') || (d0 == 'f')) { acc = 15; }
   else if ((d0 == 'G') || (d0 == 'g')) { acc = 16; }
   else if ((d0 == 'H') || (d0 == 'h')) { acc = 17; }
   else if ((d0 == 'J') || (d0 == 'j')) { acc = 18; }
   else if ((d0 == 'K') || (d0 == 'k')) { acc = 19; }
   else if ((d0 == 'L') || (d0 == 'l')) { acc = 20; }
   else if ((d0 == 'M') || (d0 == 'm')) { acc = 21; }
   else if ((d0 == 'N') || (d0 == 'n')) { acc = 22; }
   else if ((d0 == 'P') || (d0 == 'p')) { acc = 23; }
   else if ((d0 == 'Q') || (d0 == 'q')) { acc = 24; }
   else if ((d0 == 'R') || (d0 == 'r')) { acc = 25; }
   else if ((d0 == 'S') || (d0 == 's')) { acc = 26; }
   else if ((d0 == 'T') || (d0 == 't')) { acc = 27; }
   else if ((d0 == 'U') || (d0 == 'u')) { acc = 28; }
   else if ((d0 == 'V') || (d0 == 'v')) { acc = 29; }
   else if ((d0 == 'W') || (d0 == 'w')) { acc = 32; }
   else if ((d0 == 'X') || (d0 == 'x')) { acc = 30; }
   else if ((d0 == 'Y') || (d0 == 'y')) { acc = 31; }
   else if ((d0 == 'Z') || (d0 == 'z')) { acc = 33; }
   else if ((d0 == 'I') || (d0 == 'i')) { acc = 34; }
   else if ((d0 == 'O') || (d0 == 'o')) { acc = 35; }
   if (acc == 0)
   { 
           //alert("point1");	
           return false; 
   }
   else 
   	{
           accstr = new String(acc);
           acc_1 = (accstr).charAt(0);
           acc_2 = (accstr).charAt(1);
           certsum = 1*acc_1 + 9*acc_2 + 8*d1 + 7*d2 + 6*d3 + 5*d4 + 4*d5 + 3*d6 + 2*d7 + 1*d8;
           certsum_2 = parseInt(certsum%10);
           certsum_3 = 10 - certsum_2;
           if(certsum_2==0 && d9==0)return true;
           if (d9 != certsum_3) 
           {
                   //alert("point2");
                   return false;
           }
   }
   return true;
}


