
	

var emptySelectEn = "<select style='Width: 140px;' id='temp' name='temp'><option value=''>Loading...</option></select>";
var emptySelectFr = "<select style='Width: 140px;' id='temp' name='temp'><option value=''>Chargement...</option></select>";
var SortColumn = "Date";
var SortDirection = 1;

function SortClicked(SelectedColumn)
{
	
	if (SortColumn == SelectedColumn)
	{
		if (SortDirection == 1)
		{
			SortDirection = 2;
		} else {
			SortDirection = 1;
		}
	} else {
		SortColumn = SelectedColumn;
		SortDirection == 1;
	}
	Province = $('Province').value;
	Location = $('Location').value;
	City = 	$('City').value;
	SubmitFilter(Province, Location, City, '');
}


function provinceSelected()
{
	Province = $('Province').value;
	Location = $('Location').value;
	City = 	$('City').value;
	if (gup('lang') == "FR") {
		$('CitiesDropDown').innerHTML =  emptySelectFr;
		$('LocationsDropDown').innerHTML =  emptySelectFr;
	} else {
		$('CitiesDropDown').innerHTML =  emptySelectEn;
		$('LocationsDropDown').innerHTML =  emptySelectEn;
	}
	getDropDown('Locations', Province, 0, 0);
	getDropDown('Cities', Province, 0, 0);
	SubmitFilter(Province, Location, City, 'Province');
}

function locationSelected()
{
	Province = $('Province').value;
	Location = $('Location').value;
	City = 	$('City').value;

	if (Location != '') 
	{	
		if (gup('lang') == "FR") {
			$('CitiesDropDown').innerHTML =  emptySelectFr;
			$('LocationsDropDown').innerHTML =  emptySelectFr;
		} else {
			$('CitiesDropDown').innerHTML =  emptySelectEn;
			$('LocationsDropDown').innerHTML =  emptySelectEn;
		}
		
		getDropDown('Cities', 0, Location, 0);
		setProvinceIDFromLocation(Location);
		getDropDown('Locations', 0, Location, 0);
	} else {
		getDropDown('Cities', Province, 0, 0);
	}
	SubmitFilter(Province, Location, City, 'Location');
}
function DatesUpdated()
{
	Province = $('Province').value;
	Location = $('Location').value;
	City = 	$('City').value;
	SubmitFilter(Province, Location, City, '');
}
function citySelected()
{
	Province = $('Province').value;
	Location = $('Location').value;
	City = 	$('City').value;
	if (City != '') {
		if (gup('lang') == "FR") {
			$('CitiesDropDown').innerHTML =  emptySelectFr;
			$('LocationsDropDown').innerHTML =  emptySelectFr;
		} else {
			$('CitiesDropDown').innerHTML =  emptySelectEn;
			$('LocationsDropDown').innerHTML =  emptySelectEn;
		}
		getDropDown('Locations', 0, 0, City);
		setProvinceIDFromCity(City);
		getDropDown('Cities', 0, 0, City);
	}
	SubmitFilter(Province, Location, City, 'City');
}
function getDropDown(type, prov, loc, city)
{
	var Lang = gup('lang');
	new Ajax.Request('/ajax/'+type+'/',
	{ 
		method: 'post',
        parameters: {LOCATION: loc, PROVINCE: prov, CITY: city, LANG: Lang},
        onComplete: function(request)
        {
        	name = type + "DropDown";
        	$(name).innerHTML =  request.responseText;
		}		
	}); 
}

function setProvinceIDFromCity(CityID)
{
	new Ajax.Request('/ajax/ProvinceID/',
	{ 
		method: 'post',
        parameters: {CITY: CityID},
        onComplete: function(request)
        {
        	$('Province').selectedIndex =  parseInt(request.responseText);
		}		
	}); 
}
function setProvinceIDFromLocation(LocationID)
{
	new Ajax.Request('/ajax/ProvinceID/',
	{ 
		method: 'post',
        parameters: {LOCATION: LocationID},
        onComplete: function(request)
        {
        	$('Province').selectedIndex =  parseInt(request.responseText);
		}		
	}); 
}
function searchLink(Province, Location)
{
	if (Location != 0)
	{
		if (gup('lang') == "FR") {
			$('CitiesDropDown').innerHTML =  emptySelectFr;
			$('LocationsDropDown').innerHTML =  emptySelectFr;
		} else {
			$('CitiesDropDown').innerHTML =  emptySelectEn;
			$('LocationsDropDown').innerHTML =  emptySelectEn;
		}
		getDropDown('Cities', 0, Location, 0);
		setProvinceIDFromLocation(Location);
		getDropDown('Locations', 0, Location, 0);
		SubmitFilter(Province,Location,0,'');	
	} else if (Province != 0) {
		if (gup('lang') == "FR") {
			$('CitiesDropDown').innerHTML =  emptySelectFr;
			$('LocationsDropDown').innerHTML =  emptySelectFr;
		} else {
			$('CitiesDropDown').innerHTML =  emptySelectEn;
			$('LocationsDropDown').innerHTML =  emptySelectEn;
		}
		getDropDown('Cities', Province, 0, 0);
		$('Province').selectedIndex = parseInt(Province);
		getDropDown('Locations', Province, 0, 0);
		SubmitFilter(Province,Location,0,'');	
	}
	
}

function SubmitFilter(Province, Location, City, DropDownUsed)
{
	var dateError = 0;
	if ($('startRange').value != 0) 
	{
		if (isDate($('startRange')))
		{
			dateError = 0;
		} else {
			dateError = 1;
		}
	}
	if ($('endRange').value != 0)
	{
		if (isDate($('endRange')))
		{
			dateError = 0;
		} else {
			dateError = 1;
		}
	}
	var Mcode = gup('mcode');
	var Lang = gup('lang');
	if (dateError == 0)
	{
	    new Ajax.Request('/ajax/mainTable/',
	    { 
	    	method: 'post',
	    	parameters: {Location: Location, mcode: Mcode, lang: Lang, Province: Province, City: City, StartDate: $('startRange').value, EndDate: $('endRange').value, SortColumn: SortColumn, SortDirection: SortDirection, DropDown: DropDownUsed},
			onComplete: function(request)
	        {  
	        	$('ResultData').innerHTML = request.responseText;  
	        }
	    });  
	    new Ajax.Request('/ajax/SearchLinks/',
	    { 
	    	method: 'post',
	    	parameters: {Location: Location, mcode: Mcode, Province: Province, City: City, StartDate: $('startRange').value, EndDate: $('endRange').value,DropDown: DropDownUsed},
			onComplete: function(request)
	        {  
	        	$('SearchLinks').innerHTML = request.responseText;  
	        }
		});
	}
}   

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];
}

	
var dtCh= "-";
var minYear=2008;
var maxYear=2020;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}


function validate_email(value)
{

		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) 
  		{
  			
  			return false;
  		} else {
			return true;
		}
}


function isDate(field){
	
	var dtStr = field.value;
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strYear=dtStr.substring(0,pos1) 
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strDay=dtStr.substring(pos2+1)

	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : yyyy-mm-dd")
		field.focus();
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		field.focus();
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		field.focus();
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		field.focus();
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		field.focus();
		return false
	}
return true
}


function ResetFilters()
{
	$('Province').selectedIndex = 0;
	$('Location').selectedIndex = 0;
	$('City').selectedIndex = 0;	
	$('startRange').value = '';
	$('endRange').value = '';
	getDropDown('Locations', 0, 0, 0);
	getDropDown('Cities', 0, 0, 0);
	SubmitFilter(0,0,0,'');
	
}


function EmailAFriend()
{
	var Lang ='' 
	if (gup('lang') == 'FR') { Lang = "FR"; } else { Lang = "EN"; }
	Errors = '';
	if ($('SenderEmail').value == '') 
	{ 
		if (Lang == "FR") { Errors += "Veuillez écrire votre adresse de courriel\n";} else {Errors += "Please enter your email address\n";}
	} else {
		if (!validate_email($('SenderEmail').value)) { if (Lang == "FR") { Errors += "Votre adresse de courriel est invalide. Veuillez fournir une adresse de courriel ayant le format nomusager@domaine.com\n";} else {Errors += "Your email address is invalid. Please provide an address in username@domain.com format\n"; }}
	}
	if ($('Sender').value == '') { 
		if (Lang == "FR") { Errors += "Veuillez fournir votre nom\n";} else {Errors += "Please provide your (First) name\n";} 
	}
	if ($('ToEmail').value == '') 
	{ 
		if (Lang == "FR") {Errors += "Veuillez fournir l'adresse du destinataire\n"; } else {Errors += "Please provide a recipient address\n";} 
	} else {
		if (!validate_email($('ToEmail').value)) { if (Lang == "FR") {Errors += "L'adresse de courriel du destinataire est invalide. Veuillez fournir une adresse de courriel ayant le format nomusager@domaine.com" } else {Errors += "The recipient email is invalid. Please provide an address in username@domain.com format\n"; }}
	}
	
	if (Errors == '')
	{
		$('doSend').disabled = true;
		new Ajax.Request('/ajax/emailafriend/',
	    { 
	    	method: 'post',
	    	
	    	parameters: {lang: Lang, Sender: $('Sender').value, SenderEmail: $('SenderEmail').value, ToEmail: $('ToEmail').value, FID: $('FID').value, Comments: $('Comments').value},
			onComplete: function(request)
	        {  
	        	var x = request.responseText;
					if (x.indexOf("true") != -1) 	
					{
						if (Lang == "FR") { alert("Courriel envoyé!"); } else { alert("Email Sent!"); };
						myAjaxWindow.close();
						return true;
					} else {
						if (Lang == "FR") { alert('Courriel non envoyé ! Veuillez contacter l’administrateur du site !');} else {alert("Email NOT Sent! Please Contact Site Admin!");}
						return false;
					} 
	        }
	    });
	    return false;  	
	} else {
		alert (Errors);
		return false;
	}
}

function ShowEmailForm()
{
	var Lang ='' 
	if (gup('lang') == 'FR') { Lang = "FR"; } else { Lang = "EN"; }
	
	myAjaxWindow = new Control.Modal(false,
	{  	opacity: 0.8, position: 'absolute', width: 350,
    	contents: function()
     	{  
        	new Ajax.Request('/careerfair/email/?lang='+Lang,
         	{ 
         		method: 'get',
         		parameters: { FID:  gup('FID') },
            	onComplete: function(request)
            	{  
                	myAjaxWindow.update(request.responseText);  
            	}.bind(this)  
        	});  
         return 'loading';  
     	}  
 	});  
 						
	myAjaxWindow.open();

}

function GoToPage(PageVal)
{
	var dateError = 0;
	var Province = $('Province').value;
	var Location = $('Location').value;
	var City = 	$('City').value;
	var Mcode = gup('mcode');
	
	if ($('startRange').value != 0) 
	{
		if (isDate($('startRange')))
		{
			dateError = 0;
		} else {
			dateError = 1;
		}
	}
	if ($('endRange').value != 0)
	{
		if (isDate($('endRange')))
		{
			dateError = 0;
		} else {
			dateError = 1;
		}
	}
	var Lang = gup('lang');
	
	if (dateError == 0)
	{
	    new Ajax.Request('/ajax/mainTable/',
	    { 
	    	method: 'post',
	    	parameters: {Page: PageVal, Location: Location, mcode: Mcode, lang: Lang, Province: Province, City: City, StartDate: $('startRange').value, EndDate: $('endRange').value, SortColumn: SortColumn, SortDirection: SortDirection},
			onComplete: function(request)
	        {  
	        	$('ResultData').innerHTML = request.responseText;  
	        }
	    });  
	    new Ajax.Request('/ajax/SearchLinks/',
	    { 
	    	method: 'post',
	    	parameters: {Page: PageVal, Location: Location, mcode: Mcode, Province: Province, City: City, StartDate: $('startRange').value, EndDate: $('endRange').value},
			onComplete: function(request)
	        {  
	        	$('SearchLinks').innerHTML = request.responseText;  
	        }
		});
	}
}  



