function formatCurrency( strValue ) {
	var indx = strValue.indexOf(".");
	var retStr = '';
	if ( indx < 0 ) {
	  retStr = strValue + '.00';
	} else {
	  var cents = strValue.substring(indx + 1, strValue.length );
	  if ( cents == '' ) cents = '00';
	  if ( cents.length == 1 ) cents = cents + '0';
	  if ( cents.length > 2 ) cents = cents.substring(0,2);
	  retStr = strValue.substring(0,indx) + '.' + cents;
	}
	return retStr;
}

function validInt(strVal, errMsg){
	var objRegExp  = /(^-?\d\d*$)/;
	if ( ! objRegExp.test(strVal) ) {
	    alert ( errMsg );
	    return false;
	}
	return true;
}

function validateCurrency( strValue, errMsg ) {
	if ( strValue == null || strValue == '' ) { alert( errMsg ); return false; }
	var indx = strValue.indexOf('.');
	if ( strValue.indexOf('.') < 0 ) {
	    return validInt(strValue, errMsg);
	} else {
	    var dollars = strValue.substring(0,indx );
	    if ( ! validInt(dollars, errMsg) ) return false;
	    var cents = strValue.substring(indx + 1, strValue.length );
	    if ( ! validInt(cents, errMsg) ) return false;
	}
	return true;
}


function doCalculate(x){

    if ( ! validInt( document.smoke.cigPerDay.value,
		     'Provide proper values for cigarates you smoke per day !') ) return false;
    if ( ! validateCurrency( document.smoke.valPerPack.value,
		     'Provide proper values for price per pack !') ) return false;
    var valPerCigar = document.smoke.valPerPack.value / 20;
    var valPerDay = valPerCigar * document.smoke.cigPerDay.value;
    var valPerYear = valPerDay * x;
    var strValPerYear = valPerYear;
    strValPerYear = formatCurrency( strValPerYear.toString() );
    var formatedVal = formatCurrency( strValPerYear.toString() );
    document.smoke.Total.value = formatedVal;
    // document.smoke.label0.text = "Your Cost Is";
    document.smoke.monthly.style.color="#000000";
    document.smoke.yearly.style.color="#000000";
    event.srcElement.style.color="#FF3333";
}

function randomcolor() {
	event.srcElement.style.color="CC33FF";
	alert('srcElement =' + event.srcElement + ':');
	alert('tst1 =' + document.tst1 );
}

