﻿
function get_cookie(cookie_name)
{
    var results = document.cookie.match('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');
    if (results)
        return (unescape(results[2]));
    else
        return null;
}

$(document).ready(function() {

    // all links with a .download class get checked for a cookie named isRegistered
    // if the cookie doesn't exist it means they haven't used the registration form
    // so we send them there appending the href for the link as a query string key/value destinationUrl
    $('.download, .restricted').click(function() {
    
        var a = $(this);
    
        if (!get_cookie("isRegistered"))
        {
            var url = '/Catalogue-Registration-Form.aspx?destinationUrl=' + a.attr('href');
            //alert(url);
            window.location.href = url;
            return false;
        }
    
    });

    // when the registration form is click we do some basic validation
    // and set a cookie named isRegistered
    // the cookie expires in 6 months
    // is querystring key/value destinationUrl is passed in, redirect the user to destinationUrl
    $('#download-register-form .download-register-form-button-row input').click(function() {
        
        var firstname = $('#download-register-form input.firstname').val();
        var lastname = $('#download-register-form input.lastname').val();
        var company = $('#download-register-form input.company ').val();
        var jobtitle = $('#download-register-form input.jobtitle ').val();
        var emailaddress = $('#download-register-form input.emailaddress ').val();
        
        if (firstname.length === 0) {
            alert('Please enter your first name');
            return false;
        }
        
        if (lastname.length === 0) {
            alert('Please enter your last name');
            return false;
        }
    
        if (company.length === 0) {
            alert('Please enter your company name');
            return false;
        }
        
        if (jobtitle.length === 0) {
            alert('Please enter your job title');
            return false;
        }
        
        if (emailaddress.length === 0) {
            alert('Please enter your email address');
            return false;
        } else {
            var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
            if(reg.test(emailaddress) == false) {
               alert('Please enter a valid email address');
               return false;
            }
        }
           
        // add the isRegistered cookie
        var now = new Date();
        var expires_date = new Date(new Date().setDate(now.getDate() + 365)) //days
        var date_gmt = expires_date.toGMTString();
        document.cookie = "isRegistered=true; expires=" + date_gmt;
    
        // changed to server side redirect on post back
        //var destinationUrl = $('#download-register-form-destinationUrl').val();
        //if (destinationUrl.length > 0) {
            //window.location.href = destinationUrl;
            //return false;
        //}
            
    });
    
});
