var cond  = false;
var level = false;

var API = '../estimate/qry.php';

function clearHistory() {
	$('input:[id=area]').val('');
	$('input:[id=tsubo]').val('');
	$('input:[id=input_cost]').val('');
	$('input:[id=input_cost_tsubo]').val('');
	$('input:[id=email]').val('');
	$('input:[id=input_addr]').val('');

	$('select:[id=prefecture]').val('--');
	$('select:[id=q2]').val('--');
	$('select:[id=q1]').val('--');
	$('select:[id=impr]').val('1');
	$('select:[id=year]').val('1');
	$('select:[id=uses2]').val('25');
	$('select:[id=uses]').val('-3');
	
	$('input:[id=input_cost]').css('background-color', '#ffffff');
	$('input:[id=input_cost_tsubo]').css('background-color', '#ffffff');
	
	// 初期座標
	var point = new GLatLng(35.672497,139.722354);
	map.setCenter(point, 17);
}

var current = 1;
function tab(index) {
	// 自分自身のみ
	if (index == 1) {
		if (current == index) {
			hiddenP2();
			hiddenP3();
			showP1();
			clearHistory();
			current = index;
			
			$('a:[id=tab_2]').css('cursor', 'none');
			$('a:[id=tab_3]').css('cursor', 'none');
			$('a:[id=tab_1]').css('cursor', 'pointer');
		} else {
			return;
		}
	} else if (index == 2) {
		if (current == index) {
			hiddenP3();
			hiddenP1();
			showP2();
			current = index;
			
			$('a:[id=tab_1]').css('cursor', 'none');
			$('a:[id=tab_3]').css('cursor', 'none');
			$('a:[id=tab_2]').css('cursor', 'pointer');
		} else {
			return;
		}
	} else if (index == 3) {
		if (current == index) {
			hiddenP1();
			hiddenP2();
			showP3();
			clearHistory();
			current = index;
			
			$('a:[id=tab_1]').css('cursor', 'none');
			$('a:[id=tab_2]').css('cursor', 'none');
			$('a:[id=tab_3]').css('cursor', 'pointer');
		} else {
			return;
		}
	}

	// 全ての画像・BOXをパッシブに
	$('img:[id=img_step_1]').attr('src', '../images/diagnosis/step1_off.gif');
	$('img:[id=img_step_2]').attr('src', '../images/diagnosis/step2_off.gif');
	$('img:[id=img_step_3]').attr('src', '../images/diagnosis/step3_off.gif');
	$('div:[id^=step_]').css('display', 'none');

	// 指定画像・BOXをアクティブに
	$('img:[id=img_step_'+index+']').attr('src', '../images/diagnosis/step'+index+'_on.gif');
	$('div:[id=step_'+index+']').css('display', 'block');
}

function geocode() {
	var geo = new GClientGeocoder();
	geo.getLatLng($('input:[id=input_addr]').val(), checkGeo);
}

function checkGeo(point) {
	if(point) {
		map.setCenter(point, map.getZoom());
	}
}

// mod 09/05/18
var gm_center = null;
function checkMap() {
	var cost = $('input:[id=input_cost]').val();
	if (cost) {
		cost = zenTohan(cost);
		if (!isNumeric(cost)) {
			alert('[平米単価]入力に誤りがあります。');
			return;
		}
	} else {
		try {
			/*
			var center = map.getCenter();

			// 通信開始
			$.ajax({
				dataType: 'xml',
				data: {
					'm': 'pricexml',
					'x': center.lng(),
					'y': center.lat()
				},
				cache: false,
				url: API,
				success: checkData2,
				error: checkDataError,
				complete: function() {}
			});
			*/
			//alert(gm_center.lng()+'\n'+gm_center.lat());
			// 通信開始
			$.ajax({
				dataType: 'xml',
				data: {
					'm': 'pricexml',
					'x': gm_center.lng(),
					'y': gm_center.lat()
				},
				cache: false,
				url: API,
				success: checkData2,
				error: checkDataError,
				complete: function() {}
			});
		} catch (e) {
			alert('処理に失敗しました。');
		} finally {}
	}
}

function setMap() {
gm_center = map.getCenter();
gotoStep2();
}
function gotoStep1() {
	gm_center = null;

	cond = true;
	current = 1;
	tab(1);
}
function gotoStep2() {
	cond = true;
	current = 2;
	tab(2);
}

function checkData(xml) {
	try {
		// 解析
		$(xml).find('landsolution').each(function() {
			if ($('status', this).text() === 'success') {
				cond = true;
				tab(2);
			} else {
				cond = false;
				level = false;
				alert('ご指定の住所の近くには、地価公示の標準地がありません。\n診断対象の土地の地価を直接入力してください。');
			}
		});
	} catch (e) {
		alert('処理に失敗しました。');
	} finally {}
}
function checkData2(xml) {
	try {
		// 解析
		$(xml).find('landsolution').each(function() {
			if ($('status', this).text() === 'success') {
				//estimate();
			} else {
				alert('ご指定の住所の近くには、地価公示の標準地がありません。診断対象の土地の地価を直接入力してください。');
				$('input:[id=input_cost]').css('background-color', '#ffcde5');
				$('input:[id=input_cost_tsubo]').css('background-color', '#ffcde5');
				return;
			}
		});
	} catch (e) {
		alert('処理に失敗しました。');
	} finally {}
}

function checkDataError(xml, status, e) {
	alert('処理に失敗しました。');
}

function estimate() {
	$('input:[id=pkid]').val('');

	var area = $('input:[id=area]').val();
	if (area) {
		area = zenTohan(area);
		if (!isNumeric(area)) {
			alert('[面積]入力値に誤りがあります。');
			return;
		}
		if (parseInt(area) < 0 || parseInt(area) > 500000) {
			alert('[面積]入力は 0～500,000 までとなります。');
			return;
		}
	} else {
		alert('[面積]入力されていません。');
		return;
	}

	var email = $('input:[id=email]').val();
	if (!email) {
		alert('[Eメール]入力されていません。');
		return;
	}

	var tmp_q1 = $('select:[id=q1]').children('option:selected').val();
	if (tmp_q1 === '--') {
		alert('利用目的が選択されていません。');
		return;
	}

	var tmp_q2 = $('select:[id=q2]').children('option:selected').val();
	if (tmp_q2 === '--') {
		alert('業種が選択されていません。');
		return;
	}

	var tmp_prefecture = $('select:[id=prefecture]').children('option:selected').val();
	if (tmp_prefecture === '--') {
		alert('所在地が選択されていません。');
		return;
	}

	var uses = $('select:[id=uses]').children('option:selected').val();
	if (uses === '-1' || uses === '-2' || uses === '-3') {
		alert('[用途]選択されていません。');
		return;
	}
	if (uses === '99') {
		uses = $('select:[id=uses2]').children('option:selected').val()
	}
	
	var cost = $('input:[id=input_cost]').val();
	if (cost) {
		cost = zenTohan(cost);
		if (!isNumeric(cost)) {
			alert('[平米単価]入力に誤りがあります。');
			return;
		}
	} else {
	}

	try {
/*
		var center = map.getCenter();

		// 通信開始
		$.ajax({
			dataType: 'xml',
			data: {
				'm': 'estimate',
				'x': center.lng(),
				'y': center.lat(),
				'uses': uses,
				'area': area,
				'year': $('select:[id=year]').children('option:selected').val(),
				'impr': $('select:[id=impr]').children('option:selected').val(),
				'prefecture': $('select:[id=prefecture]').children('option:selected').val(),
				'email': email,
				'cost': cost,
				'q1': $('select:[id=q1]').children('option:selected').val(),
				'q2': $('select:[id=q2]').children('option:selected').val()
			},
			type: 'POST',
			cache: false,
			url: API,
			success: checkEstimate,
			error: checkEstimateError,
			complete: function() {}
		});
*/
		// 通信開始
		$.ajax({
			dataType: 'xml',
			data: {
				'm': 'estimate',
				'x': gm_center.lng(),
				'y': gm_center.lat(),
				'uses': uses,
				'area': area,
				'year': $('select:[id=year]').children('option:selected').val(),
				'impr': $('select:[id=impr]').children('option:selected').val(),
				'prefecture': $('select:[id=prefecture]').children('option:selected').val(),
				'email': email,
				'cost': cost,
				'q1': $('select:[id=q1]').children('option:selected').val(),
				'q2': $('select:[id=q2]').children('option:selected').val()
			},
			type: 'POST',
			cache: false,
			url: API,
			success: checkEstimate,
			error: checkEstimateError,
			complete: function() {}
		});
	} catch (e) {
		alert('処理に失敗しました。');
	} finally {}
}

function checkEstimate(xml) {
	try {
		// 解析
		$(xml).find('landsolution').find('estimate').each(function() {
			if ($('status', this).text() === 'success') {
				level = true;
/*
				$('dt:[id^=result_]').removeClass('check');
				$('dt:[id=result_'+$('level', this).text()+']').addClass('check');
*/			
				$('div:[id^=level_]').css('display', 'none');
				$('div:[id=level_'+$('level', this).text()+']').css('display', 'block');

				$('img:[id=img_level]').attr('src', '../images/diagnosis/level'+$('level', this).text()+'.gif');
//				$('div:[id=img_level]').html('<img src="../images/diagnosis/level'+$('level', this).text()+'.gif" width="480" height="170"/>');

				$('input:[id=pkid]').val($('logpkid', this).text());
				current = 3;
				tab(3);
				location.href = '#jmp'; 
			} else {
				level = false;
			}
		});
	} catch (e) {
		alert('処理に失敗しました。');
	} finally {}
}

function checkEstimateError(xml, status, e) {
	alert('処理に失敗しました。');
}

function createHierarchy() {
	var index = $('select:[id=uses]').children('option:selected').val();
	if (index === '99') {
		$('select:[id=uses2]').css('display', 'block');
	} else {
		$('select:[id=uses2]').css('display', 'none');
	}
}

function contact() {
	//var url = './diagnosis.php?pkid='+$('input:[id=pkid]').val();
	//var url = 'https://www.landsolution.co.jp/diagnosis.php?pkid='+$('input:[id=pkid]').val();
	var url = 'https://www.landsolution.co.jp/diagnosis/diagnosis.php?pkid='+$('input:[id=pkid]').val();
	location.href = url;
}

function zenTohan(txt) {
	var han = "0123456789.,-+";
	var zen = "０１２３４５６７８９．，－＋";
	var str = "";
	for (i=0; i<txt.length; i++) {
		c = txt.charAt(i);
		n = zen.indexOf(c,0);
		
		if (n >= 0) c = han.charAt(n);
		str += c;
	}
	return str;
}

function isNumeric(argValue) {
/*
	if (num.match(/[^0-9]/g)) {
		return false;
	}
	return true;
*/
	if (argValue.match(/[^0-9|^.]/g)) {
		alert('有効な数値ではありません');
		return false;
	}

	if (argValue.charAt(0) == ".") {
		alert('有効な数値ではありません');
		return false;
	}
	return true;  
}

function toM() {
	var tsubo;

	if (!$('input:[id=tsubo]').val()) {
		$('input:[id=area]').val('');
		return;
	}

	tsubo = $('input:[id=tsubo]').val();
	tsubo = removeComma(tsubo);
	tsubo = zenTohan(tsubo);
	$('input:[id=tsubo]').val(tsubo)

	if (!isNumeric(tsubo)) {
		$('input:[id=tsubo]').val('');
		return;
	}
	var m = parseFloat(tsubo);
	m = m*3.3057;
	//m = m*0.3025;
	//$('input:[id=area]').val(Math.round(m*100000)/100000);
	$('input:[id=area]').val(Math.round(m*10)/10);
}
function toTsubo() {
	var area;

	if (!$('input:[id=area]').val()) {
		$('input:[id=tsubo]').val('');
		return;
	}

	area = $('input:[id=area]').val();
	area = removeComma(area);
	area = zenTohan(area);
	$('input:[id=area]').val(area);

	if (!isNumeric(area)) {
		$('input:[id=area]').val('');
		return;
	}
	var m = parseFloat(area);
	m = m*0.3025;
	//m = m*3.3057;
	//$('input:[id=tsubo]').val(Math.round(m*100000)/100000);
	$('input:[id=tsubo]').val(Math.round(m*10)/10);
}

function toMCost() {
	var cost_tsubo;

	if (!$('input:[id=input_cost_tsubo]').val()) {
		$('input:[id=input_cost]').val('');
		return;
	}

	cost_tsubo = $('input:[id=input_cost_tsubo]').val();
	cost_tsubo = removeComma(cost_tsubo);
	cost_tsubo = zenTohan(cost_tsubo);
	$('input:[id=input_cost_tsubo]').val(cost_tsubo);

	if (!isNumeric(cost_tsubo)) {
		$('input:[id=input_cost_tsubo]').val('');
		return;
	}
	var m = parseFloat(cost_tsubo);
	//m = m*3.3057;
	m = m*0.3025;
	//$('input:[id=input_cost]').val(Math.round(m*100000)/100000);
	$('input:[id=input_cost]').val(Math.round(m*10)/10);
}
function toTsuboCost() {
	var costm;

	if (!$('input:[id=input_cost]').val()) {
		$('input:[id=input_cost_tsubo]').val('');
		return;
	}

	costm = $('input:[id=input_cost]').val();
	costm = removeComma(costm);
	costm = zenTohan(costm);
	$('input:[id=input_cost]').val(costm);
	
	if (!isNumeric(costm)) {
		$('input:[id=input_cost]').val('');
		return;
	}
	var m = parseFloat(costm);
	//m = m*0.3025;
	m = m*3.3057;
	//$('input:[id=input_cost_tsubo]').val(Math.round(m*100000)/100000);
	$('input:[id=input_cost_tsubo]').val(Math.round(m*10)/10);
}

function hiddenP1() {
	$('p:[id=txt1]').css('display', 'none');
}
function hiddenP2() {
	$('p:[id=txt2]').css('display', 'none');
	$('div:[id=clear_button]').css('display', 'none');
}
function hiddenP3() {
	$('p:[id=txt3]').css('display', 'none');
	$('div:[id=clear_button]').css('display', 'none');
}
function showP1() {
	$('p:[id=txt1]').css('display', 'block');
}
function showP2() {
	$('p:[id=txt2]').css('display', 'block');
	$('div:[id=clear_button]').css('display', 'block');
}
function showP3() {
	$('p:[id=txt3]').css('display', 'block');
	$('div:[id=clear_button]').css('display', 'block');
}

function removeComma(value) {
    return value.split(",").join("")
}