function makeAmountMonitary(amount){
	var nAmount = amount.replace('$','');
	var nAmount = nAmount.replace(',','');
	nAmount = (Number(nAmount))?Number(nAmount):0;
	nAmount = nAmount.toFixed(2);
	if(Number(nAmount) < 0){
		nAmount = String(nAmount);
		nAmount =  nAmount.substring(nAmount.indexOf('-')+1,nAmount.length);
		sAmount = "-$" +nAmount;
	}else{
		sAmount = "$" +nAmount;
	}
	return(sAmount);
}

function formatAmount(amount){
	var nAmount = amount.replace('$','');
	var nAmount = nAmount.replace(',','');
	nAmount = (Number(nAmount))?Number(nAmount):0;
	nAmount = nAmount.toFixed(2);

	if(isNaN(amount) || 0 == Number(nAmount)) {
		sAmount = "";
	} else if(Number(nAmount) < 0){
		nAmount = String(nAmount);
		sAmount =  nAmount.substring(nAmount.indexOf('-')+1,nAmount.length);
	} else {
		sAmount = nAmount;
	}

	return(sAmount);
}

function makeAmountMonetaryWithoutDollar(amount){
	var nAmount = amount.replace('$','');
	var nAmount = nAmount.replace(',','');
	nAmount = (Number(nAmount))?Number(Math.round(nAmount)):0;
	nAmount = nAmount.toFixed(2);
	if(Number(nAmount) < 0){
		nAmount = String(nAmount);
		nAmount =  nAmount.substring(nAmount.indexOf('-')+1,nAmount.length);
	}

	return(nAmount);
}

function makeAmountMonetaryWithoutDollarWithNegatives(amount){
	var nAmount = amount.replace('$','');
	var nAmount = nAmount.replace(',','');
	nAmount = (Number(nAmount))?Number(Math.round(nAmount*100)/100):0;
	nAmount = nAmount.toFixed(2);

	return(nAmount);
}

function allowOnlyNumeric(amount){
	amount = (Number(amount))?Number(amount):'';

	return(amount);
}

function changeFormat( inputId ) {
	var inputAmount = document.getElementById( inputId ).value;
	inputAmount = (Number(inputAmount))?Number(inputAmount):'';
	inputAmount = inputAmount.replace("$", '');

	document.getElementById( inputId ).value = inputAmount;
	//var inputAmount = Number( ( document.getElementById( inputId ).value ).replace("$", ''));
	//document.getElementById( inputId ).value = inputAmount.toFixed(2);
}

function makeAmountSignedMonetaryWithoutDollar(amount){
	var nAmount = amount.replace('$','');
	var nAmount = nAmount.replace(',','');
	nAmount = (Number(nAmount))?Number(nAmount):0;
	nAmount = nAmount.toFixed(2);
	return(nAmount);
}

function validate_time( objInput )
{
	// regular expression to match required time format
	re = /^(\d{1,2}):(\d{2})?$/;

	if(objInput.value != '') {
		if(regs = objInput.value.match(re)) {
			if(regs[3]) {
				if(regs[1] < 1 || regs[1] > 12) {
					alert("Invalid value for hours: " + regs[1]);
					objInput.value = '';
					//objInput.focus();
					return false;
				}
			} else {
				if(regs[1] > 23) {
					alert("Invalid value for hours: " + regs[1]);
					objInput.value = '';
					//objInput.focus();
					return false;
				}
			}
			if(regs[2] > 59) {
				alert("Invalid value for minutes: " + regs[2]);
				objInput.value = '';
				//objInput.focus();
				return false;
			}
		} else {
			alert("Invalid time format: " + objInput.value);
			objInput.value = '';
			//objInput.focus();
			return false;
		}
	}

	return true;
}

/*
Valid input formats
m/yy
mmyy
mm/yy
m/yyyy
mmyyyy
mm/yyyy
*/
function checkPostMonth(obj) {
  
  var ilen;
  var sout;
  var yy, mm, dd;
  var tmp;
  var inumsep;
  var imonth, iyear, iday;
  var errorMessage;
  var isep1, isep2;  //holds position of "/"
  var imstart, imlen;
  var idstart, idlen;
  var iystart, iylen;
  var ss = new String(obj.value);
  var d = new Date();
  var m = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
  var cutOffYear = 50;

  ilen = ss.length;
  iday = 0;  //in case is never gets set below
  
  // check for required field
  if (ilen == 0) {return true;}

  errorMessage = "Expecting date in mm/yyyy format."

  // all chars must be numeric or "/"
  inumsep = 0
  for( var i = 0; i < ilen; i++){
    tmp = ss.charAt(i);
    if (tmp == '/') {
      inumsep = inumsep + 1;
      if (inumsep == 1) {isep1 = i}
      else if (inumsep == 2) {isep2 = i}
    }
  }

  //check for valid number of "/"
  if ( inumsep > 1 ) { alert( errorMessage ); return false; }

  //check for valid placement of "/"
  if (inumsep > 0) {
    if (isep1 != 1 && isep1 != 2) {alert( errorMessage ); return false;}
  }

  if (inumsep > 1) {
    if (isep2 != 3 && isep2 != 4 && isep2 != 5) {alert( errorMessage ); return false;}
  }

  //set start and length of month, day and year
  if (inumsep == 0) {
    if (ilen == 4) {
      imstart = 0; imlen = 2;
      iystart = 2; iylen = 2;
    } else if (ilen == 6 && obj.isDay) {
      imstart = 0; imlen = 2;
      idstart = 2; idlen = 2;
      iystart = 4; iylen = 2;
    } else if (ilen == 6 && !obj.isDay) {
      imstart = 0; imlen = 2;
      iystart = 2; iylen = 4;
    } else if (ilen == 8) {
      imstart = 0; imlen = 2;
      idstart = 2; idlen = 2;
      iystart = 4; iylen = 4;
    }
    
  } else if (inumsep == 1) {
    imstart = 0; imlen = isep1;
    iystart = isep1  + 1; iylen = ilen - isep1 - 1
  
  } else if (inumsep == 2) {
    imstart = 0; imlen = isep1;
    idstart = isep1  + 1; idlen = isep2 - isep1 - 1
    iystart = isep2 + 1; iylen = ilen - isep2 - 1
  }

  imonth = parseInt(ss.substr(imstart, imlen), 10);  //10 param sets to base 10 (default is 8!) //without it, "08" and "09" returned 0
  iyear = parseInt(ss.substr(iystart, iylen), 10);

  if (isNaN(imonth) || isNaN(iyear) || isNaN(iday)) { alert( errorMessage ); return false; }

  errorMessage = "Expecting date in mm/yy format.";

  if (iylen != 4 && iylen != 2) { alert( errorMessage ); return false; }
  if (imlen != 1 && imlen != 2) { alert( errorMessage ); return false; }
  if (imonth < 1 || imonth > 12) { alert( errorMessage ); return false; }
  if (iylen == 4 && (iyear < 1875 || iyear > 2199)) { alert( errorMessage ); return false; }
  if (iylen == 2 && (iyear < 0 || iyear > 99)) { alert( errorMessage ); return false; }

  mm = imonth;
  mm = mm > 9 ? mm : "0" + mm.toString();
  dd = iday;
  dd = dd > 9 ? dd : "0" + dd.toString();
  yy = iyear;
  yy = yy > 9 ? yy : "0" + yy.toString();

  if (iyear < 1000 ) {
    if ( parseInt(yy) < cutOffYear ) { yy = "20" + yy }
    else { yy = "19" +  yy }
    iyear = parseInt(yy)
  }

  sout = mm + "/" + yy;

  obj.value = sout;

  return true;
}

function allowOnlyPositiveInteger(amount){
	amount = (parseInt( Math.abs( amount ) ))?parseInt( Math.abs( amount ) ):'';

	return(amount);
}

function allowOnlyValidPostMonth( obj ) {

	// Validates whether post month is of mm/yyyy format

	var boolIsValid 	= true;
    var strPostMonth 	= obj.value;
    var objRegExp 		= /^\d{1,2}\/\d{4}$/;

    if( !objRegExp.test( strPostMonth ) ) {
    	boolIsValid	= false
    } else {
		// check whether the month exceeds 12
		var intMonth = strPostMonth.substring( 0, 2 );

		if( 12 < intMonth ) {
			boolIsValid	= false;
		}
    }

    if( true == boolIsValid ) {		
    	return strPostMonth;
    }else if( false == boolIsValid ) {
    	return "";
    }
}