var is_estimate = false;
var estimate;
var estimateObject = [];
var estimateFrame;
var infoNumber;
var textList = [];
var textFont = "20px Arial";
var textColor = "black";
var mustColor = "yellow";
var noneColor = "white";
var textBaseLine = "alphabetic";
var paddingX = 2;
var paddingY = 2;


var height = 25
var tableY = 100;
var numOfRow = 40;
			//0: No		1:Name				2: Note				3: the number			4:Check					5:Check 		6:Type
var data1 = [["番号", 	"品名"				,"備考"					,"量数"					,"チェック",		"チェック",		"NONE"],
			["1", 	"丸太下土台"			,"①～"					,"本"					,"",				"",				"MUST"],
			["2", 	"丸太上土台"			,"①～"					,"本"					,"",				"",				"MUST"],
			["3", 	"95mm桧土台"			,"①～"					,"本"					,"",				"",				"NONE"],
			["4", 	"柱"					,"①～"					,"本"					,"",				"",				"MUST"],
			["5", 	"桁、梁"				,"①～"					,"本"					,"",				"",				"NONE"],
			["6", 	"小屋束"				,"①～"					,"本"					,"",				"",				"NONE"],
			["7", 	"壁材"				,"1725mm"				,"本"					,"",				"",				"MUST"],
			["8", 	"壁材"				,"815mm"				,"本"					,"",				"",				"MUST"],
			["9", 	"壁材"				,"360mm"				,"本"					,"",				"",				"MUST"],
			["10", 	"壁調整材"			,"1725mm"				,"セット"				,"",				"",				"MUST"],
			["11", 	"壁調整材"			,"815mm"				,"セット"				,"",				"",				"MUST"],
			["12", 	"壁調整材"			,"360mm"				,"セット"				,"",				"",				"MUST"],
			["13", 	"フロアパネル"		,""						,"枚"					,"",				"",				"MUST"],
			["14", 	"巾木（幅広)"			,"1725mm"				,"本"					,"",				"",				"WANT"],
			["15", 	"巾木（幅広)"			,"815mm"				,"本"					,"",				"",				"WANT"],
			["16", 	"巾木（幅広)"			,"360mm"				,"本"					,"",				"",				"WANT"],
			["17", 	"巾木（幅狹)"			,"約2000mm"				,"本"					,"",				"",				"WANT"],
			["18", 	"小屋パネル"			,"三角形"				,"枚"					,"",				"",				"WANT"],
			["19", 	"小屋パネル"			,"台形  片流れ：四角"		,"枚"					,"",				"",				"WANT"],
			["20", 	"桁三角材"			,""						,"本"					,"",				"",				"NONE"],
			["21", 	"野地板パネル"		,""						,"枚"					,"",				"",				"MUST"],
			["22", 	"破風板"				,""						,"本"					,"",				"",				"WANT"],
			["23", 	"軒先水切り板"		,""						,"ｍ"					,"",				"",				"WANT"],
			["24", 	"棟鉄板下地材"		,""						,"ｍ"					,"",				"",				"WANT"],
			["25", 	"開きドア枠セット"		,""						,"セット"				,"",				"",				"WANT"],
			["26", 	"引き違い戸枠セット"	,"大"					,"セット"				,"",				"",				"WANT"],
			["27", 	"窓枠セット"			,"中"					,"セット"				,"",				"",				"WANT"],
			["28", 	"窓枠セット"			,"小"					,"セット"				,"",				"",				"WANT"],
			["29", 	"踏み台"				,""						,"台"					,"",				"",				"MUST"],
			["30", 	"手摺柱"				,""						,"本"					,"",				"",				"MUST"],
			["31", 	"手摺笠木"			,""						,"本"					,"",				"",				"MUST"],
			["32", 	"手摺セット"			,""						,"セット"				,"",				"",				"MUST"],
			["33", 	"ウッドデッキパネル"	,""						,"セット"				,"",				"",				"MUST"],
			["34", 	"ウッドデッキ板"		,""						,"本"					,"",				"",				"MUST"],
			["35", 	"鉄板"				,"軒、ケラバ、棟"			,"セット"				,"",				"",				"WANT"],
			["36", 	"鉄板"				,"ロフト仕様など"			,"セット"				,"",				"",				"WANT"],
			["37", 	"ルーフィング"		,""						,"本"					,"",				"",				"WANT"],
			["38", 	"シングルベスト"		,""						,"束"					,"",				"",				"WANT"],
			["39", 	"開きドア"			,"金具付き"				,"本"					,"",				"",				"MUST"],
			["40", 	"引き違い戸"			,"大"					,"セット"				,"",				"",				"MUST"]];
	
			//0: No		1:Name							2: Note				3: the number			4:Check					5:Check 		6:Type
var data2 = [["番号", 	"品名"						,"備考"				,"量数"					,"チェック",		"チェック",			"MUST"],
			["41", 		"窓"							,"中"				,"セット"				,"",				"",					"MUST"],
			["42", 		"窓"							,"小"				,"セット"				,"",				"",					"MUST"],
			["43", 		"網戸"						,"大"				,"本"					,"",				"",					"MUST"],
			["44", 		"網戸"						,"中"				,"本"					,"",				"",					"MUST"],
			["45", 		"網戸"						,"小"				,"本"					,"",				"",					"MUST"],
			["46", 		"ドア戸当金具"				,""					,"個"					,"",				"",					"OPTION"],
			["47", 		"引き違い戸カギ"				,""					,"個"					,"",				"",					"OPTION"],
			["48", 		"窓カギ"						,""					,"個"					,"",				"",					"OPTION"],
			["49", 		"窓レール"					,"大"				,"本"					,"",				"",					"OPTION"],
			["50", 		"窓レール"					,"小"				,"本"					,"",				"",					"OPTION"],
			["51", 		"網戸レール"					,"大"				,"本"					,"",				"",					"OPTION"],
			["52", 		"網戸レール"					,"小"				,"本"					,"",				"",					"OPTION"],
			["53", 		"ボルトナットセット"			,"300mm"			,"セット"				,"",				"",					"OPTION"],
			["54", 		"ボルトナットセット"			,"255mm"			,"セット"				,"",				"",					"OPTION"],
			["55", 		"ケミカルアンカーセット"		,""					,"セット"				,"",				"",					"NONE"],
			["56", 		"ビス、釘セット"				,""					,"一式"					,"",				"",					"OPTION"],
			["57", 		"ターカー用針"				,""					,""						,"",				"",					"OPTION"],
			["58", 		"コーキング"					,""					,"本"					,"",				"",					"OPTION"],
			["59", 		"シングルベストセメント"		,""					,"本"					,"",				"",					"OPTION"],
			["60", 		"ソフトマット"				,"1m"				,"本"					,"",				"",					"OPTION"],
			["61", 		"あて木"						,""					,""						,"",				"",					"NONE"],
			["62", 		"塗料"						,"外装（ ）"		,"Ｌ"					,"",				"",					"NONE"],
			["63", 		"塗料"						,"内装（白木)"		,"Ｌ"					,"",				"",					"NONE"],
			["64", 		"塗料"						,"補修用"			,"式"					,"",				"",					"NONE"],
			["65", 		"壁材取付用補助棒"				,""					,"式"					,"",				"",					"NONE"],
			["66", 		"高さ調整板"					,"厚み30mm"			,"枚"					,"",				"",					"NONE"],
			["67", 		"高さ調整板"					,"厚み10ｍｍ"		,"枚"					,"",				"",					"NONE"],
			["68", 		"束石"						,"50ｘ250ｘ250"		,"個"					,"",				"",					"NONE"],
			["69", 		"束石"						,"特注"				,"個"					,"",				"",					"NONE"],
			["70", 		"砂利"						,""					,"袋"					,"",				"",					"NONE"],
			["71", 		"砂"							,""					,"袋"					,"",				"",					"NONE"],
			["72", 		"セメント"					,""					,"袋"					,"",				"",					"NONE"],
			["73", 		"打ち込み杭"					,"ｍ"				,"本"					,"",				"",					"NONE"],
			["74", 		""							,""					,""						,"",				"",					"NONE"],
			["75", 		""							,""					,""						,"",				"",					"NONE"],
			["76", 		""							,""					,""						,"",				"",					"NONE"],
			["77", 		""							,""					,""						,"",				"",					"NONE"],
			["78", 		""							,""					,""						,"",				"",					"NONE"],
			["79", 		""							,""					,""						,"",				"",					"NONE"],
			["80", 		""							,""					,""						,"",				"",					"NONE"]];
	
var estimate = function () {
	saveDataToCsv();
    //updateEsitamateData();
    //viewEstimateForm();

}
var updateEsitamateData = function () {
    //フロアパネル　455mm x 455mmごとに ９枚
    data1[13][3] = 9 * ((floor.width) * (floor.height)) / (455 * 455) + "枚";

    //柱　柱の数
    data1[4][3] = pillarIndexStage.length + " 枚";

    //柱梁　柱の間の壁の数
    data1[5][3] = pillarIndexStage.length + " 枚"; // TBD

    //窓枠セット　窓の数
    data1[28][3] = countNumOfObjectTypeInWall("normalwindow") + " セット";
    data1[29][3] = countNumOfObjectTypeInWall("smallwindow") + " セット";

    //ウッドデッキパネル　ウッドデッキ　455mm x 455mm ごとに　１個
    data1[33][3] = ((floor.width) * (floor.height)) / (455 * 455) + "セット";

    //開きドア　開きドアの数
    data1[39][3] = countNumOfObjectTypeInWall("1wingDoor") + " 本";

    //引き違い戸　引き違い戸の数（現状はないが、オブジェクトに追加）
    data1[39][3] = countNumOfObjectTypeInWall("stepDoor") + " 本";

    //窓　窓の数
    data2[1][3] = countNumOfObjectTypeInWall("normalwindow") + " セット";
    data2[2][3] = countNumOfObjectTypeInWall("smallwindow") + " セット";

    //網戸　窓の数
    data2[4][3] = countNumOfObjectTypeInWall("normalwindow") + " セット";
    data2[5][3] = countNumOfObjectTypeInWall("smallwindow") + " セット";

    //ドア戸当金具　ドアの数 x ２
    data2[6][3] = 2 * (countNumOfObjectTypeInWall("normalwindow") + countNumOfObjectTypeInWall("smallwindow")) + " 個";
}

var countNumOfObjectTypeInWall = function (objName) {
    var cnt = 0;
    for (var i = 0; i < arrObjectInWall.length; i++) {
        var object = arrObjectInWall[i];
        if (object.oldObject[1] == objName) {
            cnt++;
        }

    }
    return cnt

}


// animation loop / game loop
var view2dFromEstimate = function () {
    document.getElementById('canvas').style.display = 'block';

    document.getElementById('view2d').style.display = 'none';
    document.getElementById('canvas3d').style.display = 'none';
    document.getElementById('inside').style.display = 'none';
    document.getElementById('outside').style.display = 'none';
    document.getElementById('no-roof').style.display = 'none';

    document.getElementById('es-container').style.display = 'none';


};
var viewEstimateForm = function () {

    document.getElementById('canvas').style.display = 'none';
    document.getElementById('view2d').style.display = 'none';
    document.getElementById('inside').style.display = 'none';
    document.getElementById('outside').style.display = 'none';
    document.getElementById('no-roof').style.display = 'none';
    document.getElementById('canvas3d').style.display = 'none';

    document.getElementById('es-container').style.display = 'block';

    var table1 = document.getElementById("estimate");
    // Clear table data
    //$("#estimate tr").remove();
	var headString ='<tr><th style="width: 25px; border-collapse: collapse; border: 1px solid #000;">番号</th><th style="width: 180px; border-collapse: collapse; border: 1px solid #000;">品名</th><th style="width: 80px; border-collapse: collapse; border: 1px solid #000;">備考</th><th style="width: 70px; border-collapse: collapse; border: 1px solid #000;">数量</th><th style="width: 60px; border-collapse: collapse; border: 1px solid #000;">チェック</th><th style="width: 60px; border-collapse: collapse; border: 1px solid #000;">チェック</th><th style="width: 15px; border-top: none; border-bottom:none;color:#fff;">Spaces</th><th style="width: 25px; border-collapse: collapse; border: 1px solid #000;">番号</th><th style="width: 180px; border-collapse: collapse; border: 1px solid #000;">品名</th> <th style="width: 80px; border-collapse: collapse; border: 1px solid #000;">備考</th><th style="width: 70px; border-collapse: collapse; border: 1px solid #000;">数量</th><th style="width: 60px; border-collapse: collapse; border: 1px solid #000;">チェック</th><th style="width: 60px; border-collapse: collapse; border: 1px solid #000;">チェック</th></tr>';
    
    table1.innerHTML = headString;

    for ( var i = 1; i <= numOfRow; i++)
    {
	    
	    var row = document.createElement("TR");
		// No
	    var no = document.createElement("td");
	    no.style.cssText = "border-collapse: collapse; border: 1px solid #000; text-align:center";
	    var noText = document.createTextNode(data1[i][0]);	    
	    no.appendChild(noText);
	    row.appendChild(no);

	    // Name
	    if(!( i==8 || i == 9 || i== 11 || i == 12 || i == 15 || i==16 || i == 19 || i == 28 || i == 36 )){
		    var name = document.createElement("td");
		    if(i == 7 || i == 10 || i == 14 ){ name.setAttribute("rowspan", "3");}
		    if(i == 18 || i == 27 || i == 35 ){ name.setAttribute("rowspan", "2");}
		    
		    name.style.cssText = "border-collapse: collapse; border: 1px solid #000; text-align:left;";
		    if (data1[i][6] == "MUST")
		    {
		    	name.bgColor = "Yellow";
		    }
		    else if(data1[i][6] == "WANT")
		    {
		    	name.bgColor = "#fecb9f";
		    }
		    else if(data1[i][6] == "OPTION")
		    {
		    	name.bgColor = "#Fe99c9";
		    }
		    var nameText = document.createTextNode(data1[i][1]);	    
		    name.appendChild(nameText);
		    row.appendChild(name);
	    }
	    var note = document.createElement("td");
	    note.style.cssText = "border-collapse: collapse; border: 1px solid #000; text-align:left;";
	    var noteText = document.createTextNode(data1[i][2]);
	    note.appendChild(noteText);
	    row.appendChild(note);
	    
	    var quality = document.createElement("td");
	    quality.style.cssText = "border-collapse: collapse; border: 1px solid #000; text-align:right;";
	    var qualityText = document.createTextNode(data1[i][3]);	    
	    quality.appendChild(qualityText);
	    row.appendChild(quality);

	    var check1 = document.createElement("td");
	    check1.style.cssText = "border-collapse: collapse; border: 1px solid #000;";
	    var check1Text = document.createTextNode(data1[i][4]);	    
	    check1.appendChild(check1Text);
	    row.appendChild(check1);


	    var check2 = document.createElement("td");
	    check2.style.cssText = "border-collapse: collapse; border: 1px solid #000;";
	    var check2Text = document.createTextNode(data1[i][5]);	    
	    check2.appendChild(check2Text);
	    row.appendChild(check2);

	    var free = document.createElement("td");
	    free.style.cssText = "border-top: none; border-bottom: 0;";
	    row.appendChild(free);

	    //======== Start for second table ============
	    // No
	    var no2 = document.createElement("td");
	    no2.style.cssText = "border-collapse: collapse; border: 1px solid #000; text-align:center";
	    var noText2 = document.createTextNode(data2[i][0]);	    
	    no2.appendChild(noText2);
	    row.appendChild(no2);

	    // Name
	    if(!( i == 2 || i == 4 || i == 5 || i == 10 || i == 12 || i == 14 || i == 23 || i == 24 || i == 27 || i == 29 )){
		    var name2 = document.createElement("td");
		    if(i == 3 || i == 22 ){ name2.setAttribute("rowspan", "3");}
		    if(i == 1 || i == 9 || i == 11 || i == 13 || i == 26 || i == 28 ){ name2.setAttribute("rowspan", "2");}
		    name2.style.cssText = "border-collapse: collapse; border: 1px solid #000; text-align:left;";
		    if (data2[i][6] == "MUST")
		    {
		    	name2.bgColor = "Yellow";
		    }
		    else if(data2[i][6] == "WANT")
		    {
		    	name2.bgColor = "#fecb9f";
		    }
		    else if(data2[i][6] == "OPTION")
		    {
		    	name2.bgColor = "#Fe99c9";
		    }
		    var nameText2 = document.createTextNode(data2[i][1]);	    
		    name2.appendChild(nameText2);
		    row.appendChild(name2);
	    }

	    var note2 = document.createElement("td");
	    note2.style.cssText = "border-collapse: collapse; border: 1px solid #000; text-align:left;";
	    var noteText2 = document.createTextNode(data2[i][2]);	    
	    note2.appendChild(noteText2);
	    row.appendChild(note2);

	    var quality2 = document.createElement("td");
	    quality2.style.cssText = "border-collapse: collapse; border: 1px solid #000; text-align:right;";
	    var qualityText2 = document.createTextNode(data2[i][3]);	    
	    quality2.appendChild(qualityText2);
	    row.appendChild(quality2);

	    var check12 = document.createElement("td");
	    check12.style.cssText = "border-collapse: collapse; border: 1px solid #000;";
	    var check12Text = document.createTextNode(data2[i][4]);	    
	    check12.appendChild(check12Text);
	    row.appendChild(check12);

	    var check22 = document.createElement("td");
	    check22.style.cssText = "border-collapse: collapse; border: 1px solid #000;";
	    var check22Text = document.createTextNode(data2[i][5]);	    
	    check22.appendChild(check22Text);
	    row.appendChild(check22);
	    
	    table1.appendChild(row);

    }
    // Draw the note
    var row1 = document.createElement("tr");
    row1.style.cssText = "text-align:left;";
    row1.innerHTML = '<td colspan="13" style="color:#fff">free</td>';
    table1.appendChild(row1);
    
    
	var row3 = document.createElement("tr");
    row3.style.cssText = "text-align:left;";
    row3.innerHTML = '<td></td><td bgcolor="Yellow" style="width: 60px; border-collapse: collapse; border: 1px solid #000;"></td><td>必須</td><td colspan="9">※必須 対応。「拾いたい」 、簡単に出力できる部分であれ 出力する。「できれ 拾いたい」 、追加費用で対応。</td>';
	table1.appendChild(row3);

	var row2 = document.createElement("tr");
    row2.style.cssText = "text-align:left;";
    row2.innerHTML = '<td></td><td bgcolor="#fecb9f" style="width: 60px; border-collapse: collapse; border: 1px solid #000;"></td><td>拾いたい</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>';
	table1.appendChild(row2);


	var row4 = document.createElement("tr");
    row4.style.cssText = "text-align:left;";
	row4.innerHTML = '<td></td><td bgcolor="#Fe99c9" style="width: 60px; border-collapse: collapse; border: 1px solid #000;"></td><td>できれ 拾いたい</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>';
	table1.appendChild(row4);

    
};

var requestSendEstimate = function () {
	// var htmlData = document.getElementById('estimate').outerHTML;
	// document.getElementById('htmlData').value = htmlData;
	// var form = document.getElementById('EsForm');
	// form.submit()
	var reEmail = document.getElementById('reEmail').value;
	if(reEmail == ""){ alert('Must input received email address !'); return;}
	if(!validateEmail(reEmail)){ alert('Input email is not corrected !'); return;}

	document.getElementById('mstatus').innerHTML = 'Sending an email ...';
	// Set estimate data to hidden file
	var htmlData = document.getElementById('estimate').outerHTML;
	var reEmail = document.getElementById('reEmail').value;
  	//var data = 'reEmail=' + reEmail + '& htmlData=' + htmlData;
  	var data = {
  			reEmail: reEmail,
  			htmlData: htmlData
		};
  	$.post("sendEstimateEmail.php", data, 
  		function(result){
			alert(result);
			document.getElementById('mstatus').innerHTML = '';
		})
  	
};

var validateEmail = function(email) {
  var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  return re.test(email);
}


