var searchString;

function confirmDelete() {
	if(!confirm("Confirm delete?")) {
		return false;
	}
}

function bookmarkPage() {

	if (window.sidebar) { 
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) {
		window.external.AddFavorite( url, title); }
	else if(window.opera && window.print) {
		return true; }
 }
 
 function submitVote(pid,vote,id) {
 	
 	ajaxNow("pid="+pid+"&vote="+vote+"&id="+id);
 
 }
 
 function ajaxSearch(query) {
 	var pretext='Search our site...';
 	if( (query!='') && (query!=pretext) ) {
	 	document.getElementById('search_button').disabled=true;
	 	document.getElementById('search_rbox').style.display='block';
	 	document.getElementById('searching').style.display='block';
	 	document.getElementById('search_results').innerHTML='';
	 	ajaxSearchF('q='+query,1);
	 	searchString=query;
 	} else {
 		alert("You didn't enter a search term.");
 	}
 	
 }
 
 function searchPage(page) {
 
 	ajaxSearchF('q='+searchString,page);
 
 }
 
 function searchBox(contents) {
 	var clength=contents.length;
 	if( (clength<1) || (contents==null) ) {
 		document.getElementById('search_button').disabled=true;
 	} else {
 		document.getElementById('search_button').disabled=false;
 	}
 }
 
function searchEnter(e){ //e is event object passed from function invocation
var characterCode;
if(e && e.which){ //if which property of event object is supported (NN4)
e = e
characterCode = e.which //character code is contained in NN4's which property
}
else{
e = event
characterCode = e.keyCode //character code is contained in IE's keyCode property
}
if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
	if(!document.getElementById('search_button').disabled) {
		document.getElementById('search_button').click();
	}
}

}
 
 function searchClick(contents,from) {
 	var pretext='Search our site...';
 	if(contents==pretext) {
 		document.getElementById('q').value='';
 		document.getElementById('search_button').disabled=true;
 		document.getElementById('q').className='searchq';
 	}
  	if( (contents=='') && (from=='blur') ) {
 		document.getElementById('q').value=pretext;
 		document.getElementById('search_button').disabled=false;
 		document.getElementById('q').className='searchq search_grey';
 	}
 }
 
 function substr( f_string, f_start, f_length ) {
    // http://kevin.vanzonneveld.net
    // +     original by: Martijn Wieringa
    // +     bugfixed by: T.Wild
    // +      tweaked by: Onno Marsman
    // *       example 1: substr('abcdef', 0, -1);
    // *       returns 1: 'abcde'
    // *       example 2: substr(2, 0, -6);
    // *       returns 2: ''
 
    f_string += '';
 
    if(f_start < 0) {
        f_start += f_string.length;
    }
 
    if(f_length == undefined) {
        f_length = f_string.length;
    } else if(f_length < 0){
        f_length += f_string.length;
    } else {
        f_length += f_start;
    }
 
    if(f_length < f_start) {
        f_length = f_start;
    }
 
    return f_string.substring(f_start, f_length);
}

function ajaxNow(params)
{
  var xmlHttp;
  try
  {
    xmlHttp=new XMLHttpRequest();
  }
  catch (e)
  {
    try
    {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      try
      {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        alert("Your browser is old and does not have AJAX support!");
        return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
  {
    if(xmlHttp.readyState==4)
    {
      var response = xmlHttp.responseText;
      	if (substr(response,0,7) == 'success') {
      		var new_rating = substr(response,8);
      		document.getElementById('ratingdata').innerHTML=new_rating;
      		document.getElementById('ratingspan').className='rating_submitted';
      		document.getElementById('ratingspan').innerHTML='Rating Submitted!';
      	} else if(response=='fail') {
      		document.getElementById('ratingspan').className='rating_failed';
      		document.getElementById('ratingspan').innerHTML='Error Rating';
      	} else if(response=='dupe') {
      		document.getElementById('ratingspan').className='rating_submitted';
      		document.getElementById('ratingspan').innerHTML='Already Rated';
      	}

    }
  }
  xmlHttp.open("POST",site_dir+"action/vote.php",true);
  xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlHttp.setRequestHeader("Content-length", params.length);
  xmlHttp.setRequestHeader("Connection", "close");
  xmlHttp.send(params);
}

function ajaxSearchF(params,page)
{
  var xmlHttp;
  try
  {
    xmlHttp=new XMLHttpRequest();
  }
  catch (e)
  {
    try
    {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      try
      {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        alert("Your browser is old and does not have AJAX support!");
        return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
  {
    if(xmlHttp.readyState==4)
    {
      var response = xmlHttp.responseText;
	document.getElementById('searching').style.display='none';
	
	if (substr(response,0,3) == 'ok:') {
      		var html = substr(response,3);
		document.getElementById('search_results').innerHTML=html;
	} else {
		alert(response);
		document.getElementById('search_results').innerHTML='There was an error.';
	}
	document.getElementById('search_button').disabled=false;

    }
  }
  xmlHttp.open("POST",site_dir+"action/search.php",true);
  xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlHttp.setRequestHeader("Content-length", params.length);
  xmlHttp.setRequestHeader("Connection", "close");
  params=params+'&pg='+page;
  xmlHttp.send(params);
}

function npcAjax(params)
{
  var xmlHttp;
  try
  {
    xmlHttp=new XMLHttpRequest();
  }
  catch (e)
  {
    try
    {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      try
      {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        alert("Your browser is old and does not have AJAX support!");
        return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
  {
    if(xmlHttp.readyState==4)
    {
      var response = xmlHttp.responseText;
      if(response=='fail') {
      	document.getElementById('npcsearchresults').innerHTML='<div class="npc_sboxr">Error Searching</div>';
      } else if(response=='') {
      	document.getElementById('npcsearchresults').innerHTML='<div class="npc_sboxr">No Results</div>';
      } else {
      var npcresults = response.split('\n');
      var storenpcsr='';
      for(i in npcresults) {
      	var thisnpc=npcresults[i].split('|');
      	storenpcsr=storenpcsr+'<div class="npc_sboxr"><a href="'+thisnpc[0]+'.html">'+thisnpc[1]+'</a></div>';
      }
      document.getElementById('npcrbox').style.display='block';
	document.getElementById('npcsearchresults').innerHTML=storenpcsr;
	}
    }
  }
  xmlHttp.open("POST",site_dir+"action/npcsearch.php",true);
  xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlHttp.setRequestHeader("Content-length", params.length);
  xmlHttp.setRequestHeader("Connection", "close");
  xmlHttp.send(params);
}

var NPCtimeidM = 0;

function carrynpcsearchM() {
	npcAjaxM('query='+document.getElementById('npcsearchvalue').value);
}

 function searchNPCBoxM(contents) {
 	var clength=contents.length;
 	if( (clength<1) || (contents==null) ) {
 		document.getElementById('npcrbox').style.display='none';
 		clearTimeout(NPCtimeidM);
 	} else {
 		clearTimeout(NPCtimeid);
 		NPCtimeidM = setTimeout ( "carrynpcsearchM()", 500 );
 	}
 }
function npcAjaxM(params)
{
  var xmlHttp;
  try
  {
    xmlHttp=new XMLHttpRequest();
  }
  catch (e)
  {
    try
    {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      try
      {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        alert("Your browser is old and does not have AJAX support!");
        return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
  {
    if(xmlHttp.readyState==4)
    {
      var response = xmlHttp.responseText;
      document.getElementById('npcrbox').style.display='block';
      if(response=='fail') {
      	document.getElementById('npcrbox').innerHTML='<div class="npc_sboxrm">Error Searching</div>';
      } else if(response=='') {
      	document.getElementById('npcrbox').innerHTML='<div class="npc_sboxrm">No Results</div>';
      } else {
      var npcresults = response.split('\n');
      var storenpcsr='<div style="float:left;width:48%;">';
      var npcfloat='left';
      var npccount=1;
      for(i in npcresults) {
      	if( (npccount>5) && (npcfloat!=='right') ){
      		npcfloat=='right';
      		storenpcsr=storenpcsr+'</div><div style="float:right;width:48%;">';
      	}
      	var thisnpc=npcresults[i].split('|');
      	storenpcsr=storenpcsr+'<div class="npc_sboxrm"><a href="'+site_dir+'npc/'+thisnpc[0]+'.html">'+thisnpc[1]+'</a></div>';
      	npccount++;
      }
      storenpcsr=storenpcsr+'</div><div class=\"clearBoth\"></div>';
	document.getElementById('npcrbox').innerHTML=storenpcsr;
	}
    }
  }
  xmlHttp.open("POST",site_dir+"action/npcsearch.php",true);
  xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlHttp.setRequestHeader("Content-length", params.length);
  xmlHttp.setRequestHeader("Connection", "close");
  xmlHttp.send(params);
}

function newFolder() {
	var album_name=prompt("Folder Name:","");
	if (album_name!=null) {
		document.newfolderform.new_folder.value=album_name;
		document.newfolderform.submit();
	}
}

var NPCtimeid = 0;
var NPCtimeido = 0;

function carrynpcsearch() {
	npcAjax('query='+document.getElementById('npcsearchvalue').value);
}

function closenpcresults() {
	document.getElementById('npcrbox').style.display='none';
}

function offselnpcs() {
	NPCtimeido = setTimeout ( "closenpcresults()", 3000 );
}

function npcsregactive(contents) {
	clearTimeout(NPCtimeido);
	var clength=contents.length;
 	if( (clength<1) || (contents==null) ) {
 		
 	} else {
 		document.getElementById('npcrbox').style.display='block';
 	}
}
function npcsregactive2() {
	clearTimeout(NPCtimeido);
}

 function searchNPCBox(contents) {
 	var clength=contents.length;
 	if( (clength<1) || (contents==null) ) {
 		document.getElementById('npcrbox').style.display='none';
 	} else {
 		clearTimeout(NPCtimeid);
 		NPCtimeid = setTimeout ( "carrynpcsearch()", 800 );
 	}
 }

function deleteSelected(faction) {
	f=document.selectform;
	var t=0;
	var c=f['selectlist[]'];
	for(var i=0;i<c.length;i++){
	c[i].checked?t++:null;
	}
	if(t>0) {
		if(confirm("Confirm delete?")) {
			document.selectform.selecttype.value='delete';
			document.selectform.action = faction;
			document.selectform.submit();
		}
	} else {
		alert("Nothing selected");
	}
}

function renameSelected(faction) {
	f=document.selectform;
	var t=0;
	var c=f['selectlist[]'];
	for(var i=0;i<c.length;i++){
	c[i].checked?t++:null;
	}
	if(t==1) {
			var newname=prompt("New Name?",c[1].value);
			if( (newname!=c[1].value) && (newname!=null) ) {
				document.selectform.selecttype.value='rename';
				document.selectform.txtdata.value=newname;
				document.selectform.action = faction;
				document.selectform.submit();
			}
	} else {
		alert("Select only one");
	}
}

var cIndexSel;
function indexPSel(link,id) {
	if(cIndexSel!=null) {
		document.getElementById(cIndexSel).className='pollopt';
	}
	link.className='pollopt pollsel';
	cIndexSel=link.id;
	document.getElementById('v'+id).click();
}


var pscrolldir;
function setpscroll(dir) {
	pscrolldir=dir;
}

function imgregen(go) {
	
	if(confirm("Generate image. Are you sure?")) {
		
		window.location.href=go;
		
	}
	
}

var smmagnify=2;
var smmagnified=0;
function zoomSelMap(dir) {
	
	var smheight=document.getElementById('selmap').height;
	var smwidth=document.getElementById('selmap').width;
	if(dir) {
		document.getElementById('selmap').height=(smheight*smmagnify);
		document.getElementById('selmap').width=(smwidth*smmagnify);
		smmagnified=smmagnified+1;
		document.getElementById('imgmapwrap').scrollLeft=(document.getElementById('imgmapwrap').scrollLeft*smmagnify)+450;
		document.getElementById('imgmapwrap').scrollTop=(document.getElementById('imgmapwrap').scrollTop*smmagnify)+300;
	} else if(!dir) {
		document.getElementById('imgmapwrap').scrollLeft=((document.getElementById('imgmapwrap').scrollLeft-450)/smmagnify);
		document.getElementById('imgmapwrap').scrollTop=((document.getElementById('imgmapwrap').scrollTop-300)/smmagnify);
		document.getElementById('selmap').height=(smheight/smmagnify);
		document.getElementById('selmap').width=(smwidth/smmagnify);
		smmagnified=smmagnified-1;
	}
	
}

function selectcoord(x,y,mid) {
	setCookie('mapselcoords',x+','+y,365);
	opener.document.npcform.x.value = x;
	opener.document.npcform.y.value = y;
	opener.document.npcform.map_id.value = mid;
	window.close();
}

function mapSelect() {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(site_dir+'action/map/coordselect.php', '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=900,height=600,left = 240,top = 84');");
}

function viewMap(mid,x,y,name) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(site_dir+'action/map/view.php?mid='+mid+'&x='+x+'&y='+y+'&name='+name, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=1,menubar=0,resizable=0,width=516,height=602,left = 240,top = 84');");
}

function createCoordJS(mid,x,y) {
	var clabel=prompt('Label');
	alert("javascript:viewMap("+mid+","+x+","+y+",'"+clabel+"');");
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}