
//////////////////
//              //
// MYACTTV_I.js //
//              //
//////////////////


///////////////////////////////////////////////
//                                           //
// THIS MUST BE CHANGED IF THE SITE IS MOVED //
//                                           //
///////////////////////////////////////////////

var base_URL = "";
var toggle = 0;

/////////////////////////
// CHECK EMAIL ADDRESS //
/////////////////////////
function check_email_address(email)
{
  var at = "@";
  var dot = ".";
  var location_of_at = email.indexOf(at);
  var location_of_dot = email.indexOf(dot);
  var length_of_address = email.length;
  var error_message = 'Please enter a valid e-mail address.';
  
  if (location_of_at == -1 || location_of_at == 0 || location_of_at == length_of_address)
  {
    return error_message;
  }
  else if (location_of_dot == -1 || location_of_dot == 0 || location_of_dot == length_of_address - 1)
  {
    return error_message;
  }
  else if (email.indexOf(at,(location_of_at + 1)) != -1)
  {
    return error_message;
  }
  else if (email.substring(location_of_at - 1, location_of_at) == dot || email.substring(location_of_at + 1, location_of_at + 2) == dot)
  {
    return error_message;
  }
  else if (email.indexOf(dot,(location_of_at + 2)) == -1)
  {
    return error_message;
  }
  else if (email.indexOf(" ") != -1)
  {
    return error_message;
  }
  else
  {
    return '1';
  }
}

//////////////////
// WHAT COUNTRY //
//////////////////
function what_country()
{
  var country = document.getElementById('country').value;
  var state_row = document.getElementById('state_row');
  var zip_row = document.getElementById('zip_row');
  var province_row = document.getElementById('province_row');
  var postal_row = document.getElementById('postal_row');

  // FF uses 'table-row', IE uses 'block'.
  if (country == 'USA')
  {
    if( document.getElementById && navigator.product != 'Gecko')
    {
    	state_row.style.display = 'block';
    	zip_row.style.display = 'block';
    }
    else
    {
      state_row.style.display = 'table-row';
      zip_row.style.display = 'table-row';
    }
    province_row.style.display = 'none';
    postal_row.style.display = 'none';
  }
  else if (country == 'CA')
  {
    state_row.style.display = 'none';
    zip_row.style.display = 'none';
    if( document.getElementById  && navigator.product != 'Gecko')
    {
      province_row.style.display = 'block';
      postal_row.style.display = 'block';
    }
    else
    {
      province_row.style.display = 'table-row';
      postal_row.style.display = 'table-row';
    }
  }
  else
  {
    state_row.style.display = 'none';
    zip_row.style.display = 'none';
    province_row.style.display = 'none';
    postal_row.style.display = 'none';
  }
  
  return true;  
}


/////////////////////
// CHECK ROLE DATA //
/////////////////////
function check_role_data()
{
  var name          = document.add_roles.role_name.value;
  var high_age      = document.add_roles.high_age.value;
  var low_age       = document.add_roles.low_age.value;
  var any_age       = document.add_roles.RoleAnyAge.checked;
  var ethnic_appear = document.add_roles.ethnic_appear.value;
  var any_ethnicity = document.add_roles.RoleAnyEthnicity.checked;
  var description   = document.add_roles.description.value;
  var pay_rate      = document.add_roles.pay_rate.value;
  
  if (name == '' || description == '' || pay_rate == '' || (ethnic_appear == '' && any_ethnicity == false) || ((low_age == '' || high_age == '') && any_age == false))
  {
    alert('You forgot to enter data in some required field(s).');
    return;
  }
  else if (any_age == true && (low_age != '' || high_age != ''))
  {
    alert('You have selected \"any age\" and entered an age range, please choose one or the other.');
    return;
  }
  else if (any_age == false && (isNaN(low_age) || isNaN(high_age)))
  {
    alert('Please enter numbers for the ages in \"age range\".');
    return;
  }
  else if (any_age == false && low_age >= high_age)
  {
    alert('The low age must be less than the high age.');
    return;
  }
  else if (any_ethnicity == true && ethnic_appear != '')
  {
    alert('You have selected an ethnicity and \"any ethnicity\", please choose one or the other.');
    return;
  }
  else
  {
    document.add_roles.submit();
  }
}

////////////////////////
// CHECK PROJECT DATA //
////////////////////////
function check_project_data()
{
  var project_name     = document.enter_project_data.project_name.value;
  var casting_director = document.enter_project_data.casting_director.value;
  var shoot_month      = document.enter_project_data.shoot_month.value;
  var shoot_day        = document.enter_project_data.shoot_day.value;
  var shoot_year       = document.enter_project_data.shoot_year.value;
  var location         = document.enter_project_data.location.value;
  var email            = document.enter_project_data.email.value;
  var telephone        = document.enter_project_data.telephone.value;
  var state            = document.enter_project_data.state.value;
  var all_states       = document.enter_project_data.all_states.checked;
  
  var audition_month   = document.enter_project_data.audition_month.value;
  var audition_day     = document.enter_project_data.audition_day.value;
  var audition_year    = document.enter_project_data.audition_year.value;
  
  var sub_month        = document.enter_project_data.sub_month.value;
  var sub_day          = document.enter_project_data.sub_day.value;
  var sub_year         = document.enter_project_data.sub_year.value;

  var check = '(project_name != "")';
  check    += ' && (casting_director != "")';
  check    += ' && (location != "")';
  check    += ' && (email != "")';
  check    += ' && (telephone != "")';
  check    += ' && ((state != "")';
  check    += ' || (all_states == true))';
  
  if (eval(check))
  {
    var response = check_email_address(email);
    if (response != '1')
    {
      alert(response);
      return;
    }
    
    if (state != '' && all_states == true)
    {
      alert('You have selected one or more states and\nalso selected \'all\' states. Which is it?');
      return;
    }

    if (shoot_month == '' || shoot_day == '' || shoot_year == '')
    {
      alert('You have not entered a valid shoot/start date.');
      return;
    }

    if ((audition_month != '' || audition_day != ''  || audition_year != '') && (audition_month == '' || audition_day == ''  || audition_year == ''))
    {
      alert('You have not entered a valid audition date.');
      return;
    }

    if ((sub_month != '' || sub_day != ''  || sub_year != '') && (sub_month == '' || sub_day == ''  || sub_year == ''))
    {
      alert('You have not entered a valid submission date.');
      return;
    }
    else
    {
      document.enter_project_data.submit();
    }
  }
  else
  {
    alert('You have neglected to enter some required data.\nPlease check the required fields for data.');
    return;
  }
}

/////////////////////////////
// CHECK EDIT PROJECT DATA //
/////////////////////////////
function check_edit_project_data()
{
  var project_name     = document.edit_project_data.project_name.value;
  var casting_director = document.edit_project_data.casting_director.value;
  var location         = document.edit_project_data.location.value;
  var email            = document.edit_project_data.email.value;
  var telephone        = document.edit_project_data.telephone.value;
  var state            = document.edit_project_data.state.value;
  var all_states       = document.edit_project_data.all_states.checked;

  var check = '(project_name != "")';
  check    += ' && (casting_director != "")';
  check    += ' && (location != "")';
  check    += ' && (email != "")';
  check    += ' && (telephone != "")';
  check    += ' && ((state != "")';
  check    += ' || (all_states == true))';
  
  if (eval(check))
  {
    var response = check_email_address(email);
    if (response != '1')
    {
      alert(response);
      return;
    }
    
    if (state != '' && all_states == true)
    {
      alert('You have selected one or more states and\nalso selected \'all\' states. Which is it?');
      return;
    }
    else
    {
      document.edit_project_data.submit();
    }
  }
  else
  {
    alert('You have neglected to enter some required data.\nPlease check the required fields for data.');
    return;
  }
}

/////////////////////////////////////////////////////////
// DISPLAY OF NON-REQUIRED PROJECT DATA INPUT FIELDS   //
//                                                     //
// $show_additional = 0, don't show additional fields. //
// $show_additional = 1, show additional fields.       //
//                                                     //
// $additional_data is set by JS code in the view.     //
/////////////////////////////////////////////////////////

if (show_additional)
{
  //alert(show_additional);
  // Show the additional fields.
  document.getElementById('toggle_button').value ='close';
  document.getElementById('extra').style.display='block';
  toggle = 1;
}
else
{
  //alert(show_additional);
  // Don't show the additional fields.
  document.getElementById('toggle_button').value ='open';
  document.getElementById('extra').style.display='none';
  toggle = 0;
}

//////////////////////////////////////////////////////////////
// TOGGLE DISPLAY OF NON-REQUIRED PROJECT DATA INPUT FIELDS //
//////////////////////////////////////////////////////////////

function toggle_display(source)
{
  var form_name = '';
  if (source == 'edit')
  {
    form_name = 'edit_project_data';
  }
  else
  {
    form_name = 'enter_project_data';
  }
  if (toggle)
  {
    // Hide extra fields.
    var director           = eval('document.' + form_name + '.director.value');
    var producer           = eval('document.' + form_name + '.producer.value');
    var production_company = eval('document.' + form_name + '.production_company.value');
    var audition_location  = eval('document.' + form_name + '.audition_location.value');
    var synopsis           = eval('document.' + form_name + '.synopsis.value');
    var project_info       = eval('document.' + form_name + '.project_info.value');
    var production_info    = eval('document.' + form_name + '.production_info.value');
    var hard_sub_address   = eval('document.' + form_name + '.hard_sub_address.value');
    
    if (director == '' && producer == '' && production_company == '' && audition_location == '' && synopsis == '' && project_info == '' && production_info == '' && hard_sub_address == '')
    {
      document.getElementById('toggle_button').value ='open';
      document.getElementById('extra').style.display='none';
      toggle = 0;
    }
    else
    {
      alert('You have entered some date in the non-required section.\nThis section may not be closed unless all the fields are empty.');
      return;
    }
  }
  else
  {
    // Show extra fields.
    document.getElementById('toggle_button').value ='close';
    document.getElementById('extra').style.display='block';
    toggle = 1;
  }
}

///////////////
// EDIT ROLE //
///////////////
function edit_role(role_id, source)
{
  var url = base_URL + '/casting_admins/edit_role/' + role_id + '/' + source;
  window.location = url;
}

//////////////////////////
// CHECK EDIT ROLE DATA //
//////////////////////////
function check_edit_role_data()
{
  var name          = document.edit_role.role_name.value;
  var high_age      = document.edit_role.high_age.value;
  var low_age       = document.edit_role.low_age.value;
  var any_age       = document.edit_role.any_age.checked;
  var ethnic_appear = document.edit_role.ethnic_appear.value;
  var any_ethnicity = document.edit_role.any_ethnicity.checked;
  var description   = document.edit_role.description.value;
  var pay_rate      = document.edit_role.pay_rate.value;

  if (name == '' || description == '' || pay_rate == '' || (ethnic_appear == '' && any_ethnicity == false) || ((low_age == '' || high_age == '') && any_age == false))
  {
    alert('You forgot to enter data in some required field(s).');
    return;
  }
  else
  {
    if (any_age == true && (low_age != '' || high_age != ''))
    {
      alert('You have selected \"any age\" and entered an age range, please choose one or the other.');
      return;
    }
    else
    {
      if (any_age == false && (isNaN(low_age) || isNaN(high_age)))
      {
        alert('Please enter numbers for the ages in \"age range\".');
        return;
      }
      else
      {
        if (any_age == false && low_age >= high_age)
        {
          alert('The low age must be less than the high age.');
          return;
        }
        else
        {
          if (any_ethnicity == true && ethnic_appear != '')
          {
            alert('You have selected an ethnicity and \"any ethnicity\", please choose one or the other.');
            return;
          }
          else
          {
            document.edit_role.submit();
          }
        }
      }
    }
  }
}

/////////////////
// DELETE ROLE //
/////////////////
function delete_role(role_id, source)
{
  var url = base_URL + '/casting_admins/delete_role/' + role_id + '/' + source;
  window.location = url;
}

////////////////////////////
// CHECK ADMIN LOGIN DATA //
////////////////////////////
function check_admin_login_data()
{
  var email          = document.admin_login_data.email.value; 
  var new_password_1 = document.admin_login_data.new_password_1.value; 
  var new_password_2 = document.admin_login_data.new_password_2.value; 
  
  if (email == '' && new_password_1 == '' && new_password_2 == '')
  {
    alert('No data was entered. if you do not want to make any changes here, click another link.');
    return;
  }
  else
  {
    if (new_password_1 != '' || new_password_2 != '')
    {
      if (new_password_1 == new_password_2)
      {
        if (new_password_1.length < 6 || new_password_1.length > 10)
        {
          alert('The new password must be between five and eleven characters in length.');
          return;
        }
        else
        {
          document.admin_login_data.submit();
        }
      }
      else
      {
        alert('The two new-password entries do not agree.');
        return;
      }
    }
    else
    {
      document.admin_login_data.submit();
    } 
  }
}

////////////////////////
// SCENE NAME ENTERED //
////////////////////////
function scene_name_entered()
{
  var scene_name = document.choose_role.scene_name.value;
  scene_name = scene_name.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
  
  if (scene_name == '')
  {
    alert('No scene name was entered.');
    return;
  }
  else
  {
    // Check that the name contains only a-zA-Z1-0.
    var allowed_char = new RegExp('^[0-9A-Za-z\\s]$');
    
    var length_of_scene_name = scene_name.length;
    var array = scene_name.split("");
    
    var error = 0;
    for (var i = 0; i < length_of_scene_name; i++)
    {
      if (allowed_char.test(array[i]))
      {
        // nada
      }
      else
      {
        error = 1;
      }
    }
    
    if (error)
    {
      alert('There are disallowed characters in the scene name.');
      return;
    }
    else
    {
      document.choose_role.submit();
    }
  }
}

/////////////////////
// CHECK FILE NAME //
/////////////////////
function check_file_name()
{
  var document_name = document.upload_side.side_document.value;
  
  if (document_name == '')
  {
    alert('No document was selected.');
    return;
  }
  else
  {
  	
      var allowed_char = new RegExp('^[0-9A-Za-z\:\\\/\ \_\.\\s]$');
      
      var length_of_document_name = document_name.length;
      var array = document_name.split("");
      
      var error = 0;
      /*for (var i = 0; i < length_of_document_name; i++)
      {
      	alert(allowed_char.test(array[i]));
      	if (allowed_char.test(array[i]))
        {
          // nada
        }
        else
        {
          error = 1;
        }
      }
      */
      if (error)
      {
        alert('There are disallowed characters in the name.');
        return;
      }
      else
      {
        var extension = document_name.substring(document_name.length - 3, document_name.length);
        extension = extension.toLowerCase();
    
        if (extension != 'pdf' && extension != 'doc')
        {
          alert('You selected a .' + extension + ' file; please select a .doc or .pdf file instead!');
          return;
        }
        else
        {
          document.upload_side.submit();
        }
      }
    }
    
  }


//////////////////
// SHOW CONFIRM //
//////////////////
function show_confirm()
{
  document.getElementById('confirm').style.display = 'inline';
}

//////////////////
// HIDE CONFIRM //
//////////////////
function hide_confirm()
{
  document.getElementById('sideID').selectedIndex = -1;
  document.getElementById('confirm').style.display = 'none';
}