<?php
require_once 'library/top_application.php';
require_once 'library/logged.php';

$sql = mysql_query('SELECT `uid`, `username`, `manage_user`, `manage_section`, `manage_news`, `manage_content`, `manage_product`, `manage_gallery`, `manage_banner`, `email` FROM `'.TBL_USER.'` WHERE `uid` = '.$_SESSION['Logged_Uid']);
$row = mysql_fetch_assoc($sql);

$sql_all_user = mysql_query('SELECT `uid`, `username`, `manage_user`, `manage_section`, `manage_news`, `manage_content`, `manage_product`, `manage_gallery`, `manage_banner`, `create_date`, `login_date`, `email` FROM `'.TBL_USER.'` ORDER BY uid ASC');
$row_all_user = mysql_fetch_assoc($sql_all_user);

if ($row["manage_user"] == 1){
	
	// $sql_site = mysql_query('SELECT * FROM `general_setting`');
	$sql_site = mysql_query('SELECT * FROM `contact_setting`');
	$row_site = mysql_fetch_assoc($sql_site);
}

?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Artech Creative</title>
<link href="css/css_admin.css?rnd=13" rel="stylesheet" type="text/css">
<link href="../css/tiny.css" rel="stylesheet" type="text/css">
<link href="css/drag_list.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="../js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="../js/jquery-ui-min.js"></script>
<script type="text/javascript" src="../js/tinybox_product.js"></script>
<script type="text/javascript" src="../js/check_function.js"></script>
<!--[if IE]>
    <script src="../js/html5.js"></script>
<![endif]-->
<script type="text/javascript" src="../js2/password.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	$("input[name=confrim_password]").change(function(){
		var new_pw = $('input[name=new_password]').val();
		var cf_pw = $(this).val();
		
		if (check_PW_length(new_pw) == false){
			$('#check_confirm_pw').html('<span style="color: red">密碼太短!</span>')
			return
		} else {
			if(cf_pw == new_pw){
				$('#check_confirm_pw').html('<span style="color: green">OK!</span>')
				$(this).removeClass("error");
			} else {
				$('#check_confirm_pw').html('<span style="color: red">密碼不符, 請重試!</span>')
				$(this).addClass("error");
			}
		}
	});
	$("input[name=email]").change(function(){
		var this_value = $(this).val();	
	
		if (checkEmail(this_value) == false){
			$(this).addClass("error");
			$('#er_email').show();
			$('#er_email').html('<span style="color: red">電郵格式錯誤</span>');
		} else {
			$('#er_email').hide();
			$(this).removeClass("error");
		}
	})
	
	/****** check before submit ******/
	$('#btn_submit').click(function(){
		var error = false;															
		var email= $('input[name=email]').val();
		var new_pw = $('input[name=new_password]').val();
		var cf_pw = $('input[name=confrim_password]').val();
		
		if(!new_pw || !cf_pw){
			$('#check_confirm_pw').html('<span style="color: red">請輸入密碼!</span>')
			error = true;
		} 
		if (check_PW_length(new_pw) == false){
			$('#check_confirm_pw').html('<span style="color: red">密碼太短!</span>')
			error = true;
			return
		}
		if (checkEmail(email) == false){
			$('input[name=email]').addClass("error");
			$('#er_email').show();
			$('#er_email').html('<span style="color: red">電郵格式錯誤</span>');
			error = true;
		} else {
			$('#er_email').hide();
			$('input[name=email]').removeClass("error");
		}
		if(new_pw != cf_pw){
			error = true;
			$('#check_confirm_pw').html('<span style="color: red">密碼不符, 請重試!</span>')
			return
		}
		if (error == true){
			$("#update_info").html('<b style="color:red;">更新失敗!!!</b>');
			return
		} else {
			$.ajax({
				type: "POST",
				url: "ajax/update_admin.php",
				data: {"uid" : <?php echo $_SESSION['Logged_Uid'];?>, "n_email" : email, "n_password" : new_pw},
				cache: false,
				success: function(html){
					$("#update_info").html(html);
				}
			});
		}
	})
	
	$('.manage_edit').each(function(){
		var btn_show = 	$('.showing_2', this);
		var btn_not_show = $('.not_showing_2', this);
		var this_id_name = 'uid';
		var this_table = 'contact_setting';
		
		btn_show.click(function(){
			var this_field = $(this).attr('rel');
			var this_id = parseInt($(this).attr('rev'));
			$.ajax({
				type: "POST",
				url: "ajax/update_yesno.php",
				data: {"id_name" : this_id_name, "id" : this_id, "update_field" :this_field, "show" : 2, "table": this_table},
				cache: false,
				success: function(html){
					btn_show.hide();
					btn_not_show.show();
				}
			});
		})
		
		btn_not_show.click(function(){
			var this_field = $(this).attr('rel');
			var this_id = parseInt($(this).attr('rev'));
			$.ajax({
				type: "POST",
				url: "ajax/update_yesno.php",
				data: {"id_name" : this_id_name, "id" : this_id, "update_field" :this_field, "show" : 1, "table": this_table},
				cache: false,
				success: function(html){
					btn_show.show();
					btn_not_show.hide();
				}
			});
		})
	})
	
	$('.btn_delete').click(function(){
		var this_id = parseInt($(this).attr('rel'));
		var table_name = 'contact_setting';
		var this_id_name = 'uid';
		
		if (confirm("Confirm to delete this admin ?")){
			$.ajax({
				type: "POST",
				url: "ajax/delete.php",
				data: {"table" : table_name, "id_name" : this_id_name, "del_id" : this_id},
				cache: false,
				success: function(html){
					location.reload();
				}
			});
		} else {
			return false;
		}
	})
	
	$('#btn_more_option').click(function(){
		if($(this).hasClass('collapse')){
			$('#more_option').css('display', 'block');
			$(this).removeClass('collapse').addClass('expand');
			$(this).text('Hide Option [-]');
		} else {
			$('#more_option').css('display', 'none');
			$(this).removeClass('expand').addClass('collapse');
			$(this).text('More Option [+]');
		}
	})
})

function closeJS(){
	location.reload();
}
function changeInput(input_name){
	var input_val =  $.trim($('#'+input_name).val());
	$.ajax({
		type: "POST",
		url: "ajax/update_contact.php",
		data: {"val" : input_val, "field" : input_name},
		cache: false,
		success: function(html){
			$('#update_g_info').html(html);
		}
	});
}
function onlyTextInput(input_name){
	var input_val = $('#'+input_name).val();
	var iChars = "!#$%^@&*+=[]\\\'/{}|\":<>?~_";
	var error = false;
	for (var i = 0; i < input_val.length; i++) {
		if (iChars.indexOf(input_val.charAt(i)) != -1) {
			$('#'+input_name).addClass("error");
			$('#er_'+input_name).show();
			$('#er_'+input_name).text("不能有符號");	
			error = true
			break;			  
		} else {
			$('#er_'+input_name).hide();
			$('#'+input_name).removeClass("error");			
		}
	}
	if(error == true){
		return
	} else {
		$.ajax({
			type: "POST",
			url: "ajax/update_contact.php",
			data: {"val" : input_val, "field" : input_name},
			cache: false,
			success: function(html){
				$('#update_g_info').html(html);
			}
		});
	}
	
}

</script>
</head>

<body>
<div id="wrap">
  <section id="column_left">
    <?php include 'library/left_menu.php';?>
  </section>
  <section id="column_right">
    <div class="admin_content"> 
      <h1>聯絡我們設定</h1>
      
      
      <?php if ($row["manage_user"] == 1){?>

      	 <span id="update_g_info"></span>
      <div class="sep_line" style="margin-bottom:20px;"></div>
       <div class="table_row">
         
         
        <div class="clearfix" style="height:20px"></div>
        <div class="table_row">
          <div class="table_label" style="width:100px">聯絡地址:</div>
          <div class="table_input"> ENG:
            <input name="ep_address" id="ep_address" type="text" value="<?php echo $row_site["ep_address"]; ?>" style="width:250px" onChange="changeInput('ep_address')">
            <span style="color:red">*</span> <span id="er_ep_sitename" class="er_msg"></span><br>
            繁中:
            <input name="cp_address" id="cp_address"  type="text" value="<?php echo $row_site["cp_address"]; ?>" style="width:250px" onChange="changeInput('cp_address')">
            <span id="er_cp_sitename" class="er_msg"></span> </div>
        </div>
        <div class="clearfix" style="height:20px"></div>
        <div class="table_row">
          <div class="table_label" style="width:100px">聯絡電話:</div>
          <div class="table_input"> ENG:
            <input name="ep_tel" id="ep_tel" type="text" value="<?php echo $row_site["ep_tel"]; ?>" style="width:250px" onChange="changeInput('ep_tel')">
            <span style="color:red">*</span> <span id="er_ep_sitename" class="er_msg"></span><br>
            繁中:
            <input name="cp_tel" id="cp_tel"  type="text" value="<?php echo $row_site["cp_tel"]; ?>" style="width:250px" onChange="changeInput('cp_tel')">
            <span id="er_cp_sitename" class="er_msg"></span> </div>
        </div>
        <div class="clearfix" style="height:20px"></div>
        <div class="table_row">
          <div class="table_label" style="width:100px">電郵地址:</div>
          <div class="table_input"> ENG:
            <input name="ep_email" id="ep_email" type="text" value="<?php echo $row_site["ep_email"]; ?>" style="width:250px" onChange="changeInput('ep_email')">
            <span style="color:red">*</span> <span id="er_ep_sitename" class="er_msg"></span><br>
            繁中:
            <input name="cp_email" id="cp_email"  type="text" value="<?php echo $row_site["cp_email"]; ?>" style="width:250px" onChange="changeInput('cp_email')">
            <span id="er_cp_sitename" class="er_msg"></span> </div>
        </div>
        <div class="clearfix" style="height:20px"></div>
        <div class="table_row">
          <div class="table_label" style="width:100px">服務時間:</div>
          <div class="table_input"> ENG:
             <textarea rows="4" cols="50" name="ep_time" id="ep_time"  type="text"  style="width:250px" onChange="changeInput('ep_time')"><?php echo $row_site["ep_time"]; ?></textarea>	
            <span style="color:red">*</span> <span id="er_ep_sitename" class="er_msg"></span><br>
            繁中:
              <textarea rows="4" cols="50" name="cp_time" id="cp_time"  type="text"  style="width:250px" onChange="changeInput('cp_time')"><?php echo $row_site["cp_time"]; ?></textarea>	
            <span id="er_cp_sitename" class="er_msg"></span> </div>
        </div>
        <div class="clearfix" style="height:20px"></div>
        <div class="table_row">
          <div class="table_label" style="width:100px">地圖資料:</div>
          <div class="table_input"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
             <textarea rows="4" cols="50" name="gmap" id="gmap"  type="text"  style="width:250px" onChange="changeInput('gmap')"><?php echo $row_site["gmap"]; ?></textarea>	
            <span style="color:red">*</span> <span id="er_ep_sitename" class="er_msg"></span><br>
            </div>
        </div>

        <br>
      <br>
      <br>
     


      
      <?php
           
		}?>
      <div class="clearfix" style="height:50px;"></div>
    </div>
  </section>
</div>
<?php include 'library/bottom.php'; ?>
</body>
</html>
