/************************************************
	Common javascript functions
************************************************/

//Used for confirming a delete action by a user on a DataGrid row.
function ConfirmDelete()
{
if (confirm("Are you sure you want to delete this record?")==true)
	return true;
else
	return false;
}

function ConfirmDelete(itemType)
{
	if (confirm("Are you sure you want to delete this " + itemType + "?")==true)
		return true;
	else
		return false;
}

//Used for cancel buttons and set through the page load like above.
function ConfirmCancel()
{
if (confirm("Warning: This action may result in loss of data. Are you sure you wish to continue?")==true)
	return true;
else
	return false;
}

//Captures event when Enter key is pressed so that it can be ignored.
function CaptureEnter()
{
	if (window.event.keyCode == 13)
	{
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
}

//These functions are used to limit the number of characters that can appear in a multiline text area
function textCounterKeyPress(item, limit)
{
	if(item.value.length >= limit && item.document.selection.createRange().text.length == 0)
	{	
		event.returnValue = false;
	}
}
function textCounterBeforePaste(item, limit)
{
	if(item.value.length >= limit)
	{
		event.returnValue = false;
	}
}

function CommaFormatted(amount)
{
    var delimiter = ","; // replace comma if desired
    var a = amount.split('.',2)
    var d = a[1];
    var i = parseInt(a[0]);
    if(isNaN(i)) { return ''; }
    var minus = '';
    if(i < 0) { minus = '-'; }
    i = Math.abs(i);
    var n = new String(i);
    var a = [];
    while(n.length > 3)
    {
	    var nn = n.substr(n.length-3);
	    a.unshift(nn);
	    n = n.substr(0,n.length-3);
    }
    if(n.length > 0) { a.unshift(n); }
    n = a.join(delimiter);
    if(d.length < 1) { amount = n; }
    else { amount = n + '.' + d; }
    amount = minus + amount;
    return amount;
 }

/*****************************************************************************
** The function ValidateEmail will be used to validate email addresses with custom validators
*****************************************************************************/
function ValidateEmail(sender, args)
{
    var info = args.Value;
    var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if(!filter.test(info))
    { args.IsValid = false; return; }
    args.IsValid = true; 
    return;
}