






/************************************************/
/*         SURVEY VALIDATION Customer Tag       */

var checkFields = new Array();
var errorMessages = new Array();
var idx = 0;
var _password = '';
var _confirmPassword = '';
var _addMaintainCustomerTagValidation = false;

function maintainCustomerTagValidation() {

 var alertMsg = "";
 
 for(var i=0; i<checkFields.length; i++) {
 
    var fld = document.getElementById( checkFields[i] );
    
    if(fld && checkFields[i].indexOf('email') != -1 && !validateEmail(fld) ) {
      alertMsg += errorMessages[i] + "\n";
    }
    else if( fld && checkFields[i].indexOf('password') != -1) {
         _password = fld.value;
         if( '' == trim(fld.value) ) {
           alertMsg += errorMessages[i] + "\n";
         }
    }
    else if( fld && checkFields[i].indexOf('confirmPassword') != -1) {
         _confirmPassword = fld.value;
         if( '' == trim(fld.value) ) {
           alertMsg += errorMessages[i] + "\n";
         }
    }
    else if( fld && '' == trim(fld.value)) {
      alertMsg += errorMessages[i] + "\n";
    }
 }
 
 if ( '' != trim(_password) && '' != trim(_confirmPassword) && trim(_password) != trim(_confirmPassword) ) {
      alertMsg += "Password and Confirm Password dose not match " + "\n";
 }

 return alertMsg;
}

function addEvent (elmt, eventType, handler, captureMode){
    if(document.addEventListener){
        elmt.addEventListener(eventType, handler, captureMode)
    }
    else{
        elmt.attachEvent('on'+eventType, handler);
    }
}


function isArray(obj) {
    if (obj.constructor.toString().indexOf("Array") == -1) {
        return false;
    }
    else {
        return true;
    }
}

/************************************************/

/* START Survey Validator dynamicSurvey  */

var vaidateFields = new Array();
var vaidateFieldsNames = new Array();
var initialText = ""


function validateSurvey() {
 var alertMsg = "";
 
 for(var i=0; i<vaidateFields.length; i++) {

    var _vaidateFields = vaidateFields[i];

    if( isArray( _vaidateFields ) ) {
        var _vaildItem = false;
        for(var j = 0; j < _vaidateFields.length; j++) {
            var fld = document.getElementsByName( _vaidateFields[j] );
            
            alert(fld);
            
            if ( validateFieldBoolean( fld ) ) {
                _vaildItem = true;
                break;
            }
        }

        if( !_vaildItem ) {
            alertMsg += vaidateFieldsNames[i] + '\n';
        }
    }
    else {
        var fld = document.getElementsByName( _vaidateFields );
        
        if( fld && fld[0] ) {
            alertMsg += validateField( i, fld );
        }
    }
 }

 alertMsg += maintainCustomerTagValidation();
 
 if( alertMsg != '' ) {
    alert( initialText + alertMsg );
    return false;
 }
 
 return true;
}

function validateField(_idx, _fld ) {
    var _alertMsg = '';
    var _len = _fld.length;
    
    if( _len == 1 ) {
      if(_fld[0].type == 'text' && (_fld[0].value == null || trim( _fld[0].value ) == '') ) {
          _alertMsg += vaidateFieldsNames[_idx] + '\n';
      }
      if(_fld[0].type == 'select-one') {
         var _val =  _fld[0].options[ _fld[0].selectedIndex ].value;
         if( _val == null || trim( _val ) == '' || trim( _val ) == '-1' ) {
            _alertMsg += vaidateFieldsNames[_idx] + '\n';
         }
      }
      if(_fld[0].type == 'textarea' && (_fld[0].value == null || trim( _fld[0].value ) == '') ) {
          _alertMsg += vaidateFieldsNames[_idx] + '\n';
      }
      if(_fld[0].type == 'checkbox' && _fld[0].checked != true ) {
          _alertMsg += vaidateFieldsNames[_idx] + '\n';
      }
    }
    else {
      if(_fld[0].type == 'radio' && !validateRadio(_fld) ) {
          _alertMsg += vaidateFieldsNames[_idx] + '\n';
      }    
    }

    return _alertMsg;
}

function validateFieldBoolean( _fld ) {
    var _returnValue = true;
    var _len = _fld.length;

    if( _len == 1 ) {
      if(_fld[0].type == 'text' && (_fld[0].value == null || trim( _fld[0].value ) == '') ) {
          _returnValue = false;
      }
      if(_fld[0].type == 'select-one' ) {
         var _val =  _fld[0].options[ _fld[0].selectedIndex ].value;
         if( _val == null || trim( _val ) == '' || trim( _val ) == '-1' ) {
            _returnValue = false;
         }
      }
      if(_fld[0].type == 'textarea' && (_fld[0].value == null || trim( _fld[0].value ) == '') ) {
          _returnValue = false;
      }
      if(_fld[0].type == 'checkbox' && _fld[0].checked != true ) {
          _returnValue = false;
      }
    }
    else {
      if(_fld[0].type == 'radio' && !validateRadio(_fld) ) {
          _returnValue = false;
      }
    }

    return _returnValue;
}


function validateRadio(_obj){
  var result = false;
  for(var i=0; i<_obj.length; i++){
    if(_obj[i].checked==true) result = true;
  }
  return result;
}



/* END Survey Validator dynamicSurvey  */