var Cost_SF;
var Total_Cost;
var Cost_of_Living_Value;
var Temp_Cost;
var Plans_Levels;
var SqFt_Width_Inches;
var SqFt_Width_Feet;
var SqFt_Depth_Inches;
var SqFt_Depth_Feet;
var Brick_Garage_Plans;
var Garage_Plans_Finish;
var search_text;
var garageplanslevels;
var garage_plans_construction_type;
var garage_plans_costofliving;
var garage_plans_cost_of_living;
var garage_plans_work_yourself;
var garage_plans_workyourself;
var garageplansconstructiontype;
var garageplanscol;
var garageplanswdy;

//popup functions
function popUp(URL) {
	"use strict";
    var day = new Date();
    var id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=500,height=610,left = 465,top = 100');");
}

function popUp2(URL) {
	"use strict";
    var day = new Date();
    var id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=300,height=300,left = 465,top = 100');");
}

//sidebar calculators
function convert_to_feetin(frm_m_to_f) {
	"use strict";
	var msmt_in_inches;
	var msmt_in_feet;
	var remainder;
	if (isNaN(frm_m_to_f.meters.value)) {
		alert("Value entered must be a number");
		return false;
	}
	if (frm_m_to_f.meters.value === "") {
		frm_m_to_f.meters.value = 0;
	}
	msmt_in_inches = (frm_m_to_f.meters.value * 39.3700787);
	msmt_in_feet = parseInt(msmt_in_inches / 12, 10);
    remainder = msmt_in_inches / 12 - msmt_in_feet;
    frm_m_to_f.feet.value = msmt_in_feet;
    frm_m_to_f.inches.value = Math.round(remainder * 12);
    if (frm_m_to_f.inches.value === 12) {
		frm_m_to_f.inches.value = 0;
		frm_m_to_f.feet.value = parseFloat(frm_m_to_f.feet.value) + 1;
    }
	return false;
}

function convert_to_meters(frm_f_to_m) {
	"use strict";
	var meters_from_feet;
	if (isNaN(frm_f_to_m.feet_to_m.value) || isNaN(frm_f_to_m.inches_to_m.value)) {
		alert("Value entered must be a number");
		return false;
	}

	if (frm_f_to_m.feet_to_m.value === "") {
		frm_f_to_m.feet_to_m.value = 0;
	}

	if (frm_f_to_m.inches_to_m.value === "") {
		frm_f_to_m.inches_to_m.value = 0;
	}

	meters_from_feet = ((parseFloat(frm_f_to_m.feet_to_m.value * 12) + parseFloat(frm_f_to_m.inches_to_m.value)) / 39.3700787);
	if (meters_from_feet === 0 || isNaN(meters_from_feet)) {
		return false;
	}
	frm_f_to_m.meters_from_feet.value = Math.round(meters_from_feet * 100) / 100;
	return false;
}

function show_m_to_f() {
    document.frm_f_to_m.style.display = 'none';
    document.frm_m_to_f.style.display = 'block';
}

function show_f_to_m() {
    document.frm_m_to_f.style.display = 'none';
    document.frm_f_to_m.style.display = 'block';
}

function initializeconverter() {
	reload_garage_plans_data();
}

function calculate_square_feet(frm_square_feet) {
	var garage_plans_square_feet;
	if (isNaN(frm_square_feet.width_feet.value) || isNaN(frm_square_feet.width_inches.value)) {
		alert("Value entered must be a number");
		return false;
	}

	if (isNaN(frm_square_feet.depth_feet.value) || isNaN(frm_square_feet.depth_inches.value)) {
		alert("Value entered must be a number");
		return false;
	}

	if (frm_square_feet.depth_feet.value === "") {
		frm_square_feet.depth_feet.value = 0;
	}

	if (frm_square_feet.depth_inches.value === "") {
		frm_square_feet.depth_inches.value = 0;
	}

	if (frm_square_feet.width_feet.value === "") {
		frm_square_feet.width_feet.value = 0;
	}

	if (frm_square_feet.width_inches.value === "") {
		frm_square_feet.width_inches.value = 0;
	}

	SqFt_Width_Inches = frm_square_feet.width_inches.value;
	SqFt_Width_Feet = frm_square_feet.width_feet.value;
	SqFt_Depth_Inches = frm_square_feet.depth_inches.value;
	SqFt_Depth_Feet = frm_square_feet.depth_feet.value;

	get_levels_value();
	garage_plans_square_feet = ((((parseFloat(SqFt_Depth_Feet) * 12) + parseFloat(SqFt_Depth_Inches)) * ((parseFloat(SqFt_Width_Feet) * 12) + parseFloat(SqFt_Width_Inches))) / 144) * Plans_Levels;
	if (garage_plans_square_feet === 0 || isNaN(garage_plans_square_feet)) {
		return false;
	}
	frm_square_feet.garage_plans_square_feet.value = Math.round(garage_plans_square_feet * 100) / 100;
	return false;
}

//retrieve search form variables for use in calccost
function get_col() {
for (var i=0; i < document.catproductsform91923.costofliving.length; i++)
   {
   if (document.catproductsform91923.costofliving[i].checked)
      {
      var costofliving = document.catproductsform91923.costofliving[i].value;
      }
   }
      return (costofliving);
}

function get_rooftype() {
for (var i=0; i < document.catproductsform91923.rooftype.length; i++)
   {
   if (document.catproductsform91923.rooftype[i].checked)
      {
      var garagerooftype = document.catproductsform91923.rooftype[i].value;
		}
	}
      return (garagerooftype);
}

function get_ct_value() {
for (var i=0; i < document.catproductsform91923.constructiontype.length; i++)
   {
   if (document.catproductsform91923.constructiontype[i].checked)
      {
      var constructiontype = document.catproductsform91923.constructiontype[i].value;
     }
   }
      return (constructiontype);
}

function get_stories_value() {
//returns Number_of_Stories value to calccost
for (var i=0; i < document.catproductsform91923.numberofstories.length; i++)
   {
   if (document.catproductsform91923.numberofstories[i].checked)
      {
      var numberofstories = document.catproductsform91923.numberofstories[i].value;
      }
   }
      return (numberofstories);
}

function get_wy_value() {
for (var i=0; i < document.catproductsform91923.selfwork.length; i++)
   {
   if (document.catproductsform91923.selfwork[i].checked)
      {
      var workyourself = document.catproductsform91923.selfwork[i].value;
      }
   }
      return (workyourself);
}

function get_brick_value() {
for (var i=0; i < document.catproductsform91923.brick.length; i++)
   {
   if (document.catproductsform91923.brick[i].selected)
      {
      var brick = document.catproductsform91923.brick[i].value;
      }
   }
      return (brick);
}

function get_garage_plans_workshop() {
      if (document.catproductsform91923.workshop.checked === true) {
		var workshopgarageplans = document.catproductsform91923.workshop.value;
      }
	  else {
		workshopgarageplans = 1;
	  }
	  return (workshopgarageplans);
}

function get_garage_plans_rv() {
      if (document.catproductsform91923.rv.checked === true) {
		var rvgarageplans = document.catproductsform91923.rv.value;
      }
	  else {
		rvgarageplans = 1;
	  }
	  return (rvgarageplans);
}	  
	  
function return_Dollars (dollars) {
        divider =100;
  
        workNum=Math.abs((Math.round(dollars*divider)/divider));
  
        workStr=""+workNum;
  
        if (workStr.indexOf(".")==-1) {workStr+="."}
  
        dStr=workStr.substr(0,workStr.indexOf("."));dNum=dStr-0
        pStr=workStr.substr(workStr.indexOf("."))
  
        while (pStr.length-1< 2) {pStr+="0"}
  
        if (pStr =='.') pStr ='';
  
   if (dNum>=1000) {
           dLen=dStr.length
           dStr=parseInt(""+(dNum/1000))+","+dStr.substring(dLen-3,dLen)
        } 

        retval = dStr + pStr
  
       Cost_SF =  "$"+retval
 }

function return_Total_Dollars (dollars) {
        divider =1;
  
        workNum=Math.abs((Math.round(dollars*divider)/divider));
  
        workStr=""+workNum
  
        if (workStr.indexOf(".")==-1) {workStr+="."}
  
        dStr=workStr.substr(0,workStr.indexOf("."));dNum=dStr-0
  
   if (dNum>=1000) {
           dLen=dStr.length
           dStr=parseInt(""+(dNum/1000))+","+dStr.substring(dLen-3,dLen)
        } 

        retval = dStr
  
       Total_Cost =  "$"+retval
 }

function get_levels_value() {

for (var i=0; i < document.frm_square_feet.garage_plans_levels.length; i++)
   {
   if (document.frm_square_feet.garage_plans_levels[i].checked)
      {
      Plans_Levels = document.frm_square_feet.garage_plans_levels[i].value;
      }
   }
      return (Plans_Levels)
}

//get control index settings to save settings
function getgarageplanscostofliving() {
for (var i=0; i < document.catproductsform91923.CostOfLiving.length; i++)
   {
   if (document.catproductsform91923.CostOfLiving[i].checked)
      {
      garageplanscostofliving = i;
      }
   }
      return (garageplanscostofliving);
}

function getgarageplansworkyourself() {
for (var i=0; i < document.catproductsform91923.SelfWork.length; i++)
   {
   if (document.catproductsform91923.SelfWork[i].checked)
      {
      garageplansworkyourself = i;
      }
   }
      return (garageplansworkyourself);
}

function getgarageplansconstructiontype() {
for (var i=0; i < document.catproductsform91923.constructiontype.length; i++)
   {
   if (document.catproductsform91923.constructiontype[i].checked)
      {
      garageplansconstructiontype = i;
      }
   }
      return (garageplansconstructiontype);
}

function getgarageplansrooftype() {
for (var i=0; i < document.catproductsform91923.rooftype.length; i++)
   {
   if (document.catproductsform91923.rooftype[i].checked)
      {
      garageplansrooftype = i;
      }
   }
      return (garageplansrooftype);
}

function getgarageplansstories() {
for (var i=0; i < document.catproductsform91923.numberofstories.length; i++)
   {
   if (document.catproductsform91923.numberofstories[i].checked)
      {
      garageplansstories = i;
      }
   }
      return (garageplansstories);
}

function getgarageplansfinish() {
for (var i=0; i < document.catproductsform91923.brick.length; i++)
   {
   if (document.catproductsform91923.brick[i].selected)
      {
      garageplansfinish = i;
      }
   }
      return (garageplansfinish);
}

function getgarageplansworkshop() {
      if (document.catproductsform91923.workshop.checked === true) {
		garageplansworkshop = 1;
      }
	  else {
		garageplansworkshop = 0;
	  }
	  return (garageplansworkshop);
}

function getgarageplansrv() {
      if (document.catproductsform91923.rv.checked === true) {
		garageplansrv = 1;
      }
	  else {
		garageplansrv = 0;
	  }
	  return (garageplansrv);
}

function getgarageplanssquarefeet() {
	garageplanssquarefeet = document.catproductsform91923.GarageSize.value;
	return (garageplanssquarefeet);
}

function reload_garage_plans_data() {
	document.catproductsform91923.garagesize.value = Square_Feet;
	document.catproductsform91923.cost_sf.value = Cost_SF;
	document.catproductsform91923.total_cost.value = Total_Cost;
		return false;
}

function CalcCost() {
retrieveGaragePlansSettings();
var Cost_of_Living = get_col();
var Construction_Type = get_ct_value();
var Work_Yourself = get_wy_value();
var Garage_Roof_Type = get_rooftype();
var Number_of_Stories = get_stories_value();
var Brick = get_brick_value();
var Workshop_Garage_Plans = get_garage_plans_workshop();
var RV_Garage_Plans = get_garage_plans_rv();
var Square_Feet = document.catproductsform91923.garagesize.value;
//alert(Cost_of_Living + " " + Construction_Type + " " + Work_Yourself + " " + Garage_Roof_Type + " " + Number_of_Stories + " " + Brick + " " + Workshop_Garage_Plans + " " + RV_Garage_Plans + " " + Square_Feet);
//SqFt_High = Number(Square_Feet) + Number(150);
//SqFt_Low = Number(Square_Feet) - Number(150);
//if (SqFt_Low <= 0)
//	{
//	SqFt_Low = 0;
//	}
Cost_SF = parseFloat(Cost_of_Living) * parseFloat(Construction_Type) * parseFloat(Work_Yourself) * parseFloat(Garage_Roof_Type) * parseFloat(Brick) * parseFloat(Workshop_Garage_Plans) * parseFloat(RV_Garage_Plans) * parseFloat(Number_of_Stories);
Total_Cost = parseFloat(Cost_SF) * parseFloat(Square_Feet);
return_Dollars(Cost_SF);
return_Total_Dollars(Total_Cost);
document.catproductsform91923.cost_sf.value = Cost_SF;
document.catproductsform91923.total_cost.value = Total_Cost;
		return false;
}

function trim(stringToTrim) {
return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function searchString() {
var living=document.catproductsform91923.SearchLiving.value;
var levels=document.catproductsform91923.SearchLevels.value;
var rv=document.catproductsform91923.SearchRV.value;
var ws=document.catproductsform91923.SearchWorkshop.value;
var carport=document.catproductsform91923.SearchCarport.value;
var roof=document.catproductsform91923.SearchRoof.value;
var finish=document.catproductsform91923.SearchFinish.value;
search_text = "";
search_text += levels + " ";
search_text += living + " ";
search_text += rv + " ";
search_text += ws + " ";
search_text += carport + " ";
search_text += roof + " ";
search_text += finish;
document.catproductsform91923.CAT_ProductSearch.value = trim(search_text);
}

function get_stories_value_setting(){
//returns which radio button is clicked
for (var i=0; i < document.catproductsform91923.numberofstories.length; i++)
   {
   if (document.catproductsform91923.numberofstories[i].checked)
      {
      Number_of_Stories_Value = i;
      }
   }
      return (Number_of_Stories_Value)
}


function setGaragePlansSettings(col, wdy, constr, roof, sto, fin, ws, rv, sf) {
//garage size
	document.catproductsform91923.GarageSize.value = sf;
//stories
if (sto === 0) {
	document.catproductsform91923.SearchLevels.value = "L1"
	}
else {
	document.catproductsform91923.SearchLevels.value = "L2"
	}
document.catproductsform91923.numberofstories[sto].click();
//construction type
if (constr  === 0) {
	document.catproductsform91923.SearchLiving.value = "SX"
	document.catproductsform91923.SearchCarport.value = "HY"
	}
else if (constr  === 1) {
	document.catproductsform91923.SearchLiving.value = "SN"
	document.catproductsform91923.SearchCarport.value = "HN"
	}
else if (constr  === 2) {
	document.catproductsform91923.SearchLiving.value = "SL"
	document.catproductsform91923.SearchCarport.value = "HN"
	}
else if (constr === 3) {
	document.catproductsform91923.SearchLiving.value = "S1"
	document.catproductsform91923.SearchCarport.value = "HN"
	}
else if (constr === 4) {
	document.catproductsform91923.SearchLiving.value = "S2"
	document.catproductsform91923.SearchCarport.value = "HN"
	}
else {
	document.catproductsform91923.SearchLiving.value = "S3"
	document.catproductsform91923.SearchCarport.value = "HN"
	}
document.catproductsform91923.constructiontype[constr].click();
//cost of living
document.catproductsform91923.CostOfLiving[col].click();
//work doing yourself
document.catproductsform91923.SelfWork[wdy].click();
//roof
if (roof  === 0) {
	document.catproductsform91923.SearchRoof.value = "R1"
	}
else if (roof  === 1) {
	document.catproductsform91923.SearchRoof.value = "R2"
	}
else {
	document.catproductsform91923.SearchRoof.value = "R3"
	}
document.catproductsform91923.rooftype[roof].click();
//exterior finish
if (fin  === 0) {
	document.catproductsform91923.SearchFinish.value = "MX"
	}
else if (fin  === 1) {
	document.catproductsform91923.SearchFinish.value = "M3"
	}
else if (fin  === 2) {
	document.catproductsform91923.SearchFinish.value = "M4"
	}
else {
	document.catproductsform91923.SearchFinish.value = "M5"
	}
document.catproductsform91923.brick.selectedIndex == fin;
//workshop
if (ws  === 0) {
	document.catproductsform91923.SearchWorkshop.value = "BN"
	document.catproductsform91923.workshop.checked = false;
	}
else {
	document.catproductsform91923.SearchWorkshop.value = "BY"
	document.catproductsform91923.workshop.checked = true;
	}
//rv
if (rv  === 0) {
	document.catproductsform91923.SearchRV.value = "FN"
	document.catproductsform91923.rv.checked = false;
	}
else {
	document.catproductsform91923.SearchRV.value = "FY"
	document.catproductsform91923.rv.checked = true;
	}
}

function saveGaragePlansSettings() {
var savegarageplanscostofliving = getgarageplanscostofliving();
var savegarageplansworkyourself = getgarageplansworkyourself();
var savegarageplansconstructiontype = getgarageplansconstructiontype();
var savegarageplansrooftype = getgarageplansrooftype();
var savegarageplansstories = getgarageplansstories();
var savegarageplansfinish = getgarageplansfinish();
var savegarageplansworkshop = getgarageplansworkshop();
var savegarageplansrv = getgarageplansrv();
var savegarageplanssquarefeet = getgarageplanssquarefeet();

setCookie("StoriesGaragePlans", savegarageplansstories, 15);
setCookie("ConstructionTypeGaragePlans", savegarageplansconstructiontype, 15);
setCookie("COLGaragePlans", savegarageplanscostofliving, 15);
setCookie("WDYGaragePlans", savegarageplansworkyourself, 15);
setCookie("RVGaragePlans", savegarageplansrv, 15);
setCookie("WorkshopGaragePlans", savegarageplansworkshop, 15);
setCookie("RoofGaragePlans", savegarageplansrooftype, 15);
setCookie("FinishGaragePlans", savegarageplansfinish, 15);
setCookie("SquareFeetGaragePlans", savegarageplanssquarefeet, 15);

setGaragePlansSettings(savegarageplanscostofliving, savegarageplansworkyourself, savegarageplansconstructiontype, savegarageplansrooftype, savegarageplansstories, savegarageplansfinish, savegarageplansworkshop, savegarageplansrv, savegarageplanssquarefeet)
searchString();
}

function retrieveGaragePlansSettings() {
var retrievegarageplanscostofliving = parseFloat(getCookie("COLGaragePlans"));
var retrievegarageplansworkyourself = parseFloat(getCookie("WDYGaragePlans"));
var retrievegarageplansconstructiontype = parseFloat(getCookie("ConstructionTypeGaragePlans"));
var retrievegarageplansrooftype = parseFloat(getCookie("RoofGaragePlans"));
var retrievegarageplansstories = parseFloat(getCookie("StoriesGaragePlans"));
var retrievegarageplansfinish = parseFloat(getCookie("FinishGaragePlans"));
var retrievegarageplansworkshop = parseFloat(getCookie("WorkshopGaragePlans"));
var retrievegarageplansrv = parseFloat(getCookie("RVGaragePlans"));
var retrievegarageplanssquarefeet = parseFloat(getCookie("SquareFeetGaragePlans"));

setGaragePlansSettings(retrievegarageplanscostofliving, retrievegarageplansworkyourself, retrievegarageplansconstructiontype, retrievegarageplansrooftype, retrievegarageplansstories, retrievegarageplansfinish, retrievegarageplansworkshop, retrievegarageplansrv, retrievegarageplanssquarefeet)
}

function checkRefresh()
{
	// Get the time now and convert to UTC seconds
	var today = new Date();
	var now = today.getUTCSeconds();

	// Get the cookie
	var cookie = document.cookie;
	var cookieArray = cookie.split('; ');

	// Parse the cookies: get the stored time
	for(var loop=0; loop < cookieArray.length; loop++)
	{
		var nameValue = cookieArray[loop].split('=');
		// Get the cookie time stamp
		if ( nameValue[0].toString() === 'SHTS' )
		{
			var cookieTime = parseInt( nameValue[1] );
		}
		// Get the cookie page
		else if ( nameValue[0].toString() === 'SHTSP' )
		{
			var cookieName = nameValue[1];
		}
	}

	if ( cookieName &&
		cookieTime &&
		cookieName === escape(location.href) &&
		Math.abs(now - cookieTime) < 5 )
	{
		// Refresh detected
		CalcCost();
	}	
	else {
	CalcCost();
	}
}

function prepareForRefresh()
{
	if ( refresh_prepare > 0 )
	{
		var today = new Date();
		var now = today.getUTCSeconds();
		document.cookie = 'SHTS=' + now + ';';
		document.cookie = 'SHTSP=' + escape(location.href) + ';';
	}
	else
	{
		document.cookie = 'SHTS=;';
		document.cookie = 'SHTSP=;';
	}
}

function disableRefreshDetection()
{
	refresh_prepare = 0;
	return true;
} 

// By default, turn refresh detection on
var refresh_prepare = 1;

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x===c_name)
    {
    return unescape(y);
    }
  }
}


function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays===null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}

