// GLOBAL VARIABLES
var ns4 = document.layers
var ns6 = document.getElementById&&!document.all
var ie4 = document.all
var child;
var child2;

// SUBMIT FORM
function submitForm() 
{
    document.form.submit();
}

function ClosePopup() 
{
	window.opener.document.form.submit();
	window.close();
}

// GET DOCUMENT ELEMENT
function GetObject(id)
{
	return document.getElementById(id);
	
	// This function checks for DOM strategy, then 
	// returns an object reference.
	
	if (document.all)
	{
		return document.all[id];
	}
	else if(document.layers)
	{
		return document.layers[id];
	}
	else
	{
		return document.getElementById(id);
	}
}

// SET HIDDEN FIELD VALUE
function SetField(field, val)
{
	GetObject(field).value = val;
}

// SHOW/HIDE DIV & SPAN TAGS
function DisplayToggle(id)
{
	var daObj = GetObject(id);
	if (daObj)
	{
		if (daObj.style.display == "none")
		{
			// display the object
			daObj.style.display = "";
			daObj.style.visibility = "visible";
		}
		else
		{
			// disable display
			daObj.style.display = "none";
			//daObj.style.visibility = "hidden";
			if (ns6)
			{
				daObj.style.visibility = "collapse";
			}
		}
	}
}

// SHOW/HIDE DIV & SPAN TAGS
function showHideContent(id)
{
	var daObj = GetObject(id);
	if (daObj)
	{
		if (daObj.style.display == "none")
		{
			// display the object
			daObj.style.display = "";
			daObj.style.visibility = "visible";
		}
		else
		{
			// disable display
			daObj.style.display = "none";
			//daObj.style.visibility = "hidden";
			if (ns6)
			{
				daObj.style.visibility = "collapse";
			}
		}
	}
}

// Booklet Caculation
function bookletCalc(LblName,clubName,studentVal)
{
	var booklets = 0;
	var lbl = GetObject(LblName);

	if (isNaN(studentVal))
	{	lbl.innerText = 0;
		alert("must be numeric");
	}
	else
	{	if (clubName == "TBS")
		{	lbl.innerHTML = studentVal;
		}
		else
		{	booklets = round_decimals(studentVal / 16,0);
			if (studentVal / 16 > booklets)
			{	lbl.innerHTML = parseFloat(booklets) + 1;
			}
			else
			{
				lbl.innerHTML = booklets;
			}
		}
	}
}


// Display Online Ordering Section
function DisplayOnlineOrdering()
{
	var chk1 = document.form.elements['Book Club'];
	var chk2 = document.form.elements['Online Cat'];
//	var chk3 = document.form.elements['Book Fairs'];
	var oo = GetObject('Online Ordering');

	if ((chk1.checked) || (chk2.checked))
	{
   	// display the object
		oo.style.display = "";
		oo.style.visibility = "visible";
	}
	else
	{
	// disable display
		oo.style.display = "none";
		if (ns6)
		{
			oo.style.visibility = "collapse";
		}
	}
}

function round_decimals(original_number, decimals) 
{
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) 
{
   var value_string = rounded_value.toString()
   var decimal_location = value_string.indexOf(".")
   if (decimal_location == -1) {
   		decimal_part_length = 0
        value_string += decimal_places > 0 ? "." : ""
    }
    else {
     decimal_part_length = value_string.length - decimal_location - 1
    }
    var pad_total = decimal_places - decimal_part_length
    if (pad_total > 0) {
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}
