// JavaScript Document
function loadcontent2(str)
{
	text=str.substr(0,2);
	leg=str.length;
	so=eval(str.substr(2,leg));
	if(text=='tc')
	{
		location.href='index.php?tc='+so;
		return 1;
	}
	if(text=='mg')
	{	
		location.href='index.php?mg='+so;
		return 1;
	}
	
	
}

function loadcontent(str)
{
	text=str.substr(0,2);
	leg=str.length;
	so=eval(str.substr(2,leg));
	if(text=='tc')
	{
		loadtrang(so);
		return 1;
	}
	if(text=='mg')
	{	loadmanga(so);
		return 1;
	}
	
	
}

function loadallchapter()
{	document.getElementById('main').innerHTML='';
	index.attachData('allchapter','1');
	index.update();
}
function loadalltapchi()
{
	document.getElementById('main').innerHTML='';
	index.attachData('alltapchi','1');
	index.update();
}
function loadtrang(id)
{	id=eval(id);
	if(id!="")
	{
	document.getElementById('main').innerHTML='';
	index.attachData('tc',id);
	index.update();
	}
}
function loadmanga(idmanga)
{	
	idmanga=eval(idmanga);
	if(idmanga!="")
	{
	document.getElementById('main').innerHTML='';
	index.attachData('mg',idmanga);
	index.update();
	}
}
function loadnews(id)
{
	document.getElementById('main').innerHTML='';
	index.attachData('news',id);
	index.update();
}

function register()
{
		document.getElementById('main').innerHTML='';
		index.attachData('dangky','1');
		index.update();
	
}

function quenpass()
{
		document.getElementById('main').innerHTML='';
		index.attachData('quanpass','1');
		index.update();
	
}
function logout()
{
	koolajax.load('code.php?logout');
	location.href='index.php';
}
function login()
{
	user=document.getElementById('logusername').value;
	pass=document.getElementById('logpassword').value;
	ch=document.getElementById('remember').checked;
	 if(ch)
  	{
	 remem=1;
 	}
 	 else
 	 {
	  remem=0;
 	 }
	 
	if(user=="" || pass=="")
	{
		alert('Please enter your username or password !');
		return false;
	}
	var request=new Request({
	method:"post",
	url:"code.php",
	onOpen:function()
	{
	document.getElementById( "load" ).innerHTML = "<img src='../../koolajax/loading/10.gif' alt='Loading.....'>";
	},
	onDone:function(result)
	{
	document.getElementById( "load" ).innerHTML = " ";
	var xmlDoc=koolajax.parseXml(result);
	x=xmlDoc.getElementsByTagName('result');
	text=x[0].childNodes[0].nodeValue;
	if(text=='xong')
	{
		location.href='index.php';
	}
	
	if(text=='miss')
	{
		alert('Password or username is incorrect !');
	}
	}
	})
	request.addArg('remember',remem);
	request.addArg('username',user);
	request.addArg('password',pass);
	koolajax.sendRequest(request);
	
}
function valid2()
{	email=document.getElementById('email').value;
	kiemtra=document.getElementById('kiemtra').value;
	if(email == "" || kiemtra=="")
	{
	alert('Please fill in the required fields');
	return false;
	}
	getpass();
}


function getpass()
{
	var request=new Request({
	method:"post",
	url:"code.php",
	onOpen:function()
	{
	document.getElementById( "load" ).innerHTML = "<img src='../../koolajax/loading/10.gif' alt='Loading.....'>";
	},
	onDone:function(result)
	{
	document.getElementById( "load" ).style.visibility='hidden';
	var xmlDoc = koolajax.parseXml(result);
	 x = xmlDoc.getElementsByTagName("result");
	 text=x[0].childNodes[0].nodeValue; 
	if(text=='failsecurity')
	{
	alert('Security code is wrong, check again !');
	loadcode();
	}
	if(text=='nouser')
	{
	alert('This account does not exist !');
	loadcode();
	}
	
	if(text=='thanhcong')
	{
	alert('Password has been sent to your email, check your email !');
	location.href='index.php';
	}
	if(text=='guimail')
	{
	alert('Some error happened, please contact webmaster to notify this !');
	location.href='index.php';
	}
	
	
	
	}
	});
	email=document.getElementById('email').value;
	kiemtra=document.getElementById('kiemtra').value;
	request.addArg('email',email);
	request.addArg('kiemtra',kiemtra);
	request.addArg('getpass','getpass');
	koolajax.sendRequest(request);
	
	
	
}




//bat buoc nhap day du du lieu moi cho submit form
//luu y 
	function dangky()
		{
			var request = new Request({
			method:"post",		
			url:"code.php",
			onOpen:function()
				{
					document.getElementById( "load" ).innerHTML = "<img src='../../koolajax/loading/10.gif' alt='Loading.....'>";
					document.getElementById( "load" ).style.visibility='visible';
				},	
	
			onDone:function(result){
					//Show order was processed
					document.getElementById( "load" ).style.visibility='hidden';
					var xmlDoc = koolajax.parseXml(result);
					 x = xmlDoc.getElementsByTagName("result");
					 text=x[0].childNodes[0].nodeValue; 
					 if(text=='email')
					 {
					 alert('Invalid email');
					 }
					 
					  if(text=='suce')
					 {
					 alert('Security code is wrong');
					 }
					 
					 if(text=='email fall')
					 {
					 alert('Email has been used, please use another email address !');
					 }
					 
					  if(text=='user')
					 {
					 alert('This username has been used !');
					 }
					 
					   if(text=='suc')
					 {
					 alert('Your registration has been successful !');
					 location.href='index.php';
					 }
					 
					    if(text=='eror')
					 {
					 alert('Some error happened, please contact webmaster to notify this !');
					 }

						
			//		location.href='index.php';
					
												
				}			
			});	
			//lay data tu form:
		
			username=document.getElementById('username').value
			pass1=document.getElementById('pass1').value
			pass2=document.getElementById('pass2').value
			email=document.getElementById('email').value
			code=document.getElementById('kiemtra').value
			
			//Send value to request, add value 'start order' to 'Posted'
			request.addArg("username",username);
			request.addArg("pass1",pass1);
			request.addArg("pass2",pass2);
			request.addArg("email",email);
			request.addArg('kiemtra',code);
			koolajax.sendRequest(request);				
		}		
		


function validate()
{
	if(document.form1.username.value=="" || document.form1.pass1.value=="" || document.form1.pass2.value=="" || document.form1.email.value=="" || document.form1.kiemtra.value=="" )
	{
		alert('Please,Fill in the require fields');
		return false;
		
	}
	if(document.form1.username.length < 4 )
	{
	alert('Username must be 4 letters at least ');
	
	return false;
	}
	
		if(document.form1.pass1.value!=document.form1.pass2.value)
		{
		alert('Password doesn\'t match');
		return false;
	
		}
		
		dangky();
	
}




function loadcode()
{
code=koolajax.load('ngaunhien.php');
document.getElementById('secu').innerHTML=code;
}




function loadchapter(chapter,page)
{

manga.attachData('chapter',chapter);
manga.attachData('page',page);
manga.update();
}

function xuly()
	{	
	
	
	
		lch=eval(document.getElementById('chapters').length);
		lp=eval(document.getElementById('page').length);
		p=eval(document.getElementById('page').value);
		ch=eval(document.getElementById('chapters').value);
		p+=1;
		if(p > lp)
		{
			index=document.getElementById('chapters').selectedIndex;     //lay index chapter hien tai
			fch=document.getElementById('chapters')[lch-1].value;    //gia tri cua chapter cuoi
			if(ch==fch)												// neu chapter hien tai == chapter cuoi							
			{ 
			 alert('The latest chapter will be update as soon as possible')
			}
			else
			{
				nextch = document.getElementById('chapters')[index+1].value; //lay gia tri cua chapter tiep theo
				location.href='manga.php?ch='+nextch;
				
		
				
			}
			
		
		}
		else
		{
		p2=document.getElementById('page').selectedIndex;
		document.getElementById('page').selectedIndex=(p2+1);
		loadchapter(ch,p);
		}
	
			
		
	
	}
	
	
	
	
	
	
	function xulyback()
	{	
		lch=eval(document.getElementById('chapters').length);
		lp=eval(document.getElementById('page').length);
		p=eval(document.getElementById('page').value);
		cp=eval(document.getElementById('chapters').value);
		p-=1;
		
		
	
	//	alert(page+','+lpage);
		if(p ==0 )
		{
					
		}
		else
		{
		p1=document.getElementById('page').selectedIndex;
		document.getElementById('page').selectedIndex=(p1-1);
		loadchapter(ch,p);
		}
	}
	
	
	
	function nextchapter()
	{	
	
		lch=eval(document.getElementById('chapters').length);
		ch=eval(document.getElementById('chapters').value);
		
			index=document.getElementById('chapters').selectedIndex;     //lay index chapter hien tai
			fch=document.getElementById('chapters')[lch-1].value;    //gia tri cua chapter cuoi
			if(ch==fch)												// neu chapter hien tai == chapter cuoi							
			{ 
			 alert('Bạn đã xem xong chapter mới nhất của manga này ! ')
			}
			else
			{
				nextch = document.getElementById('chapters')[index+1].value; //lay gia tri cua chapter tiep theo
				location.href='manga.php?ch='+nextch;
						
			}
	
	
	}
	
	function previouschapter()
	{	
	
		lch=eval(document.getElementById('chapters').length);
		ch=eval(document.getElementById('chapters').value);
		
			index=document.getElementById('chapters').selectedIndex;     //lay index chapter hien tai
			fch=document.getElementById('chapters')[0].value;    //gia tri cua chapter cuoi
			if(ch==fch)												// neu chapter hien tai == chapter cuoi							
			{ 
			 alert('Bạn đã xem xong chapter đầu tiên rồi :))')
			}
			else
			{
				nextch = document.getElementById('chapters')[index-1].value; //lay gia tri cua chapter tiep theo
				location.href='manga.php?ch='+nextch;
						
			}
	
	
	}
	
	

function delphanhoi(id)
{
	var request=new Request({
							method:"post",
							url:"code.php",
							onDone:function(result)
							{
								var xmlDoc = koolajax.parseXml(result);
								 x = xmlDoc.getElementsByTagName("result");
							 text=x[0].childNodes[0].nodeValue; 
								if(text=='success')
								{
										mybookinstance1.refresh(bookphanhoi);
								}
								if(text=='fail')
								{
										
								}
							}
							});
		request.addArg('delphanhoi','abc');
		request.addArg('idph',id);
		koolajax.sendRequest(request);
	
}
function xoabaiviet(id,tl)
{
	var request=new Request({
							method:"post",
							url:"code.php",
							onDone:function(result)
							{
								var xmlDoc = koolajax.parseXml(result);
		 	x = xmlDoc.getElementsByTagName("result");
		 	text=x[0].childNodes[0].nodeValue; 
			if(text=='fail')
			{
			alert('You don\'t have permission to do this task !');
			return ;
			}
			if(text=='suc')
			{
			location.href='khung.php?news='+tl;
			return ;
			}
			
			
					
							}
							});
		request.addArg('idbaiviet',id);
		request.addArg('action','xoabaiviet');
		koolajax.sendRequest(request);
}

function BASIC_GetCookie(Name){
var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
if (document.cookie.match(re)) //if cookie found
  return document.cookie.match(re)[0].split("=")[1] //return its value
return ""
}

function BASIC_SetCookie(name, value, days){
if (typeof days!="undefined"){ //if set persistent cookie
  var expireDate = new Date()
  var expstring=expireDate.setDate(expireDate.getDate()+days)
  document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()
}
else //else if this is a session only cookie
  document.cookie = name+"="+value
}

function baivietvipham(id)
{
	var t=BASIC_GetCookie('coolmangabaiviet'+id);
	if(!t)
	{
		BASIC_SetCookie('coolmangabaiviet'+id,'true',1);
	var request=new Request({
							method:"post",
							url:"code.php",
							onDone:function(result)
							{
								alert('Thanks you reporting, This article will be block when report reach to 10 !')
							}
							});
		request.addArg('idbaiviet',id);
		request.addArg('action','baivietvipham');
		koolajax.sendRequest(request);
	}
}





 function changepass1()
 {
 pass1=document.getElementById('pass1').value;
 pass2=document.getElementById('pass2').value;
 if(pass1=="" )
 	{
	alert('Xin vui lòng kiểm tra lại mật khẩu ');
	return false;
	}
if(pass1!=pass2)
{
alert('Mật khẩu không khớp');
return false;
}	
	var request=new Request({
	method:"post",
	url:"code.php",
	onOpen:function()
	{
	document.getElementById('load').innerHTML= "<img src='koolajax/loading/2.gif' alt='Loading.....'>";
	},
	onDone:function(result)
	{
	document.getElementById('load').innerHTML= "";
	var xmlDoc = koolajax.parseXml(result);
		 	x = xmlDoc.getElementsByTagName("result");
		 	text=x[0].childNodes[0].nodeValue; 
			if(text=='fail')
			{
			alert('Some errors happed !');
			return ;
			}
			if(text=='suc')
			{
				alert('Your password has been changed !');
			location.href='index.php';
			return ;
			}
	
	}
	});
	
	request.addArg('newpass',pass1);
	koolajax.sendRequest(request);
	
	
 }
 
 
 function newavatar()
 {
 var hinhanh=document.getElementById('hinhanh').value;
 	if(hinhanh=="")
 		{
			return false;
		}
		else
		{
			var request=new Request({
			method:"post",
			url:"code.php",
			onDone:function(result)
			{
			var xmlDoc = koolajax.parseXml(result);
		 	x = xmlDoc.getElementsByTagName("result");
		 	text=x[0].childNodes[0].nodeValue; 
			if(text=='fail')
			{
			alert('Some errors happed !');
			return ;
			}
			if(text=='suc')
			{
				alert('Your profile  has been updated !');
			location.href='index.php';
			return ;
			}
			}
			});
			request.addArg('updatehinhanh',hinhanh);
			koolajax.sendRequest(request);
		
		}
 }
 
 
 
 /* ----------------------------------------------------- Comment in chapter-------------------------- */

 
 
 
 function themphanhoi(idch)
{
	ed1=tinyMCE.get("phanhoi");
	data=ed1.getContent();
	kt=document.getElementById('kiemtra').value;
	if(data==""|| kt=="")
	{
		alert("Xin vui lòng nhập đầy đủ thông tin  !");
		return false;
	}
		var request = new Request({
			method:"post",		
			url:"code.php",
			onOpen:function()
				{
					
					document.getElementById( "load" ).innerHTML = "<img src='koolajax/loading/2.gif' alt='Loading.....'>";
					
				},	
	
	
			onDone:function(result){
				document.getElementById( "load" ).style.visibility='hidden';
				xmldoc=koolajax.parseXml(result);
			 
	 
				x=xmldoc.getElementsByTagName('result');
				text=x[0].childNodes[0].nodeValue;
			if(text=='login')
			{
				alert('Hãy đăng nhập để viết phản hồi !');
			}
			
			if(text=='failsecurity')
				{
					alert('Xin vui lòng kiểm tra lại mã xác nhận');
					loadcode();
				}
				
				if(text=="success")
				{
					alert('Thêm phản hồi thành công :))');
					mybookinstance1.refresh(bookphanhoi);
					loadcode();
				}
				if(text=="fail")
				{
					alert("Xin vui lòng thử lại :))")
					loadcode();
				}
			
								
				}			
			});	
			//lay data tu form:
			tieude=document.getElementById('ten').value;
			request.addArg("idch",idch)
			request.addArg("noidung",data)
			request.addArg("kiemtra",kt);
			request.addArg("tieude",tieude)
				request.addArg("geturl","ch");
			request.addArg("themphanhoi","abc");
			koolajax.sendRequest(request);					
}

  /* ----------------------------------------------------- End Comment in chapter-------------------------- */
 function bookmark(tieude,idch)
 {
	 
		var request = new Request({
			method:"post",		
			url:"code.php",
			onOpen:function()
				{
					
					document.getElementById( "load" ).innerHTML = "<img src='koolajax/loading/2.gif' alt='Loading.....'>";
					
				},	
	
	
			onDone:function(result){
				document.getElementById( "load" ).style.visibility='hidden';
				xmldoc=koolajax.parseXml(result);
			 				x=xmldoc.getElementsByTagName('result');
				text=x[0].childNodes[0].nodeValue;
			if(text=='login')
			{
				alert('Xin vui lòng đăng nhập để thực hiện chức năng này !');
			}
			if(text=='exist')
			{
				alert('Chapter này đã được bookmark !');
			}
			
		
				
				if(text=="suc")
				{
					alert('Bạn đã bookmark thành công :))');
					
				}
				if(text=="fail")
				{
					alert("Xin vui lòng thử lại :))")
					
				}
			
								
				}			
			});	
			//lay data tu form:
			
			request.addArg("ch",idch)
			request.addArg("tieude",tieude)
			request.addArg("bookmark","abc");
			koolajax.sendRequest(request);		
	 
 }
 
 
  function delbookmark(idbm)
 {
	 	if(confirm("Bạn có chắc là bạn muốn xóa bookmark này không"))
		{
		var request = new Request({
			method:"post",		
			url:"code.php",
			onOpen:function()
				{
					
					document.getElementById( "load" ).innerHTML = "<img src='koolajax/loading/2.gif' alt='Loading.....'>";
					
				},	
	
	
			onDone:function(result){
				document.getElementById( "load" ).style.visibility='hidden';
				xmldoc=koolajax.parseXml(result);
			 				x=xmldoc.getElementsByTagName('result');
				text=x[0].childNodes[0].nodeValue;
			if(text=='login')
			{
				alert('Xin vui lòng đăng nhập để thực hiện chức năng này !');
			}
		
							
				if(text=="suc")
				{
					location.href='index.php?action=bm';
					
				}
				if(text=="fail")
				{
					alert("Xin vui lòng thử lại :))")
					
				}
			
								
				}			
			});	
			//lay data tu form:
			
			request.addArg("idbm",idbm)
			
			request.addArg("delbookmark","abc");
			koolajax.sendRequest(request);		
		}
	 
 }
 
 
 
 function badlink(idch)
 {	

	 var request = new Request({
			method:"post",		
			url:"code.php",
				onOpen:function()
				{
					
					
					
				},	
			onDone:function(result){
							xmldoc=koolajax.parseXml(result);
			 				x=xmldoc.getElementsByTagName('result');
				text=x[0].childNodes[0].nodeValue;
						
				if(text=="suc")
				{
					alert('Thông báo của bạn đã được ghi nhận :))');
					
				}
				if(text=="fail")
				{
					alert("Xin vui lòng thử lại :))")
					
				}
			
								
				}			
			});	
			//lay data tu form:
			request.addArg("ch",idch)
			request.addArg("badlink","abc");
			koolajax.sendRequest(request);		
	 
 }