var ajax_processing;
var ajax_active;
var returned_xml;
var current_ajax_date;
var search_date;
var do_redirect = false;

////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
function store_product_dates(calendar_date,product_idx,id){


if(product_idx=="0"){

////// adding a event add / remove hidden inputs to be stored


if(document.getElementById("calendar_date_" + calendar_date)){


var elm = document.forms['editproduct'].elements['calendar_date_' + calendar_date];    
elm.parentNode.removeChild(elm); 
id.className="day_box"

}
else
{



	newField = document.createElement("input");
	newField.type = "hidden";
	newField.name = "calendar_date_" + calendar_date;
	newField.value = calendar_date;
	newField.id = "calendar_date_" + calendar_date;
	document.forms['editproduct'].appendChild(newField);

	id.className="day_box_on"
}

}
else
{

//// updating event use ajax to store straight in db

  	if(!ajax_active){	

	var ajax_time = new Date()

	returned_xml = ajaxXML("/admin/ajax_date.asp?product_idx=" + product_idx + "&calendar_date=" + calendar_date + "&ajax_time=" + ajax_time);
  	ajax_active = true;
  	setTimeout("store_product_dates('',1,'')",500);
	current_ajax_date = id;
	}
	else
	{
		if(!ajax_processing){
		
			ajax_active = false;
			
			if(current_ajax_date.className=="day_box_on"){

				current_ajax_date.className="day_box"

			}
			else
			{
				
				current_ajax_date.className="day_box_on"
			
			}
		
		}
		else
		{
		
		setTimeout("store_product_dates('',1,'')",500);
		
		}
	}
}

}
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
function ajaxXML(Xml_URL)
  {  
  
  var xmlReturn;
  try
    {    // Firefox, Opera 8.0+, Safari    
	xmlReturn=new XMLHttpRequest();   
	 }
  catch (e)
    {    // Internet Explorer
	  try
      {      
	 xmlReturn=new ActiveXObject("Msxml2.XMLHTTP");
	   }
    catch (e)
      {
	       try
        {
		       xmlReturn=new ActiveXObject("Microsoft.XMLHTTP");
			           }
      catch (e)
        {       
		 alert("Your browser does not support AJAX!");
		  return false; 
		   }
		 }
		}
    xmlReturn.onreadystatechange=function()
      {
      if(xmlReturn.readyState==4)
        {
		ajax_processing = false;
        //document.getElementById(ID_Name).innerHTML=xmlHttp.responseText;
        //document.getElementById(ID_Name).innerHTML=
		returned_xml = xmlReturn.responseXML;
        }
      }
    xmlReturn.open("GET",Xml_URL,true);
    xmlReturn.send(null);  
	}
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
function calendar_multi_select(product_idx,start_date,select_type){

if(product_idx=="0"){

	multi_calendar_dates(start_date,select_type);
	
}
else
{

  	if(!ajax_active){	

	var ajax_time = new Date()
	document.getElementById("year_event_calendar").innerHTML="<div align='center'><img src='/images/loading.gif'><br>Updating calendar</div>";
	ajaxPage("/admin/ajax_multi_date.asp?select_type=" + select_type + "&product_idx=" + product_idx + "&start_date=" + start_date + "&ajax_time=" + ajax_time,"year_event_calendar");
  	ajax_active = true;
	ajax_processing = true;

  	setTimeout("calendar_multi_select(1,'',1)",500);
	}
	else
	{
		if(!ajax_processing){
		
			ajax_active = false;
			
		
		}
		else
		{
		
			setTimeout("calendar_multi_select(1,'',1)",500);
		
		}
	}

}	

}
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
function multi_calendar_dates(start_date,select_type){

var todays_date = new Date()

//if(select_type==1||select_type==7||select_type==5||select_type==6){

	//todays_date = dateAdd("d",-1,todays_date);

//}

var current_date = new Date()

var date_split = start_date.split("/")

current_date.setFullYear(date_split[2]);
current_date.setMonth(date_split[1]*1-1);
current_date.setDate(date_split[0]);

//alert(current_date.getDate());

var end_date = new Date()

end_date.setMonth(11);
//end_date.setFullYear(start_date.getFullYear());
end_date.setDate(31);


//alert("here " + todays_date);
//alert("here " + end_date);

	while (current_date<=end_date)
	{
	
	
	if(Date.parse(current_date)>=Date.parse(todays_date)){

					//alert("here " + current_date);
		
					day_of_week = current_date.getDay()
					
					//alert("day_of_week = " + day_of_week);
					
					switch(select_type)
					{
					case 1:
					
						if(day_of_week!=0&&day_of_week<6){
										
							multi_save_date(current_date,true);
										
						}
						
					  break;    
					  
					case 2:
					
						if(day_of_week==6||day_of_week==0){
										
							multi_save_date(current_date,true);
										
						}
						
					  break;   
					  
					case 3:
					
						if(day_of_week==6){
										
							multi_save_date(current_date,true);
										
						}
						
					  break;   
					  
					case 4:
					
						if(day_of_week==0){
								//alert("Sunday date = " + current_date);		
							multi_save_date(current_date,true);
										
						}
						
					  break;   
			
					case 5:
					
							multi_save_date(current_date,false);
						
					  break;  
					  
					case 6:
					
							multi_save_date(current_date,true);
						
					  break; 
					  
					case 7:
					
						if(day_of_week!=0&&day_of_week<6){
										
							multi_save_date(current_date,false);
										
						}
						
					  break;  
					  
					case 8:
					
						if(day_of_week==6||day_of_week==0){
										
							multi_save_date(current_date,false);
										
						}
						
					  break;   
					  
					case 9:
					
						if(day_of_week==6){
										
							multi_save_date(current_date,false);
										
						}
						
					  break;   
					  
					case 10:
					
						if(day_of_week==0){
										
							multi_save_date(current_date,false);
										
						}
						
					  break;   
					  
					default:
					}
		}
		else
		{
			//alert(current_date.valueOf())
		
		}
		
				current_date = dateAdd("d",1,current_date)
				//alert(current_date);
		
	}

}

////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
function multi_save_date(input_date,store){


		//str_date = input_date.toString();
		//str_date = str_date.split(" ");


		var temp_day = input_date.getDate();
		var temp_month =  input_date.getMonth()+1;
		var temp_year =  input_date.getFullYear();
		
		var calendar_date = temp_day + "/" + temp_month + "/" + temp_year;

		//alert(calendar_date);

	if(!store){
	
			if(document.getElementById("calendar_date_" + calendar_date)){
			
					var elm = document.forms['editproduct'].elements['calendar_date_' + calendar_date];    
					elm.parentNode.removeChild(elm); 
					
					document.getElementById("day_box_" + calendar_date).className="day_box";
			
			}
	}
	else
	{
	
	//alert("store");
	
		if(!document.getElementById("calendar_date_" + calendar_date)){
	
	
		newField = document.createElement("input");
		newField.type = "hidden";
		newField.name = "calendar_date_" + calendar_date;
		newField.value = calendar_date;
		newField.id = "calendar_date_" + calendar_date;
		document.forms['editproduct'].appendChild(newField);
	
		document.getElementById("day_box_" + calendar_date).className="day_box_on";
		
		}
	}


}
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
function update_month_calendar(calendar_date,product_idx){

				
				if(!ajax_active){	
			
		
						var ajax_time = new Date()
						document.getElementById("month_calendar").innerHTML="<div align='center'><img src='/images/loading.gif'><br>Updating calendar</div>";
						
						if(product_idx!=0){
						
						selected_date = document.basket1.date_selected.value;
						//alert("here");
						}
						else
						{
						
						//alert("There");
						selected_date = search_date;
						
						}
						
						ajaxPage("/inc/month_calendar.asp?calendar_date=" + calendar_date + "&date_selected=" + selected_date + "&product_idx=" + product_idx + "&ajax_time=" + ajax_time,"month_calendar");
						ajax_active = true;
						ajax_processing = true;
						setTimeout("update_month_calendar('')",500);
				}
				else
				{
					if(!ajax_processing){
					
						ajax_active = false;
						
						if(do_redirect){
						
							//alert("Redirect to search results for " + search_date);
							document.location = "/breaks_by_date.asp?search_date=" + search_date;
						}
					
					}
					else
					{
					
						setTimeout("update_month_calendar('')",500);
					
					}

				}
				

}
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
function select_calendar_day(selected_date){

	document.basket1.date_selected.value = selected_date;
	
}

////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
function select_search_date(selected_date){

search_date = selected_date;
do_redirect = true;

}
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
