var popupUniversalStatus = 0;
var popupPatientStatus = 0;
var popupContactUsStatus = 0;
var popupExitSurveyStatus = 0;
var popupPatientStoryStatus = 0;
var popupPatientStoryThankYouStatus = 0;

var exitCheckY = null;


function ccAddEvent(object,oEvent,fnctn)
{
	if(object.addEventListener) object.addEventListener(oEvent,fnctn,false);
	else if(object.attachEvent) object.attachEvent("on" + oEvent, fnctn);
}

function stupidFunction() {
    alert("hello");
}


$(document).ready(function () {

    $(document).mousemove(function (e) {
        if (formIsOpen == false) {
            if (e.pageY > 300) {
                popIsOpen = true;
                //console.log("pop = true");
            }
            else {
                popIsOpen = false;
                //console.log("pop = false");
            }
        }
    });

    $("#exit-survey").live('click', function (event) {
        //window.open('https://procure.pages.sites.dynamicsonline.com/Leads/requestconsultation.aspx','','scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no');
        event.preventDefault();
        var linkID = $(this).attr("id");
        var docScrollTop = $(document).scrollTop(); //Gets Scroll position
        var clickcoordinate = event.pageY; //Gets position where the click happene
        loadExitSurveyPopup(docScrollTop);  //LOADING POPUP
    });

    //$("#contact-us-footer-content").hover(function(){popIsOpen = true;}, function(){popIsOpen = false;});

    $(".share-stories-popup").live('click', function (event) {
        //window.open('https://procure.pages.sites.dynamicsonline.com/Leads/requestconsultation.aspx','','scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no');
        event.preventDefault();
        var linkID = $(this).attr("id");
        var docScrollTop = $(document).scrollTop(); //Gets Scroll position
        var clickcoordinate = event.pageY; //Gets position where the click happene
        loadPatientStoriesPopup(docScrollTop, clickcoordinate, linkID);  //LOADING POPUP
        popIsOpen = true;
        formIsOpen = true;
    });

    $(".request-consultation-popup").live('click', function (event) {
        //window.open('https://procure.pages.sites.dynamicsonline.com/Leads/requestconsultation.aspx','','scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no');
        event.preventDefault();
        var linkID = $(this).attr("id");
        var docScrollTop = $(document).scrollTop(); //Gets Scroll position
        var clickcoordinate = event.pageY; //Gets position where the click happene
        loadUniversalPopup(docScrollTop, clickcoordinate, linkID);  //LOADING POPUP
        popIsOpen = true;
        formIsOpen = true;
    });

    $(".refer-patient-popup").live('click', function (event) {
        //window.open('https://procure.pages.sites.dynamicsonline.com/Leads/referpatient.aspx','','scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no');
        event.preventDefault();
        var linkID = $(this).attr("id");
        var docScrollTop = $(document).scrollTop(); //Gets Scroll position
        var clickcoordinate = event.pageY; //Gets position where the click happene
        loadPatientPopup(docScrollTop, clickcoordinate, linkID);  //LOADING POPUP
        popIsOpen = true;
        formIsOpen = true;
    });

    $(".contact-us-popup").live('click', function (event) {
        //window.open('https://procure.pages.sites.dynamicsonline.com/Leads/contactus.aspx','','scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no');
        event.preventDefault();
        var contactUsFormType = $(this).attr("rel");
        var linkID = $(this).attr("id");
        var docScrollTop = $(document).scrollTop(); //Gets Scroll position
        var clickcoordinate = event.pageY; //Gets position where the click happene
        loadContactUsPopup(docScrollTop, clickcoordinate, linkID, contactUsFormType);  //LOADING POPUP
        popIsOpen = true;
        formIsOpen = true;
    });

    //Click out event!   
    $("#backgroundPopup, .popupClose").live('click', function () {
        disableUniversalPopup();
        popIsOpen = false;
        formIsOpen = false;
    });

    if ($(window).width() === 1000) {
        $('#left-content-column').css({ 'width': '280px' });
        $('#main-body-copy').css({ 'padding-left': '30px' });
    }
}); 



ccAddEvent(window,"load",function(e){
	ccAddEvent(document,"mouseout",function(e){
		e = e ? e : window.event;
		var from = e.relatedTarget || e.toElement;
		if(!from || from.nodeName == "HTML")
		{
			var docScrollTop = $(document).scrollTop(); //Gets Scroll position
			loadExitSurveyPopup(docScrollTop);  //LOADING POPUP
		}
	});
});

function loadExitSurveyPopup(docScrollTop) 
{
    //alert(popIsOpen);

	if (!popIsOpen)
	{	
		if(!checkFormCookie())
		{
			var winH = $(window).height();
			var winW = $(window).width();
			var topmargin = winH / 2 - $('#popupExitSurvey').height() / 2 + docScrollTop;
			if (topmargin < 0) topmargin = 0;
			
			//Determine positioning based on user scrolling
			$('#popupExitSurvey').css('top', topmargin);
			$('#popupExitSurvey').css('left', winW / 2 - $('#popupExitSurvey').width()/2);

			$(window).resize(function() 
			{
				var newwinW = $(window).width();
				$('#popupExitSurvey').css('left', (newwinW / 2 - $('#popupExitSurvey').width()/2));
			});
			
			if(popupExitSurveyStatus==0)
			{
				$("#backgroundPopup").css({"opacity": "0.7"});
				$("#backgroundPopup").fadeIn("slow");
				$("#popupExitSurvey").fadeIn("slow");
				popupExitSurveyStatus = 1;
				setCookie("formCookie", "true", 365);
				/*
				$('#ExitSurveyLoadingImage').append('<img src="/DesktopModules/ProCureMedia/loading.gif"/> <span>Loading form...Please Wait</span>');
				$('#ExitSurveyAjax').load('/exitsurveyform.aspx', function() 
				{
					$('#ExitSurveyLoadingImage').hide();
						
		//			if($('#patientStorySubmit').length < 1)
		//			{
		//				$('#popupPatientStory-content').append('<input type="button" name="patientStorySubmit" id="patientStorySubmit" value="Submit" />');
		//			}
					
					//setUpDefaultValues();
				});*/
				//$('#popupPatientStory-content').find('h3').hide();
				//$('#RequestConsultationAjax').html("<p style='font-family: Arial, Helvetica, sans-serif;font-size: 12px;color: #565656;line-height: 18px;'>Thank you for your interest in ProCure and proton therapy. The online inquiry form is currently unavailable. Please email us at <a style='font-family: Arial, Helvetica, sans-serif;font-size: 12px;font-weight: bold;color: #01727B;text-decoration: none;' href='mailto:contact_us@procure.com'>contact_us@procure.com</a> with your name, question, and the best way to reach you, and a ProCure representative will follow up with you shortly. Thank you, and we apologize for the inconvenience.</p>");
			}
		}

	}
	
	return false;
} 



function loadPatientStoriesPopup(docScrollTop, clickcoordinate, linkID) 
{	
	var winH = $(window).height();
	var winW = $(window).width();
	
	//Determine positioning based on user scrolling
	$('#popupPatientStory').css('top', winH / 2 - $('#popupPatientStory').height()/2 + docScrollTop);
	$('#popupPatientStory').css('left', winW / 2 - $('#popupPatientStory').width()/2);

	$(window).resize(function() 
	{
		var newwinW = $(window).width();
		$('#popupPatientStory').css('left', (newwinW / 2 - $('#popupPatientStory').width()/2));
	});
	
	if(popupPatientStoryStatus==0)
	{
		$("#backgroundPopup").css({"opacity": "0.7"});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupPatientStory").fadeIn("slow");
		popupPatientStoryStatus = 1;
		
        $('#PatientStoryLoadingImage').append('<img src="/DesktopModules/ProCureMedia/loading.gif"/> <span>Loading form...Please Wait</span>');
        $('#PatientStoryAjax').load('/patientstoryform.aspx', function() 
        {
			$('#PatientStoryLoadingImage').hide();
                
//			if($('#patientStorySubmit').length < 1)
//			{
//				$('#popupPatientStory-content').append('<input type="button" name="patientStorySubmit" id="patientStorySubmit" value="Submit" />');
//			}
			
			//setUpDefaultValues();
		});
		//$('#popupPatientStory-content').find('h3').hide();
		//$('#RequestConsultationAjax').html("<p style='font-family: Arial, Helvetica, sans-serif;font-size: 12px;color: #565656;line-height: 18px;'>Thank you for your interest in ProCure and proton therapy. The online inquiry form is currently unavailable. Please email us at <a style='font-family: Arial, Helvetica, sans-serif;font-size: 12px;font-weight: bold;color: #01727B;text-decoration: none;' href='mailto:contact_us@procure.com'>contact_us@procure.com</a> with your name, question, and the best way to reach you, and a ProCure representative will follow up with you shortly. Thank you, and we apologize for the inconvenience.</p>");
	}
} 

function loadPatientStoriesThankYouPopup(docScrollTop) 
{	
	var winH = $(window).height();
	var winW = $(window).width();
	
	//Determine positioning based on user scrolling
	$('#popupPatientStoryThankYou').css('top', winH / 2 - $('#popupPatientStoryThankYou').height()/2 + docScrollTop);
	$('#popupPatientStoryThankYou').css('left', winW / 2 - $('#popupPatientStoryThankYou').width()/2);

	$(window).resize(function() 
	{
		var newwinW = $(window).width();
		$('#popupPatientStory').css('left', (newwinW / 2));
	});
	
	if(popupPatientStoryThankYouStatus==0)
	{
		$("#popupPatientStoryThankYou").fadeIn("slow");
		popupPatientStoryThankYouStatus = 1;
        $('#PatientStoryThankYouAjax').html("<p>Your story has been submitted. If it is chosen to appear on the ProCure website, a representative will notify you; In the meantime, we appreciate your support for ProCure. If you have any questions about the Share Your Story program, please email us at <a href='mailto:sharemystory@procure.com'>sharemystory@procure.com</a>.</p><p><a href='/'>Go back to ProCure.com &raquo;</a></p>");
	}
} 

function loadUniversalPopup(docScrollTop, clickcoordinate, linkID) 
{	
	var winH = $(window).height();
	var winW = $(window).width();
	
	//Determine positioning based on user scrolling
	$('#popupUniversal').css('top', winH / 2 - $('#popupUniversal').height()/2 + docScrollTop);
	$('#popupUniversal').css('left', winW / 2 - $('#popupUniversal').width()/2);

	$(window).resize(function() 
	{
		var newwinW = $(window).width();
		$('#popupUniversal').css('left', (newwinW / 2));
	});
	
	if(popupUniversalStatus==0)
	{
		$("#backgroundPopup").css({"opacity": "0.7"});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupUniversal").fadeIn("slow");
		popupUniversalStatus = 1;
		
		$('#ConsultationLoadingImage').append('<img src="/DesktopModules/ProCureMedia/loading.gif"/> <span>Loading form...Please Wait</span>');
		$('#RequestConsultationAjax').load('/requestconsultation.aspx', function() {
			
			$('#ConsultationLoadingImage').hide();
			if($('#requestSubmit').length < 1)
			{
				$('#popupUniversal-content').append('<input type="button" name="requestSubmit" id="requestSubmit" value="Submit" />');
			}
			
			setUpDefaultValues();
		});
		//$('#popupUniversal-content').find('h3').hide();
		//$('#RequestConsultationAjax').html("<p style='font-family: Arial, Helvetica, sans-serif;font-size: 12px;color: #565656;line-height: 18px;'>Thank you for your interest in ProCure and proton therapy. The online inquiry form is currently unavailable. Please email us at <a style='font-family: Arial, Helvetica, sans-serif;font-size: 12px;font-weight: bold;color: #01727B;text-decoration: none;' href='mailto:contact_us@procure.com'>contact_us@procure.com</a> with your name, question, and the best way to reach you, and a ProCure representative will follow up with you shortly. Thank you, and we apologize for the inconvenience.</p>");
	}
}

function setUpDefaultValues()
{
	var pathname = window.location.pathname;
	pathname = pathname.toLowerCase();
	
	$("#RequestConsultation1_new_clinicaltrialinterest-Picklist").parent().hide();
	$("#RequestConsultation1_new_clinicaltrialinterest-Picklist").val('3');
	$("#ReferPatient1_new_clinicaltrialinterest-Picklist").parent().hide();
	$("#ReferPatient1_new_clinicaltrialinterest-Picklist").val('3');
	$("#RequestConsultation1_new_sourceofpatient-Picklist").parent().hide();
	$("#RequestConsultation1_new_sourceofpatient-Picklist").val('6');
	$("#ReferPatient1_new_sourceofpatient-Picklist").parent().hide();
	$("#ReferPatient1_new_sourceofpatient-Picklist").val('6');
	
	
	if(pathname.indexOf("oklahoma") != -1)
	{
		$("#RequestConsultation1_new_patientlocation-Picklist").val('1');
	}
	else if (pathname.indexOf("illinois") != -1)
	{
		$("#RequestConsultation1_new_patientlocation-Picklist").val('2');
	}
	else
	{
		$("#RequestConsultation1_new_patientlocation-Picklist").val('1');
	}
}

function setUpDefaultValuesContactUs(contactUsFormType)
{
	switch (contactUsFormType) 
	{
		case "dev":
			$("#ContactUs1_new_inquirytype-Picklist").val('6');
			break;
		case "corp":
			$("#ContactUs1_new_inquirytype-Picklist").val('4');
			break;
		default:
			break;
	}
	
	//default
	$("#ContactUs1_new_customertype-Picklist").val('6');
	$("#ContactUs1_new_contactpreference-Picklist").val('1');
	
	//set the following fields to default not applicable as well as hiding them
	$("#ContactUs1_new_clinicaltrialinterest-Picklist").val('3').parent().hide();
	$("#ContactUs1_new_sourceofpatient-Picklist").val('6').parent().hide();
	$("#ContactUs1_new_patientlocation-Picklist").val('3').parent().hide();
}

function loadPatientPopup(docScrollTop, clickcoordinate, linkID) 
{
	var winH = $(window).height();
	var winW = $(window).width();
	
	//Determine positioning based on user scrolling
	$('#popupPatient').css('top', winH / 2 - $('#popupPatient').height()/2 + docScrollTop);
	$('#popupPatient').css('left', winW / 2 - $('#popupPatient').width()/2);

	$(window).resize(function() 
	{
		var newwinW = $(window).width();
		$('#popupPatient').css('left', newwinW / 2);
	});
	
	if(popupPatientStatus==0)
	{
		$("#backgroundPopup").css({"opacity": "0.7"});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupPatient").fadeIn("slow");
		popupPatientStatus = 1;
		
		$('#PatientLoadingImage').append('<img src="/DesktopModules/ProCureMedia/loading.gif"/> <span>Loading form...Please Wait</span>');
		$('#ReferPatientAjax').load('/referpatient.aspx', function() {
		
			$('#PatientLoadingImage').remove();
			if($('#patientSubmit').length < 1)
			{
				$('#popupPatient-content').append('<input type="button" name="patientSubmit" id="patientSubmit" value="Submit" />');
			}
			
			setUpDefaultValues();
			
			$('#ReferPatient1_new_inquirytype-Picklist').val(3);
		});
		//$('#popupPatient-content').find('h3').hide();
		//$('#ReferPatientAjax').html("<p style='font-family: Arial, Helvetica, sans-serif;font-size: 12px;color: #565656;line-height: 18px;'>Thank you for your interest in ProCure and proton therapy. The online inquiry form is currently unavailable. Please email us at <a style='font-family: Arial, Helvetica, sans-serif;font-size: 12px;font-weight: bold;color: #01727B;text-decoration: none;' href='mailto:contact_us@procure.com'>contact_us@procure.com</a> with your name, question, and the best way to reach you, and a ProCure representative will follow up with you shortly. Thank you, and we apologize for the inconvenience.</p>");
	}
}

function loadContactUsPopup(docScrollTop, clickcoordinate, linkID, contactUsFormType) 
{
	var winH = $(window).height();
	var winW = $(window).width();
	
	//Determine positioning based on user scrolling
	$('#popupContactUs').css('top', winH / 2 - $('#popupContactUs').height()/2 + docScrollTop);
	$('#popupContactUs').css('left', winW / 2 - $('#popupContactUs').width()/2);

	$(window).resize(function() 
	{
		var newwinW = $(window).width();
		$('#popupContactUs').css('left', newwinW / 2);
	});
	
	if(popupContactUsStatus==0)
	{
		$("#backgroundPopup").css({"opacity": "0.7"});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContactUs").fadeIn("slow");
		popupContactUsStatus = 1;
		
		$('#ContactLoadingImage').append('<img src="/DesktopModules/ProCureMedia/loading.gif"/> <span>Loading form...Please Wait</span>');
		$('#ContactUsAjax').load('/Contact-Us.aspx', function() {
		
			$('#ContactLoadingImage').remove();
			if($('#contactusSubmit').length < 1)
			{
				$('#popupContact-content').append('<input type="button" name="contactusSubmit" id="contactusSubmit" value="Submit" />');
			}
			
			setUpDefaultValuesContactUs(contactUsFormType);
			
			$('#ReferPatient1_new_inquirytype-Picklist').val(3);
		});
		//$('#popupContact-content').find('h3').hide();
		//$('#ContactUsAjax').html("<p style='font-family: Arial, Helvetica, sans-serif;font-size: 12px;color: #565656;line-height: 18px;'><h1 style='color: #01727B; font-family: Georgia,Georgia,serif; font-size: 32px; font-style: italic; font-weight: normal; margin-bottom: 30px; margin-left: 0; margin-right: 0; margin-top: 0; padding-bottom: 0; padding-left: 0; padding-right: 0; padding-top: 0; width: 94%;'>Thank You</h1>Thank you for your interest in ProCure and proton therapy. The online inquiry form is currently unavailable. Please email us at <a style='font-family: Arial, Helvetica, sans-serif;font-size: 12px;font-weight: bold;color: #01727B;text-decoration: none;' href='mailto:contact_us@procure.com'>contact_us@procure.com</a> with your name, question, and the best way to reach you, and a ProCure representative will follow up with you shortly. Thank you, and we apologize for the inconvenience.</p>");
	}
}

//disables popup 
function disableUniversalPopup()
{     
	if(popupUniversalStatus==1) 
	{   
		$("#backgroundPopup").fadeOut("slow");
		$("#popupUniversal").fadeOut("slow");
	
		popupUniversalStatus = 0;
		
		$("#requestSubmit").fadeOut();
		$("#requestSubmit").next().remove();
		$("#requestSubmit").next().remove();
		$("#requestSubmit").remove();
		$("#popupUniversal-content").children(':first-child').children(':nth-child(3)').remove();
		$("#popupUniversal-content").children(':first-child').children(':nth-child(3)').remove();
		$(".thankyou").remove();
		$("#popupUniversal-content").css("display","");
	}   

	if(popupPatientStatus==1) 
	{   
		$("#backgroundPopup").fadeOut("slow");
		$("#popupPatient").fadeOut("slow");

		popupPatientStatus = 0;
		
		$("#patientSubmit").fadeOut();
		$("#patientSubmit").next().remove();
		$("#patientSubmit").next().remove();
		$("#patientSubmit").remove();
		$("#popupPatient-content").children(':first-child').children(':nth-child(2)').remove();
		$("#popupPatient-content").children(':first-child').children(':nth-child(2)').remove();
		$(".thankyou").remove();
		$("#popupPatient-content").css("display","");
	} 
	
	if(popupContactUsStatus==1) 
	{   
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContactUs").fadeOut("slow");

		popupContactUsStatus = 0;
		
		$("#contactusSubmit").fadeOut();
		$("#contactusSubmit").next().remove();
		$("#contactusSubmit").next().remove();
		$("#contactusSubmit").remove();
		$("#popupContact-content").children(':first-child').children(':nth-child(2)').remove();
		$("#popupContact-content").children(':first-child').children(':nth-child(2)').remove();
		$(".thankyou").remove();
		$("#popupContact-content").css("display","");
	} 	
	
	if(popupPatientStoryStatus==1) 
	{   
		$("#backgroundPopup").fadeOut("slow");
		$("#popupPatientStory").fadeOut("slow");

		popupPatientStoryStatus = 0;
		
//		$("#patientStorySubmit").fadeOut();
//		$("#patientStorySubmit").next().remove();
//		$("#patientStorySubmit").next().remove();
//		$("#patientStorySubmit").remove();
		$("#popupPatientStory-content").children(':first-child').children(':nth-child(2)').remove();
		$("#popupPatientStory-content").children(':first-child').children(':nth-child(2)').remove();
		$(".thankyou").remove();
		$("#popupPatientStory-content").css("display","");
	} 
	
	if(popupPatientStoryThankYouStatus==1) 
	{   
		$("#backgroundPopup").fadeOut("slow");
		$("#popupPatientStoryThankYou").fadeOut("slow");

		popupPatientStoryThankYouStatus = 0;
		
		$("#popupPatientStoryThankYou-content").children(':first-child').children(':nth-child(2)').remove();
		$("#popupPatientStoryThankYou-content").children(':first-child').children(':nth-child(2)').remove();
		$(".thankyou").remove();
		$("#popupPatientStoryThankYou-content").css("display","");
	} 
	
	if(popupExitSurveyStatus==1) 
	{   
		$("#backgroundPopup").fadeOut("slow");
		$("#popupExitSurvey").fadeOut("slow");

		popupExitSurveyStatus = 0;
		
		//$("#popupExitSurvey-content").children(':first-child').children(':nth-child(3)').remove();
		//$("#popupExitSurvey-content").children(':first-child').children(':nth-child(3)').remove();
		$(".thankyou").remove();
		$("#popupExitSurvey-content").css("display","");
	} 
}
