// JavaScript Document
/*
formatTextBox 	-->	
convertToInt 	-->	
formatNum 		-->	Formats Text box's (neg --> pos)
decRound 		-->	Round to nerest 2 decimal Round
*/

// allow only positive money values
function formatTextBox (object) {
	var num = object.value

	if ( num == "" ) {  return ; }
		else if ( isNaN(num) ) 		{ num = "" }
		else if( num >= 0) 			{ num = ( decRound( num ) )  }
		else  /* (value < 0 ) */ 	{ num = ( decRound( 0 - num ) ) }
			
	object.value = num
}

function formatNum(value) {
	var returnValue = ""

	if ( isNaN( value) ) 		{ returnValue = "&nbsp;" + 0.00 + "&nbsp;"  }
	else if( value >= 0) 		{ returnValue = "&nbsp;" + formatNumC(value) + "&nbsp;" }
	else  /* (value < 0 ) */	{ returnValue = "(" + formatNumC(0 - value) + ")"  }
			
	return returnValue ;
}
function formatNumC(num) {
	var myVal = decRound(num), iLast = myVal.length
	if( myVal.indexOf('.') ==  (myVal.length-3)) {
		switch (myVal.length) {
			case  7: case  8: case  9:  myVal = myVal.substring(0,iLast-6) + "," + myVal.substring(iLast-6,iLast) ; break ;
			case 10: case 11: case 12:  myVal = myVal.substring(0,iLast-9) + "," + myVal.substring(iLast-9,iLast-6) + "," + myVal.substring(iLast-6,iLast) ; break ;
			case 13: case 14: case 15:  myVal = myVal.substring(0,iLast-12) + "," + myVal.substring(iLast-12,iLast-9) + "," + myVal.substring(iLast-9,iLast-6) + "," + myVal.substring(iLast-6,iLast) ; break ;
		}
	}
	return myVal ;
}
function formatNumInt(num) {
	if ( !isNaN(num) ) {
		var isNeg = false ; num = parseInt(num).toString()
		if (num <0) { isNeg = true ; num = (0 - num) ; 	}
		
		var iLast = num.length 
		switch ( num.length ) {
			case  4: case  5: case  6:  num = num.substring(0,iLast-3) + "," + num.substring(iLast-3,iLast) ; break ;
			case  7: case  8: case  9:  num = num.substring(0,iLast-6) + "," + num.substring(iLast-6,iLast-3) + ", "+ num.substring(iLast-3,iLast) ; break ;
			case 10: case 11: case 12:  num = num.substring(0,iLast-9) + "," + num.substring(iLast-9,iLast-6) + "," + num.substring(iLast-6,iLast-3) + ", "+ num.substring(iLast-3,iLast) ; break ;
		}
		if (isNeg) { num = "-"+num}
	} else { num = "" }
	return num ;
}

function decRound(num) { return (decRound1(num)) }
function decRound1( number ) {
	if ( isNaN( number ) ) { return "0.00" }
	if ( Math.abs( number ) < 0.0049 ) { return "0.00" }
	// number = parseFloat(number)
	
	var arynum = (number.toString()).split('.')
	
	//a number without a decimal
	if (arynum.length == 1) {
		if( isNaN(parseInt(arynum[0])) ) 	{ return ("0.00") ; }
		else 								{ return ( arynum[0]+'.00') ; }
	}
	
	//a num with a decmal
	else {
		//decimal at end - nn.
		if(arynum[1].length == 0) 		{ return ( arynum[0] + '.00' ) }
		
		// nn.n
		else if(arynum[1].length == 1) { return (arynum[0].toString() + '.' +  arynum[1].toString() + '0') }
		
		// nn.nn
		else if(arynum[1].length == 2) { return (arynum[0].toString() + '.' + arynum[1].toString() ) }
		else {
			var dec = arynum[1].slice(0,2)
			//rounding necessay & truncation -- XXX.nn5
			if (arynum[1].charAt(2) >= 5) {
				//whole number is incremented
				if ( dec == "99" ) {
					//alert(arynum[0])
					//negitive whole number
					if ( arynum[0].toString().charAt(0) == '-' ) 	{ return ( (--arynum[0]).toString() + '.00' ) }
					//positive whole number
					else 											{ return ( (++arynum[0]).toString() + '.00' ) }
				}
				//hundreth place is incremented
				else if ( dec <  9 )								{ return ( (arynum[0]).toString() + '.0' + (++dec).toString()  ) }
				//tenths place is incremented
				else 												{ return ( arynum[0].toString() + '.' +  (++dec).toString() ) }
			}
			
			// no rounding necessay & truncation -- XXX.nn0
			else 													{ return ( arynum[0].toString() + '.' +  dec.toString() ) }
		}
	}
}

function dateVerifiction( obj ) {
	var sDate = new Date (3003,01,01)
	if ( !isNaN(obj.value) ) {
		if( obj.value.length==0 ) {
		}
			else if( obj.value.length==6 ) {
				sDate.setYear( parseInt("20" + obj.value.substring(4,6)) )
				sDate.setMonth( parseInt(obj.value.substring(0,2),10)-1 )
				sDate.setDate( parseInt( obj.value.substring(2,4),10) )
			}
			else if( obj.value.length==8 ) {
				sDate.setYear( parseInt( obj.value.substring(4,8) ,10) )
				sDate.setMonth( parseInt(obj.value.substring(0,2) ,10)-1 )
				sDate.setDate( parseInt( obj.value.substring(2,4) ,10 ) )
			}
	}
		else if (obj.value.split('/').length == 3) {
			if( !isNaN( obj.value.split('/')[0] ) && !isNaN( obj.value.split('/')[1] ) && !isNaN( obj.value.split('/')[2] ) ) {
				if( obj.value.split('/')[2].length == 2 ) {
					sDate.setYear(  parseInt( "20" + obj.value.split('/')[2] ,10 ) )
				} 
					else if ( obj.value.split('/')[2].length == 4 )  {
						sDate.setYear(  parseInt( obj.value.split('/')[2],10 ) )
					}
				sDate.setMonth( parseInt( obj.value.split('/')[0],10 )-1 )
				sDate.setDate(  parseInt( obj.value.split('/')[1],10 ) )
			}
		}

	//-------------------------
	if ( sDate.getYear() != "3003" ) {
		var month = "00" + parseInt(sDate.getMonth() + 1 ,10)
		var day = "00" + sDate.getDate().toString()
		
		var year = sDate.getYear().toString()
		if( year.length == 2 ) {
			year = "19" + year  
		}
		obj.value = month.substring(month.length -2,month.length) + "/" + day.substring(day.length -2,day.length) + "/" + year
	}
	else {
		obj.value = ""
	}
}