// JavaScript Document

$(document).ready(function() {
	clearSessionMem();
	showDonationOptions();
	setDT();
	hideCodes();
	hidePubMessage();
	hideLocalMessage();
	addClickEvent();
	creditParticpant();
	setPersonalPageVal();
	setPrizeVal();
	setSupportedLocale();
	hideEmailSummaryColumns();
	hideDonationSummaryColumns();
	resetFeatureTitles();
	addUsernamePasswordInfo();
	udpateConfirmationInfo();
	udpateLabels();
	addFieldInstructions();
	disabledMenu();
	vSearchStyles();
	removeTextLabels();
});

function addClickEvent(){
	if($("#FormDonation").length > 0){
		var obj = $("#FormDonation input[name='SubmitButton']");
	}else if($("#ContactForm").length > 0){
		var obj = $("#ContactForm input[name='SubmitButton']");
	}else if($("#registrationForm").length > 0){
		var obj = $("#registrationForm input[name='imageField2']");		
	}else if($("form[name^='SUPPORTER']").length > 0){
		var obj = $("form[name^='SUPPORTER'] input[name='SubmitButtom']");	
	}else if($("form[action^='updateProfile.asp']").length > 0){ 
		var obj = $("form[action^='updateProfile.asp'] input[src$='images/buttons/btn_updateSilver.gif']");	
	}else if($("form[name='billingForm']").length > 0){
		var obj = $("form[name='billingForm'] input[src$='imagesSSL/buttons/submitsilver.gif']");
	}else if($("#FormSupporter").length > 0){
		var obj = $("#FormSupporter input[src$='imagesSSL/buttons/submitsilver.gif']");	
	} else {
		return false;
	}
	
	var origEvt = obj.attr("onclick");
	obj.attr("onclick","");
	obj.click(function() {
		getXML(); // first pass set default values
		getXML(); // second pass set overide values for event fields
		pauseJS(1500);
		if (origEvt) {
			origEvt.apply(this);
		}
	});
}

function getXML(){
		$.ajax({
		type: "GET",
		url: "/atf/cf/{282e45d9-a03a-49d1-883c-39f4feaf7246}/POSTAL_CODES.XML",
		dataType: "xml",
		async: false,
		success: parseXml
	});
}

var parseXMLCount = 0;

function parseXml(xml){
	str = $("#mainContent input[name='zip']").val();
	queryAttr = "PostalCodeBeginswith";
	
	if (parseXMLCount == 1){
		if (typeof(sessvars.personalPageLocation) != "undefined"){
			if (sessvars.personalPageLocation.length > 2){
				str = sessvars.personalPageLocation;
				queryAttr = "ChapterCode";
			}
		} else if ( typeof(sessvars.paticipantLocale) != "undefined" ){
			if (sessvars.paticipantLocale.length > 2){
				str = sessvars.paticipantLocale;
				queryAttr = "ChapterCode";
			}
		} else if ($("#eventChapter").length > 0){
			str = $("#eventChapterCode").text();
			queryAttr = "ChapterCode";
		}else{
			parseXMLCount = 0;
		}
	}
	
	$(xml).find("PostalData").each(function(){		
		var checkCode = $(this).find(queryAttr).text();
		var RegExPattern = "^" + checkCode;
		var RegularExpression  =  new RegExp(RegExPattern,"i");
		var xmatch = RegularExpression.test(str);
		
		if(xmatch){
			var chapterCode = $(this).find('ChapterCode').text();
			var chapterName = $(this).find('Chapter').text();
			var regionName = $(this).find('Region').text();
			var regionID = $(this).find('RegionID').text();
			var regionDesc = $(this).find('RegionDescription').text();
			var districtID = $(this).find('DistrictID').text();
			var districtDesc = $(this).find('DistrictDescription').text();

			if ($("#postalCodeVerify").length > 0){
				setThirdPartyCodes(chapterCode,chapterName);
				return false;
			}

			if (parseXMLCount == 0){
				setGeneralCodes(chapterCode,chapterName,districtID,districtDesc,regionID,regionDesc);
				parseXMLCount++;
				return false;
			}
			
			if (parseXMLCount == 1){
				setOverideCodes(chapterCode,chapterName,districtID,districtDesc,regionID,regionDesc);
				return false;
			}
		}
	});
}

function setGeneralCodes(chapterCode,chapterName,districtID,districtDesc,regionID,regionDesc){
	if(regionID == ""){rCode = "";}else{rCode = regionID + "-" + regionDesc;}
	if(districtID == ""){dCode = "";}else{dCode = districtID + "-" + districtDesc;}	
	if(chapterCode == ""){cCode = "";}else{cCode = chapterCode + "-" + chapterName;}	
	
// code for standard forms (contact, registration, donation) - text field
	$("input[name='Field5091022']").val(cCode);
	$("input[name='Field5106936']").val(dCode);
	$("input[name='Field5106937']").val(rCode);
		
	$("input[name='Field5091023']").val(cCode);			
	$("input[name='Field5106939']").val(dCode);
	$("input[name='Field5106938']").val(rCode);
	
	// code for standard forms (contact, registration, donation) - select field	
	$("select[name='Field5121942']").val(cCode);
	$("select[name='Field5121941']").val(dCode);
	$("select[name='Field5121940']").val(rCode);
	
	$("select[name='Field5121952']").val(cCode);			
	$("select[name='Field5121951']").val(dCode);
	$("select[name='Field5121950']").val(rCode);
	
	// code for faf forms 
	$("input[name='custom_5091022']").val(cCode);
	$("input[name='custom_5106936']").val(dCode);
	$("input[name='custom_5106937']").val(rCode);
	
	$("input[name='custom_5091023']").val(cCode);			
	$("input[name='custom_5106939']").val(dCode);
	$("input[name='custom_5106938']").val(rCode);
	
	// // code for faf forms) - select field	
	$("select[name='custom_5121942']").val(cCode);
	$("select[name='custom_5121941']").val(dCode);
	$("select[name='custom_5121940']").val(rCode);
	
	$("select[name='custom_5121952']").val(cCode);			
	$("select[name='custom_5121951']").val(dCode);
	$("select[name='custom_5121950']").val(rCode);
}

function setOverideCodes(chapterCode,chapterName,districtID,districtDesc,regionID,regionDesc){
	if(regionID == ""){rCode = "";}else{rCode = regionID + "-" + regionDesc;}
	if(districtID == ""){dCode = "";}else{dCode = districtID + "-" + districtDesc;}	
	if(chapterCode == ""){cCode = "";}else{cCode = chapterCode + "-" + chapterName;}	
	
	// code for faf forms
	$("input[name='custom_5091023']").val(cCode);			
	$("input[name='custom_5106939']").val(dCode);
	$("input[name='custom_5106938']").val(rCode);
	
	// code for CFAF forms 
	$("input[name='Field5091023']").val(cCode);			
	$("input[name='Field5106939']").val(dCode);
	$("input[name='Field5106938']").val(rCode);
	
	// code for faf forms - select field	
	$("select[name='custom_5121952']").val(cCode);			
	$("select[name='custom_5121951']").val(dCode);
	$("select[name='custom_5121950']").val(rCode);
	
	// code for CFAF forms - select field	
	$("select[name='Field5121952']").val(cCode);			
	$("select[name='Field5121951']").val(dCode);
	$("select[name='Field5121950']").val(rCode);
}

function setThirdPartyCodes(chapterCode,chapterName){
	// code for cfaf - third party events
	sessvars.paticipantLocale = chapterCode + " - " + chapterName;
	sessvars.paticipantZip = $("input[name='zip']").val();
}

function hideCodes(){
	if($("form[name='billingForm']").length < 1){
		// code for standard forms (contact, registration, donation)
		$("input[name='Field5091022']").closest("tr").hide();
		$("input[name='Field5106936']").closest("tr").hide();
		$("input[name='Field5106937']").closest("tr").hide();
		
		$("input[name='Field5091023']").closest("tr").hide();
		$("input[name='Field5106938']").closest("tr").hide();
		$("input[name='Field5106939']").closest("tr").hide();
		
		// code for standard forms (contact, registration, donation) - select
		$("select[name='Field5121942']").closest("tr").hide();
		$("select[name='Field5121941']").closest("tr").hide();
		$("select[name='Field5121940']").closest("tr").hide();
		
		$("select[name='Field5121952']").closest("tr").hide();		
		$("select[name='Field5121951']").closest("tr").hide();
		$("select[name='Field5121950']").closest("tr").hide();
		
		// code for FAF
		$("input[name='custom_5091022']").closest("tr").hide();
		$("input[name='custom_5106936']").closest("tr").hide();
		$("input[name='custom_5106937']").closest("tr").hide();
		
		$("input[name='custom_5091023']").closest("tr").hide();
		$("input[name='custom_5106938']").closest("tr").hide();
		$("input[name='custom_5106939']").closest("tr").hide();
		
		// code for FAF - select
		$("select[name='custom_5121942']").closest("tr").hide();
		$("select[name='custom_5121941']").closest("tr").hide();
		$("select[name='custom_5121940']").closest("tr").hide();
		
		$("select[name='custom_5121952']").closest("tr").hide();		
		$("select[name='custom_5121951']").closest("tr").hide();
		$("select[name='custom_5121950']").closest("tr").hide();
			
		//Particpant Credited
		$("input[name='Field5091024']").closest("tr").hide();
		
		//Supported Locations/Segments
		$("input[name='Field5113365']").closest("tr").hide();
		$("input[name='Field5113344']").closest("tr").hide();
		$("input[name='Field5113366']").closest("tr").hide();
		$("input[name='Field5100140']").closest("tr").hide();
		$("input[name='SegmentID']").closest(".CTtablein").hide();
	}
}

function creditParticpant(){
	if ($("input[name='Field5091024']").length > 0){
		var pCredit = "";
		
		if ($("input[name='participantSupporterId']").length > 0){
			var particpantCredited = $("input[name='participantSupporterId']").val();
		}else if(sessvars.personalPageSID){
			var particpantCredited = sessvars.personalPageSID;
		}else{
			particpantCredited = 0;
		}
		
		if ($("input[name='teamID']").length > 0){
			var teamCredited = $("input[name='teamID']").val();
		}else if(sessvars.personalTeamSID){
			var teamCredited = sessvars.personalTeamSID;
		}else if(sessvars.teamPageSID){
			var teamCredited = sessvars.teamPageSID;
		} else {
			teamCredited = 0;
		}
		
		if ($("input[name='participantFullName']").length > 0){
			var participantName = $("input[name='participantFullName']").val();
		}else if(sessvars.personalPageFNAME && sessvars.personalPageLNAME){
			var participantName = sessvars.personalPageFNAME + " " + sessvars.personalPageLNAME;
		}else if(sessvars.teamPageName){
			var participantName = sessvars.teamPageName;
		}
		
		if (particpantCredited != 0){
			pCredit = particpantCredited + " - " + participantName;
		}else if(teamCredited != 0){
			pCredit = teamCredited + " - " + participantName;
		}
		
		$("input[name='Field5091024']").val(pCredit);
	}
}

function pauseJS(timeInMilliS){
	var date = new Date();
	var curDate = null;
	do {
		curDate = new Date();
	} while(curDate-date < timeInMilliS);
}

function showDonationOptions(){
	if($("#FormDonation").length > 0){
		$("#donationType").show();
	}else{
		$("#donationType").hide();
	}
}

function selectDT(){
	var baseURL = "/site/lookup.asp?c=ajIRK4NLLhJ0E&b=";
	$("input[name='dtype']:checked").each(function(){
		var URL = baseURL + $(this).val();
		document.location.href = URL;
  	});
}

function setDT(){
	var formName = $("#FormDonation").attr("name");
	$("input[name='dtype']").each(function(){
		var tFormName = "SUPPORTER" + $(this).attr("id");
		if (formName == tFormName){
			$(this).attr("checked",true);
		}										   
  	});
}

/* JavaScript for CFAF (HNW/Third Party Events)*/

//sessvars.$.debug();
sessvars.$.prefs.crossDomain = true;

function setLocales(){
	sessvars.paticipantLocale = $("#tpLocales option:selected").val();
	var strVal = $("#tpLocales option:selected").val();
	showLocalMessage(strVal);
}

function setPostalLocale(){
	getXML();
	pauseJS(1500);
	$("#registrationSteps").show();
}

function setSupportedLocale(){
	if($("input[name='Field5113365']").length > 0 && identValEV == "HNW"){
		$("input[name='Field5113365']").val(sessvars.paticipantLocale);
	}
	if($("input[name='Field5113344']").length > 0 && identValEV == "ThirdParty"){
		$("input[name='Field5113344']").val(sessvars.paticipantLocale);
	}
	if($("input[name='Field5113366']").length > 0 && identValEV == "Other"){
		$("input[name='Field5113366']").val(sessvars.paticipantLocale);
	}
	if($("input[name='SegmentID']").length > 0){
		if(sessvars.paticipantLocale != "" || sessvars.personalPageLocation !=""){
			var str = "";
			if (sessvars.paticipantLocale){
				str = sessvars.paticipantLocale;
			} else {
				str = sessvars.personalPageLocation;
			}
			var RegExPattern = str;
			var RegularExpression  =  new RegExp(RegExPattern,"i");
			$("input[name='SegmentID']").each(function(){
				var locVal = $(this).closest("td").next("td").text();
				var xmatch = RegularExpression.test(locVal);
				if (xmatch){
					$(this).attr("checked",true);
				}										   
			});
		}
	}
}


function setPersonalPageVal(){
	var pageStr = "ShowPage.aspx";
	var RegExPattern = pageStr;
	var RegularExpression  =  new RegExp(RegExPattern,"i");
	var xmatch = RegularExpression.test(document.URL);
	if (xmatch){
		sessvars.personalPageType = pageType;
		if(evField1 != "" && identValEV == "ThirdParty"){
			sessvars.personalPageLocation = evField1;
			sessvars.paticipantLocale = evField1;
		}
		if(evField2 != "" && identValEV == "HNW"){
			sessvars.personalPageLocation = evField2;
			//sessvars.paticipantLocale = evField2;
		}
		if(evField3 != "" && identValEV == "Other"){
			sessvars.personalPageLocation = evField3;
			sessvars.paticipantLocale = evField3;
		}
		showLocalMessage(sessvars.personalPageLocation);
		if(sessvars.personalPageType == "individual"){
			sessvars.personalPageSID = personalSupporterID;
			sessvars.personalPageFNAME = personalFirstName;
			sessvars.personalPageLNAME = personalLastName;
			$(".linkDonate").show();
		}else if(sessvars.personalPageType == "team"){
			sessvars.teamPageSID = teamID;
			sessvars.teamPageName = teamName;
			$(".linkDonate").show();
			if(sessvars.paticipantLocale){
				$(".linkMember").show();
			}
		}
	}
}

function setPrizeVal(){
	var pageStr = "Prizes.htm";
	var RegExPattern = pageStr;
	var RegularExpression  =  new RegExp(RegExPattern,"i");
	var xmatch = RegularExpression.test(document.URL);
	if (xmatch){
		if(evField1 != "" && identValEV == "ThirdParty"){
			sessvars.personalPageLocation = evField1;
		}
		if(evField2 != "" && identValEV == "HNW"){
			sessvars.personalPageLocation = evField2;
		}
		if(evField3 != "" && identValEV == "Other"){
			sessvars.personalPageLocation = evField3;
		}
		showLocalMessage(sessvars.personalPageLocation);
		$(".linkPrize").show();
	}
}

function hideLocalMessage(){
	$("div[id^='eventMessage']").hide();
	$(".linkDonate,.linkMember,.linkPrize").hide();
	$("#registrationSteps").hide();

}

function showLocalMessage(x){
	hideLocalMessage();
	var strIdent = x.substring(0,3);
	var messageIdent = "#eventMessage" + strIdent;
	$(messageIdent).show();
}

function hidePubMessage(){
	$("p:contains('This content is not available yet.')").remove();
	$("p:contains('This content is no longer available.')").remove();
}

function hideEmailSummaryColumns(){
	$("table.listren-listtbl tr td:contains('Total Catalog Purchases')").closest("table").attr("id","emSummary");
	$("#emSummary td:nth-child(7)").hide();
}

function hideDonationSummaryColumns(){
	$("table.listren-listtbl tr td:contains('Purchases Received')").closest("table").attr("id","dnSummary");
	$("#dnSummary td:nth-child(5)").hide();
}

function resetFeatureTitles(){
	$("strong:contains('Top Teams:')").html("<strong>Top Events:<br></strong>")
}

function clearSessionMem(){
	var myFeatureID = new Array("Home","JoinParticipate","Search_Participants","Search_Teams","General_Donation","Start_an_Event");
	$.each(myFeatureID, function(index,value){
		var pageStr = value;
		var RegExPattern = pageStr;
		var RegularExpression  =  new RegExp(RegExPattern,"i");
		var xmatch = RegularExpression.test(document.URL);
		if (xmatch){
			sessvars.$.clearMem();
		}
	});
}

function addUsernamePasswordInfo(){
	$("tr.CTsubtitle td b font:contains('Create Username and Password')").html("<span>Create Username and Password</span><br><span style='font-weight:normal'>(Password must be 5 or more characters)</span>");
}

function udpateConfirmationInfo(){
	$("tr td.CTtitle b font:contains('Confirmation')").closest("tr").remove();
}

function udpateLabels(){
	$(".FAFBodyTable table tr td:contains('Step 1 of 2: input your info')").html("Step 1 of 2: Input Your Information");
	$(".FAFBodyTable table tr td:contains('Step 2 of 2: review your order info')").html("Step 2 of 2: Review Your Order Information");
	$(".FAFBodyTable table tr td a:contains('Add Another Sponsorship Level')").html("Add Sponsorship Level or Update Billing Information");
}

function addFieldInstructions(){
	$("#interest,#availability,#skill,select[name='Field5127536']").after("<br><span style='font-size:smaller'>Keep the 'Control' key pressed to select more than one.</span>");
}

function disabledMenu(){
	$(".disabled").closest("ul").remove();
}

function vSearchStyles(){
	$("#volSearch table:eq(0) tr").css("background-color","#125A58");
	$("#volSearch table:eq(0) td").css("background-color","#125A58");
	$("#volSearch table:eq(0) tr td:has(a)").css("background-color","#5E9D9B");
	$("#volSearch table:eq(1) tr td:even").css("background-color","#E2F3F2");
}

function removeTextLabels(){
	$("#TablePledgeLevelInner font.CTtablein:contains('Level')").closest("tr").hide();
	$("select[name='Field5106970']").css("width","305");
}

function redirectPage(urlCheck,urlRedirect){
	var RegExPattern = urlCheck;
	var RegularExpression  =  new RegExp(RegExPattern,"i");
	var xmatch = RegularExpression.test(document.URL);
	if (xmatch){
		document.location.href = urlRedirect;
	}
}

function hideUrlBasedObject(urlCheck,obj){
	var RegExPattern = urlCheck;
	var RegularExpression  =  new RegExp(RegExPattern,"i");
	var xmatch = RegularExpression.test(document.URL);
	if (xmatch){
		$(obj).hide();
	}
}


