// JavaScript Document - Contact Us
//Add new variables (should match the class name of the option tag, besides 'external') with new URLs if required. gotoPage() may need additions if added.

var agencyContact = "http://www.qwestfederalcontacts.com/lyam/";
var netContracts = "http://www.qwest.com/networx/contactus/";

//Initialize variables
var blockContainer;
var isValids;
var catId;
var tooltipContent;
var winWidth = $(window).width();
var winHeight = $(window).height();
var external = false;
var thisVal;
var count = 0;
var tipParentText;
var tooltipShown = false;
var formIsValid = true;
var abc;

////////////////////////////////////////
//WIP: check to see if a path exists and open a category onLoad
////////////////////////////////////////
function openCat(){
    log.info("openCat", "Calling openCat.", 5);
//    var thisURL = document.location.href.toString();

		var refURL = document.referrer;
		log.info("openCat","Document.refrerrer: "+thisURL,3);
		var thisURL = document.location.href;
		var fedGovern = thisURL.indexOf("federalGovernment");
		var stateLocal = thisURL.indexOf("stateGovernment");
		var education = thisURL.indexOf("education");
		var largeBiz = thisURL.search("largeBusiness");

	    var voipProd = refURL.search("http://www.qwest.com/smallbusiness/");

   if (fedGovern != -1 ) {
        // $("div.bmg_contact_tab_off").trigger("click");
        $("div#tab3a").find("div.catContent").slideDown("slow");
		$("div#tab3a").addClass("bmg_theme_action_call_wht_contact").removeClass("bmg_theme_action_call_wht_contact_off");
    }
   else if (largeBiz  != -1 || voipProd  != -1) {
        // $("div.bmg_contact_tab_off").trigger("click");
        $("div#tab1a").find("div.catContent").slideDown("slow");
		$("div#tab1a").addClass("bmg_theme_action_call_wht_contact").removeClass("bmg_theme_action_call_wht_contact_off");
    }
	   else if (stateLocal != -1 || education!= -1) {
        // $("div.bmg_contact_tab_off").trigger("click");
        $("div#tab2a").find("div.catContent").slideDown("slow");
		$("div#tab2a").addClass("bmg_theme_action_call_wht_contact").removeClass("bmg_theme_action_call_wht_contact_off");
    }

}


////////////////////////////////////////
//if class 'external' exists in the option, goes to URLs defined in the variables on top. If not defined, the option tag also needs to have a class with the same name as the variable, depending on where the URL points to)
////////////////////////////////////////
function gotoPage(n, thisOpt){
    log.info("gotoPage", "Calling gotoPage().", 5);
    external = true;
    try {
        if ($(thisOpt).hasClass('agencyContact')) {
            $(n).attr({
                "href": agencyContact,
                "target": "_blank"
            });
        }
        else 
            if ($(thisOpt).hasClass('netContracts')) {
                $(n).attr({
                    "href": netContracts,
                    "target": "_blank"
                });
            }
    } 
    catch (err) {
    }
}

////////////////////////////////////////
//Basic validation and setting of booleans, as well as routing to gotoPage() for external links
////////////////////////////////////////
function isValid(n, catId, blockContainer, valContainer, thisVal,actionURL){
	log.info("isValid", "Calling isValid", 5);
	var visBlocks = $(blockContainer).find("select").parent("div").filter(":visible");
			isValids = true;	
			$(n).attr({
				"href": "#",
				"target": ""
			});
$(visBlocks).children('select').find('option:selected').each(function(){
			thisVal = this.value;
            if ($(this).hasClass('external')) {
                gotoPage(n, this);
				 isValids = false;				
            }
            else {
                isValids = true;
                external = false;
            }
    });
	log.info("isValid", "All fields valid: "+isValids, 5);
	if (isValids) {
		sendValues(n, catId, blockContainer, valContainer,actionURL);
	}
	
}


////////////////////////////////////////
//Builds the validation - rules and messages, manipulates error container/messages and routes to  sendValues() to builds args
////////////////////////////////////////
function validateThis(n, valContainer,actionURL){	
    log.info("validateThis", "Calling validateThis", 5);
    blockContainer = $(n).parents(".catBlock");
	$(blockContainer).siblings().find("select, input").addClass("hidden");
	$(blockContainer).find("div:hidden").children("select, input").addClass("hidden");
		$(blockContainer).find("select").each(function(i) {
		$("option:first",this).val("select");																			   
	});
	var hasChoices = $(blockContainer).find("input:radio").length;

	//$(blockContainer).find("select").removeClass("hidden");
    catId = $(blockContainer).find("h2.catName").attr('id');
	$('select').change(function(){	if ($(this).valid()){} else {} }); 
	validator = $("#bmg_mainForm").bind("invalid-form.validate", function() {
	$(".bmg_errorContainer").html("<span><h3>" + validator.numberOfInvalids() + " required field(s) missing and highlighted below. </h3></span>"); 
	}).validate({
		onfocusout: false,
		onsubmit: true, 
		onchange:false,
		ignore: ".hidden",
		errorElement: "div",
		errorContainer: $(n).parents().find(".bmg_errorContainer"),
rules: {
		pubPriInterestArea: {
			required: true
		},
		pubPriState: {
			required: true
		},
		stateLocalInterestArea: {
			required: true
		},
		stateLocalState: {
			required: true
		},
		fedGovInterestArea: {
			required: true
		},
		fedGovContractQuest: {
			required: true
		},
		fedGovContractQuestMAA: {
			required: true
		},
		quesType: {		
			required: true	
		}
	},
	messages: {
			pubPriInterestArea: {
			required: "<p><img src='/qcms/qCmsRepository/Global/largeBusiness/images/thumbnail-icons/bmg_ico_alert.gif' alt='Icon: Alert' /><label for='pubPriInterestArea'> Select an Area of Interest.</label></p>"
			},
			pubPriState: {
			required: "<p><img src='/qcms/qCmsRepository/Global/largeBusiness/images/thumbnail-icons/bmg_ico_alert.gif' alt='Icon: Alert' /> <label for='pubPriState'> Select a State/Province.</label></p>"
			},
			
			stateLocalInterestArea: {
			required: "<p><img src='/qcms/qCmsRepository/Global/largeBusiness/images/thumbnail-icons/bmg_ico_alert.gif' alt='Icon: Alert' /><label for='stateLocalInterestArea'> Select an Area of Interest.</label></p>"
			},
			stateLocalState: {
			required: "<p><img src='/qcms/qCmsRepository/Global/largeBusiness/images/thumbnail-icons/bmg_ico_alert.gif' alt='Icon: Alert' /> <label for='stateLocalState'> Select a State/Province.</label></p>"
			},
			fedGovInterestArea: {
			required: "<p><img src='/qcms/qCmsRepository/Global/largeBusiness/images/thumbnail-icons/bmg_ico_alert.gif' alt='Icon: Alert' /><label for='fedGovInterestArea'> Select an Area of Interest.</label></p>"
			},
			fedGovContractQuest: {
			required: "<p><img src='/qcms/qCmsRepository/Global/largeBusiness/images/thumbnail-icons/bmg_ico_alert.gif' alt='Icon: Alert' /> <label for='fedGovContractQuest'> Select a Contract.</label></p>"
			},
			fedGovContractQuestMAA: {
			required: "<p><img src='/qcms/qCmsRepository/Global/largeBusiness/images/thumbnail-icons/bmg_ico_alert.gif' alt='Icon: Alert' /><label for='fedGovContractQuestMAA'> Select a Metro Area.</label></p>"
			},
			quesType: {
			required: "<p><img src='/qcms/qCmsRepository/Global/largeBusiness/images/thumbnail-icons/bmg_ico_alert.gif' alt='Icon: Alert' /><label for='quesType'>  Select any one Qwest Channel</label></p>"
			}
		}
	});
	
	$(blockContainer).siblings().find("div.bmg_errorContainer").filter(":not:empty").css("border","1px solid red");

	if ($("form#bmg_mainForm").valid()) {
		isValid(n, catId, blockContainer,valContainer,thisVal,actionURL);
		formIsValid = true;
	}
	else {
	$(blockContainer).siblings().find("div.bmg_errorContainer").hide();
	$(blockContainer).find("div.bmg_errorContainer").show();
		formIsValid = false;
		if (hasChoices>=0) {
			$(blockContainer).find("input:radio:first").parent("p").prev("p").after($("div.error"))
		}
	}
}


////////////////////////////////////////
//Builds args, and submits forms only if the select option does not route to an external link
////////////////////////////////////////
function sendValues(n, catId, blockContainer, valContainer,actionURL){
    log.info("sendValues", "Calling sendValues", 5);
    var catNameIndex = $("form[name=bmg_mainForm]").find("h2.catName").index($(blockContainer).find("h2"));
    catNameIndex = catNameIndex + 1;
    args = "";
	baseURL="";
	 $(valContainer).value="";
    if ($(blockContainer).find("select.interest").length > 0) {
        if (catNameIndex == 1) {
            args += "Public/Private Company~" + $("div:visible", blockContainer).children("select:first").val() + "~" + $("div:visible", blockContainer).children("select:first").find("option:selected").attr("dbid") + ";";
			}
        else 
            if (catNameIndex == 2) {
			args += "State/Local Gov/Education~" + $("div:visible", blockContainer).children("select:first").val() + "~" + $("div:visible", blockContainer).children("select:first").find("option:selected").attr("dbid") + ";";
			}
            else 
                if (catNameIndex == 3) {
                    var thisText;
                    var visSelLen = $("div:visible", blockContainer).children("select").length;
                    $("div:visible", blockContainer).children("select:last").find("option:selected").each(function(){
                        thisText = $(this).text();
                        thisDbid = $(this).attr("dbid");
                        if (visSelLen == 1) {
                            args += "Federal~" + thisText + "~" + thisDbid + ";";
                        }
                        else 
                            if (visSelLen == 2) {
                                args += "Federal Contract~" + thisText + "~" + thisDbid + ";";
                            }
                            else 
                                if (visSelLen == 3) {
                                    args += "Federal MAA~" + thisText + "~" + thisDbid + ";";
                                }
                    });
                }
        // 	args+=catId+"|";
        args += (catNameIndex) + ",";
    }
    
    if ($(blockContainer).find("input[name=quesType]").is(":checked")) {
		args += catId + "~";
		args += $(blockContainer).find("input[name=quesType]:checked").next().text()+ "~-1;";
        args += (catNameIndex) + ",";
        args += $(blockContainer).find("input[name=quesType]:checked").attr("id") + ";";
        args += $(blockContainer).find("input[name=quesType]:checked").val() + ";";
        args += $("form[name=bmg_mainForm]").find("input[name=quesType]").index($(blockContainer).find("input[name=quesType]:checked")) + 1 + ",";
    }
    if ($(blockContainer).find("div").hasClass("intState")) {
        if ($(blockContainer).find("select.interest").length > 0) {
            args += $(blockContainer).find("select.interest").children("option:selected").attr("id") + ";";
            args += $(blockContainer).find("select.interest").val() + ";";
            args += $(blockContainer).find("select.interest").get(0).selectedIndex + ",";
        }

        if ($(blockContainer).find("select.state").length > 0) {
            args += "-1" + ";";
            args += $(blockContainer).find("select.state").children("option:selected").val() + ";";
            args += $(blockContainer).find("select.state").children("option:selected").attr("region") + ";";
            args += $(blockContainer).find("select.state").get(0).selectedIndex + ",";
        }
    }

	if ($("div.fedGov", blockContainer).find("select").parent("div").is(":visible")) {
        $("div:visible", blockContainer).children("select").find("option:selected").each(function(){
            args += $(this).attr("id") + ";";
            args += $(this).val() + ";";
            args += $(this).parent("select").get(0).selectedIndex + 1 + ",";
        });
    }

    if (!external) {
        $(valContainer).val(args);
        hiddenParams = $(valContainer).val();
		//var baseURL = $("form[name=bmg_mainForm]").attr("action");
        baseURL += actionURL+"?&parameters=" + hiddenParams;
//        document.bmg_mainForm.action = baseURL;
		$("#bmg_mainForm").attr("action",baseURL)
		//alert(actionURL)
		log.info("sendValues","Form action: " + $("form[name=bmg_mainForm]").attr("action"),3);
		log.info("sendValues","Value in hidden field: " + hiddenParams,3);		
       // baseURL=actionURL;
		$("form[name=bmg_mainForm]").submit();
        //return false;
    }
}

//Get the basic tooltip structure ready
var tooltipCont = "<div class='tf_toolTip tf_tool'><div class='tf_toolTip_container'><div class='tf_toolTip_title tf_toolTip_title_smaller'><div><h3 class='headingAlign'></h3></div></div><div class='tf_toolTip_cont'><p class='preSale'></p><div class='bmg_clear'></div></div></div></div>"

function genTooltip(n){
    log.info("genTooltip", "Calling genTooltip", 5);
    $("div.tf_tool").remove();	
	tooltipShown = true;
	blockContainer = $(n).parents(".tooltip_block_container");
    var thisIndex = $("a.tooltip", blockContainer).index(n) + 1; // the index from all the tooltip trigger links
    var thisWidth = $(n).width();
    var thisHeight = $(n).height();
    var thisPos = $(n).position();
	var thisOffset = $(n).offset();
    var $this = $(n);
    log.info("genTooltip", "Tooltip xml: " + xmlURL, 2)
	try {
		if (productLanding) {
			xmlURL = "/qcms/qCmsRepository/Global/largeBusiness/xml/tooltipProducts.xml"; // load the products landing xml
		    tipParentText = $(n).text();
		}
	    else if(contactUs) {	

			xmlURL = "/qcms/qCmsRepository/Global/largeBusiness/xml/tooltip.xml"; //load the contact-us landing page xml
			tipParentText = $(n).prev().text();
		}
	}
	catch(err) {
		log.error("genTooltip","JS: Error: "+err);
	}
	    $.ajax({
        type: "GET",
        url: xmlURL,
		cache: true,//use the cached XML for subsequent calls
        dataType: "xml",
        success: function(xml){
			
            log.info("genTooltip", "Tooltip xml loaded successfully: " + xmlURL, 2)
            var tipContent;
            var tipTitle;
            $(xml).find('tooltipContainer tooltip').each(function(){
			var tipId = $(this).attr('id');
			tipContent = $(this).parent('tooltipContainer').children("tooltip[id=tooltip_" + thisIndex + "]").find('content').text();
			tipTitle = $(this).parent().children("tooltip[id=tooltip_" + thisIndex + "]").find('title').text();
            });
			//inject the tooltip into the DOM
			$("body").append(tooltipCont);
			var winWidth1 = $(window).width(); // new tooltip width
			var winHeight1 = $(window).height(); // new tooltip height
			var tooltip = $("div.tf_tool"); //the tooltip container
            var tooltipWidth = $(tooltip).width(); 
            var tooltipHeight = $(tooltip).height();
            var tooltipPos = $(tooltip).position();
            var tooltipLeft = tooltipPos.left;
			//the default positioning of the tooltip
				$(tooltip).css({
					height: tooltipHeight,
                    left: (thisOffset.left) + (thisWidth),
                    top: thisOffset.top - 35
                });
			//insert content into the tooltip 
			$(tooltip).find("div.tf_toolTip_cont p").append(tipContent);
			if (productLanding) {
	            $(tooltip).find("div.tf_toolTip_title h3").append(tipParentText);							
			}

			else if(contactUs) {
				$(tooltip).find("div.tf_toolTip_title h3").append(tipTitle);			
			}
			
			//new tooltip dimensions after content feed
				var newTooltipHeight = $("div.tf_toolTip").height();
				var newTooltipWidth = $("div.tf_toolTip").width();
			// get the browser-agnostic scrollTop and scrollLeft positions
			if ( document.documentElement && (document.documentElement.scrollTop)) {
				browserTopY = document.documentElement.scrollTop; 
			} else {
				browserTopY = document.body.scrollTop; 
			}
			
			if ( document.documentElement && (document.documentElement.scrollLeft)) {     
				browserLeftX = document.documentElement.scrollLeft; 
			} else {
				browserLeftX = document.body.scrollLeft; 
			}
		
			var thisOffset1 = $(n).offset(); //new offset iof the tooltip trigger
			var thisPosition1 = $(n).position(); 
			//calculate the space for the 4 areas
			var leftSpace = thisOffset1.left - browserLeftX;
			var rightSpace = winWidth1 - ((thisOffset1.left + thisWidth) - browserLeftX);
			var topSpace = thisOffset1.top - browserTopY;
			var bottomSpace = winHeight1 - (thisOffset1.top - browserTopY);
			//get booleans per area
			var isRight = (newTooltipWidth <= rightSpace);
			var isLeft = (newTooltipWidth <= leftSpace);
			var isTop = (newTooltipHeight <= topSpace);
			var isBottom = (newTooltipHeight <= bottomSpace);

			if(isRight && (newTooltipHeight <= winHeight1 - ($(tooltip).offset().top - browserTopY)) && (topSpace > 35)) {
				//alert("1");
				$(tooltip).addClass('tf_toolTip').removeClass('tf_toolTip3, tf_toolTip1, tf_toolTip2')
				$(tooltip).css({
                    left: (thisOffset1.left+thisWidth)
                });
			}
			else if(isLeft && ((newTooltipHeight <= winHeight1 - ($(tooltip).offset().top - browserTopY))) && (topSpace > 35)) {
				//alert("2");
				$(tooltip).addClass('tf_toolTip1').removeClass('tf_toolTip, tf_toolTip3, tf_toolTip2')
				$(tooltip).css({
                    left: (thisOffset1.left)-(tooltipWidth)
                });
			}
			else if(isTop && ($(tooltip).offset().left - (tooltipWidth/2) - (thisWidth/2) - browserLeftX >= 0) && (rightSpace >= tooltipWidth/2 - thisWidth/2)){
				//alert("3");
				$(tooltip).addClass('tf_toolTip3').removeClass('tf_toolTip, tf_toolTip1, tf_toolTip2')
				//alert(tooltipHeight);
				$(tooltip).css({
                    left: (thisOffset1.left - ((tooltipWidth/2) - (thisWidth/2))),
					top: (thisOffset1.top - newTooltipHeight-5)
                });
			}
			else if(isBottom && ($(tooltip).offset().left - (tooltipWidth/2) - (thisWidth/2) - browserLeftX >= 0) && (rightSpace >= tooltipWidth/2 - thisWidth/2)){
				//alert("4");
				$(tooltip).addClass('tf_toolTip2').removeClass('tf_toolTip, tf_toolTip1, tf_toolTip3');
				$(tooltip).css({
                    left: (thisOffset1.left - (tooltipWidth/2) + (thisWidth/2)),
					top: (thisOffset1.top+thisHeight)
                });
			}
			else {
//				alert("5")
				window.resizeBy(tooltipWidth+20,thisHeight+tooltipHeight+20);
				$(tooltip).css({
                    left: (thisOffset1.left+(thisWidth))
                });

				$(tooltip).addClass('tf_toolTip').removeClass('tf_toolTip3, tf_toolTip1, tf_toolTip2')
//				$(tooltip).find("div.tf_toolTip_title h3").html("Browser Window Size")
//				$(tooltip).find("div.tf_toolTip_cont p").html("<span style='color:#ff0000'>Resizing the browser to enable viewing this content.<br> Please hover over the link again.</span>");
			}
        },
        failure: function(msg){
            log.error("","Ajax failure: "+msg)
        }
    });
}

function desTooltip(n){
    log.info("desTooltip", "Calling desTooltip", 5);
    $("div.tf_toolTip, div.tf_toolTip1,div.tf_toolTip2, div.tf_toolTip3").remove();
	tooltipShown=false;
}
	var nextCatContent;
	var accordOpened = false;


function openAccord(n){
	//remove the class 'hidden' to enable validation
	$(n).parents(".catBlock").find("select, input").removeClass("hidden");
    log.info("openAccord", "Calling openAccord", 5);
    nextCatContent = $(n).next("div.catContent");
	$(n).parents("div.catBlock").siblings().find("div.initHidden").hide();	//hide the selects that are initially hidden
	//reset selects to defaults
	$(n).parents("div.catBlock").siblings().find("select").each(function() {
		$(this).val($("option:first",this).text());
	});
	//reset radio buttons to default
	$(n).parents("div.catBlock").siblings().find("input:radio").each(function() {
		$(this).attr("checked",false);
	});

	if (nextCatContent.is("visible")) {
		accordOpened = true;
	}
	    $("form[id=bmg_mainForm]").find("div.catContent").not(nextCatContent).each(function(i){
        $(this).slideUp("fast");
    });
	$(n).parents("div.catBlock").siblings().removeClass("bmg_theme_action_call_wht_contact").addClass("bmg_theme_action_call_wht_contact_off")
    $(n).next("div.catContent").slideDown("fast");

    log.info("openAccord", "Is the form Valid: "+formIsValid, 5);

	if (!formIsValid) {
		try {
			$('#bmg_mainForm')[0].reset();
			validator.resetForm();
			//bmg_mainForm.submit = {};
			$(n).parents("div.catBlock").siblings().find("div.initHidden").hide();
		}
		catch(err) {
			log.error("Error: "+err.message);
		}		
	}
}

var defWinWidth;


function showHover(n) {						  
	var isVisible = $(n).find("div.catContent").is(":visible");
	if(!isVisible) {
		$(n).removeClass("bmg_theme_action_call_wht_contact_off");
		$(n).addClass("bmg_theme_action_call_wht_contact");
	}
	else {	
		$(n).removeClass("bmg_theme_action_call_wht_contact_off");
		$(n).addClass("bmg_theme_action_call_wht_contact");
	}
}


function hideHover(n) {
	var isVisible = $(n).find("div.catContent").is(":visible")		
	if(!isVisible) {
		$(n).removeClass("bmg_theme_action_call_wht_contact");
		$(n).addClass("bmg_theme_action_call_wht_contact_off");
	}
	else {
		$(n).removeClass("bmg_theme_action_call_wht_contact_off");
		$(n).addClass("bmg_theme_action_call_wht_contact");
	}
}
$(document).ready(function(){
	var actionURL = $("form[name=bmg_mainForm]").attr("action");
					   
    var valContainer = $("form[name=bmg_mainForm]").find("input:hidden");
	$("div.fedGov select").each(function() {
		$("option",this).each(function() {							   
			if($(this).val()=="Contact Qwest team for your agency") {
				$(this).addClass("external agencyContact");
			}
			if($(this).val()=="Networx") {
				$(this).addClass("external netContracts");
			}
		});					 
	});
    $("a.popup").click(function(e, n){
        e.preventDefault();
    });    
    openAccord();
	 $("h2.catName").click(function(n){
        openAccord(this);
    });

    $("a.tooltip").hoverIntent(function(n){
		if(!tooltipShown) {									
        	genTooltip(this);
		}
    }, function(){
        desTooltip(this);
    });
/*	  $("a.tooltip").focus(function(n){
		if(!tooltipShown) {						
      	  genTooltip(this);
		};
    });

	$("a.tooltip").blur(function(n){
		desTooltip(this);
	});
*/    $("a.bmg_arrow_icon_grLt").click(function(){
	    var valContainer = $("form[name=bmg_mainForm]").find("input:hidden");
	//	$(valContainer).value="";
         validateThis(this,valContainer,actionURL)
    });
	$("div.catBlock").hover(function (n) {
		showHover(this);		
      }, 
      function (n) {
		hideHover(this);
      });
	$("div.catBlock").click(function (n) {
		showHover(this);		
	  }, 
	  function (n) {
		hideHover(this);
	  });

    $("select#fedGovInterestArea").change(function(){
        if ($(this).children('option:selected').text() == "Contract Questions") {
			$(this, 'div#sf').parents("tr").next().show();
            $("div#FG_content_Contract").show();
			$("div#FG_content_MAA").hide();
			$("select#fedGovContractQuest").removeClass("hidden");
			$("select#fedGovContractQuest").val($("select#fedGovContractQuest option:first").text());
			$("select#fedGovContractQuestMAA").val($("select#fedGovContractQuestMAA option:first").text());
			fedContract=true;
        }

        else {
		// reset select fields that appear after this
			$(this, 'div#sf').nextAll().find("select").each(function(i) {
				$(this).val($("option:first",this).text());
			});
			$("select#fedGovContractQuest").val($("select#fedGovContractQuest option:last").text());
			$("select#fedGovContractQuestMAA").val($("select#fedGovContractQuestMAA option:last").text());// force a valid value to enable the count to be updated as this will be hidden with the error still in place
			$("select#fedGovContractQuest, select#fedGovContractQuestMAA").addClass("hidden");
			 $("select#fedGovContractQuest, select#fedGovContractQuestMAA").valid();

            $(this, 'div#sf').nextAll().hide();
			 $(this, 'div#sf').parents("tr").next().hide();
			
        }
		if (!formIsValid) {
			try {
				validator.resetForm();
				validator.prepareForm();
				validator.hideErrors();
			}
			catch(err) {
				log.error("Error: "+err.message);
			}		
		}
	});

    $("select#fedGovContractQuest").change(function(){
        if ($(this).val() == "Metropolitan Area Acquisition (MAA)") {
            $("div#FG_content_MAA").show();
			$("select#fedGovContractQuestMAA").removeClass("hidden");
			$("select#fedGovContractQuestMAA").val($("select#fedGovContractQuestMAA option:first").text());	
        }
        else {
		// reset select fields that appear after this
			 $(this).parent().nextAll().find("select").each(function(i) {
				$(this).val($("option:first",this).text());
			});
			$("select#fedGovContractQuestMAA").addClass("hidden");
			$("select#fedGovContractQuestMAA").val($("select#fedGovContractQuestMAA option:last").text()); // force a valid value to enable the count to be updated as this will be hidden with the error still in place
            $("div#FG_content_MAA").hide();			
			$("select#fedGovContractQuestMAA").valid();	 
        }
		if (!formIsValid) {
			try {
				validator.resetForm();
				validator.prepareForm();
				validator.hideErrors();
			}
			catch(err) {
				log.error("Error: "+err.message);
			}		
		}
    });
    openCat();
});