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 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 = "<br>Required";
					}
				}
			}
		}
	}
	if(!haserror) {
		theform.submit();
	}
}

var gBasePrice = 0;
var gOtherOptionTotal = 0;
var gCheckboxOptionTotal = 0;
function setPrice() {
	var upgrades = parseFloat(gOtherOptionTotal) + parseFloat(gCheckboxOptionTotal);
	var total = parseFloat(gBasePrice) + parseFloat(upgrades);
	var divHtml = "<b class=red>Upgrades:</b> " + formatcurrency(upgrades);
	var divHtml = divHtml + "<br><b class=red>Boat Total:</b> " + formatcurrency(total);
	setSpanText('divPrice', divHtml);
}

function onChangeOptionSetPrice(el) {
	// this function should really loop through all select options
	var optionText = el.options[el.selectedIndex].text;
	//alert(el.options[el.selectedIndex].text);

	var charStart = optionText.indexOf('$');
	var charEnd = optionText.indexOf(')');

	var price = optionText.substring(charStart+1, charEnd);
	if( price == "") {
		price = 0;
	}

	gOtherOptionTotal = parseFloat(price);
	setPrice();
}

function onClickCheckboxSetPrice(el, price) {
	if(price != '') {
		price = parseFloat(price);
	} else {
		price = 0;
	}
	if(el.checked) {
		gCheckboxOptionTotal += price;
	} else {
		gCheckboxOptionTotal -= price;
	}
	setPrice();
}

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;
			}
		}
	}

}

