﻿$(function(){
	var email = $("#email");
	var emailInfo = $("#emailInfo");
	
	$("#add_mail").submit(function(){
		$('.loadingSave').fadeIn(1000,function(){
				$(this).html('<img src="images/ajax-loader.gif" alt="Loading" align="absmiddle" />').removeClass().addClass('loadingSave').fadeTo(900,1);
		});
			$.post("save_youremail.php",{your_email:$('#your_email').val()} ,function(data){
				if(data=='yes'){
					$('.loadingSave').fadeTo(200,0.1,function(){ 
						//$(this).html('<img src="images/yes.png" alt="บันทึกข้อมูลแล้ว" align="absmiddle" />').addClass('available').fadeTo(900,1);
						$(this).html(unescape('<img src="images/yes.png" alt="บันทึกข้อมูลแล้ว ทางเราจะส่ง e-mail ไปยังเมล์ที่ท่านระบุ เพื่อเป็นการแจ้งให้ท่านทราบอีกครั้ง ขอขอบคุณ" align="absmiddle" />')).removeClass().addClass('loadingSave').fadeTo(900,1);
					});
				}else if(data=='no'){
					$('.loadingSave').fadeTo(200,0.1,function(){ 
					//add message and change the class of the box and start fading
						//$(this).html('<img src="images/no.png" alt="ไม่สามารถบันทึกได้!!" align="absmiddle" />').addClass('error').fadeTo(900,1);
						$(this).html(unescape('<img src="images/no.png" alt="เกิดข้อผิดพลาดในการจัดเก็บข้อมูล โปรดลองใหม่อีกครั้ง!!" align="absmiddle" />')).removeClass().addClass('loadingSave').fadeTo(900,1);
					});
				}else if(data=='haved'){
					$('.loadingSave').fadeTo(200,0.1,function(){ 
					//add message and change the class of the box and start fading
						//$(this).html('<img src="images/no.png" alt="ไม่สามารถบันทึกได้!!" align="absmiddle" />').addClass('error').fadeTo(900,1);
						$(this).html(unescape('<img src="images/mail.png" alt="e-mail address นี้มีอยู่ในระบบแล้ว" align="absmiddle" />')).removeClass().addClass('loadingSave').fadeTo(900,1);
					});
				}else if(data=='no_match'){
					$('.loadingSave').fadeTo(200,0.1,function(){ 
						$(this).html(unescape('<img src="images/no.png" alt="รูปแบบอีเมล์ไม่ถูกต้อง โปรดลองอีกครั้ง" align="absmiddle" />')).removeClass().addClass('loadingSave').fadeTo(900,1);
					});
				}
			});
		return false;
	});
	

$('#ctl_show_add_pro').toggle(function(){
  $('#show_add_pro').show();$('#menuTitle').hide();
},function(){
  $('#show_add_pro').hide();$('#menuTitle').show();
});

$('#m_help').toggle(function(){
  $('#show_m_help').show();
},function(){
  $('#show_m_help').hide();
});

	$("#checkall_pro").livequery('click', function(){
		//alert($("#checkall_twk").attr("checked"));						   
		if($("#checkall_pro").attr("checked")==true){
			$(":checkbox").attr("checked","checked");
		}else{
			$(":checkbox").attr("checked","");
		}
	});
	
	$("#m_d_pro").livequery('click', function(){
		var numCHK=0;
		var type_form='pro';
		$.each($(":checkbox"), function(index) {
			if($("#chkbox_"+type_form+index).attr("checked")==true){
				numCHK++;
			}
		});
		if(numCHK>0){
		if (confirm('คุณต้องการลบข้อมูลที่เลือกใช่หรือไม่?')) {
				DeleteTABLE('pro');
		}
		}
	});
	$('#submit_add_pro').livequery('click', function(){ 
		$('.loadingSave').fadeIn(1000,function(){ //start fading the messagebox
			//add message and change the class of the box and start fading
			$(this).html('<img src="../images/ajax-loader.gif" alt="กำลังบันทึกข้อมูล..." align="absmiddle" /> กำลังบันทึกข้อมูล...').fadeTo(900,1);
		});
		$.post('save_pro.php',{ h_pro:$('#h_pro').val(),d_pro:$('#d_pro').val(),set_page:$('#set_page').val(),chkIMG:$('#chkIMG').val(),rand:Math.random() } ,function(data){
			/*alert(data);
			if(data=='yes'){
				$('.loadingSave').fadeTo(900,1,function(){ 
					$(this).html('<font color=green><b>บันทึกเรียบร้อยแล้ว</b></font>').fadeTo(900,1);
				});*/
				/*ShowDataTABLE('pro','load_data_pro.php');
				$('#h_pro').val('');
				$('#d_pro').val('');
				$('#h_pro').focus();*/
				window.location='data_promotions.php';
			/*}else{
				$('.loadingSave').fadeTo(900,1,function(){ 
					$(this).html('<font color=red><b>ไม่สามารถบันทึกได้</b></font>').fadeTo(900,1);
				});
			}*/
		});
		return false;
	});
	/*$("#form_add_pro").livequery('submit', function(){ 
		$('.loadingSave').fadeIn(1000,function(){ //start fading the messagebox
			//add message and change the class of the box and start fading
			$(this).html('<img src="../images/ajax-loader.gif" alt="กำลังบันทึกข้อมูล..." align="absmiddle" /> กำลังบันทึกข้อมูล...').fadeTo(900,1);
		});
		$.post('save_pro.php',{ h_pro:$('#h_pro').val(),d_pro:$('#d_pro').val(),set_page:$('#set_page').val(),rand:Math.random() } ,function(data){
			if(data=='yes'){
				$('.loadingSave').fadeTo(900,1,function(){ 
					$(this).html('<font color=green><b>บันทึกเรียบร้อยแล้ว</b></font>').fadeTo(900,1);
				});
				ShowDataTABLE('pro','load_data_pro.php');
				$('#h_pro').val('');
				$('#d_pro').val('');
				$('#h_pro').focus();
			}else{
				$('.loadingSave').fadeTo(900,1,function(){ 
					$(this).html('<font color=red><b>ไม่สามารถบันทึกได้</b></font>').fadeTo(900,1);
				});
			}
		});
		return false;
	});*/
	
	$("#m_r_pro").livequery('click', function(){
		//ShowDataTABLE('pro','load_data_pro.php');
		window.location='data_promotions.php';
	});
	
});

function DeleteTABLE(type_form){
	
	$('#show_action_'+type_form).livequery(function(){
		//ShowCheckingImg(this,'Deleting','loadingSave');
		$(this).html('<img src="../images/ajax-loader.gif" alt="กำลังลบข้อมูล..." align="absmiddle" /> <b><font color=red>กำลังลบข้อมูล...</font><b>').fadeTo(900,1);
		//$('#loadData').fadeIn('normal');
		//alert($("#chkbox_twk0").val());
		var condDel='';
		var chkFRT=0;
		$.each($(":checkbox"), function(index) {
			if($("#chkbox_"+type_form+index).attr("checked")==true){
				if(chkFRT==0){
					condDel=condDel+' id_'+type_form+'=&ja23'+$("#chkbox_"+type_form+index).val()+'&ja23';
				}else{
					condDel=condDel+' or id_'+type_form+'=&ja23'+$("#chkbox_"+type_form+index).val()+'&ja23';
				}
				chkFRT++;
			}
		});
		//alert(condDel);
		if(condDel){
			$.post(type_form+"_del.php",{ condDel:condDel,rand:Math.random() } ,function(data){
				//if(data=='yes'){
					//ShowDataTABLE('pro','load_data_pro.php');
					//$('#show_action_'+type_form).hide('fast');
					window.location='data_promotions.php';
				/*}else{
					$('#show_action_'+type_form).html('<font color=red><b>ไม่สามารถลบข้อมูลได้</b></font>');
				}*/
			});
		}
		return false;
	});
}
function show_pic(sc){
	document.form_add_pro.action='php_upload.php';
	document.form_add_pro.target='mypre';
	document.form_add_pro.submit();	
}
function edit_pro(name_pro,index,sts){
	if(sts=='open'){
		$('#show_'+name_pro+index).hide('fast');
		$('#edit_'+name_pro+index).show('fast');
	}else if(sts=='close'){
			$('#show_'+name_pro+index).show('fast');
		$('#edit_'+name_pro+index).hide('fast');
	}
}

function bt_update_edit_pro(id_pro,name_pro,index,fcr){
	$('.loadingUpdate_'+name_pro).fadeIn(1000,function(){ //start fading the messagebox
			//add message and change the class of the box and start fading
			$(this).html('<img src="../images/ajax-loader.gif" alt="กำลังบันทึกข้อมูล..." align="absmiddle" /> กำลังบันทึกข้อมูล...').fadeTo(900,1);
		});
	if(name_pro=='header_pro'){name_proV=$('#header_pro'+index).val();}else if(name_pro=='detail_pro'){name_proV=$('#detail_pro'+index).val();}
		$.post('update_pro.php',{ id_pro:id_pro,name_pro:name_pro,name_proV:name_proV,rand:Math.random() } ,function(data){
			window.location='data_promotions.php';
			//if(data=='yes'){
				/*$('.loadingUpdate_'+name_pro).fadeTo(900,1,function(){ 
					$(this).html('').fadeTo(900,1);
				});
				if(name_proV!=''){
					$('#text_'+name_pro+index).html(name_proV);
				}else{
					$('#text_'+name_pro+index).html('<font color='+fcr+'>คลิกเพื่อแก้ไขข้อความ</font>');
				}
				edit_pro(name_pro,index,'close');
			}else{
				$('.loadingUpdate_'+name_pro).fadeTo(900,1,function(){ 
					$(this).html('<font color=red><b>ไม่สามารถบันทึกได้</b></font>').fadeTo(900,1);
				});
			}*/
		});
}

function ShowDataTABLE(type_form,toLoadX,n_1,v_1){
	if(v_1!=''){var toLoadX=toLoadX+'?'+n_1+'='+v_1;}
	$("#checkall_"+type_form).attr("checked","");
	$('#table_'+type_form+'_load').livequery(function(){
		$(this).html('<br><div id="loadData" align=center><img src="../images/ajax-loader.gif" alt="กำลังโหลดข้อมูล..." align="absmiddle" /> กำลังโหลดข้อมูล...</div><br>');
		$('#loadData').fadeIn('normal');
		$.ajax({
			url: toLoadX,
			error: function(){
				$('#table_'+type_form+'_load').html(unescape('<br /><div align="center" class="error_page">ไม่พบข้อมูล</div><br />'), showNewContentX());
			},
			success: function() {
				//alert(type_form+' {type_form : ShowDataTABLE -> success');
				$('#table_'+type_form+'_load').load(toLoadX,'',showNewContentX());
			}
		});
		function showNewContentX() {
			$('#table_'+type_form+'_load').show('normal',hideLoaderX());
		}
		function hideLoaderX() {
			$('#loadData').fadeOut('normal');
		}
	});
}

/*function  show_wb(sID,sForm,sIDB,sIDok,sIDno,sA,sB,sC,sD,sE,sF,sG,sH,sI,sJ,sK){
	oObj = document.getElementById(sID);
	if(sForm=='' || sForm==0){
		display(sID);
		hide(sIDB);
		hide(sIDok);
		hide(sIDno);
		hide(sA);
		hide(sB);
		hide(sC);
		hide(sD);
		hide(sE);
		hide(sF);
		hide(sG);
		hide(sH);
		hide(sI);
		hide(sJ);
		hide(sK);
		document.form_chk_wb.chk_wb.value=1;
	}else{
		hide(sID);
		display(sIDB);
		display(sIDok);
		display(sIDno);
		display(sA);
		display(sB);
		display(sC);
		display(sD);
		display(sE);
		display(sF);
		display(sG);
		display(sH);
		display(sI);
		display(sJ);
		display(sK);
		document.form_chk_wb.chk_wb.value=0;
	}
}*/
function uzXmlHttp(){
var xmlhttp = false;
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
xmlhttp = false;
}
}

if(!xmlhttp && document.createElement){
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}
// End XmlHttp Object
function result_addform(){
	var name = document.form_wb.name.value;
	var email = document.form_wb.email.value;
	var subject = document.form_wb.subject.value;
	var note = document.form_wb.note.value;
	/*var result;
	var url = 'addwebboard.php?name=' + name + '&email='+ email + '&subject=' + subject + '&note='  + note; 
	//alert(url);
	xmlhttp = uzXmlHttp();
	xmlhttp.open("GET", url, false);
	xmlhttp.send(null); 
	// ѺҡѺҡ server ŧ result
	result = xmlhttp.responseText;*/
			$.post("addwebboard.php",{ name:name,email:email,subject:subject,note:note,rand:Math.random() } ,function(result){
				//alert(result);
				if(result==1){
					display("re_ok");
					hide("re_no");
					location.href='webboard.php';
				}else{
					hide("re_ok");
					display("re_no");
				}
			});
	//alert(result);
	/*if(result==1){
		display("re_ok");
		hide("re_no");
		location.href='webboard.php';
	}else{
		hide("re_ok");
		display("re_no");
	}*/
	hide("wb");
}
function result_addform_ans(){
	var id = document.form_swb.id.value;
	var name = document.form_swb.name.value;
	var email = document.form_swb.email.value;
	var note = document.form_swb.note.value;
			$.post("addwebboard_ans.php",{ id:id,name:name,email:email,note:note,rand:Math.random() } ,function(result){
				if(result==1){
					display("re_ok");
					hide("re_no");
					location.href='showwebboard.php?id=' + id;
				}else{
					hide("re_ok");
					display("re_no");
				}
			});
	/*var result;
	var url = 'addwebboard_ans.php?id=' + id + '&name=' + name + '&email='+ email + '&note='  + note; 
	xmlhttp = uzXmlHttp();
	xmlhttp.open("GET", url, false);
	xmlhttp.send(null); 
	// รับค่ากลับจาก server มาเก็บลง result
	result = xmlhttp.responseText;
	//alert(result);
	// นำค่าที่ได้ออกไปแสงผล
	if(result==1){
		display("re_ok");
		hide("re_no");
		location.href='showwebboard.php?id=' + id;
	}else{
		hide("re_ok");
		display("re_no");
	}*/
	hide("wb_ans");
}
function  show_wb(sID,sForm){
	oObj = document.getElementById(sID);
	if(sForm=='' || sForm==0){
		display(sID);
		document.form_chk_wb.chk_wb.value=1;
	}else{
		hide(sID);
		document.form_chk_wb.chk_wb.value=0;
	}
}
function display(sID) {
	var oObj = document.getElementById(sID);
	if (oObj) {
		oObj.style.display='inline';
	}
}

function hide(sID) {
	var oObj = document.getElementById(sID);
	if (oObj) {
		oObj.style.display='none';
	}
}

function emoticon(what,num){
	if(num==0){
		document.form_wb.note.value = document.form_wb.elements.note.value+" "+what;
		document.form_wb.note.focus();
	}else if(num==1){
		document.form_swb.note.value = document.form_swb.elements.note.value+" "+what;
		document.form_swb.note.focus();
	}else if(num==2){
		document.form_contact.c_detail.value = document.form_contact.elements.c_detail.value+" "+what;
		document.form_contact.c_detail.focus();
	}
}
