function stripDecimalSeparators(value)
{
	value = value.replace(/,/gi, ".");
	var lastDotIdx = value.lastIndexOf(".");
	if (lastDotIdx > -1)
		value = value.substring(0, lastDotIdx).replace(/\./gi, "") + value.substr(lastDotIdx);
	return value;
}

function parseInteger(value)
{
	if (value == null || value.length == 0)
		return 0;
	value = stripDecimalSeparators(value);
	
	var legalDigits = "1234567890-";
	for (var i=0 ; i < value.length; ++i)
	{
		if (legalDigits.indexOf(value.charAt(i)) == -1)
			return 0;
	}
	return parseInt(value);
}

function parseDecimal(value)
{
	if (value == null || value.length == 0)
		return 0;
	value = stripDecimalSeparators(value);
	
	var legalDigits = ".1234567890-";
	for (var i=0 ; i < value.length; ++i)
	{
		if (legalDigits.indexOf(value.charAt(i)) == -1)
			return 0;
	}
	return parseFloat(value);
}

function LTrim(val)
{
  var c, i, j = -1;
  
  for(i = 0;i < val.length;++i)
  {
    c = val.charAt(i);
    if(!isSpace(c))
    {
      j = i;
      break;
    }
  }
  
  if(j != -1)
    return val.substring(j, val.length);
  else
    return '';
}
function RTrim(val)
{
  var c, i, j = -1;
  
  for(i = val.length-1;i >= 0;--i)
  {
    c = val.charAt(i);
    if(!isSpace(c))
    {
      j = i;
      break;
    }
  }
  
  if(j != -1)
    return val.substring(0, j + 1);
  else
    return '';
}

function Trim(val)
{
  return LTrim(RTrim(val));
}

function isSpace(val)
{
  if( (val == ' ') || (val == '\t') || (val == '\n') || (val == '\r') )
    return true;
  else
    return false;
}

function checkDate(formday, formmonth, formyear)
{
	var day   = parseInt(formday, 10);
	var month = parseInt(formmonth, 10);
	var year  = parseInt(formyear, 10);
	var febdays = 28;
		
	if(isNaN(day) || isNaN(month) || isNaN(year) || 
	   (day != formday) || (month != formmonth) || (year != formyear)|| (formyear.length != 4) )
	  return false;
	  
  if( (month > 12) || (day < 1) || (month < 1) || (year < 1) )
    return false;
			
	if( (month == 1) || (month == 3) || (month == 5) || (month == 7) ||
	    (month == 8) || (month == 10) || (month == 12) )
	{
		if(day > 31)
		  return false;
	}
	else
	{
		if(day > 30)
		  return false;
	}
	
	if(month == 2)
	{
	  if( ( ((year % 4) == 0) && ((year % 100) != 0) ) || ((year % 400) == 0) )
	    febdays = 29;
	  if(day > febdays)
			return false;
	}
	return true;
}


function ProceedNextField(e, length, nextFieldName){
		var nextField;
		nextField = document.all ? document.all[nextFieldName] : document.getElementById(nextFieldName);
		if (nextField != null)
		{
			if (nextField.value != null)
			{
		        if(e.value.length == length && nextField.value.length == 0) {
		            nextField.focus();
		        }
		  }else
		  {
		  	if(e.value.length == length)
		  	{
		  		nextField.focus();
		  	}
		  }
  	}
}

