
nn=(document.layers)?true:false;
ie=(document.all)?true:false;

function fViewProfileGlobal(pProfileId)
  {
   oScreenWidth = 600;
   oScreenHeight = 400;
   if(screen.height)
     {
     oScreenWidth = screen.width * (0.8);
     oScreenHeight = screen.height * (0.8);
     }

   launchWindow("http://www.cryptozoology.com/profile/profile_view.php?id=" + pProfileId, "profile", oScreenWidth, oScreenHeight, "yes");
  }


function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}


function myParseInt(pValue)
  {
  if(pValue == '')
    return 0;
  return parseInt(pValue);
  }

  
function StringtoXML(text)
  {
  if (window.ActiveXObject)
	  {
    var doc=new ActiveXObject('Microsoft.XMLDOM');
    doc.async='false';
    doc.loadXML(text);
    } 
	else 
	  {
    var parser=new DOMParser();
    var doc=parser.parseFromString(text,'text/xml');
    }
  return doc;
  }
  
function div_exists(pId)
  {
  var divs = document.getElementsByTagName('div');
  for(i=0;i<divs.length;i++)
	  {
    if( divs[i].getAttribute('id') == pId )
		  return true;
		}
	return false;  
	}  

  
function xmlhttpPost(strURL, fnPtr, params) {
    var xmlHttpReq = '';
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlHttpReq.open('POST', strURL, true);
    xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttpReq.onreadystatechange = function() {
        if (xmlHttpReq.readyState == 4) {
            if(fnPtr != null)
              fnPtr(xmlHttpReq.responseXML);
        }
    }
    xmlHttpReq.send(params);
    }  
  



function getRequestAttributeFromXml(pXml)
  {
  return  pXml.getElementsByTagName("service")[0].getAttribute("request");
  }

function printProp(po) {
  for (property in po) {
    alert(property+': ' + po[property]);
  } }

function fGetLoggedInMemberName()
  {
  return oLoggedInMemberName;
  }

function fGetLoggedInMemberId()
  {
  return oLoggedInMemberId;
  }   

function fGetLoggedInIp()
  {
  return oLoggedInIp;
  }  
  
function fSetHeaderLoginLink()
  {
  // set the proper login info in the header.
  fSetHeaderLoginState();
  }  



function fMakeXmlSafe(pText)
  {
  pText = str_replace('&','_amp_',pText);
  pText = str_replace("'",'_sq_',pText);
  pText = str_replace('"','_dq_',pText);
  pText = str_replace('=','_eq_',pText);
  pText = str_replace('<','',pText);
  pText = str_replace('>','',pText);
  return pText;
	}  	

var oLoginInProcess = false;
function fLogin()
  {
  oLoginInProcess = true;
  var login_name = window.document.getElementById('login_name').value;
  var login_password = window.document.getElementById('login_password').value;
  var remember = window.document.getElementById('login_remember').checked;

  if(login_name != '' && login_password != '')
    {
    window.document.getElementById('div_login_form_form').style.display = 'none';
    window.document.getElementById('div_login_form_processing').style.display = '';
		login_name = fMakeXmlSafe(login_name);
		login_password = fMakeXmlSafe(login_password);
    var xmlString = '<service request="login">';
    xmlString    += '  <login>' + login_name + '</login>';
    xmlString    += '  <password>' + login_password + '</password>';
    xmlString    += '  <remember>' + remember + '</remember>';
    xmlString    += '</service>';
    //alert('http://www.cryptozoology.com/service.php?xml=' + (xmlString));
    xmlhttpPost('http://www.cryptozoology.com/service.php', fProcessLoginXml, "xml=" + escape(xmlString) );
    }
  }

function fProcessLoginXml(xml)
  {
  oLoginInProcess = false;
  window.document.getElementById('div_login_form_form').style.display = '';
  window.document.getElementById('div_login_form_processing').style.display = 'none';
  
  var result = xml.getElementsByTagName("result")[0].childNodes[0].nodeValue;
  if(result == 'success')
    {
    var member_id = xml.getElementsByTagName("member_id")[0].childNodes[0].nodeValue;
    var member_name = xml.getElementsByTagName("member_name")[0].childNodes[0].nodeValue;
    var num_unread_messages = xml.getElementsByTagName("unread_messages")[0].childNodes[0].nodeValue;
    var num_unread_forum_replies = xml.getElementsByTagName("unread_forum_replies")[0].childNodes[0].nodeValue;
    num_unread_messages = myParseInt(num_unread_messages);
    num_unread_forum_replies = myParseInt(num_unread_forum_replies);
    oLoggedInMemberId = member_id;
    oLoggedInMemberName = member_name;    
    fShowUnreadNotice(num_unread_messages, num_unread_forum_replies);
    window.document.getElementById('rememberFrame').src = 'http://www.cryptozoology.com/remember.php?cb=' + Math.round(Math.random*10000);
    fSetHeaderLoginState();
    fShowLoginSuccess();
    }
  if(result == 'fail')
    {
    var message = xml.getElementsByTagName("message")[0].childNodes[0].nodeValue;
    fShowLoginFailure(message);
    }
  }
  
function fCheckUnread()
  {
  if(oLoggedInMemberId > 0)
    {
    var xmlString = '<service request="check_unread">';
    xmlString    += '</service>';
    xmlhttpPost('http://www.cryptozoology.com/service.php', fProcessCheckUnreadXml, "xml=" + escape(xmlString) );
    }
  else
    {
    fShowUnreadNotice(0,0);
    }
	}  
	
function fProcessCheckUnreadXml(xml)
  {
  var result = xml.getElementsByTagName("result")[0].childNodes[0].nodeValue;
  if(result == 'success')
    {
    var num_unread_messages = xml.getElementsByTagName("unread_messages")[0].childNodes[0].nodeValue;
    var num_unread_forum_replies = xml.getElementsByTagName("unread_forum_replies")[0].childNodes[0].nodeValue;
    num_unread_messages = myParseInt(num_unread_messages);
    num_unread_forum_replies = myParseInt(num_unread_forum_replies);
    fShowUnreadNotice(num_unread_messages, num_unread_forum_replies);
    }
  }
  
function fShowUnreadNotice(pNumMessages, pNumForumReplies)
  {
  if(pNumMessages == 0 && pNumForumReplies == 0)
    {
    window.document.getElementById('div_unread_stuff').style.display = 'none';
    return;
    }
  window.document.getElementById('div_unread_stuff').style.display = 'block';
  if(pNumMessages > 0)
    {
	  var inner_html = 'You have <span style="font-weight: bold; font-size: 9pt;">' + pNumMessages + '</span> unread ';
		if(pNumMessages == 1)
		  inner_html += ' message';
		else
		  inner_html += ' messages';
		window.document.getElementById('div_unread_messages').innerHTML = inner_html;
	  }
  if(pNumForumReplies > 0)
    {
	  var inner_html = 'You have <span style="font-weight: bold; font-size: 9pt;">' + pNumForumReplies + '</span> unread forum ';
		if(pNumForumReplies == 1)
		  inner_html += ' reply';
		else
		  inner_html += ' replies';
		window.document.getElementById('div_unread_forum_replies').innerHTML = inner_html;
	  }

  }
  
function fUpdateLogin()
  {
  if(fGetLoggedInMemberId() > 0)
    {
    var xmlString = '<service request="update_login">';
    xmlString    += '  <id>' + fGetLoggedInMemberId() + '</id>';
    xmlString    += '</service>';
    //alert('http://www.cryptozoology.com/service.php?xml=' + (xmlString));
    xmlhttpPost('http://www.cryptozoology.com/service.php', fProcessUpdateLogin, "xml=" + escape(xmlString) );
    }
  }
function fProcessUpdateLogin(xml)
  {
  }
  


function fLogout()
  {
  //alert('fLogout');
  window.document.getElementById('login_name').value = '';
  window.document.getElementById('login_password').value = '';

  var xmlString = '<service request="logout">';
  xmlString    += '  <id>' + fGetLoggedInMemberId() + '</id>';
  xmlString    += '  <login>' + fGetLoggedInMemberName() + '</login>';
  xmlString    += '</service>';
  //alert('http://www.cryptozoology.com/service.php?xml=' + (xmlString));
  xmlhttpPost('http://www.cryptozoology.com/service.php', fProcessLogoutXml, "xml=" + escape(xmlString) );

  }

function fProcessLogoutXml(xml)
  {
  var result = xml.getElementsByTagName("result")[0].childNodes[0].nodeValue;
  if(result == 'success')
    {
    oLoggedInMemberId = 0;
    oLoggedInMemberName = '';  
    fShowUnreadNotice(0,0);
    }
  if(result == 'fail')
    {
    // there should be no fail. But, just in case, deal with a fail result
    oLoggedInMemberId = 0;
    oLoggedInMemberName = '';
    }
  window.document.getElementById('rememberFrame').src = 'http://www.cryptozoology.com/remember.php?cb=' + Math.round(Math.random*10000);
  fSetHeaderLoginState();  
	if( fGetLoggedInMemberName() == '' && (window.document.location == 'http://www.cryptozoology.com/profile/' || 
	                                       window.document.location == 'http://www.cryptozoology.com/profile/index.php' || 
																				 window.document.location == "http://www.cryptozoology.com/profile/profile_pictures_create_process.php" || 
																				 window.document.location == "http://www.cryptozoology.com/profile/profile_pictures_edit.php" || 
																				 window.document.location == "http://www.cryptozoology.com/profile/profile_edit.php"  || 
																				 window.document.location == "http://www.cryptozoology.com/profile/profile_edit_process.php") )
	  window.document.location =  'http://www.cryptozoology.com/profile/profile_login.php';  
  }
  
function fClearLoginArea()
  {  
	window.document.getElementById('div_login_form_processing').style.display = 'none';
  window.document.getElementById('div_login_form').style.display = 'none';
  window.document.getElementById('div_login_message').style.display = 'none';
	}  

 
  
function fShowLoginSuccess()
  {
  window.document.getElementById('div_login_form_processing').style.display = 'none';
  window.document.getElementById('div_login_form_form').style.display = 'none';
  window.document.getElementById('div_login_message').style.display = '';
  window.document.getElementById('div_login_text').innerHTML = 'success';
  setTimeout(fCloseLoginDiv, 2000);
	}  
	
function fCloseLoginDiv()
  {
  if(div_exists('div_login_form'))
    window.document.getElementById('div_login_form').style.display = 'none';
  if(div_exists('div_login_form_form'))
    window.document.getElementById('div_login_form_form').style.display = '';
  if(div_exists('div_login_form_processing'))
    window.document.getElementById('div_login_form_processing').style.display = 'none';
  if(div_exists('div_login_message'))
    window.document.getElementById('div_login_message').style.display = 'none';
	if( fGetLoggedInMemberName() != '' && window.document.location == 'http://www.cryptozoology.com/profile/profile_login.php')
	  window.document.location =  'http://www.cryptozoology.com/profile/';
	}	
  
function fShowLoginFailure(pMessage)
  {
  window.document.getElementById('div_login_form_processing').style.display = 'none';
  window.document.getElementById('div_login_form_form').style.display = 'none';
  window.document.getElementById('div_login_message').style.display = '';
  window.document.getElementById('div_login_text').innerHTML = pMessage;
  setTimeout(fCloseMessageDiv, 2000);
	}  
  
function fCloseMessageDiv()
  {
  window.document.getElementById('div_login_form_form').style.display = '';
  window.document.getElementById('div_login_form_processing').style.display = 'none';
  window.document.getElementById('div_login_message').style.display = 'none';  
	}	
  


function fLoginLogout()
  {
  if(fGetLoggedInMemberName() == '')
    {
    fShowLoginForm();
    }
  else
    {
    fLogout();
    }
  }

function fRegisterProfile()
  {
  if(fGetLoggedInMemberName() == '')
    {
    window.document.location = 'http://www.cryptozoology.com/profile/profile_login.php';
    }
  else
    {
    window.document.location = 'http://www.cryptozoology.com/profile/';
    }
  }
  
function fShowLoginForm()
  {
  window.document.getElementById('div_login_form').style.display = '';
  window.document.getElementById('div_login_form_form').style.display = '';
  window.document.getElementById('div_login_form_processing').style.display = 'none';
  window.document.getElementById('div_login_message').style.display = 'none';    
  window.document.getElementById('login_name').focus();
  window.document.getElementById('login_remember').checked = false;
	}  
  
  
function fSetHeaderLoginState()
  {
	fInit();
	}  

function fInit()
  {
  if(oLoggedInMemberName != '')
    {
    window.document.getElementById('anchor_register_profile').innerHTML = 'Profile';
    window.document.getElementById('anchor_login_logout').innerHTML = 'Logout ' + oLoggedInMemberName;
    window.document.getElementById('div_header_facebook_button').style.visibility = 'hidden';
    window.document.getElementById('div_header_facebook_button').style.display = 'none';
    window.document.getElementById('div_browserid_button').style.visibility = 'hidden';
    window.document.getElementById('div_browserid_button').style.display = 'none';
    }
  else
    {
    window.document.getElementById('anchor_register_profile').innerHTML = 'Register';
    window.document.getElementById('anchor_login_logout').innerHTML = 'Login';
    window.document.getElementById('div_header_facebook_button').style.visibility = '';
    window.document.getElementById('div_header_facebook_button').style.display = '';
    window.document.getElementById('div_browserid_button').style.visibility = '';
    window.document.getElementById('div_browserid_button').style.display = '';
    }
  fUpdateMembersOnline();   
  fCheckUnread();
  setInterval("fCheckUnread()", 10000);
  
	}  
	
 function keyHandler(e)
   {
   evt = e || window.event;
   var keyPressed = evt.which || evt.keyCode;
	 if(keyPressed == 13)
	   {
     if(oCurrentFocus == 'form_search' && document.getElementById('form_search').value != '')
       fSearch2();
     if(oCurrentFocus == 'login_name' || oCurrentFocus == 'login_password')
       fLogin();
		 } 	
 }
 document.onkeypress = keyHandler;
 var oCurrentFocus = '';
 function fFocusSet(pId,pState)
   {
   if(pId != '' && pState == 'on')
     oCurrentFocus = pId;
   else
     oCurrentFocus = '';
   }
	
	
function fUpdateMembersOnlineTimer()
  {
  fUpdateMembersOnline();
	setTimeout(fUpdateMembersOnlineTimer, 10000); 
	}	
  
var oUpdateRequestPending = false;  
function fUpdateMembersOnline()
  {
  //alert('t1, oUpdateRequestPending:' + oUpdateRequestPending + ', oLoginInProcess: ' + oLoginInProcess);
  if(oUpdateRequestPending == true ||   oLoginInProcess == true)
    {
    return;
    }
  else
    {
    oUpdateRequestPending = true;
    }
  var xmlString = '<service request="members_online">';
  xmlString    += '  <time>' + parseInt(((new Date).getTime())/ 1000) + '</time>';
  xmlString    += '  <this_user>' + myParseInt(fGetLoggedInMemberId()) + '</this_user>';
  xmlString    += '  <this_user_ip>' + fGetLoggedInIp() + '</this_user_ip>';
  xmlString    += '</service>';
  //alert('http://www.cryptozoology.com/service.php?xml=' + (xmlString));
  xmlhttpPost('http://www.cryptozoology.com/service.php', fProcessMembersOnlineXml, "xml=" + escape(xmlString) );
  }

function fProcessMembersOnlineXml(xml)
  {
  //alert( 'fProcessMembersOnlineXml');
  //div_members_online
  oUpdateRequestPending = false;
  var result = xml.getElementsByTagName("result")[0].childNodes[0].nodeValue;
  if(result == 'success')
    {
    var membersOnline = xml.getElementsByTagName("members_online")[0].childNodes[0].nodeValue;
    //alert(membersOnline);
    membersOnline = str_replace('&lt;','<',membersOnline);
    membersOnline = str_replace('&gt;','>',membersOnline);
    if(membersOnline == '')
      membersOnline = '(none)';
		window.document.getElementById('div_members_online').innerHTML = '<span style="font-size: 11pt;">Members online:</span> ' + membersOnline;
    
    }
  if(result == 'fail')
    {
    var message = xml.getElementsByTagName("message")[0].childNodes[0].nodeValue;
		window.document.getElementById('div_members_online').innerHTML = '';
    }
  }
  
function str_replace (search, replace, subject, count) {
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }

    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}  		
		
  var IE = (navigator.userAgent.indexOf('MSIE') != -1);
function SetCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 window.document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString();
}  

// better cookie functions
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
  

			
function fCheckForDivInDocument(pName)
  {
  var nodeList = document.documentElement.getElementsByTagName( "div" );
  for(j=0; j<nodeList.length; j++)
    if(nodeList[j].id == pName)
      return true;
  return false;
	}		

function fChangeImage(pName,pType)
  {
  document.images[pName].src = pType;
  }
  
var launchWin;
function launchWindow(url, width, height, scrolling)
	{
	if(launchWin)
	  launchWin.close();
	screenX = (screen.width /2) - (width /2); // used to center.
	screenY = (screen.height /2) - (height /2); // used to center.
	  launchWin = window.open(url,"pop","toolbar=no,directories=no,menubar=no,location=yes,scrollbars=" +
          scrolling + ",resizable=yes,width=" + width + ",height=" + height + ",top=" + screenY +
          ",left=" + screenX + ",screenX=" + screenX + ",screenY=" + screenY);
	launchWin.focus();
	}  

var launchWin2;
function launchWindow2(url, width, height, scrolling)
	{
	if(launchWin2)
	  launchWin2.close();
	screenX = (screen.width /2) - (width /2); // used to center.
	screenY = (screen.height /2) - (height /2); // used to center.
	  launchWin2 = window.open(url,"pop2","toolbar=no,directories=no,menubar=no,location=yes,scrollbars=" +
          scrolling + ",resizable=yes,width=" + width + ",height=" + height + ",top=" + screenY +
          ",left=" + screenX + ",screenX=" + screenX + ",screenY=" + screenY);
	launchWin2.focus();
	}  

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(fInit);

function addResizeEvent(func) {
  var oldonresize = window.onresize;
  if (typeof window.onresize != 'function') {
    window.onresize = func; 
  } else {
    window.onresize = function() {
      if (oldonresize) {
        oldonresize();
      }
      func();
    }
  }
}
