/*
 * SOAR
 *
 * Copyright (c) Jostens Inc. 2006-2009. All rights reserved.
 */
Object.extend(Element, {
  hideVis: function(id) {
    gebi(id).style.visibility = 'hidden';
  },

  showVis: function(id) {
    gebi(id).style.visibility = 'visible';
  }
});

// calendar key handling
Object.extend(Event, {
  keyNum:function(event) {
  	return (event.keyCode ? event.keyCode : event.which);
  }
});

// Absolute positions
// See http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function ajaxCheckForLogin(transport)
{
	var isLogin = false;
	try {
		isLogin = (transport.responseText.indexOf("form id=\"loginCriteria\"") > -1);
	}
	catch (e) {
		isLogin = true;
	}
	if (isLogin) {
		ajaxLogin(transport);
	}
	return isLogin;
}

function ajaxLogin(transport)
{
	window.location = "/";
}

function swfLogin()
{
	window.location = "/";
}

Form.sortBy = function(formId, columnName, direction, sortUrl) {
	var form = gebi(formId);
	form.sortBy.value = columnName;
	form.sortDirection.value = direction;
	form.action = sortUrl;

	if (typeof(enableDateFields) != "undefined") {
		enableDateFields();
	}
	form.submit();
}

// @see http://scriptaculous.stikipad.com/scriptaculous/show/EffectsTreasureChest
function fadeTableRow(rowid,opts) {
	opts = opts ? opts : {};
    var row = gebi(rowid);
    var cells = row.childNodes;
    for(i=0;i<cells.length;i++) {
        if(cells[i].tagName == 'TD') {
            new Effect.Fade(cells[i],opts);
        }
    }
    new Effect.Fade(row,opts);
}

/**
 * @param changedBy the dps change +/-, which is typically the #pages/2
 */
function pageCountChanged(changedBy) {
	var f=gebi('priceForm');
	var up = parseInt(f.productUnitPriceInCents.value,10);
	var d = parseInt(f.productDPSPriceInCents.value,10);
	var np = up + d * changedBy;
	f.productUnitPriceInCents.value = np;
	gebi('productCurrentPriceInCents').innerHTML = "$" + (np / 100.0).toFixed(2);
}

function blockSel() {
	return false;
}

Ajax.waiting = function() {
	var ajaxWaiting = gebi('ajaxWaiting');
	if (!ajaxWaiting)
	{
		ajaxWaiting = document.createElement('input');
		ajaxWaiting.id = 'ajaxWaiting';
		document.body.appendChild(ajaxWaiting);
	}
	var ret = Ajax.activeRequestCount > 0 ? true : false;
	ajaxWaiting.value = ret;
	return ret;
}

function utfToIso88591(str) {
	var ret = new Array();
	for (var i=0;i<str.length;i++) {
		var c = str.charCodeAt(i);
		if (c == 32 || c >= 48 && c <= 122 || c == 46) {
			ret[i]=str.charAt(i);
		} else {
			ret[i]="&#" + c + ";";
		}
	}
	return ret.join('');
}

// @see Position.positionedOffset
function positionedOffsetT(element) {
    var valueT = 0;
    do {
      valueT += element.offsetTop || 0;
      element = element.offsetParent;
      if (element) {
        p = Element.getStyle(element, 'position');
        if (p == 'relative' || p == 'absolute') break;
      }
    } while (element);
    return valueT;
}
// @see Position.realOffset
function realOffsetT(element) {
	var valueT = 0;
    do {
      valueT += element.scrollTop || 0;
      element = element.parentNode;
    } while (element);
    return valueT;
}

// faster then prototype $
var gelms = {};
function gebi(id) {
	var ret = gelms[id];
	if (ret == null) {
		ret = document.getElementById(id);
		gelms[id] = ret;
	}
	return ret;
}

// destination urls - you can append to these in your pages
if (typeof(Event) != 'undefined' && typeof(Event.observe) != 'undefined') {
	var destUrls = ['a_col_terms', 'a_col_privacy'];
	function addDestUrls() {
		destUrls.each(addDestUrl);
	}
	function addDestUrl(aId) {
		var a = $(aId);
		if (a) {
			var loc = window.location.pathname + window.location.search + "";
			var href = a.href;
			if (loc.indexOf(href) == -1) {
				a.href += '?destination=' + escape(loc);
			} else {
				var s = document.createElement("span");
				s.appendChild(document.createTextNode(a.innerHTML));
				a.parentNode.replaceChild(s, a);
			}
		}
	}
	Event.observe(window,'load',addDestUrls);
}

// Minimal window height
// @since 1.12.01 moved to includes.jsp
// window.bSubHt = true;
//@since 1.12.01
var sectionIds = ['header-content', 'main-content', 'footer-content'];
function fixSubHt() {
	try {
		if (window.bSubHt) {
			var sc = $('sub-container');
			if (sc) {
				var minHt = 300;
				var scDim = Element.getDimensions(sc);
				if (scDim.height < minHt) {
					Position.prepare();
					// @since 1.12.01
					var availHt = f_clientHeight();
					sectionIds.each(function (sectionId) {
		            	try {availHt -= Element.getDimensions(sectionId).height; } catch (e) { }
		            });
					var h = Math.max(Math.min(availHt, minHt), minHt);
					sc.style.height=h+'px';
				}
			}
		}
	} catch (e) { }
}
Event.observe(window, 'load', fixSubHt);

// Test whether cookies are enabled
// Relocated from includes.jsp
// @since 1.12
window.bCookieTest = true;
function cookieTest() {
	if (window.bCookieTest) {
		var cookieEnabled = navigator.cookieEnabled;
		if (!cookieEnabled && (typeof document.cookie == "string")) {
		    if (document.cookie.length == 0) {
		        document.cookie = "test";
		        cookieEnabled = (document.cookie == "test");
		        document.cookie = "";
		    } else {
		        cookieEnabled = true;
		    }
		}
		if (!cookieEnabled) {
			window.location = '/jsp/noCookies.jsp';
		}
	}
}
Event.observe(window, 'load', cookieTest);