﻿ITVM=window.ITVM||{};ITVM.UserRegistration=(function($)
{var handlerUrl='/common/template/usercontrols/handlers/UserRegistrationHandler.ashx';var jobSectorSelectBoxId;function filterNatureOfBusinessByJobSector()
{$.getJSON(handlerUrl,{action:'filternatureofbusinessbyjobsector',sector_id:$(this).val()},function(j)
{var options='';for(var i=0;i<j.length;i++)
{options+='<option value="'+j[i].Key+'">'+j[i].Value+'</option>';}
$('#'+jobSectorSelectBoxId).next('div').hide();if(options!=='')
{$('#natureofbusiness').html(options).parent().show();}
else
{$('#natureofbusiness').parent().hide();}
if($('#'+jobSectorSelectBoxId).children('option:selected').text().toLowerCase()=="other")
{$('#'+jobSectorSelectBoxId).next("div").show();}
else
{$('#jobsectorother').val('');}});}
function isEmailUnique(text)
{var r;function setReturn(data)
{r=data.is_email_unique;}
$.ajax({url:handlerUrl,type:'POST',cache:false,global:false,async:false,dataType:'json',success:setReturn,data:{action:'isemailunique',email:text}});return r;}
function sendCommand(options,callback)
{$.ajax({data:options,dataType:'json',success:callback,type:'GET',url:handlerUrl});}
function registerValidation()
{var regForm=document.getElementsByTagName('form')[0];var allowPPCheck=true;function showFeaturesCheck()
{if(allowPPCheck)
{if(typeof ITVM!='undefined'&&typeof ITVM.UI!='undefined'&&typeof ITVM.UI.Dialog!='undefined')
{ITVM.UI.Dialog.show({customClasses:'tools',close:false,title:'Programme Planner and Sponsorship',content:'<p>Are you sure you don\'t want access to the Programme Planner tool and Sponsorship content? If you want to request access, simply go back to the form and opt in.</p>',buttons:[{label:'&lt; Back to form',action:'close',placement:'left'},{label:'No thanks &gt;',action:submitAfterPPCheck,placement:'right'}]});}
else
{if(confirm('Are you sure you don\'t want access to the Programme Planner tool and Sponsorship content?\n\nClick OK to continue if not, or Cancel to go back to the form and opt in.'))
{submitAfterPPCheck();}}}}
function submitAfterPPCheck()
{JHCMS.Validation.disableAll();document.getElementById(ITVM.serverControls.SubmitRegistration).click();}
function anyChecked(params)
{for(var i=0;i<params.fields.length;i++)
{var elm=document.getElementById(params.fields[i]);if(elm&&elm.checked)
{return true;}}
return false;}
JHCMS.Validation.register(ITVM.serverControls.SubmitRegistration,'click',{onSuccess:function(){allowPPCheck=true;}});JHCMS.Validation.register(ITVM.serverControls.SubmitRegistration,'click',{strength:'enforce',isValid:'itv.isEmailUniqueInDatabase',parameters:{field:'emailaddress'},message:'The email address provided is already registered with us. <br/> If you have forgotten your password we can <a href="#login" onclick="$(\'#login-button,.login-surrogate\').click($(\'#login-field-container\').find(\'.forgotten\').click())">send a reminder</a> to your email.',onSuccess:function(){jQuery('#emailaddress').parent('li').removeClass('error');},onFailure:function(){allowPPCheck=false;jQuery('#emailaddress').parent('li').addClass('error');}});JHCMS.Validation.register(ITVM.serverControls.SubmitRegistration,'click',{strength:'enforce',isValid:'selectedOptionHasValue',parameters:{field:jobSectorSelectBoxId},message:'You must select your job sector.',onSuccess:function(){jQuery('#'+jobSectorSelectBoxId).parent('li').removeClass('error');},onFailure:function(){allowPPCheck=false;jQuery('#'+jobSectorSelectBoxId).parent('li').addClass('error');}});JHCMS.Validation.register(ITVM.serverControls.SubmitRegistration,'click',{strength:'enforce',isValid:'isTextFilled',parameters:{field:'firstname'},message:'You must provide your first name.',onSuccess:function(){jQuery('#firstname').parent('li').removeClass('error');},onFailure:function(){allowPPCheck=false;jQuery('#firstname').parent('li').addClass('error');}});JHCMS.Validation.register(ITVM.serverControls.SubmitRegistration,'click',{strength:'enforce',isValid:'isTextFilled',parameters:{field:'surname'},message:'You must provide your surname.',onSuccess:function(){jQuery('#surname').parent('li').removeClass('error');},onFailure:function(){allowPPCheck=false;jQuery('#surname').parent('li').addClass('error');}});JHCMS.Validation.register(ITVM.serverControls.SubmitRegistration,'click',{strength:'enforce',isValid:'isTextFilled',parameters:{field:'jobtitle'},message:'You must provide your job title.',onSuccess:function(){jQuery('#jobtitle').parent('li').removeClass('error');},onFailure:function(){allowPPCheck=false;jQuery('#jobtitle').parent('li').addClass('error');}});JHCMS.Validation.register(ITVM.serverControls.SubmitRegistration,'click',{strength:'enforce',isValid:'isTextFilled',parameters:{field:'companyname'},message:'You must provide your company name.',onSuccess:function(){jQuery('#companyname').parent('li').removeClass('error');},onFailure:function(){allowPPCheck=false;jQuery('#companyname').parent('li').addClass('error');}});JHCMS.Validation.register(ITVM.serverControls.SubmitRegistration,'click',{strength:'enforce',isValid:'isTextFilled',parameters:{field:'companybuilding'},message:'You must provide your company building name/number.',onSuccess:function(){jQuery('#companybuilding').parent('li').removeClass('error');},onFailure:function(){allowPPCheck=false;jQuery('#companybuilding').parent('li').addClass('error');}});JHCMS.Validation.register(ITVM.serverControls.SubmitRegistration,'click',{strength:'enforce',isValid:'isTextFilled',parameters:{field:'companystreet1'},message:'You must provide the first line of the street address.',onSuccess:function(){jQuery('#companystreet1').parent('li').removeClass('error');},onFailure:function(){allowPPCheck=false;jQuery('#companystreet1').parent('li').addClass('error');}});JHCMS.Validation.register(ITVM.serverControls.SubmitRegistration,'click',{strength:'enforce',isValid:'isTextFilled',parameters:{field:'companytown'},message:'You must provide your town.',onSuccess:function(){jQuery('#companytown').parent('li').removeClass('error');},onFailure:function(){allowPPCheck=false;jQuery('#companytown').parent('li').addClass('error');}});JHCMS.Validation.register(ITVM.serverControls.SubmitRegistration,'click',{strength:'enforce',isValid:'isTextFilled',parameters:{field:'companypostcode'},message:'You must provide your postcode.',onSuccess:function(){jQuery('#companypostcode').parent('li').removeClass('error');},onFailure:function(){allowPPCheck=false;jQuery('#companypostcode').parent('li').addClass('error');}});JHCMS.Validation.register(ITVM.serverControls.SubmitRegistration,'click',{strength:'enforce',isValid:'isTextFilled',parameters:{field:'workphone'},message:'You must provide your work telephone number.',onSuccess:function(){jQuery('#workphone').parent('li').removeClass('error');},onFailure:function(){allowPPCheck=false;jQuery('#workphone').parent('li').addClass('error');}});JHCMS.Validation.register(ITVM.serverControls.SubmitRegistration,'click',{strength:'enforce',isValid:'isTextFilled',parameters:{field:'emailaddress'},message:'You must provide your email address.',onSuccess:function(){jQuery('#emailaddress').parent('li').removeClass('error');},onFailure:function(){allowPPCheck=false;jQuery('#emailaddress').parent('li').addClass('error');}});JHCMS.Validation.register(ITVM.serverControls.SubmitRegistration,'click',{strength:'enforce',isValid:'isEmailAddress',parameters:{field:'emailaddress'},message:'The email address you entered is invalid.',onSuccess:function(){jQuery('#emailaddress').parent('li').removeClass('error');},onFailure:function(){allowPPCheck=false;jQuery('#emailaddress').parent('li').addClass('error');}});JHCMS.Validation.register(ITVM.serverControls.SubmitRegistration,'click',{strength:'enforce',isValid:'textValuesMatch',parameters:{field1:'emailaddress',field2:'confirmemailaddress'},message:'You must correctly confirm your email address.',onSuccess:function(){jQuery('#confirmemailaddress').parent('li').removeClass('error');},onFailure:function(){allowPPCheck=false;jQuery('#confirmemailaddress').parent('li').addClass('error');}});JHCMS.Validation.register(ITVM.serverControls.SubmitRegistration,'click',{strength:'enforce',isValid:'isTextFilled',parameters:{field:'password'},message:'You must provide a password.',onSuccess:function(){jQuery('#password').parent('li').removeClass('error');},onFailure:function(){allowPPCheck=false;jQuery('#password').parent('li').addClass('error');}});JHCMS.Validation.register(ITVM.serverControls.SubmitRegistration,'click',{strength:'enforce',isValid:'minimumTextLength',parameters:{field:'password',minLen:6},message:'Your password must be at least 6 characters.',onSuccess:function(){jQuery('#password').parent('li').removeClass('error');},onFailure:function(){allowPPCheck=false;jQuery('#password').parent('li').addClass('error');}});if(document.getElementById('terms'))
{JHCMS.Validation.register(ITVM.serverControls.SubmitRegistration,'click',{strength:'enforce',isValid:'isChecked',parameters:{field:'terms'},message:'You must state that you have read and agree to the terms and conditions.',onSuccess:function(){jQuery('#terms').parent('li').removeClass('error');},onFailure:function(){allowPPCheck=false;jQuery('#terms').parent('li').addClass('error');}});}
JHCMS.Validation.register(ITVM.serverControls.SubmitRegistration,'click',{strength:'enforce',isValid:anyChecked,parameters:{fields:['answeradvanceprogrammeyes','answersponsorshipyes']},message:'Are you sure you do not require Programme Planner or Sponsorship content access?',onFailure:function(){showFeaturesCheck();}});}
function init(args)
{jobSectorSelectBoxId=args.jobSectorSelectBoxId;$('#'+jobSectorSelectBoxId).change(filterNatureOfBusinessByJobSector);$('.registration').find('.empty').removeClass('empty').hide();if(typeof JHCMS!='undefined'&&typeof JHCMS.Validation!='undefined')
{JHCMS.Validation.setCustomMessageHandler(function registrationMessageHandler(messages)
{if(messages.enforce.length)
{var i,messageHtml='<h4>Please check the following problems, update the form, and resubmit your registration:<'+'/h4><ul>';for(i=0;i<messages.enforce.length;i++)
{messageHtml+='<li>'+messages.enforce[i].message+'<'+'/li>';}
messageHtml+='<'+'/ul>';$('#registration-error').html(messageHtml).show();if($.scrollTo)
{$.scrollTo('#registration-error',500);}}
return false;});JHCMS.Validation.extend({generic:{isEmailUniqueInDatabase:function(parameters)
{var text=document.getElementById(parameters.field).value;var r=ITVM.UserRegistration.isEmailUnique(text);return r;}}},'itv');registerValidation();}}
return{init:init,registerValidation:registerValidation,isEmailUnique:isEmailUnique};})(jQuery);
