﻿$(document).ready(function(){
	
	//$(".accordion2 h3").eq(0).addClass("active");
	//$(".accordion2 p").eq(0).show();
 
	$(".accordion2 h3").click(function(){
 jQuery(this).next(".content").slideToggle("fast");
		//$(this).next("p").slideToggle("fast")
		//$(this).siblings("p:visible").slideUp("fast");
		$(this).toggleClass("active");
		//$(this).siblings("h3").removeClass("active");
	});
 
});

var download_file;

function setCookie(c_name,value,exdays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

function checkCookie()
{
var username=getCookie("username");
  if (username!=null && username!="")
  {
  alert("Welcome again " + username);
  }
else 
  {
  username=prompt("Please enter your name:","");
  if (username!=null && username!="")
    {
    setCookie("username",username,365);
    }
  }
}

function dl(filename)
{
	download_file = filename;
}

function download(f, name)
{
	myTempWindow = window.open(f,'','left=10000,screenX=10000');
	myTempWindow.document.execCommand('SaveAs','null', name);
	//myTempWindow.close();
}

function attach_file( p_script_url ) {
      
	$.post(p_script_url);
	//alert(p_script_url);
}


$(function () {

    var firstname = $("#firstname");
    var lastname = $("#lastname");
    var companyname = $("#companyname");
    var email = $("#email");
    var phone = $("#phone");
    var allFields = $([]).add(firstname).add(lastname).add(companyname).add(email).add(phone);
    var tips = $("#validateTips");

    function updateTips(t) {
        tips.text(t).effect("highlight", {}, 1500);
    }

    function checkLength(o, n, min, max) {

        if (o.val().length > max || o.val().length < min) {
            o.addClass('ui-state-error');
            updateTips("Length of " + n + " must be between " + min + " and " + max + ".");
            return false;
        } else {
            return true;
        }

    }


    function checkRegexp(o, regexp, n) {

        if (!(regexp.test(o.val()))) {
            o.addClass('ui-state-error');
            updateTips(n);
            return false;
        } else {
            return true;
        }

    }

    $("#dialog").dialog({
        bgiframe: false,
        autoOpen: false,
        height: 300,
        width: 400,
        modal: true,
        buttons: {
            'Start Download': function () {

                //var industry = $("#industry").val();
                //var department = $("#department").val();
                var bValid = true;
                allFields.removeClass('ui-state-error');

                bValid = bValid && checkLength(firstname, "firstname", 3, 16);
                bValid = bValid && checkLength(lastname, "lastname", 3, 16);
                var name = firstname.val() + " " + lastname.val();
                //bValid = bValid && checkLength(companyname, "companyname", 3, 16);
                //bValid = bValid && checkLength(email, "email", 6, 80);
                //bValid = bValid && checkLength(phone, "phone", 10, 11);


                //bValid = bValid && checkRegexp(name,/^[a-z]([0-9a-z_])+$/i,"Name may consist of a-z, 0-9, underscores, begin with a letter.");
                // From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
                //bValid = bValid && checkRegexp(email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@jquery.com");
                //bValid = bValid && checkRegexp(password,/^([0-9a-zA-Z])+$/,"Password can only be alpha-numeric");


                // Update here
                if (bValid) {
                    setCookie("insightreg", "true", 15);
                    attach_file('/Insight/mysql_insert.php?name=' + name + '&email=' + email.val() + '&phone=' + phone.val());
                    

                    $(this).dialog('close');
                    location.href = "/Downloads/" + download_file;
                    //download('/Downloads/' + download_file, download_file);


                }
            },
            Cancel: function () {
                $(this).dialog('close');
            }
        },
        close: function () {
            allFields.val('').removeClass('ui-state-error');
        }
    });



    $('#wp1, #wp2, #wp3, #wp4, #cs1, #cs2, #cs3, #cs4, #od1, #od2, #od3, #od4').click(function (event) {
        //download_file = $(this).constructor.name;
        $('#dialog').dialog('option', 'position', [event.clientX, event.clientY]);
        var cookie = getCookie("insightreg");
        if (cookie != null && cookie != "") {
            location.href = "/Downloads/" + download_file;
        }
        else {
            $('#dialog').dialog('open');
        }
    })

		.hover(
			function () {
			    //$(this).addClass("ui-state-hover"); 
			},
			function () {
			    //$(this).removeClass("ui-state-hover"); 
			}
		).mousedown(function () {
		    $(this).addClass("ui-state-active");
		})
		.mouseup(function () {
		    $(this).removeClass("ui-state-active");
		});

});

