/*-----------------------------------------
 *
 *  JAVASCRIPTS.JS
 *
 *  eNature.net Javascript Library
 *  ©2005 eNaure.net
 *
 *  Written by PHPeeb Programming Services
 *  http://www.phpeeb.com/
 *
 *  Last MAJOR modification: 19 Jan 2548
 *
 *----------------------------------------*/

/*========================================*/

//----------------------------------------------------
// Simple Popup Window
// Custom Size, URL, Resize and Scrollbar jargon
// Also option to have window centered, if desired
//----------------------------------------------------
	
function openWindow(url, width, height, is_scroll, is_resize, is_centered) {

	var pHoriz;
	var top;
	var left;

	if (is_centered == 1) {
		if(window.screen) {
			left = (screen.width - width) / 2;
    			top  = (screen.height - height) / 2;
		}
	} else {
		left = 100;
		top = 100;
	}

	if(!pHoriz || pHoriz.closed) {
       		pHoriz = window.open(url,'','width='+ width +', height='+ height +', left='+ left +', top='+ top +', screenX='+ left +', screenY='+ top +', scrollbars='+ is_scroll +', resizable= '+ is_resize +', menubar=no, toolbar=no');
    	} else {
       		pHoriz.focus();
    	}
}


function download_pop(url) {

	var top;
	var left;

	if(window.screen) {
		left = (screen.width - 500) / 2;
		top  = (screen.height - 300) / 2;
	}
	
       	window.open(url,'DL','width=580, height=380, left='+ left +', top='+ top +', screenX='+ left +', screenY='+ top +', scrollbars=0, resizable=0, menubar=0, toolbar=0');
}



//-----------------------------------------------
// Going Somewhere?
//-----------------------------------------------

function boink(url) {
	document.location.href = url;
}

//-----------------------------------------------
// Toggle Visibility of single item
//-----------------------------------------------

function toggle(id) {
	var itm = null;

	if (document.getElementById) {
		itm = document.getElementById(id);
	} else if (document.all) {
		itm = document.all[id];
	} else if (document.layers) {
		itm = document.layers[id];
	} 

	if (!itm) {
		// do nothing
	} else if (itm.style) {
		if (itm.style.display == "none") { 
			itm.style.display = ""; 
		} else { 
			itm.style.display = "none";
		}
	} else { 
		itm.visibility = "show"; 
	}
}
	


//-------------------------------------------------
// Toggle Visibility of hidden element to VISIBLE
//-------------------------------------------------

function showField(id) {
	var itm = null;

	if (document.getElementById) {
		itm = document.getElementById(id);
	} 
	else if (document.all) {
		itm = document.all[id];
	} 
	
	else if (document.layers) {
		itm = document.layers[id];
	}
	
	if (!itm) {
		// do nothing
	}

	else if (itm.style) {
		if (itm.style.display == "none") { 
			itm.style.display = ""; 
		} else { 
			itm.style.display = ""; 
		}
	} else { 
		itm.visibility = "show"; 
	}
}

//-------------------------------------------------
// Toggle Visibility of hidden element to HIDDEN
//-------------------------------------------------

function hideField(id) {
	var itm = null;

	if (document.getElementById) {
		itm = document.getElementById(id);
	}

	else if (document.all) {
		itm = document.all[id];
	} 
	
	else if (document.layers) {
		itm = document.layers[id];
	}
	if (!itm) {
		// do nothing
	}

	else if (itm.style) {
		if (itm.style.display == "show") { 
			itm.style.display = "none"; 
		} else { 
			itm.style.display = "none"; 
		}
	} else { 
		itm.visibility = "none"; 
	}
}

//----------------------------------------------------
// Category Menu Functions
//----------------------------------------------------

function show_bmenu() {
	var menu = document.getElementById('BMENU');

	if (menu.style.display == 'none') {
		menu.style.display = 'inline';
		//setTimeout("hide_bmenu()", 5000);
	} else {
		return;
	}
}

function hide_bmenu() {
	var menu = document.getElementById('BMENU');

	if (menu.style.display == 'inline') {
		menu.style.display = 'none';
		force_bmenu_hide();
	} else {
		return;
	}
}

function keep_bmenu() {
	setTimeout("hide_bmenu()", 50000);
}  

function force_bmenu_hide() {
	document.getElementById('BMENU').style.display = 'none';
}

//----------------------------------------------------
// Confirmation Popup box - direct to URL if true,
// Alert box if false, saying all is well
//----------------------------------------------------

function confirmAction(url, warnText)
{	
	var desc
	var agree = confirm(warnText);
	
	if (agree) {
		window.location.href = url;
	} else {
		alert ('Ok, no action has been taken');
		return false;
	}
}


//-----------------------------------------------
// Anti-Robot Shopping Cart Link
//-----------------------------------------------


var addcart_load = "<img src='/images/load_bar_orange.gif' width='43' height='11' />";

function add_cart(pid,conf)
{
	var c = '';
	var notify_div = document.getElementById('IC_'+ pid);

	notify_div.innerHTML = addcart_load;

	update_shopping_cart("/nmo/cart/add:" + pid + ":a/"+ conf, pid);

	if (conf == '?confirm=1') {
		destroy_overlay();
	}
}

//-----------------------------------------------
// Remove from Cart
//-----------------------------------------------

function remove_from_cart(cart_id, pid) {

	var notify_div = document.getElementById('IC_'+ pid);
	notify_div.innerHTML = addcart_load;

	update_shopping_cart("/nmo/cart/remove:" + cart_id, pid);
}


function destroy_overlay() {
	document.getElementById('overlay').style.display = "none";
	document.getElementById('overlay_message').style.display = "none";
}

//---------------------------------------------------------
// It assigns toggle options on or off...
// It does this whenever it's told.
//---------------------------------------------------------


function tcheck(source, remote, opt) { 
	var zfield; 
	//var thisForm = document.mainform.elements[source];
	var thisForm = source;

	for (var i = 0; i < thisForm.options.length; i++) { 
		if (thisForm.options[i].value == opt) {
			if(thisForm.options[i].selected == true) { 
				toggleFormElement(remote, 'on'); 
			} else { 
				toggleFormElement(remote, 'off'); 
				setSelectOff(remote);
			} 
		} 
	} 
} 


//----------------------------------------------------------
// It toggles the boxes - one or ten
// Or else it gets the hose again...
//----------------------------------------------------------


function setSelectOptions(select, tf, skip) {

	var selectObject = document.mainform.elements[select];
	var selectCount  = selectObject.length;

	for (var i = skip; i < selectCount; i++) {
		if (tf == 'on') {
			selectObject.options[i].selected = true;
		} else {
			selectObject.options[i].selected = false;
		}
	} 
	return true;
}


//-----------------------------------------------------------------
// Toggle form elements from disabled to enabled and vice/versa;
// Swap out element background colors for visuals, since we use
// CSS designer form display properties :)
//-----------------------------------------------------------------


function toggleFormElement(it, state) {

	var bcolor = '#EBE6DE';
	var tcolor = '#000000';
	var tform = document.getElementById(it);
	
	if (document.all || document.getElementById) {
		if (state == 'on') {
			tform.disabled = false;
			tform.style.color = tcolor;
			tform.style.backgroundColor = bcolor;

		} else {
			tform.disabled = true;
			tform.style.color = '';
			tform.style.backgroundColor = '';
		}
	}
}


//------------------------------------------------------------------
// Toggle Select Menu multiple selectability based on search mode
// Yes, Precious we'll get the hose!
//------------------------------------------------------------------


function toggleSelectPerms(mode, the_select) {

	var selectCount  = the_select.length;

	if (mode == 'strict') {
		the_select.multiple = false;

	} else {
		the_select.multiple = true;

	}

	return false;
}

//--------------------------------------------------------
// Expand Images Without screwing up layout
//--------------------------------------------------------


function expandimg(img, div) {
	if (document.getElementById) {
		itm = document.getElementById(div);
	} else if (document.all) {
		itm = document.all[div];
	} else if (document.layers) {
		itm = document.layers[div];
	}

	if (itm.style.overflow == 'auto') {
		itm.style.overflow = '';
	} else {
		itm.style.overflow = 'auto';
	}
}

//--------------------------------------------------------
// Audio Swap
//--------------------------------------------------------


function audioswap() {

	var menu  = document.getElementById('V');
	var frame = document.getElementById('I');

	var lang  = menu.options[menu.selectedIndex].value;

	frame.src = '/images/turing_sound/index.php?lang='+ lang;
}

//--------------------------------------------------------
// History Page Layout
//--------------------------------------------------------

var irow = '0';
var icol = '';

function highlite_histrow(id, orig_color) {

	if (icol == '') {
		icol = orig_color;
	}

	if (irow != 0) {
		old_element = document.getElementById('TR_'+irow);
		new_element = document.getElementById('TR_'+id);
		old_inner_div = document.getElementById('TB_'+irow);
		new_inner_div = document.getElementById('TB_'+id);

		if (irow != id) {
			new_element.style.borderColor = '#B2C0CA';
			old_element.style.borderColor = icol;
			
			new_inner_div.style.display = '';
			old_inner_div.style.display = 'none';

			toggle_arrow(id, 'ON');
			toggle_arrow(irow, 'OFF');
		} else {
			if (new_inner_div.style.display == '') {
				old_element.style.borderColor = icol;
				old_inner_div.style.display = 'none';
				toggle_arrow(irow, 'OFF');
				id = irow;
			} else { 
				old_element.style.borderColor = '#B2C0CA';
				old_inner_div.style.display = '';
				toggle_arrow(id, 'ON');
				id = irow;
			}
		}
	} else {
		var new_element = document.getElementById('TR_'+id);
		var new_inner_div = document.getElementById('TB_'+id);

		new_element.style.borderColor = '#B2C0CA';
		new_inner_div.style.display = '';
	
		toggle_arrow(id, 'ON');
	}

	irow = id;
	icol = orig_color;
}


function toggle_arrow(id, state) {
	
	arrow = document.getElementById('AR_'+id);
	
	if (state == 'ON') {
		arrow.innerHTML = '&#9660;';
	} else {
		arrow.innerHTML = '&#9658;';
	}
}



//------------------------------------------------------------------
// Check Search Box
//------------------------------------------------------------------

var scherr = "";

function chksearch(form) {

	var box = form.query;

	if (box.value == '' || box.value.length < 3) {
		if (document.getElementById('swarn').style.display == 'none') {
			Slide('swarn',{duration:.5}).down();
			scherr = 1;
		}
	
		return false;
	}
}

function errhide() {

	if (scherr == 1) {
		document.getElementById('swarn').style.display = 'none';
		scherr = 0;
		open_div = ''
	}
}



//-----------------------------------------------------------------
// Animate DIV Toggle
//-----------------------------------------------------------------

var content_height = 200;
var slide_time = 250.0;
var open_div = '';

function toggle_slide(div_id, slide_time_override, content_height_override)
{
  	if(open_div == div_id) {
    		div_id = '';
	}

	if (content_height_override > 0 ) {
		content_height = content_height_override;
	}

	if (slide_time_override > 0 ) {
		slide_time = slide_time_override;
	}


   
  setTimeout("slide_it("
      + new Date().getTime() + "," + slide_time + ",'"
      + open_div + "','" + div_id + "')", 33);
 
  open_div = div_id;
}


var slideInUse = new Array();

function Slide(objId, options) {
	this.obj = document.getElementById(objId);
	this.duration = 1;
	this.height = parseInt(this.obj.style.height);

	if(typeof options != 'undefined') { this.options = options; } else { this.options = {}; }
	if(this.options.duration) { this.duration = this.options.duration; }
		
	this.up = function() {
		this.curHeight = this.height;
		this.newHeight = '1';
		if(slideInUse[objId] != true) {
			var finishTime = this.slide();
			window.setTimeout("Slide('"+objId+"').finishup("+this.height+");",finishTime);
		}
	}
	
	this.down = function() {
		this.newHeight = this.height;
		this.curHeight = '1';
		if(slideInUse[objId] != true) {
			this.obj.style.height = '1px';
			this.obj.style.display = 'block';
			this.slide();
		}
	}
	
	this.slide = function() {
		slideInUse[objId] = true;
		var frames = 30 * duration; // Running at 30 fps

		var tIncrement = (duration*1000) / frames;
		tIncrement = Math.round(tIncrement);
		var sIncrement = (this.curHeight-this.newHeight) / frames;

		var frameSizes = new Array();
		for(var i=0; i < frames; i++) {
			if(i < frames/2) {
				frameSizes[i] = (sIncrement * (i/frames))*4;
			} else {
				frameSizes[i] = (sIncrement * (1-(i/frames)))*4;
			}
		}
		
		for(var i=0; i < frames; i++) {
			this.curHeight = this.curHeight - frameSizes[i];
			window.setTimeout("document.getElementById('"+objId+"').style.height='"+Math.round(this.curHeight)+"px';",tIncrement * i);
		}
		
		window.setTimeout("delete(slideInUse['"+objId+"']);",tIncrement * i);
		
		if(this.options.onComplete) {
			window.setTimeout(this.options.onComplete, tIncrement * (i-2));
		}
		
		return tIncrement * i;
	}
	
	this.finishup = function(height) {
		this.obj.style.display = 'none';
		this.obj.style.height = height + 'px';
	}
	
	return this;
}



//--------------------------------------------------------------------
// A carefully timed focus....
//--------------------------------------------------------------------

function dfocus(fid, odiv, time) {

	var f = document.getElementById(odiv).style.display;
	if (f == '' || f == 'none') {
		setTimeout("document.getElementById('"+ fid +"').focus()", time);
	}
}








