// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function stripNonDecimal(value) {
    return value.replace(/[^0-9.]/g, "");
}

function parseDecimal(value) {
    return parseFloat(stripNonDecimal(value));
}

function validEmail(email) {
//    var emailRegEx = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i;
    var emailRegEx = /^(.+)@(.+)\.(.+)$/i;
    if(email.match(emailRegEx)){
        return true;
    }
    else{
        return false;
    }
}

function loadQuestion(question_id, survey_id){
    if($('question_' + question_id + '_container').empty()){
        new Ajax.Request('/my/surveys/' + survey_id + '/questions/' + question_id + '/load_question', {});
    }
}

function reload_question(question_id, survey_id)
{
  new Ajax.Request('/my/surveys/' + survey_id + '/questions/' + question_id + '/reload', {asynchronous:true, evalScripts:true, method:'post'});
}

function loadSection(survey_section_id, survey_id){
  if($('survey_section_' + survey_section_id + '_container').empty()){
    new Ajax.Request('/my/surveys/' + survey_id + '/survey_sections/' + survey_section_id + '/load_section', {});
  }
}
function emptySection(survey_section_id){
  $('survey_section_' + survey_section_id + '_container').update();
}

function loadSectionWithQuestion(question_id, survey_section_id, survey_id){
  if($('survey_section_' + survey_section_id + '_container').empty()){
    new Ajax.Request('/my/surveys/' + survey_id + '/survey_sections/' + survey_section_id + '/load_section?question_id=' + question_id, {});
  }
}

function expandQuestion(question_id, survey_section_id, survey_id)
{
  new Ajax.Request('/my/surveys/' + survey_id + '/survey_sections/' + survey_section_id + '/load_section/?question_id=' + question_id, {});
}

//toggable rows in questions delegations
function trToggle(holderId)
{
    $$('.toggable_question_'+holderId).each(function(element) {element.toggle()} )
    
    $$('#question_bt_'+holderId+" .show_toggle_button")[0].toggle();
    $$('#question_bt_'+holderId+" .hide_toggle_button")[0].toggle();
}

function toggleAllRows(checked)
{
    $$('.toggable_row').each(function(element)
        {
            if(checked) element.hide();
            else element.show();
        }
    )
    
    if (checked)
        {
            $$('.show_toggle_button').each(function(element) {element.show()} )
            $$('.hide_toggle_button').each(function(element) {element.hide()} )
        }
        else
            {
                $$('.show_toggle_button').each(function(element) {element.hide()} )
                $$('.hide_toggle_button').each(function(element) {element.show()} )
            }
            
}
//--
        