﻿var emailValid;
var passwordValid
var passwordConfirmed;
var firstnameValid;
var lastnameValid;
var jobTitleValid;
var locValid;
var termsValid;
var euRegistered = false;

function ForceLogon()
{
    $get('pnlRegister').style.display = 'none';
    $get('dvTitle').innerHTML = 'Login';
    //$get('termsAndConditions').style.display = 'none';
    $get('dvMin').style.display = 'none'
    $get('dvForgottonEmail').style.display = 'block';
}

function ShowLogon()
{
    //var regex = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
    //var regex = /^[\w\.-]*[a-zA-Z0-9_]@[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
    var regex = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    //var regex = /^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$/;
    
    var controltovalidate = $get('ctl00_ContentPlaceHolder1_jobseekermember1_txtEmail')
        
    if(regex.test(controltovalidate.value))
    {
        //get('imgGo').style.display = 'none';
        controltovalidate.style.border = '1px solid #71D455';
        //$get('imgGo').src = "../images/tick-green.gif"
        $get('ctl00_ContentPlaceHolder1_jobseekermember1_EmailValidationSuccess').style.display = 'block';
        $get('EmailValidationFail').style.display = 'none';
        emailValid = true;
        
        var ajaxResult = JobsGroup.ValidateControls.CheckEmailExists(controltovalidate.value, EmailHandled);
    }
    else if (controltovalidate.value == "")
    {
        controltovalidate.style.border = '1px solid red';
        $get('ctl00_ContentPlaceHolder1_jobseekermember1_EmailValidationSuccess').style.display = 'none';
        $get('EmailValidationFail').style.display = 'block';
        $get('EmailFailLabel').innerHTML = "Please enter your email address";
        emailValid = false;
    }
    else
    {
        controltovalidate.style.border = '1px solid red';
        $get('ctl00_ContentPlaceHolder1_jobseekermember1_EmailValidationSuccess').style.display = 'none';
        $get('EmailValidationFail').style.display = 'block';
        $get('EmailFailLabel').innerHTML = "Please enter a valid email address";
        emailValid = false;
    }
}

function EmailHandled(result)
{
    var panel = $get('pnlRegister')
    //var terms = $get('termsAndConditions')
    $get('dvUnknownEmail').style.display = 'none';
    
    $get('pnlAlreadyReg').style.display = 'none';
    
    if(result == true)
    {
        panel.style.display = 'none';
        //terms.style.display = 'none';
        $get('dvTitle').innerHTML = 'Login';
        var ajaxResult = JobsGroup.ValidateControls.CheckIfNew(NewHandler);
        $get('dvMin').style.display = 'none'
        $get('dvForgottonEmail').style.display = 'block';
    }
    else
    {
        if($get('dvTitle').innerHTML == 'Login')
        {
            $get('dvUnknownEmail').style.display = 'block';
        }
        panel.style.display = 'block';
        //terms.style.display = 'block';
        $get('dvTitle').innerHTML = 'Register';
        $get('dvMin').style.display = 'block'
        $get('dvForgottonEmail').style.display = 'none';
    }
}

function NewHandler(result)
{
    var panelReg = $get('pnlAlreadyReg');
    
    if (result == true)
    {
        panelReg.style.display = 'block';
    }
    else
    {
        panelReg.style.display = 'none';
    }
}

function PasswordCheck()
{
    var controltovalidate = $get('ctl00_ContentPlaceHolder1_jobseekermember1_txtPassword');
    // var password = $get('ctl00_ContentPlaceHolder1_jobseekermember1_txtPassword');
    var success = $get('PasswordSuccess');
    var fail = $get('PasswordFail');
    var failLabel = $get('PasswordFailLabel');
    
    if(controltovalidate.value.indexOf(" ") != -1)
    {
        controltovalidate.style.border = '1px solid red';
        success.style.display = 'none';
        fail.style.display = 'block';
        failLabel.innerHTML = "Your password cannot contain spaces";
        passwordValid = false;
        ShowLogon();
    }
    else if(controltovalidate.value == '')
    {
        if($get('pnlRegister').style.display = 'block')
        {
            controltovalidate.style.border = '1px solid red';
            success.style.display = 'none';
            fail.style.display = 'block';
            failLabel.innerHTML = "Please enter your password";
            passwordValid = false;
            ShowLogon();
        }
        else
        {
            passwordValid = true;
        }
    }
    else
    {
        if($get('pnlRegister').style.display == 'block')
        {
            if (controltovalidate.value.length >= 8)
            {
                controltovalidate.style.border = '1px solid #71D455';
                success.style.display = 'block';
                fail.style.display = 'none';
                passwordValid = true;
            }
            else
            {
                controltovalidate.style.border = '1px solid red';
                success.style.display = 'none';
                fail.style.display = 'block';
                failLabel.innerHTML = "Your password is less than 8 characters";
                passwordValid = false;
            }
        }
        else if (controltovalidate.value.length < 4)
        {
            controltovalidate.style.border = '1px solid red';
            success.style.display = 'none';
            fail.style.display = 'block';
            failLabel.innerHTML = "Your password is too short";
            passwordValid = false;
        }
        else
        {
            controltovalidate.style.border = '1px solid #71D455';
            success.style.display = 'block';
            fail.style.display = 'none';
            passwordValid = true;
        } 
    }
}

function ConfirmPassword()
{
    var controlpassword = $get('ctl00_ContentPlaceHolder1_jobseekermember1_txtPassword');
    var controltovalidate = $get('ctl00_ContentPlaceHolder1_jobseekermember1_txtConfirmPassword');
    var success = $get('PasswordConfirmSuccess');
    var fail = $get('PasswordConfimFail');
    var failLabel = $get('PasswordConfimFailLabel');
    
    if(controltovalidate.value == controlpassword.value)
    {
            if(controltovalidate.value == "")
            {
                controltovalidate.style.border = '1px solid red';
                success.style.display = 'none';
                fail.style.display = 'block';
                failLabel.innerHTML = "Please confirm your password";
                passwordConfirmed = false;
            }
            else
            { 
                controltovalidate.style.border = '1px solid #71D455';
                success.style.display = 'block';
                fail.style.display = 'none';
                passwordConfirmed = true;                
            }
    }
    else
    {
            controltovalidate.style.border = '1px solid red';
            success.style.display = 'none';
            fail.style.display = 'block';
            failLabel.innerHTML = "Your passwords do not match";
            passwordConfirmed = false;
    }
}

function ConfirmFirstname()
{
    var controltovalidate = $get('ctl00_ContentPlaceHolder1_jobseekermember1_txtFirstName');
    var success = $get('FirstnameSuccess');
    var fail = $get('FirstnameFail');
    var failLabel = $get('FirstnameFailLabel');
    
    if(controltovalidate.value == '')
    {
        controltovalidate.style.border = '1px solid red';
        success.style.display = 'none';
        fail.style.display = 'block';
        failLabel.innerHTML = "Please enter your firstname";
        firstnameValid = false;
    }
    else
    {
        controltovalidate.style.border = '1px solid #71D455';
        success.style.display = 'block';
        fail.style.display = 'none';
        firstnameValid = true;
    }
}

function ConfirmLastname()
{
    var controltovalidate = $get('ctl00_ContentPlaceHolder1_jobseekermember1_txtLastName');
    var success = $get('LastnameSuccess');
    var fail = $get('LastnameFail');
    var failLabel = $get('LastnameFailLabel');
    
    if(controltovalidate.value == '')
    {
        controltovalidate.style.border = '1px solid red';
        success.style.display = 'none';
        fail.style.display = 'block';
        failLabel.innerHTML = "Please enter your lastname";
        lastnameValid = false;
    }
    else
    {
        controltovalidate.style.border = '1px solid #71D455';
        success.style.display = 'block';
        fail.style.display = 'none';
        lastnameValid = true;
    }
}

function ConfirmJobTitle()
{
    var controltovalidate = $get('ctl00_ContentPlaceHolder1_jobseekermember1_jbeReg1_txtJobTitle');
    var success = $get('JobTitleSuccess');
    var fail = $get('JobTitleFail');
    var failLabel = $get('JobTitleFailLabel');
    
    if(controltovalidate.value == '')
    {
        controltovalidate.style.border = '1px solid red';
        success.style.display = 'none';
        fail.style.display = 'block';
        failLabel.innerHTML = "Please enter a job title";
        jobTitleValid = false;
    }
    else
    {
        controltovalidate.style.border = '1px solid #71D455';
        success.style.display = 'block';
        fail.style.display = 'none';
        jobTitleValid = true;
    }
}

function ConfirmLocation()
{
    var controltovalidate = $get('ctl00_ContentPlaceHolder1_jobseekermember1_jbeReg1_locationTextbox1_txtLocation');
    var success = $get('LocSuccess');
    var fail = $get('LocFail');
    var failLabel = $get('LocFailLabel');
    
    if(controltovalidate.value == '' || controltovalidate.value.indexOf("E.g.") != -1)
    {
        controltovalidate.style.border = '1px solid red';
        success.style.display = 'none';
        fail.style.display = 'block';
        failLabel.innerHTML = "Please enter a location";
        locValid = false;
    }
    else
    {
        controltovalidate.style.border = '1px solid #71D455';
        success.style.display = 'block';
        fail.style.display = 'none';
        locValid = true;
    }
}

function SelectEU(control)
{
    $get('ctl00_ContentPlaceHolder1_jobseekermember1_rdoUK').checked = false;
    $get('ctl00_ContentPlaceHolder1_jobseekermember1_rdoNonUK').checked = false;

    var success = $get('EUAuthSuccess');
    var fail = $get('EUAuthFail');
    //var failLabel = $get('EUAuthFailLabel');
    
    $get(control).checked = true;
        
    //controltovalidate.style.border = '1px solid #71D455';
    success.style.display = 'block';
    fail.style.display = 'none';
    euRegistered = true;
}

function ConfirmTermsAndConditions()
{
    var checkbox = $get('ctl00_ContentPlaceHolder1_jobseekermember1_chkTermsConditions')
    var failLabel = $get('dvChkError')
    
    if(checkbox.checked == false)
    {
        failLabel.innerHTML = '<div style="color:red">Please accept terms and conditions</div>';
        termsValid = false;
    }
    else
    {
        termsValid = true;
    }
}

function ConfirmEUAuth()
{
    var okBox = $get('EUAuthSuccess')
    var failBox= $get('EUAuthFail')
    
    if(euRegistered == false)
    {
        okBox.style.display = 'none';
        failBox.style.display = 'block';
    }
    else
    {
        okBox.style.display = 'block';
        failBox.style.display = 'none';
    }
}

function SaveForm()
{
    if($get('pnlRegister').style.display == 'none')//Logon
    {
        ShowLogon();
        PasswordCheck();
        
        if(emailValid == true && passwordValid == true)
        {
            var email = $get('ctl00_ContentPlaceHolder1_jobseekermember1_txtEmail');
            var password = $get('ctl00_ContentPlaceHolder1_jobseekermember1_txtPassword');
            JobsGroup.ValidateControls.Logon(password.value, LogonResponse);
        }
    }
    else //Register
    {
//        if(browser == 'Microsoft Internet Explorer')
//        {
//            ShowLogon();
//        }
        PasswordCheck();
        ConfirmPassword();
        ConfirmFirstname();
        ConfirmLastname();
        ConfirmJobTitle();
        ConfirmLocation();
        ConfirmEUAuth();
        //ConfirmTermsAndConditions();
        termsValid = true;
        if(emailValid == true && passwordValid == true && passwordConfirmed == true && firstnameValid == true && lastnameValid == true && termsValid == true && euRegistered == true && jobTitleValid == true && locValid == true)
        {
            CheckJBEStatus();
            //__doPostBack('btnSave', '');
        }
    }
}

function LogonResponse(result)
{
    if(result == true)
    {
        __doPostBack('btnLogon','')
    }
    else
    {   
        alert('The password you supplied is incorrect');
        //$get('dvChkError').innerHTML = '<div style="color:red">The password you supplied is incorrect</div>';
        //$get('dvPopup').style.display = 'block';
        //$get('dvContent').innerHTML = '<div style="color:red">The password you supplied is incorrect</div>';
    }
}

function SendPassword()
{
    var controltovalidate = $get('ctl00_ContentPlaceHolder1_jobseekermember1_txtEmail');
    
    
    if(controltovalidate.value == '')
    {
        $get('dvPopup').style.display = 'block';
        $get('dvContent').innerHTML = 'Please enter your email address';
       //alert('Please enter your email address');
    }
    else
    {    
        JobsGroup.EmailService.SendPasswordEmail(controltovalidate.value, SuccessfullEmailHandler);
    }
}

function SendPasswordRec()
{
    var controltovalidate = $get('ctl00_ContentPlaceHolder1_keywordContainer_logon_txtUserName');
    
    
    if(controltovalidate.value == '')
    {
        $get('dvPopup').style.display = 'block';
        $get('dvContent').innerHTML = 'Please enter your email address';
       //alert('Please enter your email address');
    }
    else
    {    
        JobsGroup.EmailService.SendPasswordEmailRec(controltovalidate.value, SuccessfullEmailHandler);
    }
}

function SuccessfullEmailHandler(result)
{
    if(result)
    {
        $get('dvPopup').style.display = 'block';
        $get('dvContent').innerHTML = 'Your password has been sent to your email address - please check your inbox.';
        //alert('A password reminder has been sent to your email address');
    }
    else
    {
        $get('dvPopup').style.display = 'block';
        $get('dvContent').innerHTML = 'We were unable to send your password reminder as we do not recognise your email address. Please check the spelling and try and again. Hint: Did you register with a different email address?';
        //alert('Your password was unable to be sent');
    }
}

function CheckJBEStatus()
{
    var checkbox = $get('ctl00_ContentPlaceHolder1_jobseekermember1_jbeReg1_chkSetup').checked;
    var jobTitle = $get('ctl00_ContentPlaceHolder1_jobseekermember1_jbeReg1_txtJobTitle').value;
    var skills = $get('ctl00_ContentPlaceHolder1_jobseekermember1_jbeReg1_txtSkills').value;
    var location = $get('ctl00_ContentPlaceHolder1_jobseekermember1_jbeReg1_locationTextbox1_txtLocation').value;
    
    if(checkbox == true)
    {
        if(jobTitle != '' || skills != '' || location != 'E.g. London, Greater London, South East, ENGLAND')
        {
            __doPostBack('btnSave', '');
        }
        else
        {
            alert('You have indicated you would like to receive email job alerts. Please enter your preferences or un-tick the box.');
        }
    }
    else if (checkbox == false)
    {
        if(jobTitle != '' || skills != '' || location != 'E.g. London, Greater London, South East, ENGLAND')
        {
            confirmation();
        }
        else
        {
            __doPostBack('btnSave', '');
        }
    }
}

function confirmation() 
{
	var answer = confirm("You have not ticked the email job alerts confirmation box. Click ‘Cancel’ to return to the form and tick the box, or ‘OK’ to proceed without activating your alerts.")
	if (answer)
	{
		 __doPostBack('btnSave', '');
	}
}






