var uname = "u.name";
var uemail = "u.email";
var usite = "u.site";
var uoptin = "u.optin";
var utaf = "u.taf";
var exit = 0;
var exit_show_count = 0;
var exit_max_show_count = 2;

  function saveInformation(form1) {
    // Save user information from the first form!    
    createCookie(uname, form1.Contact0FirstName.value, 120);
    createCookie(uemail, form1.Contact0Email.value, 120);
    createCookie(uoptin, "y", 14);
    form1.submit();
    return true;
 }
 
 function saveInformation1() {
    /* Save user information from the first form!
    createCookie(uname, $("form#form1").find("input[@name='Contact0FirstName']").val(), 120);
    createCookie(uemail, $("form#form1").find("input[@name='Contact0Email']").val(), 120);
    createCookie(uoptin, "y", 7);
    */
 }
 function saveInformation2() {
 
    /* Save user information from the second form!
    createCookie(uname, $("form#form2").find("input[@name='Contact0FirstName']").val(), 120);
    createCookie(uemail, $("form#form2").find("input[@name='Contact0Email']").val(), 120);
    createCookie(uoptin, "y", 7);
    */
 }
 function saveInformation3() {
    // Save user information from the second form!
    createCookie(utaf, "y", 7);
 }
 function initForm_old() {
    /* Autofill user information if available - optin page
    $("input[@name='Contact0FirstName']").val(readCookie(uname));
    $("input[@name='Contact0Email']").val(readCookie(uemail));

    // Autofill user information if available - taf page
    $("input[@name='namex6']").val(readCookie(uname));
    $("input[@name='emailx6']").val(readCookie(uemail));

    // Save comment information when form is submitted
    $("form[@name='form1']").submit(saveInformation1);
    $("form[@name='form2']").submit(saveInformation2);
    $("form[@name='emailForm']").submit(saveInformation3);
    */
 }
 
 function initForm() {
    // Autofill user information if available - optin page
    try {
		$('input[name=Contact0FirstName]').val(readCookie(uname));
		$('input[name=Contact0Email]').val(readCookie(uemail));

		// Autofill user information if available - taf page
		$('input[name=namex6]').val(readCookie(uname));
		$('input[name=emailx6]').val(readCookie(uemail));
		} catch(err) {}
    

   //hide the all of the element with class hiddenform
	$(".hiddenform").hide();
	  //toggle the form display
	  $(".formlink").click(function()
	  {
		$(this).next(".hiddenform").slideToggle(300);			
	  });
	  
	
	$(document).mousemove(function(e) {
		
		
		// don't launch exitpop until they've entered the page.
		if(e.pageY >= $(window).scrollTop() + 101 && exit==0) {
			exit=1;
		}
		
		// launch exit pop if they head for the top of the browser to exit.
		if(e.pageY <= $(window).scrollTop()+ 30 && exit==1)
		{
		
		// Launch MODAL BOX
		$('#exit_content').modal({onOpen: modalOpen, onClose: simplemodal_close});
		}
		
		});
	  
	
 }

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; domain=extraordinaryminds.com; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return '';
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


function modalOpen (dialog) {
	if (exit_show_count < exit_max_show_count){
		dialog.overlay.fadeIn('fast', function () {
			dialog.container.fadeIn('fast', function () {
				dialog.data.hide().slideDown('fast');
			});
		});
	}
}

function simplemodal_close (dialog) {
	dialog.data.fadeOut('fast', function () {
		dialog.container.hide('fast', function () {
			dialog.overlay.slideUp('fast', function () {
				$.modal.close();
				exit_show_count++;
			});
		});
	});
}

   $(document).ready(initForm);
  
