// #@(#)mj_account_main.js	1.19 12:10:47,09/03/17 (yy/mm/dd)

function create_form_check(theform)
{
    var num_emp_mkt_cd_checked = 0;
    var num_channel_code_checked = 0;

    for (var i = 0; i < theform.elements.length; i++)
    {
      if (theform.elements[i].name == "emp_mkt_cd")
      {
         if (theform.elements[i].type == "checkbox")
         {
            if (theform.elements[i].checked == true)
            {
               num_emp_mkt_cd_checked++;
            }
         }
         else
         {
            num_emp_mkt_cd_checked = 1
         }
      }

      if ( (theform.elements[i].name == "channel_code_atleast") &&
           (theform.elements[i].type == "checkbox") &&
           (theform.elements[i].checked == true) )
      {
	  num_channel_code_checked++;
      }
    } // end loop


    if (theform.firstname.value == "")
    {
      alert("Please enter a value for the \"First Name\" field.");
      theform.firstname.focus();
      return (false);
    }

    if (theform.surname.value == "")
    {
      alert("Please enter a value for the \"Surname\" field.");
      theform.surname.focus();
      return (false);
    }

    if(!valid_email(theform.email))
    {
      return (false);
    }

    if (theform.cc.value == "")
    {
      alert("Please select a Country of Residence.");
      theform.cc.focus();
      return (false);
    }


    if (theform.password.value == "")
    {
       alert("Please enter a value for the \"Password\" field.");
       theform.password.focus();
       return (false);
    }
    else if (theform.password.value.length < 8
                || theform.password.value.length > 21
                || theform.password.value.search(/\d/) == -1
                || theform.password.value.search(/[a-zA-Z]/) == -1
                || theform.password.value.match(/ /))
    {
       alert("The password must be between 8 and 21 characters long, contain at least one letter and one number, and may not contain spaces.");
       theform.password.focus();
       return (false);
    }

    if (theform.password_confirmation.value == "")
    {
       alert("Please enter a value for the \"Confirm Password\" field.");
       theform.password_confirmation.focus();
       return (false);
    }
    else if (theform.password.value != theform.password_confirmation.value)
    {
	alert("Your password and password confirmation do not match");
	theform.password.focus();
	return (false);
    }

    if (num_emp_mkt_cd_checked == 0)
    {
        alert("Please select an industry sector.");
	return false;
    }

    if (num_emp_mkt_cd_checked > 4)
    {
	alert("Please select 4 or fewer industry sectors.");
	return false;
    }    

    // JBE checks.

    var search_to_jbe = theform.search_to_jbe;
    if ( (search_to_jbe.type == 'checkbox') && (search_to_jbe.checked == false)	)
    {
	var inputs = theform.getElementsByTagName("input");
	var input;

	var searchTermEntered = (num_channel_code_checked > 0);
	for (var i = 0; !searchTermEntered && (i < inputs.length); i++)
	{
	    input = inputs[i];
	    if ( input.type != 'hidden' )
	    {
		searchTermEntered = searchTermEntered || ( input.name.match(/^job_title_/) && (input.value != "") );
		searchTermEntered = searchTermEntered || ( input.name.match(/^skill_/) && (input.value != "") );
		searchTermEntered = searchTermEntered || ( input.name.match(/^location_\w\wclude$/) && (input.value != "") );
	    }
	}

	if ( searchTermEntered )
	{
	    if ( confirm("You have entered some Jobs-by-Email Search Terms but have not ticked the check box opting into this service.  Select 'OK' to confirm that you want to receive Jobs by Email, or 'Cancel' to return to the registration page.") )
	    {
		theform.search_to_jbe.checked = true;
	    }
	    else
	    {
		return false;
	    }
	} 
    }

    return true;
}

function create_form_step_1_check(theform)
{
	var num_emp_mkt_cd_checked = 0;

	for (var i = 0; i < theform.elements.length; i++)
	{
		if (theform.elements[i].name == "emp_mkt_cd")
		{
			if (theform.elements[i].type == "checkbox")
			{
				if (theform.elements[i].checked == true)
				{
					num_emp_mkt_cd_checked++;
				}
			}
			else
			{
				num_emp_mkt_cd_checked = 1
			}
		}
	} // end loop


	if (theform.firstname.value == "")
	{
		alert("Please enter a value for the \"First Name\" field.");
		theform.firstname.focus();
		return (false);
	}

	if (theform.surname.value == "")
	{
		alert("Please enter a value for the \"Surname\" field.");
		theform.surname.focus();	
		return (false);
	}

	if(!valid_email(theform.email))
	{
		return (false);
	}

	if (theform.cc.value == "")
	{
		alert("Please select a Country of Residence.");
		theform.cc.focus();
		return (false);
	}

	if (theform.county && theform.county.disabled == false && theform.county.value == "" )
	{
		alert("Please select a County.");
		theform.county.focus();
		return (false);
	}

	if (theform.password.value == "")
	{
		alert("Please enter a value for the \"Password\" field.");
		theform.password.focus();
		return (false);
	}
        else if (theform.password.value.length < 8
                        || theform.password.value.length > 21
                        || theform.password.value.search(/\d/) == -1
                        || theform.password.value.search(/[a-zA-Z]/) == -1
                        || theform.password.value.match(/ /))
        {
                alert("The password must be between 8 and 21 characters long, contain at least one letter and one number, and may not contain spaces.");
                theform.password.focus();
                return (false);
        }

	if (theform.password_confirmation.value == "")
	{
		alert("Please enter a value for the \"Confirm Password\" field.");
		theform.password_confirmation.focus();
		return (false);
	}
	else if (theform.password.value != theform.password_confirmation.value)
	{
		alert("Your password and password confirmation do not match");
		theform.password.focus();
		return (false);
	}

	if (num_emp_mkt_cd_checked == 0)
	{
		alert("Please select an industry sector.");
		return false;
	}

	if (num_emp_mkt_cd_checked > 4)
	{
		alert("Please select 4 or fewer industry sectors.");
		return false;
	}

	return true;
}

function create_form_step_2_check(theform)
{
	var num_channel_code_checked = 0;

	for (var i = 0; i < theform.elements.length; i++)
	{
		if ( (theform.elements[i].name == "channel_code_atleast") &&
			(theform.elements[i].type == "checkbox") &&
			(theform.elements[i].checked == true) )
		{
			num_channel_code_checked++;
		}
	} // end loop

	var inputs = theform.getElementsByTagName("input");
	var input;

	var searchTermEntered = (num_channel_code_checked > 0);
	for (var i = 0; !searchTermEntered && (i < inputs.length); i++)
	{
		input = inputs[i];
		if ( input.type != 'hidden' )
		{
			searchTermEntered = searchTermEntered || ( input.name.match(/^job_title_/) && (input.value != "") );
			searchTermEntered = searchTermEntered || ( input.name.match(/^skill_/) && (input.value != "") );
			searchTermEntered = searchTermEntered || ( input.name.match(/^location_\w\wclude$/) && (input.value != "") );
		}
	}

   // JBE checks.
   var search_to_jbe = theform.search_to_jbe;

	if ( searchTermEntered && ( search_to_jbe.type == 'checkbox' && search_to_jbe.checked == false ) )
	{
		if ( confirm("You have entered some Jobs-by-Email Search Terms but have not ticked the check box opting into this service.  Select 'OK' to confirm that you want to receive Jobs by Email, or 'Cancel' to return to the registration page.") )
		{
			theform.search_to_jbe.checked = true;
		}
		else
		{
			return false;
		}
	}
	else if ( ! searchTermEntered && (  search_to_jbe.type == 'checkbox' && search_to_jbe.checked == true ) )
	{
		if ( ! confirm("Are you sure you want to create a Jobs-by-Email alert without specifying any search terms? This will provide a very wide range of jobs. If you wish to improve the quality of your JBE, click 'Cancel', fill in the 'Job Title', 'Skills' and 'Location' fields, and resubmit the form.") )
		{
			return false;
		}
	}

	return true;
}

function set_focus()
{
    document.create_form.firstname.focus();
}
 
function OpenMonitor(name)
{
    nname=navigator.userAgent;
    features = "toolbar=1,location=0,directories=0,status=0,menubar=1,scrollbars=1,copyhistory=0,width=795,height=500,top=20,left=0,resizable=1";
    nversion=navigator.appVersion.substring(0,1);
    if ((nname.lastIndexOf("MSIE")==-1) || (nversion!=4))
    {
	    myWindow = window.open(name,"my_jobsite_sub_window",features);
	    myWindow.focus();
    }
    else // IE 4 (but not 3 or 5) throws a security exception
    {    // when focusing on a window on another domain
	 //
	    myWindow = window.open(name,"my_jobsite_sub_window",features);
    }
} 

function uncheck_all_jobtypes(the_form)
{
    var jobtype_total = the_form.jobtype.length;

    for (i = 0; i < jobtype_total; i++)
    {
	if (the_form.jobtype[i].value != "E")
        {
	    the_form.jobtype[i].checked = false;
	}
    }
}

function uncheck_jobtype_all(the_form)
{
    var jobtype_total = the_form.jobtype.length;

    for (i = 0; i < jobtype_total; i++)
    {
	if (the_form.jobtype[i].value == "E")
        {
	    the_form.jobtype[i].checked = false;
        }
    }
}
if(typeof jQuery == "function"){
	$(document).ready(function(){	
		$("#upload_cv").click(function () {
			n = $('#upload_cv:checked').length;
			if (n) {
				$("#garlik").fadeTo("fast","1");
				$("#garlik_opt_in").attr("disabled","");
			}
			else{
				$("#garlik").fadeTo("fast","0.40");
				$("#garlik_opt_in").attr("disabled","disabled").attr("checked","");
			}
		});	
	});	
}
