
//var reDate  = /^[A-Za-z]{3}\s+\d{1,2}\s*,\s*\d{4}\s*$/ ;
var reDate  = /^\d{1,2}\/\d{1,2}\/\d{2,4}/
function validDate( dt, sDesc, flgOptional )
    {
    var nRet = alertNullData( dt, sDesc, flgOptional );
    if( nRet < 1 )
      return nRet

    if( !reDate.test( dt.value ) )  //date is not in date-format
        {
        alert( 'Please specify your ' + sDesc
	    + ' \n    in "Jan 02, 2000" format');
        dt.focus();
        return false;
        }

    //make sure a valid date was entered
    if( isNaN(eval( "new Date(\'" + dt.value + "\')" )) )
	{
	alert( dt.value + '\nis not a valid date'
	    + '\nPlease enter a ' + sDesc
	    + ' in "Jan 02, 2000" format');
	dt.focus();
	return false
	}
    
    return true ;
    }

//left trim
var reLtrim = /^\s*\b(.+)$/
function ltrim( s )
    {
    if( !s.length ) return ""
    return (reLtrim.exec(s) != null) ? RegExp.$1 : ""
    }

//right trim
var reRtrim = /^(.+)\b\s*$/
function rtrim( s )
    {
    if( !s.length ) return ""
    return (reRtrim.exec(s) != null) ? RegExp.$1 : ""
    }

//trim
function trim( s )
    {
    return rtrim( ltrim( s ) )
    }

//arg[1] - [control, description, [mandatory]]
//arg[2] - an optional default value
//returns  1 - data is valid
//         0 - data is required but not available
//        -1    - data is optional but not available
function alertNullData( ctl, sDesc, sOptional )
    {
    ctl.value = trim( ctl.value );

    if( ctl.value.length )
        return 1 ;

    if( null == sOptional )     //mandatory data
        {
        alert( 'Please enter your ' + sDesc + ' (Dont use special character)');
        ctl.focus();
	return 0
        }

    return -1
    }

var reEmail = /^.*@.*\....?$/ ;
function validEmail( ctl, sDesc, sOptional )
    {
    var nRet = alertNullData( ctl, sDesc, sOptional );
    if( nRet < 1 )
      return nRet

    if( ctl.value.substr(0, 7).toLowerCase() == 'mailto:' )
	ctl.value = ctl.value.substr(7)

    if( !reEmail.test( ctl.value ) )
      {
      alert( 'Your ' + sDesc + ' must be of the form name@domain.xyz' );
       ctl.focus();
      return false
      }
    return true ;
    }

var reUrl = /^(https?:\/\/)?([^\.]+\.)+\w+(\/.*)?$/
function validUrl( ctl, sDesc, sOptional )
    {
    var nRet = alertNullData( ctl, sDesc, sOptional );
    if( nRet < 1 )
      return nRet

    if( null == reUrl.test( ctl.value ) )
      {
      alert( 'Your ' + sDesc + ' must be of the form domain.xyz' );
      ctl.focus();
      return false
      }

    var sProto = RegExp.$1.toLowerCase()
    if( "http://" == sProto || "https://" == sProto )
        ctl.value = ctl.value.substr(sProto.length)

    return true ;
    }

var reInt = /^\s*[\-\+]?\d+\s*$/ ;
function validInt( ctl, sDesc, flgOptional, nMax, nMin )
    {
    var nRet = alertNullData( ctl, sDesc, flgOptional )
    if( 1 != nRet )
        {
        ctl.value = '0'
        return nRet
	}

    if( !reInt.test( ctl.value ) )
        {
        alert( 'Your ' + sDesc + ' is not a valid number' );
        ctl.focus();
        return false
        }

    if( nMax && parseInt(ctl.value) > nMax )
        {
        alert( 'Your ' + sDesc + ' can not be bigger than ' + nMax );
        ctl.focus();
        return false
        }

    return true ;
    }

var reFloat = /^\s*\d+(\.\d+)?\s*$/ ;
function validFloat( ctl, sDesc, flgOptional )
    {
    var nRet = alertNullData( ctl, sDesc, flgOptional )
    if( 1 != nRet )
        {
        ctl.value = '0.0'
        return nRet
	}

    //has to be a number
    if( !reFloat.test( ctl.value ) )
        {
        alert( 'Your ' + sDesc + ' is not a valid number' );
        ctl.focus();
        return false
        }

    //can not be 0
    if( null == flgOptional && 0.0 >= parseFloat(ctl.value) )
        {
        alert( 'Your ' + sDesc + ' can not be zero or less' );
        ctl.focus();
        return false
        }

    return true ;
    }

var rePhone = /^\d{3}\-\d{3}\-\d{4}/
function validPhone( ctl, sDesc, flgOptional )
    {
    var nRet = alertNullData( ctl, sDesc, flgOptional )
    if( 1 != nRet )
        return nRet

    if( '1' == ctl.value.charAt(0) )
        ctl.value = ctl.value.substr(
	    ('-' == ctl.value.charAt(1) ? 2 : 1) )

    if( '(' == ctl.value.charAt(0) )
        ctl.value = ctl.value.substr(1, 3) + '-' + ctl.value.substr(5)

    if( ' ' == ctl.value.charAt(7) )
        ctl.value = ctl.value.substr(0, 7) + '-' + ctl.value.substr(8)

    if( ctl.value.length < 12 )
        {
        alert( sDesc + 'must be at least 12 characters long'
	    + '\nIt must be of the form xxx-xxx-xxxx' );
        ctl.focus();
	return false
	}

    //has to be nnn-nnn-nnnn
    if( !rePhone.test( ctl.value ) )
        {
        alert( sDesc + ' is not a valid phone number'
	    + '\nIt must be of the form xxx-xxx-xxxx' );
        ctl.focus();
        return false
        }

    return true ;
    }

function checkText( ctlText, sDesc )
    {
    if( !ctlText.value )
        {
        alert( 'Please enter your ' + sDesc );
        ctlText.focus();
        }
    return ctlText.value ;
    }


