var selectedid;

function CalculateTotal(naam, aantal, bestemming)
{
	var textbox;
	var total;
	var temp;
	
	total =0;
	temp =0;
	for (var i=1;i<=aantal;i++)
	{
		textbox=getElementById(naam+i);
		
		if (textbox.value == '')
		{
		}
		else
		{
			//temp=parseInt(textbox.value);
			
			//var regExpr = /./g;			
			//var str = textbox.value.replace(regExpr, ',')
			//var str = textbox.value.replace('.', ',');
			var value = textbox.value.replace(',', '.');
			
			//temp=parseFloat(textbox.value);
			temp=parseFloat(value);
									
			total+=temp;
			
			//if (!IsNumeric(textbox.value))
			if (!IsDouble(textbox.value))
			{
				textbox.style.color ='Red';	
			}
			else
			{
				textbox.style.color ='Black';
			}
		}
	}
	
	textbox=getElementById(bestemming);
	
	// quick and dirty: haal alles na de eerste decimaal weg
	var strTotal = Number(total).toString();
	var index = strTotal.indexOf('.');
	
	if (index >= 0)
	{
		strTotal = strTotal.substring(0, index + 2);
	}
	
	textbox.innerHTML = strTotal;
	
	//if (!IsNumeric(textbox.innerHTML))
	if (!IsDouble(textbox.innerHTML))
	{
		textbox.style.color ='Red';
	}
	else
	{
		textbox.style.color ='Black';	
	}
}

function CalculateTotal3(naam, aantal, bestemming)
{
	var textbox;
	var total;
	var temp;
	
	total =0;
	temp =0;
	
	for (var i=0;i<aantal;i++)
	{
		textbox=getElementById(naam+i);
		
		if (textbox.value == '')
		{
		}
		else
		{
			//temp=parseInt(textbox.value);
			temp=parseFloat(textbox.value);
			
			total+=temp;
			
			//if (!IsNumeric(textbox.value))
			if (!IsDouble(textbox.value))
			{
				textbox.style.color ='Red';	
			}
			else
			{
				textbox.style.color ='Black';
			}
		}
	}
	
	textbox=getElementById(bestemming);
	textbox.innerHTML = total;	
	
	//if (!IsNumeric(textbox.innerHTML))
	if (!IsDouble(textbox.innerHTML))
	{
		textbox.style.color ='Red';
	}
	else
	{
		textbox.style.color ='Black';	
	}
}

function GetDataViaAJAX(id, value) 
{        
	CreateXmlHttp();
	
	//alert("Parameters in GetDataViaAJAX. id = " + id + ", value = " + value);
	
	if(XmlHttp!=null) 
	{ 
		//the id of the master combobox
		selectedid = id;
		
		// 06.071 Solves the error for acceptation and production but creates it in development.
		// FOR DEVELOPMENT: var url = "/dsmnutrinorm/Content/Bedrijf/GetGewasData.aspx" + "?id=" + id;
		var url = "../../Content/Bedrijf/GetGewasData.aspx" + "?id=" + id;
		url+= "&value=" 
		
		url+=value;
		
		//alert("Te openen URL = " + url);
		XmlHttp.onreadystatechange = ProcessResponse;
		//alert("erics melding 001. Status = " + XmlHttp.statusText);
		XmlHttp.open("GET", url , true); 
		//alert("erics melding 001. Status = " + XmlHttp.statusText);
		XmlHttp.send(null);
	} 
		        
	return false; 
}



//Called when response comes back from server
function ProcessResponse()
{
	
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		
		
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{
			FillList2(selectedid, XmlHttp.responseText);
			//FillList(XmlHttp.responseXML.documentElement);
		}
		else
		{
			alert("Error retrieving data! Error: " + XmlHttp.status);
			//alert("Http responseText: " + XmlHttp.responseText);
		}
	}
} 

			
function FillList2(controlId, responseText)
{
	
	
	//alert("in fill list2");
	
	var region = document.getElementById(controlId + "Gewas");
	
	region.innerHTML = responseText;
}

function FillList(rootNode)
{
	//the combobox
	var dropslave = document.getElementById("dropSlave");

	//Clears the combo box contents.
	for (var count = dropslave.options.length-1; count >-1; count--)
	{
		dropslave.options[count] = null;
	}

	//The root data node
	var element = rootNode.getElementsByTagName("data");
	var textValue; 
	var optionItem;

	var nodes = element.childNodes;
	for (var count = 0; count < nodes.length; count++)
	{
		textValue = GetInnerText(nodes[count]);
		optionItem = new Option( textValue, textValue,  false, false);
		slave.options[count] = optionItem;
	}
				
	/*var elements = rootNode.getElementsByTagName('description');
	var textValue; 
	var optionItem;
	
	for (var count = 0; count < elements.length; count++)
	{
   	textValue = GetInnerText(elements[count]);
		optionItem = new Option( textValue, textValue,  false, false);
		slave.options[count] = optionItem;
	}
	*/
}

function CalculateTotal2(naam, start, eind, bestemming)
{
	var textbox;
	var total;
	var temp;
	
	total =0;
	temp =0;
	
	for (var i=start;i<=eind;i++)
	{
		span=getElementById(naam+i);
		
		if (span.innerText == '')
		{
		}
		else
		{
			//temp=parseInt(span.innerText);
			temp=parseFloat(span.innerText);
			
			total+=temp;
						
			//if (!IsNumeric(span.innerText))
			if (!IsDouble(span.innerText))
			{
				span.style.color ='Red'
			}
			else
			{
				span.style.color ='Black'	
			}
		}
	}
	
	span=getElementById(bestemming);
	
	// quick and dirty: haal alles na de eerste decimaal weg
	/*
	var strTotal = Number(total).toString();
	var index = strTotal.indexOf('.');
	
	if (index >= 0)
	{
		strTotal = strTotal.substring(0, index + 2);
	}
	*/
	
	total = round_decimals(total, 1);
	
	span.innerHTML = total;
	//span.innerHTML = strTotal;	
	
	//if (!IsNumeric(span.innerHTML))
	if (!IsDouble(span.innerHTML))
	{
		span.style.color ='Red'
	}
	else
	{
		span.style.color ='Black'	
	}
}

function CalculateAllTotals()
{
	for (var rij=0;rij<5;rij++)
	{
		CalculateTotal('TextBoxGrond' + rij, 4,'TextBoxGrondTotaal' + rij);
	}
	
	CalculateTotal2('TextBoxGrondTotaal',0,4,'TextBoxGrondTotaalTotaal');

	for (var rij=0;rij<27;rij++)
	{
		var naam = 'TextBoxBouwland' + rij;
		
		CalculateTotal(naam,3, 'TextBoxBouwlandTotaal' + rij);
	}
	
	CalculateTotal2('TextBoxBouwlandTotaal',0,14,'TextBoxEersteTeeltTotaalTotaal');
	CalculateTotal2('TextBoxBouwlandTotaal',15,20,'TextBoxVolgTeeltTotaalTotaal');
	CalculateTotal2('TextBoxBouwlandTotaal',21,23,'TextBoxherfstTeeltTotaalTotaal');
	CalculateTotal2('TextBoxBouwlandTotaal',24,26,'TextBoxGroenbemestersTotaalTotaal');
}

function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals);
    var result2 = Math.round(result1);
    var result3 = result2 / Math.pow(10, decimals);
    return pad_with_zeros(result3, decimals);
}

function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString();
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".");

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0;
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : "";
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1;
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length;
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0";
        }
        
    return value_string;
}

