/*
function $() {
	var element = arguments[0];
	if (document.getElementById) {
		return document.getElementById(element);
	} else if (document.all) {
		return document.all[name];
	} else if (document.layers) {
		return document.layers[name];
	}
	return null;
}

function getInputByName(form, name) {
    var inputs = form.getElementsByTagName('input');

    if (!name)
      return null;

    var matchingInputs = new Array();
    for (var i = 0; i < inputs.length; i++) {
      var input = inputs[i];
      if (name && input.name != name)
        continue;
      return input;
    }

    return null;
  }
*/

function getForm() {
    var theForm = document.forms['aspnetForm'];
    if (!theForm) {
        return document.aspnetForm;
    }
    return theForm;
}

function openWhatIsThis(path) {
	window.open(path,'','height=300,width=400,scrollbars=1');
	return false;
}
function OpenProductDetailWindow(ProdID)
{
	window.open("ProductDetailWindow.aspx?pid=" + ProdID + "&close=true", "", 'toolbar=no,scrollbars=yes,status=no,width=600,height=400,resizable=yes');
}
function OpenHelpWindow(HelpID)
{
	window.open("Help/HelpSystem.aspx?HelpTopicID=" + HelpID, "",'toolbar=no,scrollbars=yes,status=no,width=600,height=400,resizable=yes');
}

function OpenServiceWindow(topicID)
{
	window.open("service.aspx?id=" + topicID + "&print=true", "", 'menubar=yes,location=yes,toolbar=yes,scrollbars=yes,status=yes,width=600,height=400,resizable=yes');
}

function OpenLegendsWindow(path)
{
	window.open(path + "/legends.aspx", "", 'toolbar=no,scrollbars=yes,status=no,width=600,height=400,resizable=yes');
}
// Obsolete
function OpenWindow(path, page)
{
	window.open(path + "/" + page, "", 'toolbar=no,scrollbars=yes,status=no,width=600,height=400,resizable=yes');
	//return false;
}
// Obsolete
function OpenWindowReg(path, page)
{
	window.open(path + "/" + page, "", 'menubar=yes,location=yes,toolbar=yes,scrollbars=yes,status=yes,width=600,height=400,resizable=yes');
}

function OpenSmallWindow2(path) {
	window.open(path, "", 'toolbar=no,scrollbars=yes,status=no,width=600,height=400,resizable=yes');
	//return false;
}

function OpenWindow2(path) {
	window.open(path, "", 'toolbar=no,scrollbars=yes,status=no,width=600,height=400,resizable=yes');
	//return false;
}

function OpenWindowReg2(path) {
	window.open(path, "", 'menubar=yes,location=yes,toolbar=yes,scrollbars=yes,status=yes,width=600,height=400,resizable=yes');
}


function OpenMcCoy()
{
	window.open("http://www.mccoymedical.com","",'toolbar=yes,scrollbars=yes,status=no,width=640,height=480,resizable=yes'); 
	return false;
}


function isDigit (c)
{
	return ((c >= "0") && (c <= "9"))
}

function isEmpty(s)
{   
	return ((s == null) || (s.length == 0))
}

function isInteger(s) {
    return (s.toString().search(/^-?[0-9]+$/) == 0);
}

/*
function isInteger (s)
{   
	var i;

	if (isEmpty(s)) 
	    if (isInteger.arguments.length == 1) return true;
	    else return (isInteger.arguments[1] == true);

	// Search through string's characters one by one
	// until we find a non-numeric character.
	// When we do, return false; if we don't, return true.

	for (i = 0; i < s.length; i++)
	{   
	    // Check that current character is number.
	    var c = s.charAt(i);

	    if (!isDigit(c)) return false;
	}

	// All characters are numbers.
	return true;
}
*/

function dp(price) 
{
	var str = "" + price;
	var number = str.length - str.indexOf('.');
	if (str.indexOf('.') == -1)
	    return str + '.00';
	
	if (number == 1)
	    return str + '00';
	
	if (number == 2)
	    return str + '0';
	
	if (number > 3)
	    return str.substring(0, str.length - number + 3);
	    
	return str;
}

function Calculate(qty, cost, total)
{
    //var theForm = getForm();
	var lTotal;
	var lQty;
	var bsvalue;
		
	total.value = "$" + dp(0);
	
	if((qty.value == ""))
	{
		lQty = 1;
		qty.value = 1;
	}
	else
		lQty = qty.value;
	
	if((Number(lQty) < 0) || (isInteger(lQty) == false))
	{
		alert("Please enter a non-negative value for the Quantity field.");
		qty.value = 1;
		lQty = qty.value;
		qty.focus();
		lTotal = dp(lQty) * dp(cost);
		total.value = "$" + dp(lTotal);
	}
	else
	{
		lTotal = dp(lQty) * dp(cost);
		total.value = "$" + dp(lTotal);
		$('#aspnetForm')[0].dirty.value = "1"
	}
}

function CalculatePPO(qty, cost, total)
{
    var theForm = getForm();
	var lTotal;
	var lQty;
	var bsvalue;
	
	total.value = "$" + dp(0);
	
	if((qty.value == ""))
	{
		lQty = 1;
		qty.value = 1;
	}
	else
		lQty = qty.value;
	
	if((Number(lQty) < 0) || (isInteger(lQty) == false))
	{
		alert("Please enter a non-negative value for the Quantity field.");
		qty.value = 1;
		lQty = qty.value;
		qty.focus();
		lTotal = dp(lQty) * dp(cost);
		total.value = "$" + dp(lTotal);
	}
	else
	{
		lTotal = dp(lQty) * dp(cost);
		total.value = "$" + dp(lTotal);
		theForm.dirty.value = "1"
	}
}

function CalculateAP(qty, cost, total, cbo)
{
    //var theForm = getForm();
	var lTotal;
	var lQty;
	var bsvalue;
	
	total.value = "$" + dp(0);

	bsvalue = cbo.options[cbo.selectedIndex].value;
	
	// Only process if this is a book -- not a slip or none
	if(bsvalue == "B")
	{
		if(isInteger(qty.value) == false || qty.value == "0")
		{
			lQty = 1;
			qty.value = 1;
		}
		else
			lQty = qty.value;
		
		if(Number(lQty) < 0)
		{
			alert("Please enter a non-negative value for the Quantity field.");
			qty.value = 1;
			lQty = qty.value;
			qty.focus();
			lTotal = dp(lQty) * dp(cost);
			total.value = "$" + dp(lTotal);
		}
		else
		{
			lTotal = dp(lQty) * dp(cost);
			total.value = "$" + dp(lTotal);
			$('#aspnetForm')[0].dirty.value = "1";
		}
	} else {
		$('#aspnetForm')[0].dirty.value = "1";
		qty.value = 0;
	}
		
	CalcGrandTotal();
}

function CalcGrandTotal()
{
    //var theForm = getForm();
    var theForm = $('#aspnetForm')[0];
	// Control variables
	var qtyCtl = 0;
	var discCtl = 0;
	
	// Value variables
	var qty = 0;
	var disc = 0.0;
	var grand = 0.0;
	var count = 0;
	
	if(theForm.grandTotal1 != null) {
	    
		//qtyCtl = getInputByName(theForm, "txtQty0");
	    //discCtl = getInputByName(theForm, "txtDisc0");
	    qtyCtl = $('#txtQty0')[0];
	    discCtl = $('#txtDisc0')[0];
		count++;
		while(qtyCtl != null) {
			grand += (qtyCtl.value * discCtl.value);
			// Get next controls
			//qtyCtl = getInputByName(theForm, "txtQty" + count);
			//discCtl = getInputByName(theForm, "txtDisc" + count);
			qtyCtl = $('#txtQty' + count)[0];
			discCtl = $('#txtDisc' + count)[0];
			count++;
		}
		eval("document." + theForm.name + ".grandTotal1.value = '$" + dp(grand) + "'");
		eval("document." + theForm.name + ".grandTotal2.value = '$" + dp(grand) + "'");
	}
}

function CheckDirty()
{
    var theForm = getForm();
	if (theForm.dirty.value == "0")
		return true;
	else
		return confirm('You have modified some of the values without saving your changes. If you proceed with this step your changes will be lost. Do you wish to proceed?')
}

function PostSecure(addr) {
	var theForm = getForm();
	theForm.action = addr;
	return true;
}

function AddToCart(appPath, qty, isbn, spotlightid)
{
	document.location = appPath + "/spotlight.aspx?sid=" + spotlightid + "&qty=" + qty.value + "&pid=" + isbn;
}

function checkCapsLock( e ) {
	var theForm = getForm();
	if(theForm.capsCheck.value == "1")
		return;
	
	var myKeyCode = 0;
	var myShiftKey = false;
	var myMsg = 'Caps Lock is On.\n\nTo prevent entering your password incorrectly,\nyou should press the Caps Lock button to turn it off.';

	// Internet Explorer 4+
	if ( document.all ) {
		myKeyCode=e.keyCode;
		myShiftKey=e.shiftKey;

	// Netscape 4
	} else if ( document.layers ) {
		myKeyCode=e.which;
		myShiftKey=( myKeyCode == 16 ) ? true : false;

	// Netscape 6
	} else if ( document.getElementById ) {
		myKeyCode=e.which;
		myShiftKey=( myKeyCode == 16 ) ? true : false;

	}

	// Upper case letters are seen without depressing the Shift key, therefore Caps Lock is on
	if ( ( myKeyCode >= 65 && myKeyCode <= 90 ) && !myShiftKey ) {
		theForm.capsCheck.value = "1";
		alert( myMsg );

	// Lower case letters are seen while depressing the Shift key, therefore Caps Lock is on
	} else if ( ( myKeyCode >= 97 && myKeyCode <= 122 ) && myShiftKey ) {
		theForm.capsCheck.value = "1";
		alert( myMsg );
	}
}

function LoadAddresses(cboS, cboB)
{
	//debugger
	DoAddress('sa', OO2BAddressArray, cboS.options[cboS.selectedIndex].value);
	DoAddress('ba', OO2BAddressArray, cboB.options[cboB.selectedIndex].value);
}

function ClearNodes(divControl) {
  for (i=0 ; i < divControl.childNodes.length ; i++)
       divControl.removeChild(divControl.childNodes[i]);
}

function DoAddress(tAddress, array, id)
{
	var obj = getObject(tAddress);
	ClearNodes(obj);
	var theText = "";
	if(id == -99) {
		var newText = document.createTextNode("[New Address]");
		obj.appendChild(newText);
		return;
	}
	
	for(var i = 0; i < array.length - 1; i++) {
		var vals = array[i].split('|');
		if(vals[0] == id) {
			if(vals[1].length == 0) {
				if(vals[0] == -1 || vals[0] == 0)
					theText = "[On File]";
				else
					theText = "";
			} else {
				text = vals[1];
				var isEmpty = text.replace(";","");
				var addressItems = text.split(';');
				var str = "";
				// Name
				// Address1
				// Address2
				// City, State, Zip, USA
				//alert("id: " + id + "\nisEmpty: " + isEmpty.length + "\n" + addressItems);
				if(isEmpty.length > 0) {
					str = addressItems[0];
					if(addressItems[1] != null && addressItems[1] != "")
						str += "\n" + addressItems[1];
					if(addressItems[2] != null && addressItems[2] != "")
						str += "\n" + addressItems[2];
					if(addressItems[3] != null && addressItems[3] != "")
						str += "\n" + addressItems[3];
					theText = str;
				}
				else
					theText = "";
			}
		}
	}
	var newText = document.createTextNode(theText);
	obj.appendChild(newText);
}

function getObject(objectId) {
	return $(objectId)[0];
}


function DoPostBack(cboDates) {
	val = cboDates.options[cboDates.selectedIndex].value;
	if(val == "-1")
		return false;
	else
		return true;
}

function expandCollapse() {
	for (var i=0; i<expandCollapse.arguments.length; i++) {
		var element = document.getElementById(expandCollapse.arguments[i]);
		element.style.display = (element.style.display == "none") ? "block" : "none";
	}
}

function toggle(divID,obj) {
	var div = $(divID)[0];
	if(!div) {
		return;
	}
	if(div.style.display == "none") {
		//obj.innerText = "-";
		div.style.display = "";
	} else {
		//obj.innerText = "+";
		div.style.display = "none";
	}
	
	return false;
}
