var oWindow = null;

if (typeof( iFITTEDWINPRICE ) == 'undefined'){
	var iFITTEDWINPRICE	= 120;
}
if (typeof( iFITTEDDOORPRICE ) == 'undefined'){
	var iFITTEDDOORPRICE = 124;
}
var lIsDIY       = false;

function doSelect(){
	if (oWindow != null && !oWindow.closed){
		oWindow.close();
		oWindow = null;
	}
	
	if (iPId == 1){
		oWindow = window.open('P_WindowLookup.aspx?cid=' + iCId, 'lookup', 'scrollbars=Yes,toolbar=No,resizable=yes,menubar=no,status=no,height=600,width=870', false);		
	}
	else if (iPId == 2){
		oWindow = window.open('P_DoorLookup.aspx?cid=' + iCId, 'lookup', 'scrollbars=Yes,toolbar=No,resizable=yes,menubar=no,status=no,height=600,width=870', false);			
	}
}

function doSelectCallBack(iPId, sProdNo, sImage, nPrice, iOpeners, iPanes){
	var sPrice;
	var oInput;
	var oLink;
	
	sPrice = getCurrency(nPrice);

	if (iPId == 1){
		oProductDiv.innerHTML = '<div style="width:187;"><a href="javascript:doSelect(1)"><img border=0 height=109 src="' + sImage + '" alt="Window ' + sProdNo + ' with ' + iPanes + ' panes and ' + iOpeners + ' openers from ' + sPrice + '"></a></div>';

		oInput = document.getElementById('ProductOpeners');
		if (oInput != null){
			oInput.value = iOpeners;
		}
		oInput = document.getElementById('ProductPanes');
		if (oInput != null){
			oInput.value = iPanes;
		}
		oLink = document.getElementById('SelectLink');
		if (oLink != null){
			oLink.innerText = 'Select another window style';
		}
	}
	else if( iPId == 2){
		oProductDiv.innerHTML = '<div style="width:187;" align=center><a href="javascript:doSelect(2)"><img border=0 height=109 src="' + sImage + '" alt="Door ' + sProdNo + ' from ' + sPrice + '"></a></div>';
		
		oLink = document.getElementById('SelectLink');
		if (oLink != null){
			oLink.innerText = 'Select another door style';
		}	
	}
	
	oInput = document.getElementById('ProductNo');
	if (oInput != null){
		oInput.value = sProdNo;
	}
	oInput = document.getElementById('ProductImage');
	if (oInput != null){
		oInput.value = sImage;
	}
	oInput = document.getElementById('ProductPrice');
	if (oInput != null){
		oInput.value = nPrice;
	}
	
	doCalc();
}

function getPrice(nSize){
	var iIndex;
	var nPrice;
	
	nPrice = 0;
	if (nSize < naSize[0]){
		alert('The total size (width + height) cannot be less than ' + (naSize[0] * 1000) + ' mm.'); 	
	}
	else if (nSize > naSize[naSize.length - 1]){
		alert('The total size (width + height) cannot be greater than ' + (naSize[naSize.length - 1] * 1000) + ' mm.'); 
	}
	else if(nSize == naSize[naSize.length - 1]){
		nPrice = naPrice[naSize.length - 1];
	}
	else{
		
		for (iIndex = 1; iIndex < naSize.length; iIndex++){
			if (nSize < naSize[iIndex] && nSize >= naSize[iIndex -1]){
				nPrice = naPrice[iIndex -1];
			} 
		}
	}
	return nPrice;

}

function getCurrency(nNum){
	var sNum;
	sNum = '' + nNum;
	if (sNum.indexOf('.') > -1){
		
		if (sNum.substring(sNum.indexOf('.') + 1).length < 2){
			sNum += '0';
		}

	}
	else{
		sNum += '.00';
	}
	return sNum;
}

function doRound(nNum) {
	return Math.round(nNum * 100) / 100;
}

function getMeters(nNumber, sField){
	var oUnitSelect;
	var nReturn;
	
	nReturn = nNumber;
	
	oUnitSelect = document.getElementById(sField);
	if (oUnitSelect != null){
		if (oUnitSelect.value == 'm'){
			nReturn = nNumber;
		}
		else if (oUnitSelect.value == 'cm'){
			nReturn = nNumber / 100;
		}
		else if (oUnitSelect.value == 'mm'){
			nReturn = nNumber / 1000;
		}
		else if (oUnitSelect.value == 'inch'){
			nReturn = nNumber * 0.0254;
		}
	}
	return nReturn;
}

function doDoorCalc(oProductPrice, oPriceDisplayField, oPriceEnterField, oProductDetails, oProdType){
	var oHeight;
	var oWidth;
	var nHeightInM;
	var nWidthInM;
	var nCalcPrice;
	var sCalcPrice;

	oHeight       = document.getElementById('Height');
	oWidth        = document.getElementById('Width');

	if (oHeight != null && oHeight.value != '' && oWidth != null && oWidth.value != ''){
		try{
		//alert(oWidth.value + '    ' + oHeight.value);
			nHeightInM = parseFloat(oHeight.value);
			nWidthInM  = parseFloat(oWidth.value);
			if (!isNaN( nWidthInM ) && !isNaN( nHeightInM ) ){
				// units
				nHeightInM = getMeters(nHeightInM, 'HeightUnits');
				nWidthInM  = getMeters(nWidthInM, 'WidthUnits');
			
				if ( nHeightInM > 2.2 ){
					oPriceDisplayField.value = '';
					oPriceEnterField.value   = '';	
					alert('The height of the door cannot exceed 2200mm (2.2m).\n\nIf you would like a door that exceed the standard height then please contact us.');
				}
				else{
			
					nCalcPrice = parseFloat(oProductPrice.value);
					
					nCalcPrice = doRound(nCalcPrice);
					nCalcPrice = addOnFittedPrice(nCalcPrice, oProductDetails, 2, lIsDIY);
					
					sCalcPrice = getCurrency(nCalcPrice);

					var oHandle = document.getElementById('Handle');
					if ( oHandle != null ){
						oProductDetails.value    = doRound(nWidthInM * 1000) + ' mm x ' + doRound(nHeightInM * 1000) + ' mm + ' + oHandle.value + ' handle';
					}
					else{
						oProductDetails.value    = doRound(nWidthInM * 1000) + ' mm x ' + doRound(nHeightInM * 1000) + ' mm';			
					}

					oPriceDisplayField.value = sCalcPrice;
					oPriceEnterField.value   = nCalcPrice;
						
					oProdType.value          = 'DOOR';
				}
			}
			else{
				oPriceDisplayField.value = '';
				oPriceEnterField.value   = '';	
				alert('Please enter a valid height and width.');
			}
		}
		catch(e){
			oPriceDisplayField.value = '';
			oPriceEnterField.value   = '';	
			alert('Please enter a valid height and width.');
		}			
	}
	else{
		oPriceDisplayField.value = '';
		oPriceEnterField.value   = '';	
	}
	

}

function doWindowCalc(oProductPrice, oPriceDisplayField, oPriceEnterField, oProductDetails, oProdType){
	var oHeight;
	var oWidth;
	var oWoodgrain;
	var nSize;
	var nSizePrice;
	var nCalcPrice;
	var sCalcPrice;
	var nHeightInM;
	var nWidthInM;
	var oProductExtra;
	var iOpeners;
	var iPanes;
	
	oHeight       = document.getElementById('Height');
	oWidth        = document.getElementById('Width');
	oWoodgrain    = document.getElementById('Woodgrain');
	if (oHeight != null && oHeight.value != '' && oWidth != null && oWidth.value != '' && oWoodgrain != null){
		try{
			oProductExtra = document.getElementById('ProductOpeners');
			if (oProductExtra != null){
				try{
					iOpeners = parseInt(oProductExtra.value);
					if (isNaN(iOpeners)){
						iOpeners = 0;
					}
				}
				catch(e){
						iOpeners = 0;
				}
			}
			oProductExtra = document.getElementById('ProductPanes');
			if (oProductExtra != null){
				try{
					iPanes = parseInt(oProductExtra.value);
					if (isNaN(iPanes)){
						iPanes = 0;
					}
				}
				catch(e){
						iPanes = 0;
				}
			}

			nHeightInM = parseFloat(oHeight.value);
			nWidthInM  = parseFloat(oWidth.value);
			if (!isNaN( nWidthInM ) && !isNaN( nHeightInM ) ){
				// units
				nHeightInM = getMeters(nHeightInM, 'HeightUnits');
				nWidthInM  = getMeters(nWidthInM, 'WidthUnits');
			
				nSize =  nHeightInM + nWidthInM;
				nSizePrice = getPrice(nSize);
				if (nSizePrice > 0){
					nCalcPrice = nSizePrice + parseFloat(oProductPrice.value);
					
					oProductDetails.value    = doRound(nWidthInM * 1000) + ' mm x ' + doRound(nHeightInM * 1000) + ' mm';

					nCalcPrice = addOnWoodgrain(oWoodgrain, nCalcPrice, oProductDetails);
					nCalcPrice = addOnHindgesAndHandles(nCalcPrice, oProductDetails, iOpeners);
					nCalcPrice = addOnTrickleVents(nCalcPrice, oProductDetails);
					nCalcPrice = addOnGlassStyle(nCalcPrice, oProductDetails, iPanes);
					nCalcPrice = addOnEnergyRating(nCalcPrice, oProductDetails, iPanes);
					nCalcPrice = addOnWasteDisposal(nCalcPrice, oProductDetails, iPanes);
					nCalcPrice = addOnGuarantee(nCalcPrice, oProductDetails, iPanes);

					addOnCill(oProductDetails);
					
					nCalcPrice = doRound(nCalcPrice);
					nCalcPrice = addOnFittedPrice(nCalcPrice, oProductDetails, 1, lIsDIY);
									
					
					sCalcPrice = getCurrency(nCalcPrice);

					oPriceDisplayField.value = sCalcPrice;
					oPriceEnterField.value   = nCalcPrice;
					
						
					oProdType.value          = 'WIN';
				}
				else{
					oPriceDisplayField.value = '';
					oPriceEnterField.value   = '';	
				}
			}
			else{
				oPriceDisplayField.value = '';
				oPriceEnterField.value   = '';	
				alert('Please enter a valid height and width.');
			}
		}
		catch(e){
			oPriceDisplayField.value = '';
			oPriceEnterField.value   = '';	
			alert('Please enter a valid height and width.');
		}			
	}
	else{
		oPriceDisplayField.value = '';
		oPriceEnterField.value   = '';	
	}
}

function addOnTrickleVents(nCalcPrice, oProductDetails){
	var nReturn;
	var oProductExtra;
	var iNumber;
	
	nReturn = nCalcPrice;
	
	oProductExtra = document.getElementById('TrickleVents');
	if (oProductExtra != null){
		if (oProductExtra.value != '' && oProductExtra.value != '0'){
			try{
				iNumber = parseInt(oProductExtra.value);
				if (!isNaN(iNumber)){
					nReturn += iNumber * nTRICKLEVENT ;
					
					if (iNumber == 1){
						oProductDetails.value += ' + ' + iNumber + ' Trickle Vent';
					}
					else{
						oProductDetails.value += ' + ' + iNumber + ' Trickle Vents';				
					}
				} 
				else{
					alert('Please enter a valid number for "Trickle Vents".');
				}
			
			}
			catch(e){
				alert('Please enter a valid number for "Trickle Vents".');
			}
		}
	}
	return nReturn;
}

function addOnGlassStyle(nCalcPrice, oProductDetails, iPanes){
	var nReturn;
	var oProductExtra;
	
	nReturn = nCalcPrice;
	
	oProductExtra = document.getElementById('GlassStyle');
	if (oProductExtra != null){
		if (oProductExtra.value == 'Toughend Safety K Glass'){
			nReturn += (nSAFETYGLASS  * iPanes);
			oProductDetails.value += ' + Toughend Safety K Glass';
		}
		else if (oProductExtra.value == 'Georgian Style K Glass'){
			nReturn += (nGEORGIANGLASS  * iPanes);
			oProductDetails.value += ' + Georgian Style K Glass';		
		}
		else if (oProductExtra.value == 'Lead Style K Glass'){
			nReturn += (nLEADGLASS  * iPanes);
			oProductDetails.value += ' + Lead Style K Glass';		
		}
		else if (oProductExtra.value == 'Patterned K Glass'){
			nReturn += (nPATTERNEDGLASS * iPanes);
			oProductDetails.value += ' + Patterned K Glass';
		}
	}
	return nReturn;
}

function addOnEnergyRating(nCalcPrice, oProductDetails, iPanes){
	var nReturn;
	var oProductExtra;
	
	nReturn = nCalcPrice;
	
	oProductExtra = document.getElementById('EnergyRating');
	if (oProductExtra != null){
		if (oProductExtra.value == 'E Rated'){
			nReturn += (nENERGYRATINGE * iPanes);
		}
		else if (oProductExtra.value == 'D Rated'){
			nReturn += (nENERGYRATINGD * iPanes);
			oProductDetails.value += ' + D Rated';		
		}
		else if (oProductExtra.value == 'C Rated'){
			nReturn += (nENERGYRATINGC * iPanes);
			oProductDetails.value += ' + C Rated';		
		}
		else if (oProductExtra.value == 'B Rated'){
			nReturn += (nENERGYRATINGB * iPanes);
			oProductDetails.value += ' + B Rated';		
		}
		else if (oProductExtra.value == 'A Rated'){
			nReturn += (nENERGYRATINGA * iPanes);
			oProductDetails.value += ' + A Rated';		
		}
	}
	return nReturn;
}

function addOnWasteDisposal(nCalcPrice, oProductDetails, iPanes){
	var nReturn;
	var oProductExtra;
	
	nReturn = nCalcPrice;
	
	oProductExtra = document.getElementById('WasteDisposal');
	if (oProductExtra != null){
		if (oProductExtra.value == 'Full'){
			nReturn += (nWASTEDISPOSAL);
			oProductDetails.value += ' + Fully Disposed';	
		}
		else if (oProductExtra.value == 'Joint'){
			nReturn += (nJOINTDISPOSAL);
			oProductDetails.value += ' + Joint Disposed';		
		}
		else if (oProductExtra.value == ''){
			nReturn += (nOWNDISPOSAL);
		}
		
	}
	return nReturn;
}

function addOnGuarantee(nCalcPrice, oProductDetails, iPanes){
	var nReturn;
	var oProductExtra;
	
	nReturn = nCalcPrice;
	
	oProductExtra = document.getElementById('Guarantee');
	if (oProductExtra != null){
		if (oProductExtra.value == '10Year'){
			nReturn += (nGUARANTEE);
			oProductDetails.value += ' + 10 Yr Guarantee';	
		}
		else if (oProductExtra.value == ''){
			//
		}
		
	}
	return nReturn;
}

function addOnFittedPrice(nCalcPrice, oProductDetails, iPType, lDIY){
	var nReturn;
	var oProductExtra;
	
	nReturn = nCalcPrice;
	
	if (lDIY){
		oProductExtra = document.getElementById('Fitting');
		if (oProductExtra != null){
			if (oProductExtra.value == 'Full'){
				nReturn += (iPType == 1 ? iFITTEDWINPRICE : iFITTEDDOORPRICE);
				oProductDetails.value += ' + Fully Fitted';	
			}
			else if (oProductExtra.value == ''){
				//
			}

		}
		
	}
	else{
		nReturn += (iPType == 1 ? iFITTEDWINPRICE : iFITTEDDOORPRICE);
	}
	return nReturn;
}


function addOnCill(oProductDetails){
	var oProductExtra;
	
	oProductExtra = document.getElementById('Cill');
	if (oProductExtra != null){
		if (oProductExtra.value != ''){
			oProductDetails.value += ' + ' + oProductExtra.value + ' Cill';
		}
	}

}


function addOnHindgesAndHandles( nCalcPrice, oProductDetails, iOpeners){
	var nReturn;
	var oProductExtra;

	nReturn = nCalcPrice;
	if (iOpeners > 0){
		oProductExtra = document.getElementById('Hinges');
		if (oProductExtra != null){
			if (oProductExtra.value == 'Fire Hinges'){ 
				nReturn += (nEXTRAHINGES  * iOpeners);
				if (iOpeners == 1){
					oProductDetails.value += ' + ' + iOpeners + ' Fire Hinge';
				}
				else{
					oProductDetails.value += ' + ' + iOpeners + ' Fire Hinges';				
				}
			}
			else if (oProductExtra.value == 'Restrictor Hinges'){
				nReturn += (nEXTRAHINGES  * iOpeners);
				if (iOpeners == 1){
					oProductDetails.value += ' + ' + iOpeners + ' Restrictor Hinge';
				}
				else{
					oProductDetails.value += ' + ' + iOpeners + ' Restrictor Hinges';				
				}
			}
		}
		
		oProductExtra = document.getElementById('Handle');
		if (oProductExtra != null){
			if (oProductExtra.value == 'Gold'){
				nReturn += (nGOLDHANDLE  * iOpeners);
				if (iOpeners == 1){
					oProductDetails.value += ' + ' + iOpeners + ' Gold Handle';
				}
				else{
					oProductDetails.value += ' + ' + iOpeners + ' Gold Handles';				
				}
			}
			else if (oProductExtra.value == 'Chrome'){
				nReturn += (nCHROMEHANDLE  * iOpeners);
				if (iOpeners == 1){
					oProductDetails.value += ' + ' + iOpeners + ' Chrome Handle';
				}
				else{
					oProductDetails.value += ' + ' + iOpeners + ' Chrome Handles';				
				}
			}
		}
	}
	
	return nReturn;
}

function addOnWoodgrain(oWoodgrain, nCalcPrice, oProductDetails){
	var nReturn;
	
	nReturn = nCalcPrice;
	if (oWoodgrain.value == '1 x Woodgrain' || 
			oWoodgrain.value == '1 x Rose Wood' ||
			oWoodgrain.value == '1 x Oak Wood' ){
		nReturn = nCalcPrice *  nCOLOURONESIDES ;
		oProductDetails.value += ' + ' + oWoodgrain.value;
	}
	else if (oWoodgrain.value == '2 x Woodgrain' || 
			oWoodgrain.value == '2 x Rose Wood' ||
			oWoodgrain.value == '2 x Oak Wood' ){
		nReturn = nCalcPrice * nCOLOURBOTHSIDES ;
		oProductDetails.value += ' + ' + oWoodgrain.value;
	}
	return nReturn;
}

function doCalc(){
	var oHeight;
	var oWidth;
	var oWoodgrain;
	var oProductPrice
	var oPriceDisplayField;
	var oPriceEnterField;
	var nSize;
	var nSizePrice;
	var nCalcPrice;
	var sCalcPrice;
	var oProductDetails;
	var oProdType;
	var nHeightInM;
	var nWidthInM;
	var oProductExtra;
	var iOpeners;
	var iPanes;
	
	oProductPrice      = document.getElementById('ProductPrice');
	oPriceDisplayField = document.getElementById('DisplayPrice');
	oPriceEnterField   = document.getElementById('CalcPrice');  
	oProductDetails    = document.getElementById('ProductDetails');  
    oProdType          = document.getElementById('ProductType');
      
	if (oProductPrice != null && oProductPrice.value != '' && oPriceDisplayField != null && oPriceEnterField != null && oProductDetails != null && oProdType != null){
		if (iPId == 1){
			doWindowCalc(oProductPrice,oPriceDisplayField,oPriceEnterField,oProductDetails,oProdType);  
		}	
		else if (iPId == 2){
			doDoorCalc( oProductPrice,oPriceDisplayField,oPriceEnterField,oProductDetails,oProdType);
		}	
	}
}

function doAddToQuote(){
	var oPriceField;
	var oForm;
	
	oPriceField = document.getElementById('DisplayPrice');  
	if (oPriceField != null){
		if (oPriceField.value != ''){
			oForm = document.getElementById('QuoteForm');
			if (oForm != null){ 
				oForm.submit();		
			}
		}
		else{
			alert('Please select an item and enter the options to calculate the price.\n\n You can then add the item to the quote below.');
		}
	}
	
}

function productChange(){
	oProductDiv.innerHTML = '<div style="width:187;" align=center></div>';
	oLink = document.getElementById('SelectLink');
	if (oLink != null){
		oLink.innerText = 'Loading...';
		oLink.href = 'javascript:alert(\"Please wait while page is loading.\");';
	}	
}
