function CreateAjaxObject() {
	var xmlhttp = false;

	try	{ 
		// Firefox, Opera, Safari
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		try {
			// Internet Explorer 6+
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				// Internet Explorer 5.5+
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX.");
			}
		}
	}
	
	return xmlhttp;
}

function Redirect(page) {
	window.location = page;
}

function reloadPage()
{
	window.location.reload();
}

function LoadSection(section) {
	if (section.indexOf("|") > 0) {
		temp = section;
		section = temp.substring(0, temp.indexOf("|"));
		param = temp.substring(temp.indexOf("|") + 1, temp.length);
	}
	switch (section) {
		case 'sidebar':
			var xmlhttp = CreateAjaxObject();
			xmlhttp.open("GET", "/sidebar/", true);
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4) {
					document.getElementById("sidebar_userstuff").innerHTML = xmlhttp.responseText;
				}
			}
			xmlhttp.send(null);
			return false;
		case 'episode':
			window.location.reload();
			return false;
		case 'download':
			var xmlhttp = CreateAjaxObject();
			xmlhttp.open("GET", "/downloads/" + param, true);
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4) {
					document.getElementById("download").innerHTML = xmlhttp.responseText;
				}
			}
			xmlhttp.send(null);
			return false;
		case 'mirror':
			var xmlhttp = CreateAjaxObject();
			xmlhttp.open("GET", "/mirror/" + param, true);
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4) {
					document.getElementById("mirror").innerHTML = xmlhttp.responseText;
				}
			}
			xmlhttp.send(null);
			return false;
		case 'genre':
			var xmlhttp = CreateAjaxObject();
			xmlhttp.open("GET", "/genrelist/" + param, true);
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4) {
					document.getElementById("genre").innerHTML = xmlhttp.responseText;
				}
			}
			xmlhttp.send(null);
			return false;
		case 'tag':
			var xmlhttp = CreateAjaxObject();
			xmlhttp.open("GET", "/taglist/" + param, true);
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4) {
					document.getElementById("tag").innerHTML = xmlhttp.responseText;
				}
			}
			xmlhttp.send(null);
			return false;
		case 'alttitle':
			var xmlhttp = CreateAjaxObject();
			xmlhttp.open("GET", "/alttitlelist/" + param, true);
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4) {
					document.getElementById("alttitle").innerHTML = xmlhttp.responseText;
				}
			}
			xmlhttp.send(null);
			return false;
		case 'photos':
			var xmlhttp = CreateAjaxObject();
			xmlhttp.open("GET", "/photos/" + param, true);
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4) {
					document.getElementById("photos").innerHTML = xmlhttp.responseText;
				}
			}
			xmlhttp.send(null);
			return false;
			
		case 'review':
			var xmlhttp = CreateAjaxObject();
			xmlhttp.open("GET", "/review/" + param, true);
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4) {
					document.getElementById("review").innerHTML = xmlhttp.responseText;
					var xmlhttp2 = CreateAjaxObject();
					xmlhttp2.open("GET", "/stars/" + param, true);
					xmlhttp2.onreadystatechange = function() {
					if (xmlhttp2.readyState == 4) {
						document.getElementById("stars").innerHTML = xmlhttp2.responseText;
						}
					}
					xmlhttp2.send(null);
				}
			}
			xmlhttp.send(null);
			return false;
		}
}

function Logout() {
	var xmlhttp = CreateAjaxObject();
	xmlhttp.open("GET", "/logout/", true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			LoadSection('sidebar');
		}
	}
	xmlhttp.send(null);
	return false;
}

function isNumberKey(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

	return true;
}

function isAlphaNumberSpaceKey(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode
	if((charCode > 47 && charCode < 58) || (charCode > 64 && charCode < 91) || (charCode > 96 && charCode < 123) || charCode <= 32)
		return true;
		
	return false;
}

function sendAjaxRequest(episodeId,animeId)
{
	var xmlhttp = CreateAjaxObject();
		if(!toggle_lastWatched)
		{
			xmlhttp.open("GET", "/lastEpisodeWatch/" + episodeId+'/'+animeId+'/true', true);
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4) {
					//alert( xmlhttp.responseText);
					if(xmlhttp.responseText!='login')
					{
						//alert('Marked as Last Watched Episode');
						document.getElementById('lastWatched').innerHTML="This was my last watched episode";
						toggle_lastWatched=true;	
					}
					else
					{
						showPopWin('/login/', 740, 400, LoadSection, true);
					}	
				}
			}
			xmlhttp.send(null);
		}
		else
		{
			xmlhttp.open("GET", "/lastEpisodeWatch/" + episodeId+'/'+animeId+'/false', true);
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4) {
					//alert( xmlhttp.responseText);
					if(xmlhttp.responseText!='login')
					{
						document.getElementById('lastWatched').innerHTML="Last Watched Episode?";
						alert('Removed Last Watched Episode Marker');
						toggle_lastWatched=false;	
					}
					else
					{
						showPopWin('/login/', 740, 400, LoadSection, true);
					}
					
				}
			}
			xmlhttp.send(null);
		}
	return false;
}

function getLastWatched(animeID){
	var xmlhttp = CreateAjaxObject();
	xmlhttp.open("GET", "/getlastEpisodeWatch/" + animeID, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			//alert( xmlhttp.responseText);
			if(xmlhttp.responseText!="no")
			{
				Element.addClassName('episode'+xmlhttp.responseText,'lwrow');				
			}
			
		}
	}
	xmlhttp.send(null);
}
//check if editing rights are avilable to logged in user, and add's the link to anime page
function isAllowedToEdit(anime_id)
{
		var xmlhttp = CreateAjaxObject();
			xmlhttp.open("GET", "/editshowlink/"+anime_id, true);
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4) {
					if(xmlhttp.responseText!='fail')
					{
						document.getElementById("editAnimeShow").style.display='block';
						document.getElementById("editAnimeShow").innerHTML = xmlhttp.responseText;
					}
				}
			}
			xmlhttp.send(null);

}

function postComment()
{
		var xmlhttp = CreateAjaxObject();
		var cmt=trim(document.getElementById('cm_submitcomments').value);
		var cmtid=document.getElementById('cm_commentid').value;
		var epid=document.getElementById('cm_episodeid').value;
		if(cmt.length==0)
		{
			return false;
		}
		ajaxifyComments(cmt,epid,cmtid,'cm_submitcomments','cmtpostmsg');
	return false;
}

function submitChildComment(commentid)
{
		var cmt=trim(document.getElementById('child_comment_box_'+commentid).value);
		var cmtid=document.getElementById('commentid_'+commentid).value;
		var epid=document.getElementById('episodeid_'+commentid).value;
		if(cmt.length==0)
		{
			return false;
		}
		ajaxifyComments(cmt,epid,cmtid,'child_comment_box_'+commentid,'child_comment_'+commentid);
	return false;
}

function ajaxifyComments(cmt,epid,cmtid,txetareaid,msgboxid)
{
	var xmlhttp = CreateAjaxObject();
		document.getElementById(txetareaid).value='';
		var param='submitcomments='+cmt+'&episodeid='+epid+'&commentid='+cmtid;
			xmlhttp.open("POST", "/addcomment", true);
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttp.setRequestHeader("Content-length", param.length);
			xmlhttp.setRequestHeader("Connection", "close");
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4) {
					if(xmlhttp.responseText =='saved')
					{
						document.getElementById(msgboxid).style.display='block';
						document.getElementById(msgboxid).innerHTML='Your comment saved.';
					}
					else if(xmlhttp.responseText=='failed')
					{
						document.getElementById(msgboxid).style.display='block';
						document.getElementById(txetareaid).value=cmt;
						document.getElementById(msgboxid).innerHTML='Falied to save please try again.';
					}
					else
					{
						document.getElementById(msgboxid).style.display='block';
						document.getElementById(txetareaid).value=cmt;
						document.getElementById(msgboxid).innerHTML=xmlhttp.responseText;
					}
				}
			}
			xmlhttp.send(param);
}

function deleteThis(type,id,episode_id)
{
	switch(type)
	{
		case 'episode':
				if(confirm("Are You Sure You Want To Delete This?"))
				{
					window.location="/deletethis/episode/"+episode_id+"/"+id;
				}
			break;
		case 'mirror':
				if(confirm("Are You Sure You Want To Delete This?"))
				{
					window.location="/deletethis/mirror/"+episode_id+"/"+id;
				}
			break;
		case 'download':	
				if(confirm("Are You Sure You Want To Delete This?"))
				{
					window.location="/deletethis/download/"+episode_id+"/"+id;

				}
			break;
		case 'review':	
				if(confirm("Are You Sure You Want To Delete This?"))
				{
					window.location="/deletethis/review/"+episode_id+"/"+id;

				}
			break;
		
	}
	//return false;
}

function checkLink(link,id,type)
{	
		if(trim(link).length==0)
		{	
			return;
		}
		var xmlhttp = CreateAjaxObject();
			if(type=='mirror')
				xmlhttp.open("GET", "/check-link-mirror/"+encodeURI(trim(link)), true);
			else
				xmlhttp.open("GET", "/check-link-downlaod/"+encodeURI(trim(link)), true);
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4) {
				
					if(xmlhttp.responseText=='fail')
					{
						document.getElementById(id).style.border="2px solid #ACACAC";
						document.getElementById('error_'+id).innerHTML="";
						document.getElementById('error_'+id).style.height="1px";
						
						if(form_with_error!=0)
							form_with_error=form_with_error-1;
					}
					else if(xmlhttp.responseText=='true')
					{
						document.getElementById(id).style.border="2px solid #FF9900";
						document.getElementById('error_'+id).style.color="#FFDBA6";
						document.getElementById('error_'+id).style.height="14px";
						document.getElementById('error_'+id).innerHTML="Sorry, but link has already been added to site, try different one.";
						form_with_error=form_with_error+1;
					}
					else if(xmlhttp.responseText=='Invalid')
					{
						document.getElementById(id).style.border="2px solid #FF9900";
						document.getElementById('error_'+id).style.color="#FFDBA6";
						document.getElementById('error_'+id).style.height="14px";
						document.getElementById('error_'+id).innerHTML="Invalid Link, Please try a different one";
						form_with_error=form_with_error+1;
					}
				}
			}
			xmlhttp.send(null);
}

function checkLinkParts(link,id,type)
{	
		var xmlhttp = CreateAjaxObject();
			if(type=='mirror')
				xmlhttp.open("GET", "/check-link-mirror/"+encodeURI(trim(link)), true);
			else
				xmlhttp.open("GET", "/check-link-downlaod/"+encodeURI(trim(link)), true);
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4) {
				
					if(xmlhttp.responseText=='fail')
					{
						document.getElementById(id).style.border="2px solid #ACACAC";
						document.getElementById('error_'+id).innerHTML="";
						document.getElementById('error_'+id).style.height="1px";
						
						if(form_with_error!=0)
							form_with_error=form_with_error-1;
					}
					else if(xmlhttp.responseText=='true')
					{
						document.getElementById(id).style.border="2px solid #FF9900";
						document.getElementById('error_'+id).style.color="#FFDBA6";
						document.getElementById('error_'+id).style.height="14px";
						document.getElementById('error_'+id).innerHTML="Sorry, but link has already been added to site, try different one.";
						form_with_error=form_with_error+1;
					}
					else if(xmlhttp.responseText=='Invalid')
					{
						document.getElementById(id).style.border="2px solid #FF9900";
						document.getElementById('error_'+id).style.color="#FFDBA6";
						document.getElementById('error_'+id).style.height="14px";
						document.getElementById('error_'+id).innerHTML="Invalid Link, Please try a different one";
						form_with_error=form_with_error+1;
					}
				}
			}
			xmlhttp.send(null);
}
/**
*
*  Javascript trim, ltrim, rtrim
*  http://www.webtoolkit.info/
*
**/
 
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}