// JavaScript Document

formelements = new Array();
preis = new Array();



   function validateRange()
   {
      var s = document.frmInput.txtInput.value;
      var A = document.frmInput.txtA.value;
      var B = document.frmInput.txtB.value;

      switch (isIntegerInRange(s, A, B))
      {
         case true:
            alert(s + " is in range from " + A + " to " + B)
            break;
         case false:
            alert(s + " is not in range from " + A + " to " + B)
      }
   }

// isIntegerInRange (STRING s, INTEGER a, INTEGER b)
   function isIntegerInRange (s, a, b)
   {   if (isEmpty(s))
         if (isIntegerInRange.arguments.length == 1) return false;
         else return (isIntegerInRange.arguments[1] == true);

      // Catch non-integer strings to avoid creating a NaN below,
      // which isn't available on JavaScript 1.0 for Windows.
      if (!isInteger(s, false)) return false;

      // Now, explicitly change the type to integer via parseInt
      // so that the comparison code below will work both on
      // JavaScript 1.2 (which typechecks in equality comparisons)
      // and JavaScript 1.1 and before (which doesn't).
      var num = parseInt (s);
      return ((num >= a) && (num <= b));
   }

   function isInteger (s)
   {
      var i;

      if (isEmpty(s))
      if (isInteger.arguments.length == 1) return 0;
      else return (isInteger.arguments[1] == true);

      for (i = 0; i < s.length; i++)
      {
         var c = s.charAt(i);

         if (!isDigit(c)) return false;
      }

      return true;
   }

   function isEmpty(s)
   {
      return ((s == null) || (s.length == 0))
   }

   function isDigit (c)
   {
      return ((c >= "0") && (c <= "9"))
   }

function validateInt()
   {
      var o = document.frmInput.txtInput;
      switch (isInteger(o.value))
      {
         case true:
           	return true;
         case false:
            return false;
      }
   }

function updateTotal(){
	total = 0;
	for(i = 0; i<formelements.length ; i++){
		if(isInteger(document.forms[0].elements[formelements[i]].value)){
			total += parseInt(document.forms[0].elements[formelements[i]].value) * preis[i];
		}
	}
	
	if(total%1 == 0){
		total += ".- sFr.";	
	}else{
		total += "0 sFr.";	
	}
	
	document.getElementById("mailformTOTAL").value = total;
	document.getElementById("calcsum").innerHTML = total;
}

function appendListeners(){
	
	formelementslength = document.forms[0].elements.length;

	for (i = 0; i<formelementslength ; i++ ){
		
		if(document.forms[0].elements[i].type == "text" && document.forms[0].elements[i].name != "TOTAL" && document.forms[0].elements[i].name != "PLZ"  && document.forms[0].elements[i].name != "Tel"){
			formelements.push(i);
			document.forms[0].elements[i].onchange = updateTotal;
		}
		
	}
	
	label = document.getElementsByTagName("label");
	
	for(i = 0; i < label.length; i++){
		temp = label[i].innerHTML.split("/");
		if(isNaN(parseFloat(temp[2]))){
			preis.push(0);	
		}else{
			preis.push(parseFloat(temp[2]));
		}

		
	}
}


window.onload = appendListeners;
