// JavaScript Document

//--- Element Declaration ---//
var NO_RESULTS = "#noResults";
var VIEW_QUESTION_LINK = "#viewQuestionsLink";
var TOPIC = "select[name=topic]";
var SUBTOPIC = "select[name=subtopic]";
var QUESTION = "input[name=question]";
var SEARCH_BTN = "#searchBtn";
var MESSAGE =  "textarea[name=message]";
var NAME =  "input[name=name]";
var EMAIL =  "input[name=email]";
var DETAILS_AREA = "#detailsArea";
var RESULTS_AREA = "#resultsArea";
var RESULTS = "#results";
var ADD_MORE_DETAILS = ".addMoreDetails"; 
var TRYING_TO_DO = "#tryingToDo";
var WERE_AT =  "#wereAt";
var URL = "#url";
var BROWSER =  "#browser";
var OS = "#os";


var QUESTION_STAR = "#questionStar";
var TOPIC_STAR = "#topicStar";
var SUBTOPIC_STAR = "#subtopicStar";
var MESSAGE_STAR = "#messageStar";
var EMAIL_STAR = "#emailStar";
var NAME_STAR = "#nameStar"; 
var ERROR_MSG_BAR = "#errorMsgBar";
var FAQ_QUESTION = ".faqQuestion";
var FAQ_ANSWER = ".faqAnswer";
var SUBTOPIC_AREA = ".subtopicArea";


var gbl_allTopics;
var gbl_allResults;
var gbl_hasSubtopic = false;

//--- On load execution ---//
$(function() {
	/*** Init Page ***/

	//Add the required star
	$(".requiredStar").before(REQUIRED_STAR_IMG);
	
	
	/*** Page Events ***/
	$(ADD_MORE_DETAILS).click(function() {
		showForm();		
	});


	
});


//--- Page Functions---//


/*
* Validate the contact form by the type and what to execte afterwards.
*
* @param submitType 	search | submit : determes what to validate.
*/
function validateContactUs(submitType) {
	var hasErrors = false;	
	var curElem;
	var errorMsg;

	//Determine what fields need to be validated.
	//Always need to be validated.
	/*
	errorBeforeElem =  $(QUESTION_STAR);
	errorAfterElem = $(QUESTION);
	if ( isNullEmpty( $(QUESTION).val() ) ) {
		displayErrorMsg( errorBeforeElem, "", errorAfterElem, "QuestionEmpty");
		hasErrors = true;	
	} else {
		clearMsg( errorBeforeElem, errorAfterElem);
	}
	*/
	//Only validate if it is submitting form.
	if (submitType=="submit") {
		
		errorBeforeElem =  $(TOPIC_STAR);
		errorAfterElem = $(TOPIC);
		//validate name
		if ( isNullEmpty( $(TOPIC).val() ) ) {
			displayErrorMsg( errorBeforeElem, "", errorAfterElem, "TopicEmpty");
			hasErrors = true;	
		} else {
			clearMsg( errorBeforeElem, errorAfterElem);
		}
		
		/*
		errorBeforeElem =  $(SUBTOPIC_STAR);
		errorAfterElem = $(SUBTOPIC);
		//validate name
		if (gbl_hasSubtopic &&  isNullEmpty( $(SUBTOPIC).val() ) ) {
			displayErrorMsg( errorBeforeElem, "", errorAfterElem, "SubtopicEmpty");
			hasErrors = true;	
		} else {
			clearMsg( errorBeforeElem, errorAfterElem);
		}
		*/
		//validate details
		errorBeforeElem =  $(MESSAGE_STAR);
		errorAfterElem = $(MESSAGE);
		if ( isNullEmpty( $(MESSAGE).val() ) ) {
				displayErrorMsg( errorBeforeElem, "", errorAfterElem, "MessageEmpty");		
			hasErrors = true;	
		} else {
			clearMsg( errorBeforeElem, errorAfterElem);
		}

		//validate name
		errorBeforeElem =  $(NAME_STAR);
		errorAfterElem = $(NAME);
		if ( isNullEmpty( $(NAME).val() ) ) {
			displayErrorMsg( errorBeforeElem, "", errorAfterElem, "NameEmpty");		 
			hasErrors = true;	
		} else {
			clearMsg( errorBeforeElem, errorAfterElem);
		}
		

		//validate email
		errorBeforeElem =  $(EMAIL_STAR);
		errorAfterElem = $(EMAIL);
		if ( isNullEmpty( $(EMAIL).val() ) ) {
			displayErrorMsg( errorBeforeElem, "", errorAfterElem, "EmailEmpty");		
			hasErrors = true;	
		} else if (!validateEmail($(EMAIL).val())) {
			displayErrorMsg( errorBeforeElem, "", errorAfterElem, "EmailInvalid");		
			hasErrors = true;	
		} else {
			clearMsg( errorBeforeElem, errorAfterElem);
		}
		
		errorBeforeElem =  "";
		errorAfterElem = "";
		
		if (!hasErrors) {
			document.contactForm.submit();			
		} else {			
			slideContent(ERROR_MSG_BAR, "DOWN");
			gotoElement("#top");	
		}//if (hasErrors) {
	} if (submitType=="search") {
		
		errorBeforeElem =  $(QUESTION_STAR);
		errorAfterElem = $(QUESTION);
		if ( isNullEmpty( $(QUESTION).val() ) ) {
			displayErrorMsg( errorBeforeElem, "", errorAfterElem, "KeywordsEmpty");
			hasErrors = true;	
		} else {
			clearMsg( errorBeforeElem, errorAfterElem);
		}
		

		slideContent(ERROR_MSG_BAR, "UP");
		if (hasErrors) {
			//Hide the reults area
			slideContent(RESULTS_AREA, "UP");
			//Hide the rest of the form
			slideContent(DETAILS_AREA, "UP");	
		} else {
			//Hide the rest of the form
			slideContent(DETAILS_AREA, "UP");	
			//Clear the form of data
			clearFormData();
			//Make a query to retrieve related searches.
			searchFAQ( $(QUESTION).val() );	
		}//if (hasErrors) {

		
		
	}//if (submitType=="submit") {
	
	
	

}



/*
* Get the faq results and display it in the results page.
*
* @param query	the query (question) to search for to get results.
*/
function searchFAQ(query) {
	var allResults;
	var completed;
	
	var curPage = $("#helpWin").attr("answer");

	$.ajax({
		type: "POST",
		url: "/ajaxCalls/searchFAQ.jsp",
		data: "query="+query+"&hw="+curPage,
		success: function(data, textStatus){
			if ( data.trim() !="") {
				//Display the resutls
				$(RESULTS).html(data);
				showResults();			
			} else {
				//Display message
				showNoResults();
			}
				
		},
		error :  function (XMLHttpRequest, textStatus, errorThrown) {
			completed =false;
		},
		dataType : "html"
	 });
	/*
	$.ajax({
		type: "POST",
		url: "/servlet/cs",
		data: "op=search&query="+query,
		success: function(data, textStatus){
			if ( data.response.list.length >0) {
				//Display the resutls
				displayAllResults( data.response.list);			
			} else {
				//Display message
				showNoResults();
			}
		},
		error :  function (XMLHttpRequest, textStatus, errorThrown) {
			completed =false;
		},
		dataType : "json"
	 });
	*/
}

/*
* Display the results to the results area
*
* @param data	An array of results to display
*/
function displayAllResults(results) {
	var allQuestions = "<ul class='arrowUL'>";
	//Display the resutls
	for( var i in results) {
		allQuestions += createResult(results[i]);
	}
	allQuestions +="</ul>";
	$(RESULTS).html(allQuestions);
	
	showResults();
	
	//Show the view question again link
	slideContent(VIEW_QUESTION_LINK, "DOWN");
	
	
}


/*
* Create the li objects for the resutsl.
* 
* @param key		the text to display
* @param value	The value for the link to open the dynamic content.
*/
function createResult(res) {
	//replace database breaks with html breaks;
	var answer = res.answer
	answer = answer.replace(/\n/g, "<br>");
	answer = answer.replace(/\r/g, "<br>");
	return "<li style='padding-top:10px' ><a href='#' faqId='"+res.id+"' class='faqQuestion' onclick='getFaqAnswer("+res.id+"); return false'>"+res.question+"</a><div class='faqAnswer' faqId='"+res.id+"' style='display:none'>"+answer+"</div></li>";
}

/*
* Retrieve and display the faq answer below the question.
*
* @param id 	the id of the question.
*/
function displayAnswer(id) {
	var answerElem = FAQ_ANSWER+"[resultId="+id+"]";
	//Check if answer is already displayed.
	if ( $(answerElem).css("display") == "none") {
		//Answer is hidden so show it
		toggleQuestionArrow(answerElem, "show");
		slideContent(answerElem, "DOWN");
	} else {
		//Answer is already shown so hide it.
		toggleQuestionArrow(answerElem, "hide");
		slideContent(answerElem, "UP");
	}
}


/*
* Display the correct bullet arrow for the question depending on the action.
*
* @param qnum 		the id of the question 
* @param action 	show | hide . display the bullet arrow as on or off.
*/
function toggleQuestionArrow(elem, action) {
	if (action == "show") {
		imgSrc = "arrow_on.gif";	
	} else {
		imgSrc = "arrow_off.gif";
	}
	$(elem).parent().css("list-style-image", "url(/images/faqs/"+imgSrc+")");
}

/*
* If user came from the help page, then this will show the form and hid the question box.
*/
function fromHelpEmail() {
	$(".hidSectionHelp").hide();
	showForm();
}

/*
* Display the rest of the contact form and hide the results area.
*/
function showForm() {
	//hide the no resutls
	slideContent(NO_RESULTS, "UP");	
	//Hide the Results.
	slideContent(RESULTS_AREA, "UP");	
	//Shot the form
	slideContent(DETAILS_AREA, "DOWN");		
	
}

/*
* Display the resutls area and hid the contact form 
*/
function showResults() {
	//hide the no resutls
	slideContent(NO_RESULTS, "UP");		
	//hide the form
	slideContent(DETAILS_AREA, "UP");	
	//hide all the answers
	$(FAQ_ANSWER).each(function() {
		slideContent(this, "UP");	
		toggleQuestionArrow(this,"hide");
	});
	//show the Results.
	slideContent(RESULTS_AREA, "DOWN");
	
}

/*
* Display the resutls area and hid the contact form 
*/
function showNoResults() {		
	//Hide the view question again link
	slideContent(VIEW_QUESTION_LINK, "UP");
	//hide the form
	slideContent(DETAILS_AREA, "UP");	
	//hid the Results.
	slideContent(RESULTS_AREA, "UP");
	//hide the no resutls
	slideContent(NO_RESULTS, "DOWN");	

	
}

/*
* Clear form fields including error.
*/
function clearFormData() {
		$(TOPIC).val("");		
		clearMsg( $(TOPIC_STAR), $(TOPIC));
		$(SUBTOPIC).val("");		
		clearMsg( $(SUBTOPIC_STAR), $(SUBTOPIC));
		$(SUBTOPIC).attr("disabled", "disabled");	
		$(MESSAGE).val("");		
		clearMsg( $(MESSAGE_STAR), $(MESSAGE));
		$(TRYING_TO_DO).val("");		
		$(WERE_AT).val("");	
		$(URL).val("");	
		$(BROWSER).val("");	
		$(OS).val("");
		

}


/*
* Reload the page when user clicks cancel
*/
function cancelContactUs() {
	window.location.href = "/customerSupport.jsp";
}


/*
* Display the error on the page.
*
* @param	name		The name of the input
*
* return 	The error message.
*/
function getMessage(name) {
	switch (name) {		
		case "KeywordsEmpty" :  msg =  "Please enter some keywords to search for."; break;
		case "QuestionEmpty" :  msg =  "Please enter a question."; break;
		case "TopicEmpty" :  msg =  "Please select a topic."; break;
		case "SubtopicEmpty" :  msg =  "Please select a subtopic."; break;
		case "MessageEmpty" :  msg =  "Please enter details about your question."; break;
		case "NameEmpty" :  msg =  "Please enter your name."; break;
		case "EmailEmpty" :  msg =  "Please enter your email address."; break;
		case "EmailInvalid" :  msg =  "Please enter a valid email address."; break;
		default : msg=""; break;
	}
	return msg;
	
}




