var xmlHttp
var currentTd

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	  {
	  	// Firefox, Opera 8.0+, Safari
		  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  	// Internet Explorer
	  try
	    {
	    	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	    }
	  catch (e)
	    {
	   		 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	    }
	  }
	return xmlHttp;
}


function change_others(current_item)
{
	

	var course=''
	var country=''
	var city=''
	var date=''
	var date_changed='';
	var all_date='';
	var get_item='';
	
	if	(current_item!='from_monthes' && current_item!='from_years' &&
		 current_item!='to_monthes' && current_item!='to_years')
		 date_changed=0;
	else
		date_changed=1;
		
	
	
	
	if(document.getElementById('courses_select'))
		var course=document.getElementById('courses_select').value
		
	if(document.getElementById('countries_select'))
		var country=document.getElementById('countries_select').value
		
	if(document.getElementById('cities_select'))	
		var city=document.getElementById('cities_select').value
	
//	if(document.getElementById('dates_select'))
//	var date=document.getElementById('dates_select').value
	
	if(document.getElementById('monthes_select_from'))
		var from_month=document.getElementById('monthes_select_from').value
	
	if(document.getElementById('monthes_select_to'))
		var to_month=document.getElementById('monthes_select_to').value
	
	if(document.getElementById('years_select_from'))
		var from_year=document.getElementById('years_select_from').value
	
	if(document.getElementById('years_select_to'))
		var to_year=document.getElementById('years_select_to').value
	
	if(document.getElementById('min_year'))
		var min_year=document.getElementById('min_year').value
		
	if(document.getElementById('max_year'))
		var max_year=document.getElementById('max_year').value
	
	
		
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 	 alert ("Your browser does not support AJAX!");
	  	return;
	 } 
	 
	 if(current_item=='')
	 {
	 	return;
	 }
  

	 
	 //send object to each file except what's changed
	if(current_item!='courses')
	 {

	 	xmlHttp_courses=GetXmlHttpObject()
		var url="../includes/ajax/get_courses_ajax.php"
		
		url=url+"?current=" + current_item
		url=url+"&course=" + course
		url=url+"&country=" + country
		url=url+"&city="+ city
		url=url+"&t_month="+ to_month
		url=url+"&t_year="+ to_year
		url=url+"&f_month="+ from_month
		url=url+"&f_year="+ from_year
		url=url+"&max_year="+max_year
		url=url+"&min_year="+min_year
		url=url+"&sid=" +Math.random()
		 
		xmlHttp_courses.onreadystatechange=coursesChanged
		xmlHttp_courses.open("GET",url,true);
		xmlHttp_courses.send(null);

	 }
	 
	 
	if(current_item!='countries')
	 {
	 

	 	xmlHttp_countries=GetXmlHttpObject()
		var url="../includes/ajax/get_countries_ajax.php"
	
		url=url+"?current="+current_item
		url=url+"&course="+course
		url=url+"&country="+country
		url=url+"&city="+city
		url=url+"&t_month="+to_month
		url=url+"&t_year="+to_year
		url=url+"&f_month="+from_month
		url=url+"&f_year="+from_year
		url=url+"&max_year="+max_year
		url=url+"&min_year="+min_year
		url=url+"&sid="+Math.random()
		 
		xmlHttp_countries.onreadystatechange=countriesChanged
		xmlHttp_countries.open("GET",url,true);
		xmlHttp_countries.send(null);
	
	 }
	 
	 
	if(current_item!='cities')
	 {
	 	xmlHttp_cities=GetXmlHttpObject()
		var url="../includes/ajax/get_cities_ajax.php"
		
		url=url+"?current="+current_item
		url=url+"&course="+course
		url=url+"&country="+country
		url=url+"&city="+city
		url=url+"&t_month="+to_month
		url=url+"&t_year="+to_year
		url=url+"&f_month="+from_month
		url=url+"&f_year="+from_year
		url=url+"&max_year="+max_year
		url=url+"&min_year="+min_year
		url=url+"&sid="+Math.random()
		
		xmlHttp_cities.onreadystatechange=citiesChanged
		xmlHttp_cities.open("GET",url,true);
		xmlHttp_cities.send(null);
	 }
	 
	 
	 //if the changed item not one of the dates, change the whole block of the date, to write it once only
	if	(!date_changed)
	 {
	 	all_date=1
	 	xmlHttp_dates=GetXmlHttpObject()
		var url="../includes/ajax/get_dates_ajax.php"
			
		url=url+"?current="+current_item
		url=url+"&course="+course
		url=url+"&country="+country
		url=url+"&city="+city
		url=url+"&t_month="+to_month
		url=url+"&t_year="+to_year
		url=url+"&f_month="+from_month
		url=url+"&f_year="+from_year
		url=url+"&max_year="+max_year
		url=url+"&min_year="+min_year
		url=url+"&all_date="+all_date		
		url=url+"&sid="+Math.random()
		 
		xmlHttp_dates.onreadystatechange=datesChanged
		xmlHttp_dates.open("GET",url,true);
		xmlHttp_dates.send(null);
	 }
	
	 
	 
	 
	 //handle the case that if one of the date is changed and we want to change the related 
	 //year or month
	 if(date_changed)
	 {
	 	
	 	all_date=0
	 
	 	if(current_item=='from_monthes')
		 {
	 	
		  	xmlHttp_from_monthes=GetXmlHttpObject()
			
		 	var url="../includes/ajax/get_dates_ajax.php"
				
			url=url+"?current="+current_item
			url=url+"&course="+course
			url=url+"&country="+country
			url=url+"&city="+city
			url=url+"&t_month="+to_month
			url=url+"&t_year="+to_year
			url=url+"&f_month="+from_month
			url=url+"&f_year="+from_year
			url=url+"&max_year="+max_year
			url=url+"&min_year="+min_year
			url=url+"&all_date="+all_date
			url=url+"&get_item="+"from_years"		
			url=url+"&sid="+Math.random()
			 
			xmlHttp_from_monthes.onreadystatechange=from_monthesChanged
			xmlHttp_from_monthes.open("GET",url,true);
			xmlHttp_from_monthes.send(null);
		 }//to_monthes
		 	

	 	if(current_item=='from_years')
		 {
	
		 	xmlHttp_from_years=GetXmlHttpObject()
			
		 	var url="../includes/ajax/get_dates_ajax.php"
				
			url=url+"?current="+current_item
			url=url+"&course="+course
			url=url+"&country="+country
			url=url+"&city="+city
			url=url+"&t_month="+to_month
			url=url+"&t_year="+to_year
			url=url+"&f_month="+from_month
			url=url+"&f_year="+from_year
			url=url+"&all_date="+all_date
			
			url=url+"&max_year="+max_year
			url=url+"&min_year="+min_year

			url=url+"&get_item="+"from_monthes"		
			url=url+"&sid="+Math.random()

			xmlHttp_from_years.onreadystatechange=from_yearsChanged
			xmlHttp_from_years.open("GET",url,true);
			xmlHttp_from_years.send(null);
		 }//to_years
		 	
	 	if(current_item=='to_monthes')
		 {
		 	xmlHttp_to_monthes=GetXmlHttpObject()
			
		 	var url="../includes/ajax/get_dates_ajax.php"
				
			url=url+"?current="+current_item
			url=url+"&course="+course
			url=url+"&country="+country
			url=url+"&city="+city
			url=url+"&t_month="+to_month
			url=url+"&t_year="+to_year
			url=url+"&f_month="+from_month
			url=url+"&f_year="+from_year
			url=url+"&max_year="+max_year
			url=url+"&min_year="+min_year

			url=url+"&all_date="+all_date
			url=url+"&get_item="+"to_years"		
	
			url=url+"&sid="+Math.random()
			 
			xmlHttp_to_monthes.onreadystatechange=to_monthesChanged
			xmlHttp_to_monthes.open("GET",url,true);
			xmlHttp_to_monthes.send(null);
		 }//to_monthes
		 	
	 	if(current_item=='to_years')
		 {
		 	xmlHttp_to_years=GetXmlHttpObject()
			
		 	var url="../includes/ajax/get_dates_ajax.php"
				
			url=url+"?current="+current_item
			url=url+"&course="+course
			url=url+"&country="+country
			url=url+"&city="+city
			url=url+"&t_month="+to_month
			url=url+"&t_year="+to_year
			url=url+"&f_month="+from_month
			url=url+"&f_year="+from_year
			
			url=url+"&max_year="+max_year
			url=url+"&min_year="+min_year

			url=url+"&all_date="+all_date		
			url=url+"&get_item=" + "to_monthes"		
			url=url+"&sid="+Math.random()
			 
			xmlHttp_to_years.onreadystatechange=to_yearsChanged
			xmlHttp_to_years.open("GET",url,true);
			xmlHttp_to_years.send(null);
		 }//to_monthes
		 	
			 
	 }
		 
	 
}





function coursesChanged() 
{ 
	if (xmlHttp_courses.readyState==4)
	{ 
			document.getElementById("courses_td").innerHTML=xmlHttp_courses.responseText;
	}
}


function countriesChanged() 
{ 
	if (xmlHttp_countries.readyState==4)
	{ 
			document.getElementById("countries_td").innerHTML=xmlHttp_countries.responseText
		
	}
}


function citiesChanged() 
{ 
	if (xmlHttp_cities.readyState==4)
	{ 
			document.getElementById("cities_td").innerHTML=xmlHttp_cities.responseText;
	}
}

function datesChanged() 
{ 
	if (xmlHttp_dates.readyState==4)
	{ 
			document.getElementById("dates_td").innerHTML=xmlHttp_dates.responseText;
	}
}


function from_monthesChanged()
{
	if (xmlHttp_from_monthes.readyState==4)
	{ 
	
			document.getElementById("from_years_td").innerHTML=xmlHttp_from_monthes.responseText;
	}
}


function from_yearsChanged()
{
	
	
	if (xmlHttp_from_years.readyState==4)
	{ 
			document.getElementById("from_monthes_td").innerHTML=xmlHttp_from_years.responseText;
	}
}


function to_monthesChanged()
{
	if (xmlHttp_to_monthes.readyState==4)
	{ 
			document.getElementById("to_years_td").innerHTML=xmlHttp_to_monthes.responseText;
	}
}


function to_yearsChanged()
{
	if (xmlHttp_to_years.readyState==4)
	{ 
			document.getElementById("to_monthes_td").innerHTML=xmlHttp_to_years.responseText;
	}
}



function checkOnSubmit()
{
	
	document.getElementById('error_msg_td').innerHTML=''
	var invaled_item=false
	//this function just make sure that from_date<=to_date
	
	if(document.getElementById('monthes_select_from'))
		var from_month=document.getElementById('monthes_select_from').value
	
	if(document.getElementById('monthes_select_to'))
		var to_month=document.getElementById('monthes_select_to').value
	
	if(document.getElementById('years_select_from'))
		var from_year=document.getElementById('years_select_from').value
	
	if(document.getElementById('years_select_to'))
		var to_year=document.getElementById('years_select_to').value
	
	
	from_month=parseInt(from_month)
	to_month=parseInt(to_month)
	from_year=parseInt(from_year)
	to_year=parseInt(to_year)
	
	if( to_year > from_year )
		invaled_item= true
		
	else if( to_year < from_year )	
		invaled_item=  false
		
	else //equals--->check monthes
	{
		if (to_month < from_month)
			invaled_item=  false
		else
			invaled_item=  true
	}
	

	
	if(invaled_item==false)
	{
		document.getElementById('error_msg_td').innerHTML=' * To date can\'t be before the From date.. '
		return false
	}
	
		
}