//window.onload = initAjaxHandlers;

var contentType;
var pollNumberX;
var feedbackResp=new Array('','that despite the frequency of daytime and nighttime symptoms and symptoms during exercise, play, or physical exertion during the past 4 weeks, approximately 7 out of 10 asthma patients reported that their asthma was either well controlled (40%) or completely controlled (31%).','that despite the frequency of daytime and nighttime symptoms and symptoms during exercise, play, or physical exertion during the past 4 weeks, approximately 7 out of 10 asthma patients reported that their asthma was either well controlled (40%) or completely controlled (31%).','that more than one-third of all asthma patients (&ge;12 years of age, from survey of current asthma patients) reported that they have had an asthma episode so bad that they thought their life was in danger (36%).','that 32% of asthma patients (&ge;12 years of age, from survey of current asthma patients) reported that their doctor had developed a written action plan for them.','that only 24% of patients with asthma (&ge;12 years of age, from survey of current asthma patients) report that they have ever heard the term &quot;asthma exacerbation&quot;. The term &quot;asthma flare-up&quot; is recognized by more asthma patients (71%) than &quot;asthma exacerbation&quot; (24%) but fewer than &quot;asthma attack&quot; (97%).','that only 33% of patients with asthma (&ge;12 years of age, from survey of current asthma patients) reported that they had been given a lung function test in the past year.','that 45% of asthma patients (current asthma survey, &ge;16 years of age) in 2009 reported having no limitation on their normal activities compared with 36% of asthma patients (current asthma survey, &ge;16 years of age) in 1998.');
function initAjaxHandlers(){
	if (document.getElementById("content_sidebar")){
			var cSidebar = document.getElementById("content_sidebar");
		
		
			var execSum = cSidebar.getElementsByTagName("a")[0];
			execSum.onclick = function(){
				pageTracker._trackEvent('Downloads', 'Download', 'ExecSummary.pdf');
				
			}
	}
	if (document.getElementById("pollSubmitBtn")){
		document.getElementById("pollSubmitBtn").onclick  = function(){
			createRequest('poll');
			return false;
			
		}
	}
	if (document.getElementById("signUpSubmitBtn")){
		document.getElementById("signUpSubmitBtn").onclick  = function(){
		createRequest('signUp');
		return false;
		}
	}
	if (document.getElementById("signUpMiddleButton")){
		document.getElementById("signUpMiddleButton").onclick  = function(){
		createRequest('signUpMiddle');
		return false;
		}
	}
	
	if (document.getElementById("emailAddy")){
		document.getElementById("emailAddy").onkeydown = function(){
		if (event.keyCode == 13) 
			{        
				event.cancelBubble = true;
				event.returnValue = false;
			}
		}
	}
	
	if (document.getElementById("emailAddy2")){
		document.getElementById("emailAddy2").onkeydown = function(){
		if (event.keyCode == 13) 
			{        
				event.cancelBubble = true;
				event.returnValue = false;
			}
		}
	}
}

var xhr= false;

function createRequest(qid, trackerNum, rating) {
 try{
    xhr = new XMLHttpRequest();
  } catch (trymicrosoft) {
    try {
      xhr = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (othermicrosoft) {
      try {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        xhr = false;
      }
    }
  }
	
  if (xhr){
	
	  if (qid == 'poll'){
		contentType = 'poll';
		
		for (var i=0; i < document.pollForm.pollq.length; i++)
		   {
		   if (document.pollForm.pollq[i].checked)
			  {
				  for (var j=0; j < document.pollForm.pollq.length; j++){
					  document.pollForm.pollq[j].disabled = true;
				  }
				document.getElementById("pollSubmitBtn").removeAttribute("onclick");
				
			    var user_answer = document.pollForm.pollq[i].value;
				
				var pollid = document.pollForm.id;
				pollid = pollid.substring(pollid.length-1, pollid.length);
				pollNumberX = pollid;
				
				xhr.open("GET", "aspx/poll-process.aspx?poll_id=" + pollid + "&answer=" + user_answer, true);
				xhr.onreadystatechange = showContents;
			    xhr.send(null);
				pageTracker._trackEvent('Polls', 'Poll: ' + pollNumberX, 'Success');
			  }
		   }
		
	  }
	   else if (qid == 'signUp'){
		   contentType = 'signUp';
		   	pageTracker._trackEvent('SignUp', 'SignUp', 'Attempt');
		  var email = document.signUpForm.emailAddy.value;
		  
		  email = email.replace(/^\s*/, "").replace(/\s*$/, "");

		  //reg exp test
		  var filter = /^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,4})$/;
			if (!filter.test(email) || (email.length>50) || (email.length==0)) {
				alert('Please provide a valid email address');
				email.focus
				return false;
			}else{
				document.getElementById("signUpSubmitBtn").visible = false;
		 		document.signUpForm.emailAddy.disabled = true;
		  		xhr.open("GET", "aspx/signup-process.aspx?email=" + email, true);
				xhr.onreadystatechange = showContents;
				xhr.send(null);
				pageTracker._trackEvent('SignUp', 'SignUp', 'Success');
			}
	  }
	   else if (qid == 'signUpMiddle'){
		   contentType = 'signUpMiddle';
		   pageTracker._trackEvent('SignUp', 'SignUpFrom Middle', 'Attempt');
		  var email2 = document.signUpForm2.emailAddy2.value;
		  
		  email2 = email2.replace(/^\s*/, "").replace(/\s*$/, "");
			
		  //reg exp test
		  var filter2 = /^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,4})$/;
			if (!filter2.test(email2) || (email2.length>50) || (email2.length==0)) {
				alert('Please provide a valid email address');
				email2.focus;
				return false;
			}else{
				
				//document.getElementById("signUpSubmitBtn").visible = false;
		 		document.signUpForm2.emailAddy2.disabled = true;
		  		xhr.open("GET", "aspx/signup-process.aspx?email=" + email2, true);
				xhr.onreadystatechange = showContents;
				xhr.send(null);
				pageTracker._trackEvent('SignUp', 'SignUpFrom Middle', 'Success');
			}
	  }
	   else if (qid == 'tracker'){
					 contentType = 'tracker';
				    			
					xhr.open("GET", "aspx/tracker-process.aspx?tracker_id=" + trackerNum + "&answer=" + rating, true);					
					pageTracker._trackEvent('Trackers', 'Tracker: '+trackerNum, 'Success');
					//xhr.onreadystatechange = showContents;
					xhr.send(null);
		
	  }
	
  }else{
    alert("Error initializing XMLHttpRequest!");
  }
}

function showContents() {
  
 if(xhr.readyState == 4 && xhr.status == 200){
	 
	  if (contentType == 'poll'){
		  var responseData = xhr.responseText;
		  var yesValue = responseData.substring(0, responseData.indexOf(';'));
		  var noValue = responseData.substring(responseData.indexOf(';') +1, responseData.length);
	 	  //document.getElementById("pollContent").innerHTML = "Yes:" + yesValue + "%  NO: " + noValue + "%";
		 document.getElementById("pollContent").innerHTML =' <p class = "yesResp"><span class="response">YES</span><span class="percent">' + yesValue +  '%</span></p><p class = "noResp"><span class="response">NO</span><span class="percent">' + noValue + '%</span></p>';
		 var pollBot = document.getElementById("pollbottom");
		 pollBot.id="none";
		 var pollDiv = document.getElementById("poll");
		 pollDiv.innerHTML = pollDiv.innerHTML  +'<div id="aim_results"><h2>The AIM survey revealed</h2><p>' + feedbackResp[pollNumberX] + '</p></div>'
		  //create cookie, add yes no %s to cookie 
	  }else if (contentType == 'signUp'){
		  document.getElementById("signUpContent").innerHTML = "";
		  document.getElementById("signup").style.backgroundImage = "url(images/common/signup_thankyou.gif)";
		   //create cookie
	   }else if (contentType == 'signUpMiddle'){
		  document.getElementById("res_submit").innerHTML = "";
		  document.getElementById("res_submit").style.backgroundImage = "url(images/resources/gray-box-sign-up-thank-you.gif)";
		   //create cookie   
	  }else if (contentType == 'tracker'){
		   document.getElementById("trackerContent").innerHTML = "Thanks for rating this.";
	  }
  }
  // Do something with the xhr variable
}
