var StyleProperties = {
	accelerator: 'accelerator',
	background: 'background',
	backgroundAttachment: 'background-attachment',
	backgroundColor: 'background-color',
	backgroundImage: 'background-image',
	backgroundPosition: 'background-position',
	backgroundPositionX: 'background-position-x',
	backgroundPositionY: 'background-position-y',
	backgroundRepeat: 'background-repeat',
	behavior: 'behavior',
	border: 'border',
	borderBottom: 'border-bottom',
	borderBottomColor: 'border-bottom-color',
	borderBottomStyle: 'border-bottom-style',
	borderBottomWidth: 'border-bottom-width',
	borderCollapse: 'border-collapse',
	borderColor: 'border-color',
	borderLeft: 'border-left',
	borderLeftColor: 'border-left-color',
	borderLeftStyle: 'border-left-style',
	borderLeftWidth: 'border-left-width',
	borderRight: 'border-right',
	borderRightColor: 'border-right-color',
	borderRightStyle: 'border-right-style',
	borderRightWidth: 'border-right-width',
	borderSpacing: 'border-spacing',
	borderStyle: 'border-style',
	borderTop: 'border-top',
	borderTopColor: 'border-top-color',
	borderTopStyle: 'border-top-style',
	borderTopWidth: 'border-top-width',
	borderWidth: 'border-width',
	bottom: 'bottom',
	captionSide: 'caption-side',
	clear: 'clear',
	clip: 'clip',
	clipBottom: 'clip-bottom',
	clipLeft: 'clip-left',
	clipRight: 'clip-right',
	clipTop: 'clip-top',
	color: 'color',
	content: 'content',
	counterIncrement: 'counter-increment',
	counterReset: 'counter-reset',
	cssFloat: 'css-float',
	cssText: 'css-text',
	cursor: 'cursor',
	direction: 'direction',
	display: 'display',
	emptyCells: 'empty-cells',
	filter: 'filter',
	font: 'font',
	fontFamily: 'font-family',
	fontSize: 'font-size',
	fontSizeAdjust: 'font-size-adjust',
	fontStretch: 'font-stretch',
	fontStyle: 'font-style',
	fontVariant: 'font-variant',
	fontWeight: 'font-weight',
	height: 'height',
	imeMode: 'ime-mode',
	layoutGrid: 'layout-grid',
	layoutGridChar: 'layout-grid-char',
	layoutGridLine: 'layout-grid-line',
	layoutGridMode: 'layout-grid-mode',
	layoutGridType: 'layout-grid-type',
	left: 'left',
	letterSpacing: 'letter-spacing',
	lineBreak: 'line-break',
	lineHeight: 'line-height',
	listStyle: 'list-style',
	listStyleImage: 'list-style-image',
	listStylePosition: 'list-style-position',
	listStyleType: 'list-style-type',
	margin: 'margin',
	marginBottom: 'margin-bottom',
	marginLeft: 'margin-left',
	marginRight: 'margin-right',
	marginTop: 'margin-top',
	markerOffset: 'marker-offset',
	marks: 'marks',
	maxHeight: 'max-height',
	maxWidth: 'max-width',
	minHeight: 'min-height',
	minWidth: 'min-width',
	MozOpacity: '-moz-opacity',
	orphans: 'orphans',
	outline: 'outline',
	outlineColor: 'outline-color',
	outlineStyle: 'outline-style',
	outlineWidth: 'outline-width',
	overflow: 'overflow',
	overflowX: 'overflow-x',
	overflowY: 'overflow-y',
	padding: 'padding',
	paddingBottom: 'padding-bottom',
	paddingLeft: 'padding-left',
	paddingRight: 'padding-right',
	paddingTop: 'padding-top',
	page: 'page',
	pageBreakAfter: 'page-break-after',
	pageBreakBefore: 'page-break-before',
	pageBreakInside: 'page-break-inside',
	pixelBottom: 'pixel-bottom',
	pixelHeight: 'pixel-height',
	pixelLeft: 'pixel-left',
	pixelRight: 'pixel-right',
	pixelTop: 'pixel-top',
	pixelWidth: 'pixel-width',
	posBottom: 'pos-bottom',
	posHeight: 'pos-height',
	position: 'position',
	posLeft: 'pos-left',
	posRight: 'pos-right',
	posTop: 'pos-top',
	posWidth: 'pos-width',
	quotes: 'quotes',
	right: 'right',
	rubyAlign: 'ruby-align',
	rubyOverhang: 'ruby-overhang',
	rubyPosition: 'ruby-position',
	size: 'size',
	styleFloat: 'style-float',
	tableLayout: 'table-layout',
	textAlign: 'text-align',
	textAlignLast: 'text-align-last',
	textAutospace: 'text-autospace',
	textDecoration: 'text-decoration',
	textDecorationBlink: 'text-decoration-blink',
	textDecorationLineNone: 'text-decoration-line-none',
	textDecorationLineOverline: 'text-decoration-line-overline',
	textDecorationLineThrough: 'text-decoration-line-through',
	textDecorationLineUnderline: 'text-decoration-line-underline',
	textIndent: 'text-indent',
	textJustify: 'text-justify',
	textJustifyTrim: 'text-justify-trim',
	textKashidaSpace: 'text-kashida-space',
	textOverflow: 'text-overflow',
	textShadow: 'text-shadow',
	textTransform: 'text-transform',
	textUnderlinePosition: 'text-underline-position',
	top: 'top',
	unicodeBidi: 'unicode-bidi',
	verticalAlign: 'vertical-align',
	visibility: 'visibility',
	whiteSpace: 'white-space',
	widows: 'widows',
	width: 'width',
	width: 'width',
	wordBreak: 'word-break',
	wordSpacing: 'word-spacing',
	wordWrap: 'word-wrap',
	writingMode: 'writing-mode',
	zIndex: 'z-index',
	zoom: 'zoom'
}

function debugObject(o) {
	var k = null;
	var s = '';
	for (k in o) {
		if (o[k] && (typeof o[k] != 'function')) {
			s += k + ': ' + o[k] + '\n';
		}
	}
	alert(s);
}

function debugStyle(o) {
	var k = null;
	var r = null;
	var s = '';
	if (o.style) {
		for(k in StyleProperties) {
			r = getStyle(o, k);
			if (r) {
				s += k + ': ' + r + '\n';
			}
		}
	}
	alert(s);
}

function getStyleProperty(obj, property) {
	if (StyleProperties[property]) {
		if (window.getComputedStyle) {
			property = StyleProperties[property];
			return window.getComputedStyle(obj, null).getPropertyValue(property);
		} else if (obj.currentStyle && obj.currentStyle[property]) {
			return obj.currentStyle[property];
		}
	}
	return null;
}

function getStyle(from) {
	var key = null;
	var newStyle = {};
	var r = null;
	if (from.style) {
		for (key in StyleProperties) {
			if (r = getStyleProperty(from, key)) {
				newStyle[key] = r;
			}
		}
	}
	return newStyle;
}

function initJS() {
	from = document.getElementById('SiteTitle');
	to = document.getElementById('SiteTitleInput');
	if (from && to) {
		// f = getStyle(from);
		// t = getStyle(to);
		f = from;
		t = to;
		s = {};
		for(k in f) {
			if (typeof f[k] != 'function' && typeof t[k] != 'function' && f[k] != t[k]) {
				s[k] = f[k] + ' : ' + t[k];
			}
		}
		debugObject(s);
	}
	/*
	to.style.borderBottom = '1px dashed #cccccc;';
	to.style.backgroundColor = '#f8f8f8;';
	// to.style.display = 'table';
	to.style.width = 'auto';
	to.style.height = 'auto';
	//to.style.
	// div.autosize { display: table; width: 1px; }
	// div.autosize > div { display: table-cell; }
	*/
}

toggleSettings = new CookieUtil('toggleset', 129600, "/");
openCategories = new Object();
allCategories = new Object();
currentCategory = 0;
categoryTreeBaseURL = '';
rootCategory = null;

function Category(aId, aParent, aName, aCount, isItemCat) {
	this.id = aId;
	this.parentId = aParent;
	this.parent = null;
	this.name = aName;
	this.activeCount = aCount;
	this.isItemCategory = isItemCat;
	this.numChildren = 0;
	this.children = [];
	this.childrenList = new Object();
}


Category.prototype.registerWithParent = function() {
	this.parent = allCategories[this.parentId];
	if (this.parent) {
		if (this.parent == this) {
			rootCategory = this;
			this.parent = null;
			return;
		}
		this.parent.registerChild(this);
	}
}
	
Category.prototype.registerChild = function(child) {
	if (this.childrenList[child.id] == undefined) {
		this.children.push(child);
		this.childrenList[child.id] = this.children.length - 1;
		this.numChildren++;
	} else {
		// update child in existing position
		var cp = this.childrenList[child.id];
		this.children[cp] = child;
	}
}

Category.prototype.getHTML = function(level) {
	var childHTML = '';
	var cid;
	if (!this.isItemCategory) {
		for (cid in this.children) {
			childHTML += this.children[cid].getHTML(level + 1);
		}
	}
	
	var countContent = (
				'<span class="catItemCount">('
				+ this.activeCount
				+ ')</span>'
			);
	
	if (currentCategory == this.id) {
		var link = (
				'<strong>'
				+ this.name
				+ '</strong>&nbsp;'
				+ countContent
			);
	} else {
		var linkUrl = (this.isItemCategory) ? '/admin.cfm?page=item' : categoryTreeBaseURL;
		var link = (
				'<a href="javascript:void;" onclick="location.href=\''
				+ linkUrl
				+ '&catId='
				+ this.id
				+ '\'; return cancelEvent(event);">'
				+ this.name
				+ '</a>&nbsp;'
				+ countContent
			);
	}
	if (this.id > 0) {
		if (childHTML == '' || this.isItemCategory) {
			return (
				'<span id="cat_'
				+ this.id
				+ '" class="cat_empty" onclick="return cancelEvent(event);">'
				+ link
				+ (this.isItemCategory ? '<span class="catItemCount">i</span>' : '')
				+ '<span id="sub_'
				+ this.id
				+ '" class="sub"></span></span>'
			);
		} else {
    	    return (
				'<span id="cat_'
				+ this.id
				+ '" class="cat" onclick="return toggleCategory(event, this, '
				+ this.id
				+ ');">'
				+ link
				+ '<span id="sub_'
				+ this.id
				+ '" class="sub"><br />'
				+ childHTML
				+ '</span></span>'
			);
		}
	} else {
		return childHTML;
	}
	return '';
}

function toggleInit() {
	var i = 0;
	var id = 0;

	// get list of open categories
	var t = toggleSettings.getSubValue('categories');
	if (t == undefined) {
		t = '';
		toggleSettings.setSubValue('categories', t);
	}
	t = t.split(',');
	for (i = 0; i < t.length; i++) {
		openCategories[t[i]] = true;
	}
	
	// loop through list of categories and paint them
	for(id in allCategories) {
		allCategories[id].registerWithParent();
		if (id == 0) {
			rootCategory = allCategories[id];
		}
	}
	
	var c = document.getElementById('CategoryTree');
	c.innerHTML = rootCategory.getHTML(1);
	
	for(id in allCategories) {
		// if not open, toggle to open, the close if not in list of open categories
		var d = document.getElementById('cat_' + id);
		toggleCategory(null, d, id, true);
		if (!openCategories[id]) {
			toggleCategory(null, d, id, true);
		}
	}

	// remove from list of open categories the ones that don't exist anymore
	for(id in openCategories) {
		if (!allCategories[id]) {
			delete openCategories[id];
		}
	}
	storeCategoryToggleSet();

}

function openCategory(id) {
	// alert('opening ' + id);
	openCategories[id] = true;
	storeCategoryToggleSet();
}

function closeCategory(id) {
	// alert('closing ' + id);
	delete openCategories[id];
	storeCategoryToggleSet();
}

function storeCategoryToggleSet() {
	var l = [];
	for (var id in openCategories) {
		l.push(id);
	}
	// alert('storing: ' + l.join(','));
	toggleSettings.setSubValue('categories', l.join(','));
}

function toggleCategory(event, el, category, avoidStorage) {
	var s = document.getElementById('sub_' + category);
	if (s) {
		if (s.style) {
			if (s.isExpanded) {
				if (el) {
					removeClass(el, 'openCat');
				}
				s.style.display = 'none';
				s.isExpanded = false;
				if (!avoidStorage) {
					closeCategory(category);
				}
			} else {
				if (el) {
					addClass(el, 'openCat');
				}
				s.style.display = 'inline';
				s.isExpanded = true;
				if (!avoidStorage) {
					openCategory(category);
				}
			}
		}
	}
	return cancelEvent(event);
}

function forceOpen(category) {
	if (!openCategories[category]) {
		// alert('about to open: ' + category);
		var d = document.getElementById('cat_' + category);
		toggleCategory(null, d, category);
	} else {
		// alert('was open: ' + category);
	}
}

function cancelEvent(event) {
	if (event != null) {
		if (event.preventDefault) {
			event.preventDefault();
		}
		if (event.stopPropagation) {
			event.stopPropagation();
		}
		event.returnValue = false;
		event.cancelBubble = true;
	}
	return false;
}

function hasClass(target, aclass) {
	if (target.className == null) {
		return false;
	}
	var classes = target.className.split(" ");
	for (var i = 0;  i< classes.length; i++) {
		if(classes[i] == aclass) {
			return true;
		}
	}
	return false;
}

function addClass(target, aclass) {
	if (hasClass(target, aclass)) {
		return;
	}
	target.className += " " + aclass;
	// debug('added class [' + aclass + '] to ' + target.id);
}

function removeClass(target, aclass) {
	if (target.className == null) {
		return;
	}
	var classes = target.className.split(" ");
	var result = new Array();
	for (var i = 0; i < classes.length; i++) {
		if(classes[i] != aclass) {
			result[result.length]=classes[i];
		}
	}
	target.className = result.join(" ");
}

function focusDefault(formName, fieldId) {
	if (document.forms && document.forms[formName] && document.forms[formName][fieldId]) {
		document.forms[formName][fieldId].focus();
		if (document.forms[formName][fieldId].select) {
			document.forms[formName][fieldId].select();
		}
	}
}

/**
 * Copyright (C) 2002-2003, CodeHouse.com. All rights reserved.
 * CodeHouse(TM) is a registered trademark.
 *
 * THIS SOURCE CODE MAY BE USED FREELY PROVIDED THAT
 * IT IS NOT MODIFIED OR DISTRIBUTED, AND IT IS USED
 * ON A PUBLICLY ACCESSIBLE INTERNET WEB SITE.
 * 
 * CodeHouse.com JavaScript Library Module: Cookie Utility Class
 *
 * You can obtain this script at http://www.codehouse.com
 */
function CookieUtil(name, duration, path, domain, secure) {
   this.affix = "";
   if( duration ) {   	  
      var date = new Date();
	  var curTime = new Date().getTime();
	  date.setTime(curTime + (1000 * 60 * duration));
	  this.affix = "; expires=" + date.toGMTString();
   }
   if( path ) this.affix += "; path=" + path;
   if( domain ) this.affix += "; domain=" + domain;
   if( secure ) this.affix += "; secure=" + secure;
      
   function getValue() {
      var m = document.cookie.match(new RegExp("(" + name + "=[^;]*)(;|$)"));
      return m ? m[1] : null;   
   }
   
   this.cookieExists = function() {
      return getValue() ? true : false;
   }
      
   this.expire = function() {
      var date = new Date();
	  date.setFullYear(date.getYear() - 1);
	  document.cookie=name + "=noop; expires=" + date.toGMTString(); 
   }
        
   this.setSubValue = function(key, value) {
      var ck = getValue();
      if( /[;, ]/.test(value) ) {
         //Mac IE doesn't support encodeURI
		 value = window.encodeURI ? encodeURI(value) : escape(value);
      }
      if( value ) {
         var attrPair = "@" + key + value;
         if( ck ) {
             if( new RegExp("@" + key).test(ck) ) {
		        document.cookie = ck.replace(new RegExp("@" + key + "[^@;]*"), attrPair) + this.affix;
	         } else {
		        document.cookie = ck.replace(new RegExp("(" + name + "=[^;]*)(;|$)"), "$1" + attrPair) + this.affix;
	         }
         } else {
	        document.cookie = name + "=" + attrPair + this.affix;
         }
      } else {      
	     if( new RegExp("@" + key).test(ck) ) {
	        document.cookie = ck.replace(new RegExp("@" + key + "[^@;]*"), "") + this.affix;
	     }
      }
   }

      
   this.getSubValue = function(key) {
      var ck = getValue();
      if( ck ) {
         var m = ck.match(new RegExp("@" + key + "([^@;]*)"));
	     if( m ) {
	        var value = m[1];
	        if( value ) { 
	           //Mac IE doesn't support decodeURI
			   return window.decodeURI ? decodeURI(value) : unescape(value);
	        }
	     }
      }
   }
}

PaymentSets = {
	methodDropDownRef: null,
	methodList: {},
	add: function(method) {
		this.methodList[method.methodName] = method;
	},
	update: function(toValue) {
		if (!toValue) {
			if (!this.methodDropDownRef) {
				this.methodDropDownRef = document.getElementById('method');
			}
			if (!this.methodDropDownRef) {
				return false;
			}
			toValue = this.methodDropDownRef.value;
		}
		for(var i in this.methodList) {
			if (i != toValue) {
				this.methodList[i].disable();
			}
		}
		if (this.methodList[toValue]) {
			this.methodList[toValue].enable();
		}
	}
};

PaymentSet = function(
        methodName,
		externalLabel,
		externalText,
		securityLabel,
		securityText,
		expiresLabel,
		expiresText,
		showExpires,
		showElementIDs
) {

    this.methodName = methodName;	
	this.externalLabel = externalLabel;
	this.externalText = externalText;
	this.securityLabel = securityLabel;
	this.securityText = securityText;
	this.expiresLabel = expiresLabel;
	this.expiresText = expiresText;
	this.showExpires = showExpires;
	this.showElementIDs = showElementIDs;
	this.init();
	
};

PaymentSet.prototype.init = function() {
    PaymentSets.add(this);
    this.externalLabelRef = document.getElementById('externalLabel');
    this.externalTextRef = document.getElementById('externalText');
    this.securityLabelRef = document.getElementById('securityLabel');
    this.securityTextRef = document.getElementById('securityText');
    this.expiresLabelRef = document.getElementById('expiresLabel');
    this.expiresTextRef = document.getElementById('expiresText');
    // add the expires div to list of things to show or hide, if requested
    if (this.showExpires) {
    	this.showElementIDs.push('expiresDiv');
    }
    this.showElementsRef = {};
    for(var i in this.showElementIDs) {
    	this.showElementsRef[this.showElementIDs[i]] = document.getElementById(this.showElementIDs[i]);
    }
    this.disable();
};
PaymentSet.prototype.setElementsDisplay = function(displayValue) {
	var el = null;
	for(var i in this.showElementsRef) {
		el = this.showElementsRef[i];
		if (el && el.style) {
			el.style.display = displayValue;
		}
	}
};
PaymentSet.prototype.disable = function() {
	this.setElementsDisplay('none');
};
PaymentSet.prototype.enable = function() {
    this.externalLabelRef.innerHTML = this.externalLabel + ':';
    this.externalTextRef.innerHTML = this.externalText;
    this.securityLabelRef.innerHTML = this.securityLabel + ':';
    this.securityTextRef.innerHTML = this.securityText;
    this.expiresLabelRef.innerHTML = this.expiresLabel + ':';
    this.expiresTextRef.innerHTML = this.expiresText;
	this.setElementsDisplay('block');
};

