// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
var contactsBlock = {
  editExisting: function(id) {
/*  Element.show('edit_contact_' + id + '_address');
    Element.show('edit_contact_' + id + '_city');
    Element.show('edit_contact_' + id + '_postal_code');
    Element.show('edit_contact_' + id + '_country');
    Element.show('edit_contact_' + id + '_tel_1');
    Element.show('edit_contact_' + id + '_tel_2');
    Element.hide('show_contact_' + id + '_address');
    Element.hide('show_contact_' + id + '_city');
    Element.hide('show_contact_' + id + '_postal_code');
    Element.hide('show_contact_' + id + '_country');
    Element.hide('show_contact_' + id + '_tel_1');
    Element.hide('show_contact_' + id + '_tel_2');*/
    util.toggleEdit('edit_contact_' + id, 'show_contact_' + id)
    if($('contact_' + id + '_controls'))
      Element.show('contact_' + id + '_controls');
  },
  
  showExisting: function(id) {
/*  Element.hide('edit_contact_' + id + '_address');
    Element.hide('edit_contact_' + id + '_city');
    Element.hide('edit_contact_' + id + '_postal_code');
    Element.hide('edit_contact_' + id + '_country');
    Element.hide('edit_contact_' + id + '_tel_1');
    Element.hide('edit_contact_' + id + '_tel_2');
    Element.show('show_contact_' + id + '_address');
    Element.show('show_contact_' + id + '_city');
    Element.show('show_contact_' + id + '_postal_code');
    Element.show('show_contact_' + id + '_country');
    Element.show('show_contact_' + id + '_tel_1');
    Element.show('show_contact_' + id + '_tel_2');*/
    util.toggleEdit('show_contact_' + id, 'edit_contact_' + id)
    if($('contact_' + id + '_controls'))
      Element.hide('contact_' + id + '_controls');
  },
  
  addNew: function(contact_type) {
    Element.show(contact_type + '_contact');
    Element.hide(contact_type + '_contact_message');
  },
  
  cancelNew: function(contact_type) {
    Element.hide(contact_type + '_contact');
    Element.show(contact_type + '_contact_message');
  }
}

var matrixBlock = {
  toggle: function(row,col,t) {
    e = $('matrix_' + col + '_' + row)
    if(e.checked && col > 0)
      $('matrix_0_' + row).checked = false;
    if(e.checked && col == 0) {
      for(i = 1; i <= t; i++)
        $('matrix_' + i + '_' + row).checked = false;
    } 
  }
}

var parentsBlock = {
  editExisting: function(id) {
    //contactsBlock.editExisting(contact_id);
/*  Element.show('edit_parent_' + id + '_name');
    Element.show('edit_parent_' + id + '_relationship');
    Element.show('edit_parent_' + id + '_race');
    Element.show('edit_parent_' + id + '_religion');
    Element.show('edit_parent_' + id + '_nationality');
    Element.show('edit_parent_' + id + '_country_of_residence');
    Element.show('edit_parent_' + id + '_occupation');
    Element.show('edit_parent_' + id + '_employer');
    Element.hide('show_parent_' + id + '_name');
    Element.hide('show_parent_' + id + '_relationship');
    Element.hide('show_parent_' + id + '_race');
    Element.hide('show_parent_' + id + '_religion');
    Element.hide('show_parent_' + id + '_nationality');
    Element.hide('show_parent_' + id + '_country_of_residence');
    Element.hide('show_parent_' + id + '_occupation');
    Element.hide('show_parent_' + id + '_employer');*/
    util.toggleEdit('edit_parent_' + id, 'show_parent_' + id);
    Element.show('parent_' + id + '_controls');
  },
  
  showExisting: function(id) {
    //contactsBlock.showExisting(contact_id);
    /*Element.hide('edit_parent_' + id + '_name');
    Element.hide('edit_parent_' + id + '_relationship');
    Element.hide('edit_parent_' + id + '_race');
    Element.hide('edit_parent_' + id + '_religion');
    Element.hide('edit_parent_' + id + '_nationality');
    Element.hide('edit_parent_' + id + '_country_of_residence');
    Element.hide('edit_parent_' + id + '_occupation');
    Element.hide('edit_parent_' + id + '_employer');
    Element.show('show_parent_' + id + '_name');
    Element.show('show_parent_' + id + '_relationship');
    Element.show('show_parent_' + id + '_race');
    Element.show('show_parent_' + id + '_religion');
    Element.show('show_parent_' + id + '_nationality');
    Element.show('show_parent_' + id + '_country_of_residence');
    Element.show('show_parent_' + id + '_occupation');
    Element.show('show_parent_' + id + '_employer');*/
    util.toggleEdit('show_parent_' + id, 'edit_parent_' + id);
    Element.hide('parent_' + id + '_controls');
  },
  
  addNew: function(parent_no) {
    //Element.show('parent_info_' + parent_no);
    Element.show('parent_info');
    Element.hide('parents_info_message');
  },
  
  cancelNew: function(parent_no) {
    //Element.hide('parent_info_' + parent_no);
    Element.hide('parent_info');
    Element.show('parents_info_message');
  }
}

var scoresBlock = {
  editExisting: function(id) {
    Element.show('edit_score_' + id);
    Element.hide('show_score_' + id);
  },
  
  showExisting: function(id) {
    Element.hide('edit_score_' + id);
    Element.show('show_score_' + id);
  },
  
  addNew: function(id) {
    Element.show('add_test_' + id + '_score');
    Element.hide('show_test_' + id + '_score');
  },
  
  cancelNew: function(id) {
    Element.hide('add_test_' + id + '_score');
    Element.show('show_test_' + id + '_score');
  }
}

var studentAssessmentsBlock = {
  edit: function() {
    Element.show('edit_assessments');
    Element.hide('show_assessments');
  },
  
  show: function() {
    Element.hide('edit_assessments');
    Element.show('show_assessments');
  },

  showGuideline: function(conduct) {
    Element.show(conduct);
  },
  
  hideGuideline: function(conduct) {
    Element.hide(conduct);
  },
  
  toggleHighlight: function(student_id, className) {
    $('student_' + student_id).className = className;
  }
}

var studentBlock = {
  edit: function() {
    util.toggleEdit('edit_student_','show_student_');
    Element.show('student_controls');
  },
  
  show: function() {
    util.toggleEdit('show_student_','edit_student_');
    Element.hide('student_controls');
  }
}

var util = {
  toggleEdit: function(show,hide) {
    dds = document.getElementsByTagName('dd');
    for(var i=0;i<dds.length;i++) {
      if(dds[i].id) {
        if(dds[i].id.substring(0,show.length) == show)
          Element.show(dds[i]);
        if(dds[i].id.substring(0,hide.length) == hide)
          Element.hide(dds[i]);
      }
    }
  }
}

var attendanceBlock = {
  showReason: function(student_id) {
    Element.show('attendance_' + student_id + '_reason');
    this.toggleRemarks(student_id);
  },
  
  hideReason: function(student_id) {
    Element.hide('attendance_' + student_id + '_reason');
    Element.hide('attendance_'+ student_id +'_remarks');
  },
  
  showRemarks: function(student_id) {
    Element.show('attendance_'+ student_id +'_remarks');
    Field.focus('attendance_'+ student_id +'_remarks');
  },
  
  hideRemarks: function(student_id) {
    Element.hide('attendance_'+ student_id +'_remarks');
  },
  
  toggleReason: function(student_id, type_id) {
    $('student_' + student_id).style.backgroundColor = 'transparent';
    if(String(type_id) == '3')
      this.showReason(student_id);
    else if(String(type_id) == '2' || String(type_id) == '5') {
      this.hideReason(student_id);
      this.showRemarks(student_id);
    }
    else
      this.hideReason(student_id);
  },
  
  toggleRemarks: function(student_id) {
    if($F('attendance_'+ student_id +'_reason') == 'Others')
      this.showRemarks(student_id);
    else
      this.hideRemarks(student_id);
  },
  
  toggleAllPresent: function(aform) {
    var p_name = /^attendance_\d+_1$/;
    var other_names = /^attendance_\d+_[2-5]$/;
    radios = Form.getInputs(aform, 'radio');
    for(var i=0;i<radios.length;i++) {
      element = radios[i];
      if(p_name.test(element.id))
        element.checked = true;
      else if(other_names.test(element.id))
        element.checked = false;
      sid = element.id.split('_')[1];
      this.hideReason(sid);
      $('student_' + sid).style.backgroundColor = 'transparent';
    }
  },
  
  validate: function(aform) {
    var id_regex = /^attendance_\d+_1$/;
    var unmarked = new Array();
    radios = Form.getInputs(aform, 'radio');
    for(var i=0;i<radios.length;i++) {
      radio = radios[i];
      if(id_regex.test(radio.id)) {
        sid = radio.id.split('_')[1]
        if(!this.checkedAnyAttendanceType(sid)) {
          unmarked.push(sid);
        }
      }
    }
    if(unmarked.length > 0) {
      errorBlock.unmarkAttendanceError(unmarked);
      return false;
    }
    return true;
  },
  
  checkedAnyAttendanceType: function(student_id) {
    for(var i=1;i<6;i++) {
      if($('attendance_' + student_id + '_' + i).checked)
        return true;
    }
    return false;
  }
}

var validationBlock = {
  validateInteger: function(textobj){
    if(this.isInteger($F(textobj)))
      return true;
    else {
      errorBlock.integerError();
      textobj.value = textobj.defaultValue;
      textobj.focus();
      return false;
    }
  },
  
  validateNumber: function(textobj) {
    if(this.isNumeric($F(textobj)))
      return true;
    else {
      errorBlock.numericError();
      textobj.value = textobj.defaultValue;
      textobj.focus();
      return false;
    }
  },
  
  validateRange: function(textobj,lower,upper) {
    if(this.validateNumber(textobj)) {
      if(!this.isInRange($F(textobj),lower,upper))
        textobj.value = textobj.defaultValue;
      else
        return true;
    }
    return false;
  },
  
  validateMinSec: function(textobj) {
    if(this.isMinSec($F(textobj)))
      return true;
    else {
      errorBlock.minSecError();
      textobj.value = textobj.defaultValue;
      textobj.focus();
      return false;
    }
  },
  
  isInteger: function(val) {
    var numeric = /^\d*$||^\B$/;
    return numeric.test(val);    
  },
  
  isNumeric: function(val) {
    var numeric = /^-?\d+$|^-?\d+\.\d+$|^\B$/;
    return numeric.test(val);
  },
  
  isInRange: function(val,lower,upper) {
    if(parseFloat(val) < lower) {
      errorBlock.rangeError("less", lower);
      return false;
    }
    if(parseFloat(val) > upper) {
      errorBlock.rangeError("greater", upper);
      return false;
    }
    return true;
  },
  
  isMinSec: function(val) {
    var minsec = /^\d*\d:[543210]\d$|^\B$/;
    return minsec.test(val);
  }
}

var errorBlock = {
  integerError: function() {
    alert("The value you have entered is not a valid integer!");
  },
  
  numericError: function() {
    alert("The value you have entered is not a valid number!");
  },
  
  rangeError: function(exit, limit) {
    alert("The value you have entered must not be " + exit + " then " + limit);
  },
  
  unmarkAttendanceError: function(unmarked) {
    alert("Please mark the attendance of students highlighted in pink.");
  },
  
  minSecError: function() {
    alert("Ther value have entered is not a valide time format. Please use mm:ss. eg 12:30.");
  }
}

var TabBox = Class.create();
TabBox.prototype = {
  
  initialize: function(element_id) {
    var box = $(element_id);
    this.tabs = box.getElementsByTagName('li');
    //this.tab_areas = box.getElementsByTagName('div');
    this.tab_areas = document.getElementsByClassName('tab_area', box);
    for (var i=0; i < this.tabs.length; i++) {
      this.setupTab(this.tabs[i]);
    }
    //for (var i=0; i < this.tab_areas.length; i++) {
    //  this.tab_areas[i].hide();
    //}
    // this.tab_areas[0].show();
  },
  
  onClickTab: function(event) {
    var tab = Event.element(event);
    this.unselectAllTabs();
    tab.className = 'selected';
    this.hideAllTabAreas();
    this.tab_areas[this.selectedIndex(tab)].show();
  },
  
  unselectAllTabs: function() {
    for (var i=0; i < this.tabs.length; i++) {
      this.tabs[i].className = '';
    }
  },
  
  hideAllTabAreas: function() {
    for (var i=0; i < this.tab_areas.length; i++) {
      this.tab_areas[i].hide();
    }
  },
  
  selectedIndex: function(tab) {
    for (var i=0; i < this.tabs.length; i++) {
      if(this.tabs[i].id == tab.id) return i;
    }
  },
  
  setupTab: function(tab) {
    Event.observe(tab, 'click', this.onClickTab.bindAsEventListener(this));
  }
}

var ccaBlock = {
  toggleSearchAdd: function(link) {
    var cur = link.innerHTML;
    if(cur == 'Search') {
      link.innerHTML = 'Add';
      $('user_action').value = 'search';
      $('list_all').show();
    }
    else {
      link.innerHTML = 'Search';
      $('user_action').value = 'new';
      $('list_all').hide();
    }
    $('action_name').innerHTML = cur;
    $('student_name').focus();
  },
  
  participationOnClick: function(participation) {
    var ids = participation.id.split('_');
    if(ids[0] == 'create') {
      new Ajax.Request('/co_curricular_activities/create_participation_in_overview', {asynchronous:true, evalScripts:true, parameters:'id=' + ids[1] + '&year=' + ids[2] + '&term=' + ids[3] + '&student_id=' + ids[4]});
    }
    else {
      new Ajax.Request('/co_curricular_activities/delete_participation_in_overview', {asynchronous:true, evalScripts:true, parameters:'id=' + ids[1]});
    }
  }
}