function $(myid) {
  return document.getElementById(myid);
}

function changeClass(myid, myclass) {
  if (myclass == undefined) myClass = '';
  if ($(myid)) $(myid).className = myclass;
}

// check form input, return false if value is in arguments
// checkInputValue(inputObj, value1, value2, ...)
function checkInputValue() {
  var args = checkInputValue.arguments;
  var inputObj = args[0];
  for (var j = 1; j < args.length; j++) {
    if (args[j] == inputObj.value) return false;
  }
  return true;
}

// check form input, clear default value from input
// checkInputValue(inputObj, defaultValue1, defaultValue2, ...)
function clearDefaultValue() {
  var args = clearDefaultValue.arguments;
  var inputObj = args[0];
  for (var j = 1; j < args.length; j++) {
    if (args[j] == inputObj.value) inputObj.value = '';
  }
}

newWindow = null;

function popUpWin(projectUrl, projectW, projectH, windowSameSize, scrollbars, chrome) {
	
	closeWin();  // check to see if there is an existing window
	
	if (projectW == 0 && projectH == 0) {
	  if (windowSameSize == '1') {
		  type = 'samesize';
		} else {
	    type = 'fullScreen';
		}
	}
	else {
	  type = 'console';
	}
	
	if (type == "fullScreen"){
		strWidth = screen.availWidth - 10;    // add a very small amount of wiggle room
		strHeight = screen.availHeight - 10;  // add a very small amount of wiggle room
	}
	
	var x = (screen.availWidth - projectW) /2;
	var y = (screen.availHeight - projectH)/3;
	
  var myChrome = (chrome == 1) ? 'yes' : 'no';
	var tools="toolbar="+myChrome+",statusbar="+myChrome+",location="+myChrome+",menubar="+myChrome+",";

	if (type == "fullScreen") {
	  tools += "scrollbars=yes,resizable=yes,width="+strWidth+",height="+strHeight+",top=0,left=0";

	} else if (type == "console") {
	  tools += "scrollbars=";
		tools += ('1' == scrollbars) ? 'yes' : 'no'; 
		tools += ",resizable=yes,width="+projectW+",height="+projectH+",left="+x+",top="+y+"";
	}
	
	newWindow = window.open(projectUrl, '_blank', tools);
	newWindow.focus();
	
}

function closeWin() {
	if (newWindow != null){
		if(!newWindow.closed)
			newWindow.close();
	}
}

function popHelp() {
	
	var x = (screen.availWidth - 320) /2;
	var y = (screen.availHeight - 600)/3;
	
  var tools = "resizable=yes,toolbar=no,location=no,scrollbars=yes,width=320,height=600,left="+x+",top="+y+"";
	helpWindow = window.open('help-forums.php', 'newWin', tools);
	helpWindow.focus();
	
}

// Forgot login box (AJAX)

function displayForgotLogin(onoroff) {
  if (onoroff)  {
	  var myvis = "visible";
		var mycolor = '#92AEA4';
		document.getElementById("inputForgotLogin").focus();
	}
	if (!onoroff) {
	  var myvis = "hidden";
		var mycolor = '#669999';
		document.getElementById("inputForgotLogin").blur();
	}
	document.getElementById("forgotLogin").style.color = mycolor;
  document.getElementById("divForgotLogin").style.visibility = myvis;
}
function doForgotLogin() {
  document.getElementById("spanForgotLoginAjax").style.color = 'red';
  var email = document.getElementById("inputForgotLogin").value;
	ajaxForgotLogin(email);
}

function ajaxForgotLogin(email) {
	var postStr = 'email='+email;
	httpRequest("POST","../ajax/ajaxForgotLogin.php",1,forgotLoginResponse,postStr);
}

function forgotLoginResponse() {
  if (request.readyState == 4) {
	  document.getElementById("spanForgotLoginAjax").style.color = '#E7E4E2';
	  if (request.status == 200) {
      var mytext = request.responseText;
      if (mytext == 1) {
			  alert("An email with your username has been successfuly sent!  Thank you.");
				document.getElementById("divForgotLogin").style.visibility = "hidden";
			}
			else if (mytext == 2) {
			  alert("<spam blocker> You have attempted too many requests.  Please try again later.");
			}  
			else if (mytext == 3) {
			  alert("The email address you entered is not in our records.  You may register for the forums by following the 'register' link in the navigation area.  Thank you.");
			} 
			else {
			  alert("The email address you entered was invalid.  Please try again.");
			}
		}
		else {
      alert("There was a communications error attempting to send your email.  Please try again.");
		}
  }
}

// ajax functions
function httpRequest(type,url,asynch,respHandle) {
  // moz
  if (window.XMLHttpRequest) {
    request = new XMLHttpRequest();
  }
  // ie
  else if (window.ActiveXObject) {
    request = new ActiveXObject("Msxml2.XMLHTTP");
    if (!request) request = new ActiveXObject("Microsoft.XMLHTTP");
  }
  // send request | error
  if (request) {
	  if (type.toLowerCase() != "post") {
      initRequest(type,url,asynch,respHandle);
    }
	  else {
	    var args = arguments[4];
	    if (args != null && args.length > 0) {
	      initRequest(type,url,asynch,respHandle,args);
	    }
		}
	}
  else {
    alert("The item you clicked is unavailable.  Please update your browser.");
  }
}

function initRequest(type,url,asynch,respHandle) {
  try {
    request.onreadystatechange = respHandle;
  	request.open(type,url,asynch);
  	if (type.toLowerCase() == "post") {
  	  request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
  		request.send(arguments[4]);
  	}
  	else {
  	  request.send(null);
    }
	} catch (errv) {
	  alert("AJAX can not contact the server at this moment.  Please try again.  (Error: "+errv.message+")");
	}
}