/*-----------------------------------------
 *
 *  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, y'all!
//----------------------------------------------------
	
function openWindow(url, width, height, is_scroll, is_resize, is_centered, fromleft, fromtop, fromright) {

	var pHoriz;
	var left;
	var top;
	var right;

	if (height == '100%') {
		height = screen.height;
	}

	if (width == '100%') {
		width = screen.width;
	}

	if (is_centered == 1) {
		if(window.screen) {
			left = (screen.width - width) / 2;
    			top  = (screen.height - height) / 2;
		}
	} else {
		if (fromleft == 0 || fromleft == "") {
			left = '50';
		} else {
			left = fromleft;
		}

		if (fromright != 0 || fromright != "") {
			left = screen.width - width;
		} else {
			left = fromleft;
		}

		if (fromtop == 0 || fromtop != "") {
			top = '50';
		} else {
			top = fromtop;
		}
	}

	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=0, toolbar=0, statusbar=0');
    	} else {
       		pHoriz.focus();
    	}
}


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

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


//-----------------------------------------------
// Clear Field
//-----------------------------------------------

function clearval(f) {
	f.value='';
}


//-------------------------------------------------
// Verify Search form has valid data
//-------------------------------------------------

function verify_searchform(f) {

	var query = f.query.value;

	if (query == '' || query == 'Enter Search Words...') {
		return false;
	} 
}


//-----------------------------------------------
// Toggle Visibility of overlay menu
//-----------------------------------------------

var showing = '';

function toggle_menu(id) {
	var itm = null;

	if (showing == '') {
		showing = 'US';
	}

	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 = "inline"; }
		else { itm.style.display = "none"; }
	} else { 
		itm.visibility = "show"; 
	}

	/*if (id == 'searchbox') {
		cb = document.getElementById('CL');
		cb.style.display = itm.style.display;
	}*/
}
	

//-----------------------------------------------
// Toggle Which Overlay Menu is Shown
//-----------------------------------------------

toggled = '';

function toggle_menu_div(id) {

	var itm = null;
	
	if (toggled == '') {
		toggled = 'US';
	}

	if (document.getElementById) {
		itm = document.getElementById(id);
		hide = document.getElementById(toggled);
	} else if (document.all) {
		itm = document.all[id];
		hide = document.all[toggled];
	} else if (document.layers) {
		itm = document.layers[id];
		hide = document.layers[toggled];
	}
	
	if (!itm) {
		// do nothing
	} else if (itm.style) {
		if (itm.style.display == "none") { 
			itm.style.display = "inline";
			hide.style.display = "none"; 
		} else { 
			itm.style.display = "none"; 
		}
	} else { 
		itm.visibility = "show"; 
	}

	toggled = id;
}

//-------------------------------------------------
// Toggle Color of Menu Button
//-------------------------------------------------

function swapcolor(button, toggled, bgcolor, txtcolor) {
	the_div = document.getElementById(toggled);
	button = document.getElementById(button);

	if (the_div.style.display == 'inline') {
		button.style.backgroundColor = bgcolor;
		button.style.color = txtcolor;
	} else {
		button.style.backgroundColor = '';
		button.style.color = '';
	}
}

//-------------------------------------------------
// 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"; 
	}
}


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

function confirm_action(url, message)
{	
	var desc
	var agree = confirm (message);
	
	if (agree) {
		window.location.href = url +'&js_confirm=1';
	} else {
		url.blur();
		return false;
	}
}


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

function add_cart(pid,format,is_uk)
{
	if (is_uk != false && format !='DVD')
	{
		confirm = confirm('Sorry, but we only ship DVD\'s to the following countries:\n\t- United Kingdom\n\t- Ireland\n\t- Germany\n\t- Japan\n\t- Australia\n\t- Canada\nIf you are having your order shipped to a location outside any of these countries, Click on \"OK\" to add this item to your cart. Otherwise, click on cancel. You will always be able to remove unwanted items from your shopping cart.');

		if (confirm) {
			document.location.href = "en?d=exec|cart|add:" + pid + ":" + format + "|";
		}
		else {
			alert('This item has not been added to your shopping cart');
		}
	}
	else
	{
		document.location.href = "/en?d=exec|cart|add:" + pid + ":" + format + "|";
	}
}

//---------------------------------------------------------
// 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;
}



//------------------------------------------------------------------
// Set value of ID'ed element
//------------------------------------------------------------------

function setval(el, value) {

	element = document.getElementById(el);
	element.value = value;
}

function addEngine()
{
	if ((typeof window.sidebar == "object") && (typeof window.sidebar.addSearchEngine == "function")) {
		window.sidebar.addSearchEngine(
		"http://www.russianbare.com/lib/searchbar/enaturenet.src",
		"http://www.russianbare.com/lib/searchbar/enaturenet.png",
		'Enature.net',
		'DVD & Video');
  	} else {
    		alert("Netscape 6 or Mozilla is needed to install a sherlock plugin");
	}
}

//---------------------------------------------------------------------
// Expand Address Book Entries
//---------------------------------------------------------------------

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

function expand_row(id, orig_color) {

	if (icol == '') {
		icol = '#F7E5C9';
	}

	if (irow != 0) {
		old_element = document.getElementById('R-'+irow);
		new_element = document.getElementById('R-'+id);

		old_inner_div = document.getElementById('T-'+irow);
		new_inner_div = document.getElementById('T-'+id);

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

			new_element.className = 'addbook_row_sel';
			old_element.className = 'addbook_row';
			
			new_inner_div.style.display = '';
			old_inner_div.style.display = 'none';

			toggle_arrow(id, 'ON');
			toggle_arrow(irow, 'OFF');

			select_address(id);

		} else {
			if (new_inner_div.style.display == '') {
				old_element.style.borderColor = icol;
				old_inner_div.style.display = 'none';
				old_element.className = 'addbook_row';
				toggle_arrow(irow, 'OFF');
				select_address(0);
				id = irow;
			} else { 
				old_element.style.borderColor = '#B2C0CA';
				old_inner_div.style.display = '';
				old_element.className = 'addbook_row_sel';
				toggle_arrow(id, 'ON');
				select_address(id);
				id = irow;
			}
		}
	} else {
		var new_element = document.getElementById('R-'+id);
		var new_inner_div = document.getElementById('T-'+id);

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

	irow = id;
	icol = orig_color;
}


//--------------------------------------------
// Toggle Expand/Collapse Indicators
//--------------------------------------------

function toggle_arrow(id, state) {
	
	arrow = document.getElementById('A-'+id);
	
	if (state == 'ON') {
		arrow.src = 'images/collapse.gif';
		arrow.height = '11';
		arrow.width = '11';
	} else {
		arrow.src = 'images/expand.gif';
		arrow.height = '11';
		arrow.width = '11';
	}
}


//--------------------------------------------
// Just something simple and stupid....
//--------------------------------------------

function select_address(aid) {
	hidden = document.getElementById('AD');
	if (aid != '') {
		hidden.value = aid;
	} else {
		hidden.value = 'JS_OFF';
	}
}

//--------------------------------------------
// Format floats to include trailing zeros
// in currency and other float formats
//--------------------------------------------

function format_float(num) {
	
	var num;

	num = num.toString().replace(/\$|\,/g,'');
	if (isNaN(num)) {
		num = "0";
	}

	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num * 100 + 0.50000000001);
	dec = num % 100;
	num = Math.floor(num / 100).toString();

	if(dec < 10) {
		dec = "0" + dec;
	}

	for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
		num = num.substring(0, num.length - (4 *i + 3))+','+num.substring(num.length - (4 *i + 3));
	}

	return num + '.' + dec;
}
