/**
 * general.js
 *
 * Author: Jarett Creason
 * Date: Dec 2007
 *
 * General javascript functions used for different things
 */

// get the object regardless of browser version or type
function getObject(name) 
{
	if(document.name) {
		return name;
	} else {
		var ns4 = (document.layers) ? true : false;
		var w3c = (document.getElementById) ? true : false;
		var ie4 = (document.all) ? true : false;

		if (ns4) return eval('document.' + name);
		if (w3c) return document.getElementById(name);
		if (ie4) return eval('document.all.' + name);
	}
	return false;
}
// Hide all elements passed in 
function hideSections(element_array) 
{
	for(var i=0; i < element_array.length; i++) {
		getObject(element_array[i]).style.display = 'none';
	}
}

// Show all elements passed in 
function showSections(element_array) 
{
	for(var i=0; i < element_array.length; i++) {
		getObject(element_array[i]).style.display = 'block';
	}
}

// get the mouse's current X position
function getMouseX(e) // works on IE6,FF,Moz,Opera7
{
	if (!e) e = window.event;
	if (e) { 
		if (e.pageX) {
			var mousex = e.pageX;
		} else if (e.clientX || e.clientY) {
			var mousex = e.clientX + document.body.scrollLeft;
		}  
		return mousex;
	}
}

// get the mouse's current Y position
function getMouseY(e) // works on IE6,FF,Moz,Opera7
{
	if (!e) e = window.event; 
	if (e) { 
		if (e.pageY) { 
			mousey = e.pageY;
		} else if (e.clientY) {
			mousey = e.clientY + document.body.scrollTop;
		}  
		return mousey;
	}
}

/**
 *    placeDiv  - The section to be moved and placed
 *    alignTo   - The section to base the placing and aligning on; note: this must have position set!
 *    placement - possible values = (right, left, above, below) -- where to move the placeDiv in relation to the alignTo section
 *    alignment - possible values = (left, center, right, top, bottom) -- line up the placeDiv to which side of the alignTo section
 *    display   - whether to control the display or not, 0 means to not change, only place, otherwise give this a value
 *                of block or inline (whichever type you want it to be displayed as (see css manuals)) 
 **/
function placeAndAlign(placeDiv, alignTo, placement, alignment, display) 
{
	// change display if user specified us to
	if(display != 0) {
		if(getObject(placeDiv).style.display == display) {
			getObject(placeDiv).style.display = "none";
		} else {
			getObject(placeDiv).style.display = display;
		}
	}

	if(getObject(placeDiv).style.display != "none") {
		// get the top and left coordiantes base on parameters
		if(placement == 'above') {
			var topCord = findPosY(getObject(alignTo)) - getObject(placeDiv).offsetHeight;
			if(alignment == 'left' || alignment == 'top') {
				var leftCord = findPosX(getObject(alignTo));
			} else if(alignment == 'center') {
				var leftCord = findPosX(getObject(alignTo)) - ((getObject(placeDiv).offsetWidth - getObject(alignTo).offsetWidth) / 2);
			} else if(alignment == 'right' || alignment == 'bottom') {
				var leftCord = findPosX(getObject(alignTo)) - (getObject(placeDiv).offsetWidth - getObject(alignTo).offsetWidth);
			}
		} else if(placement == 'right') {
			var leftCord = findPosX(getObject(alignTo)) + getObject(alignTo).offsetWidth;
			if(alignment == 'left' || alignment == 'top') {
				var topCord = findPosY(getObject(alignTo));
			} else if(alignment == 'center') {
				var topCord = findPosY(getObject(alignTo)) + (getObject(alignTo).offsetHeight / 2) - (getObject(placeDiv).offsetHeight / 2);
			} else if(alignment == 'right' || alignment == 'bottom') {
				var topCord = findPosY(getObject(alignTo)) + getObject(alignTo).offsetHeight - getObject(placeDiv).offsetHeight;
			}
		} else if(placement == 'below') {
			var topCord = findPosY(getObject(alignTo)) + getObject(alignTo).offsetHeight;
			if(alignment == 'left' || alignment == 'top') {
				var leftCord = findPosX(getObject(alignTo));
			} else if(alignment == 'center') {
				var leftCord = findPosX(getObject(alignTo)) - ((getObject(placeDiv).offsetWidth - getObject(alignTo).offsetWidth) / 2);
			} else if(alignment == 'right' || alignment == 'bottom') {
				var leftCord = findPosX(getObject(alignTo)) - (getObject(placeDiv).offsetWidth - getObject(alignTo).offsetWidth);
			}
		} else if(placement == 'left') {
			var leftCord = findPosX(getObject(alignTo)) - getObject(placeDiv).offsetWidth;
			if(alignment == 'left' || alignment == 'top') {
				var topCord = findPosY(getObject(alignTo));
			} else if(alignment == 'center') {
				var topCord = findPosY(getObject(alignTo)) + (getObject(alignTo).offsetHeight / 2) - (getObject(placeDiv).offsetHeight / 2);
			} else if(alignment == 'right' || alignment == 'bottom') {
				var topCord = findPosY(getObject(alignTo)) + getObject(alignTo).offsetHeight - getObject(placeDiv).offsetHeight;
			}
		}

		// make sure the div stays in the screen
		if(topCord < 0) {
			topCord = 0;
		}
		if(leftCord < 0) {
			leftCord = 0;
		}

		// set the div in place now
		getObject(placeDiv).style.top = topCord;
		getObject(placeDiv).style.left = leftCord;
	}
}

// find the element's _real_ x position, go through each parent
function findPosX(obj)
{
	obj = getObject(obj);
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	} else if (obj.x)
		curleft += obj.x;
	return curleft;
}

// find the element's _real_ y position, go through each parent
function findPosY(obj)
{
	obj = getObject(obj);
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	} else if (obj.y)
		curtop += obj.y;
	return curtop;
}

// check if right mouse was clicked 
function checkRightClick(e)
{
	var rightclick;
	if (!e) var e = window.event;
	if (e.which) rightclick = (e.which == 3);
	else if (e.button) rightclick = (e.button == 2);
	return rightclick;
}

/**************************************************
  				FORM CHECKING FUNCTIONS    
 **************************************************/

// checks to make sure that a password is secure enough 
// and that the verifyPassword matches the password if given
function checkNewPassword(password, verifyPassword) 
{
	var result = false;
	if(!verifyPassword) {
		verifyPassword = false;
	}

	if(!validPassword(password)) {
		alert("The password must be a mixture of upper and lower case characters or it must be a mixture of alphabetic and non-alphabetic characters and more than 5 characters in length.");
	} else if(password != verifyPassword) {
		alert("Your passwords do not match.");
	} else {
		result = true;
	}

	return result;
}

// basic password strength detection
function validPassword(pass)
{
	var result = true;            // test variable

	// test if password is 5 charaters or more
	if(pass.length < 6) {
		result = false;
	}

	// Seach for a mixture of upper and lower case characters or it must be a mixture of alphabetic and non-alphabetic characters
	if(!((pass.match(/[A-Z]/) && pass.match(/[a-z]/)) || (pass.match(/[a-zA-Z]/) && pass.match(/[^a-zA-Z]/)))) {
		result = false;
	}
	return result;
}

// _very_ basic checking for valid e-mail
function isValidEmail(str) 
{
	var result = true;
	if(!((str.indexOf("@") > 0) && (str.indexOf(".", str.indexOf("@")) > 0))) {
		result = false;
		alert('Your e-mail must be a real e-mail address.  Example: bob@somewhere.com');
	}
	return result;
}

// Checks if time is in HH:MM:SS format.
// (seconds are not required)
function IsValidTime(timeStr) 
{
	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?$/;

	var matchArray = timeStr.match(timePat);
	if (matchArray == null) {
		alert("Time is not in a valid format.");
		return false;
	}
	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];

	// make the second not required
	if (second == "") {
		second = null; 
	}

	if (hour < 0  || hour > 12) {
		alert("Hour must be between 1 and 12.");
		return false;
	}
	if (minute<0 || minute > 59) {
		alert ("Minute must be between 0 and 59.");
		return false;
	}
	if (second != null && (second < 0 || second > 59)) {
		alert ("Second must be between 0 and 59.");
		return false;
	}
	return true;
}

// basic check to see if this is an integer or not
function isInteger(s) 
{
	var i;
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if ((c < "0") || (c > "9")) {
			return false;
		}
	}
	return true;
}

// similar to PHP's nl2br, replace all "\n" with "<br/>"
function nl2br(str)
{
	return str.replace(/\n/g, '<br/>');
}

/**
 * http://kevin.vanzonneveld.net 
 * + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 
 * + improved by: Legaev Andrey 
 * + bugfixed by: Cord 
 *
 * example 1: is_array(['Kevin', 'van', 'Zonneveld']); 
 * 	returns 1: true 
 * example 2: is_array('Kevin van Zonneveld'); 
 * 	returns 2: false   
 */
function is_array( mixed_var ) { 
	return ( mixed_var instanceof Array ); 
}
