/* ----------------------------------------------------------------------------------------------------------------
*	
Function:	RetainageFee

Functionality:
Calculates Retainage Fee based on Retainage % and Sales Order subTotal.

Deployment:
The script can be deployed on Sales Order.

Event triggering the script:
	1) validateLine
	
Custom Fields:
+-----------------------------------------------------------------------------------------------------------------+
| S. No.|	Id					|	Type			|	Level	|		Description							
+-----------------------------------------------------------------------------------------------------------------+
| (1)	|	_retainagepercent	|	Percentage		|	Body	|	    It holds the Retainage percentage.		  |

Create above custom fields with their id and type as mentioned in table.
*------------------------------------------------------------------------------------------------------------------
*/

// Global Variables
{
	debugValidateLine			= false;
	debugRetainageFee			= false;
}
// End - Global Variables


/* -----------------------------------------------------------
 *	Function:	validateLine
 *	Description:
 *		This function is fired when the user hits Add/Done
 *		button on the item line
 * -----------------------------------------------------------
 */

function validateLine ( type )
{
	if ( debugValidateLine ) alert ( "[DEBUG - validateLine] Entering checks on the line item" ) ;

	if ( type == 'item')
	{		
		var currentItem = nlapiGetCurrentLineItemText ( 'item', 'item' ) ;
		
		if(currentItem == 'Retainage')
		{
			var retainageFee = RetainageFee();
			nlapiSetCurrentLineItemValue ( 'item', 'rate', retainageFee) ;
		}
	}

	if ( debugValidateLine ) alert ( "[DEBUG - validateLine] Exiting the validateLine function" ) ;

	return true ;
}

function RetainageFee()
{
	if ( debugRetainageFee) alert ( "[DEBUG - debugRetainageFee] Starting Retainage Fee calculation." ) ;

	var subTotal = nlapiGetFieldValue ( 'subtotal') ;
 
	if ( debugRetainageFee ) alert ( "[DEBUG - debugRetainageFee] subTotal: " + subTotal ) ;

	var retainagePercent = nlapiGetFieldValue ( 'custbody_retainagepercent') ;
	if ( debugRetainageFee ) alert ( "[DEBUG - debugRetainageFee] retainagePercent: " + retainagePercent ) ;

	var retainageAmount = 0.01*subTotal*retainagePercent*(-1);
	if ( debugRetainageFee ) alert ( "[DEBUG - debugRetainageFee] retainageAmount: " + retainageAmount ) ;

	return retainageAmount;
}