﻿function FilterCourseSchedule() 
{       
 
  var outURL = '';
  
  //Filter TechnologyArea
  var TechnologyAreaURL = '';
  var contTechnologyArea = 0;
  var aux = document.getElementById('filterTechnologyArea');
  if (aux != null)
  {
	  var listInputs = aux.getElementsByTagName('input');
	  if (listInputs.length != 0)
	  {
	      for (var i = 0; i < listInputs.length; i++) 
	      {
	         if (listInputs[i].checked) 
	         {
	         	if (contTechnologyArea == 0)
	         	{
	         		TechnologyAreaURL = '?technologyarea=' + listInputs[i].name;
	         		contTechnologyArea = contTechnologyArea + 1;
	         	}
	         	else
	         	{
	            	TechnologyAreaURL = TechnologyAreaURL + ';' + listInputs[i].name;
	            }
	         }  
	      }
	  }
	  if (TechnologyAreaURL != '')
	  {
	  	outURL = TechnologyAreaURL;
	  }
  }
  
  //Filter Country
  var CountryURL = '';
  var contCountry = 0;
  aux = null;
  aux = document.getElementById('containerCountry');
  if (aux != null)
  {
	  listInputs = aux.getElementsByTagName('input');
	  if (listInputs.length != 0)
	  {
	    for (var i = 0; i < listInputs.length; i++) 
	    {
	         if (listInputs[i].checked) 
	         {
	         	if (contCountry == 0)
	         	{
	         		if (outURL != '')
		          	{
		          		CountryURL = CountryURL + '&country=' + listInputs[i].name;
		          	}
		          	else
		          	{
		          		CountryURL = '?country=' + listInputs[i].name;
		          	}
	         		contCountry = contCountry + 1;
	         	}
	         	else
	         	{
	            	CountryURL = CountryURL + ';' + listInputs[i].name;
	            }
	         }  
	    }
	  }
	  if (CountryURL != '')
	  {
	  	outURL = outURL + CountryURL ;
	  }
   }

 
  //Filter Languages
  var langURL = '';
  var contLang = 0;
  aux = null;
  aux = document.getElementById('filterLanguage');
  if (aux != null)
  {
	  listInputs = aux.getElementsByTagName('input');
	  if (listInputs.length != 0)
	  {
	    for (var i = 0; i < listInputs.length; i++) 
	    {
	         if (listInputs[i].checked) 
	         {
	         	if (contLang == 0)
	         	{
	         		if (outURL != '')
		          	{
		          		langURL = langURL + '&language=' + listInputs[i].name;
		          	}
		          	else
		          	{
		          		langURL = '?language=' + listInputs[i].name;
		          	}
	         		contLang = contLang + 1;
	         	}
	         	else
	         	{
	            	langURL = langURL + ';' + listInputs[i].name;
	            }
	         }  
	    }
	  }
	  if (langURL != '')
	  {
	  	outURL = outURL + langURL;
	  }
   }
  
  
  //Filter Mentors
  var autURL = '';
  var contAut = 0;
  aux = null;
  aux = document.getElementById('filterMentor');
  if (aux != null)
  {
	  listInputs = aux.getElementsByTagName('input');
	  if (listInputs.length != 0)
	  {
	    for (var i = 0; i < listInputs.length; i++) 
	    {
	    	 if (listInputs[i].checked) 
	         {
	         	if (contAut == 0)
	         	{
	         		if (outURL != '')
		          	{
		          		autURL = autURL + '&mentor=' + listInputs[i].name;
		          	}
		          	else
		          	{
		          		autURL = '?mentor=' + listInputs[i].name;
		          	}
	         		contAut = contAut + 1;
	         	}
	         	else
	         	{
	            	autURL = autURL + ';' + listInputs[i].name;
	            }
	         }  
	    }
	  }
	  if (autURL != '')
	  {
	  	outURL = outURL + autURL;
	  }
   }
  

  //Filter Technology
  var TechnologyURL = '';
  var contTechnology = 0;
  aux = null;
  aux = document.getElementById('filterTechnology');
  if (aux != null)
  {
	  listInputs = aux.getElementsByTagName('input');
	  if (listInputs.length != 0)
	  {
	    for (var i = 0; i < listInputs.length; i++) 
	    {
	    	 if (listInputs[i].checked) 
	         {
	         	if (contTechnology == 0)
	         	{
	         		if (outURL != '')
		          	{
		          		TechnologyURL = TechnologyURL + '&technology=' + listInputs[i].name;
		          	}
		          	else
		          	{
		          		TechnologyURL = '?technology=' + listInputs[i].name;
		          	}
	         		contTechnology = contTechnology + 1;
	         	}
	         	else
	         	{
	            	TechnologyURL = TechnologyURL + ';' + listInputs[i].name;
	            }
	         }  
	    }
	  }
	  if (TechnologyURL != '')
	  {
	  	outURL = outURL + TechnologyURL ;
	  }
   }

  //Filter Location
  var LocationURL = '';
  var contLocation = 0;
  aux = null;
  aux = document.getElementById('filterLocation');
  if (aux != null)
  {
	  listInputs = aux.getElementsByTagName('input');
	  if (listInputs.length != 0)
	  {
	    for (var i = 0; i < listInputs.length; i++) 
	    {
	    	 if (listInputs[i].checked) 
	         {
	         	if (contLocation == 0)
	         	{
	         		if (outURL != '')
		          	{
		          		LocationURL = LocationURL + '&location=' + listInputs[i].name;
		          	}
		          	else
		          	{
		          		LocationURL = '?location=' + listInputs[i].name;
		          	}
	         		contLocation = contLocation + 1;
	         	}
	         	else
	         	{
	            	LocationURL = LocationURL + ';' + listInputs[i].name;
	            }
	         }  
	    }
	  }
	  if (LocationURL != '')
	  {
	  	outURL = outURL + LocationURL;
	  }
   }


  //Filter CourseType
  var CourseTypeURL = '';
  var contCourseType = 0;
  aux = null;
  aux = document.getElementById('filterCourseType');
  if (aux != null)
  {
	  listInputs = aux.getElementsByTagName('input');
	  if (listInputs.length != 0)
	  {
	    for (var i = 0; i < listInputs.length; i++) 
	    {
	    	 if (listInputs[i].checked) 
	         {
	         	if (contCourseType == 0)
	         	{
	         		if (outURL != '')
		          	{
		          		CourseTypeURL = CourseTypeURL + '&coursetype=' + listInputs[i].name;
		          	}
		          	else
		          	{
		          		CourseTypeURL = '?coursetype=' + listInputs[i].name;
		          	}
	         		contCourseType = contCourseType + 1;
	         	}
	         	else
	         	{
	            	CourseTypeURL = CourseTypeURL + ';' + listInputs[i].name;
	            }
	         }  
	    }
	  }
	  if (CourseTypeURL != '')
	  {
	  	outURL = outURL + CourseTypeURL ;
	  }
   }


  //Filter Region
  var RegionURL = '';
  var contRegion = 0;
  aux = null;
  aux = document.getElementById('filterRegion');
  if (aux != null)
  {
	  listInputs = aux.getElementsByTagName('input');
	  if (listInputs.length != 0)
	  {
	    for (var i = 0; i < listInputs.length; i++) 
	    {
	    	 if (listInputs[i].checked) 
	         {
	         	if (contRegion == 0)
	         	{
	         		if (outURL != '')
		          	{
		          		RegionURL = RegionURL + '&region=' + listInputs[i].name;
		          	}
		          	else
		          	{
		          		RegionURL = '?region=' + listInputs[i].name;
		          	}
	         		contRegion = contRegion + 1;
	         	}
	         	else
	         	{
	            	RegionURL = RegionURL + ';' + listInputs[i].name;
	            }
	         }  
	    }
	  }
	  if (RegionURL != '')
	  {
	  	outURL = outURL + RegionURL ;
	  }
   }


  //Filter OrganizingRegion
  var OrganizingRegionURL = '';
  var contOrganizingRegion = 0;
  aux = null;
  aux = document.getElementById('filterOrganizingRegion');
  if (aux != null)
  {
	  listInputs = aux.getElementsByTagName('input');
	  if (listInputs.length != 0)
	  {
	    for (var i = 0; i < listInputs.length; i++) 
	    {
	    	 if (listInputs[i].checked) 
	         {
	         	if (contOrganizingRegion == 0)
	         	{
	         		if (outURL != '')
		          	{
		          		OrganizingRegionURL = OrganizingRegionURL + '&organizingregion=' + listInputs[i].name;
		          	}
		          	else
		          	{
		          		OrganizingRegionURL = '?organizingregion=' + listInputs[i].name;
		          	}
	         		contOrganizingRegion = contOrganizingRegion + 1;
	         	}
	         	else
	         	{
	            	OrganizingRegionURL = OrganizingRegionURL + ';' + listInputs[i].name;
	            }
	         }  
	    }
	  }
	  if (OrganizingRegionURL != '')
	  {
	  	outURL = outURL + OrganizingRegionURL ;
	  }
   }


  //Filter Dates
  var datesURL = '';
  var contDates = 0;
  aux = null;
  aux = document.getElementById('filterDates');
  if (aux != null)
  {
	  listInputs = aux.getElementsByTagName('input');
	  listAnchors = aux.getElementsByTagName('a');
	
	  if (listAnchors.length != 0)
	  {
	  	//Array to store list of dates
	  	var listEndDates = [];
	  	var listStartDates = [];
	  	var countEndDates = 0;
	  	var countStartDates = 0;
	  	var endDateTime = '';
	  	var startDateTime = '';
	  	//Crear list ade fechas a partid de URL
	  	for (var i = 0; i < listAnchors.length; i++) {
	  		//alert(listInputs[i].checked);
	  		if (listInputs[i].checked) 
	  		{
	             var url = listAnchors[i].href;
	             var iEndInit = url.indexOf('enddatetime=') + 11;
	             var iEndFin = iEndInit + 20;
	             if ((iEndInit != -1) && (iEndFin != -1 ))
	             {
	             	endDateTime = url.substring(iEndInit + 1, iEndFin);
	             	listEndDates[countEndDates ] = endDateTime;
	             	countEndDates = countEndDates + 1;
	             }
	             var iStartInit = url.lastIndexOf('=');
	             if (iStartInit != -1)
	             {
	             	startDateTime = url.substring(iStartInit + 1);
	             	listStartDates[countStartDates] = startDateTime;
		            countStartDates = countStartDates + 1;
	             }
	             //Store dates to check after refresh
	             var fecha = listAnchors[i].innerHTML;
	             if (contDates == 0)
	             {
	             	if (outURL != '')
		          	{
		          		datesURL = '&dates=' + fecha;
		          	}
		          	else
		          	{
		          		datesURL = '?dates=' + fecha;
		          	}
	         		contDates = contDates + 1;
	             }
	             else
	             {
		         	datesURL = datesURL + ';' + fecha;
	             }
	         }
	    }
	    if (contDates > 0)
	    {
	    	var dateAssigned = 0;
	        //Recorro la lista de fechas de inicio buscando la minima
	        var minFechaInicio = '9999-99-99T99:99:99';
	        for (var i = 0; i < listStartDates.length; i++) 
	        {
	        	if (listStartDates[i] < minFechaInicio)
	        	{
	        		minFechaInicio = listStartDates[i];
	        		dateAssigned = 1;
	        	}				        	
	      	}
	      	//Recorro la lista de fechas de fin buscando la maxima
	      	var maxFechaFin = '0000-00-00T00:00:00';
	      	for (var i = 0; i < listEndDates.length; i++) 
	        {
	        	if (listEndDates[i] > maxFechaFin)
	        	{
	        		maxFechaFin = listEndDates[i];
	        		if (dateAssigned > 0)
	        		{
	        			dateAssigned = 2;
	        		}
	        	}
	      	}
	   	
			//Si se han establecido las fechas max y min
			//alert(dateAssigned);
	      	if (dateAssigned == 2)
	      	{
	      		datesURL = datesURL + '&enddatetime=' + maxFechaFin;
	      		datesURL = datesURL + '&startdatetime=' + minFechaInicio;
	      	}
	  	}
	  	if (datesURL != '')
	    {
	      	outURL = outURL + datesURL;
	    }
	  }
   }

          
  if (outURL != '')
  {
  	window.location.href = 'Home.aspx' + outURL;          
  }
  else
  {
  	//alert('You must select any field to filter.');
  	window.location.href = 'Home.aspx';
  }
}        


var hiddens= [];
hiddens['containerTechnologyArea'] = true;
hiddens['containerTechnology'] = true;
hiddens['containerMentor'] = true;
hiddens['containerLanguage'] = true;
hiddens['containerLocation'] = true;
hiddens['containerCourseType'] = true;
hiddens['containerOrganizingRegion'] = true;
hiddens['containerRegion'] = true;
hiddens['containerDate'] = true;
hiddens['containerCountry'] = true;
hiddens['containerDate'] = true;
function HideDivsNotSelected(containerID, parentImage)
{	
	if (parentImage != null)
	{	
		var image = parentImage.firstChild;
		if (image != null)
		{
			if (hiddens[containerID] == true)
			{
				image.src = '/Style Library/Images/minus.png';
				parentImage.style.borderBottom = '1px black solid';
			}
			else
			{
				image.src = '/Style Library/Images/plus.png';
				parentImage.style.borderBottom = '0px transparent none';
			}				
		}
	}
	var obj = document.getElementById(containerID);
	//alert(obj);
	if (obj != null)
	{
		var childNodeArray = obj.childNodes;
		for (var i = 0; i < childNodeArray.length; i++) 
		{
			if(childNodeArray[i].nodeType == 1) 
			{

				var sib = childNodeArray[i];
				if (sib != null)
				{
					if (!sib.firstChild.checked)
					{
						//alert('sib.style.display:' + sib.style.display);
						if (sib.style.display == 'none')
						{
							sib.style.display='block';
						}
						else
						{
							if (hiddens[containerID] == false)
							{
								sib.style.display='none';
							}
						}
					}
				}
			}
		}	
	}
	if (hiddens[containerID] == true)
	{
		hiddens[containerID] = false;
	}
	else
	{
		hiddens[containerID] = true;
	}	
}





////OBSOLETE//////
function DatesCheck()
{
	//Dates
	aux = document.getElementById('filterDates');
	listInputs = aux.getElementsByTagName('input');
	listAnchors = aux.getElementsByTagName('a');
	
	if (listAnchors.length != 0)
	{
		var querystring = window.location.querystring;

		var QSEndDate = querystring['enddatetime'];
		var QSStartDate = querystring['startdatetime'];
		if (QSEndDate && QSStartDate)
		{
			//alert('tienen valor');
			//alert('End date: ' + QSEndDate + ',Start:'+ QSStartDate);
			var endDateTime = '';
			var startDateTime = '';
			//Crear list ade fechas a partid de URL
			for (var i = 0; i < listAnchors.length; i++) 
			{
				//alert(listInputs[i].checked);
				var url = listAnchors[i].href;
				var iEndInit = url.indexOf('=');
				var iEndFin = url.indexOf('&');
				if ((iEndInit != -1) && (iEndFin != -1 ))
				{
					endDateTime = url.substring(iEndInit + 1, iEndFin);
				}
				var iStartInit = url.lastIndexOf('=');
				if (iStartInit != -1)
				{
					startDateTime = url.substring(iStartInit + 1);
				}
				if ((endDateTime!='') && (startDateTime!=''))
				{
					//Compruebo si la fecha del anchor esta contenido en la fecha de la url
					if ((startDateTime >= QSStartDate) && (endDateTime <= QSEndDate))
					{
						listInputs[i].checked = true;
					}
				}
			}
		}
	}
}

///NOT USED////
function DatesCheckNames()
{
	//Dates
	aux = document.getElementById('filterDates');
	listInputs = aux.getElementsByTagName('input');
	listAnchors = aux.getElementsByTagName('a');
	
	if (listAnchors.length != 0)
	{
		var querystring = window.location.querystring;

		var QSDates = querystring['dates'];
		if (QSDates)
		{
			for (var i = 0; i < listAnchors.length; i++) 
			{
				var name = listAnchors[i].innerHTML;
				var namesQS = QSDates.split(';');
				for (var a = 0; a < namesQS.length; a++) 
				{
					//alert('namesQSA:' + namesQS[a] + ',innerHTML:' + name);
					if (namesQS[a] != '')
					{
						if (namesQS[a] == name)
						{
							
							listInputs[i].checked = true;
						}
					}
				}
			}
		}
	}
}

//addLoadEvent(DatesCheckNames);



//Paging code
function GoPage(pageQS, number)
{
	var dirFinal = '';
	var locAux = window.location.href;
	
	//There aren't QueryString
	if (locAux.indexOf('?', 0) == -1)
	{
		dirFinal = locAux + '?' + pageQS + '=' + number;
	}
	else
	{
		//There are QuertString
		var p = pageQS + "=";
		var indBeforePage = locAux.indexOf(p);
		//Not page element in QueryString
		if (indBeforePage == -1)
		{
			dirFinal = locAux + '&' + pageQS + '=' + number;
		}
		else
		{
			//There are page alement in QueryString
			var after = locAux.substring(indBeforePage);
			var indAfterPage = after.indexOf('&');
			//Page element is the last QueryString
			if (indAfterPage == -1)
			{
				var beforePageText = locAux.substring(0,indBeforePage);
				dirFinal = beforePageText + pageQS + '=' + number;
				//alert('Before QS: ' + beforePageText);
			}
			else
			{
				//The page element is in middle of QueryString
				var beforePageText = locAux.substring(0,indBeforePage);
				//alert('Before QS: ' + beforePageText);
				var afterPageText = after.substring(indAfterPage);
				//alert('After QS: ' + afterPageText);
				dirFinal = beforePageText + pageQS + '=' + number + afterPageText;
			}
		}					
	}
	window.location.href = dirFinal;
}




