function checkDate(field) {
	var ValidDates='0123456789/';
	var sError = "";

	//	Exit if the field value is empty and there is no Default
	if (field.value.length == 0) { return false; }

	//	Check for valid date
	if (field.value.length != 10&&field.value.length != 9&&field.value.length != 8) {
		sError='Dates must be in the format of MM/DD/YYYY.';
	} else if (parseInt(field.value.substring(0, field.value.indexOf('/')-1)) < 0||parseInt(field.value.substring(0, field.value.indexOf('/')-1)) > 12) {
		sError='Dates must be in the format of MM/DD/YYYY.';
	}

	for (idx=0; idx < field.value.length; idx++) {
		if (ValidDates.search(field.value.substring(idx, idx+1)) == -1) {
			sError='Dates must be in the format of MM/DD/YYYY.';
		}
	}
	
	if (sError.length > 0) {
		alert(sError);
		field.focus();
		field.select();
		return false;
	} else {
		return true;
	}
}

function validateNum(field, type, theDefault) {
	var numbers='0123456789';
	var foundDec=false;
	var newstring='';

	//	Exit if the field value is empty and there is no Default
	if (field.value.length == 0&&theDefault == null) { return false; }

	//	Remove all characters except numbers
	for ( var i=0; i < field.value.length; i++ ) {
		temp=''+field.value.substring( i, i+1 );

		//	Check for type decimal and ignore first decimal found
		 if (type == 'decimal'&&temp == '.'&&foundDec == false) {
			newstring+=temp;
			foundDec=true;
		} else if (type != 'decimal'&&temp == '.') {
			break;
		} else {
			if (numbers.indexOf( temp ) != '-1' ) newstring+=temp;
		}
	}
	if (newstring.length == 0&&type == 'number'&&theDefault == null) { newstring = 0; }

	if (newstring.length == 0&&theDefault != null) {
		field.value=theDefault;
	} else {
		field.value=newstring;
	}
	return true;
}


//
//thanks to Robbe D. Morris @ http://www.eggheadcafe.com/articles/20031204.asp for the following code
//

function GetDecimalDelimiter(nCountryCode)
{

       var sRet='';

       switch (nCountryCode)
       {

            case 3:   
                           
                           sRet = '#';
                           break;
            
            case 2:   
                           
                           sRet = ',';
                           break;
            default:
                           sRet = '.';
                           break;
 
        }

      return sRet;

}

function GetCommaDelimiter(nCountryCode)
{

       var sRet='';

       switch (nCountryCode)
       {
            
            case 3:   
                           
                           sRet = '*';
                           break;
            case 2:   
                           
                           sRet = ',';
                           break;
            default:
                           sRet = ',';
                           break;
 
        }

      return sRet;

}

function FormatClean(num)
{
     var sVal='';
     var nVal = num.length;
     var sChar='';
     
   try
   {
       for(i=0;i<nVal;i++)
      {
         sChar = num.charAt(i);
         nChar = sChar.charCodeAt(0);
         if ((nChar >=48) && (nChar <=57))  { sVal += num.charAt(i);   }
      }
   }
    catch (exception) { AlertError("Format Clean",e); }
    return sVal;
}
 

function FormatCurrency(num,nCountryCode)
{       
        var sVal='';
        var minus='';
        var Decimal='';
        Decimal = GetDecimalDelimiter(nCountryCode);
        if (num.lastIndexOf("-") == 0) { minus='-'; }
        if (num.lastIndexOf(Decimal) < 0) { num = num + '00'; }
        num = FormatClean(num);
		
		
		
//		
//        if (num.lastIndexOf(Decimal) = 1) { num = num + '0'; }
//		
		
//		  Prior to call the format function, you extract out the value after the delimiter.  
//		  If the value is greater than 10, leave it as is and pass it to the format function.  
//		  If not, add a zero to the end and pass the new value to the format function.


		
		
		
        sVal = minus + FormatDollar(num,GetCommaDelimiter(nCountryCode)) + GetDecimalDelimiter(nCountryCode) + FormatCents(num); 
        return sVal;
}

function FormatNumber(num,nCountryCode)
{       
        var sVal='';
        var minus='';
        var CommaDelimiter='';

        try 
       {

           CommaDelimiter = GetCommaDelimiter(nCountryCode);

           if (num.lastIndexOf("-") == 0) { minus='-'; }

           num = FormatClean(num);

           num = parseInt(num);

           var samount = new String(num);
             
           for (var i = 0; i < Math.floor((samount.length-(1+i))/3); i++)
          {
             samount = samount.substring(0,samount.length-(4*i+3)) + CommaDelimiter + samount.substring(samount.length-(4*i+3));
           }

        }
         catch (exception) { AlertError("Format Number",e); }
        return minus + samount;
}

function FormatCents(amount)
{
     var cents = '';

      try
      {
           amount = parseInt(amount);
           var samount = new String(amount);

           if (samount.length == 0) { return '00'; }
           if (samount.length == 1) { return '0' + samount; }
           if (samount.length == 2) { return samount; }
         
           cents =  samount.substring(samount.length -2,samount.length);
          
      }
      catch (exception) { AlertError("Format Cents",e); }
      return cents;
}

function FormatDollar(amount,CommaDelimiter)
{
   try 
   {
  
        amount = parseInt(amount);

        var samount = new String(amount);

        if (samount.length < 3) { return 0; }  

        samount =  samount.substring(0,samount.length -2);
             
        for (var i = 0; i < Math.floor((samount.length-(1+i))/3); i++)
        {
           samount = samount.substring(0,samount.length-(4*i+3)) + CommaDelimiter + samount.substring(samount.length-(4*i+3));
         }

   }
    catch (exception) { AlertError("Format Comma",e); }
    return samount;
}

 function AlertError(MethodName,e)
 {
            if (e.description == null) { alert(MethodName + " Exception: " + e.message); }
            else {  alert(MethodName + " Exception: " + e.description); }
 }
