// Read the name, id, type, and value of one form control element
// as requested by form2ArrayString( )
function formObj2String(obj) {
    var output = "{";
    if (obj.name) {
        output += "name:'" + obj.name + "',";
    }
    if (obj.id) {
        output += "id:'" + obj.id + "',";
    }
    output += "type:'" + obj.type + "',";
    switch (obj.type) {
        case "radio":
            if (obj.name) {
                obj = document.forms[0].elements[obj.name];
                var radioVal = "value:false,index:-1";
                for (var i = 0; i < obj.length; i++) {
                    if (obj[i].checked) {
                        radioVal = "value:true,index:" + i;
                        i = obj.length;
                    } 
                }
                output += radioVal;
            } else {
                output += "value:" + obj.checked;
            }
            break;
        case "checkbox":
            output += "value:" + obj.checked;
            break;
        case "select-one":
            output += "value:" + obj.selectedIndex;
            break;
        case "select-multiple":
            output += "value:" + obj.selectedIndex;
            break;
        case "text":
            output += "value:'" + escape(obj.value) + "'";
            break;
        case "textarea":
            output += "value:'" + escape(obj.value) + "'";
            break;
        case "password":
            output += "value:'" + escape(obj.value) + "'";
            break;
        case "hidden":
            output += "value:'" + escape(obj.value) + "'";
            break;
        default:
            output += "";
    }
    output += "}"
    return output;
}
// Convert a passed form reference to a string formatted like
// a JavaScript array of objects
function form2ArrayString(form) {
    var elem, lastName = "";
    var output = "[";
    for (var i = 0; i < form.elements.length; i++) {
        elem = form.elements[i];
        if (elem.name && (elem.name != lastName)) {
            output += formObj2String(form.elements[i]) + ",";
            lastName = elem.name;
        }
    }
    output = output.substring(0, output.length-1) + "]";
    return output;
}
   
// Distribute form control values from another source to the
// controls in this page's form, whose names/ids match those
// of the original form controls
function string2FormObj(form, str) {
    var elem, objArray = eval(str);
    for (var i = 0; i < objArray.length; i++) {
        elem = (objArray[i].name) ? form.elements[objArray[i].name] : 
            document.getElementById(objArray[i].id);
        switch (objArray[i].type) {
            case "radio":
                if (objArray[i].name && objArray[i].value && objArray[i].index >= 0) {
                    elem = elem[objArray[i].index];
                }
                elem.checked = objArray[i].value;
                break;
            case "checkbox":
                elem.checked = objArray[i].value;
                break;
            case "select-one":
                elem.selectedIndex = objArray[i].value;
                break;
            case "select-multiple":
                elem.selectedIndex = objArray[i].value;
                break;
            default:
                elem.value = unescape(objArray[i].value);
        }
    }
}

// Show element on page

function showElement(element) {
	document.getElementById(element).style.display = "block";
}
function hideElement(element) {
	document.getElementById(element).style.display = "none";
}


function verifyForm(obj, step) {
	
	if (document.forms[0].temp.value == "") { alert("Please enter an estimated temperature under question 2!"); return false;}
	if (document.forms[0].sqft.value == "") { alert("Please enter a total square footage figure under question 3!");  return false;}
		
	else { nextStep(obj, step); }
}
//Move to next step in wizard

function verifyHorizontal(obj, step) {
	if ((step == 1) && (document.forms[0].textureType.value == "none")) { alert("You must select a texture type from the list."); return false;}
	if ((step == 1) && (document.forms[0].textureType.value == "multi") && (document.forms[0].multiColor1.value == "none")) { alert("You must select at least one Color from the list for a Multi-Colored Pattern."); return false;}
	if ((step == 1) && (document.forms[0].grease.value == "y") && (document.forms[0].contaminant_sf.value == "")) { alert("You must enter a square footage value to be degreased."); return false;}
	if ((step == 1) && (document.forms[0].width.value == "y") && ((document.forms[0].crack_length.value == "") || (document.forms[0].crack_width.value == "") || (document.forms[0].crack_depth.value == ""))) { alert("You must enter length, width, & depth of cracks to be repaired."); return false;}
	
	else { 
		if ((step == 1) && (document.forms[0].grout.value == "y") && (document.forms[0].color.value == document.forms[0].groutLineColor.value)) { 
			if (confirm("You have selected the same Stain Sealer colors for the grout line and texture coat.  Click OK if this is expected, or Cancel to return and select again.")) { 
				nextStep(obj,step);
			} 
			else { 
				return false;
			} 
		}
		else { nextStep(obj, step);  }
	}
}

function verifyPool(obj, step) {
	if ((step == 1) && (document.forms[0].color.value == "none")) {alert("You must select a color for the pool"); return false;}
	if ((step == 1) && (document.forms[0].grease.value == "y") && (document.forms[0].contaminant_sf.value == "")) { alert("You must enter a square footage value to be degreased."); return false;}
	if ((step == 1) && (document.forms[0].width.value == "y") && ((document.forms[0].crack_length.value == "") || (document.forms[0].crack_width.value == "") || (document.forms[0].crack_depth.value == ""))) { alert("You must enter length, width, & depth of cracks to be repaired."); return false;}
	else { nextStep(obj, step); }
}

function verifyFloor(obj, step) {
	if ((step == 1) && (document.forms[0].grease.value == "y") && (document.forms[0].contaminant_sf.value == "")) { alert("You must enter a square footage value to be degreased."); return false;}
	if ((step == 1) && (document.forms[0].width.value == "y") && ((document.forms[0].crack_length.value == "") || (document.forms[0].crack_width.value == "") || (document.forms[0].crack_depth.value == ""))) { alert("You must enter length, width, & depth of cracks to be repaired."); return false;}
	else { nextStep(obj, step); }
}

function verifyCounter(obj, step) {
	if ((step == 1) && (document.forms[0].finish.value == "none")) { alert("You must select a finish from the list."); return false;}
	else { 
		if ((step == 1) && (document.forms[0].grout.value == "y") && (document.forms[0].color.value == document.forms[0].groutLineColor.value)) { 
			if (confirm("You have selected the same Stain Sealer colors for the grout line and texture coat.  Click OK if this is expected, or Cancel to return and select again.")) { 
				nextStep(obj,step);
			} 
			else { 
				return false;
			} 
		}
		else { nextStep(obj, step);  }
		} 
}

function nextStep(obj, step) {
	
	var seq = (step) ? 1 : "";
	var jobArray = jobTypes();
	var filename, srchString;
	
	var jobType =  jobArray[document.forms[0].job_type.value].name; 
	filename =  jobType + seq + ".htm";
	
	srchString = form2ArrayString(obj);
	filename += "?" + escape(srchString);

	location.href = filename;
	
}

// Array of job Types, name is the base name of the file ie. vertical will be transformed to vertical.htm
function jobTypes() {
	var jobTypesArray = new Array();
	jobTypesArray[jobTypesArray.length] = {name: "vertical", display: "Vertical"};
	jobTypesArray[jobTypesArray.length] = {name: "horizontal", display: "Horizontal"};
	jobTypesArray[jobTypesArray.length] = {name: "pool_interior", display: "Pool Interior"};
	jobTypesArray[jobTypesArray.length] = {name: "countertop", display: "Countertop"};
	jobTypesArray[jobTypesArray.length] = {name: "industrial_floor", display: "Industrial Floor"};
	
	for (var i = 0; i < jobTypesArray.length; i++) {
		jobTypesArray[jobTypesArray[i].name] = jobTypesArray[i];
	}
	
	return jobTypesArray;
	
}

// Only Allow Numbers in Field

function numeralsOnly(evt) {

	evt = (evt) ? evt : event;
	var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0 ));
	if (charCode > 31 && (charCode < 48 || charCode > 57)) 
	//if(charCode == 46 || (charCode > 47 || charCode < 48))
	{
		alert("Enter Numeric Values Only in this Field!");
		return false;
	}
	return true;
}

function calcSkimCoat(sf, type, substrate) {
	
	//var spread = (parseInt(type) == 0) ? ((substrate == 0) ? 70 : 80) : 100;
	var spread = (parseInt(type) == 0) ? 80 : 100;
	var bags = parseInt(sf) / parseInt(spread);
	var rem = parseInt(sf) % parseInt(spread);
	
	if (rem != 0) { bags = parseInt(bags) + 1; }
	
	return bags;
}

function calcTextureCoat(sf, type) {
	
	var spread = (type == 0) ? 80 : 140;
	var bags = parseInt(sf) / parseInt(spread);
	var rem = parseInt(sf) % parseInt(spread);
	
	if (rem != 0) { bags = parseInt(bags) + 1; }
	return bags;
}

function calcMesh(sf) {
	var spread = 450;
	var rolls = parseInt(sf) / parseInt(spread);
	var rem = parseInt(sf) % parseInt(spread);
	
	if (rem != 0) { rolls = parseInt(rolls) + 1; }
	return rolls;
}

function calcBonding(bags) {
	// chaged spread from 4 to 5 2-8-2006
	var pails = parseInt(bags) / 5;
	var rem = parseInt(bags) % 5;
	
	if (rem != 0) { pails = parseInt(pails) + 1; }
	return pails;
	
}

function calcSealer (sf, coats) {
	
	var totalGals = 0;
	for (var i=0; i < coats; i++) {
		var gals = (coats == 0) ? parseInt(sf) / 235 : parseInt(sf) / 290;
		totalGals += gals;
	
	}
	return totalGals;
//	var pails = totalGals / 5;
//	var rem = totalGals % 5;
//	if (rem != 0) { pails = parseInt(pails) + 1; }
//	return pails;
}

function calcColorant(bags, code) {
	
	var stainSealer = stainSealerArray();
	var ounces = stainSealer[code].ounces;
	
	var bottles = (parseInt(ounces) * parseInt(bags)) / 32;
	var rem = (parseInt(ounces) * parseInt(bags)) % 32;
	if (rem != 0) { bottles = parseInt(bottles) + 1; }

	return bottles;
}

function calcTemplate(sf, type) {
	var spread = (type == "reusable") ? 100 : 500;
	var rolls = parseInt(sf) / parseInt(spread);
	
	var rem = parseInt(sf) % parseInt(spread);
	if (rem != 0) { rolls = parseInt(rolls) + 1; }
	return rolls;
}

function calcPrime(sf) {
		var spread = 1060;
		var kits = parseInt(sf) / parseInt(spread);
		var rem = parseInt(sf) % parseInt(spread);
		if (rem != 0) { kits = parseInt(kits) + 1; }
		return kits;
}

function calcThane(sf) {
	var spread = 760;
	var kits = parseInt(sf) / parseInt(spread);
	var rem = parseInt(sf) % parseInt(spread);
	if (rem != 0) { kits = parseInt(kits) + 1; }
	return kits;
}

function calcThane1g(sf) {
	var spread = 152;
	var kits = parseInt(sf) / parseInt(spread);
	var rem = parseInt(sf) % parseInt(spread);
	if (rem != 0) { kits = parseInt(kits) + 1; }
	return kits;
}

function calcPoxy(sf, color) {
	var spread = (color == "Clear") ? 160 : 480;
	var kits = parseInt(sf) / parseInt(spread);
	var rem = parseInt(sf) % parseInt(spread);
	if (rem != 0) { kits = parseInt(kits) + 1; }
	return kits;
}

function calcAccel(bags) {
		var spread = 4;
		var bottles = (parseInt(bags) * parseInt(spread)) / 32;
		var rem = (parseInt(ounces) * parseInt(bags)) % 32;
  	 	if (rem != 0) { bottles = parseInt(bottles) + 1; }
		return bottles;
}

function calcRetard(bags) {
		var spread = 4;
		var bottles = (parseInt(bags) * parseInt(spread)) / 32;
		var rem = (parseInt(ounces) * parseInt(bags)) % 32;
		if (rem != 0) { bottles = parseInt(bottles) + 1; }
		return bottles;
}

function calcBlueOrange(sf) {
	var spread = 200;
	var gal = parseInt(sf) / parseInt(spread);
	if (gal < 1) { gal = 1; }
	var pails = parseInt(gal) / 5;
	var rem = parseInt(gal) % 5;
	if (rem != 0) { pails = parseInt(pails) + 1; }
	return pails;
}

function calcCaulk(sf) {
	var spread = 60;
	var pints = parseInt(sf) / parseInt(spread);
	var rem = parseInt(sf) % parseInt(spread);
	if (rem != 0) { pints = parseInt(pints) + 1; }
	return pints;
}

function calcGel(sf) {
	var spread = 460;
	var units = parseInt(sf) / parseInt(spread);
	var rem = parseInt(sf) % parseInt(spread);
	if (rem != 0) { units = parseInt(units) + 1; }
	return units;
}

function calcPermaBond(sf) {
	var spread = 11;
	var units = parseInt(sf) / parseInt(spread);
	var rem = parseInt(sf) % parseInt(spread);
	if (rem != 0) { units = parseInt(units) + 1; }
	return units;
} 

function calcPaint(sf) {
	var spread = 25;
	var lbs = parseInt(sf) / parseInt(spread);
	var rem = parseInt(sf) % parseInt(spread);
	if (rem != 0) { lbs = parseInt(lbs) + 1; }
	return lbs;
}

function calcWeld(sf) {
	var spread = 250;
	var gal = parseInt(sf) / parseInt(spread);
	var rem = parseInt(sf) % parseInt(spread);
	if (rem != 0) { gal = parseInt(gal) + 1; }

	return gal;
}

function calcClearSealer(sf, coats) {
	
	var totalGals = 0;
	for (var i=0; i < coats; i++) {
		var gals = (coats == 0) ? parseInt(sf) / 130 : parseInt(sf) / 200;
		totalGals += gals;
	
	}
	var pails = totalGals / 5;
	var rem = totalGals % 5;
	if (rem != 0) { pails = parseInt(pails) + 1; }
	return pails;
	
}
 
function calcAggregate(sf, thick)
{
	var pounds = sf * thick * 7.5;
	return pounds;
}

function redrawSelect(itemNo, which)
{
	switch(which)
		{
			case 1:
				var whichSelect = "choice";
				var whichDiv = "choice_div";
				break;
			case 2:
				var whichSelect = "choice2";
				var whichDiv = "choice_div2";
				break;
			case 3:
				var whichSelect = "choice3";
				var whichDiv = "choice_div3";
				break;
			default:
				break;
		}
		
	if(productArray[itemNo].color == 'Y')
	{
		switch(itemNo)
		{
			case "1-COLORANT":
				var matrixColors = matrixColorsArray();
				break;
			case "CAULK-PT":
				var matrixColors = caulkColors();
				break;
			case "ACCENT-1":
			case "ACCENT-5":
				var matrixColors = stainSealerArray();
				break;
			case "1-AGGREGATE":
				var matrixColors = aggregateColors();
				break;
			case "1-TEMPA4":
				var matrixColors = adhesivePatterns();
				break;
			case "1-TEMPREUSE":
				var matrixColors = reusablePatterns();
				break;
			case "1-PRECOLOREDMM":
			case "1-PRECOLOREDMM":
			case "1-PPRECOLOREDM-GRIT":
			case "1-PRECOLOREDP-GRIT":
			case "1-PRECOLOEDP-SET":
			case "1-PRECOLORED-POOL":
			case "1-PRECOLOREDT-GRIT":
			case "1-PRECOLOREDT-SET":
				var matrixColors = precolorMM();
				break;
			case "PAINTCHIP":
				var matrixColors = paintChips();
				break;
			case "PPOXY":
				var matrixColors = poxyColors();
				break;
			case "PERMAPRIME":
				var matrixColors = primeColors();
				break;
			case "CHIPDISPLAY":
				var matrixColors = chipDisplays()
				break;
			default :
				break;
		}
		
			
		document.getElementById(whichSelect).options.length = 0;
		for(var i=0; i < matrixColors.length; i++)
		{
			document.getElementById(whichSelect).options[i] = new Option(matrixColors[i].name, matrixColors[i].name, false, false);
		}
		showElement(whichDiv);
	}
	else
	{
		hideElement(whichDiv);
	}
}

function AddtoOrder(itemNo, itemQty) 
{
	var lineLength = parseInt(lineArray.length);
	//location.reload(true);
	if(lineLength !=0)
	{
		document.getElementById('invoice').deleteRow(lineLength+2);
		document.getElementById('invoice').deleteRow(lineLength+2);
	}
	else
	{
		document.getElementById('invoice').deleteRow(lineLength+2);
	}
	var row=document.getElementById('invoice').insertRow(lineLength+2);
	var item_no = row.insertCell(0);
	var qty = row.insertCell(1);
	var um = row.insertCell(2);
	var desc = row.insertCell(3);
	var price = row.insertCell(4);
	var total = row.insertCell(5);
	
	lineArray[lineArray.length] = {stockid:productArray[itemNo].stockID, qty:itemQty, price:productArray[itemNo].price, total:productArray[itemNo].price * itemQty, desc:productArray[itemNo].name, um:productArray[itemNo].um};
	
	item_no.innerHTML = lineArray[lineLength].stockid;
	qty.innerHTML = lineArray[lineLength].qty;
	um.innerHTML = lineArray[lineLength].um;
	desc.innerHTML = lineArray[lineLength].desc;
	price.innerHTML = lineArray[lineLength].price;
	total.innerHTML = lineArray[lineLength].total;
	
	lineLength = lineArray.length;
	var grandTotal = 0;
	for ( i=0; i < lineArray.length; i++) {
		grandTotal += parseInt(lineArray[i].total);
	} 
	
	    row = document.getElementById('invoice').insertRow(lineLength+2);
		gTotal = row.insertCell(0);
		gTotal.colSpan="5";
		gTotal.align="right";
		gTotal.innerHTML = "<b>Order Total</b>";
		gTotal2 = row.insertCell(1);
		gTotal2.innerHTML = "$" + grandTotal;
		row = document.getElementById('invoice').insertRow(lineLength+3);
		sqft = row.insertCell(0);
		sqft.colSpan="5";
		sqft.align="right";
		sqft.innerHTML = "<b>Total SqFt.:</b>  " + variableArray['sqft'].value + "<br><br><b>Price / SqFt.:</b>  $" + Math.round(grandTotal / variableArray['sqft'].value*Math.pow(10,2)) / Math.pow(10,2);
		
}

