//
//this files should be placed at the end of the template
//translation table for js validation scripts
var locals = {
	"Please enter keywords or price to search."		: "@Please enter keywords or price to search.",
	"Please stand by..." 					: "S'il vous pla\u00EEt se par ...",
	"Please enter a correct format zip/postal code" 	: "S'il vous pla\u00EEt entrer un zip format correct / code postal",
	"Your credit card is expired - please reenter" 		: "Votre carte de cr\u00E9dit est expir\u00E9 - s\'il vous pla\u00EEt entrer \u00E0 nouveau",
	"Your CVV number is invalid - please reenter" 		: "Votre num\u00E9ro CVV est invalide - s'il vous pla\u00EEt entrer \u00E0 nouveau ",
	"Your credit card number is invalid - please reenter" 	: "Votre num\u00E9ro de carte de cr\u00E9dit n'est pas valide - s'il vous pla\u00EEt entrer \u00E0 nouveau",
	"Please enter the following fields to continue:" 	: "S'il vous pla\u00EEt entrer dans le champs ci-dessous pour continuer: ",
	"Please enter the special event you are commemorating." : "S.V.P. Veuillez indiquer l'\u00E9v\u00E9nement sp\u00E9cial que vous voulez comm\u00E9morer",
	"Please enter a first name for your tribute." 		: "S.V.P. Veuillez \u00E9crire un pr\u00E9nom pour votre hommage.",
	"Please enter a last name for your tribute." 		: "S.V.P., Veuillez \u00E9crire un nom pour votre hommage.",
	"Please fill out the required fields" 			: "S.V.P. veuillez remplir les champs requis",
	"The donation amount can not contain any punctuation." 	: "Le montant du don ne doit contenir aucune ponctuation.",
	"Please re-enter your donation amount." 		: "Veuillez entrer de nouveau le montant de votre don.",
	"A $0 donation cannot be entered." 			: "Entrer un don de plus de 0$.",
	"You have entered an invalid character in this field." 	: "Vous avez entr\u00E9 un caractère inadmissible dans ce champ.",
	"Please use the MM/DD/YYYY format without spaces." 	: "S.V.P. utiliserle format suivant MM/JJ/AAAA sans espace",
	"You have entered an invalid email address." 		: "Vous avez entr\u00E9 une adresse courriel invalide",
	"Please enter the year in YYYY format.  ex 1990" 	: "S.V.P. veuillez entrer l'ann\u00E9e dans le format suivant AAAA. Ex 1990",
	"Please enter a year later than 1752." 			: "Nous n'acceptons  pas de date avant 1753.  S.V.P. choisir une année ult\u00E9rieure.",
	"field requires at least"				: "Le champ exige au moins",
	"characters or more." 					: "caractères ou plus.",
	"This entry accepts only letters, periods, commas, hyphens and spaces." : "Cette entr\u00E9e accepte seulement des lettres, des p\u00E9riodes, des virgules, des traits d'union et des espaces.",
	"Please enter a donation amount greater than or equal to 5." 	        : "S.V.P.,Veuillez entrer un montant de 5$ ou plus.",
	"This entry must be a number.  Please remove all letters, special characters, and spaces." : "Cette entr\u00E9e doit être un nombre. Veuillez enlever tous les lettres, caractères sp\u00E9ciaux, et espaces.",
	"You have entered an invalid date or date format.  Please use the MM/DD/YYYY format without spaces.": "Vous avez entr\u00E9 un format de date invalide.  S'il vous pla\u00EEt utiliser le format suivant MM/JJ/AAAA.",
	"We do not support dates before 1753.  Please choose a later year and try again."	: "Nous n'acceptons pas de date avant 1753.  S.V.P. choisir une ann\u00E9e ult\u00E9rieure. ",
	"You have entered an invalid 'Area Code',Please try again."	        : "Vous avez entr\u00E9 un code r\u00E9gional invalide, s'il vous pla\u00EEt entrer \u00E0 nouveau.",
	"You have entered an invalid 'Phone Number', Please try again." 	: "Vous avez entr\u00E9 un numéro de t\u00E9l\u00E9phone invalide.  S'il vous pla\u00EEt, veuillez utiliser le format suivant xxx-xxx-xxxx.",
	"You have entered an invalid phone \\#.  Please re-enter your \\# in the form xxx-xxx-xxxx." 	    : "Vous avez entr\u00E9 un numéro de t\u00E9l\u00E9phone invalide.  S'il vous pla\u00EEt, veuillez utiliser le format suivant xxx-xxx-xxxx.",
	"Please enter a valid credit card.  We accept Visa, Mastercard, Discover or American Express only." : "S.V.P.,Veuillez entrer un num\u00E9ro de carte de cr\u00E9dit valide.  Nous acceptons Visa, Master Card et American Express seulement.",
	"For credit card donations a minimum donation of $5 is required" 	: "Pour les cartes de cr\u00E9dit, un don minimum de 5$ est requis",
	"formPointer.elements\\['addAmount'\\]" 		: "$('input[name=addAmount]')[0]",
	"fieldname.elements\\['addAmount'\\]" 			: "$('input[name=addAmount]')[0]",
	"Please enter a valid email address. Email addresses must include the @ sign and at least one period. (e.g. friendname@abc.com)"  : "Veuillez SVP entrer une adresse \u00E9lectronique valide. Les adresses \u00E9lectroniques doivent contenir le symbole @ et au moins un point "
}


//write to page so script will translate then hide
//for (var key in locals) {
//	$('form').append(key+"<br />");
//}
var labels = [];
	
$(document).ready(function(){
	$('#progressbar').progressBar();
	
	//perform translations
	//localize();
	//----
	//translate state dropdowns - seperate from standard translation
	//----
	getStates();
	//----
	//translate country dropdowns - seperate from standard translation
	//----
	getCountries();
});

function localize(tracex) {
	//get original value for all labels/titles
	var i = 0;
	$('.Alert').each(function(){
	   var oldlabel = $(this).closest('td').find('font:first');
	   if ($(oldlabel).find('label').length > 0) {
		  oldlabel = $(oldlabel).find('label').html();
	   } else {
		  oldlabel = $(oldlabel).html();
	   }
	   labels[i] = oldlabel;
	   labels[i] = labels[i].replace(":","");
	   i++;
	});
	
	//this is for FAF but shouldn't affect a regular page
	$('p font[color="#FF0000"]').closest('p').each(function(){
		var text = $(this).html().split(":");
	   labels[i] = text[0];
	   labels[i] = labels[i].replace(":","");
	   i++;
	});
	
	$('td font:contains(*)').each(function(){
	   var oldlabel = $(this).closest('td').find('font:first');
	   oldlabel = $(oldlabel).html();
	   labels[i] = oldlabel;
	   labels[i] = labels[i].replace(":","");
	   i++;
	});
	if (tracex == undefined) tracestat = false;
	else tracestat=tracex;
	$('body').translate('en', 'fr', {each: function(){this.progress("#progressbar")},not: '.notrans, #therm_dft, #sliderContent, .nav, #slide_viewer, #light, select[name=country], select[name=BillCountry], select[name=ShiptoCountry], select[name=state], select[name=BillState], select[name=ShiptoState], select[name=billing_state], select[name=billing_country]', google:false, table: 'sainte_justine', trace : tracestat, 
		complete: function(){
			//----
			//translate warning strings in validation scripts
			//----
			//if not an faf
			if (location.href.indexOf("/faf/") == -1) {
				$('script').each(function(){
					//standard cms forms have this check subroutine
					var checkform = new RegExp("CheckForm\\d{1,5}");
					var m = checkform.exec($(this).html());
					if (m != null) {
						if (m.index > 0) {
							doTranslate(this);
						}
					} else {
						//standard cms forms have this check subroutine
						var checkform = new RegExp("CheckForm");
						var m = checkform.exec($(this).html());
						if (m != null) {
							if (m.index > 0) {
								doTranslate(this);
							}
						}
						//estore search
						var checkform = new RegExp("DoSearch");
						var m = checkform.exec($(this).html());
						if (m != null) {
							if (m.index > 0) {
								doTranslate(this);
							}
						}	
				   }
				});
			} else {
				//in FAF translations are handled different
				$('script').each(function(){
					if (($(this).html().indexOf("javaEnabled") == -1)) {
						doTranslate(this);
					}
				});
				//correct bad html in FAF
				//FORM tag is written between the TABLE and TR tags so when the dom gets rewritten
				//the form tags get moved.
				//This code will put the tags back in the correct position
				if ((location.href.indexOf("donorPledge.asp") > 0) && (!$.browser.msie)) {
					var x = $('form[name=registrationForm]');
					$('form[name=registrationForm]').remove();
					$('.FAFBodyTable table:eq(6)').wrap(x);
				}
			}
			//setTimeout(function(){$('#fade').fadeOut();$('#light').slideUp();},600);
			//remove overlay
			$('#fade').hide('fast');
			$('#light').fadeOut();
			$('#local').show();
		}
	});
}

//this translates the warnings in the js validation
//by using the inputs that have class=Alert and replacing the warning text
function doTranslate(script) {
	var newjs = $(script).html();
	var i = 0;
	//go through all alert tags and get new label names then replace in the js warnings
	$('.Alert').each(function(){
		var newlabel = $(this).closest('td').find('font:first');
		if ($(newlabel).find('label').length > 0) {
		   newlabel = $(newlabel).find('label').html();
		} else {
		   newlabel = $(newlabel).html();
		}
		newlabel = newlabel.replace(":","");
		newjs = newjs.replace(labels[i]+'"',newlabel+'"');
		i++;
	});
	//if faf then get label tags
	$('p font[color="#FF0000"]').closest('p').each(function(){
		var text = $(this).html().split(":");
		var newlabel = text[0];
		newlabel = newlabel.replace(":","");
		newjs = newjs.replace(labels[i]+'"',newlabel+'"');
		i++;
	});
	//if faf get lable tags
	$('td font:contains(*)').each(function(){
		var newlabel = $(this).closest('td').find('font:first');
	   newlabel = $(newlabel).html();
		newlabel = newlabel.replace(":","");
		newjs = newjs.replace(labels[i]+'"',newlabel+'"');
		i++;
	});
	//got through locals array above for inline js translations
	for (var key in locals) {
		var nr = new RegExp(key,"g");
		newjs = newjs.replace(nr, locals[key]);
	}
	//remove comment tags and write new script files
	newjs = newjs.replace('<!--','');
	newjs = newjs.replace('//-->','');
	$('table:last').after("<sc"+"ript>"+newjs+"</sc"+"ript>");
}

//get states from db and replace separately from translate process
//these are large dropdowns and slow down the translation process
function getStates() {
	var opts = {};
	var url = "https://www.kinteratools.com/local/localize-states.php?callback=?";
	$.ajax({
		type : "GET",
		data : "data=",
		dataType : "json",
		url : url,
		contentType: "application/x-www-form-urlencoded;charset=ISO-8859-1",
		success : function(result){
			var opts = {};
			for (var key in result.data) {;
				opts[result.data[key].prefix] = {};
				opts[result.data[key].prefix] = result.data[key].to;
			}
			var selval = $('select[name=state]').val();
			$('select[name=state]').removeOption(0)
								.removeOption(/./)
								.addOption(opts,false)
								.selectOptions(selval,true);
			var selval = $('select[name=BillState]').val();
			$('select[name=BillState]').removeOption(0)
								.removeOption(/./)
								.addOption(opts,false)
								.selectOptions(selval,true);
			var selval = $('select[name=ShiptoState]').val();
			$('select[name=ShiptoState]').removeOption(0)
								.removeOption(/./)
								.addOption(opts,false)
								.selectOptions(selval,true);
			var selval = $('select[name=billing_state]').val();
			$('select[name=billing_state]').removeOption(0)
								.removeOption(/./)
								.addOption(opts,false)
								.selectOptions(selval,true);
		}
	});

}

//get countries from db and replace separately from translate process
//these are large dropdowns and slow down the translation process
function getCountries() {
	var opts = {};
	var url = "https://www.kinteratools.com/local/localize-countries.php?callback=?";
	$.ajax({
		type : "GET",
		data : "data=",
		dataType : "json",
		url : url,
		contentType: "application/x-www-form-urlencoded;charset=ISO-8859-1",
		success : function(result){
			var opts = {};
			for (var key in result.data) {
				opts[result.data[key].prefix] = {};
				opts[result.data[key].prefix] = result.data[key].to;
			}
			var selval = $('select[name=country]').val();
			$('select[name=country]').removeOption(0)
							.removeOption(/./)
							.addOption(opts,false)
							.selectOptions(selval,true);
			var selval = $('select[name=BillCountry]').val();
			$('select[name=BillCountry]').removeOption(0)
							.removeOption(/./)
							.addOption(opts,false)
							.selectOptions(selval,true);
			var selval = $('select[name=ShiptoCountry]').val();
			$('select[name=ShiptoCountry]').removeOption(0)
							.removeOption(/./)
							.addOption(opts,false)
							.selectOptions(selval,true);
			var selval = $('select[name=billing_country]').val();
			$('select[name=billing_country]').removeOption(0)
							.removeOption(/./)
							.addOption(opts,false)
							.selectOptions(selval,true);
		}
	});

}

//override current popup cvv window and point to translated page
var c;
function PopupCVV() {
	c = window.open('/atf/cf/{51785C68-280A-46C5-9D15-9C3886290AC4}/41_CVV.htm','file','scrollbars=yes,resizable=yes,width=640,height=480,left=100,top=100');
	c.focus();
}
function CloseCVV() {
	if (c) {	
		if (!c.closed) c.close();
		c = null;
	}
}
window.onfocus = CloseCVV;
