// JavaScript Document
var xmlhttp;
function createXMLDoc(){
xmlhttp=null;
if (window.XMLHttpRequest){
	// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }else{
	// for IE5, IE6
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
}



function sendtofriend(bookcode){ 
createXMLDoc();	
var sendfrom = document.send_to_friend.send_from.value;
var sendto = document.send_to_friend.send_to.value;
var sendmessage = document.send_to_friend.send_message.value;
	xmlhttp.open("GET","sendtofriend.php?sfr="+sendfrom+"&sto="+sendto+"&sme="+sendmessage+"&code="+bookcode,true);
	xmlhttp.onreadystatechange = function () {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) { 
			  document.getElementById("send_to_friend").innerHTML = "<div class=\"send_to_friend\"><div style=\"text-align:center;margin-top:100px;\"><h5>ส่งข้อความเรียบร้อยแล้ว</h5><br><img src=\"images/email-icon-white.png\" width=\"100\" ></div></div>"; 
			//alert("ส่งคำแนะนำ เรียบร้อยแล้ว "); 
			/*  document.getElementById("btn_tofriend").innerHTML ="<input name=\"send_btn\" id=\"send_btn\"  type=\"image\" value=\"submit\" src=\"images/send-normal.png\" onmouseover=\"oversendBtn()\" onmouseout=\"outsendBtn()\" onclick='return chkfrmtofriend();' />";
				document.send_to_friend.send_from.value = "";
                document.send_to_friend.send_to.value = ""; 
                document.send_to_friend.send_message.value = ""; */		
			}
		}else{
			document.getElementById("btn_tofriend").innerHTML = "<img src='images/loading.gif' >";
		}
	};
	xmlhttp.send(null);
} 

////<- แอดมิน ส่วนลด ////
//// หลัก //////
function setdiscount(priority,dis){ 
document.getElementById("main"+priority).innerHTML = "<input type=\"text\" value=\""+dis+"\" size=\"5\" id=\"editmain"+priority+"\" ><input type=\"button\" value=\"Edit\" onclick=\"editmaindis("+priority+")\" > "; 
document.getElementById("editmain"+priority).focus();
}

function editmaindis(pty){ 
createXMLDoc();	
var txt = document.getElementById("editmain"+pty).value; 
	xmlhttp.open("GET","editmaindis.php?txt="+txt+"&pty="+pty,true);
	xmlhttp.onreadystatechange = function () {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) { 
			  alert("ส่วนลดนี้มีผลต่อหมวดย่อย");
			  document.getElementById("main"+pty).innerHTML = xmlhttp.responseText;  			  
			  window.history.go(0);
			}
		}else{
			document.getElementById("main"+pty).innerHTML = "<img src='../images/loading.gif' >";
		}
	};
	xmlhttp.send(null);
} 

//////   สนพ ////// 
function pubsetdiscount(priority,dis){ 
document.getElementById("sub"+priority).innerHTML = "<input type=\"text\" value=\""+dis+"\" size=\"5\" id=\"editsub"+priority+"\" ><input type=\"button\" value=\"Edit\" onclick=\"editpubdis("+priority+")\" > "; 
document.getElementById("editsub"+priority).focus();
}

function editpubdis(pty){ 
createXMLDoc();	
var txt = document.getElementById("editsub"+pty).value; 
	xmlhttp.open("GET","editpubdis.php?txt="+txt+"&pty="+pty,true);
	xmlhttp.onreadystatechange = function () {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) { 
			if(xmlhttp.responseText == "no"){ 
			whatduplicate(pty);					
			}else{ 
			  document.getElementById("sub"+pty).innerHTML = xmlhttp.responseText;  
			  }
			}
		}else{
			document.getElementById("sub"+pty).innerHTML = "<img src='../images/loading.gif' >";
		}
	};
	xmlhttp.send(null);
}

function whatduplicate(p){ 
createXMLDoc();	
	xmlhttp.open("GET","editpubdis.php?type=1&pty="+p,true);
	xmlhttp.onreadystatechange = function () {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) { 
			alert(xmlhttp.responseText);
			document.getElementById("sub"+p).innerHTML = "0%"; 	
			}
		}else{ 
		document.getElementById("sub"+pty).innerHTML = "<img src='../images/loading.gif' >";
		} 
	};
	xmlhttp.send(null);
}



/// หมวดย่อย /////
function subsetdiscount(priority,dis){ 
document.getElementById("sub"+priority).innerHTML = "<input type=\"text\" value=\""+dis+"\" size=\"5\" id=\"editsub"+priority+"\" ><input type=\"button\" value=\"Edit\" onclick=\"editsubdis("+priority+")\" > "; 
document.getElementById("editsub"+priority).focus();
}

function editsubdis(pty){ 
createXMLDoc();	
var txt = document.getElementById("editsub"+pty).value; 
	xmlhttp.open("GET","editsubdis.php?txt="+txt+"&pty="+pty,true);
	xmlhttp.onreadystatechange = function () {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) { 
			  document.getElementById("sub"+pty).innerHTML = xmlhttp.responseText;  
			}
		}else{
			document.getElementById("sub"+pty).innerHTML = "<img src='../images/loading.gif' >";
		}
	};
	xmlhttp.send(null);
} 
/// แอดมิน ส่วนลด ->//// 


function EditRenamecate(pty){ 
	createXMLDoc();			
	var catename = document.getElementById("cate_edit_"+pty).value; 
	xmlhttp.open("GET","renamecate.php?id="+pty+"&value="+catename,true);
	xmlhttp.onreadystatechange = function () {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) { 				
				document.getElementById("cate_"+pty).innerHTML =xmlhttp.responseText; 
			}
		}
	};
	xmlhttp.send(null);
}


function Renamecate(priority,catename){ 
	document.getElementById("cate_"+priority).innerHTML = "<input type='text' id='cate_edit_"+priority+"' name='cate_edit_"+priority+"' value='"+catename+"' size='20' > <input type='button' onclick=EditRenamecate("+priority+"); value='Rename' > "; 
	document.getElementById("cate_edit_"+priority).select();
} 
		 
function EditRenamecatetitle(pty){ 
	createXMLDoc();			
	var catename= document.getElementById("cate_edit_title"+pty).value; 
	xmlhttp.open("GET","renamecate.php?id="+pty+"&value="+catename,true);
	xmlhttp.onreadystatechange = function () {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) { 				
				document.getElementById("cate_title"+pty).innerHTML =xmlhttp.responseText; 
			}
		}
	};
	xmlhttp.send(null);
}
		 
 function Renamecatetitle(priority,catename){ 
	document.getElementById("cate_title"+priority).innerHTML = "<input type='text' id='cate_edit_title"+priority+"' name='cate_edit_title"+priority+"' value='"+catename+"' size='20' > <input type='button' onclick=EditRenamecatetitle("+priority+"); value='Rename' > "; 
	document.getElementById("cate_edit_title"+priority).select();
 } 
 
		 
function EditCateOrder(pty){ 
	createXMLDoc();			
	var cateorder= document.getElementById("order_edit_"+pty).value; 
	xmlhttp.open("GET","cate_order_edit.php?id="+pty+"&value="+cateorder, true);
	xmlhttp.onreadystatechange = function () {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) { 
			if(xmlhttp.responseText == "no"){ 
			alert("ลำดับที่ซ้ำ กรุณาตรวจสอบ"); 			
			}else{ 
				document.getElementById("order_"+pty).innerHTML = " <label onclick=QuickEditOrder('"+pty+"','"+xmlhttp.responseText+"') >"+xmlhttp.responseText+"</label>  "; 
			}
			}
		}
	};
	xmlhttp.send(null);
}

function QuickEditOrder(priority, cateorder){ 
	document.getElementById("order_"+priority).innerHTML = "<input type='text' id='order_edit_"+priority+"' name='order_edit_"+priority+"' value='"+cateorder+"' size='5' > <input type='button' onclick=EditCateOrder("+priority+"); value='Save' > "; 
	document.getElementById("order_edit_"+priority).select();
} 


function EditBookTopOrder(bid){ 
	createXMLDoc();			
	var bookorder= document.getElementById("order_top_edit_"+bid).value; 
	xmlhttp.open("GET","book_toplist_order_edit.php?id="+bid+"&value="+bookorder, true);
	xmlhttp.onreadystatechange = function () {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) { 				
				document.getElementById("order_toplist_"+bid).innerHTML =xmlhttp.responseText; 
			}
		}
	};
	xmlhttp.send(null);
}

function EditTopOrder(book_id, book_order){ 
	document.getElementById("order_toplist_"+book_id).innerHTML = "<input type='text' id='order_top_edit_"+book_id+"' name='order_top_edit_"+book_id+"' value='"+book_order+"' size='5' > <input type='button' onclick=EditBookTopOrder("+book_id+"); value='Save' > "; 
	document.getElementById("order_top_edit_"+book_id).select();
}


			 function getRat(rat,book){ 
             createXMLDoc();
             xmlhttp.open("GET","votebook.php?vote="+rat+"&book="+book,true);
	         xmlhttp.onreadystatechange = function () {
               if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200) { 
				document.getElementById("comment_vote_value").value = rat;
      			alert("คุณโหวตให้หนังสือเล่มนี้ "+ rat +" คะแนน..เีรียบร้อยแล้ว");
				document.getElementById("loading-rat").innerHTML="";				
				document.getElementById("img-vote-bok").innerHTML=xmlhttp.responseText; 
                   } 
				   else{ 
			   document.getElementById("img-vote-bok").innerHTML= "<img src='images/admintool/loading.gif' >";
	           document.getElementById("loading-rat").innerHTML= "<img src='images/admintool/loading.gif' >";
				   }
                }            
            };
            xmlhttp.send(null);
				}
				
				function showRat(num){ 
				var i =parseInt(1);
				for(i=1;i<=num;i++){ 
				document.getElementById("star"+i).src = "images/star_full.png";
				}				
				}
				
				function reRat(){
				var i =parseInt(1);
				for(i=1;i<6;i++){ 
				document.getElementById("star"+i).src = "images/star_empty.png";
				}				
				}

function editbookSynopsis(book_synopsis,book_code){
document.getElementById("book_synopsis").innerHTML = " <textarea cols=\"65\" rows=\"5\" name=\"chg_book_synopsis\" id=\"chg_book_synopsis\" >"+book_synopsis+"</textarea> <input type=\"button\" value=\"Submit\" onclick=\"chgbookSynopsis('"+book_code+"');\" >"; 

        //<![CDATA[
            CKEDITOR.replace( 'chg_book_synopsis',{
              skin   : 'kama',
			  extraPlugins  : 'uicolor',
              uiColor  : '#fafafa', 
			  height   : 150,
              width    : 512,
toolbar :
        [
		 ['Source','Copy','Paste','PasteText','-','Bold','Italic','Underline','Link','Unlink','-','Image','Smiley','TextColor','BGColor'],'/',['Styles','Format','Font','FontSize']
        ],  
    filebrowserBrowseUrl : '/ckfinder/ckfinder.html',
    filebrowserImageBrowseUrl : '/ckfinder/ckfinder.html?Type=Images',
    filebrowserFlashBrowseUrl : '/ckfinder/ckfinder.html?Type=Flash',
    filebrowserUploadUrl : '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files',
    filebrowserImageUploadUrl : '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
    filebrowserFlashUploadUrl : '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash'
	} );
        //]]>
}

function chgbookSynopsis(bcode){ 
createXMLDoc();
var book_synopsis = String(CKEDITOR.instances["chg_book_synopsis"].getData());
///alert(book_synopsis);
////document.getElementById("overlay").width(document.width()).height(document.height());
     xmlhttp.open("GET","chg_booksynopsis.php?bcode="+bcode+"&bsynopsis="+book_synopsis,true);
	         xmlhttp.onreadystatechange = function () {
               if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200) { 
      document.getElementById("book_synopsis").innerHTML = xmlhttp.responseText; 
                   } 
				   else{ 
	  document.getElementById("book_synopsis").innerHTML= "<img src='images/admintool/loading.gif' >";
				   }
                }            
            };
            xmlhttp.send(null);
} 


function editbookTag(book_tag,book_code){ 
createXMLDoc(); 
     xmlhttp.open("GET","select_booktag.php?bcode="+book_code,true);
	         xmlhttp.onreadystatechange = function () {
               if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200) { 
      document.getElementById("book_tag").innerHTML = xmlhttp.responseText; 
                   } 
				   else{ 
	  document.getElementById("book_tag").innerHTML= "<img src='images/admintool/loading.gif' >";
				   }
                }            
            };
            xmlhttp.send(null);
} 

function chgbookTag(bcode){ 
createXMLDoc(); 
var book_tag = document.getElementById("chg_book_tag").value; 
     xmlhttp.open("GET","chg_booktag.php?bcode="+bcode+"&btag="+book_tag,true);
	         xmlhttp.onreadystatechange = function () {
               if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200) { 
      document.getElementById("book_tag").innerHTML = xmlhttp.responseText; 
                   } 
				   else{ 
	  document.getElementById("book_tag").innerHTML= "<img src='images/admintool/loading.gif' >";
				   }
                }            
            };
            xmlhttp.send(null);
} 

function Nameisbn(cisbn){ 
createXMLDoc(); 
     xmlhttp.open("GET","../core/check_isbn.php?isbn="+cisbn+"&type=n",true);
	 xmlhttp.onreadystatechange = function () {	
               if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200) { 				
				alert("isbn ซ้ำกับเรื่อง "+xmlhttp.responseText);
				// index.php?module=book&action=edit&id=8
				}
				}
	 };
  xmlhttp.send(null);
}


function checkisbn(bc){ 
	createXMLDoc(); 
	var isbn = document.getElementById("book_isbn").value; 
	xmlhttp.open("GET","../core/check_isbn.php?isbn="+isbn+"&code="+bc+"&type=s",true);
	xmlhttp.onreadystatechange = function () {	
		// alert("is"+xmlhttp.responseText); 
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) { 
				var anshttp = "";  anshttp = xmlhttp.responseText; 
				if(anshttp === "true"){ 
					document.getElementById("ans_chkisbn").value = "yes"; 
				}
				else{
					document.getElementById("ans_chkisbn").value = "no"; 
					Nameisbn(isbn);
				}
			}
		}            
	};
	xmlhttp.send(null);
} 

function Namesis(csis){ 
createXMLDoc(); 
     xmlhttp.open("GET","../core/check_sis.php?sis="+csis+"&type=n",true);
	 xmlhttp.onreadystatechange = function () {	
               if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200) { 
				alert("SIS ซ้ำกับเรื่อง "+xmlhttp.responseText);
				}
				}
	 };
  xmlhttp.send(null);
}

function checksis(bc){ 
	createXMLDoc(); 
	var sis = document.getElementById("book_sis").value; 
	xmlhttp.open("GET","../core/check_sis.php?sis="+sis+"&code="+bc+"&type=s",true);
	xmlhttp.onreadystatechange = function () {	
		// alert("is"+xmlhttp.responseText); 
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) { 
				var anshttp = "";  anshttp = xmlhttp.responseText; 
				if(anshttp === "true"){ 
					document.getElementById("ans_chksis").value = "yes"; 
				}
				else{					
					document.getElementById("ans_chksis").value = "no"; 
					Namesis(sis);
				}
			}
		}            
	};
	xmlhttp.send(null);
} 

function setPromotion(bcode){ 
createXMLDoc(); 
     xmlhttp.open("GET","set_promotion.php?bcode="+bcode,true);
	         xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4) {
              if (xmlhttp.status == 200) { 
			  if(xmlhttp.responseText=="recom"){ 
      document.getElementById("admin-promotion-book").src="images/admintool/promotion-icon-ok.png"; 
	  
			  }
			  else{ 
      document.getElementById("admin-promotion-book").src="images/admintool/promotion-icon.png"; 
			  } 
                   } 
				   else{ 
	document.getElementById("admin-promotion-book").src="images/admintool/loading.gif";
				   }
                }            
            };
            xmlhttp.send(null);
} 

function setTop10(bcode){ 
createXMLDoc(); 
     xmlhttp.open("GET","set_top10.php?bcode="+bcode,true);
	         xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4) {
              if (xmlhttp.status == 200) { 
			  
			  if(xmlhttp.responseText=="recom"){ 
      document.getElementById("admin-top10-book").src="images/admintool/top10-icon-ok.png"; 
			  }
			  else if(xmlhttp.responseText=="not"){ 
      document.getElementById("admin-top10-book").src="images/admintool/top10-icon.png"; 
	  
			  }else if(xmlhttp.responseText=="no"){ 
			  alert("มีการตั้งค่ามากกว่า 10 อันดับแล้ว ");			  
			  }
                   } 
				   else{ 
	document.getElementById("admin-top10-book").src="images/admintool/loading.gif";
				   }
                }            
            };
            xmlhttp.send(null);
} 


function setRecomment(bcode){ 
createXMLDoc(); 
     xmlhttp.open("GET","set_recomment.php?bcode="+bcode,true);
	         xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4) {
              if (xmlhttp.status == 200) { 
			  if(xmlhttp.responseText=="recom"){ 
      document.getElementById("admin-recomment-book").src="images/admintool/star-icon-ok.png"; 
	  
			  }
			  else{ 
      document.getElementById("admin-recomment-book").src="images/admintool/star-icon.png"; 
			  } 
                   } 
				   else{ 
	document.getElementById("admin-recomment-book").src="images/admintool/loading.gif";
				   }
                }            
            };
            xmlhttp.send(null);
} 



function cancelRecomment(bcode){ 
createXMLDoc(); 
     xmlhttp.open("GET","cancel_recomment.php?bcode="+bcode,true);
	         xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4) {
              if (xmlhttp.status == 200) { 
			  if(xmlhttp.responseText=="OK"){ 
      document.getElementById("admin-cancel-recomment-book").src="images/admintool/star-icon.png"; 
			  }
			  else{ 
      document.getElementById("admin-cancel-recomment-book").src="images/admintool/star-icon-ok.png"; 
			  } 
                   } 
				   else{ 
	  document.getElementById("admin-cancel-recomment-book").src="images/admintool/loading.gif";
				   }
                }            
            };
            xmlhttp.send(null);
} 



function changSubname(Subid,Subname){ 
document.getElementById("sub"+Subid).innerHTML = "<input type='text' id='value"+Subid+"' value='"+Subname+"' size='10' > <input type='button' onclick='Editsub("+Subid+")' value='Edit' > "; 
} 

function Editsub(Subid){ 
createXMLDoc();  
var txtcategory = document.getElementById("value"+Subid).value;  
     xmlhttp.open("GET","edit_subcategory.php?priority="+Subid+"&subname="+txtcategory,true);
	         xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4) {
              if (xmlhttp.status == 200) { 
			   
                     document.getElementById("sub"+Subid).innerHTML=xmlhttp.responseText+"<label> <img src='images/admintool/edit-menu-icon.png' title='แก้ไขหมวดย่อย' onclick=changSubname('"+Subid+"','"+xmlhttp.responseText+"') ></label>";
                   } else{ 
				     document.getElementById("sub"+Subid).innerHTML="<img src=images/admintool/loading.gif border=0 >";
				   }
                }            
            };
            xmlhttp.send(null);
} 

function Inserbookmode(){ 
	var hdnAction = document.getElementsByName("hdnAction").value;
	var cate = document.getElementsByName("cate").value;
	var cmdUpload = document.getElementsByName("cmdUpload").value;
	var book_name = document.getElementsByName("book_name").value; 
	var book_type = document.getElementsByName("book_type").value;
	var isbn = document.getElementsByName("isbn").value;
	var sis = document.getElementsByName("sis").value;
	var bindex = document.getElementsByName("bindex").value;
	var numpage = document.getElementsByName("numpage").value;
	var weight = document.getElementsByName("weight").value;
	var wh = document.getElementsByName("wh").value;
	var publisher = document.getElementsByName("publisher").value;
	var covertype = document.getElementsByName("covertype").value;
	var paper = document.getElementsByName("paper").value;
	var author = document.getElementsByName("author").value;
	var translate = document.getElementsByName("translate").value;
	var price = document.getElementsByName("price").value;
	var stock = document.getElementsByName("stock").value;
								
     xmlhttp.open("GET","upload.php?hdnAction="+hdnAction+"&cate="+cate+"&cmdUpload="+cmdUpload+"&book_name="+book_name+"&book_type="+book_type+"&isbn="+isbn+"&sis="+sis+"&bindex="+bindex+"&numpage="+numpage+"&weight="+weight+"&wh="+wh+"&publisher="+publisher+"&covertype="+covertype+"&paper="+paper+"&author="+author+"&translate="+translate+"&price="+price+"&stock="+stock,true);
	         xmlhttp.onreadystatechange = function () {   };
             xmlhttp.send(null);
    }
	
function CalweekMonth(){ 
createXMLDoc();  
var  selMonth = document.getElementById("month").value; 
var  selYear  = document.getElementById("year").value; 

     xmlhttp.open("GET","calweekmonth.php?m="+selMonth+"&y="+selYear,true);
	         xmlhttp.onreadystatechange = function () {
				
                if (xmlhttp.readyState == 4) {
			  if(xmlhttp.responseText == "4"){ 
			         document.getElementById("alert-choose-my").style.display = "none";
                     document.getElementById("weeknum4").style.display = "inline";
			  }else if(xmlhttp.responseText == "5"){ 
			         document.getElementById("alert-choose-my").style.display = "none";
                     document.getElementById("weeknum5").style.display = "inline";
			  }else{ 
				     document.getElementById("alert-choose-my2").innerHTML = xmlhttp.responseText;
			  }
				}
			 };
             xmlhttp.send(null);
 } 


/*
			  if(xmlhttp.responseText == 4){ 
			         document.getElementById("alert-choose-my").style.display = "none";
                     document.getElementById("weeknum4").style.display = "inline";
			  }else if(xmlhttp.responseText == 5){ 
			         document.getElementById("alert-choose-my").style.display = "none";
                     document.getElementById("weeknum5").style.display = "inline";
			  }else{ 
				     document.getElementById("alert-choose-my").value = xmlhttp.responseText;
			  }


*/



