/***********************************************************************************************************
* number.js
* 
* ICT Services, Web Transactional Team
*
* Common Javascript validation to be used in conjuction with webtrans.css
*
***********************************************************************************************************/

// toDate accepts an element and converts it's value to dd/mm/yyyy format, returns true if successful or false otherwise
var WT_INTEGER = /^-?[0-9]+$/;
var WT_POSITIVE_INTEGER = /^[0-9]+$/;
var WT_NEGATIVE_INTEGER = /^-[0-9]+$/;

var WT_NUMBER = /^-?[0-9]+\.*[0-9]*$/
var WT_POSITIVE_NUMBER = /^[0-9]+\.*[0-9]*$/;
var WT_NEGATIVE_NUMBER = /^-[0-9]+\.*[0-9]*$/;

var WT_CURRENCY = /^-?[0-9]+\.[0-9]{2}$/;
var WT_POSITIVE_CURRENCY = /^[0-9]+\.[0-9]{2}$/;
var WT_NEGATIVE_CURRENCY = /^-[0-9]+\.[0-9]{2}$/;

function wt_isNum(theElement, type)
{
	var str = theElement.value;
	if (/^$/.test(str)) return WT_OK;	
	if (!type.test(str)) return WT_WRONG;
	return WT_DONE;
}

function wt_toCurrency(theElement)
{
	if (/^$/.test(theElement.value)) return WT_OK;
	if (/^-?£/.test(theElement.value)) theElement.value = theElement.value.substring(1, theElement.value.length);
	
	if (/^\.[0-9]{1,2}$/.test(theElement.value)) theElement.value = "0" + theElement.value;	
	if (/^-?\.[0-9]{1,2}$/.test(theElement.value)) theElement.value = "-0" + theElement.value.substring(1, theElement.value.length);
	if (/^-?[0-9]+$/.test(theElement.value)) theElement.value = theElement.value + ".00";
	if (/^-?[0-9]+\.[0-9]$/.test(theElement.value)) theElement.value = theElement.value + "0";
	
	return wt_isNum(theElement, WT_CURRENCY);	
}
