
var CodeXmlDoc = null;
var KeyTimer = null;

$(document).ready(function() {

	  if ($.query.get("elig")) {
		     $.modal($('#FindCodePanel'), { persist: true, escClose: true });
		     $("#QueryTextBox").focus();			 
	  }
						   

     $("form#CodeForm").attr("autocomplete", "off").submit(BuildTable); 
	  
     $("#QueryTextBox").keyup(
		function() {
          	if ($(this).val().length > 2) {
            	KeyTimer = setTimeout(BuildTable, 100);  
     		}
    	});
     
     
     
     $("a#Family").click(function () {
          SetElig("another PSECU member", 2000);
          SetEligCookie("another PSECU member", 2000);
		  
     });
     
     $("#EligLink, #EligLink2, #EligLink3").click(
          function (e) {
		     e.preventDefault();
		     $.modal($('#FindCodePanel'), { persist: true, escClose: true });
		     $("#QueryTextBox").focus();
	     }
     );
     
	 
	//  :Contains - case-insensitive version of :contains
	jQuery.extend(jQuery.expr[':'], {
	  Contains : "jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0"
	}); 
	
	
	// Extend the text() function to put a space between nodes
	jQuery.fn.text =  function( text ) {
		if ( typeof text !== "object" && text != null )
			return this.empty().append( (this[0] && this[0].ownerDocument || document).createTextNode( text ) );

		var ret = "";

		jQuery.each( text || this, function(){
			jQuery.each( this.childNodes, function(){
				if ( this.nodeType != 8 )
					ret += this.nodeType != 1 ?
						this.nodeValue + " " :
						jQuery.fn.text( [ this ] ) + " ";	// The space
			});
		});

		return ret;
	}; 	

   
     
     GetMemberCodesXml();
	 

	 
});

function disableLaunchButton() {
	$("#LaunchApplicationLink").css({opacity: 0.3, cursor: "default", outline: "none"}).click(null);	
}


function GetMemberCodesXml() {
     $.get('/eligibility/data.aspx', function(data){
       CodeXmlDoc = data;
	 });
	 
}

function BuildTable() {

  var QueryTxt = $("#QueryTextBox").val();  
  if (QueryTxt.length === 0) return false;	 

  $("#XmlPanel").show().empty();          
  var CurrentCat = "";
  
  $("Co:Contains('" + QueryTxt + "')", CodeXmlDoc).each(function() {
  
	   if (CurrentCat.length === 0 || CurrentCat !== $("C", this).text()) {
			CurrentCat = $("C", this).text();
			$("#XmlPanel").append("<h2>" + CurrentCat + "</h2>");
	   }
  
	   //$("<a />").text($("D", this).text() + ($("E", this).length > 0 ? (" (" + $("E", this).text().trim() + ")") : "")).attr({href: "#", rel: $("MC", this).text().trim()}).click(EligLink_Click).appendTo("#XmlPanel"); 
	   $("<span />").text($("D", this).text() + ($("E", this).length > 0 ? (" (" + $("E", this).text().trim() + ")") : "")).appendTo("#XmlPanel"); 
  });

}

function EligLink_Click(event) {
	
  var Desc = $(event.target).text().trim();
  var Code = $(event.target).attr("rel").trim();  
  
 
  
  SetEligCookie(Desc, Code);  
  SetElig(Desc, Code);
  
  return false;

}

function SetElig(Desc, Code) {     
  $("#CodeDefaultText, #EligLink").hide(); 
  
  $("#YourGroup").text(Desc);
  $("#CodeSetText").show();
  
  $.modal.close();
  $("#LaunchApplicationLink").css({opacity: 1, cursor: "pointer"}).click(function() { popUp('https://apply.psecu.com/MembershipAppPSECU/default.aspx?INTERNET') });    
}


function SetEligCookie(Desc, Code) {    
     $.cookie('Elig', Code + "|" + Desc, { path: '/', expires: 10 });
}