// JavaScript Document
var f_h,t_h,area_height,area_name,div_running,opened_area='';
//===========================================================================

function login_bar(loging){
	if(loging){
		document.getElementById("attack_bt_left").innerHTML = "<a id=logout onclick=logout(); ><img src=/img/logout.gif alt=登出 width=41 height=14 /></a> <a id=sign_in_area_bt class=i_bt  onclick=poen_xx_area('sign_in_area',65); ><img src=/img/uraccount.gif  width=72 height=14 /></a>";
	}else{
		document.getElementById("attack_bt_left").innerHTML = "<a id=sign_in_area_bt class=i_bt  onclick=poen_xx_area('sign_in_area',65); ><img src=/img/login.gif alt=登入 width=30 height=14 /></a>";
	}
}
//===========================================================================

function check_h_buy_area() {
  var url = "00_cart_h.php";
  request2.open("POST", url, true);
  request2.onreadystatechange = re_check_h_buy_area;
  request2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
  request2.send(null);
}
//-----------------------------------------------
function re_check_h_buy_area(){
  if (request2.readyState == 4) {
    if (request2.status == 200) {
		
      var xmlDoc = request2.responseXML;
      var store_txt_0 = xmlDoc.getElementsByTagName("store")[0];
      var h = store_txt_0.attributes.getNamedItem ("h").nodeValue;

	  h=Math.round(h);
      parent.document.getElementById("buy_area").innerHTML = store_txt_0.firstChild.nodeValue;


	  poen_xx_area('buy_area',h);

    } else
      alert("Error! Request status is " + request2.status);
  }
}


//===========================================================================

function check_login_area() {	 

if(opened_area!='sign_in_area'){
  document.getElementById("sign_in_area").innerHTML ='<table style="margin-top:10px" width="315" height="135" border="0" align="center" cellpadding="0" cellspacing="0" ><tr><td width="182" valign="top" style="border-right:1px solid #D1D3D4;"><span id="error_msg_1" style="color:#BB0000; margin:5px 0px 0px 10px;"></span><table width="152" border="0" style="margin:5px 0px 10px 10px;"><tr><td style="color:#000000;font-size: 11px;">用戶帳號 <strong> email</strong></td></tr><tr><td><input type="text" name="email" style="width:152px; color:#000000; font-size: 11px;" /></td></tr><tr><td style="color:#000000;font-size: 11px;">密　　碼 <strong> Password</strong></td></tr><tr><td><input name="pwx" type="password"  style="width:152px; color:#000000; font-size: 11px;" /></td></tr><tr><td align="right"><img src="../img/submit.gif" width="52" height="15" style="cursor:pointer;" onclick="login();" /></td></tr></table> </td><td width="133" valign="top" ><div style=" margin:15px 0px 10px 20px; width:80px; font-size: 11px; line-height:16px; text-decoration:underline; color:#000000; cursor:pointer;" onclick="pwhelp();">忘記密碼？<br />Password Help?</div><div style=" margin:22px 0px 10px 20px; width:80px;  font-size: 11px; line-height: 16px;"><a href="index.php?member=1" style="color:#000;">註冊新帳號<br />Registration</a></div></td></tr></table>';
  
}
  poen_xx_area('sign_in_area',155);
}
//------------------------------------------

function pwhelp() {	
  document.getElementById("sign_in_area").innerHTML ='<table width="273" border="0" style="margin:25px"><tr><td colspan="2" style="color:#000000;font-size: 11px;"><p>請輸入您的帳號 email，我們會將密碼寄到您的信箱</p><p>&nbsp;</p></td></tr><tr><td><span style="color:#000000;font-size: 11px;"> <strong> email</strong></span> <input type="text" name="email" style="width:152px; color:#000000; font-size: 11px;" /><input type="text" name="xxx" style="width:1px; color:#FFFFFF;  border: 0px solid #FFFFFF; font-size: 1px;" /></td><td><img src="../img/submit.gif" width="52" height="15" style="cursor:pointer;" onclick="mailpw();" /></td></tr><tr><td colspan="2" align="center"><span id="error_msg_1" style="color:#BB0000; margin:5px 0px 0px 10px;"></span></td></tr></table>';
}
//===========================================================================

function poen_xx_area(area,h,x) {
if(div_running!=1){		
  
  area_height=h;
  area_name=area;
	if((document.getElementById(area).style.display != "block")|| x=='add' ){
	    if(opened_area!=''){
	     document.getElementById(opened_area).style.display = "none";
  	     document.getElementById(opened_area).style.height = "0px";
	     document.getElementById(opened_area+"_bt").style.background = "url(/img/i2_r.gif) no-repeat 3px 2px"; 
         }

	  div_running=1;
      f_h=0;
      t_h=h;
      run_area = setInterval('run_xx_area()',10);
      
      document.getElementById(area+"_bt").style.background = "url(/img/i2_d.gif) no-repeat 0px 5px"; 

	}else{
      div_running=1;
      f_h=h;
      t_h=0;
      run_area = setInterval('run_xx_area()',10);
	}
}
}

//------------------------------------------
//------------------------------------------
function run_xx_area() {
 f_h = f_h + (t_h-f_h)/8;
 yL = Math.round(f_h);	
 document.getElementById(area_name).style.height = yL+"px";	
 document.getElementById(area_name).style.display = "block";
  if(yL>(area_height-1)){
	clearTimeout(run_area);
	div_running=0;
	opened_area=area_name;
  }
  if(yL<1){
	clearTimeout(run_area);
	document.getElementById(area_name).style.display = "none";
	document.getElementById(area_name+"_bt").style.background = "url(/img/i2_r.gif) no-repeat 3px 2px"; 
	div_running=0;
	opened_area='';
  }	
}



function close_tool(){
	
	if(opened_area){
	poen_xx_area(opened_area,0,0);
	
	}
}






//===========================================================================
function remove(un) {
  var url = "00_cart_unset.php";
  request2.open("POST", url, true);
  request2.onreadystatechange = re_remove;
  request2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
  post_content="un="+un;
  request2.send(post_content);
}
//-----------------------------------------------
function re_remove(){
  if (request2.readyState == 4) {
    if (request2.status == 200) {
		
      var xmlDoc = request2.responseXML;
      var store_txt_0 = xmlDoc.getElementsByTagName("store")[0];
      var h = store_txt_0.attributes.getNamedItem ("h").nodeValue;

	  h=Math.round(h);
      parent.document.getElementById("buy_area").innerHTML = store_txt_0.firstChild.nodeValue;

	  poen_xx_area('buy_area',h,'add');

    } else
      alert("Error! Request status is " + request2.status);
  }
}


//===========================================================================
function goto_url(url) {
location.href=url;
}

//===========================================================================

function login() {
  var url = "00_login.php";
  request1.open("POST", url, true);
  request1.onreadystatechange = re_login;
  request1.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
  
var formname = eval('document.form1');
var email = formname.email.value ;
var pw = formname.pwx.value ;
var checkout = formname.checkout.value ;

  post_content="email="+email+"&pw="+pw+"&checkout="+checkout;
  request1.send(post_content);

}
//-----------------------------------------------
function re_login(){
  if (request1.readyState == 4) {
    if (request1.status == 200) {
		
      var xmlDoc = request1.responseXML;
      var store_txt_0 = xmlDoc.getElementsByTagName("store")[0];
      var name = store_txt_0.attributes.getNamedItem ("name").nodeValue;
	  var login = store_txt_0.attributes.getNamedItem ("login").nodeValue;
	  var checkout = store_txt_0.attributes.getNamedItem ("checkout").nodeValue;

		  
	  if(login==1){
		  if(checkout==2){
			  location.href="index.php?checkout=2";
		  }else{
		document.getElementById("error_msg_1").innerHTML = "";
		var formname = eval('document.form1');
        formname.pwx.value='' ;
	  poen_xx_area('sign_in_area',155);
      document.getElementById("user_name").innerHTML = "Hello, "+name;
	  document.getElementById("sign_in_area_bt").style.display = "none";
	  document.getElementById("logout_bt").style.display = "block";
	  document.getElementById("account_area_bt").style.visibility = "visible";
		  }
	  }else{
		document.getElementById("error_msg_1").innerHTML = "帳號或密碼錯誤　 ";
		var formname = eval('document.form1');
        formname.pwx.value='' ;
	  }

    } else
      alert("Error! Request status is " + request2.status);
  }
}

//===========================================================================
//===========================================================================

function mailpw() {
  var url = "00_mailpw.php";
  request1.open("POST", url, true);
  request1.onreadystatechange = re_mailpw;
  request1.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
  
var formname = eval('document.form1');
var email = formname.email.value ;

  post_content="email="+email;
  request1.send(post_content);
  document.getElementById("sign_in_area").innerHTML = '<div align="center" style="color:#BB0000; margin:30px 0px 0px 10px;" >密碼查詢... 寄送中...</div>';
}
//-----------------------------------------------
function re_mailpw(){
  if (request1.readyState == 4) {
    if (request1.status == 200) {
	
      var xmlDoc = request1.responseXML;
      var store_txt_0 = xmlDoc.getElementsByTagName("store")[0];
      var email = store_txt_0.attributes.getNamedItem ("email").nodeValue;
	  var login = store_txt_0.attributes.getNamedItem ("login").nodeValue;

	  if(login==1){
      document.getElementById("sign_in_area").innerHTML = '<div align="center" style="color:#BB0000; margin:30px 0px 0px 10px;" >密碼已寄出</div>';
	  poen_xx_area('sign_in_area',155);
	  }else{
	  pwhelp();
      document.form1.email.value=email ;
	  document.getElementById("error_msg_1").innerHTML = "此 email 尚未註冊";
	  }

    } else
      alert("Error! Request status is " + request2.status);
  }
}

//===========================================================================
// 登出
function logout(checkout) {
  var url = "00_logout.php";
  request1.open("POST", url, true);
  request1.onreadystatechange = re_logout;
  request1.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
  
  post_content="checkout="+checkout;
  request1.send(post_content);

}
//-----------------------------------------------
// 回登出
function re_logout(){
  if (request1.readyState == 4) {
    if (request1.status == 200) {
		
      var xmlDoc = request1.responseXML;
      var store_txt_0 = xmlDoc.getElementsByTagName("store")[0];
	  var checkout = store_txt_0.attributes.getNamedItem ("checkout").nodeValue;
		
		  if(checkout>1){
			  location.href="index.php?checkout=1";
		  }else{
			  location.href="index.php";
      //document.getElementById("user_name").innerHTML = "&nbsp;";
	  //document.getElementById("sign_in_area_bt").style.display = "block";
	  //document.getElementById("logout_bt").style.display = "none";
	  //document.getElementById("account_area_bt").style.visibility = "hidden";
		  }
    } else
      alert("Error! Request status is " + request2.status);
  }
}

//=================================================================================
//新增會員資料
function check_add_mem(){
sFlag = true ;
emname = document.add_mem.name.value ;
emmail = document.add_mem.email.value ;
pw1 = document.add_mem.pw1.value ;
pw2 = document.add_mem.pw2.value ;

if (emname ==""){
	sFlag = false ;
	 alert("請輸入姓名！" );
	 return  false ;
	}	
	
	if ((emmail.indexOf('@', 0) == -1) || emmail.indexOf('.') == -1){
	sFlag = false ;
	 alert("請輸入正確 email 帳號！" );
	 return  false ;
	}
	
if (pw1 == ""){
	sFlag = false ;
	 alert("請輸入密碼！" );
	 return  false ;
	}
	
if (pw1 != pw2){
	sFlag = false ;
	 alert("再次確認密碼與密碼不相同！" );
	 return  false ;
	}


//------------------------------------------	
	
return sFlag ;	

}
//=================================================================================
//修改會員資料
function check_edit_mem(){	
sFlag = true ;
emname = document.form3.name.value ;
pw1 = document.form3.pw1.value ;
pw2 = document.form3.pw2.value ;

if (emname ==""){
	sFlag = false ;
	 alert("姓名不能空白！" );
	 return  false ;
	}	
	
if (pw1 == ""){
	sFlag = false ;
	 alert("密碼不能空白！" );
	 return  false ;
	}
	
if (pw1 != pw2){
	sFlag = false ;
	 alert("再次確認密碼與密碼不相同！" );
	 return  false ;
	}

//------------------------------------------	
	
return sFlag ;	

}
//=================================================================================
//確認購物清單與運費
function check_fag(){	
sFlag = true ;
sum = parseInt(document.getElementById("sum").value) ;

if(sum < 3000){	
//------------------------------------------	
   if(document.form2.ft[0].checked){
       goto_url('index.php?checkout=2&ft=1');
   }else if(document.form2.ft[1].checked){
	   goto_url('index.php?checkout=2&ft=2');
   }else{
     document.getElementById("ftgmsg").innerHTML='<span style="color: #CC0000;">請選擇寄送區域，購物如滿3000運費則免！　 </span>';
   }
//------------------------------------------	
}else{	
goto_url('index.php?checkout=2');
}


}
//=================================================================================
//購物填寫資料表單
function checkform3(){
sFlag = true ;
mem = document.form3.member.value ;

emname = document.form3.name.value ;
address = document.form3.address.value ;
tel = document.form3.tel.value ;

tax_title = document.form3.tax_title.value ;
tax_no = document.form3.tax_no.value ;
//------------------------------------------
if (emname ==""){
	sFlag = false ;
	 alert("請輸入姓名！" );
	 return  false ;
	}
	
if (tel =="" ){
	sFlag = false ;
	 alert("請輸入聯絡電話！" );
	 return  false ;
	}
//------------------------------------------
if( mem == 'new'){
emmail = document.form3.email.value ;
	
if ((emmail.indexOf('@', 0) == -1) || emmail.indexOf('.') == -1){
	sFlag = false ;
	 alert("請輸入正確 email 帳號！" );
	 return  false ;
	}

}
//------------------------------------------
if (address ==""){
	sFlag = false ;
	 alert("請輸入地址！" );
	 return  false ;
	}
	
//------------------------------------------	
if(document.form3.k1[1].checked){

if (tax_title ==""){
	sFlag = false ;
	 alert("請輸入發票抬頭！" );
	 return  false ;
	}
	
if (tax_no == ""){
	sFlag = false ;
	 alert("請輸入統一編號！" );
	 return  false ;
	}	
	
}	
//------------------------------------------	
if(document.form3.c1[1].checked){
	
emname2 = document.form3.name2.value ;
address2 = document.form3.address2.value ;
tel2 = document.form3.tel2.value ;
emmail2 = document.form3.email2.value ;

if (emname2 ==""){
	sFlag = false ;
	 alert("請輸入收件人姓名！" );
	 return  false ;
	}
	
if (tel2 =="" ){
	sFlag = false ;
	 alert("請輸入收件人聯絡電話！" );
	 return  false ;
	}
	
if (address2 ==""){
	sFlag = false ;
	 alert("請輸入收件地址！" );
	 return  false ;
	}
	
}	
//------------------------------------------	
return sFlag ;	
}

//=================================================================================
//收件人同付款人
  function chk(input)
  {
    if(input.value == 2){
      document.getElementById("mi").style.display='';
    }else{
	  document.getElementById("mi").style.display='none';
	}
  }
  
//=================================================================================
//統一發票  
  function ck(input)
  {
    if(input.value == 2){
      document.getElementById("mi21").style.display='';
	  document.getElementById("mi22").style.display='';
    }else{
	  document.getElementById("mi21").style.display='none';
	  document.getElementById("mi22").style.display='none';
	}
  }


//=================================================================================
//運費 
  function ckf(input)
  {
    if(input.value == 1){
      
	  sum = parseInt(document.getElementById("sum").value)+70;
	  document.getElementById("ftgsum").innerHTML='<span class="style3">總計 NT$ <strong>'+ sum +'</strong> 元整 </span>';
	  document.getElementById("ftgmsg").innerHTML='&nbsp;';
    }else{
	  
	  sum = parseInt(document.getElementById("sum").value)+320;
	  document.getElementById("ftgsum").innerHTML='<span class="style3">總計 NT$ <strong>'+ sum +'</strong> 元整 </span>';
	  document.getElementById("ftgmsg").innerHTML='&nbsp;';
	}
  }

//======================================
//over border
  function chbor1(){
  this.style.border="1px solid #000";
  }
  
  function chbor2(){
  this.style.border="1px solid #AAA";
  }
  
//======================================
function sh_order(id){
	var order_area=document.getElementById("order_info_"+id);
	if(order_area.style.display=='none'){
		order_area.style.display='';
		document.getElementById("order_id_"+id).style.background = "url(/img/i3_d.gif) no-repeat 0px 5px"; 
	}else{
		order_area.style.display='none';
		document.getElementById("order_id_"+id).style.background = "url(/img/i3_r.gif) no-repeat 0px 2px"; 
	}
	
	
}
