// PANEL AJAX INTERACTION FUNCTIONS

function livePostBreech(tgt_form, tgt_action, livePanel, loadPanel, errorPanel, set_size)
{
	if	(document.getElementById(tgt_form))	
	{	
		// Populate field values
		var frm_fields = createFormQuery(tgt_form);
		// End of the above
		
		// POST form data
		ajaxUpdate(false , tgt_action, { params:frm_fields, meth:"post", async:true, startfunc:"livePanelGrooming('"+livePanel+"', '"+loadPanel+"', '"+errorPanel+"', false, 'setScene', '"+set_size+"')", endfunc:"livePanelGrooming('"+livePanel+"', '"+loadPanel+"', '"+errorPanel+"', req.responseText, 'shootScene', '"+set_size+"');", errorfunc:"", noauthfunc:"" });
		// End of the above
	}
}

// Set and send an AJAX job
function liveRequestBreech(getCommands, livePanel, loadPanel, errorPanel, set_size)
{ ajaxUpdate(false , "index.php?", { params:getCommands+'&rand_cache='+rand(23331, 48832003), meth:"get", async:true, startfunc:"livePanelGrooming('"+livePanel+"', '"+loadPanel+"', '"+errorPanel+"', false, 'setScene', '"+set_size+"')", endfunc:"livePanelGrooming('"+livePanel+"', '"+loadPanel+"', '"+errorPanel+"', req.responseText, 'shootScene', '"+set_size+"');", errorfunc:"", noauthfunc:"" }); } 
// End of the above

window.sceneSetting = new Array();

// Set the panels for either loading or loading AJAX command results
function livePanelGrooming(livePanel, loadPanel, errorPanel, content, action, set_size)
{ 
	if		(action && action == 'setScene')
	{
		if 	(set_size)
		{
			window.sceneSetting['width'] 	= document.getElementById(livePanel).offsetWidth;
			window.sceneSetting['height'] 	= document.getElementById(livePanel).offsetHeight;
		}
		
		if	(loadPanel && loadPanel != 'false')
		{
			if	(document.getElementById(loadPanel)) { document.getElementById(loadPanel).style.display = ''; }
			if	(document.getElementById(livePanel)) { document.getElementById(livePanel).style.display = 'none'; }
			
			if 	(set_size == 'loading' || set_size == 'all')
			{ document.getElementById(loadPanel).style.height = window.sceneSetting['height']; }
		}
	}
	else if	(action && action == 'shootScene' && content)
	{	
		if	(document.getElementById(loadPanel)) { document.getElementById(loadPanel).style.display = 'none'; }
		if	(document.getElementById(livePanel)) 
		{	
			// Populate live panel with content
			document.getElementById(livePanel).innerHTML = content;	
			// End of the above
			
			// Detect and execute embedded JS
			var responseContents = document.getElementById(livePanel).getElementsByTagName("script");
			
			if	(responseContents)
			{ 
				for (var i = 0; i < responseContents.length; i++)
				{ eval(responseContents[i].innerHTML); }
			}
			// End of the above
			
			// Show the live panel with new content added
			document.getElementById(livePanel).style.display = ''; 
			// End of the above
			
			if 	(set_size && set_size == 'all' || set_size && set_size == 'panel')
			{ document.getElementById(livePanel).style.width = window.sceneSetting['width']; } 
		}
	}
	else if	(action && action == 'shootScene' && !content)
	{
		if	(document.getElementById(loadPanel)) { document.getElementById(loadPanel).style.display = 'none'; }
		if	(document.getElementById(errorPanel)) { document.getElementById(errorPanel).style.display = ''; }
	}
}
// End of the above

// END OF THE ABOVE

// MISC FUNCTIONS

	function addListener(element, type, expression, bubbling)
	{
		bubbling = bubbling || false;
		
		if(window.addEventListener)	{ // Standard
			element.addEventListener(type, expression, bubbling);
			return true;
		} else if(window.attachEvent) { // IE
			element.attachEvent('on' + type, expression);
			return true;
		} else return false;
	}
	
	function removeListener(element, type, expression, bubbling)
	{
		bubbling = bubbling || false;
		
		if(window.addEventListener)	{ // Standard
			element.removeEventListener(type, expression, bubbling);
			return true;
		} else if(window.attachEvent) { // IE
			element.detachEvent('on' + type, expression);
			return true;
		} else return false;
	}

	function rand( min, max ) 
	{ 
		if( max ) {
			return Math.floor(Math.random() * (max - min + 1)) + min;
		} else {
			return Math.floor(Math.random() * (min + 1));
		}
	}

	function countWords(tgt_id, show_word_count, show_char_count) 
	{
		var char_count = document.getElementById(tgt_id).innerHTML.length;
	
		var fullStr = document.getElementById(tgt_id).innerHTML + " ";
		var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
		var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
		var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
		var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
		var splitString = cleanedStr.split(" ");
		var word_count = splitString.length -1;
		
		if (fullStr.length <2) 
		{ word_count = 0; }
		
		if (show_word_count && document.getElementById(tgt_id+'[word_count]'))
		{ document.getElementById(tgt_id+'[word_count]').innerHTML = word_count; }
		if (show_char_count && document.getElementById(tgt_id+'[char_count]'))
		{ document.getElementById(tgt_id+'[char_count]').innerHTML = char_count; }
		
		return word_count;
	}

	function mirrorTextEntry(ori_id, tgt_id, ori_action, tgt_action)
	{
		var ori_text;
		
		if	(document.getElementById(ori_id) && document.getElementById(tgt_id))
		{
			if		(ori_action == 'value')
			{ ori_text = document.getElementById(ori_id).value; }
			else if	(ori_action == 'html')
			{ ori_text = document.getElementById(ori_id).innerHTML; }
			
			if		(tgt_action == 'value')
			{ document.getElementById(tgt_id).value = ori_text; }
			else if	(tgt_action == 'html')
			{ document.getElementById(tgt_id).innerHTML = ori_text; }
		}
	}

	/// ERROR CHECKING & SUBMIT FUNCTIONS ///
	
		// Loop through and check form submission data
		function checkFormEntrySubmit(tgt_id, frm_conditions, error_conditions, useAJAX, tgt_action, tgt_window)
		{
			if	(tgt_id && document.getElementById(tgt_id) && frm_conditions)
			{
				var frm_value;
				var error;
				
				for (var i in frm_conditions)
				{ 
					// Populate the form value
					frm_value = document.getElementById(i).value;
					// End of the above
				
					// Check submission values
					if		(frm_conditions[i]['check'] == 'varchar')
					{ 
						if (frm_conditions[i]['length'] && frm_value.length < frm_conditions[i]['length'])
						{ error = true; higlightError(i, error_conditions, 'You must enter at least '+frm_conditions[i]['length']+' characters'); }
						else if	(!frm_value)
						{ error = true; higlightError(i, error_conditions, 'You must enter some text');  }
						else
						{ clearError(i, error_conditions);  }
					}
					else if	(frm_conditions[i]['check'] == 'number')
					{
						if (frm_conditions[i]['length'] && frm_value.length < frm_conditions[i]['length'])
						{ error = true; higlightError(i, error_conditions, 'You must enter at least '+frm_conditions[i]['length']+' characters'); }
						else if	(!frm_value || !Number(frm_value))
						{ error = true; higlightError(i, error_conditions, 'You must only use numbers');  }
						else
						{ clearError(i, error_conditions);  }
					}
					else if	(frm_conditions[i]['check'] == 'email')
					{ 
						if (frm_conditions[i]['length'] && frm_value.length < frm_conditions[i]['length'])
						{ error = true; higlightError(i, error_conditions, 'You must enter at least '+frm_conditions[i]['length']+' characters'); }
						else if	(!frm_value)
						{ error = true; higlightError(i, error_conditions, 'You must only use a valid email address');  }
						else
						{ clearError(i, error_conditions);  }
					}
					// End of the above
				}
				
				if		(!error && useAJAX)
				{ divManage('clear_comment_error', 'hide', false); livePostBreech(tgt_id, tgt_action, tgt_id+'[PANEL]', tgt_id+'[LOAD]', tgt_id+'[ERROR]', 'loading'); }
				else if	(!error && !useAJAX)
				{  
					if	(tgt_action)
					{ document.getElementById(tgt_id).action = tgt_action; }
					
					if	(tgt_window)
					{ document.getElementById(tgt_id).target = tgt_window; }
					
					// Clear error messages button
					divManage('clear_comment_error', 'hide', false);
					// End of the above
					
					document.getElementById(tgt_id).submit();
				}
				else if	(error)
				{ 
					// Clear error messages button
					divManage('clear_comment_error', 'show', false);
					// End of the above
				}
			}
		}
		// End of the above
	
		// Loop through and clear form error
		function clearFormErrorData(tgt_id, frm_conditions, error_conditions)
		{
			if	(tgt_id && document.getElementById(tgt_id) && frm_conditions)
			{
			for (var i in frm_conditions)
			{ 
				// Populate the form value
				frm_value = document.getElementById(i).value;
				// End of the above
			
				// Check submission values
				if		(frm_conditions[i]['check'] == 'varchar')
				{ clearError(i, error_conditions);  }
				else if	(frm_conditions[i]['check'] == 'number')
				{ clearError(i, error_conditions);  }
				else if	(frm_conditions[i]['check'] == 'email')
				{ clearError(i, error_conditions);  }
				// End of the above
			}
			}
			
			// Clear error messages button
			divManage('clear_comment_error', 'hide', false);
			// End of the above
		}
		// End of the above
	
		// Highlight error form inputs
		function higlightError(tgt_id, error_conditions, error_msg)
		{
			if	(tgt_id && error_conditions.alert_color && document.getElementById(tgt_id))
			{ document.getElementById(tgt_id).style.backgroundColor = error_conditions.alert_color; }
		
			if	(tgt_id && error_conditions.msg_tag && document.getElementById(tgt_id+error_conditions.msg_tag))
			{ divManage(tgt_id+error_conditions.msg_tag, 'show', false); }
			
			if	(tgt_id && error_msg && error_conditions.msg_text && document.getElementById(tgt_id+error_conditions.msg_text))
			{ document.getElementById(tgt_id+error_conditions.msg_text).innerHTML = error_msg; }
		}
		// End of the above
		
		// Clear highlighted error form inputs
		function clearError(tgt_id, error_conditions)
		{
			if	(tgt_id && document.getElementById(tgt_id))
			{ document.getElementById(tgt_id).style.backgroundColor = ''; }
			
			if	(tgt_id && error_conditions.msg_tag && document.getElementById(tgt_id+error_conditions.msg_tag))
			{ divManage(tgt_id+error_conditions.msg_tag, 'hide', false); }
			
			if	(tgt_id && error_conditions.msg_text && document.getElementById(tgt_id+error_conditions.msg_text))
			{ document.getElementById(tgt_id+error_conditions.msg_text).innerHTML = ''; }
		}
		// End of the above

	/// END OF THE ABOVE /// 

	function changeInputValue(tgt_id, tgt_value)
	{
		if	(document.getElementById(tgt_id))
		{ document.getElementById(tgt_id).value = tgt_value; }
	}

	function valueSubmitFrm(tgt_id, tgt_value, tgt_frm, tgt_action)
	{
		if	(document.getElementById(tgt_id))
		{ document.getElementById(tgt_id).value = tgt_value; }
	
		submitFrm(tgt_frm, tgt_action);
	}

	function submitFrm(tgt_id, frm_action)
	{
		if	(document.getElementById(tgt_id))
		{ 
			if	(frm_action)
			{ document.getElementById(tgt_id).action = frm_action; }
		 
		 	document.getElementById(tgt_id).submit(); 
		}
	}

	// Switch image function
	function switchSrc(tgt_id, tgt_src, nw_src, tgt_group)
	{	
		if	(tgt_id && document.getElementById(tgt_id))
		{
			if	(!window.imgRecord)
			{ window.imgRecord = new Array(); }
			
			if	(tgt_group && window.imgRecord[tgt_group])
			{ 
				switchSrc(window.imgRecord[tgt_group]['id'], false, window.imgRecord[tgt_group]['src'], ''); 
				
				if (tgt_src)
				{
					window.imgRecord[tgt_group] = new Array();
					window.imgRecord[tgt_group]['id']	= tgt_id;
					window.imgRecord[tgt_group]['src']	= tgt_src;
				}
			}
			else if (tgt_group)
			{ 
				if (tgt_src)
				{
					window.imgRecord[tgt_group] = new Array();
					window.imgRecord[tgt_group]['id']	= tgt_id;
					window.imgRecord[tgt_group]['src']	= tgt_src;
				}
			}
			
			if	(document.getElementById(tgt_id))
			{ document.getElementById(tgt_id).src = nw_src; }
		}
		else if (!tgt_id) 
		{	
			if	(tgt_group && window.imgRecord[tgt_group])
			{ switchSrc(window.imgRecord[tgt_group]['id'], false, window.imgRecord[tgt_group]['src'], ''); }
		}
	}
	// End of the above
	
	// Switch image function
	function switchBak(tgt_id, tgt_src, nw_src, tgt_group)
	{	
		if	(tgt_id && document.getElementById(tgt_id))
		{
			if	(!window.imgRecord)
			{ window.bakRecord = new Array(); }
			
			if	(tgt_group && window.bakRecord[tgt_group])
			{ 
				switchSrc(window.bakRecord[tgt_group]['id'], false, window.bakRecord[tgt_group]['src'], ''); 
				
				if (tgt_src)
				{
					window.bakRecord[tgt_group] = new Array();
					window.bakRecord[tgt_group]['id']	= tgt_id;
					window.bakRecord[tgt_group]['src']	= tgt_src;
				}
			}
			else if (tgt_group)
			{ 
				if (tgt_src)
				{
					window.bakRecord[tgt_group] = new Array();
					window.bakRecord[tgt_group]['id']	= tgt_id;
					window.bakRecord[tgt_group]['src']	= tgt_src;
				}
			}
			
			if	(document.getElementById(tgt_id))
			{ document.getElementById(tgt_id).style.backgroundImage = 'url('+nw_src+')'; }
		}
		else if (!tgt_id) 
		{	
			if	(tgt_group && window.bakRecord[tgt_group])
			{ switchBak(window.bakRecord[tgt_group]['id'], false, window.bakRecord[tgt_group]['src'], ''); }
		}
	}
	// End of the above

	function managePopUps(tgt_window, tgt_width, tgt_height, tgt_url, scrl_bars)
	{ 
		if	(!tgt_width)
		{ tgt_width = '150'; }
	
		if	(!tgt_height)
		{ tgt_height = '250'; }
	
		if	(!window.activePopups)
		{ window.activePopups = new Array(); }
		
		// Create popup and populate popup registry
		if		(tgt_url)
		{ window.activePopups[tgt_window] = window.open(tgt_url, tgt_window,"location=1,status=0,resizable=1,scrollbars="+scrl_bars+",width="+tgt_width+",height="+tgt_height); }
		else
		{ alert('No target URL entered, window could not be opened'); }
		// End of the above
	} 
	
	function clearPopUps()
	{
		if	(window.activePopups)
		{
			for (var i in window.activePopups)
			{ window.activePopups[i].close(); } 
		}
	}
	
	function formChangeData(tgt_id, tgt_method, tgt_action, tgt_target)
	{
		if	(document.getElementById(tgt_id))
		{
			if	(tgt_method)
			{ document.getElementById(tgt_id).method = tgt_method; }
			
			if	(tgt_action)
			{ document.getElementById(tgt_id).action = tgt_action; }
			
			if	(tgt_target)
			{ document.getElementById(tgt_id).target = tgt_target; }
		}
	}
	
	function addPlusSymbols(txt_string)
	{
		return txt_string.replace(" ", "+");
	}
	
	function divManage(tgt_id, tgt_action, tgt_group)
	{
		if	(!window.divRecord)
		{ window.divRecord = new Array(); }
		
		if	(tgt_action == 'hide')
		{ var action = 'none'; var counter_action = 'show'; }
		else if (tgt_action == 'show')
		{ var action = ''; var counter_action = 'hide'; }
		else if (tgt_action == 'minimize')
		{  var action = 'hidden'; var action_size = '40px'; var counter_action = 'maximize'; }
		else if (tgt_action == 'maximize')
		{ var action = 'visible'; var action_size = '100%'; var counter_action = 'minimize'; }
		else if	(tgt_action == 'toggle')
		{
			if	(document.getElementById(tgt_id))
			{
				if	(document.getElementById(tgt_id).style.display == 'none')
				{ document.getElementById(tgt_id).style.display = ''; }
				else
				{ document.getElementById(tgt_id).style.display = 'none'; }
			}
		}
		
		if	(tgt_id && document.getElementById(tgt_id) && tgt_action != 'toggle')
		{	
			if	(tgt_group && window.divRecord[tgt_group])
			{ divManage(window.divRecord[tgt_group], counter_action, ''); window.divRecord[tgt_group] = tgt_id; }
			else if (tgt_group)

			{ window.divRecord[tgt_group] = tgt_id; }
			
			if		(tgt_action == 'show' || tgt_action == 'hide')
			{ document.getElementById(tgt_id).style.display = action; }
			else if (tgt_action == 'minimize' || tgt_action == 'maximize')
			{ document.getElementById(tgt_id).style.overflow = action; document.getElementById(tgt_id).style.width = action_size; }
		}
		else if (!tgt_id && tgt_action != 'toggle') 
		{	
			if	(tgt_group && window.divRecord[tgt_group])
			{ divManage(window.divRecord[tgt_group], counter_action, false); }
		}
	}
	
	function whichBrs() 
	{
	var agt=navigator.userAgent.toLowerCase();
	if (agt.indexOf("opera") != -1) return 'Opera';
	if (agt.indexOf("staroffice") != -1) return 'Star Office';
	if (agt.indexOf("webtv") != -1) return 'WebTV';
	if (agt.indexOf("beonex") != -1) return 'Beonex';
	if (agt.indexOf("chimera") != -1) return 'Chimera';
	if (agt.indexOf("netpositive") != -1) return 'NetPositive';
	if (agt.indexOf("phoenix") != -1) return 'Phoenix';
	if (agt.indexOf("firefox") != -1) return 'Firefox';
	if (agt.indexOf("safari") != -1) return 'Safari';
	if (agt.indexOf("skipstone") != -1) return 'SkipStone';
	if (agt.indexOf("msie") != -1) return 'IE';
	if (agt.indexOf("netscape") != -1) return 'Netscape';
	if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
	if (agt.indexOf('\/') != -1) {
	if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
	return navigator.userAgent.substr(0,agt.indexOf('\/'));}
	else return 'Netscape';} else if (agt.indexOf(' ') != -1)
	return navigator.userAgent.substr(0,agt.indexOf(' '));
	else return navigator.userAgent;
	}

	window.browserType = whichBrs();

// END OF MISC FUNCTIONS