///////////////////////////////////////////////
//                                           //
// THIS MUST BE CHANGED IF THE SITE IS MOVED //
//                                           //
///////////////////////////////////////////////

var base_URL = "http://www.myacttv.com/cake";


//////////////////
// 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');

  if (country == 'USA')
  {
    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')
  {
    province_row.style.display = 'table-row';
    postal_row.style.display = 'table-row';
    state_row.style.display = 'none';
    zip_row.style.display = 'none';
  }
  else
  {
    state_row.style.display = 'none';
    zip_row.style.display = 'none';
    province_row.style.display = 'none';
    postal_row.style.display = 'none';
  }
  
  return true;  
}


//////////////////
// greater than //
//////////////////
function greaterThan()
{
  var birthday_row = document.getElementById('birthday_row');
  var birthday_status = document.getElementById('greaterThan18').value;

  if (birthday_status == 'N')
  {
    birthday_row.style.display = 'table-row';
    return true;
  }
  else
  {
    birthday_row.style.display = 'none';
  }
  return true;
}


//////////////
// validate //
//////////////
function validate(user_type)
{
  var first_name = document.forms["register"].elements["first_name"].value;
  // middle name is not required
  var last_name = document.forms["register"].elements["last_name"].value;
  // gender is a pulldown
  // greaterThan18 is a pulldown
  var password = document.forms["register"].elements["password"].value;
  var regexPW = new RegExp("[0-9A-Za-z]{6,10}");
  var email = document.forms["register"].elements["email"].value;
  var confirm = document.forms["register"].elements["confirm"].value;
  var city = document.forms["register"].elements["city"].value;
  var country = document.forms["register"].elements["country"].value;
  // state is a pulldown
  // province is a pulldown
  var accept = document.forms["register"].elements["accept"].value;
  // see below for zip and postal code testing
  // notify is a pulldown
  // inform is a pulldown
  
  if (first_name == '')
  {
    alert('Please enter your first name.');
    return false;
  }

  if (last_name == '')
  {
    alert('Please enter your last name.');
    return false;
  }
  
  if (password == '' || password.length < 6)
  {
    alert('Please enter an 6 to 10 character password, \nnumbers and letters only.');
    return false;
  }
  
  if (!regexPW.test(password))
  {
    alert('Please enter a password composed of\n6 to 10 letters and numbers only.');
    return false;
  } 
  
  if (email == '')
  {
    alert('Please enter your e-mail address.');
    return false;
  }
  
  if (confirm == '')
  {
    alert('Please enter your e-mail address in the \"CONFIRM EMAIL\" box.');
    return false;
  }
  
	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)
	{
	  alert(error_message);
	  return false;
	}
	
	if (location_of_dot == -1 || location_of_dot == 0 || location_of_dot == length_of_address - 1)
	{
	  alert(error_message);
	  return false;
	}
	
  if (email.indexOf(at,(location_of_at + 1)) != -1)
  {
    alert(error_message);
    return false;
  }
  
  if (email.substring(location_of_at - 1, location_of_at) == dot || email.substring(location_of_at + 1, location_of_at + 2) == dot)
  {
    alert(error_message);
    return false;
  }
  
  if (email.indexOf(dot,(location_of_at + 2)) == -1)
  {
    alert(error_message);
    return false;
  }
  
  if (email.indexOf(" ") != -1)
  {
    alert(error_message);
    return false;
  }
  
  if (email != confirm)
  {
    alert('The e-mail address and the confirm e-mail address must agree.');
    return false;
  } 
  
  if (city == '')
  {
    alert(' Please enter the city.');
    return false;
  }
  
  if (country == '0')
  {
      alert(' Please select a country.');
      return false;
  }
  else if (country == 'USA')
  {
    // state is a pulldown
    var zip = document.forms["register"].elements["zip"].value;
    var regexZIP = new RegExp("[0-9]{5,5}");

    if (zip == '' || !regexZIP.test(zip))
    {
      alert('Please enter a 5 digit zip code.');
      return false;
    }
  }
  else if (country == 'CA')
  {
    // province is a pulldown
    var postal = document.forms["register"].elements["postal"].value;

    if (postal == '')
    {
    alert(' Please enter a postal code.');
    return false;
    }
  }
  else
  {
    // nada
  }
  
  if (user_type == 'I')
  {
    var address = document.forms["register"].elements["address"].value;
    var primary = document.forms["register"].elements["primary_tele"].value;
    var regexTP = new RegExp("[0-9]{10,10}");
    
    if (address == '')
    {
      alert('Please enter your street address.');
      return false;
    }
    
    if (primary == '')
    {
      alert('Please enter your primary business telephone number.');
      return false;
    }

    if (!regexTP.test(primary))
    {
      alert('Please enter a primary telephone number \ncomposed of 10 numbers only.');
      return false;
    }
  }
  
  if (accept == 'N')
  {
    alert('Acceptance of the Terms of Use is required to become registered.');
    return false;
  }
  
  //alert('OK');
  document.forms['register'].submit();
}


//////////////////////
// check login data //
//////////////////////
function check_login_data()
{
  var email    = document.login.email.value;
  var password = document.login.password.value;
  
  if (email == '' || password == '')
  {
    alert('Please enter both your e-mail address and your password');
    return false;
  }
  else
  {
  	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)
  	{
  	  alert(error_message);
  	  return false;
  	}
  	
  	if (location_of_dot == -1 || location_of_dot == 0 || location_of_dot == length_of_address - 1)
  	{
  	  alert(error_message);
  	  return false;
  	}
  	
    if (email.indexOf(at,(location_of_at + 1)) != -1)
    {
      alert(error_message);
      return false;
    }
    
    if (email.substring(location_of_at - 1, location_of_at) == dot || email.substring(location_of_at + 1, location_of_at + 2) == dot)
    {
      alert(error_message);
      return false;
    }
    
    if (email.indexOf(dot,(location_of_at + 2)) == -1)
    {
      alert(error_message);
      return false;
    }
    
    if (email.indexOf(" ") != -1)
    {
      alert(error_message);
      return false;
    }
    else
    {
      document.login.submit();
    }
  }
}


/////////////////
// submit form //
/////////////////
function submit_form(sequence, direction)
{
  var form_ID = sequence + '' + direction;
  var form_to_be_submitted = document.getElementById(form_ID);
  form_to_be_submitted.submit();
}


///////////////////////////
// check edit credit data//
///////////////////////////
function check_credit_edit_data(theForm, which)
{
  if (which == 'O')
  {
    if (theForm.production_name.value == '' || theForm.role.value == '' || theForm.production_co.value == '')
    {
      alert('Please enter data in at least one field.');
      return;
    }
    else
    {
      theForm.submit();
    }
  }
  else
  {
    if (theForm.type.value == '' || theForm.school.value == '' || theForm.instructor.value == '')
    {
      alert('Please enter data in at least one field.');
      return;
    }
    else
    {
      theForm.submit();
    }
  }
}


/////////////////////////
// validate new credit //
/////////////////////////
function validate_new_credit(theForm, which)
{
  // training
  //   which 'T'
  //   theForm.type
  //   theForm.school
  //   theForm.instructor
  
  // all others
  //   which 'O'
  //   theForm.production_name
  //   theForm.role
  //   theForm.production_co
  
  if (which == 'T')
  {
    if (theForm.type.value == '' && theForm.school.value == '' && theForm.instructor.value == '')
    {
      alert('Please enter data in at least one data-entry box.');
      return;
    }
    else
    {
      theForm.submit();
    }
  }
  else
  {
    if (theForm.production_name.value == '' && theForm.role.value == '' && theForm.production_co.value == '')
    {
      alert('Please enter data in at least one data-entry box.');
      return;
    }
    else
    {
      theForm.submit();
    }
  }
}


/////////////////////
// check age range //
/////////////////////
function check_age_range()
{
  var low  = parseInt(document.about_me.age_range_low.value);
  var high = parseInt(document.about_me.age_range_high.value);

  if (low > high)
  {
    alert('The age range choices are reversed.');
    return;
  }
  else
  {
    document.about_me.submit();
  }
}


///////////////////////////
// check representations //
///////////////////////////
function check_representation()
{
  var agent_1 = document.getElementById("agent_1").value;
  var agent_2 = document.getElementById("agent_2").value;
  var agent_3 = document.getElementById("agent_3").value;
  var manager = document.getElementById("manager").value;

  if (agent_1 == '' && agent_2 == '' && agent_3 == '' && manager == '')
  {
    alert('No data entered.');
    return;
  }
  else
  {
    document.representations.submit();
  }
}


//////////////////
// SELECT PHOTO //
//////////////////
function select_photo()
{
  window.location = base_URL + "/talent/select_photo";
}


////////////////
// RESEQUENCE //
////////////////
function resequence(recordID, num_rows)
{
  var id = 'id' + recordID;
  var new_sequence_number = document.getElementById(id).value;
  
  if ((new_sequence_number < 0) || (new_sequence_number > num_rows))
  {
    alert('The sequence number must be between 1 and ' + num_rows + '.');
    return;
  }
  else
  {
    window.location = base_URL + "/talent/resequence/"  + recordID + "/" + new_sequence_number;
  }
}


///////////////
// CHECK URL //
///////////////
function check_url()
{
  var regexp = new RegExp("[^a-zA-Z]");
  var talent = document.url_entry.url.value; 
  
  if (regexp.test(talent))
  {
    alert('Letters only, please.');
    return;
  }
  else
  {
    document.url_entry.submit();
  }
}

//////////////////////////////
// CHECK MY ACCOUNT CONTACT //
//////////////////////////////
function check_my_account_contact()
{
  var address             = document.my_account_contact.address.value; 
  var city                = document.my_account_contact.city.value; 
  var zip                 = document.my_account_contact.zip.value;
  var primary_telephone   = document.my_account_contact.primary_tele.value; 
  var secondary_telephone = document.my_account_contact.secondary_tele.value; 
  var alternative_email   = document.my_account_contact.alt_email.value; 
  
  if (address == '' && city == '' && zip == '' && primary_telephone == '' && secondary_telephone == '' && alternative_email == '')
  {
    alert('No data was entered.');
    return;
  }
  else
  {
    document.my_account_contact.submit();
  }
}

////////////////////////////
// CHECK MY ACCOUNT LOGIN //
////////////////////////////
function check_my_account_login()
{
  var email          = document.my_account_login.email.value; 
  var new_password_1 = document.my_account_login.new_password_1.value; 
  var new_password_2 = document.my_account_login.new_password_2.value; 
  
  if (email == '' && new_password_1 == '' && new_password_2 == '')
  {
    alert('No data was entered.');
    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.my_account_login.submit();
        }
      }
      else
      {
        alert('The two new-password entries do not agree.');
        return;
      }
    }
    else
    {
      document.my_account_login.submit();
    } 
  }
}


