//+clean_and_tidy


//max_length=12;                         //max_length= length of text input
//spacing="";            // if $ sign is not wanted, remove it from spacing string.


//-clean_and_tidy



function clean (w){
    spacing="";
    max_length=12;
	if(!w){
	    alert("missing input after "+ w.name);
	    return(0);
	}
	y="0"+w.value;                          //0 to prevent NaN
	yl=y.length;
	for(z=0;z<yl;z++){
		if(y.charAt(z)<"." || y.substring(z,z+1)>"9" || y.substring(z,z+1)=="/"){
			y=(y.substring(0,z) + y.substring(z+1));
			z--;
			yl--;
		}
	}
	//alert('y: ' + y);
	y=parseFloat(y);
	//alert('parseFloat(y): ' + y);
	x=Math.floor(y);
	//alert('Math.floor(y): ' + x);
	y=y * 100;
	//alert('y * 100: ' + y);
	x=x * 100;
	//alert('x * 100: ' + x);
	xx=y-x;
	//alert('y-x: ' + xx);
	xx = xx / 100;
	y = y / 100;
	x = x / 100;
	//alert('xx/100: ' + xx);
	xx=xx+"00.00"                 //xx=the cents only (with zeroes).
	//alert('xx+"00.00": ' + xx);
	a=xx.indexOf(".");
  // alert('xx.indexOf("."): ' + a);
	q=x+xx.substring(a,a+3);
  // alert('x+xx.substring(a,a+3): ' + q);
  
  var ql = 0;
	if(q.length < max_length){
	  ql = (parseInt(max_length) - parseInt(q.length));
	}
	if(y){
	  w.value = spacing.substring(0, ql) + q;
	}else{
	  w.value = spacing + "0.00";
	}
	r = parseFloat(q);                            //should be dddd.dd
  // w2=w;
	// finally if this number is blank, return a formatted 0 : 0.00
	return (r);
}

function cleanTag (w, include_dollar_sign){
  spacing = "";
    if (include_dollar_sign == true){
    	spacing="$";
    }
  max_length=12;
	if(!w){
	    alert("missing input after "+ w2.name);
	    return(0);
	}
	y="0"+w.childNodes[0].nodeValue;                          //0 to prevent NaN
	yl=y.length;
	for(z=0;z<yl;z++){
		if(y.charAt(z)<"." || y.substring(z,z+1)>"9" || y.substring(z,z+1)=="/"){
			y=(y.substring(0,z) + y.substring(z+1));
			z--;
			yl--;
		}
	}
	//alert('y: ' + y);
	y=parseFloat(y);
	//alert('parseFloat(y): ' + y);
	x=Math.floor(y);
	//alert('Math.floor(y): ' + x);
	y=y * 100;
	//alert('y * 100: ' + y);
	x=x * 100;
	//alert('x * 100: ' + x);
	xx=y-x;
	//alert('y-x: ' + xx);
	xx = xx / 100;
	y = y / 100;
	x = x / 100;
	//alert('xx/100: ' + xx);
	xx=xx+"00.00"                 //xx=the cents only (with zeroes).
	//alert('xx+"00.00": ' + xx);
	a=xx.indexOf(".");
	//alert('xx.indexOf("."): ' + a);
	q=x+xx.substring(a,a+3);
	//alert('x+xx.substring(a,a+3): ' + q);
  
  var ql = 0;
  if (q.length < max_length){
    ql = max_length - q.length;
  }
	w.childNodes[0].nodeValue = spacing + "0.00";
	if (y){
	  w.childNodes[0].nodeValue = spacing.substring(0,ql) + q;
  }
	r=parseFloat(q);                            //should be dddd.dd
	w2=w;
	return (r);
}


function tidy (y){
    max_length=12;                         //max_length= length of text input
    spacing="$               ";
    x=Math.floor(y);
    xx=y-x;
    xx=xx+"00.00"                 //xx=the cents only (with zeroes).
    a=xx.indexOf(".");
    q=x+xx.substring(a,a+3);
    ql= (q.length<max_length)?(max_length-q.length):0;
    r=(y)?spacing.substring(0,ql)+q:"";
    return (r);
}


//display tidys up a number for display: hopefully leaving 2 decimal points

function display(numb){
  if (parseFloat(numb)){
    if (numb== Math.round(numb)){
      return ( numb + ".00")
    }
    else {
      numb=(numb + "0");
      return (numb.substring(0,(numb.indexOf(".")+3)))
    }
  }
  else{
    return "0.00"
  }
}


function trim(w){
	if(!w){
	    return(0);
	}
	y=w.value;                          //0 to prevent NaN
	yl=y.length;
	for(z=0;z<yl;z++){
		if(y.charAt(z)=="," || y.substring(z,z+1)==" " || y.substring(z,z+1)=="/"){
			y=(y.substring(0,z) + y.substring(z+1));
			z--;
			yl--;
		}
	}
	numb=parseFloat(y);
	if (isNaN(numb)){
	    w.value='';
	    return 0}
    if (numb== Math.round(numb)){
        w.value= numb + ".00"
        return parseFloat( numb + ".00")
    }
    else {
        numb=(numb + "0");
        w.value=numb.substring(0,(numb.indexOf(".")+3))
        return parseFloat(numb.substring(0,(numb.indexOf(".")+3)))
    }
}

function tidiedValueToNumber(value){
  return value.replace("$", "").replace(",", "");
}
