// Defining variables to make sure that each addition to the form has a number for the array.
var secondary_qualificationid=1;
var undergraduate_qualificationid=1;
var postgraduate_qualificationid=1;
var previous_addressid=1;
var previous_jobid=1;
var d=new Date();

// This function adds another secondary qualification to the form dynamically.
function add_another_secondary_qualification() {
	secondary_qualificationid++;
	$('#q-add-another-secondary-qualification').before('<div class="form-line-item">\n<label for="se_qual_' + secondary_qualificationid + '">Level:</label>\n<select name="se_qual[' + secondary_qualificationid + '][level]" class="dob"><option value="">Please choose...</option><option value="Higher">Higher</option><option value="A-Level">A-Level</option><option value="AS-Level">AS-Level</option><option value="Int Bac">Int Bac</option><option value="Other">Other</option></select>\n<label for="se_qual_' + secondary_qualificationid + '">Subject / Grade:</label><input name="se_qual[' + secondary_qualificationid + '][subject]" class="five-text" type="text" /> / <input name="se_qual[' + secondary_qualificationid + '][grade]" class="two-text" type="text" />\n</div>');
};

// This function adds another undergraduate qualification to the form dynamically.
function add_another_undergraduate_qualification() {
	undergraduate_qualificationid++;
	$('#q-add-another-undergraduate-qualification').before('<div class="form-line-item">\n<label for="ug_qual_' + undergraduate_qualificationid + '">Level:</label>\n<select name="ug_qual[' + undergraduate_qualificationid + '][level]" class="dob"><option value="">Please choose...</option><option value="BA">BA</option><option value="BSc">BSc</option><option value="LLB">LLB</option><option value="MA">MA</option><option value="MSc">MSc</option><option value="LLM">LLM</option><option value="GDL">GDL</option></select></select>\n<label for="ug_qual_' + undergraduate_qualificationid + '">Subject / Grade:</label><input name="ug_qual[' + undergraduate_qualificationid + '][subject]" class="five-text" type="text" /> / <input name="ug_qual[' + undergraduate_qualificationid + '][grade]" class="two-text" type="text" />\n</div>');
};

// This function adds another postgraduate qualification to the form dynamically.
function add_another_postgraduate_qualification() {
	postgraduate_qualificationid++;
	$('#q-add-another-postgraduate-qualification').before('<div class="form-line-item">\n<label for="pg_qual_' + postgraduate_qualificationid + '">Level:</label>\n<select name="pg_qual[' + postgraduate_qualificationid + '][level]" class="dob"><option value="">Please choose...</option><option value="PGDL">PGDL</option><option value="GDL">GDL</option><option value="LPC">LPC</option><option value="CPE">CPE</option><option value="BVC">BVC</option><option value="BPTC">BPTC</option></select>\n<label for="pg_qual_' + postgraduate_qualificationid + '">Grade:</label><input name="pg_qual[' + postgraduate_qualificationid + '][grade]" class="two-text" type="text" />\n</div>');
};

// This function adds another address to the form dynamically.
function add_previous_address() {
	$('#q-add-previous-address').before('<div class="form-line-item heading"><h2>Previous Address - '+ previous_addressid +'</h2></div><div class="form-line-item">\n<label for="contact-house-name-number" class="required">House Name / Number:</label>\n<input name="pastaddress['+ previous_addressid +'][house_name_number]" class="wide-text required" type="text" />\n</div>\n<div class="form-line-item">\n<label for="contact-address-one" class="required">Address:</label>\n<input name="pastaddress['+ previous_addressid +'][address_1]" class="wide-text required" type="text" />\n<label for="contact-address-two"></label>\n<input name="pastaddress['+ previous_addressid +'][address_2]" class="wide-text" type="text" />\n<label for="contact-address-three"></label>\n<input name="pastaddress['+ previous_addressid +'][address_3]" class="wide-text" type="text" />\n</div>\n<div class="form-line-item">\n<label for="contact-city" class="required">City:</label>\n<input name="pastaddress['+ previous_addressid +'][city]" class="wide-text required" type="text" />\n</div>\n<div class="form-line-item">\n<label for="contact-county" class="required">County:</label>\n<input name="pastaddress['+ previous_addressid +'][county]" class="wide-text required" type="text" />\n</div>\n<div class="form-line-item">\n<label for="contact-country">Country:</label>\n<input name="pastaddress['+ previous_addressid +'][country]" class="wide-text" type="text" />\n</div>\n<div class="form-line-item">\n<label for="contact-postcode" class="required">Postcode:</label>\n<input name="pastaddress['+ previous_addressid +'][postcode]" class="wide-text required" type="text" />\n</div>');
	$('#add-address-message').remove();
	previous_addressid++;
  ("#whathelp").rules("add", "required");
};

// This function adds another works experience to the form dynamically.
function add_previous_experience() {
	$('#q-add-another-job').before('<div class="form-line-item heading"><h2>Previous Employment - '+ previous_jobid +'</h2></div><div class="form-line-item"><label for="prev_company">Company: <span class="deep-red">*</span></label><input name="prev['+ previous_jobid +'][company]" id="prev_company" class="wide-text required" type="text" /></div>\n<div class="form-line-item"><label for="prev_manager">Your manager\'s name: <span class="deep-red">*</span></label><input name="prev['+ previous_jobid +'][manager]" id="prev_manager" class="wide-text required" type="text" /></div>\n<div class="form-line-item"><label for="prev_role">Role: <span class="deep-red">*</span></label><textarea name="prev['+ previous_jobid +'][role]" class="wide-text-area required" rows="" cols="" ></textarea></div>\n<div class="form-line-item"><label for="prev_dates">Dates: <span class="deep-red">*</span></label><input name="prev['+ previous_jobid +'][from_month]" id="prev_from_month" class="shortest-text required" min="01" max="12" type="text" />/<input name="prev['+ previous_jobid +'][from_year]" id="prev_from_year" class="short-text required" minlength="4" min="1900" type="text" /> to <input name="prev['+ previous_jobid +'][to_month]" id="prev_to_month"  min="01" max="12" class="shortest-text" type="text" />/<input name="prev['+ previous_jobid +'][to_year]" id="prev_to_year" class="short-text required" minlength="4" min="1900" type="text" /> <span class="descriptive">mm/yyyy</span></div>\n<div class="form-line-item"><label for="prev_reason">Reason for leaving: <span class="deep-red">*</span></label><input name="prev['+ previous_jobid +'][reason]" id="prev_reason" class="wide-text required" type="text" /></div>\n<div class="form-line-item"><label for="previous-employment-reference">Contact For References: <span class="deep-red">*</span></label><span class="radio-option"><label><input name="prev['+ previous_jobid +'][reference]" id="prev_reference" type="radio" value="1" id="previous-employment-reference-yes" checked="checked" />Yes</label></span><span class="radio-option"><label><input name="prev['+ previous_jobid +'][reference]" id="prev_reference" type="radio" value="0" id="previous-employment-reference-no" />No</label></span></div>');
	$('#add-job-message').remove();
	previous_jobid++;
};

			
// This function counts the number of words in a textarea.
function wordCount(fld) {
	var msgStr = "You may only have 350 words in your personal statement";
	var words = fld.value.split (/[\s{,}]/);
	$('#wordCount').html('350 Words Maximum (' + words.length + ' used)');
	if (words.length >= 350) {alert (msgStr);}
};

$(document).ready(function()
{
  // Open all A tags with "rel=external" to open in a new page
  $("a[rel='external']").click(function()
  {
    window.open(this.href);
    return false;
  });

  // Sets the main page slideshow to open the signup link
  $("#slideshow").click(function() {
    window.location = "free-sign-up";
  });

  // Show/hide the login form
  $('.paralegal-login-button a').click(function() {
    $('.paralegal-login-form').slideToggle();
  });

  // Sets up the slideshow DIV for the homepage
  $('#slideshow').cycle({
      timeout: 8000
  });

  // Calls the respective function from above for dynamically adding another qualification
  $('#add-another-secondary-qualification').click(add_another_secondary_qualification);
  $('#add-another-undergraduate-qualification').click(add_another_undergraduate_qualification);
  $('#add-another-postgraduate-qualification').click(add_another_postgraduate_qualification);
  $('#add-previous-address').click(add_previous_address);
  $('#add-another-job').click(add_previous_experience);

  // Sets the form wizard up for use on the sign up form
  if($("#ilit-paralegal-signup").length > 0 ) 
  {
  
    $("#ilit-paralegal-signup").formwizard(
    {
      historyEnabled: true,
      afterNext: stepNext,
      afterBack: stepNext,
      formPluginEnabled: false,
      focusFirstInput: true,
      validationEnabled: true
    },
    {
      errorElement: "div",
      rules:
      {
        first_name:     {minlength: 2},
        family_name:    {minlength: 2},
        dob_day:        {min: 01, max: 31},
        dob_month:      {min: 01, max: 12},
        dob_year:       {min: 1900, max: d.getFullYear()},
        se_from_year:   {minlength: 4, min: 1900, max: d.getFullYear()},
        se_to_year:     {minlength: 4, min: $('#se_from_year').val(), max: d.getFullYear()}

      }
    });

  };
  
  $('#se_from_year').keyup(function() {
    $('#se_to_year').validate();
  });

  
  // Validate the contact form
  if($("#ilit-contact-form").length > 0 ) {
    $("#ilit-contact-form").validate();
  };
  
  if($("#ilit-profile-edit").length > 0 ) {
    $("#ilit-profile-edit").validate();
  }
  
  if($("#add-exp").length > 0 ) {
    //$("#add-exp").validate();
  }

});

function stepNext(data) {
	$('#side-'+data.previousStep).removeClass('current-node');
	$('#side-'+data.currentStep).addClass('current-node');
};
