/*
* subjectLevel2.js
*
* Handles the dynamic stuff: loading the specific
* subject select box, displaying any associated content,
* and shows and hides the "Other" subject text box.
*
* insert in the <head> of *_Email.asp:
* 	//<script type="text/javascript" src="../subjectLevel2.js"></script>
*   ***>> replaced by 
*   <script type="text/javascript" src="subjectLevel2.js"></script>
*
* vgw		11/5/2008 created
* 
*/
$(function()
{
	//$('#txtNameFirst').focus();
	$('#select1').focus();
	var subj1Val = "";
	var subOtherText = "Enter your subject (up to 50 characters)."
$('#select1').change(function(event)
	{
		if ( $('#select1').val() == "Other" )
		{
			$("#select2").remove();
			dispOther(subOtherText);
			$('#subOther').click(function()
			{
				if ( $('#subOther').val() == subOtherText )
				{ $('#subOther').val("");	} // clear textbox for user typing
			});
		}
		else
		{
			$("#subOther").hide();
			//$("#subOther").remove();
			if ( $("#select2").length > 0 )  $("#select2").remove();
			$("#holdFAQ").remove();
			$("#continue").hide();
			subj1Val = $('#select1').val();
			$("<select id='select2' name='select2'><option value=''>Select a specific subject...</option></select>").insertAfter("#update-target");
			//
			// binds event to dynamically created element
			$('#select2').change(function(event2)
			{ dispFAQ(subj1Val); });
			//
			 $.ajax(
			 {
				type: "GET",
				url: "subjects.xml",
				dataType: "xml",
				success: function(xml)
				{
					$(xml).find("subjectL1").each(function()
					{
						if ($(this).attr("value") == subj1Val)
						{
							var maxlen = 0;
							$(this).children("subjectL2").each(function()
							{
								//  L2 subjects can be associated with 1 or more specific programs
								//  "include" can contain a comma-delimited list
								var include2 = $(this).attr("include").toUpperCase();
								if (include2.indexOf("ALL") >= 0 || include2.indexOf(programID.toUpperCase()) >= 0)
								{
									var subj2_text = $(this).attr("opt");
			  					if (subj2_text.length > maxlen) maxlen = subj2_text.length;
									$('<option></option>')
										.html(subj2_text)
										.val(subj2_text)
										.appendTo('#select2');
								$("#select2").width(maxlen*6+36); // this is arbitrary but seemed to look ok
							 } //close if (include2.indexOf...){
						 }); //close each(
						return false; //break out of .each
					} //close if (){
				}); //close each(
			 }
			}); //close $.ajax(
		 }  // end else
	}); //close change(
}); //close $(

function dispFAQ(subj1Val)
{
		var answer = "<p style='font-weight: bold;'>This information might answer your question:</p>";
		if ( $("#holdFAQ").length > 0 )
		{
			$("#holdFAQ").remove();
			$("#continue").hide();
		}
			var subj2Val = $('#select2').val();
			$("<div id='holdFAQ'></div>").insertAfter("#containerFAQ");
      $.ajax(
      {
        type: "GET",
        url: "subjects.xml",
        dataType: "xml",
        success: function(xml)
        {
         $(xml).find("subjectL1").each(function()
         {
					 if ($(this).attr("value") == subj1Val)
					 {
	           $(this).children("subjectL2").each(function()
	           {
							if ($(this).attr("opt") == subj2Val)
							{
								if ($(this).attr("target") != "" || $(this).attr("target").length > 0) // not all subjects have content
								{
									//var subj2_label = $(this).attr("labelID");
                  var rootTarget = "_"+$(this).attr("target");
									var subj2_target = programYR+rootTarget;
									//alert("subj2_target = "+subj2_target);
									var holdTarget = "";
									holdTarget = getContent(subj2_target).toString();
									if (holdTarget.length < 5)
									{
									  subj2_target = defaultPrefix+rootTarget;
									  //programYR = defaultPrefix;
									  holdTarget = getContent(subj2_target).toString();
									}
									if (holdTarget.length < 5)
									{
									  holdTarget = "An error has occurred. ";
									}
									/** create link (or not) at bottom of content ***/
									var more = "";
									$(this).attr("more") == null ? more="" : more = $(this).attr("more").toString();
									var moreTxt = "";
									switch(more)
									{
									  case "y":
									    moreTxt = "<p><a href='"+subj2_target+$(this).attr("linklbl")+"' target='_blank'>More on this topic...</a></p>";
									    break;
									  case "c":
									    moreTxt = "<p><a href='"+defaultPrefix+"_contact_opener.asp#faqcontact' target='_blank'>Contact Information...</a></p>";
									    break;
									  default:
									    moreTxt = "";
									}
									holdTarget = holdTarget.replace(/[\n\f\r\t]/g," ");
									if (defaultPrefix == "NY") 
									{ holdTarget = holdTarget.replace(/ ATS.P/g," ATS&#8211;P"); } // - is displaying as ? haven't figured out how to type an en dash yet
									holdTarget = holdTarget.replace(/(20\d\d).(20)/g,"$1&#8211;$2"); // - is displaying as ? \x96
									//var pattern = "=\s*\""+subj2_label+"\s*([^>].*?)>([^</"+$(this).attr("tag")+"].*?)</"+$(this).attr("tag");
									var pattern = "<"+$(this).attr("tag")+">(.*)</"+$(this).attr("tag")+">";
									var re = new RegExp(pattern, "i");
									var match = re.exec(holdTarget)
									var contentToGet = answer+RegExp.$1+moreTxt;
									$("#holdFAQ")
										.html(contentToGet)
										.addClass("hldFAQ");
									$("#continue").show();
								} //close if ("target != ""){
              } //close if ("opt"...){
             }); //close each(
						return false; //break out of .each
					} //close if (){
        }); //close each(
      }
    }); //close $.ajax(
  //}); //close change(
}; //close function

function getContent(url)
{
  var retval = "";
  $.ajax(
	{
		type: "GET",
		url: url,
		async: false,
		dataType: "html",
		success: function(response)
		{ retval = response; },
		error: function(xhr)
 	  { retval = xhr.status; }
	});
  return retval;
}

function dispOther(subOtherText)
{
	$("<input type='text' size='50' maxlength='50' name='subOther' id='subOther'/>")
		.val(subOtherText)
    .appendTo('#update-target');
}
