// Generic javascript functions

// GENERATES A DIV CONTAINING A "PLEASE WAIT..." MESSAGE
function generateWaitDiv(OverrideText, GenerateOnlyDisableDiv, DoNotGenerateDisableDiv, CustomCSSArray, OverrideInnerHTML) {

	if (!GenerateOnlyDisableDiv) {
		var NewDiv = document.createElement('div');
	
		if (!OverrideInnerHTML)
			var ContainedText = document.createTextNode(((OverrideText)?OverrideText:'Please Wait') + '...');
		
		with (NewDiv.style) {
			width         = '180px';
			position      = 'absolute';
			border        = '1px solid #BCBCFF';
			background    = '#EFEFFF';
			fontWeight    = 'bold';
			textAlign     = 'center';
			verticalAlign = 'middle';
			paddingTop    = '20px';
			paddingBottom = '20px';
			zIndex        = '3';
			
			// Override CSS...
			if (typeof CustomCSSArray == 'object') {
				for (CSSDirective in CustomCSSArray)
					eval(CSSDirective + ' = \'' + CustomCSSArray[CSSDirective] + '\'');
			}		
			
			ScrollTop = document.documentElement.scrollTop;
			
			left = (getWindowDimension('x')/2) - ((parseInt(width)+20)/2) + 'px';
			top  = String(ScrollTop + parseInt((getWindowDimension('y')/2) -
			       (parseInt(paddingTop) + parseInt(paddingBottom)+10))) + 'px';
		}
		
		NewDiv.setAttribute('id', 'WaitDiv');
		
		if (!OverrideInnerHTML)
			NewDiv.appendChild(ContainedText);
	}	
	
	if (!DoNotGenerateDisableDiv) {
		var NewDisableDiv = document.createElement('div');
		
		with (NewDisableDiv.style) {
			top      = '0px';
			left     = '0px';
			width    = '100%';
			position = 'absolute';
			
			if (IsMSIE)
				background = "url('images/space.gif')";
		}
		
		NewDisableDiv.style.height = document.body.scrollHeight + 'px';
	}
	
	NewDisableDiv.setAttribute('id', 'DisableDiv');
	
	if (!GenerateOnlyDisableDiv) {
		if (OverrideInnerHTML)
			NewDiv.innerHTML = OverrideInnerHTML;

		document.body.appendChild(NewDiv);
	}
	
	document.body.appendChild(NewDisableDiv);
}


// Deletes wait div
function deleteWaitDiv(GenerateOnlyDisableDiv) {
	if (!GenerateOnlyDisableDiv)
		document.body.removeChild(document.getElementById('WaitDiv'));
	
	document.body.removeChild(document.getElementById('DisableDiv'));
}


// Gets page dimensions without scroll
function getWindowDimension(Dim) {
	
	if (Dim == 'x')
		return (IsMSIE)?document.documentElement.clientWidth:window.innerWidth;
	else
		return (IsMSIE)?document.documentElement.clientHeight:window.innerHeight;
}

// Gets page dimensions with scroll
function getWindowDimensionWithScroll(Dim) {
	if (Dim == 'x')
		return (IsMSIE)?document.body.offsetWidth:window.innerWidth + window.scrollMaxX;
	else
		return (IsMSIE)?document.body.offsetHeight:window.innerHeight + window.scrollMaxY;
}



// Function to create image overlay
function enlargeImg(ImgSrc, ImgLabel, ImgHTML) {
	
	IsYoutubeVideo = ImgSrc.substr(0,9) == 'VIDEOSRC:';

	with (document) {
		NewContainerDiv	= createElement('div');
		NewDisableDiv	= createElement('div');
		NewImg		= createElement('img');
		NewXImg		= createElement('img');
		//ImgHTMLElmnt	= createTextNode(ImgHTML);
		NewTextContainerDiv = createElement('div');
	}
	
	with (NewImg) {
		src		= ImgSrc;
		style.margin	= '0px 0px 20px 0px';
		style.cursor	= 'pointer';
		style.display	= 'block';
	
		TmpLabel = ImgLabel + ' [click to close]';		
		setAttribute('alt', TmpLabel);
		setAttribute('title', TmpLabel);
		setAttribute('id', 'MainImg');
	}
	
	with (NewXImg) {
		src = 'images/x.gif';
		
		style.position	= 'absolute';
		style.margin	= '-35px 0px 0px ' + ((IsMSIE7 && IsYoutubeVideo)?'5':NewImg.width+5) + 'px';
		style.cursor	= 'pointer';
		
		setAttribute('width', '30');
		setAttribute('height', '35');
		setAttribute('alt', 'close');
		setAttribute('title', 'close');
		setAttribute('id', 'XImg');
	}
	
	setEvent(NewImg, 'onClick', 'closeImg()');
	setEvent(NewXImg, 'onClick', 'closeImg()');
	
	if (NewImg.width == 0 && !IsYoutubeVideo) {
		setTimeout('enlargeImg("' + ImgSrc + '", "' + ImgLabel + '", "' + ImgHTML + '")', 500);
		return;
	}
	else if (IsYoutubeVideo)
		NewImg.width = 425;
	
	NewTextContainerDiv.style.width = NewImg.width;

	setLeft = (Math.round(document.body.clientWidth / 2)*1)/1 - ((NewImg.width + 10) / 2);
	setLeft = ((String(setLeft).indexOf('.') == -1)?String(setLeft):String(setLeft).substr(0, String(setLeft).lastIndexOf('.'))) + 'px';
	
	setTop = String(document.body.scrollTop + 35) + 'px';
	
	with (NewContainerDiv) {
		setAttribute('id', 'MainImgContainer');
		
		style.position	= 'absolute';
		style.top	= setTop;
		style.left	= setLeft;
		style.zIndex	= '5';
		style.background= '#FFFFFF';
		style.fontFamily= 'Arial';
		style.fontSize	= '12px';
		style.padding   = '20px'; 
	}
	
	with (NewDisableDiv.style) {
		top        = '0px';
		left       = '0px';
		width      = '100%';
		position   = 'absolute';
		//height     = String(document.body.scrollHeight) + 'px';
		height     = (document.documentElement.scrollTop + getWindowDimension('y')) + 'px';
		background = '#000000';
		zIndex     = '4';
		
		if (IsMSIE) {
			//toggleSelectMenuVisibility(true, 'hidden');
			filter = 'alpha(opacity=75)';
		}
		else {
			setProperty('-moz-opacity', '.75', ''); 
			setProperty('opacity', '.75', '');			
		}			
	}
	
	NewDisableDiv.setAttribute('id', 'DisableDiv');
	
	with (document.body) {
		appendChild(NewDisableDiv);
		appendChild(NewContainerDiv);
	}
	
	if (IsYoutubeVideo) {
		NewContainerDiv.innerHTML
		= '<object width="425" height="344" style="margin-bottom: 20px"><param name="movie" value="' + ImgSrc.substr(9) + '"></param>'
		+ '<param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>'
		+ '<embed src="' + ImgSrc.substr(9) + '" type="application/x-shockwave-flash" width="425" height="344" allowscriptaccess="always" '
		+ 'allowfullscreen="true" style="margin-bottom: 20px"></embed></object>';
		
		NewContainerDiv.appendChild(NewXImg);
	}
	else {
		NewContainerDiv.appendChild(NewXImg);
		NewContainerDiv.appendChild(NewImg);
	}
	
	NewContainerDiv.appendChild(NewTextContainerDiv);
	NewTextContainerDiv.innerHTML = ImgHTML;
}

// Function to close overlay
function closeImg() {	
	with (document.body) {
		removeChild(document.getElementById('DisableDiv'));
		removeChild(document.getElementById('MainImgContainer'));
	}
}

// ASSIGNS A JAVASCRIPT EVENT TO A SPECIFIED OBJECT ACCORDING TO THE BROWSER
function setEvent(Obj, Event, Action) {
	
	// For IE, use attachEvent
	if (IsMSIE) {
		eval("Obj.attachEvent('" + Event.toLowerCase() + "', function() { " + Action + " });");
	}
	
	// For proper browsers, simply use setAttribute
	else
		Obj.setAttribute(Event, Action);
}


// GETS A SPECIFIED GET VARIABLE FROM A GIVEN OR CURRENT URL
function fetchGETVar(GETVar, OverrideURL) {
	
	// Figure out what URL to use and then parse out the unnecessary parts
	UseURL = (OverrideURL)?OverrideURL:document.location.href;
	if (UseURL.indexOf('?') != -1 && UseURL.indexOf(GETVar) != -1) {
		URLArray      = UseURL.split('?');
		QueryStrArray = URLArray[1].split('&');
		
		// Set array of get vars
		GETVarArray = new Array(QueryStrArray.length);
		
		// Set the array values
		for (i in QueryStrArray) {
			CurrentVarSplit = QueryStrArray[i].split('=');
			CurrentVarName  = CurrentVarSplit[0];
			CurrentVarValue = CurrentVarSplit[1];
			eval('GETVarArray[\'' + CurrentVarName + '\'] = CurrentVarValue;');
		}
		
		if (GETVar)
			return GETVarArray[GETVar];
	}
	else
		return UseURL.substr(UseURL.lastIndexOf('/')+1);
}

// Changes opacity onMouseOver/Out
function hoverOpac(Obj, TargetOpac) {
	with (Obj.style) {
		opacity      = (TargetOpac / 100);
		MozOpacity   = (TargetOpac / 100);
		KhtmlOpacity = (TargetOpac / 100);
		filter       = "alpha(opacity=" + TargetOpac + ")";
	}
}


// Checks if a value exists in an array and optionally returns the index
function in_array(valueToMatch, arrayToCheck, ReturnIndex, CompareKeysNotValues) {
	
	NewArray = new Array();
	
	for (var i in arrayToCheck) {
		if (valueToMatch == ((CompareKeysNotValues)?i:arrayToCheck[i])) {
			if (ReturnIndex == 'array_values')
				NewArray.push(arrayToCheck[i]);
				
			else if (ReturnIndex == 'array_indexes')
				NewArray.push(i);
				
			else if (ReturnIndex == true)
				return i;
			
			else
				return true;
		}
	}
	
	if ((ReturnIndex != null && typeof ReturnIndex != 'boolean') || NewArray.length > 0)
		return NewArray;
	else
		return false;
}
