function init() {
	var defaultItem = 0;
	// Initialize the dropdown for Shutter Style
	var selectStyle = document.forms[formName].sunbeltcart_1_sbsStyle_Add;
	selectStyle.onchange=calcTotal;
	selectStyle.options.length=0;
	for(var i=0; i<sbs_styleList.length; i++) {
		sbs_style[sbs_styleList[i][0]] = i;
		if(sbs_styleList[i][3]=="True") {
			defaultItem = i;
		}
		selectStyle.options[i] = new Option(sbs_styleList[i][1],sbs_styleList[i][0]);
	}
	selectStyle.options[defaultItem].selected = true;

	// Initialize the shutter length change event
	var setLength = document.forms[formName].sunbeltcart_1_sbsLength_Add;
	setLength.value = minLengthInches;
	setLength.onkeypress = blockEnter;
	setLength.onkeyup = validate;	
	setLength.onchange = hardValidate;

	// Initialize the shutter width change event
	var setWidth = document.forms[formName].sunbeltcart_1_sbsWidth_Add;
	setWidth.value = minWidthInches;
	setWidth.onkeypress = blockEnter;
	setWidth.onkeyup = validate;
	setWidth.onchange = hardValidate;

	// Initialize the shutter fraction change events
	var setLengthFraction = document.forms[formName].sunbeltcart_1_sbsLengthFraction_Add;
	var setWidthFraction = document.forms[formName].sunbeltcart_1_sbsWidthFraction_Add;
	setLengthFraction.options.length=0;
	setWidthFraction.options.length=0;
	for(var i=0; i<fractionInches; i++) {
		var numer = i;
		var denom = fractionInches;
		var gcd_div = gcd(denom, numer);
		if (gcd_div>1) {
			numer = numer / gcd_div;
			denom = denom / gcd_div;
		}
		optVal = numer / denom;
		if (numer==0) {
			optText = '0"';
		} else {
			optText = numer + '/' + denom + '"';
		}
		setLengthFraction.options[i] = new Option(optText,optVal);
		setWidthFraction.options[i] = new Option(optText,optVal);
	}
	setLengthFraction.onchange = validate;
	setWidthFraction.onchange = validate;

	// Initialize the dropdown for Shutter Wood Type
	defaultItem = 0;
	var selectWood = document.forms[formName].sunbeltcart_1_sbsWood_Add;
	selectWood.onchange=calcTotal;
	selectWood.options.length=0;
	for(var i=0; i<sbs_woodList.length; i++) {
		sbs_wood[sbs_woodList[i][0]] = i;
		if(sbs_woodList[i][3]=="True") {
			defaultItem = i;
		}
		selectWood.options[i] = new Option(sbs_woodList[i][1],sbs_woodList[i][0], sbs_woodList[i][3]);
	}
	selectWood.options[defaultItem].selected = true;

	// Initialize the dropdown for Shutter Finish
	defaultItem = 0;
	var selectFinish = document.forms[formName].sunbeltcart_1_sbsFinish_Add;
	selectFinish.onchange=calcTotal;
	selectFinish.options.length=0;
	for(var i=0; i<sbs_finishList.length; i++) {
		sbs_finish[sbs_finishList[i][0]] = i;
		if(sbs_finishList[i][3]=="True") {
			defaultItem = i;
		}
		selectFinish.options[i] = new Option(sbs_finishList[i][1],sbs_finishList[i][0], sbs_finishList[i][3]);
	}
	selectFinish.options[defaultItem].selected = true;


	// Initialize the dropdown for Shutter Options
	defaultItem = 0;
//	var selectOptions = document.forms[formName].sunbeltcart_1_sbsOptions_Add;
	var otherOptions = document.getElementById("otherOptions");
	var otherOptionsHTML = "";
//	selectOptions.onchange=calcTotal;
//	selectOptions.options.length=0;
	document.getElementById("otherOptionsCount").value = sbs_optionsList.length;
	for(var i=0; i<sbs_optionsList.length; i++) {
		sbs_options[sbs_optionsList[i][0]] = i;
		if(sbs_optionsList[i][3]=="True") {
			defaultItem = i;
		}
//		selectOptions.options[i] = new Option(sbs_optionsList[i][1],sbs_optionsList[i][0], sbs_optionsList[i][3]);
		
		otherOptionsHTML = otherOptionsHTML + "<input type='checkbox' name='otherOptions_" + i + "' id='otherOptions_" + i + "' value='" + sbs_optionsList[i][1] + "' onClick='javascript: calcTotal()'";
		if(sbs_optionsList[i][3]=="True") {
			otherOptionsHTML = otherOptionsHTML + " checked";	
		}
		otherOptionsHTML = otherOptionsHTML + ">&nbsp;<label for='otherOptions_" + i + "'>" + sbs_optionsList[i][1] + "</label><br>";
	}
	otherOptions.innerHTML = otherOptionsHTML;
//	selectOptions.options[defaultItem].selected = true;


//	initCaps();

	// Initialize the shutter quantity
	var setQuantity = document.forms[formName].sunbeltcart_1_Quantity_Add;
	setQuantity.onkeypress = blockEnter;	
	setQuantity.onkeyup = validate;
	setQuantity.onchange = hardValidate;
	setQuantity.value=1;

	calcTotal();	

}

function initCaps() {
//	alert(document.getElementById("caps").innerHTML);
	var defaultItem = 0;

	// Initialize the dropdown for Shutter Style
	var selectCaps = document.forms[formName].sunbeltcart_1_sbsCaps_Add;
	selectCaps.onchange=calcTotal;
	selectCaps.options.length=0;
//	var currentStyle = document.forms[formName].sunbeltcart_1_sbsStyle_Add.value;	

//	if(currentStyle!="style4") {
		for(var i=0; i<sbs_capsList.length; i++) {
			sbs_caps[sbs_capsList[i][0]] = i;
			if(sbs_capsList[i][3]=="True") {
				defaultItem = i;
			}
			selectCaps.options[i] = new Option(sbs_capsList[i][1],sbs_capsList[i][0]);
		}
		selectCaps.options[defaultItem].selected = true;
//	} else {
//		selectCaps.options.length=0;
//		selectCaps.options[0] = new Option("Caps not available","no-caps");
//	}
}

function calcTotal() {
	document.productImage.src = "images/productShots/" + document.forms[formName].sunbeltcart_1_ID_Add.value + '/' + document.forms[formName].sunbeltcart_1_sbsStyle_Add.value +".jpg"

	var styleVal = parseFloat(sbs_styleList[sbs_style[document.forms[formName].sunbeltcart_1_sbsStyle_Add.value]][2]);

	var lengthInches = parseInt(document.forms[formName].sunbeltcart_1_sbsLength_Add.value);
	var lengthInchesFraction = parseFloat(document.forms[formName].sunbeltcart_1_sbsLengthFraction_Add.value);
	var length = lengthInches + lengthInchesFraction;

	var widthInches = parseInt(document.forms[formName].sunbeltcart_1_sbsWidth_Add.value);
	var widthInchesFraction = parseFloat(document.forms[formName].sunbeltcart_1_sbsWidthFraction_Add.value);
	var width = widthInches + widthInchesFraction;

	if(length>=oversizeShippingThreshold || width>=oversizeShippingThreshold) {
		document.getElementById("freeShip").style.display='none';
	} else {
		document.getElementById("freeShip").style.display='inline';
	}

//    var capsVal = parseFloat(sbs_capsList[sbs_caps[document.forms[formName].sunbeltcart_1_sbsCaps_Add.value]][2]);


	var woodVal = parseFloat(sbs_woodList[sbs_wood[document.forms[formName].sunbeltcart_1_sbsWood_Add.value]][2]);
	var finishVal = parseFloat(sbs_finishList[sbs_finish[document.forms[formName].sunbeltcart_1_sbsFinish_Add.value]][2]);
	var quantity = parseInt(document.forms[formName].sunbeltcart_1_Quantity_Add.value);
	var optionsVal = 0.0;

	for(var i=0; i<sbs_optionsList.length; i++) {
		if(document.getElementById("otherOptions_" + i).checked) {
			switch (i) {
			case 0:
				optionsVal = optionsVal + parseFloat(sbs_optionsList[i][2]) * width;
				break;
			case 1:
				optionsVal = optionsVal + parseFloat(sbs_optionsList[i][2]);
				break;
			default:
				break;
			}
		}
	}

	// Original formula
//	var unitPrice = ( basePrice + ( (styleVal * (length * width / 144) * woodVal) ) + capsVal + optionsVal + finishVal);
	var unitPrice = ( basePrice + ( (styleVal * (length * width / 144) * woodVal) ) + optionsVal + finishVal);
	// Round and format to 2 decimal places
	unitPrice = (Math.round(unitPrice*100)/100).toFixed(2);
	var totalPrice = (Math.round(unitPrice*quantity*100)/100).toFixed(2);
	var suggPrice = ( totalPrice/( 1-(discount/100) ) ).toFixed(2);
  
	document.forms[formName].sunbeltcart_1_Price_Add.value=unitPrice;
	document.getElementById('sugg_Retail').innerHTML = "&nbsp;$" + suggPrice;
	document.getElementById('your_Savings').innerHTML = "&nbsp;" + discount + "%";
	document.getElementById('total_Price').innerHTML = "&nbsp;$" + totalPrice;
	setDesc();
}

function setDesc() {
  
	var style = sbs_styleList[sbs_style[document.forms[formName].sunbeltcart_1_sbsStyle_Add.value]][1];
	var length = parseInt(document.forms[formName].sunbeltcart_1_sbsLength_Add.value) + parseFloat(document.forms[formName].sunbeltcart_1_sbsLengthFraction_Add.value);
	var width = parseInt(document.forms[formName].sunbeltcart_1_sbsWidth_Add.value) + parseFloat(document.forms[formName].sunbeltcart_1_sbsWidthFraction_Add.value);
	var wood = sbs_woodList[sbs_wood[document.forms[formName].sunbeltcart_1_sbsWood_Add.value]][1];
	var finish = sbs_finishList[sbs_finish[document.forms[formName].sunbeltcart_1_sbsFinish_Add.value]][0];
	var options = "" 
	//sbs_optionsList[sbs_options[document.forms[formName].sunbeltcart_1_sbsOptions_Add.value]][0];

	for(var i=0; i<sbs_optionsList.length; i++) {
		if(document.getElementById("otherOptions_" + i).checked) {
//			alert(sbs_optionsList[i][1]);
			options = options + "<li>" + sbs_optionsList[i][1] + "</li>";
		}
	}
	if (options=="") {
		options="None";
	}

	var desc = '';
	desc = desc + 'style: ' + style + '<br>';
	desc = desc + 'dimensions(wid x len):' + width + '" x ' + length + '"<br>';
	desc = desc + 'wood: ' + wood +  '<br>';
	desc = desc + 'finish: ' + finish + '<br>';
	if(options!='') {
		desc = desc + 'other options:<br>';
		desc = desc + '<ul>' + options + '</ul>';
	}		
	desc = desc + 'quantity specified is in single units';
  document.forms[formName].sunbeltcart_1_Description_Add.value = desc;
}

function validate() {
//	alert("calling validate function!");
	validateQuantity(false);
	validateLength(false);
	validateWidth(false);
	calcTotal();
}

function hardValidate() {
//	alert("calling hard validate function!");
	validateQuantity(true);
	validateLength(true);
	validateWidth(true);
	calcTotal();
}


function validateQuantity(isForce) {
	var myObj = document.forms[formName].sunbeltcart_1_Quantity_Add;

	if(!isForce) {
		if(myObj.value.length==0 || myObj.value==null) {
			return;
		}
	}

	if(isNaN(myObj.value)) {
//		alert("The quantity must be a valid value.");
//		alert(myObj.value.length);
		if((myObj.value.length)<2) {
			myObj.value = 1;
		} else {
			myObj.value = left(myObj.value, myObj.value.length-1);
		}
		return;
	}

	var qty = parseInt(myObj.value);
	if(qty < 1) {
		alert("Minimum valid quantity is 1 pair of shutters");
		myObj.value = 1;
		return;
	}
}

function validateLength(isForce) {
	var myObj = document.forms[formName].sunbeltcart_1_sbsLength_Add;
	var myObj1 = document.forms[formName].sunbeltcart_1_sbsLengthFraction_Add;

	if(!isForce) {
		if(myObj.value.length==0 || myObj.value==null) {
			return;
		}
	}

	if(isNaN(myObj.value) ) {
		alert("The quantity must be a valid integer greater than zero.");
		if((myObj.value.length)<2) {
			myObj.value = 1;
		} else {
			myObj.value = left(myObj.value, myObj.value.length-1);
		}
		return;
	}

	if(myObj.value<=0) {
		alert("The quantity must be a number greater than zero.");
		myObj.value = minLengthInches;
		return;
	}


	var lengthInches = parseInt(myObj.value);
	var lengthInchesFraction = parseFloat(myObj1.value);
	var length = lengthInches + lengthInchesFraction;

	if(length > parseFloat(maxLengthInches)) {
		alert("Maximum valid shutter length is " + maxLengthInches + " inches. Length will be set to this maximum.");
		myObj.value = maxLengthInches;
		myObj1.value = 0;
	}
	if((length < parseFloat(minLengthInches)) & isForce) {
		alert("Minimum valid shutter length is " + minLengthInches + " inches. Length will be set to this minimum.");
		myObj.value = minLengthInches;
		myObj1.value = 0;
	}
}

function validateWidth(isForce) {
	var myObj = document.forms[formName].sunbeltcart_1_sbsWidth_Add;
	var myObj1 = document.forms[formName].sunbeltcart_1_sbsWidthFraction_Add;

	if(!isForce) {
		if(myObj.value.length==0 || myObj.value==null) {
			return;
		}
	}

	if(isNaN(myObj.value) ) {
		alert("The quantity must be a valid integer greater than zero.");
		if((myObj.value.length)<2) {
			myObj.value = 1;
		} else {
			myObj.value = left(myObj.value, myObj.value.length-1);
		}
		return;
	}

	if(myObj.value<=0) {
		alert("The quantity must be a number greater than zero.");
		myObj.value = minLengthInches;
		return;
	}


	var widthInches = parseInt(myObj.value);
	var widthInchesFraction = parseFloat(myObj1.value);
	var width = widthInches + widthInchesFraction;

	if(width > parseFloat(maxWidthInches)) {
		alert("Maximum valid shutter width is " + maxWidthInches + " inches. Width will be set to this maximum.");
		myObj.value = maxWidthInches;
		myObj1.value = 0;
	}
	if((width < parseFloat(minWidthInches)) & isForce) {
		alert("Minimum valid shutter width is " + minWidthInches + " inches. Width will be set to this minimum.");
		myObj.value = minWidthInches;
		myObj1.value = 0;
	}
}

function blockEnter(e) {
	// Traps the enter key so we don't jump to 
	// the cart page
	var charNum;
	if(window.event) {
		charNum = e.keyCode;
	} else if (e.which) {
		charNum = e.which;
	}
//	alert(charNum);
	if(charNum==13) {	
		void(0);
		return false;
	}
	if(charNum==9) {
		validate();
	}

	if((charNum<48 || charNum>57) && charNum!=8 && charNum!=9) {	
		void(0);
		return false;
	}
}


function gcd(a, b) {
	if (a==b) return a;
	var x;
	var y;
	var r;
	if (a>b) {
		x = a;
		y = b;
	} else { 
	    x = b;
		y = a;
	}
	if (y==0) return x;
	r = x % y;
	return gcd(y, r);
}

function left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

