/*
----------- FundSys.net(TM) Copyright SySys(R) Corp 2002 ------------
====================================================================
  Created By: BH
  Last Edited By: BH
  Inception Date: 1/1/2002
  Last Edited Date: 1/1/2002
  Description:  None
  File Dependencies: Manage_form.js
										 validate.js
===================================================================''*/

//GLOBAL
var pWasJSError = false;
var pJSMessage = '';
var pJSHeader = '----------------------------------------------------------------------\n';
var pJSHeaderContent = 'The requested action was not completed for the following reasons:\n';
var pJSSubHeader = '----------------------------------------------------------------------\n';
var pJSFooter = '----------------------------------------------------------------------\n';
var pFirstErrorField = '';
var theme = '';

/*'''---------------------------------
'' addJSMessage(msg, fieldName){
--------------------------------------
   Created By: SySys:bh
   Compatibility: IE6
   Description: Adds a message line to the JS message
   Parameters: 
     msg: message to add
     fieldName: base form field name to add message about
     theme: current Theme ID
   Dependencies: Manage_form.js:markFieldImg
   Returns: None
----------------------------------'''*/ 
function addJSMessage(msg, fieldName, theme){
  pWasJSError = true;
  pJSMessage += msg + '\n';
  //indicate first error for focus
  if(pFirstErrorField.length==0){
    pFirstErrorField = fieldName;
  }  
  markFieldImg(fieldName, '/_/resources/themes/' + theme + '/images/but_fieldAlert.gif')
}

/*'''---------------------------------
'' addJSMessageIfFail(script, msg, fieldName){
--------------------------------------
   Created By: SySys:bh
   Compatibility: IE6
   Description: Adds a message line to the JS message if the script or call returns false
   Parameters: 
     script: script to evaluate
     msg: failure message to add if failed
     fieldName: form field name
     theme: current theme ID
   Returns: None
   Dependencies: validate.js:addJSMessage
----------------------------------'''*/ 
function addJSMessageIfFail(script, msg, fieldName, theme){
  if(!eval(script)){
    addJSMessage(msg, fieldName, theme);
  }
}

/*'''---------------------------------
'' addJSMessageIfSuccess(script, msg, fieldName){
--------------------------------------
   Created By: SySys:bh
   Compatibility: IE6
   Description: Adds a message line to the JS message if the script or call returns true
   Parameters: 
     script: script to evaluate
     msg: failure message to add if failed
     fieldName: form field name
     theme: current theme ID
   Returns: None
   Dependencies: validate.js:addJSMessage
----------------------------------'''*/ 
function addJSMessageIfSuccess(script, msg, fieldName, theme){
  if(eval(script)){
    addJSMessage(msg, fieldName, theme);
  }
}

/*'''---------------------------------
'' validateText(){
--------------------------------------
   Created By: SySys:bh
   Compatibility: IE6
   Description: returns string that has been added using addJSMessage
   Parameters: None
   Returns: message text for validation that has been added by addJSMessage
     returns blank string if pWasJSError is false
   Notes:  This is used to customize your error report rather than using the alert box version by calling "validateSubmitAlert"
----------------------------------'''*/ 
function validateText(){
  return pJSMessage;
}


/*'''---------------------------------
'' preValidationSubmitCode(){
--------------------------------------
   Created By: SySys:bh
   Compatibility: IE6
   Description: called before the validateSubmitAlert that is called when the page submits.  
     This function does nothing and is intended to be overriden on each page that 
     uses validation if need be.
   Parameters: None
   Returns: n/a
----------------------------------'''*/ 
function preValidationSubmitCode(){}


/*'''---------------------------------
'' validateSubmitAlert(form){
--------------------------------------
   Created By: SySys:bh
   Compatibility: IE6
   Description: Validates the elements created using the FSWebForms.dll.  Alerts message if error occured
   Parameters: 
     form: form element
   Returns: true if no error, alert and false if error
   Notes: The validate code for a form element is stored in a 
     hidden form element named "submitValidate_FormName" where "FormName" matches the form field's name
     The "Label_FormName" field stores the textual description of the field
     The "isRequired_FormName" field indicates if the field may be left blank
   Dependencies: validate.js:JSMessageClear
----------------------------------'''*/ 
function validateSubmitAlert(form){
  preValidationSubmitCode();
  var curName = ''; var curScript = ''; 
  for(var i=0; i<form.elements.length; i++){
    curName = form.elements[i].name;
    if(curName.indexOf('submitValidate_')>-1){
      var baseName = curName.substring(15, curName.length); //remove submitValidate_
      curScript = eval('form.' + curName + '.value');
//alert(curScript);
      //detect radios
      if(!(curScript)){
        curScript = eval('form.' + curName + '[0].value');
      }
      if(curScript.length > 0){
        eval(curScript);
      }
    }
  }
  if(!isFormDirty(form.name) && !pWasJSError){
    alert('The form has not changed.  There is no need to save.');
    JSMessageClear();
    return false;    
  }
  if(pWasJSError){
    alert(pJSHeader + pJSHeaderContent + pJSSubHeader + pJSMessage + pJSFooter);
    //focus cursor to first element
    eval('document.' + form.name + '.' + pFirstErrorField + '.focus()'); 
    //don't select combo box
    if(fieldType(eval('document.' + form.name + '.' + pFirstErrorField)) != 's'){
      eval('document.' + form.name + '.' + pFirstErrorField + '.select()');
    }
    JSMessageClear();
    return false;
  }  
  return true;
}


/*'''---------------------------------
'' isRequired(form, formFieldName){
--------------------------------------
   Created By: SySys:bh
   Compatibility: IE6
   Description: Clears the alerts
   Parameters: 
     form: form object
     formFieldName: name of field to check requirement status
   Returns: true/false
   Notes:
     The "isRequired_FormName" field indicates if the field may be left blank
     where "FormName" matches the form field's name (formFieldName parameter)
----------------------------------'''*/ 
function isRequired(form, formFieldName){
  var req = eval('form.isRequired_' + formFieldName + '.value');
  if(req != null){
    return (req.toLowerCase()=='true');  
  }
  else{
    var req = eval('form.isRequired_' + formFieldName + '[0].value');
    return (req.toLowerCase()=='true');  
  }
}

/*'''---------------------------------
'' noValue(formField){
--------------------------------------
   Created By: SySys:bh
   Compatibility: IE6
   Description: chooses the proper methods to determin if an element has a value chosen
   Parameters: 
     formField: form field object to be tested for a value
   Returns: true/false
   Dependencies: Manage_form.js:fieldType, selectedValue
   Notes:
     HTML input elements each need to check different properties.  
     The Default Non-selected values for Select elements is -1
----------------------------------'''*/ 
function noValue(formField){
  //determin element type
  //Select
  if(fieldType(formField)=='s'){
    //select elements index value (-1 is the default for not selected)
    if(selectedValue(formField)==-1){
      return true;
    }
    return false;
  }else{
    return isEmpty(formField.value);
  }
}

/*'''---------------------------------
'' JSMessageClear(){
--------------------------------------
   Created By: SySys:bh
   Compatibility: IE6
   Description: Clears the alerts
   Parameters: none
   Returns: None
----------------------------------'''*/ 
function JSMessageClear(){
  pFirstErrorField = '';
  pWasJSError = false;
  pJSMessage = '';
}



/*'''---------------------------------
'' JSTypeMessageText(type, params){
--------------------------------------
   Created By: SySys:bh
   Compatibility: IE6
   Description: returns string that has been added using addJSMessage
   Parameters: 
     type: default validation types (date, integer, money, numeric, email, emailList, calendar, max(x), min(x))
     params: parameters (min max etc)
   Returns: message text that is added automatically for the given type to be displayed in the JSMessage
   Notes:  
     The <Label> tag allows you to insert the label into your message.
     The <Value> tag allows you to insert the value of the form field into your message.
----------------------------------'''*/ 
function JSTypeMessageText(type, params){
  var msg = '';
  
  switch(type.toLowerCase()){
    case 'date':
      msg = 'The value \'<Value>\' in the \'<Label>\' must be a valid date.';
      break;
    case 'integer':
      msg = 'The value \'<Value>\' in the \'<Label>\' must be a valid integer.';
      break;
    case 'money':
      msg = 'The value \'<Value>\' in the \'<Label>\' must be a valid monitary value.';
      break;
    case 'numeric':
      msg = 'The value \'<Value>\' in the \'<Label>\' must be a valid number.';
      break;
    case 'email':
      msg = 'The value \'<Value>\' in the \'<Label>\' must be a single valid email address.';
      break;
    case 'emaillist':
      msg = 'The value \'<Value>\' in the \'<Label>\' must be a valid list of email addresses (separated by comma or semicolons).';
      break;
    case 'max':
      msg = 'The value in the \'<Label>\' is to long.  It will only accept ' + params + ' characters.';
      break;
    case 'min':
      msg = 'The value in the \'<Label>\' does not contain enough caracters.  It must be at least ' + params + ' characters long.';
      break;
    case 'calendar':
      msg = 'The value in the \'<Label>\' field does not contain a valid date.';
      break;
    case 'forcerequired':
      msg = 'The \'<Label>\' field requires a value.';
      break;
    default:
      msg = '> Unknown JSTypeMessageText for type \'' + type + '\'.  Please edit the validate.js file.';
  }//end switch    
  return msg;
}


/*'''---------------------------------
'' doNothing()
--------------------------------------
   Created By: SySys:bh
   Compatibility: ALL
   Description: dummy function that returns nothing
   Parameters: None
   Returns: nonthing
----------------------------------'''*/ 
function doNothing(){}
