﻿  $(document).ready(function() {
    $('#wpdownload').click(function() {
        

	//check for com.vtrenz.imawebcookie - to grab websyncid
        var websyncid = "";

        if ($.cookie("com.vtrenz.iMAWebCookie"))
        {
            websyncid = $.cookie("com.vtrenz.iMAWebCookie");
        }
        if ($.cookie("wpload"))
        {
            // Active Cookie found
            idval = $.cookie("wpload").split(":");
            
            var link = $('#wpid').attr('href');
            var equalPosition = link.indexOf('='); //Get the position of '='
            var number = link.substring(equalPosition + 1); //Split the string and get the number.
            
            link = $('#wpname').attr('href');
            equalPosition = link.indexOf('='); //Get the position of '='
            var name = link.substring(equalPosition + 1); //Split the string and get the number.
            
		    dstring = "t=0&id=" + number + "&uid=" + idval[1] + "&n=" + name + "&syncid=" + websyncid;
		    // Submit info via ajax call
		    $.ajax(
                  {
                       type: "POST",
                       url: "/whitepapers/useridcheck.aspx",
                       data: dstring,
                       async: false,
                       success: function(msg)
                       {
                        dest = $('#wpdownload').attr('href');
                        fullmsg = msg.split("^");
                        //dwv = window.open(dest);
                        //return true;
                        $('#dialog2').dialog('open');
                        $('#emailsent').html('<b>' + fullmsg[1] + '</b>');
                       },
                        error:function (xhr, ajaxOptions, thrownError)
                        {
                            alert('An unknown error has occurred. Please try again.')
                            alert(thrownError);
	                        
                        }

                 });
        }
        else
        {
            // No cookie found - ask questions
            
            $('#dialog').dialog('open');
        }
        return false;
    });
    function isValidEmailAddress(emailAddress) 
    {
        var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
        return pattern.test(emailAddress);
    }
    function checkdialogform()
    {
        retvalue = true;
        alerttxt = '';
        if ($('#fname').val() == '')
        {
            retvalue = false;
            alerttxt = alerttxt + 'Please enter your first name';
        }
        if ($('#lname').val() == '')
        {
            retvalue = false;
            alerttxt = alerttxt + '\nPlease enter your last name';
        }
        if (!isValidEmailAddress($('#e-mail').val()))
        {
            retvalue = false;
            alerttxt = alerttxt + '\nPlease enter your valid organization e-mail address';
        }
        if ($('#companyname').val() == '')
        {
            retvalue = false;
            alerttxt = alerttxt + '\nPlease enter your company name';
        }
        if (retvalue == false)
        {
            alert(alerttxt);
            return false;
        }
        else 
        {
            $('#dialog').dialog("close");
            $('#dialog1').dialog('open');
        }
    }
    function checkdialog1form()
    {
        retvalue = true;
        alerttxt = '';
        
        if ($('#industry').val() == '')
        {
            retvalue = false;
            alerttxt = alerttxt + '\nPlease select your industry';
        }
        if (retvalue == false)
        {
            alert(alerttxt);
            return false;
        }
        else return true;
    }
    // Dialog			
				$('#dialog').dialog({
					title: "Please Enter The Following Details",
					modal:true,
					autoOpen: false,
					height: 350,
					width: 500,
					buttons: {
						"Next": function() { 
							checkdialogform();
							
						}, 
						"Cancel": function() { 
							$(this).dialog("close"); 
						} 
					}
				});
				
				$('#dialog1').dialog({
					title: "Please Answer The Following",
					modal:true,
					autoOpen: false,
					height: 450,
					width: 500,
					buttons: {
						"Submit": function() { 
							if (checkdialog1form())
							{
							    var link = $('#wpid').attr('href');
                                var equalPosition = link.indexOf('='); //Get the position of '='
                                var number = link.substring(equalPosition + 1); //Split the string and get the number.

                                link = $('#wpname').attr('href');
                                equalPosition = link.indexOf('='); //Get the position of '='
                                name = link.substring(equalPosition + 1); //Split the string and get the number.
							    dstring = "t=1&n="+ name + "&id=" + number + "&f=" + $('#fname').val() +  "&em=" + $('#e-mail').val() + "&l=" + $('#lname').val()  + "&ti=" + $('#jtitle').val()  + "&co=" + $('#companyname').val() + "&s=" + $('#txtState').val() + "&a=" + $('#txtAddress').val()+ "&c=" + $('#txtCity').val() + "&z=" + $('#txtZip').val() + "&p=" + $('#txtPhoneNumber').val() + "&q1=" + $('#chkQ1').is(':checked') + "&q2=" + $('#chkQ2').is(':checked') + "&q3=" + $('#chkQ3').is(':checked') + "&q4=" + $('#chkQ4').is(':checked') + "&q5=" + $('#chkQ5').is(':checked') + "&syncid=" + websyncid;
							    // Submit info via ajax call
							    $.ajax(
		                              {
			                               type: "POST",
			                               url: "/whitepapers/useridcheck.aspx",
			                               data: dstring,
			                               async: false,
			                               success: function(msg)
			                               {
				                            $('#dialog1').dialog("close");
				                            today = new Date();
                                            today.setMinutes(52);
                                            fullmsg = msg.split("^");
                                            
				                            $.cookie("wpload", "id:"+fullmsg[0], { expires: 30 });
				                            dest = $('#wpdownload').attr('href');
				                            
				                            //dwv = window.open(dest)
				                            //return true;
				                            $('#dialog1').dialog("close");
                                            $('#dialog2').dialog('open');
                                            $('#emailsent').html('<b>' + fullmsg[1] + '</b>');
			                               },
				                            error:function (xhr, ajaxOptions, thrownError)
				                            {
					                            alert('An unknown error has occurred. Please try again.')
    					                        
				                            }

		                             });
		                     }
						}, 
						"Cancel": function() { 
							$(this).dialog("close"); 
						} 
					}
				});
				$('#dialog2').dialog({
					title: "Confirmation",
					modal:true,
					autoOpen: false,
					height: 350,
					width: 500,
					buttons: {
						"OK": function() { 
							$(this).dialog("close");
							location.href="/whitepapers/";
							} 
					    }
				});
				// Dialog Link
				$('#dialog_link').click(function(){
					$('#dialog').dialog('open');
					return false;
				})
				.hover(
					function() { $(this).addClass('ui-hover-state'); }, 
					function() { $(this).removeClass('ui-hover-state'); }
				);

  });

