// ****************************** //
// Bestellung form functionality  //
// Michal Erdody Webaholix s.r.o. //
// ****************************** //

function orderTotal(oform, prefix)
{
	// set references to fields
	var qty = oform[prefix + "_qty"];
	var stHold = oform[prefix + "_stHold"];
	var price = oform[prefix + "_price"];
	var stVis = oform[prefix + "_stVis"];
//alert (stVis.value);
	// only bother if the field has contents
	if (qty == "")return;

	// if the with is not a number (NaN) or is zero or less everything goes blank (zero) 
	if(isNaN(qty.value) || (qty.value <= 0)){
		qty.value = 0;
		stHold.value = 0,00;
		//stHold.value = stHold.value.replace('.', ',');
	}
	   
	// else the field is a valid number, so calculate the total order cost and put that value in the hidden subtotal field 
	else {
	   stHold.value = ((Math.round(qty.value * price.value * 100))/100).toFixed(2);
	}
	   
	// call the routine which checks if the visible subtotal is correct
	visTotal(oform, prefix);
}

// checks if the visible subtotal is correct, ie, if it equals the hidden subtotal field
function visTotal(oform, prefix){
	var stHold = oform[prefix + "_stHold"];
	var stVis = oform[prefix + "_stVis"];

	if (stVis.value != stHold.value){
		stVis.value = ((stHold.value)*1).toFixed(2);
		summeTotal(oform);
	}
}

// gesamtsumme, zwischensumme and warensumme counting
function summeTotal(oform){
	var smallest_stVis = oform["smallest_stVis"];
	var smallest_temp = smallest_stVis.value.replace(',', '.');
	var smallest = parseFloat(smallest_temp);
        
	var small_stVis = oform["small_stVis"];
	var small_temp = small_stVis.value.replace(',', '.');
	var small = parseFloat(small_temp);
	//alert (small);
	
	var middle_stVis = oform["middle_stVis"];
	var middle_temp = middle_stVis.value.replace(',', '.');
	var middle = parseFloat(middle_temp);
	
	//var big_stVis = oform["big_stVis"];
	//var big_temp = big_stVis.value.replace(',', '.');
	//var big = parseFloat(big_temp);
	
	var warensumme = oform["warensumme"];
	var zwischensumme = oform["zwischensumme"];
	var gesamtsumme = oform["gesamtsumme"];
	
	// warensumme counting and string replace 
	//warensumme_temp = (small+middle+big).toFixed(2);
	warensumme_temp = (smallest+small+middle).toFixed(2);
	warensumme.value = warensumme_temp.replace('.', ',');
	
	// zwischensumme counting and string replace 
	zwischensumme_temp = (parseFloat(warensumme_temp) + 10).toFixed(2);
	zwischensumme.value = zwischensumme_temp.replace('.', ',');	
	
	// gesamtsumme counting and string replace 
	gesamtsumme_temp = (parseFloat(zwischensumme_temp)).toFixed(2);
	gesamtsumme.value = gesamtsumme_temp.replace('.', ',');
	
	// summe string replace after all operations
	small_stVis.value = small_stVis.value.replace('.', ',');
	middle_stVis.value = middle_stVis.value.replace('.', ',');
	//big_stVis.value = big_stVis.value.replace('.', ',');
	
}

function highlight(field) {
	field.focus();
    field.select();
}

// set bestellung qty input fields after loading page content
function set_fields() {
	var oform = document.Formular;

	if (oform){
		orderTotal(oform, 'small');
		orderTotal(oform, 'middle');
		orderTotal(oform, 'smallest');
	}
}
