var totalfee = 0;
var thousandMarker = ',';
var seperator = '.';
var symbol = '$';
var optionSep = '|';
var priceSet = price = Array( );
cj("input,#priceset select,#priceset").each(function () {
if ( cj(this).attr('price') ) {
var eleType = cj(this).attr('type');
if ( this.tagName == 'SELECT' ) {
eleType = 'select-one';
}
switch( eleType ) {
case 'checkbox':
//default calcution of element.
eval( 'var option = ' + cj(this).attr('price') ) ;
ele = option[0];
optionPart = option[1].split(optionSep);
addprice = parseFloat( optionPart[0] );
if( cj(this).prop('checked') ) {
totalfee += addprice;
price[ele] += addprice;
}
//event driven calculation of element.
cj(this).click( function(){
if ( cj(this).prop('checked') ) {
totalfee += addprice;
price[ele] += addprice;
} else {
totalfee -= addprice;
price[ele] -= addprice;
}
display( totalfee );
});
display( totalfee );
break;
case 'radio':
//default calcution of element.
eval( 'var option = ' + cj(this).attr('price') );
ele = option[0];
optionPart = option[1].split(optionSep);
addprice = parseFloat( optionPart[0] );
if ( ! price[ele] ) {
price[ele] = 0;
}
if( cj(this).prop('checked') ) {
totalfee = parseFloat(totalfee) + addprice - parseFloat(price[ele]);
price[ele] = addprice;
}
//event driven calculation of element.
cj(this).click( function(){
totalfee = parseFloat(totalfee) + addprice - parseFloat(price[ele]);
price[ele] = addprice;
display( totalfee );
});
display( totalfee );
break;
case 'text':
//default calcution of element.
calculateText( this );
//event driven calculation of element.
cj(this).bind( 'keyup', function() { calculateText( this );
}).bind( 'blur' , function() { calculateText( this );
});
break;
case 'select-one':
//default calcution of element.
var ele = cj(this).attr('id');
if ( ! price[ele] ) {
price[ele] = 0;
}
eval( 'var selectedText = ' + cj(this).attr('price') );
var addprice = 0;
if ( cj(this).val( ) ) {
optionPart = selectedText[cj(this).val( )].split(optionSep);
addprice = parseFloat( optionPart[0] );
}
if ( addprice ) {
totalfee = parseFloat(totalfee) + addprice - parseFloat(price[ele]);
price[ele] = addprice;
}
//event driven calculation of element.
cj(this).change( function() {
var ele = cj(this).attr('id');
if ( ! price[ele] ) {
price[ele] = 0;
}
eval( 'var selectedText = ' + cj(this).attr('price') );
var addprice = 0;
if ( cj(this).val( ) ) {
optionPart = selectedText[cj(this).val( )].split(optionSep);
addprice = parseFloat( optionPart[0] );
}
if ( addprice ) {
totalfee = parseFloat(totalfee) + addprice - parseFloat(price[ele]);
price[ele] = addprice;
} else {
totalfee = parseFloat(totalfee) - parseFloat(price[ele]);
price[ele] = parseFloat('0');
}
display( totalfee );
});
display( totalfee );
break;
}
}
});
//calculation for text box.
function calculateText( object ) {
var textval = parseFloat( cj(object).val() );
eval( 'var option = '+ cj(object).attr('price') );
ele = option[0];
if ( ! price[ele] ) {
price[ele] = 0;
}
optionPart = option[1].split(optionSep);
addprice = parseFloat( optionPart[0] );
var curval = textval * addprice;
if ( textval >= 0 ) {
totalfee = parseFloat(totalfee) + curval - parseFloat(price[ele]);
price[ele] = curval;
}
else {
totalfee = parseFloat(totalfee) - parseFloat(price[ele]);
price[ele] = parseFloat('0');
}
display( totalfee );
}
//display calculated amount
function display( totalfee ) {
// totalfee is monetary, round it to 2 decimal points so it can
// go as a float - CRM-13491
totalfee = Math.round(totalfee*100)/100;
var totalEventFee = formatMoney( totalfee, 2, seperator, thousandMarker);
document.getElementById('pricevalue').innerHTML = ""+symbol+" "+totalEventFee;
scriptfee = totalfee;
scriptarray = price;
cj('#total_amount').val( totalfee );
cj('#pricevalue').data('raw-total', totalfee).trigger('change');
( totalfee < 0 ) ? cj('table#pricelabel').addClass('disabled') : cj('table#pricelabel').removeClass('disabled');
if (typeof skipPaymentMethod == 'function') {
skipPaymentMethod();
}
}
//money formatting/localization
function formatMoney (amount, c, d, t) {
var n = amount,
c = isNaN(c = Math.abs(c)) ? 2 : c,
d = d == undefined ? "," : d,
t = t == undefined ? "." : t, s = n < 0 ? "-" : "",
i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "",
j = (j = i.length) > 3 ? j % 3 : 0;
return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}