/*
#----------------------------------------------------------------------------
# Name      :   idc_form_validation.js
# Purpose   :   
# Project   :   
#
# Created   :   Steve Steiner on 2006-04-18.
#
# Copyright (c) 2006 Integrated Development Corp.. All rights reserved.
#----------------------------------------------------------------------------
*/
// alert("hi");

var timeoutInProgress = 0;
var focusField = null;

function setFocusDelayed()
{
    if(focusField){
       try {
           focusField.focus();
       } catch(e) {
            ;;
       }
    }
    focusField = null;
    timeoutInProgress = 0;
}   

function setFocus(field)
{
  // save vfld in global variable so value retained when routine exits
  timeoutInProgress = 1;
  focusField = field;
  setTimeout( 'setFocusDelayed()', 30 );
}

validNotAgree = function( field )
{
    var valuu = getSelectedRadio( 'formtoolForm', '_unb_radio_agree');
    return valuu == 'agree';
}

validNotEmpty = function( field )
{
    if( field.value ){
        return( field.value != "" && field.value != "-----" );
    }
    // else{
    //     alert("void field value");
    // }
    return(false);
}

validEmail = function(Xemail, optional) 
{ 
    if (!Xemail.value && optional) { 
        return true; 
    } 
    
    var re_mail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/;
    
    if(!re_mail.test(Xemail.value)){   
        return false; 
    }
    
    return true; 
}

validAllNumeric = function  (field, optional)
{
    if( !field.value && optional){
        return true;
    }
    var exp = /^([0-9])+$/;
    
    if( !exp.test(field.value) ){
        return false;
    }
    
    return true;
}

function isURL (url)
{   
    // TBD: allocate pre-compiled one outside of function body
    var urlPattern = /^(?:(?:ftp|https?):\/\/)?(?:[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\.)+(?:com|edu|biz|org|gov|int|info|mil|net|name|museum|coop|aero|[a-z][a-z])\b(?:\d+)?(?:\/[^;"'<>()\[\]{}\s\x7f-\xff]*(?:[.,?]+[^;"'<>()\[\]{}\s\x7f-\xff]+)*)?/;
 
    if( !urlPattern.test(url.toLowerCase()) ){
        return false;
    }
 
    return true;
}

validURL = function(field, optional )
{
    if( !field.value && optional){
        return true;
    }
    
    return( isURL( field.value) );
}

function inlineValidate( validationFunction, fieldname, optional, errMsgID, msg )
{
    if( timeoutInProgress != 0){
        return( true );
    }
    
    field = document.getElementById( fieldname );
    valid = validationFunction( field, optional );
    
    if( valid ){
        msg = "";     // kill the message if field is valid
    }

    showErrMsg( errMsgID, msg );
    
    // Only start a new timeout if one's not in progress
    // This makes sure that slow machines don't get confused since
    // a timeout could 'start' beetween the top of this function and here
    // apparrently
    if( !valid && timeoutInProgress == 0 ){
        setFocus( field );
    }
    
    return( valid )
}

function showErrMsg( errMsgID, msg )
{
    if(errMsgID){
        var txtNode = document.createTextNode( msg );
        elem = document.getElementById( errMsgID );
    	while(elem.hasChildNodes())
    	{
    	    elem.removeChild(elem.firstChild);
    	}
        elem.appendChild( txtNode );
    }
}

function validate( validationFunction, fieldname, optional, errMsgID, msg )
{
    this.field = document.getElementById( fieldname );
    valid = validationFunction( this.field, optional );
    if(valid){
        msg = "";
    }
    showErrMsg( errMsgID, msg );
    return( msg );
}

function formValidate( validationFunctionArray )
{
    msg = "";
    first = 0;
    for( x in validationFunctionArray ){        // loop through to call each function
        f = validationFunctionArray[x];
        retMsg = f();
        if(retMsg != ""){  // msg is not blank on error
            if( first == 0){
                first = f.field;
            }
            msg += retMsg + "\n"; // \n separates messages with CR/LF
        }
    }

    if(msg != ""){
        alert(msg);
//        setTimeout( "document.forms.formtoolForm."+first+".focus()",50); 
        return(false);      // at least one validation returned an error message
    }
    else{
        return(true);       // nobody failed
    }
}


