/*---[ NEWSLETTER ]---------------------------------------------------------------*/
function newsletter_action(language){
	var news_div =  document.getElementById('newsletter');
	var error_div =  document.getElementById('error_div');
	
	//var newsletterID = document.getElementById('newsletterID').value;
	//var lang = document.getElementById('lang').value;
	var email = document.getElementById('email').value;
	if (!validate_email(email)) {
		error = (language=='e') ? 'The email address provided is not in the right format' : "L'adresse du courriel soumis n'est pas dans le bon format." ;
		alert(error);
		return false;
	}

	
		
	/*
	if (document.getElementById('sub').checked ) {
		action = 0;
	} else if (document.getElementById('unsub').checked ) {
		action = 1;
	} else 	{
		var alertstr = (language=='e') ? "You must chose either Subscribe or Unsubscribe to the newsletter" : "Vous devez choisir soit: Enregistrer ou Retirer du bulletin" ;
		alert(alertstr);
		return false;
	}
	*/		
	
		var xmlHttp=null; 
		 try {
			// Firefox, Opera 8.0+, Safari, IE7+
			xmlHttp = new XMLHttpRequest(); // xmlHttp is now a XMLHttpRequest.
		 } catch (e) {
			// Internet Explorer
			try {
			   xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
			   xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
		 }
		 xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) 
		 {
		  /* try { // In some instances, status cannot be retrieved and will produce 
				 // an error (e.g. Port is not responsive)
		  */		  
				if (xmlHttp.status == 200) {
				 // Set the main HTML of the body to the info provided by the 
				 // Ajax Request
				// alert(xmlHttp.responseText);
				 var response=xmlHttp.responseText.toString();
				 response = unescape(response).replace('+',' ');
				 response = response.replace('+',' ');
				 response = response.replace('+',' ');
				 response = response.replace('+',' ');
				 response = response.replace('+',' ');
				 response = response.replace('+',' ');
				 response = response.replace('+',' ');
				 response = response.replace('+',' ');
				 response = response.replace('+',' '); 
				alert(response);
			  } else {
				  var errstr = (language=='e') ? 'There was an error submitting your information.  Please try again' : "Une erreur s'est produite durant la transmission de votre information.  Veuillez réessayer" ;
				  error_div.innerHTML = errstr;
			  }
		  }				
		 }	
		 
		xmlHttp.open("GET","/mailchimp.api.php?subscribe=true"+ 
					"&lang=" +language+
					"&email="+email	); 
		xmlHttp.send(null); // Since there is no supplied form, null takes its place 
							 // as a new form.
		return false;
}

function mailchimpForm(language,blogFeed) {

var fnames = new Array();
var ftypes = new Array();
fnames[0]='EMAIL';ftypes[0]='email';
fnames[1]='FNAME';ftypes[1]='text';
fnames[2]='LNAME';ftypes[2]='text';
fnames[3]='MMERGE3';ftypes[3]='phone';
fnames[4]='PROGRAM';ftypes[4]='dropdown';
fnames[5]='INTEREST';ftypes[5]='dropdown';
fnames[6]='REFERRAL';ftypes[6]='dropdown';
fnames[7]='LOCATION';ftypes[7]='text';

	var news_div =  document.getElementById('newsletter'); 
	var error_div =  document.getElementById('error_div');

	//var newsletterID = document.getElementById('newsletterID').value;
	//var lang = document.getElementById('lang').value;
	var email = document.getElementById('mce-'+fnames[0]).value;
	if (!validate_email(email)) {
		error = (language=='e') ? 'The email address provided is not in the right format' : "L'adresse du courriel soumis n'est pas dans le bon format." ;
		alert(error);
		return false; 
	}
	
		var xmlHttp=null; 
		 try {
			// Firefox, Opera 8.0+, Safari, IE7+
			xmlHttp = new XMLHttpRequest(); // xmlHttp is now a XMLHttpRequest.
		 } catch (e) {
			// Internet Explorer
			try {
			   xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
			   xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
		 }
		 xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) 
		 {
		  /* try { // In some instances, status cannot be retrieved and will produce 
				 // an error (e.g. Port is not responsive)
		  */		  
				if (xmlHttp.status == 200) {
				 // Set the main HTML of the body to the info provided by the 
				 // Ajax Request
				// alert(xmlHttp.responseText);
				 var response=xmlHttp.responseText.toString();
				 response = unescape(response).replace('+',' ');
				 response = response.replace('+',' ');
				 response = response.replace('+',' ');
				 response = response.replace('+',' ');
				 response = response.replace('+',' ');
				 response = response.replace('+',' ');
				 response = response.replace('+',' ');
				 response = response.replace('+',' ');
				 response = response.replace('+',' ');
				 response = response.replace('+',' ');
				 response = response.replace('+',' ');
				 response = response.replace('+',' ');
				if (response.indexOf('ucess') > 0) {
					window.location="http://www.fangshendo.com/pages/index_"+language+".aspx?Articleid=160";
				} else {
					alert(response);
				  var errstr = (language=='e') ? 'There was an error submitting your information.  Please try again' : "Une erreur s'est produite durant la transmission de votre information.  Veuillez réessayer" ;
				  error_div.innerHTML = errstr;
				}
			}
		  }				
		 }	
		 
		var url="/mailchimp.api.php?subscribe=true&email=" + email + "&lang=" +language + "&listID="+blogFeed;
		for (var i=0;i<fnames.length;i++ ) {
			url=url+'&'+fnames[i]+'='+document.getElementById('mce-'+fnames[i]).value;
		}		
		//alert (url);
		xmlHttp.open("GET",url); 
		xmlHttp.send(null); // Since there is no supplied form, null takes its place 
							 // as a new form.
		return false;
}

function validate_email(email) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(email) == false) {
		return false;
	} else {
		return true;
	}
}

function FSDblogsignup(lang,email,name,blogFeed) {

	//var newsletterID = document.getElementById('newsletterID').value;
	//var lang = document.getElementById('lang').value;
	email=document.getElementById(email).value;
	if (!validate_email(email)) {
		error = (lang=='e') ? 'The email address provided is not in the right format' : "L'adresse du courriel soumis n'est pas dans le bon format." ;
		alert(error);
		return false; 
	}
	name =document.getElementById(name).value;
	if (name.length < 2) {
		error = (lang=='e') ? 'Please enter your first name' : "Entrez votre nom" ;
		alert(error);
		return false;
	}	
	
	
var xmlHttp=null; 
		 try {
			// Firefox, Opera 8.0+, Safari, IE7+
			xmlHttp = new XMLHttpRequest(); // xmlHttp is now a XMLHttpRequest.
		 } catch (e) {
			// Internet Explorer
			try {
			   xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
			   xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
		 }
		 xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) 
		 {
		  /* try { // In some instances, status cannot be retrieved and will produce 
				 // an error (e.g. Port is not responsive)
		  */		  
				if (xmlHttp.status == 200) {
				 // Set the main HTML of the body to the info provided by the 
				 // Ajax Request
				// alert(xmlHttp.responseText);
				 var response=xmlHttp.responseText.toString();
				 response = unescape(response).replace('+',' ');
				 response = response.replace('+',' ');
				 response = response.replace('+',' ');
				 response = response.replace('+',' ');
				 response = response.replace('+',' ');
				 response = response.replace('+',' ');
				 response = response.replace('+',' ');
				 response = response.replace('+',' ');
				 response = response.replace('+',' ');
				alert(response);
			  } else {
				  var errstr = (lang=='e') ? 'There was an error submitting your information.  Please try again' : "Une erreur s'est produite durant la transmission de votre information.  Veuillez réessayer" ;
				  error_div.innerHTML = errstr;
			  }
		  }				
		 }	
		 
		var url="/mailchimp.api.php?subscribe=true&email=" + email + "&lang=" +lang + "&listID="+blogFeed +"&fname="+name;		
		//alert (url);
		xmlHttp.open("GET",url); 
		xmlHttp.send(null); // Since there is no supplied form, null takes its place 
							 // as a new form.
		return false;

}

/*--[ SEARCH BOX ]-------------------------------------------------------------------
*  Use the following 3 functions if a search box is required in the template.
*  Ensure you give the search box an id='txtSearch' attribute
*  callAdvancedSearch is for the 'search' button.  This will send the search criteria
*    useage: <a href="#" id="submitSearch" >Search</a>
*  getChars will check the box each keypress to see if they pressed Enter, which
*       will transfer them to the search page.
*    useage: <input class="search_box" type="text" width="115" id="txtSearch" name="txtSearch">
*	You will need to change the url in callAdvancedSearch function.
*-----------------------------------------------------------------------------------*/
function callAdvancedSearch(lang) { 
	var criteria = document.getElementById('txtSearch').value;
	var url ="/pages/index" + lang + ".aspx?ArticleID=42@criteria=" + criteria;
	window.location=url;
	return false;
}
function getChars(e, lang) {     
	if (!e) var e = window.event;        //Internet Explorer Event
	//Grab the keycode for all browsers
	var characterCode=(e.charCode)?e.charCode: ((e.keyCode)?e.keyCode:((e.which)?e.which:0));
	if (characterCode == 13) {        //13=Enter button    
		//Cancel default form submission action
		e.cancelBubble = true;        
		e.returnValue = false;    
		
		if (e.which ) e.preventDefault();
		
		//Run the search
		callAdvancedSearch(lang);            
	}
	return false;
}

/*---[ Language Link ]-------------------------------------------------------------
*  This is used if the language link is inside an image map.
*  Otherwise, use the default language link by the WCMS.
*  Useage: change_links() to the window.onload function on the main page
*---------------------------------------------------------------------------------*/
function change_links()
{
	var address = document.location.href;
	var home_link = address;
	home_link = home_link.split('?');

	var rExp = 'index_e';
	start = address.search(rExp);		//check to see if its on english page
	if (start==(-1)){					//english, change to french
		address = address.replace("index_f","index_e");	//replace f with e
	} else {
		address = address.replace("index_e","index_f");	//replace e with f
	}
	
	document.getElementById('switch_lang').href = address;	//change switch_lang to whatever id you need
	document.getElementById('home_link').href = home_link[0];

}

/*---[ Resize Text ]-------------------------------------------------------------
*  Will change the text size
*  Useage: <a href="#" onClick="resizeText(1)">Change Text Size</a>
*---------------------------------------------------------------------------------*/
function resizeText(multiplier) {
  if (document.getElementById('main').style.fontSize == "") {
    document.getElementById('main').style.fontSize = "0.8em";
  }
  if (document.getElementById('main').style.fontSize == '1em') {multiplier = (-1);}
  document.getElementById('main').style.fontSize = parseFloat(document.getElementById('main').style.fontSize) + (multiplier * 0.2) + "em";
}

/*---[ Change Title ]-------------------------------------------------------------
*  Appends the site name to the title until the WCMS does it 
*  Useage: changeTitle('Default Site');
*---------------------------------------------------------------------------------*/
function changeTitle(pageTitle) {
	document.title = (document.title + " - " + pageTitle);
}


//////////////////////////////////// xml2array() ////////////////////////////////////////
//See http://www.openjs.com/scripts/xml_parser/
var not_whitespace = new RegExp(/[^\s]/);//This can be given inside the funciton - I made it a global variable to make the scipt a little bit faster.
var parent_count;
//Process the xml data
function xml2array(xmlDoc,parent_count) {
	var arr;
	var parent = "";
	parent_count = parent_count || new Object;

	var attribute_inside = 0; /*:CONFIG: Value - 1 or 0
	*	If 1, Value and Attribute will be shown inside the tag - like this...
	*	For the XML string...
	*	<guid isPermaLink="true">http://www.bin-co.com/</guid>
	*	The resulting array will be...
	*	array['guid']['value'] = "http://www.bin-co.com/";
	*	array['guid']['attribute_isPermaLink'] = "true";
	*	
	*	If 0, the value will be inside the tag but the attribute will be outside - like this...	
	*	For the same XML String the resulting array will be...
	*	array['guid'] = "http://www.bin-co.com/";
	*	array['attribute_guid_isPermaLink'] = "true";
	*/

	if(xmlDoc.nodeName && xmlDoc.nodeName.charAt(0) != "#") {
		if(xmlDoc.childNodes.length > 1) { //If its a parent
			arr = new Object;
			parent = xmlDoc.nodeName;
			
		}
	}
	var value = xmlDoc.nodeValue;
	if(xmlDoc.parentNode && xmlDoc.parentNode.nodeName && value) {
		if(not_whitespace.test(value)) {//If its a child
			arr = new Object;
			arr[xmlDoc.parentNode.nodeName] = value;
		}
	}

	if(xmlDoc.childNodes.length) {
		if(xmlDoc.childNodes.length == 1) { //Just one item in this tag.
			arr = xml2array(xmlDoc.childNodes[0],parent_count); //:RECURSION:
		} else { //If there is more than one childNodes, go thru them one by one and get their results.
			var index = 0;

			for(var i=0; i<xmlDoc.childNodes.length; i++) {//Go thru all the child nodes.
				var temp = xml2array(xmlDoc.childNodes[i],parent_count); //:RECURSION:
				if(temp) {
					var assoc = false;
					var arr_count = 0; 
					for(key in temp) {
						if(isNaN(key)) assoc = true;
						arr_count++;
						if(arr_count>2) break;//We just need to know wether it is a single value array or not
					}

					if(assoc && arr_count == 1) {
						if(arr[key]) { 	//If another element exists with the same tag name before,
										//		put it in a numeric array.
							//Find out how many time this parent made its appearance
							if(!parent_count || !parent_count[key]) {
								parent_count[key] = 0;

								var temp_arr = arr[key];
								arr[key] = new Object;
								arr[key][0] = temp_arr;
							}
							parent_count[key]++;
							arr[key][parent_count[key]] = temp[key]; //Members of of a numeric array
						} else {
							parent_count[key] = 0;
							arr[key] = temp[key];
							if(xmlDoc.childNodes[i].attributes && xmlDoc.childNodes[i].attributes.length) {
								for(var j=0; j<xmlDoc.childNodes[i].attributes.length; j++) {
									var nname = xmlDoc.childNodes[i].attributes[j].nodeName;
									if(nname) {
										/* Value and Attribute inside the tag */
										if(attribute_inside) {
											var temp_arr = arr[key];
											arr[key] = new Object;
											arr[key]['value'] = temp_arr;
											arr[key]['attribute_'+nname] = xmlDoc.childNodes[i].attributes[j].nodeValue;
										} else {
										/* Value in the tag and Attribute otside the tag(in parent) */
											arr['attribute_' + key + '_' + nname] = xmlDoc.childNodes[i].attributes[j].nodeValue;
										}
									}
								} //End of 'for(var j=0; j<xmlDoc. ...'
							} //End of 'if(xmlDoc.childNodes[i] ...'
						}
					} else {
						arr[index] = temp;
						index++;
					}
				} //End of 'if(temp) {'
			} //End of 'for(var i=0; i<xmlDoc. ...'
		}
	}

	if(parent && arr) {
		var temp = arr;
		arr = new Object;
		
		arr[parent] = temp;
	}
	return arr;
}


function change_location() {
	//this will change the id of the MailChimp signup depending on which location they want to sing up for.
	var locations = new array();
	locations['Brossard']='4279a2411f';
	
	if (document.getElementById('location-key')) {
		if (gup('selected')) {
		document.getElementById('location-value').value = gup('selected');
		} 
	}
	
	
	
	
}

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
