var fDiscount;
var bDiscount;
var iEngine;
var iCuttingKit;
var aPriceWindow;
var aPrice			= new Array();
var aWidth;
var aHeight;
var aColors;

var oWidth;
var oHeight;
var oColor;
var oEngine;
var oEngineAlign;
var oCuttingKit;
var oWindows;
var oQuantity;
var oWebdoorTotalCost;

var oWebdoorView;
var oWebdoorInfo;

var sCurrency;

function vWindowOpen( sURL, iWidth, iHeight ) {
	var oOpen = window.open( sURL,"oOpen","width="+iWidth+",height="+iHeight );
}

function getAspNetFormElement(root, strType, strName) {
	// because in those asp.net forms, we dont know what the actual names is going to be on each form element
	// and because of that, we need to iterate throught all the forms -elements, but of the type specified
	var el = root.getElementsByTagName(strType);
	var objRetValue=null;
	for(var i=0;i<el.length;i++) {
		var strId = el[i].id;
		if(strId.indexOf(strName)>-1) {
			objRetValue = document.getElementById(strId);
			break;
		}
	}
	return objRetValue;
}

function vCalcPrice( sChange ) {
	
	var iColorIndex		= 0;
	for(var i=0;i<document.forms[0].Color.length;i++) {
		if(document.forms[0].Color[i].checked) iColorIndex = document.forms[0].Color[i].value;
	}
	
	var iWidthIndex		= parseInt(document.forms[0].Width.value);
	var iHeightIndex	= parseInt(document.forms[0].Height.value);
	var iPrice			= 0;
	
	var iQuantity		= 1;
	
	if(document.forms[0].Quantity.value.search(/^\d+$/)==-1) {
		alert("Quantity must be numeric");
		return;
	}
	
	iQuantity = parseInt(document.forms[0].Quantity.value);
	
	iPrice = aPrice[iWidthIndex][iHeightIndex];
	
	if(document.forms[0].CuttingKit.checked) {
		iPrice += iCuttingKit;
	}
	
	if(document.forms[0].Windows.checked)	iPrice += aPriceWindow[iWidthIndex];
	if(document.forms[0].Engine.checked) {
		document.forms[0].EngineAlign[0].disabled = false;
		document.forms[0].EngineAlign[1].disabled = false;
		iPrice += iEngine;
	}
	else {
		document.forms[0].EngineAlign[0].disabled = true;
		document.forms[0].EngineAlign[1].disabled = true;
	}
	/*
	if(fDiscount>0) {
		document.forms[0].TotalCost.value = Math.round(iPrice*iQuantity*fDiscount) + " " + sCurrency;
	}
	else {
		document.forms[0].TotalCost.value = Math.round(iPrice*iQuantity) + " " + sCurrency;
	}
	*/
	
	var fPrice = Math.round(iPrice*iQuantity);
	
	if(bDiscount) {
		document.getElementById("ShowPrice").innerHTML = Math.round(fPrice) + " " + sCurrency;
		document.getElementById("ShowDiscountPrice").innerHTML = Math.round(fPrice * (1.0 - fDiscount)) + " " + sCurrency;
	}
	else {
		document.getElementById("ShowPrice").innerHTML = Math.round(fPrice) + " " + sCurrency;
	}
	
	// Set flash
	
	switch(sChange) {
		case "Width":
			changeParameter('_global.sDoorWidth',	aWidth[iWidthIndex]);
			break;
		case "Height":
			changeParameter('_global.sDoorHeight',	aHeight[iHeightIndex]);
			break;
		case "Engine":
			if(document.forms[0].Engine.checked)	changeParameter('_global.sDoorElectricity','True');
			else	changeParameter('_global.sDoorElectricity','False');
			break;
		case "Windows":
			if(document.forms[0].Windows.checked)	changeParameter('_global.sDoorWindows','True');
			else	changeParameter('_global.sDoorWindows','False');
			break;
		case "Colors":
			changeParameter('_global.sDoorColor',aColors[iColorIndex]);
			break;
		case "EnginePlacement":
			if(document.forms[0].EngineAlign[0].checked)	changeParameter('_global.sDoorEnginePlacement',"Left");
			else											changeParameter('_global.sDoorEnginePlacement',"Right");
			break;
	}

	oWidth.value			= iWidthIndex;
	oHeight.value			= iHeightIndex;
	
	if(document.forms[0].CuttingKit.checked)	oCuttingKit.value = 1;
	else										oCuttingKit.value = 0;
	
	oColor.value			= iColorIndex;
	
	if(document.forms[0].Engine.checked)	oEngine.value = 1;
	else									oEngine.value = 0;
	
	if(document.forms[0].EngineAlign[0].checked)	oEngineAlign.value = 0;
	if(document.forms[0].EngineAlign[1].checked)	oEngineAlign.value = 1;
	
	if(document.forms[0].Windows.checked)	oWindows.value = 1;
	else									oWindows.value = 0;
	
	oQuantity.value			= document.forms[0].Quantity.value;
	
}

function initAspNetFields() {
	oWebdoorInfo		= getAspNetFormElement(document.forms[0], "input", "WebdoorInfo");
	oWebdoorView		= getAspNetFormElement(document.forms[0], "input", "WebdoorView");
	oWidth				= getAspNetFormElement(document.forms[0], "input", "WebdoorWidth");
	oHeight				= getAspNetFormElement(document.forms[0], "input", "WebdoorHeight");
	oCuttingKit			= getAspNetFormElement(document.forms[0], "input", "WebdoorCuttingKit");
	oColor				= getAspNetFormElement(document.forms[0], "input", "WebdoorColor");
	oEngine				= getAspNetFormElement(document.forms[0], "input", "WebdoorEngine");
	oEngineAlign		= getAspNetFormElement(document.forms[0], "input", "WebdoorAlign");
	oWindows			= getAspNetFormElement(document.forms[0], "input", "WebdoorWindows");
	oQuantity			= getAspNetFormElement(document.forms[0], "input", "WebdoorQuantity");
}

function changeParameter(sParameter, newValue){
	window.document.configure.SetVariable(sParameter, newValue);
	window.document.configure.GotoFrame(5);
	window.document.configure.Play();
}

function changeView( oValue ) {
	oWebdoorView.value = oValue;
}

function changeInfo( oValue ) {
	oWebdoorInfo.value = oValue;
}

