//	ajax
//	update calendar availability
//	version 2.1

//	define your default vars here
var update_url		=	WEBPATH + 'update_cal.ajax.php';	//	path to ajax file
var image_dir		=	WEBPATH + "images/";				//	path to calendar images
var icon_loading	=	"icon_loading.html";			//	loading animation image
var cal_on			=	"cal_on_";					//	start of cal number file name (om/booked)
var cal_num_img_type=	".png";						//	file type extension (include "." before extension)
var num_w			=	"10px";						//	width of numbers
var num_h			=	"10px";						//	height of numbers
var	image_loading=	"<img src='"+image_dir+icon_loading+"' border='0' width='10' height='10'/>";


//	no need to modify below here unless you know what you are doing ;)

var state_elm	=new Object();
var elm_num		=	new Object();
var special_state= new Object();
var selected_date=null;
var selected_hd_option=1;

function ajax_mod_state(id_item,the_date,the_num,e){	
	
	
	//	2008-03-12
	//	catch client id
	//	NOT USED IN CURRENT VERSION - JUST A TEST
	//id_client=document.getElementById('id_client').value;
	//alert(id_client);
	
	//catch key press
	special_state="pr";
	//the_key=what_key(e)  //comment by Maulik
	//if(the_key!="ko")special_state=the_key;
	//alert(event.keyCode);
	selected_date=the_date;
	//	 start class
	ajax 		=	new sack();
	
	//save selected date to hidden field
	var hd='';
	
	/*alert(document.getElementById("sele_date").value);*/
	var _radio_buttons = new Array();
	var _the_form = document.product;
	for(var _i=0; _i<_the_form.length; _i++)
	{
		var temp = _the_form.elements[_i].type;
		if((temp == "radio") && (_the_form.elements[_i].checked)) { 
			hd = _the_form.elements[_i].value;  
		}
	}
	
	if( document.getElementById("sele_date").value==the_date && document.getElementById("hire_date_no").value==hd)		
		document.getElementById("sele_date").value='';
	else
		document.getElementById("sele_date").value=the_date;
	/*var arr_hdn=new Array();
	arr_hdn=hd.split(";");*/
	selected_hd_option=hd;
	document.getElementById("hire_date_no").value=hd;
	//	 define image span to update
	state_elm=document.getElementById("state_"+the_date);
	// set state loading while ajax is executed
	state_elm.innerHTML = image_loading;
	
	//	define number for grahic to show (needed in completion function)
	elm_num	=	the_num;
	
	//	 call ajax file to update state
	var the_url=update_url+"?id_item="+id_item+"&the_date="+the_date+"&special_state="+special_state+"&hdate="+hd;
	/*alert(the_url);
	return false;*/
	ajax.requestFile = the_url;
	ajax.onCompletion =reset_state;
	ajax.runAJAX();
	
}
function reset_state(){
	//alert(ajax.response);
	
	var arr_msg=new Array();
	var msg=ajax.response;
			
	arr_msg=msg.split(";");
	if(arr_msg[0]=='mp')
	{
		var i=1;
		var date_arr=new Array();	
		var img_date=null;
		var span_date=null;
		var loop_date='';
		var arr_hd_no=new Array();
		
		//document.location="index.php";
		
		arr_hd_no=arr_msg[2].split(":");
		
		if(arr_hd_no.length>0)
		{
			if(arr_hd_no[0]!='')
			{
				span_date=arr_hd_no[0];
				img_date=arr_hd_no[0];
				
				if(arr_hd_no[1]==1)
					loop_date=6;
				else
					loop_date=27;
				
				var elm_num_date=null;
				var elm_num_span_date=null;
				
				elm_num_date=Date.Format(img_date,"yyyy-m-d");
				elm_num_span_date=Date.Format(img_date,"yyyy-mm-dd");
				
				date_arr=elm_num_date.split("-");
				
				for(i=0;i<=loop_date;i++)
				{
					if(i==0)
					{
						state_elm=document.getElementById("state_"+elm_num_span_date);
						state_elm.innerHTML = "<img src='"+image_dir+"cal_off_"+date_arr[2]+cal_num_img_type+"' width='"+num_w+"' height='"+num_h+"' border='0'/>";
					}
					else
					{
						span_date=Date.Format(Date.DateAdd("y", 1, span_date),"yyyy-mm-dd");
						img_date=Date.Format(Date.DateAdd("y", 1, img_date),"yyyy-m-d");
						
						
						date_arr=null;
						date_arr=img_date.split("-");
						state_elm=document.getElementById("state_"+span_date);
						
						state_elm.innerHTML = "<img src='"+image_dir+"cal_off_"+date_arr[2]+cal_num_img_type+"' width='"+num_w+"' height='"+num_h+"' border='0'/>";
					}
				}	
			}			
		}
		
		span_date=selected_date;
		img_date=selected_date;
		loop_date='';
		date_arr=null;
		i=0;
		
		if(selected_hd_option==1)
			loop_date=6;
		else
			loop_date=27;
		for(i=0;i<=loop_date;i++)
		{
			if(i==0)
			{
				state_elm=document.getElementById("state_"+span_date);
				
				state_elm.innerHTML = "<img src='"+image_dir+"cal_"+arr_msg[1]+"_"+elm_num+cal_num_img_type+"' width='"+num_w+"' height='"+num_h+"' border='0'/>";
			}
			else
			{
				span_date=Date.Format(Date.DateAdd("y", 1, span_date),"yyyy-mm-dd");
				img_date=Date.Format(Date.DateAdd("y", 1, img_date),"yyyy-m-d");
				
				
				date_arr=null;
				date_arr=img_date.split("-");
				state_elm=document.getElementById("state_"+span_date);
				
				state_elm.innerHTML = "<img src='"+image_dir+"cal_"+arr_msg[1]+"_"+date_arr[2]+cal_num_img_type+"' width='"+num_w+"' height='"+num_h+"' border='0'/>";
			}
		}
	}
	else if(arr_msg[0]=='jm')
	{
		alert(arr_msg[1]);
		state_elm.innerHTML = "<img src='"+image_dir+"cal_off_"+elm_num+cal_num_img_type+"' width='"+num_w+"' height='"+num_h+"' border='0'/>";
		document.getElementById("sele_date").value="";
	}
	else
	{
		if(arr_msg[0]=='dmp')
		{
			alert("This date is already selected by you and if you wish to cancel the provisional booking then please click on this date: "+ Date.Format(arr_msg[1],"mmmm d, yyyy"));
			state_elm.innerHTML = "<img src='"+image_dir+"cal_pr_"+elm_num+cal_num_img_type+"' width='"+num_w+"' height='"+num_h+"' border='0'/>";
			document.getElementById("sele_date").value="";
		}
		else
		{
			alert(msg);
			document.getElementById("sele_date").value="";
		}
	}
	
	
	/*if (ajax.response=="on"
	||  ajax.response=="off"
	|| 	ajax.response=="pr"
	){
			
			
			
			//state_elm.innerHTML = "<img src='"+image_dir+"cal_"+ajax.response+"_"+elm_num+cal_num_img_type+"' width='"+num_w+"' height='"+num_h+"' border='0'/>";
			//document.location="http://localhost:8080/Projects/site_bc/index.php?route=product/product&path="+document.getElementById('id_path').value+"&product_id="+document.getElementById('product_id').value;
			
	}else{
		alert(ajax.response);
	}*/
}

function what_key(e) {

   if(document.all)e = event; // IE need this
	//alert(e);
if(e.shiftKey)   return "pr";
	else return "ko";
}
