
var ANTHRO_FLAG = 1;
var Parameter1, Parameter2;

function jsAnthroCalanthro()
{
	if(jsCheckAnthroForm() == false)
		return false;
	document.anthroinfo.method = "POST";
	document.anthroinfo.action = "AnthroTool.php";	
	document.anthroinfo.subcalanthro.value = 1;
	document.anthroinfo.submit(); 
}

function jsAnthroCalanthroMulti()
{
	
	if(jsCheckAnthroForm() == false)
		return false;
	
	document.anthroinfo.method = "POST";
	document.anthroinfo.action = "AnthroTool.php?actions=multientries";	
	document.anthroinfo.subcalanthromulti.value = 1;
	document.anthroinfo.submit(); 
}

function jsAnthroSaveanthro()
{
	if(jsCheckAnthroForm() == false)
		return false;

	document.anthroinfo.method = "POST";
	document.anthroinfo.action = "AnthroProcess.php";	
	document.anthroinfo.subsaveanthro.value = 1;
	document.anthroinfo.submit(); 
}

function jsAnthroSaveanthroMulti()
{
	if(jsCheckAnthroForm() == false)
		return false;

	document.anthroinfo.method = "POST";
	document.anthroinfo.action = "AnthroProcess.php";	
	document.anthroinfo.subsaveanthromulti.value = 1;
	document.anthroinfo.submit(); 
}

function jsAddAnalysToReport()
{
	var selectedvalue = document.forms['editanthroreport'].analysis.value;
	if(selectedvalue)
		document.forms['editanthroreport'].analysisText.value +=  selectedvalue;
}

function jsAddSuggestToReport()
{
	var selectedvalue = document.forms['editanthroreport'].suggest.value;
	if(selectedvalue)
		document.forms['editanthroreport'].suggestText.value +=  selectedvalue;
}

function jsAddDoctorToReport()
{
	//alert(document.editanthroreport.suggest.clientWidth);
	var selectedvalue = document.forms['editanthroreport'].doctorstaff.value;
	if(selectedvalue)
		document.forms['editanthroreport'].doctor.value = selectedvalue;	
}


function jsAddExecuterToReport()
{
	var selectedvalue = document.forms['editanthroreport'].executerstaff.value;
	if(selectedvalue)
		document.forms['editanthroreport'].executer.value =  selectedvalue;
}

function jsSaveReportToClient(path)
{
	var fso;
	var stream;
	var ForReading = 1;
	var xmlDoc;
	
	//alert(path);
	
	// code for IE
	if (window.ActiveXObject)
	{
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	}
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc=document.implementation.createDocument("","",null);
	}
	else
	{
		alert('Your browser cannot handle this script');
	}
	
	xmlDoc.async=false;
	
	reporttag = xmlDoc.createElement("report");

	
	nodetag = xmlDoc.createElement("anthroid");
	nodetag.appendChild(xmlDoc.createTextNode(document.forms['editanthroreport'].anthroid.value));
	reporttag.appendChild(nodetag);
	
	nodetag = xmlDoc.createElement("analysis");
	nodetag.appendChild(xmlDoc.createTextNode(document.forms['editanthroreport'].analysisText.value));
	reporttag.appendChild(nodetag);

	nodetag = xmlDoc.createElement("suggest");
	nodetag.appendChild(xmlDoc.createTextNode(document.forms['editanthroreport'].suggestText.value));
	reporttag.appendChild(nodetag);

	nodetag = xmlDoc.createElement("client");
	nodetag.appendChild(xmlDoc.createTextNode(document.forms['editanthroreport'].client.value));
	reporttag.appendChild(nodetag);

	nodetag = xmlDoc.createElement("executer");
	nodetag.appendChild(xmlDoc.createTextNode(document.forms['editanthroreport'].executer.value));
	reporttag.appendChild(nodetag);

	nodetag = xmlDoc.createElement("doctor");
	nodetag.appendChild(xmlDoc.createTextNode(document.forms['editanthroreport'].doctor.value));
	reporttag.appendChild(nodetag);

	xmlDoc.appendChild(reporttag);
	
	//alert(xmlDoc.xml);
	
	
	// code for IE
	if (window.ActiveXObject)
	{
		
		fso = new ActiveXObject("Scripting.FileSystemObject"); 
		//if(fso.FolderExists("c:\\surveysystem\\") == false)
			//fso.CreateFolder("c:\\surveysystem\\");
		//alert("TEST");
		if(fso.FolderExists(path) == false)
			fso.CreateFolder(path);
		
		var fullfilename = path + document.forms['editanthroreport'].anthroid.value + ".xml";
		//alert(fullfilename);
		stream = fso.CreateTextFile(fullfilename, true, true);
	}
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation && document.implementation.createDocument)
	{
		//xmlDoc=document.implementation.createDocument("","",null);
	}
	else
	{
		alert('Your browser cannot handle this script');
	}
	
	stream.Write(xmlDoc.xml);
	stream.close();
	return true;
}

function jsSaveReport(path)
{	
	if(!jsSaveReportToClient(path))
	{
		//alert("失敗");
		return;		
	}
	alert("報告儲存至:" + path);
		
	document.forms['editanthroreport'].subaddanthroreport.value = 1;
	//alert(document.forms['editreport'].subaddreport.value);
	document.forms['editanthroreport'].method = "POST";
	document.forms['editanthroreport'].action = "AnthroProcess.php";
	document.forms['editanthroreport'].submit();	
}

function jsSavePrintReport(path)
{
	if(!jsSaveReportToClient(path))
	{
		//alert("失敗");
		return;		
	}
	alert("報告儲存至:" + path);
		
	//document.forms['editanthroreport'].subaddanthroreportwprint.value = 1;
	document.forms['editanthroreport'].method = "POST";
	document.forms['editanthroreport'].action = "AnthroTool.php?actions=print";
	document.forms['editanthroreport'].submit();	
}


function jsQuitReport()
{		
	document.forms['editanthroreport'].subquitreport.value = 1;
	document.forms['editanthroreport'].method = "POST";
	document.forms['editanthroreport'].action = "AnthroProcess.php";
	document.forms['editanthroreport'].submit();	
}

function loadXMLDoc(filepath)
{
	//alert(filepath);
	var xmlDoc;
	var fso;
	var stream;
	var ForReading = 1;
	var TristateTrue = -1;
	// code for IE
	if (window.ActiveXObject)
	{
		fso = new ActiveXObject("Scripting.FileSystemObject"); 
		//stream = fso.OpenTextFile("c:\\note.xml", ForReading, false);
		if(fso.FileExists(filepath) == false)
			return null;
		stream = fso.OpenTextFile(filepath, ForReading, false, TristateTrue);
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	}
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc=document.implementation.createDocument("","",null);
	}
	else
	{
		alert('Your browser cannot handle this script');
	}
	
	var xmlString = stream.ReadAll();
	//alert(xmlString);
	stream.close();
	xmlDoc.async=false;
	xmlDoc.loadXML(xmlString);
	return(xmlDoc);
}

function displayReportData(filepath)
{
	//alert(filepath);
	var xmlFile = loadXMLDoc(filepath);	
	if(!xmlFile)
		return;
	if(xmlFile.getElementsByTagName("analysis")[0].hasChildNodes())
		document.forms['editanthroreport'].analysisText.value = xmlFile.getElementsByTagName("analysis")[0].childNodes[0].nodeValue;
	if(xmlFile.getElementsByTagName("suggest")[0].hasChildNodes())
		document.forms['editanthroreport'].suggestText.value = xmlFile.getElementsByTagName("suggest")[0].childNodes[0].nodeValue;
	if(xmlFile.getElementsByTagName("client")[0].hasChildNodes())
		document.forms['editanthroreport'].client.value = xmlFile.getElementsByTagName("client")[0].childNodes[0].nodeValue;
	if(xmlFile.getElementsByTagName("executer")[0].hasChildNodes())
		document.forms['editanthroreport'].executer.value = xmlFile.getElementsByTagName("executer")[0].childNodes[0].nodeValue;	
	if(xmlFile.getElementsByTagName("doctor")[0].hasChildNodes())
		document.forms['editanthroreport'].doctor.value = xmlFile.getElementsByTagName("doctor")[0].childNodes[0].nodeValue;
	
}

//For comment
function jsAddComment(ctype)
{	
	if(ctype == 3 && !document.forms['maganthroreport'].newanalys.value)
		return;
	if(ctype == 4 && !document.forms['maganthroreport'].newsuggest.value)
		return;	
	
	document.forms['maganthroreport'].method = "POST";
	document.forms['maganthroreport'].action = "AnthroProcess.php";
	document.forms['maganthroreport'].ctype.value = ctype;
	document.forms['maganthroreport'].subaddcomment.value = 1;
	document.forms['maganthroreport'].submit();
}

function jsDelComment($type)
{	
	if(!window.confirm("確認要刪除這筆資料嗎")) {
		return;
	}
	
	if($type == 3)
		document.forms['maganthroreport'].rid.value = document.forms['maganthroreport'].analysis.value;
	else if($type == 4)
		document.forms['maganthroreport'].rid.value = document.forms['maganthroreport'].suggest.value;
	else return;
	
	if(!document.forms['maganthroreport'].rid.value)
	{
		return;
	}
	document.forms['maganthroreport'].method = "POST";
	document.forms['maganthroreport'].action = "AnthroProcess.php";
	document.forms['maganthroreport'].subdelcomment.value = 1;
	document.forms['maganthroreport'].submit();
}

function jsReturnToSystem()
{
	location.href = "AnthroTool.php";
}

//Check Form
function isValue(str)
{	
	if(!str.match(/^[0-9]*(\.[0-9]+)?$/))
	{		
		return false;	
	}
	return true;
}

function isDate2(str)
{
	if(!str.match(/^\d{4}\-\d\d?\-\d\d?$/))
		return false;
		
	var ar = str.replace(/\-0/g,"-").split("-");   
	ar = new Array(parseInt(ar[0]),parseInt(ar[1])-1,parseInt(ar[2]));
	var d = new Date(ar[0],ar[1],ar[2]);	
	return d.getFullYear()==ar[0] && d.getMonth()==ar[1] && d.getDate()==ar[2];   
}   

function jsCheckAnthroForm()
{
	var i = 0;
	
	var birth = document.getElementsByName("at_birthday")[0].value;
	if(birth == "")
	{
		alert("請填出生日期");
		return false;
	}
	
	if(isDate2(birth) == false)
	{
		alert("出生日期格式錯誤，請修正。")
		return false;
	}
	
	//For single expdate
	var expdate = document.getElementsByName("at_expdate");	
	for(i = 0; i < expdate.length; i++)
	{
		if(expdate[i].value == "")
		{
			alert("請填測量日期");
			return false;		
		}				
		if(isDate2(expdate[i].value) == false)
		{
			alert("測量日期格式錯誤，請修正。")
			return false;
		}		
	}

	//For multi expdate
	for(i = 0; i < 10; i++)
	{		
		var datearray = document.getElementsByName("at_expdate" + i);
		
		for(var j = 0; j < datearray.length; j++)
		{
			if( (datearray[j].value != "") && (isDate2(datearray[j].value) == false))
			{
				alert("測量日期格式錯誤，請修正。")
				return false;
			}					
		}		
	}
	
	/*
	if(document.forms['anthroinfo'].at_birthday.value == "")
	{
		alert("請填出生日期");
		return false;		
	}
	
	if(isDate2(document.forms['anthroinfo'].at_birthday.value) == false)
	{
		alert("出生日期格式錯誤，請修正。")
		return false;
	}
	
	if(document.forms['anthroinfo'].at_expdate.value == "")
	{
		alert("請填測量日期");
		return false;		
	}
	if(isDate2(document.forms['anthroinfo'].at_expdate.value) == false)
	{
		alert("測量日期格式錯誤，請修正。")
		return false;
	}
	*/
	
	//if(document.forms['anthroinfo'].at_height.value == "")
	
	var heightarray = document.getElementsByName("at_height[]");
	for(i = 0; i < heightarray.length; i++)
	{
		if(!isValue(heightarray[i].value))
		{
			alert("身高格式錯誤");
			return false;		
		}
	}
	
	var warray = document.getElementsByName("at_weight[]");
	for(i = 0; i < warray.length; i++)
	{
		if(!isValue(warray[i].value))
		{
			alert("體重格式錯誤");
			return false;		
		}
	}

	var headarray = document.getElementsByName("at_headcircu[]");
	for(i = 0; i < headarray.length; i++)
	{
		if(!isValue(headarray[i].value))
		{
			alert("頭圍格式錯誤");
			return false;		
		}
	}

	/*
	if(!isValue(document.forms['anthroinfo'].at_height.value))
	{
		alert("身高格式錯誤");
		return false;		
	}
	
	if(!isValue(document.forms['anthroinfo'].at_weight.value))
	{
		alert("體重格式錯誤");
		return false;		
	}

	if(!isValue(document.forms['anthroinfo'].at_headcircu.value))
	{
		alert("頭圍格式錯誤");
		return false;		
	}
	*/	
	return true;	
}

//Del record
function jsDelAnthroid()
{
	document.forms['listrecord'].subdelanthroid.value = 1;	
	document.forms['listrecord'].method = "POST";
	document.forms['listrecord'].action = "AnthroProcess.php";
	document.forms['listrecord'].submit();	
	
}
function jsDelRecord(delexpdate)
{
	document.forms['listrecord'].subdelrecord.value = 1;
	document.forms['listrecord'].expdatetodel.value = delexpdate;
	document.forms['listrecord'].method = "POST";
	document.forms['listrecord'].action = "AnthroProcess.php";
	document.forms['listrecord'].submit();	
	
}

//Todo: Add javascript to convert date
//
function SetUpdateTaiwanDateParm(taiwandate, globaldate)
{
	Parameter1 = taiwandate;
	Parameter2 = globaldate;
}

function UpdateTaiwanDate()
{	
	var taiwandate = Parameter1;
	var globaldate = Parameter2;
	//alert(taiwandate);
	var gdatestrobj = document.getElementsByName(globaldate);	
	var gdatestr = gdatestrobj[0].value;
	
	
	if(gdatestr)
	{
		var dsarray = gdatestr.replace(/\-0/g,"-").split("-");
		var year = parseInt(dsarray[0]) - 1911;
		document.getElementsByName(taiwandate)[0].value = year;
		document.getElementsByName(taiwandate)[1].value = dsarray[1];
		document.getElementsByName(taiwandate)[2].value = dsarray[2];
	}

/*	
	if(document.forms['anthroinfo'].at_birthday.value)
	{
		var datestring = document.forms['anthroinfo'].at_birthday.value;
		var dsarray = datestring.replace(/\-0/g,"-").split("-");	
		//alert(parseInt(dsarray[1]));
	
		document.forms['anthroinfo'].at_taiwanbirth[0].value = parseInt(dsarray[0]) - 1911;
		document.forms['anthroinfo'].at_taiwanbirth[1].value = parseInt(dsarray[1]);
		document.forms['anthroinfo'].at_taiwanbirth[2].value = parseInt(dsarray[2]);
		//alert(document.forms['anthroinfo'].at_birthyear.value);
	}

	if(document.forms['anthroinfo'].at_expdate.value)
	{
		var datestring = document.forms['anthroinfo'].at_expdate.value;
		var dsarray = datestring.replace(/\-0/g,"-").split("-");	
		//alert(parseInt(dsarray[1]));
	
		document.forms['anthroinfo'].at_taiwanexpdate[0].value = parseInt(dsarray[0]) - 1911;
		document.forms['anthroinfo'].at_taiwanexpdate[1].value = parseInt(dsarray[1]);
		document.forms['anthroinfo'].at_taiwanexpdate[2].value = parseInt(dsarray[2]);
		//alert(document.forms['anthroinfo'].at_birthyear.value);		
	}
*/	
}

function UpdateGlobalDate(taiwandate, globaldate)
{
	/*
	var year = document.forms['anthroinfo'].at_taiwanbirth[0].value;
	var month = document.forms['anthroinfo'].at_taiwanbirth[1].value;
	var day = document.forms['anthroinfo'].at_taiwanbirth[2].value;
	*/
	//alert(taiwandate);
	var item = document.getElementsByName(taiwandate);	
	var year = item[0].value;
	var month = item[1].value;
	var day = item[2].value;
	
	if( year || month || day)
	{		
		var year = parseInt(item[0].value) + 1911;		
		if(month && (month < 10))
			month = '0' + month;
		if(day && (day < 10))
			day = '0' + day;
				
		document.getElementsByName(globaldate)[0].value = year + '-' + month + '-' + day;
		//document.forms['anthroinfo'].at_birthday.value = year + '-' + month + '-' + day;
	}	
	

/*
	if( year || month || day)
	{		
		year = parseInt(year) + 1911;		
		if(month && (month < 10))
			month = '0' + month;
		if(day && (day < 10))
			day = '0' + day;
		document.forms['anthroinfo'].at_birthday.value = year + '-' + month + '-' + day;
	}	

	year = document.forms['anthroinfo'].at_taiwanexpdate[0].value;
	month = document.forms['anthroinfo'].at_taiwanexpdate[1].value;
	day = document.forms['anthroinfo'].at_taiwanexpdate[2].value;
	
	if( year || month || day)
	{		
		year = parseInt(year) + 1911;		
		if(month < 10)
			month = '0' + month;
		if(day < 10)
			day = '0' + day;
		document.forms['anthroinfo'].at_expdate.value = year + '-' + month + '-' + day;
	}	
*/
}