var returnValueStr = false;
function finishAction( value , frm)
{  	
	document.getElementById("action").value = value;
	var reconciletype = document.getElementById("reconciletype").value;

	var samplItem = [];
	var method = "post";
	var path="/ibase/webitm/jsp/ICRWizardSaveSample.jsp";

	var valueXmlString = " <?xml version = \"1.0\"?> <Root> <header> <editFlag> ";
	valueXmlString += " </editFlag> </header> ";
	valueXmlString += " <Detail1> ";
	valueXmlString += " <from_tran_date><![CDATA["+ document.getElementById("from_tran_date").value +"]]></from_tran_date> ";
	valueXmlString += " <to_tran_date><![CDATA["+ document.getElementById("to_tran_date").value +"]]></to_tran_date> ";
	valueXmlString += " <site_code_fr><![CDATA["+ document.getElementById("site_code_fr").value +"]]></site_code_fr> ";
	valueXmlString += " <site_code_to><![CDATA["+ document.getElementById("site_code_to").value +"]]></site_code_to> ";
	valueXmlString += " <chg_user><![CDATA["+ document.getElementById("chg_user").value +"]]></chg_user> ";
	valueXmlString += " <chg_term><![CDATA["+ document.getElementById("chg_term").value +"]]></chg_term> ";
	valueXmlString += " <sundry_type_fr><![CDATA["+ document.getElementById("sundry_type_fr").value +"]]></sundry_type_fr> ";
	valueXmlString += " <sundry_type_to><![CDATA["+ document.getElementById("sundry_type_to").value +"]]></sundry_type_to> ";
	valueXmlString += " <sundry_code_fr><![CDATA["+ document.getElementById("sundry_code_fr").value +"]]></sundry_code_fr> ";
	valueXmlString += " <sundry_code_to><![CDATA["+ document.getElementById("sundry_code_to").value +"]]></sundry_code_to> ";
	valueXmlString += " </Detail1> ";
	valueXmlString += getCheckedValues(frm);
	valueXmlString += " </Root> "; 
	
	samplItem["SAVE_SAMPLE_DATA"] = valueXmlString;
	samplItem["RECONCILETYPE"] = reconciletype;	
	if(returnValueStr) {
		//post_to_url(path, samplItem, method);		
		$.post('/ibase/webitm/jsp/ICRWizardSaveSample.jsp', { SAVE_SAMPLE_DATA: valueXmlString, RECONCILETYPE : reconciletype}, 
			function(returnedData){
				 console.log(returnedData);
		});
	} else {
		return false;
	}
}
function getCheckedValues(frm) {	
	var xmlData = "<Detail2>";
	var siteL = document.getElementById("site_code_fr").value;
	var siteR = document.getElementById("site_code_to").value;
	var lamount = 0.0,ramount = 0.0;
	// For each checkbox see if it has been checked, record the value.
	for (i = 0; i < frm.checkbox1.length; i++) {
		if (frm.checkbox1[i].checked) {

			cnt = frm.checkbox1[i].value;			
			var trandate = document.getElementById("TRAN_DATE" + cnt).innerHTML;
			var refser = document.getElementById("REF_SER" + cnt).innerHTML;			
			var refid = document.getElementById("REF_ID" + cnt).innerHTML;
			var sitecode = document.getElementById("SITE_CODE" + cnt).innerHTML;			
			var drAmt = document.getElementById("DR_AMT" + cnt).innerHTML;
			var crAmt = document.getElementById("CR_AMT" + cnt).innerHTML;			
			var gpNo = document.getElementById("GP_NO" + cnt).innerHTML;
			var gpDate = document.getElementById("GP_DATE" + cnt).innerHTML;			
			var bilNo = document.getElementById("BIL_NO" + cnt).innerHTML;			
			var bilDate = document.getElementById("BIL_DATE" + cnt).innerHTML;

			if(siteL == sitecode) {
				lamount = lamount + (parseFloat(drAmt) + parseFloat(crAmt)); 
			} else if(siteR == sitecode) {
				ramount = ramount + (parseFloat(drAmt) + parseFloat(crAmt)); 
			}

			xmlData += "<Detail>";
			xmlData += "<CNT><![CDATA[" + cnt + "]]></CNT>";
			xmlData += "<TRAN_DATE><![CDATA[" + trandate + "]]></TRAN_DATE>";
			xmlData += "<REF_SER><![CDATA[" + refser + "]]></REF_SER>";
			xmlData += "<REF_ID><![CDATA[" + refid + "]]></REF_ID>";
			xmlData += "<SITE_CODE><![CDATA[" + sitecode + "]]></SITE_CODE>";
			xmlData += "<DR_AMT><![CDATA[" + drAmt + "]]></DR_AMT>";
			xmlData += "<CR_AMT><![CDATA[" + crAmt + "]]></CR_AMT>";
			
			xmlData += "<GP_NO><![CDATA[" + gpNo + "]]></GP_NO>";
			xmlData += "<GP_DATE><![CDATA[" + gpDate + "]]></GP_DATE>";
			xmlData += "<BIL_NO><![CDATA[" + bilNo + "]]></BIL_NO>";
			xmlData += "<BIL_DATE><![CDATA[" + bilDate + "]]></BIL_DATE>";
			xmlData += "</Detail>";
		}
	}
	alert("Amount:::" + lamount +" : "+ ramount)
	if(lamount == ramount) {
		returnValueStr = true;
	} else {
		returnValueStr = false;
	}
	xmlData += "</Detail2>";
	//alert("xmlData : " + xmlData);
	return xmlData;
}


function isNumberKey(e)
{
	// alert("isNumberKey called...");
	var unicode = e.charCode ? e.charCode : e.keyCode;
	if ((unicode == 8) || (unicode == 9) || (unicode > 47 && unicode < 58)
		|| (unicode == 37) || (unicode == 46) || (unicode == 39)
		|| (unicode == 115) || (unicode == 121) || (unicode == 117)) {
	return true;
	} 
	else 
	{
		window.alert("This field accepts only Numbers");
		return false;
	}
}
	
function numbersonly(myfield, e, dec)
{
	var key;
	var keychar;

	if ( window.event )
	{
		key = window.event.keyCode;
	}
	else
	{
		return true;
	}

	if( key >= 48 && key <= 57 )
	{
		return true;
	}
	else
	{
		return false;
	}
}

function post_to_url(path, params, method) {
	method = method || "post"; // Set method to post by default if not specified.

	// The rest of this code assumes you are not using a library.
	// It can be made less wordy if you use one.
	var form = document.createElement("form");
	form.setAttribute("method", method);
	form.setAttribute("action", path);
	form.target = 'Items';

	for(var key in params) {
		if(params.hasOwnProperty(key)) {
			var hiddenField = document.createElement("input");
			hiddenField.setAttribute("type", "hidden");
			hiddenField.setAttribute("name", key);
			hiddenField.setAttribute("value", params[key]);

			form.appendChild(hiddenField);
		}
	}

	document.body.appendChild(form);
	
	form.submit();
}
