﻿var ADF__FName;
function trackPDF(PDFname, PDFpath) {
    /* check to see if the cookie has been set if so open the pdf in a new window*/ 
    //document.getElementById('txtBrochure').value = PDFname;
    //document.getElementById('txtBrochurePath').value = PDFpath;
    /*
	    if (checkCookie('brochureDownload'))    {
	        downloadPDF()
	    }
	    else { // cookie was not set present the user with the form 
	        popup(1);
	    }
    */
    downloadPDF(PDFname, PDFpath);
}

function downloadPDF(PDFname, PDFpath) {
    //var PDFname;
    //PDFname = document.getElementById('txtBrochure').value;
    /*
	    var bNewsLetter;
	    bNewsLetter = 0;
	    if (jQuery("#chkNewsLetter").attr("checked"))
	        bNewsLetter = 1
	    jQuery.getJSON("/brochure/register_download.aspx?txtBrochure=" + PDFname + "&chkNewsLetter=" + bNewsLetter, {id: jQuery(this).val(), ajax: 'true'}, function(j){ 
	       
	    });
    */
    
    ADF__FName = PDFname;
    ADFtrack(21491,64113);
        
    trackConv();
        
    pageTracker._trackEvent('Download', 'PDF', PDFname);
    
    //window.open(document.getElementById('txtBrochurePath').value);
    window.open(PDFpath);
    //popup(0);
}

function validateBrochureForm() {
    var blError=false;
    
    var txtFirstName;
    var txtSurname;
    var txtAddress;
    var txtEmail;
    txtFirstName = document.getElementById('txtFirstName').value;
    txtSurname = document.getElementById('txtSurname').value;
    txtEmail = document.getElementById('txtEmail').value;
    //txtAddress = document.getElementById('txtAddress').value
    
    if (txtFirstName == "")   { 
        showBrochureError('errFirstName','0');
        blError=true;
    }
    else 
        hideBrochureError('errFirstName');
    
    if (txtSurname == "")  {
        showBrochureError('errSurame','0');
        blError=true;
    }
    else 
        hideBrochureError('errSurame');
        
    if (txtEmail == "")    {
        showBrochureError('errEmail','0');
        blError=true;
    }
    else    { 
        if (isValidEmail(document.getElementById('txtEmail').value) == false)   {
            showBrochureError('errEmail','1');
            blError=true;
        }
        else 
            hideBrochureError('errEmail');
    }
    //setCookie("txtAddress", txtAddress, 18250);
    setCookie("txtEmail", txtEmail, 18250);
    setCookie("txtSurname", txtSurname, 18250);
    setCookie("txtFirstName", txtFirstName, 18250);
    setCookie("brochureDownload", "1", 18250);
    
    return !blError
}

function showBrochureError(strElement, intError)    {
    document.getElementById(strElement).style.color = 'red';
    if (intError == '1')    
        document.getElementById('formBrochureError').innerHTML = 'Please ensure you have entered a valid email';
    else
        document.getElementById('formBrochureError').innerHTML = 'Please ensure all fields marked with an asterix (*) are provided';

    document.getElementById('formBrochureError').style.display = 'block';
}

function hideBrochureError(strElement) {
    document.getElementById(strElement).style.color = 'black';
}

function popup(sw) {
    if (sw == 1) {
        document.getElementById('blackout').style.display = 'block';
        document.getElementById('divpopup').style.display = 'block';
    }
    else {
        document.getElementById('blackout').style.display = 'none';
        document.getElementById('divpopup').style.display = 'none';
        hideBrochureError('errFirstName');
        hideBrochureError('errSurame');
        hideBrochureError('errEmail');
        document.getElementById('formBrochureError').style.display = 'none';
    }
}

function isValidEmail(emailstr)   {
    var emailFilter = /^(([a-zA-Z0-9_\-]+)|(([a-zA-Z0-9_\-]+)([\.]{1,1}[a-zA-Z0-9_\-]+)+)+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;

    if (!(emailFilter.test(emailstr))) 
	    return false;
    return true;
}

function checkCookie(cookieName)  {
    cookieVal = getCookie(cookieName);
    if (cookieVal != null && cookieVal != "") 
        return true
    else    
        return false
}

function getCookie(c_name)  {
    if (document.cookie.length>0)   {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start != -1)    {
            c_start=c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";",c_start);
            if (c_end == -1)
                c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}

function setCookie(c_name,value,expiredays) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" + escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

jQuery(document).ready(function(){

    jQuery("#btnBrochureSubmit").click(function () {
        if (validateBrochureForm())
        {
            downloadPDF();
        }
    });
    
    jQuery(".fancy").fancybox({
	'hideOnContentClick': false,
	'frameWidth': 780,
	'frameHeight':540
    });
});