var pageHadAnError;

//*****************************************************************************
//************************ browser sniffing vars ******************************
//*****************************************************************************
var agt=navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);
var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1) && (agt.indexOf('webtv')==-1));
var is_nav4 = (is_nav && (is_major == 4));
var is_nav5up = (is_nav && (is_major >= 5));
var is_ie   = (agt.indexOf("msie") != -1);

//*****************************************************************************
//************************ page initialization function ***********************
//*****************************************************************************

function pageInit()
{
	for(i=0;i<elements.length;i++)
		setHideLock(elements[i], 0);
	for(i=0;i<subElements.length;i++)
		setHideLock(subElements[i], 0);
}

//*****************************************************************************
//************************** dropdown menu functions **************************
//*****************************************************************************
//For menus each menu submenu div or ul is given an id.  Each respective id is added to the elements/subElements array
//The same name should be used for the calling div with _anchor placed on the end eg. <div id="product_category"> is 
//placed on <div id=product_category_anchor>

var i;  // Iterator

var timeOut = 1000;

// NOTE: These arrays are populated through dynamic JavaScript on page
//       load to receive the data-driven names of the categories.
var subElements	=	new Array();
var elements	=	new Array();

function waitToShow(which)
{
  positionMenu(which);
	showTimer	=	setTimeout('showMenu(\'' + which + '\')', 200);
}

function showMenu(which)
{
	var baseObject;
	if(is_ie){baseObject = document.all[which].style}
	if(is_nav5up){baseObject = document.getElementById(which).style}
	baseObject.visibility = 'visible';
}

function positionMenu(menuName)
{
  var menuObject;
  var position=0;
  if(is_ie){menuObject = document.all[menuName].style}
  if(is_nav5up){menuObject = document.getElementById(menuName).style}
  position = getLeftCoord(menuName+"_anchor");
  menuObject.left = position+"px";
}

function getLeftCoord(anchorTag)
{
  var leftCoord = 0;
  var anchorObject;
  if(is_ie){anchorObject = document.all[anchorTag].offsetParent;}
  if(is_nav5up){anchorObject = document.getElementById(anchorTag);}
  leftCoord = anchorObject.offsetLeft+10;
  return leftCoord;
}

function setHideLock(which, theValue)
{
	var baseObject;

	if(is_ie){baseObject = document.all[which]}
	if(is_nav4){baseObject = document.layers[which]}
	if(is_nav5up){baseObject = document.getElementById(which)}

	baseObject.hidelock = theValue;
}

function modifyHidelock(which, theValue)
{
	var baseObject;

	if(is_ie){baseObject = document.all[which]}
	if(is_nav4){baseObject = document.layers[which]}
	if(is_nav5up){baseObject = document.getElementById(which)}

	baseObject.hidelock = theValue;
}

function hideAll()
{
	for(i=0;i<elements.length;i++)
		hideSingle(elements[i]);

	for(i=0;i<subElements.length;i++)
		hideSingle(subElements[i]);
}

function hideSubs()
{
	for(i=0;i<subElements.length;i++)
		hideSingle(subElements[i]);
}

function hideSingle(which)
{
	var baseObject;

	if(is_ie){baseObject = document.all[which]}
	if(is_nav4){baseObject = document.layers[which]}
	if(is_nav5up){baseObject = document.getElementById(which)}

	locked	=	baseObject.hidelock;

	if(locked==0)
	{
		if(is_ie){baseObject = document.all[which].style}
		if(is_nav4){baseObject = document.layers[which]}
		if(is_nav5up){baseObject = document.getElementById(which).style}

		baseObject.visibility = "hidden";
	}
}

function waitToHide()
{
	setTimeout('hideAll()', timeOut);
}

//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
function showHide(which, numItems, theDisplay)
{
  for(i=0;i<numItems;i++)
  {
    if(is_ie){baseObject = document.all[which+i].style}
    if(is_nav4){baseObject = document.layers[which+i]}
    if(is_nav5up){baseObject = document.getElementById(which+i).style}
    baseObject.display=theDisplay;
  }
  var calling="show";
  var other="hide";
  if(theDisplay=='none'){calling="hide";other="show";}

  if(is_ie){document.all[calling].style.display='none';document.all[other].style.display='inline';}
  if(is_nav4){document.layers[calling].display='none';document.layers[other].display='inline';}
  if(is_nav5up){document.getElementById(calling).style.display='none';document.getElementById(other).style.display='inline';}
}

function show(which)
{
    if(is_ie){baseObject = document.all[which].style}
    if(is_nav4){baseObject = document.layers[which]}
    if(is_nav5up){baseObject = document.getElementById(which).style}

    baseObject.display='block';
}

function hide(which)
{
    if(is_ie){baseObject = document.all[which].style}
    if(is_nav4){baseObject = document.layers[which]}
    if(is_nav5up){baseObject = document.getElementById(which).style}

    baseObject.display='none';
}

function expandAll() {
    var divs = new Array('c_promoinput', 'c_promocomplete', 'c_taxexemptq', 'c_taxexempt', 'c_shippingandbilling', 'c_billing', 'c_shipping', 'c_creditcard');

    for (i = 0; i < divs.length; i++)
        show(divs[i]);
}

function doBlanks() {
    if (document.forms['frm'].sameinfo.checked) {
        document.forms['frm'].shipping_address1.value = document.forms['frm'].billing_address1.value;
        document.forms['frm'].shipping_address2.value = document.forms['frm'].billing_address2.value;
        document.forms['frm'].shipping_company.value = document.forms['frm'].billing_company.value;
        document.forms['frm'].shipping_city.value = document.forms['frm'].billing_city.value;
        document.forms['frm'].shipping_state.selectedIndex = document.forms['frm'].billing_state.selectedIndex;
        document.forms['frm'].shipping_zip.value = document.forms['frm'].billing_zip.value;
        document.forms['frm'].shipping_phone1.value = document.forms['frm'].billing_phone1.value;
        document.forms['frm'].shipping_phone2.value = document.forms['frm'].billing_phone2.value;
        document.forms['frm'].shipping_phone3.value = document.forms['frm'].billing_phone3.value;
    }
    else {
        document.forms['frm'].shipping_company.value = '';
        document.forms['frm'].shipping_address1.value = '';
        document.forms['frm'].shipping_address2.value = '';
        document.forms['frm'].shipping_city.value = '';
        document.forms['frm'].shipping_state.selectedIndex = 0;
        document.forms['frm'].shipping_zip.value = '';
        document.forms['frm'].shipping_phone1.value = '';
        document.forms['frm'].shipping_phone2.value = '';
        document.forms['frm'].shipping_phone3.value = '';
    }
}

function doTaxes() {
    document.forms['frm'].tax_name.value = document.forms['frm'].billing_nameoncard.value;
    document.forms['frm'].tax_company.value = document.forms['frm'].billing_company.value;
    document.forms['frm'].tax_address1.value = document.forms['frm'].billing_address2.value;
    document.forms['frm'].tax_address2.value = document.forms['frm'].billing_address2.value;
    document.forms['frm'].tax_city.value = document.forms['frm'].billing_city.value;
    document.forms['frm'].tax_state.selectedIndex = document.forms['frm'].billing_state.selectedIndex;
    document.forms['frm'].tax_bstate.selectedIndex = document.forms['frm'].billing_state.selectedIndex;
    document.forms['frm'].tax_zip.value = document.forms['frm'].billing_zip.value;
    document.forms['frm'].tax_email.value = document.forms['frm'].billing_phone1.value;
}

function autoTab(input, len, nextnode) {
    if (input.value.length >= len)
        if (null != nextnode)
           nextnode.focus();
}

function myVoid() {
    return;
}

function printpage() {
    window.print();
}

function disableButton(linkButton, newText) {
    if (linkButton) {
        linkButton.innerHTML = newText;
        //linkButton.disabled = true;
    }
}

//*****************************************************************************
//************************** JQuery functions **************************
//*****************************************************************************

function CallPageMethod(methodName, onSuccess, onFail) {
	var args = '';
	var l = arguments.length;
	if (l > 3) {
		for (var i = 3; i < l - 1; i += 2) {
			if (args.length != 0) args += ',';
			args += '"' + arguments[i] + '":"' + arguments[i + 1] + '"';
		}
	}
	var loc = window.location.href;
	loc = (loc.substr(loc.length - 1, 1) == "/") ? loc + "default.aspx" : loc;
	$.ajax({
		type: "POST",
		url: loc + "/" + methodName,
		data: "{" + args + "}",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: onSuccess,
		fail: onFail
	});

}

function CallAsync(method) {
	window.setTimeout(method, 1);
}

function onRequestStart(sender, arguments) {
	toggleSpinny(arguments.EventTarget);
}

function onResponseEnd(sender, arguments) {
	// Check if we've set this variable in the page via a RegisterClientScriptBlock, and scroll the top if we have
	if (pageHadAnError) {
		scroll(0, 0);
	}
}

function toggleSpinny(elementId) {
	var callingElement = elementId.replace(/\$/g, '_');

	if ($("[id$=" + callingElement + "]").siblings(".spinny").length > 0) {
		//alert($("[id$=" + callingElement + "]").css("display"));
		$("[id$=" + callingElement + "]").toggle();
		//alert($("[id$=" + callingElement + "]").css("display"));

		//alert($("[id$=" + callingElement + "]").siblings(".spinny").css("display"));
		$("[id$=" + callingElement + "]").siblings(".spinny").toggle();
		//alert($("[id$=" + callingElement + "]").siblings(".spinny").css("display"));
	}
}