// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function create_webmenu(menu) {
    menu.setSkin("Sirop");
    menu.setImagePath("/images/research/dhtmlxwebmenu/");
    menu.setOpenMode("web");
    menu.attachEvent("onClick", function(itemId, itemType) {
            //menu.hide();
            var link = menu.getUserData(itemId, "href");
            window.location.href = link;
    });
    menu.loadXML('<%= url_for(:action => "menu_bar_xml", :selected => @main_menu_active) %>', function(){menu.init();});
}

function js_redirect_to_relative(link) {
    var url = window.location.protocol + "//" + window.location.host + link; //does not work on Safari in development mode, but should work on production server.
    url = url.replace("\n", ""); // remove line break that would cause problem in safari
    window.location.href = url;       
}





// VALIDATION HINTS/////////////////////////////////////
// this part is for the form field hints to display
// only on the condition that the text input has focus.
// otherwise, it stays hidden.

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}


function prepareForHints() {
  prepare("input");
  prepare("textarea");
  prepare("select");
}

function prepare(elem){
  var inputs = document.getElementsByTagName(elem);
  for (var i=0; i<inputs.length; i++){
    if (inputs[i].parentNode.className == 'standard_form_row') {
      inputs[i].onmouseover = function () {
        var node = this.parentNode;
        if (node.className == "fieldWithErrors"){
          node = node.parentNode;
        }
        node.getElementsByClassName("hint")[0].style.display = "inline";
      }
      inputs[i].onmouseout = function () {
        var node = this.parentNode;
        if (node.className == "fieldWithErrors"){
          node = node.parentNode;
        }
        node.getElementsByClassName("hint")[0].style.display = "none";
      }

      if (inputs[i].type != "checkbox") {
        inputs[i].onfocus = function () {
          var node = this.parentNode;
          if (node.className == "fieldWithErrors"){
            node = node.parentNode;
          }
          node.getElementsByClassName("hint")[0].style.display = "inline";
        }
        inputs[i].onblur = function () {
          var node = this.parentNode;
          if (node.className == "fieldWithErrors"){
            node = node.parentNode;
          }
          node.getElementsByClassName("hint")[0].style.display = "none";
        } 
      }

      //execute validators
      if (inputs[i].onchange)
        inputs[i].onchange();
    }
  }
}

//Validation reflection//////////////////////////////////////

function switchClassName(node, name){
  
  if ((node.getElementsByClassName("tool_tip")).length == 1){
    node.getElementsByClassName("tool_tip")[0].className = name; return;
  }
  if ((node.getElementsByClassName("welldone")).length == 1){
    node.getElementsByClassName("welldone")[0].className = name; return;
  }
  if ((node.getElementsByClassName("kindagood")).length == 1){
    node.getElementsByClassName("kindagood")[0].className = name; return;
  }
  alert('return?');
}



//VALIDATION FUNCTIONS///////////////////////////////////////

function inline_validate(is_ok, whatYouTyped){
  var form_row = whatYouTyped.parentNode;
  if (form_row.className == "fieldWithErrors"){
    form_row = form_row.parentNode;
  }
  
  if (is_ok) {
    switchClassName(form_row, "welldone");
  }
  else {
    switchClassName(form_row, "kindagood");
  }
}

function checkPrompt(whatYouTyped) {
  return (whatYouTyped.value > 0);
}

function checkNumericality(whatYouTyped) {
  var num = whatYouTyped.value;
  return (!isNaN(num) && num >= 0);
}

function checkPresence(whatYouTyped) {
  var txt = whatYouTyped.value;
  return (txt.length > 0);
}

function checkCheckedValue(whatYouTyped){
  return (whatYouTyped.checked);
}

function GridExtract(grid, col) {
  var array = new Array();
  
  for (var i = 0; i < grid.getRowsNum(); i++) {
    
    array[i] = grid.cells2(i, col).getValue();
    //var x = array[i];
    //alert(x);
  }
  return array;
}


// cookie checking *************************************

function cookies_enabled(redirect, home) {
    if (!navigator.cookieEnabled) {
        if (redirect==true) {
            window.location = "/" + home + "/user/browser_problem/cookies";            
         }
         return true;
    }
    return false;
}

// *********************************************************