// COOKIE functions
// New stuff
  function getCookie (name) 
  {  
    var arg = name + "=";  
    var alen = arg.length;  
    var clen = document.cookie.length;  
    var i = 0;  
    while (i < clen) 
    {    
      var j = i + alen;    
      if (document.cookie.substring(i, j) == arg)      
        return getCookieVal (j);    
      i = document.cookie.indexOf(" ", i) + 1;    
      if (i == 0) break;   
    }    
    return -1;
  }
  function getCookieVal (offset) 
  {  
    var endstr = document.cookie.indexOf (";", offset);  
    if (endstr == -1)    
      endstr = document.cookie.length;  
    return unescape(document.cookie.substring(offset, endstr));
  }
  function setCookie(NameOfCookie, value, expirehours) 
  { 
    var ExpireDate = new Date (); 
    ExpireDate.setTime(ExpireDate.getTime() + (expirehours * 3600 * 1000)); 
    document.cookie = NameOfCookie + "=" + escape(value) + ((expirehours == null) ? "" : "; expires=" + ExpireDate.toGMTString()) + "; path=/;"; 
  }
  function setDomainCookie(NameOfCookie, value, expirehours) 
  { 
    var ExpireDate = new Date (); 
    ExpireDate.setTime(ExpireDate.getTime() + (expirehours * 3600 * 1000)); 
    document.cookie = NameOfCookie + "=" + escape(value) + ((expirehours == null) ? "" : "; expires=" + ExpireDate.toGMTString()) + "; path=/; domain=.fanficauthors.net;"; 
  }
  function deleteCookie(NameOfCookie)
  {
    setCookie(NameOfCookie,'',-1);
  }
// Page load functions to set heights of various elements	
  function loadStyle()
  {
    if(getCookie('myfont')!='' && getCookie('myfont')!=-1) 
    {
      myfont=getCookie('myfont');
      chngFont(myfont);
    }
    if(getCookie('mysize')!='' && getCookie('mysize')!=-1) 
    {
      mysize=getCookie('mysize');
      chngSize(mysize);
    }
    checkHeight();
  }
  function checkHeight()
  {
    if (document.getElementById('quicklinks'))
    {
      var elHeight=document.getElementById('quicklinks').offsetHeight;
      var mlHeight=document.getElementById('main').offsetHeight;
      elHeight+=20;
      if (mlHeight<elHeight)
      {
        document.getElementById('main').style.height=elHeight+'px';
      }
    }
  }
// AJAX functions
// GLOBAL variables
  var url;
  var elid;
  var what;
  function handleHttpResponse()
  {
    if (xmlhttp.readyState == 4)
    {
      if (xmlhttp.status==200)
      {
        var response=xmlhttp.responseText;
				write_status(response);
      }
    }
  }
  function getresponse(url,elwhat,msg)
  {
    if (window.XMLHttpRequest) // code for Mozilla, etc.
    {
      xmlhttp=new XMLHttpRequest();
      xmlhttp.onreadystatechange=handleHttpResponse;
      write_status(' '+msg+' '+elwhat+'...');
      xmlhttp.open("GET",url,true);
      xmlhttp.send(null);
    }
    else if (window.ActiveXObject) // code for IE
    {
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      if (xmlhttp)
      {
        xmlhttp.onreadystatechange=handleHttpResponse;
        write_status(' '+msg+' '+elwhat+'...');
        xmlhttp.open("GET",url,true);
        xmlhttp.send();
      }
    }
  }
  function write_status(msg,id)
  {
    var eId=document.getElementById(elid);
    if(eId && (typeof eId.innerHTML).toString().toLowerCase()!='undefined')
    {
      eId.innerHTML=msg;
    }
  }
// generic write script
  function write_html(msg,id)
  {
    var eId=document.getElementById(id);
    if(eId && (typeof eId.innerHTML).toString().toLowerCase()!='undefined')
    {
      eId.innerHTML=msg;
    }
  }
  function checkfield(elwhat,id)
  {
    url='/scripts/check.php?t='+elwhat;
    var v=document.getElementById(elwhat).value;
    if (v!='')
    {
      url+='&v='+v;
      elid='message'+id;
      what=elwhat;
      getresponse(url,elwhat,'checking');
    }
  }
  function changescore(elwhat,id,eid)
  {
    url='/admin/reviewscore.php?t='+eid+'&r='+id;
    var dd=document.getElementById(elwhat);
    var val=dd.options[dd.selectedIndex].value;
    if (val!='' && val!=0)
    {
      url+='&v='+val;
      elid='r'+id;
      getresponse(url,elwhat,'Updating score with ');
    }

  }
// Add page function
  function checkPass()
  {
    if (document.updateform.pass.value=='')
    {
      alert("You MUST enter your password to make any changes!");
      document.updateform.pass.focus();
      return false;
    }
    else
      return true;
  }
// Hide email decrypt function
  function pemail(ints,name)
  {
    var proEmail='';
    if (ints.length>0)
    {
      for (i=0;i<ints.length;i++)
        proEmail+=String.fromCharCode(ints[i])
      document.write('<a href="mailto:'+proEmail+'">'+name+'</a>');
      proEmail='';
    }
  }
// Tooltip mouseover

  var shown = false;
  function mouseShow(theDiv)
  {
    shown = (shown) ? false : true;
    document.getElementById(theDiv).style.display = (shown) ? 'block' : 'none';
  }

// site wide story functions 
  function chngFont(newValue) 
  {
    if(newValue!='' && newValue!='clear') 
    {
      document.getElementById('main').style.fontFamily = newValue;
      setCookie('myfont',newValue,3600);
    }
    else if(newValue=='clear')
    {
      deleteCookie('myfont');
      alert('Font Style cleared - Reload the page to clear all styles');
    }
  }
  function chngSize(newValue)
  {
    if(newValue!='' && newValue!='clear')
    {
      document.getElementById('main').style.fontSize = newValue + 'pt';
      setCookie('mysize',newValue,3600);
    }
    else if(newValue=='clear')
    {
      deleteCookie('mysize');
      alert('Font Size cleared - Reload the page to clear all styles');
    }
  }
  function chngOrder(newValue,cookieName)
  {
	  if (typeof(cookieName) == "undefined" || cookieName.length ==0)
			cookieName = 'sort';
    if(newValue!='' && newValue!='')
    {
      setCookie(cookieName,newValue,3600);
      document.location.reload();
    }
  }
  function chngOrderMain(newValue,act,stid)
  {
    if(newValue!='' && newValue!='')
    {
      setCookie('sort',newValue,3600);
      document.location='index.php?stid='+stid+'&act='+act;
    }
  }
  var fonts = new Array('Palatino Linotype','Arial','Times New Roman','Verdana','Georgia','Serif','Sans-serif', 'Comic Sans MS');
  var i;
  function showStory()
  {
    var html='<div style="text-align: right;"><form>Change<select name="size" size="1" onchange="chngFont(this.options[this.selectedIndex].value)"><option value="">Font</option>';
    for (i=0;i<fonts.length;i++)
    {
      html+='<option value="'+fonts[i]+'"';
      if (getCookie('myfont')==fonts[i])
        html+=' selected';
      html+='>'+fonts[i]+'</option>';
    }
    html+='<option value="clear">Clear font</select>';
	  html+=' <select name="size" size="1" onchange="chngSize(this.options[this.selectedIndex].value)"><option value="">Size</option>';
    for (i=8;i<21;i=i+2)
    {
      html+='<option value="'+i+'"';
      if (getCookie('mysize')==i)
        html+=' selected';
      html+='>'+i+'pt</option>';
    }												
    html+='<option value="clear">Clear</select></select></div></form>';
    return html;   
  }
  var totalh;
	function dWidth()
	{
    if (getCookie('mywidth')>0)
      return getCookie('mywidth');
    var toolbars=0;
    if (!document.all)
    {
			total = window.innerWidth;
			totalh = window.innerHeight;
      toolbars=14;
		  total=total-80-toolbars;
    }
		else
    {
			total = document.body.offsetWidth;
			totalh = document.body.offsetHeight;
      toolbars=7; 
		  total=total-50-toolbars;
    }
	  return total;	
	}
  var startpos,starthpos,new_width,main_width,diffpos=0;
  var live = false;
  var msg;
  var total;
	function Position(mEvent)
	{
    if (!document.all)
   	{
      startpos = mEvent.screenX;
      starthpos = mEvent.screenY;
    }
		else
    {
   		startpos = event.clientX;
   		starthpos = event.clientY;
    }
		main_width=document.getElementById("main").style.width;
		main_width=parseInt(main_width.replace('px',''));
		diffpos=main_width-startpos;
		live = true;
    return false;
	}

	function stop_move(mEvent)
	{
		live = false;
    if (new_width>0)
    {
      if (new_width<300)
      {
        deleteCookie('mywidth');
        document.location='color.php?nn=cww';
      }
      else
        setDomainCookie('mywidth',new_width,3600);
    
    }
    document.getElementById("movediv").style.backgroundColor = "#ffffff";
   	write_html('<img src="scripts/resize.gif">','movediv');
		return false;
	}

	function start_move(mEvent)
	{
		if (live)
   	{
      if (!document.all)
         new_position = mEvent.screenX;
      else
         new_position = event.clientX;
			new_width=new_position+diffpos;
      if (new_width>100)
      {
     	  document.getElementById("main").style.width = new_width + "px";
     	  document.getElementById("movediv").style.backgroundColor = "#c0c0c0";
   	    write_html(new_width,'movediv');
      }
      else
        live=false;
    }
	}
	function setevent()
	{
	  total=dWidth()+'px';
		document.getElementById("main").style.width=total;
		document.getElementById("movediv").onmousedown = Position;
		document.onmouseup = stop_move;
		document.onmousemove = start_move;
	}
	function pop(sid)
	{
    if (sid>0)
		{
			var narray=eval('chapter'+sid);
			var narra2=eval('cid'+sid);
			var eSelect=document.getElementById('chapter');
			eSelect.options.length = 0;
		  addElements(eSelect,'Please choose a chapter','null');
    	for (i=0;i<narray.length;i++)
			{
			  addElements(eSelect,narray[i],narra2[i]);
			}																						
		}
	}
  function addElements(eSelect,eText,eValue)
	{
    var elOptNew = document.createElement('option');
    elOptNew.text = eText;
    elOptNew.value = eValue;
		try
    {
			eSelect.add(elOptNew, null); // standards compliant; doesn't work in IE
		}
		catch(ex)
		{
			eSelect.add(elOptNew); // IE only
		}
																														
	}
	function setSelect(sid,cid)
	{
    var eSelect=document.getElementById('chapter');
		var bSelect=document.getElementById('story');
		for (i=0;i<eSelect.options.length;i++)
		  if (eSelect.options[i].value==cid)
        eSelect.options[i].selected=true;
		for (i=0;i<bSelect.options.length;i++)
		  if (bSelect.options[i].value==sid)
        bSelect.options[i].selected=true;
	}
	function goLook(cid)
	{
		var sid=document.getElementById('story').options[document.getElementById('story').selectedIndex].value;
		document.location='reviewreply.php?cid='+cid+'&sid='+sid;
	}
