//Door Width
var W24 = 24;
var W30 = 30;
var W36 = 36;
var W40 = 40;
//Door Height
var H80 = 80;
var H96 = 96;
//No of doors
var N2 = 2;
var N3 = 3;
var N4 = 4;
//INFO
var OK = 0
var DOORHIDDING = 1;
var NODOORSUITABLE = 2;
var CUSTOM = 3;
//Inch Metric convert
var InchtoMetric = 0.0254;
var MetrictoInch = 0.393700787 ;
//result set
var arrResultParsing = new Array;
arrResultParsing[0] = " ";
arrResultParsing[1] = "Part of the door on the back track will be hidden<br>For custom sizes <br><a href='javascript:setCustom();validateCalc();'>please click here for custom doors calculator</a>";
arrResultParsing[2] = "NODOORSUITABLE";
arrResultParsing[3] = "For custom sizes <br><a href='javascript:setCustom();validateCalc();'>please click here for custom doors calculator</a>";
arrResultParsing[4] = " ";

//flags
var isHeightCustom = false;
var isCustom = false;


OPTION_FIELD_ = "Option no. ";
WIDTH_ = "Door Width";
DOOR_NUM_ = "Number of doors";
COMMENT_ = "Comments";

var arrResultW;
var arrResultH;
var arrResult;
var numArray;

/************************************************************************/
function calc(width,height,bolMetric)
{
	isHeightCustom = false;
	arrResultW = new Array;
	arrResultH = new Array;
	var widthForCalc = width;
	var heightForCalc = height;
	if (true == bolMetric)
	{
		widthForCalc = Math.round(widthForCalc * MetrictoInch);
		heightForCalc = Math.round(heightForCalc * MetrictoInch);
	}

	if (true == isCustom)
	{
		
		calcCustomSize(widthForCalc,document.getElementById("nuOfDoors").value);
	}
	else
	{
		calcWidth(widthForCalc);
		calcHeight(heightForCalc);
	}
	
	setOrderInScreen(orderResults(isCustom));
}
/************************************************************************/
function orderResults(isCustom)
{
	arrResult = new Array;
	var wIndex = 0;
	numArray = new Array;
	var hasOK = false;

	if (true == isCustom)
	{
		 var propC = arrResultW[0];
		 arrResult = new Array;
		 numArray[1] = 0;
		 arrResult[0] = propC;
		 return numArray;
	}
	for(i=0;i<arrResultH.length;i++)
	{
		var propHeight = arrResultH[i];
		if (CUSTOM == propHeight[0])
		{
			isHeightCustom = true;
//			 numArray[1] = 0;
//			 arrResult[0] = propHeight;
// 			 return numArray;
		}		
	}

//	alert(arrResultW.length);
	for(i=0;i<arrResultW.length;i++)
	{
		var propW = arrResultW[i];
		if (false == hasOK && CUSTOM == propW[0])
		{
			 arrResult = new Array;
			 numArray[1] = 0;
			 arrResult[0] = propW;
		 	 return numArray;
		}
		if (OK == propW[0])
		{
			 numArray[propW[1]-1] = wIndex;
			 arrResult[wIndex] = propW;
			 hasOK = true;
		}
		if (false == hasOK && DOORHIDDING == propW[0])
		{
			 numArray[propW[1]+2] = wIndex;
			 arrResult[wIndex] = propW;
		}
		wIndex++;

	}

	return hasOK;

}
/************************************************************************/
function removeResult()
{
	var theTable = document.getElementById('tblResult');

	var num = theTable.rows.length;
	for (i=0;i < num; i++)
	{
		theTable.deleteRow(0)
	}
}
/************************************************************************/
function setOrderInScreen(hasOK)
{
	removeResult();
	var theTable = document.getElementById('tblResult');
	var rowNum = 1;

	//table title
	var tr = theTable.insertRow(0);
	var td = tr.insertCell(0);
	td.innerHTML = OPTION_FIELD_;
	td.align = "center";
	td.style.fontWeight = "bold";
	td = tr.insertCell(1);
	td.align = "center";
	td.style.fontWeight = "bold";
	td.innerHTML = WIDTH_;
	td = tr.insertCell(2);
	td.align = "center";
	td.style.fontWeight = "bold";
	td.innerHTML = DOOR_NUM_;
	td = tr.insertCell(3);
	td.align = "center";
	td.style.fontWeight = "bold";
	td.innerHTML = COMMENT_;

	for( var x = 1; x < numArray.length; x++ ) 
	{
		if ("undefined" != typeof(numArray[x]))
		{
		//	alert(arrResult[numArray[x]]);
			var propW = arrResult[numArray[x]];			
			if ("undefined" != typeof(propW[2]))
			{
				if((propW[0] != OK && hasOK == false) || propW[0] == OK)
				{
					tr = theTable.insertRow(rowNum);
					td = tr.insertCell(0);
					td.align = "center";
					td.style.fontWeight = "bold";
					td.innerHTML = ""+rowNum;
					td = tr.insertCell(1);
					td.align = "center";
					td.innerHTML = propW[2]+"\"";
					td = tr.insertCell(2);
					td.align = "center";
					td.innerHTML = ""+propW[1];
					td = tr.insertCell(3);
					td.align = "left";
					if (true == isHeightCustom)
					{
						if (1 < arrResultParsing[propW[0]].length )
						{
							td.innerHTML = ""+arrResultParsing[propW[0]]+"<br>"+arrResultParsing[4];
						}
						else
						{
							td.innerHTML = " "+arrResultParsing[4];
						}
					}
					else
					{
						td.innerHTML = ""+arrResultParsing[propW[0]];
					}
				}
			}
			else
			{
				tr = theTable.insertRow(rowNum);
				td = tr.insertCell(0);
				td.colSpan = 4;
				td.align = "left";
				td.style.fontWeight = "bold";
				td.innerHTML = ""+arrResultParsing[propW[0]];
			}
			rowNum++;
		}
	}

	tr = theTable.insertRow(rowNum);
	td = tr.insertCell(0);
	td.colSpan = 4;
	td.align = "center";
	td.style.fontWeight = "bold";
	td.innerHTML = "Please call us with your door information <br> at 303.519.6708";



}
/************************************************************************/
function calcCustomSize(width,nuOfDoors)
{
	var doorWidth = width/nuOfDoors;
	doorWidth = doorWidth*100;
	doorWidth = Math.round(doorWidth)
	doorWidth = doorWidth/100;

	var doorProp = new Array;
	doorProp[0] = OK;
	doorProp[1] = nuOfDoors;
	doorProp[2] = doorWidth;
	arrResultW[0] = doorProp;
}
/************************************************************************/
function calcHeight(heightForCalc)
{
	var arrIndex = 0;
	if (heightForCalc >= 80 && heightForCalc <= 81)
	{	
		var doorProp = new Array;
		doorProp[0] = OK;
		doorProp[1] = heightForCalc;
		arrResultH[arrIndex] = doorProp;
		//arrIndex++;
	}
	else if (heightForCalc >= 96 && heightForCalc <= 97)
	{	
		var doorProp = new Array;
		doorProp[0] = OK;
		doorProp[1] = heightForCalc;
		arrResultH[arrIndex] = doorProp;
		//arrIndex++;
	}
	else
	{
		var doorProp = new Array;
		doorProp[0] = CUSTOM;
		arrResultH[arrIndex] = doorProp;
	}

}
/************************************************************************/
function calcWidth(widthForCalc)
{
	var arrIndex = 0;
	//2 doors
	if (widthForCalc < 24)
	{	
		var doorProp = new Array;
		doorProp[0] = CUSTOM;
		arrResultW[arrIndex] = doorProp;
		arrIndex++;
		return;
	}

	if (widthForCalc > 24 && widthForCalc < 48)
	{	
		var doorProp = new Array;
		doorProp[0] = DOORHIDDING;
		doorProp[1] = N2;
		doorProp[2] = W24;
		arrResultW[arrIndex] = doorProp;
		arrIndex++;
	}

	if (widthForCalc >= 48 && widthForCalc <= 49)
	{	
		var doorProp = new Array;
		doorProp[0] = OK;
		doorProp[1] = N2;
		doorProp[2] = W24;
		arrResultW[arrIndex] = doorProp;
		arrIndex++;
	}

	if (widthForCalc > 49 && widthForCalc < 60)
	{	
		var doorProp = new Array;
		doorProp[0] = DOORHIDDING;
		doorProp[1] = N2;
		doorProp[2] = W30;
		arrResultW[arrIndex] = doorProp;
		arrIndex++;
	}

	if (widthForCalc >= 60 && widthForCalc <= 61)
	{	
		var doorProp = new Array;
		doorProp[0] = OK;
		doorProp[1] = N2;
		doorProp[2] = W30;
		arrResultW[arrIndex] = doorProp;
		arrIndex++;
	}

	if (widthForCalc > 61 && widthForCalc < 72)
	{	
		var doorProp = new Array;
		doorProp[0] = DOORHIDDING;
		doorProp[1] = N2;
		doorProp[2] = W36;
		arrResultW[arrIndex] = doorProp;
		arrIndex++;
	}

	if (widthForCalc >= 72 && widthForCalc <= 73)
	{	
		var doorProp = new Array;
		doorProp[0] = OK;
		doorProp[1] = N2;
		doorProp[2] = W36;
		arrResultW[arrIndex] = doorProp;
		arrIndex++;
	}

	if (widthForCalc > 73 && widthForCalc < 80)
	{	
		var doorProp = new Array;
		doorProp[0] = DOORHIDDING;
		doorProp[1] = N2;
		doorProp[2] = W40;
		arrResultW[arrIndex] = doorProp;
		arrIndex++;
	}

	if (widthForCalc >= 80 && widthForCalc <= 81)
	{	
		var doorProp = new Array;
		doorProp[0] = OK;
		doorProp[1] = N2;
		doorProp[2] = W40;
		arrResultW[arrIndex] = doorProp;
		arrIndex++;
	}


	//3 doors
	if (widthForCalc >= 72 && widthForCalc <= 74)
	{	
		var doorProp = new Array;
		doorProp[0] = OK;
		doorProp[1] = N3;
		doorProp[2] = W24;
		arrResultW[arrIndex] = doorProp;
		arrIndex++;
	}

	if (widthForCalc > 74 && widthForCalc < 90)
	{	
		var doorProp = new Array;
		doorProp[0] = DOORHIDDING;
		doorProp[1] = N3;
		doorProp[2] = W30;
		arrResultW[arrIndex] = doorProp;
		arrIndex++;
	}

	if (widthForCalc >= 90 && widthForCalc <= 92)
	{	
		var doorProp = new Array;
		doorProp[0] = OK;
		doorProp[1] = N3;
		doorProp[2] = W30;
		arrResultW[arrIndex] = doorProp;
		arrIndex++;
	}

	if (widthForCalc > 92 && widthForCalc < 108)
	{	
		var doorProp = new Array;
		doorProp[0] = DOORHIDDING;
		doorProp[1] = N3;
		doorProp[2] = W36;
		arrResultW[arrIndex] = doorProp;
		arrIndex++;
	}

	if (widthForCalc >= 108 && widthForCalc <= 110)
	{	
		var doorProp = new Array;
		doorProp[0] = OK;
		doorProp[1] = N3;
		doorProp[2] = W36;
		arrResultW[arrIndex] = doorProp;
		arrIndex++;
	}

	if (widthForCalc > 110 && widthForCalc < 120)
	{	
		var doorProp = new Array;
		doorProp[0] = DOORHIDDING;
		doorProp[1] = N3;
		doorProp[2] = W40;
		arrResultW[arrIndex] = doorProp;
		arrIndex++;
	}

	if (widthForCalc >= 120 && widthForCalc <= 122)
	{	
		var doorProp = new Array;
		doorProp[0] = OK;
		doorProp[1] = N3;
		doorProp[2] = W40;
		arrResultW[arrIndex] = doorProp;
		arrIndex++;
	}

	//4 doors
	if (widthForCalc >= 96 && widthForCalc <= 99)
	{	
		var doorProp = new Array;
		doorProp[0] = OK;
		doorProp[1] = N4;
		doorProp[2] = W24;
		arrResultW[arrIndex] = doorProp;
		arrIndex++;
	}

	if (widthForCalc > 99 && widthForCalc < 120)
	{	
		var doorProp = new Array;
		doorProp[0] = DOORHIDDING;
		doorProp[1] = N4;
		doorProp[2] = W30;
		arrResultW[arrIndex] = doorProp;
		arrIndex++;
	}

	if (widthForCalc >= 120 && widthForCalc <= 123)
	{	
		var doorProp = new Array;
		doorProp[0] = OK;
		doorProp[1] = N4;
		doorProp[2] = W30;
		arrResultW[arrIndex] = doorProp;
		arrIndex++;
	}

	if (widthForCalc > 123 && widthForCalc < 144)
	{	
		var doorProp = new Array;
		doorProp[0] = DOORHIDDING;
		doorProp[1] = N4;
		doorProp[2] = W36;
		arrResultW[arrIndex] = doorProp;
		arrIndex++;
	}

	if (widthForCalc >= 144 && widthForCalc <= 147)
	{	
		var doorProp = new Array;
		doorProp[0] = OK;
		doorProp[1] = N4;
		doorProp[2] = W36;
		arrResultW[arrIndex] = doorProp;
		arrIndex++;
	}

	if (widthForCalc > 147 && widthForCalc < 160)
	{	
		var doorProp = new Array;
		doorProp[0] = DOORHIDDING;
		doorProp[1] = N4;
		doorProp[2] = W40;
		arrResultW[arrIndex] = doorProp;
		arrIndex++;
	}

	if (widthForCalc >= 160 && widthForCalc <= 163)
	{	
		var doorProp = new Array;
		doorProp[0] = OK;
		doorProp[1] = N4;
		doorProp[2] = W40;
		arrResultW[arrIndex] = doorProp;
		arrIndex++;
	}
	if (widthForCalc > 163)
	{	
		var doorProp = new Array;
		doorProp[0] = CUSTOM;
		arrResultW[arrIndex] = doorProp;
		arrIndex++;
		return;
	}

}
/************************************************************************/
function isNull(a) 
{
    return typeof a == 'object' && !a;
}
/************************************************************************/
function isNum(number)
{
	var hasDot = false;
	for(i=0;i<number.length;i++)
	{
		if(number.charAt(i) == ".")
		{
			hasDot = true;
		}
	}

	if(true == isInches && true == hasDot)
	{
		return false;
	}
	else
	{
		return isNumber(number);
	}
}
/************************************************************************/
function isNumber(value) {
	if (value=="") return false;

	var d = parseInt(value);
	if (!isNaN(d)) return true; else return false;		

}
/************************************************************************/
function sumbitCalc()
{
	height = document.getElementById("height").value;
	width = document.getElementById("width").value;
	if(false == isNum(height))
	{
		return;	
	}

	if(false == isNum(width))
	{
		return;
	}

}
/************************************************************************/
function validateCalc()
{
	str="";
	strN="";
	flag=true;
	focos=0;

	if((document.getElementById("width").value.length == 0))
	{
		flag=false;
		str="Width is missing<br>";
		if (focos == 0) 
		{
			document.getElementById("width").focus();
			focos = 1; 
		}
	}
	
	if((document.getElementById("height").value.length == 0))
	{
		flag=false;
		str=str + "Height is missing<br>"
		if (focos == 0) 
		{
			document.getElementById("height").focus();
			focos=1;
		}
	}
		

	 if (false == isNum(document.getElementById("width").value)) {
			str=str + "Width is invalid<br>"; 
			flag=false;
			if (focos==0) 
			{
				document.getElementById("width").focus();
				focos=1; 
			}
	}

	 if (20 > document.getElementById("width").value) {
			str=str + "Width is invalid<br>"; 
			flag=false;
			if (focos==0) 
			{
				document.getElementById("width").focus();
				focos=1; 
			}
	}

	 if (false == isNum(document.getElementById("height").value)) {
			str=str + "Height is invalid<br>"; 
			flag=false;
			if (focos==0) 
			{
				document.getElementById("height").focus();
				focos=1; 
			}
	}
	
	

	if(flag == true)
	{
	//
		setAlert(str);			
		calc(document.getElementById("width").value,document.getElementById("height").value,document.getElementById("isMetric").checked);
	}
	else
	{			
		setAlert(str);			
	}		
}
/************************************************************************/
function setCustom()
{
	isCustom = true;
	document.getElementById("trNuOfDoors").style.visibility = "visible"
	document.getElementById("calc").value = "Calculate Again"
}
/************************************************************************/
function prepareUserData()
{
	var text = "Width: "+document.getElementById("width").value+"\n";
	text = text + "Height: "+document.getElementById("height").value+"\n";
	if (true == document.getElementById("isMetric").checked)
	{
		text = text + "Metric system: Yes\n";
	}

	if (true == isCustom)
	{
		text = text + "Custom size: Yes\n";
	}

	hasOK = orderResults(isCustom);
	rowNum = 1;

	for( var x = 1; x < numArray.length; x++ ) 
	{
		if ("undefined" != typeof(numArray[x]))
		{
			var propW = arrResult[numArray[x]];			
			if ("undefined" != typeof(propW[2]))
			{
				if((propW[0] != OK && hasOK == false) || propW[0] == OK)
				{
					text = text + "\nOption "+rowNum+":\n";
					text = text + "Door width "+propW[2]+"\n";
					text = text + "No. of doors "+propW[1]+"\n\n";
				}
			}
			rowNum++;
		}
	}
	document.getElementById("UserData").value = text;
	document.getElementById("iContact").submit();

}
/************************************************************************/
function setAlert()
{
	if (document.layers) 
	{
		document.layers["calcalert"].document.close();
		document.layers["calcalert"].document.write(str);
		document.layers["calcalert"].document.close();		
	}
	else 
	{
		if (document.all) 
		{
			document.all.calcalert.innerHTML = str;
		} 
		else 
		{
			document.getElementById("calcalert").innerHTML = str;
		}
	}	

}
