/*--[ 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;
	if (document.getElementById('search_google').checked) {
		var url ="http://www.google.ca/search?hl=en&as_qdr=all&q=" + criteria;
	} else {
		var url ="/wcbns/index" + lang + ".aspx?ArticleID=896@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";
}

/*---[ NEWSLETTER ]---------------------------------------------------------------*/
function newsletter_action(language){
	var news_div =  document.getElementById('newsletter');
	
	var newsletterID = document.getElementById('newsletterID').value;
	var lang = document.getElementById('lang').value;
	var email = document.getElementById('email').value;
	if (!validate_email(email)) {
		news_div.innerHTML = (language=='e') ? 'The email address provided is not in the right format' : "L'adresse du courriel soumis n'est pas dans le bon format." ;
		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;
	}
		
	news_div.innerHTML = (language=='e') ? '<img style="position:relative; top:10px;" align="center" src="/app/Newsletter/images/ajax.gif" alt="Processing..." />' : '<img style="position:relative; top:10px;" align="center" src="/app/Newsletter/images/ajax.gif" alt="Veuillez patienter..." />' ;
	
		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();
				news_div.innerHTML = 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" ;
				  news_div.innerHTML = errstr;
			  }
		/*   } catch (e) {
			   var errstr2 = (language=='e') ? 'There was an error submitting your information(2).  Please try again' : "Une erreur s'est produite durant la transmission de votre information.  Veuillez réessayer" ;
				news_div.innerHTML = errstr2;
				return false;
		   }*/
		  }				
		 }	
		 
		var vars='newsid=' + newsletterID + '&email=' + encodeURIComponent(email) + '&action=' + action + '&lang=' + lang;
		xmlHttp.open("get","/newsletter/signup.aspx?"+vars); // .open(RequestType, Source);
		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;
	}
}



