function formatcurrency(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return "$" + s;
}

function getElement(element) {
	if (document.getElementById) {
		return document.getElementById(element);
	}	
	else if (document.all) {
		return document.all(element);
	}
	else if (document.layers){
		return document.layers[element];
	}
	else {
		return null;
	}
}

function getDocElement(doc, element) {
	if (doc.getElementById) {
		return doc.getElementById(element);
	}	
	else if (doc.all) {
		return doc.all(element);
	}
	else if (doc.layers){
		return doc.layers[element];
	}
	else {
		return null;
	}
}

function trim ( s ) {
	s = s.replace( /^\s*/, "" );
	s = s.replace( /\s*$/, "" );
	return s;
}

function setSpanText(id, text) {
	var spanObj = getElement(id);
	spanObj.innerHTML = text;
}

function changeCountry(akey, aval, theform, prefix) {
	var elname = "document." + theform.name + "." + prefix + "state";
	var selobj = eval(elname);

	for(j=selobj.length; j >= 0; j--) {
		selobj.options[j] = null;
	}

	selobj.options.length = 0;
	for(i=0; i < akey.length; i++) {
		selobj.options[i] = new Option( aval[i], akey[i] );
	}
}

function borderit(which,color){
	//if IE 4+ or NS 6+
	if (document.all||document.getElementById){
		which.style.borderColor=color;
	}
}

function openwin(htmlFile, win_width, win_height) {
	thewin=open("","NewWindow","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width="+win_width+",height="+win_height);
	thewin.location = htmlFile;
}

function checkItemDetailForm(theform) {
	var haserror = false;
	for(i=0; i < theform.elements.length; i++) {
		if( theform.elements[i].name == "required" ) {
			ogroup = theform.elements[i].value;
			objLabel = getElement('LABEL' + ogroup);
			optionValue = null;
			for(j=0; j < theform.elements.length; j++) {
				if( theform.elements[j].name == "option[" + ogroup + "]" ) {
					optionValue = theform.elements[j].value;
					break;
				}
			}
			objMsg = getElement('MSG' + ogroup);
			if(objMsg != null) {
				objMsg.innerHTML = '';
			}

			if(optionValue != null) {
				if(optionValue == '') {
					haserror = true;
					if(objMsg != null) {
						objMsg.innerHTML = "Required";
					}
				}
			}
		}
	}
	if(!haserror) {
		theform.submit();
	}
}

function addressSetShippingSame(theform, el) {
	var aFields = Array('fname','lname','company','address1','address2','country','state','city','zip','phone');

	if(el.checked) {
		for(i=0; i < aFields.length; i++) {
			var billelement = 'document.' + theform.name + '.bill' + aFields[i];
			var shipelement = 'document.' + theform.name + '.ship' + aFields[i];
			var billobj = eval(billelement);
			var shipobj = eval(shipelement);
			if(billobj != null && shipobj != null) {
				shipobj.value = billobj.value;
			}
		}
	}

}

function addressSetShipping(theform, el) {
	var aFields = Array('addressid','fname','lname','company','address1','address2','city','state','zip','country','phone');
	if(el.value != '') {
		var aValues = el.value.split('|');
		for(i=1; i < aFields.length; i++) {
			var shipelement = 'document.' + theform.name + '.ship' + aFields[i];
			var shipobj = eval(shipelement);
			if(shipobj != null) {
				shipobj.value = aValues[i];
			}
		}
	}
}

function toggleDisplay(id) {
	obj = getElement(id);
	if(obj != null) {
		if( obj.style.display == '') {
			obj.style.display = 'none';
			//objImg = getElement('arrow'+id);
			//if(objImg != null) {
			//	objImg.src = "images/arrowUP.jpg";
			//}
		} else {
			obj.style.display = '';
			//objImg = getElement('arrow'+id);
			//if(objImg != null) {
			//	objImg.src = "images/arrowDOWN.jpg";
			//}
		}
	}

	for(i = 1; i < 99; i++) {
		obj = getElement('cat' + i);
		if(obj == null) {
			break;
		} else if( ('cat' + i) != id ) {
			obj.style.display = 'none';
			//objImg = getElement('arrowcat'+i);
			//if(objImg != null) {
			//	objImg.src = "images/arrowUP.jpg";
			//}
		}
	}
}

function validateMinOrd(minord, theelement) {
	var theval = trim (theelement.value);
	if(theval != '') {
		theval = parseFloat(theval);
		if(minord > 1) {
			if( theval % minord != 0 ) {
				alert('Must be purchased in Quantities of ' + minord);
				theelement.focus();
				return false;
			}
		}
	}
	return true;
}

if(typeof jQuery != 'undefined') {

	$(document).ready(function() {
		var dLeftHeight = $('#left-sidebar').height();
		var dRightHeight = $('#right-sidebar').height();
		var dMainHeight = $('#main').height();
	
		if(dLeftHeight != null && dRightHeight != null && dMainHeight != null) {
			var dLargest = dMainHeight;
			if(dLargest < dLeftHeight) {
				dLargest = dLeftHeight;
			}
			if(dLargest < dRightHeight) {
				dLargest = dRightHeight;
			}
		
			if(dLargest > 0 && dLargest != null) {
				$('#left-sidebar').css('height', dLargest);
				$('#right-sidebar').css('height', dLargest);
			}
		}
		
	});

}

