var toSendQandATrack = false;
var currentPage = "";
var action = "";

function sendQandAOmniture(){
  if (toSendQandATrack){
      sendOmnitureTrack();
      toSendQandATrack = false;
  }
  setTimeout("sendQandAOmniture()", 1000);
}

function setQandAEvent(){
	checkCurrentPage();	
  setOnclickForAskAQuestionLink();
	if (currentPage == "QandA"){
		var divContainer = 	document.getElementById("BVQAContainer");
		setQAOnClicks(divContainer);
	}
	
  if (currentPage == "Preview Question"){
	  var BVQAPreviewQuestionButtonID = document.getElementById("BVQAPreviewQuestionButtonID");  	
	  //alert(BVQAPreviewQuestionButtonID);
   	BVQAPreviewQuestionButtonID.onclick = function(){
			      toSendQandATrack = true;
			      action = "PreviewQuestion";
		        }
	}
  
  if (currentPage == "Submit Question"){
	 	var BVQASubmitQuestionButtonID = document.getElementById("BVQASubmitQuestionButtonID");
   	BVQASubmitQuestionButtonID.onclick = function(){
			      toSendQandATrack = true;
			      action = "SubmitQuestion";
		        }
	}
	
  if (currentPage == "Preview Answer"){
	 	var BVQAPreviewAnswerButtonID = document.getElementById("BVQAPreviewAnswerButtonID");
   	BVQAPreviewAnswerButtonID.onclick = function(){
			      toSendQandATrack = true;
			      action = "PreviewAnswer";
		        }
	}
	
  if (currentPage == "Submit Answer"){
	  var BVQASubmitAnswerButtonID = document.getElementById("BVQASubmitAnswerButtonID");
   	BVQASubmitAnswerButtonID.onclick = function(){
			      toSendQandATrack = true;
			      action = "SubmitAnswer";
		        }
	}
	
  if (currentPage == "OK Answer"){
		var divContainerAdditional = 	document.getElementById("BVQAAnswerAdditionalQuestionsID");
		setQAOnClicks(divContainerAdditional);
	}
    setTimeout("setQandAEvent()", 1000);		
}

function checkCurrentPage(){ 
	currentPage = "";
	
	var BVQAGuidelinesID = document.getElementById("BVQAGuidelinesID");
	var div = document.getElementById("BVQAContainer");
	var hasAskQuestionImg = hasImageWithAlt(div,'Ask a New Question');
	var hasAnswerQuestionImg = hasImageWithAlt(div,'Answer this Question');
	if (BVQAGuidelinesID!=null && (hasAskQuestionImg || hasAnswerQuestionImg)){
      currentPage = "QandA";
	}
	
	var BVQAPreviewQuestionButtonID = document.getElementById("BVQAPreviewQuestionButtonID");
	var BVQAQuestionSummaryFieldID = document.getElementById("BVQAQuestionSummaryFieldID");
	if (BVQAPreviewQuestionButtonID!=null && BVQAQuestionSummaryFieldID!=null){
		  currentPage = "Preview Question";
	}
	
	var BVQASubmitQuestionButtonID = document.getElementById("BVQASubmitQuestionButtonID");
	var BVQAQuestionPreviewFormTitleID = document.getElementById("BVQAQuestionPreviewFormTitleID");
	if (BVQASubmitQuestionButtonID!=null && BVQAQuestionPreviewFormTitleID!=null){
		  currentPage = "Submit Question";
	}

	var BVQAPreviewAnswerButtonID = document.getElementById("BVQAPreviewAnswerButtonID");
	var BVQAAnswerEditFormTitleID = document.getElementById("BVQAAnswerEditFormTitleID");
	if (BVQAAnswerEditFormTitleID!=null && BVQAPreviewAnswerButtonID!=null){
		  currentPage = "Preview Answer";
	}


	var BVQASubmitAnswerButtonID = document.getElementById("BVQASubmitAnswerButtonID");
	var BVQAAnswerPreviewFormTitleID = document.getElementById("BVQAAnswerPreviewFormTitleID");
	if (BVQASubmitAnswerButtonID!=null && BVQAAnswerPreviewFormTitleID!=null){
		  currentPage = "Submit Answer";
	}

	var BVQAOKAnswerButtonID = document.getElementById("BVQAOKAnswerButtonID");
	var BVQAAnswerSubmittedFormTitleID = document.getElementById("BVQAAnswerSubmittedFormTitleID");
	if (BVQAAnswerSubmittedFormTitleID!=null && BVQAOKAnswerButtonID!=null){
		  currentPage = "OK Answer";
	}

}

function hasImageWithAlt(div,alt){
      var elms = div.getElementsByTagName("IMG");
      var hasQorAButton = false;
      for(var i = 0, maxI = elms.length; i < maxI; ++i) {
        var elm = elms[i];
        if (elm.alt.replace(/^\s+|\s+$/g, '')==alt) {
          	hasQorAButton = true;
        } 
     }
     return hasQorAButton;
}

function setQAOnClicks(div){
	if (div!=null){
      var elms = div.getElementsByTagName("IMG");
      for(var i = 0, maxI = elms.length; i < maxI; ++i) {
        var elm = elms[i];
        if (elm.alt.replace(/^\s+|\s+$/g, '')=="Ask a New Question") {
          	elm.onclick = function(){
          		//alert('ask a new question');
          		action = "AskANewQuestion";
              toSendQandATrack = true;
          	}
        } 
        if (elm.alt.replace(/^\s+|\s+$/g, '')=="Answer this Question") {
          	elm.onclick = function(){
          		//alert('answer this question');
          		action = "AnswerThisQuestion";
          		toSendQandATrack = true;
          		setAnswerQuestionEvent();
          	}
        } 
     }
   }
}

function getElementByValue(obj,type,value){
      var elms = obj.getElementsByTagName("*");
      for(var i = 0, maxI = elms.length; i < maxI; ++i) {
        var elm = elms[i];
        if (elm.type==type) {
          if(elm.value == value){
          	return elm;
          } 
        }
     }
}

function sendOmnitureTrack(){
  var HelpIframe = document.getElementById("helpIframe");
  //alert(HelpIframe + " exists! for action:"+action); 
  if (action!=""){
  	HelpIframe.src = omniSenderURL+"&omnitype="+action;
  	action = "";
  }
	//alert("Omniture track");
}

function setOnclickForAskAQuestionLink(){
	var linkContainer = document.getElementById("BVQASummaryBoxID");
	if(linkContainer!=null){
    var elms = linkContainer.getElementsByTagName("A");
    for(var i = 0, maxI = elms.length; i < maxI; ++i) {
        var elm = elms[i];
        if (elm.title.replace(/^\s+|\s+$/g, '')=="Ask the First Question") {
          	elm.onclick = function(){
          		//alert('Ask first question');
          		action = "AskFirstQuestion";
              displayContent('product_aa');          		
              toSendQandATrack = true;
          	}
        } 
      }
   }
}
