// addLoadEvent written by Simon Willison (http://simonwillison.net/2004/May/26/addLoadEvent/)
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

/* Opens popup window by adding a class of "popup" to the element - Blake */
function doPopups() {
  if (!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
    if (links[i].className.match("popup")) {
      links[i].onclick = function() {
      // Next two lines should be on one line
        window.open(this.href,"popup","top=40,left=40,width=555,height=625,scrollbars=yes,resizable=yes");
        return false;
      }
    }
  }
}

/* Show/Hide Mail Messages */
function showHide(e,ajax_req) {
	var item = 'item' + e.substring(5);
	if (document.getElementById(e).style.display == 'none') {
		document.getElementById(e).style.display = 'block';
		document.getElementById(item).style.background = 'url(/_assets/images/icon_minus.gif) no-repeat 0 50%';
		if (ajax_req) makeRequest('/members/mail/message_read.php?mid='+ajax_req);
	} else {
		document.getElementById(e).style.display = 'none';
		document.getElementById(item).style.background = 'url(/_assets/images/icon_plus.gif) no-repeat 0 50%';
	}
}

/* Show/Hide Board Replies */
function toggleReplies(e) {
	var item = 'reply' + e.substring(5);
	if (document.getElementById(e).style.display == 'none') {
		document.getElementById(e).style.display = 'block';
		document.getElementById(item).innerHTML='close replies';
		document.getElementById(item).style.background = 'url(/_assets/images/arrow_green_u.gif) no-repeat 0 50%';
	} else {
		document.getElementById(e).style.display = 'none';
		document.getElementById(item).innerHTML='read replies';
		document.getElementById(item).style.background = 'url(/_assets/images/arrow_green_r.gif) no-repeat 0 50%';
	}
}

/* Show/Hide advanced search options */
function toggleSearchOptions() {
	//var item = 'reply' + e.substring(5);
	if (document.getElementById('other_options').style.display == 'none') {
		document.getElementById('other_options').style.display = 'block';
		document.getElementById('search_options').innerHTML='Hide other options';
	} else {
		document.getElementById('other_options').style.display = 'none';
		document.getElementById('search_options').innerHTML='Show other options';
	}
}

/* NoSpam email links */
function noSpam(user,domain) {
	locationstring = "mailto:" + user + "@" + domain;
	window.location = locationstring;
}

/* Form Field Hints */
function prepareInputsForHints() {
	var inputs = document.getElementsByTagName("input");
	for (var i=0; i<inputs.length; i++){
		// test to see if the hint span exists first
		if (inputs[i].parentNode.getElementsByTagName("span")[0]) {
			// the span exists!  on focus, show the hint
			inputs[i].onfocus = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			}
			// when the cursor moves away from the field, hide the hint
			inputs[i].onblur = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "none";
			}
		}
	}
	// repeat the same tests as above for selects
	var selects = document.getElementsByTagName("select");
	for (var k=0; k<selects.length; k++){
		if (selects[k].parentNode.getElementsByTagName("span")[0]) {
			selects[k].onfocus = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			}
			selects[k].onblur = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "none";
			}
		}
	}
}

function makeRequest(url) { // tells the server that a piece of mail was read, called from mail inbox pages
    http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!http_request) {
        //alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    http_request.onreadystatechange = alertContents;
    http_request.open('GET', url, true);
    http_request.send(null);

}

function alertContents() { // necessary for makeRequest, called from mail inbox pages, good for error checking
    if (http_request.readyState == 4) {
		// we sent an ajax request, and it came back.
        //if (http_request.status == 200) {
            //alert(http_request.responseText);
		//}
	}
}





/*-----------------------------------------------------------------------------
	Standards Compliant Rollover Script
	Author : Daniel Nolan
	http://www.bleedingego.co.uk/webdev.php
-----------------------------------------------------------------------------*/
function initRollovers() {
	if (!document.getElementById) return
	
	var aPreLoad = new Array();
	var sTempSrc;
	var aImages = document.getElementsByTagName('img');

	for (var i = 0; i < aImages.length; i++) {		
		if (aImages[i].className == 'imgover') {
			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '_o'+ftype);

			aImages[i].setAttribute('hsrc', hsrc);
			
			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;
			
			aImages[i].onmouseover = function() {
				sTempSrc = this.getAttribute('src');
				this.setAttribute('src', this.getAttribute('hsrc'));
			}	
			
			aImages[i].onmouseout = function() {
				if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_o'+ftype, ftype);
				this.setAttribute('src', sTempSrc);
			}
		}
	}
}

//window.onload = initRollovers;
addLoadEvent(doPopups);
addLoadEvent(prepareInputsForHints);
addLoadEvent(initRollovers);