// $Id: common.js,v 1.8 2012-02-08 16:14:31 craigs Exp $
// $Log: common.js,v $
// Revision 1.8  2012-02-08 16:14:31  craigs
// Update objecttable class, RT6277
//
// Revision 1.7  2011-04-28 17:39:17  tami
// RT 5311 Added function incrementCheck for use on brand, category and search pages to check the number ordered is a multiple of x
//
// Revision 1.6  2011-04-25 12:34:18  craigs
// Removed loading of 'Pricing & Availability' script and support functions to a separate file, 'loader.php' so that php could be used to insert server specific host names taken from 'server_conf.php'
//
// Revision 1.3  2010-09-14 14:41:36  craigs
// Added multi-instantiation to initialization script
//
// Revision 1.2  2010-09-14 11:40:28  tami
// TICKET4078 - brand and category revamp
//
// Revision 1.1  2010-07-22 15:18:54  craigs
// RT3830, provides core JS functions
//

var exInfoServer = 'www.fullcompass.com.dev-craigs.fullcompass.com';
var SLIDETIME = 12, SLIDECOUNT = 12;
function expando ()
{
	this.oShield;
	this.cShield;
	this.cShieldOpen;
	this.cShieldClose;
	this.oToggle;
	this.oOpen;
	this.cOpen;
	this.oClose;
	this.cClose;
	this.oParent;
	this.oVisibleChild;
	this.bOpen = false;
	this.iClsdHeight = 0;
	this.omoreDescription;
	this.olessDescription;
	this.iCounter;
	this.tTimer;
	this.iSlideDistance;
	this.ref = oObjectTable.getRef(this);
  
	this.init = init;
	this.toggle = toggle;
	this.startMore = startMore;
	this.doMore = doMore;
	this.finishMore = finishMore;
	this.startLess = startLess;
	this.doLess = doLess;
	this.finishLess = finishLess;

	function init (shieldObject, shieldClass, shieldOpenClass, shieldCloseClass, toggleObject, openObject, openClass, closeObject, closeClass, visibleChild, clsdHeight)
	{
		if (typeof shieldObject == 'undefined' || shieldObject == null) return;
		this.oShield = shieldObject;
		if (typeof shieldClass == 'undefined' || shieldClass == null || shieldClass == "") return;
		this.cShield = shieldClass;

		if (typeof shieldOpenClass == 'undefined' || shieldOpenClass == null || shieldOpenClass == "") return;
		this.cShieldOpen = shieldOpenClass;
		if (typeof shieldCloseClass == 'undefined' || shieldCloseClass == null || shieldCloseClass == "") return;
		this.cShieldClose = shieldCloseClass;

		if (toggleObject == null && openObject == null && closeObject == null) return;

		if (typeof toggleObject != 'undefined' && toggleObject != null)
		{
			this.oToggle = toggleObject;
			this.oToggle.setAttribute('managerRef', this.ref);
			this.oToggle.onclick = function () {oObjectTable.getObject(this.getAttribute('managerRef')).toggle();};
		}
		else
		{
			if (typeof openObject == 'undefined' || openObject == null || typeof closeObject == 'undefined' || closeObject == null) return;
			this.oOpen = openObject;
			this.oOpen.setAttribute('managerRef', this.ref);
			this.oOpen.onclick = function () {oObjectTable.getObject(this.getAttribute('managerRef')).startMore();};
			this.oClose = closeObject;
			this.oClose.setAttribute('managerRef', this.ref);
			this.oClose.onclick = function () {oObjectTable.getObject(this.getAttribute('managerRef')).startLess();};
		}

		if (typeof openClass == 'undefined' || openClass == null || openClass == "") return;
		this.cOpen = openClass;
		if (typeof closeClass == 'undefined' || closeClass == null || closeClass == "") return;
		this.cClose = closeClass;

		if (visibleChild != null) this.oVisibleChild = visibleChild;

		if (typeof clsdHeight != 'undefined' && clsdHeight != null && clsdHeight >= 0)
			this.iClsdHeight = clsdHeight;
	};

	function toggle ()
	{
		var className;
		var title = "";
		if (this.bOpen)
		{
			className = this.cOpen;
			this.startLess();
			title = 'Click to expand';
		}
		else
		{
			className = this.cClose;
			this.startMore();
			title = 'Click to contract';
		}
		this.oToggle.className = className;
		this.oToggle.title = title;
		this.bOpen = !this.bOpen;
	};

	function startMore ()
	{
		if (this.tTimer) clearTimeout(this.tTimer);
		if (this.oVisibleChild != null)
			this.iSlideDistance = (this.oShield.firstChild.offsetHeight - this.oShield.offsetHeight) / SLIDECOUNT;
		else
			this.iSlideDistance = (this.oShield.firstChild.offsetHeight - this.iClsdHeight) / SLIDECOUNT;
		this.iCounter = SLIDECOUNT;
		this.oShield.className = this.cShield;
		this.doMore();
	};

	function doMore ()
	{
		if (this.tTimer) clearTimeout(this.tTimer);
		var currentHeight = this.oShield.offsetHeight;
		if (this.iCounter <= 0)
			this.finishMore();
		else
		{
			this.iCounter--;
			this.oShield.style.height = currentHeight + this.iSlideDistance + 'px';
			this.tTimer = setTimeout("oObjectTable.getObject('"+ this.ref +"').doMore()", SLIDETIME);
		}
	};

	function finishMore ()
	{
		if (this.tTimer) clearTimeout(this.tTimer);
		this.oShield.style.height = 'auto';
		this.oShield.className = this.cShield +' '+ this.cShieldClose;
	};

	function startLess ()
	{
		if (this.tTimer) clearTimeout(this.tTimer);
		this.iCounter = SLIDECOUNT;
		this.oShield.className = this.cShield;
		if (this.oVisibleChild != null)
			this.iSlideDistance = (this.oShield.offsetHeight - (this.oVisibleChild.offsetTop + this.oVisibleChild.offsetHeight + 55)) / SLIDECOUNT;
		else
			this.iSlideDistance = (this.oShield.firstChild.offsetHeight - this.iClsdHeight) / SLIDECOUNT;
		this.doLess();
	};

	function doLess ()
	{
		if (this.tTimer) clearTimeout(this.tTimer);
		var currentHeight = this.oShield.offsetHeight;
		if (this.iCounter > 1)
		{
			this.iCounter--;
			this.oShield.style.height = currentHeight - this.iSlideDistance + 'px';
			this.tTimer = setTimeout("oObjectTable.getObject('"+ this.ref +"').doLess()", SLIDETIME);
		}
		else
			this.finishLess();
	};

	function finishLess ()
	{
		if (this.tTimer) clearTimeout(this.tTimer);
		if (this.oVisibleChild != null) 
			this.oShield.style.height = this.oVisibleChild.offsetTop + this.oVisibleChild.offsetHeight + 55 + 'px';
		else
			this.oShield.style.height = this.iClsdHeight + 'px';;
		this.oShield.className = this.cShield +' '+ this.cShieldOpen;
	};
};

var oObjectTable = new objectTable();
function objectTable ()
{
	this.table = [];
	this.type = 'objectTable';

	this.getRef = getRef;
	this.createRef = createRef;
	this.deleteRef = deleteRef;
	this.getObject = getObject;
	this.deleteObject = deleteObject;
	this.getUniqueKey = getUniqueKey;

	function getRef (object)
	{
		for (var x = 0; x < this.table.length; x++)
		{
			if (this.table[x]['object'] == object && typeof this.table[x]['ref'] != 'undefined')
				return this.table[x]['ref'];
		}
		var string = '';
		while ((string = this.getUniqueKey()) === false)
		var hash = [];
		hash = {'key':string,'object':object};
		this.table.push(hash);

		return string;
	};
	function deleteRef (ref)
	{
		var object;
		if ((object	= this.getObject(ref)) == false) return;
		returnVal = this.deleteObject(object);
		object = null;
		return returnVal;
	};
	function createRef (ref, object)
	{
		var hash = [];
		hash = {'key':ref,'object':object};
		this.table.push(hash);
	};
	function getObject (ref)
	{
		for (var x = 0; x < this.table.length; x++)
		{
			if (this.table[x]['key'] == ref)
			{
				return this.table[x]['object'];
			}
		}
		return false;
	};
	function deleteObject (object)
	{
		var newTable = [];
		for (var x = 0; x < this.table.length; x++)
		{
			if (this.table[x]['object'] != object && typeof this.table[x]['key'] != 'undefined')
			{
				newTable.push({'key':this.table[x]['key'],'object':this.table[x]['object']});
			}
		}
		delete this.table;
		this.table = newTable;
		newTable = null;
	};
	function getUniqueKey ()
	{
		string = genRandString(32);
		bUnique = true;
		for (var x = 0; x < this.table.length; x++)
		{
			if (this.table[x]['key'] == string)
			bUnique = false;
		}
		if (bUnique)
			return string;

		return false;
	};
};

function genRandString (length)
{
	var pool = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ0123456789";
	var sReturn = "";

	for (var x = 0; x < length; x++)
	{
		i = Math.floor(Math.random() * 62);
		sReturn += pool.charAt(i);
	}

	return sReturn;
};

function incrementCheck(value,el,inv_saleincr){

	if(inv_saleincr ==0){inv_saleincr = 1;}
	if(value==0){return value;}

	if( value % inv_saleincr == 0 ){
		return value;

	}else{
		alert('Sorry this product must be purchased in multiples of '+inv_saleincr);

		for(var i=value; i>0; i--){
			//select next lowest multiple of inv_saleincr
			if(i % inv_saleincr ==0){
				el.value = i
				return false;
			}
		}
		el.value = inv_saleincr;
		return false;
	}

}

var onLoadFunctions = "";
function onContent(f) {
	onLoadFunctions += f + (f.charAt(f.length - 1) != ';' ? ';' : "");
};
/*
 *
 * ContentLoaded.js
 *
 * Author: Diego Perini (diego.perini at gmail.com)
 * Summary: Cross-browser wrapper for DOMContentLoaded
 * Updated: 20100801
 * License: MIT
 * Version: 1.1.1
 *
 * URL:
 * http://javascript.nwbox.com/ContentLoaded/
 * http://javascript.nwbox.com/ContentLoaded/MIT-LICENSE
 *
 * Notes:
 * based on code by Dean Edwards and John Resig
 * http://dean.edwards.name/weblog/2006/06/again/
 *
 */
// @w	window reference
// @f	function reference
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('2 B(w,f){j d=w.h,D=\'C\',u=w.E.F.G(),s=u.H(/.+(?:I|J|K|L|M|;)[\\/: ]([\\d.]+)/),v=s&&s[1]?N(s[1]):0;2 6(e){3(!h.i){h.i=O;f((e.7&&e.7==D)?e:{7:D,P:d,Q:0,R:d,S:+T U,V:e.7||e})}};3(/k\\//.5(u)&&v<l.13){(2(){3(/m|i/.5(d.n)){6(\'o-p\')}b{q(c.g,10)}})()}b 3(/r|W/.5(u)&&!w.t){d.X(\'Y\',2(e){3(d.n==\'m\'){d.Z(\'11\'+e.7,c.g);6(e)}});3(w==12){(2(){14{d.15.16(\'17\')}18(e){q(c.g,10);19}6(\'r-p\')})()}}b 3(d.x&&(/t\\//.5(u)&&v>9)||(/1a\\//.5(u)&&v>=1.8)||(/o\\//.5(u)&&v>=4.0)||(/k\\//.5(u)&&v>=l.13)){d.x(D,2(e){d.1b(D,c.g,y);6(e)},y)}b{j a=w.z;w.z=2(e){6(e||w.A);3(1c a==\'2\'){a(e||w.A)}}}};',62,75,'||function|if||test|init|type||||else|arguments||||callee|document|loaded|var|webkit|525|complete|readyState|khtml|poll|setTimeout|msie||opera||||addEventListener|false|onload|event|ContentLoaded|DOMContentLoaded||navigator|userAgent|toLowerCase|match|rv|it|ml|ra|ie|parseFloat|true|target|eventPhase|currentTarget|timeStamp|new|Date|eventType|playstation|attachEvent|onreadystatechange|detachEvent||on|top||try|documentElement|doScroll|left|catch|return|gecko|removeEventListener|typeof'.split('|'),0,{}))
ContentLoaded(window, function (e) {eval(onLoadFunctions)});

