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 formatcurrency2(amount) {
	// uses jquery plugin jquery.currency.min.js
	return '$' + $.currency(amount);
}

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;
	thewin.focus();
}

function cvv2info() {
	msg =       "If you are using a Visa or MasterCard, please provide the 3-digit CVV\n";
	msg = msg + "(Customer Verification Value). This is the number printed on\n";
	msg = msg + "the signature area on the back of the card following the Visa\n"; 
	msg = msg + "or MC card account number.\n\n";
	
	msg = msg + "If your credit card is an American Express card please provide the\n";
	msg = msg + "4-digit CID (Confidential Identifier Number). This is the 4 digit number\n";
	msg = msg + "printed above your account number on the front of your card.\n";

	alert(msg);
}

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;
			}
		}
	}

}
