/*----------------------------------------------*/
/*				  Ghrs Calendar 				*/
/*----------------------------------------------*/

/* Global variables */
var ghrs_calendar_div; 
var ghrs_jsDate = new Array(); 
var ghrs_loaded = false;
var ghrs_list_random;
var ghrs_added_functions = new Array();


/*------------------------------------------------------------------------------*/
/* Next function has the behaviour of set up some values and to call some 		*/
/* functions at the page "onload" event. 										*/
/*------------------------------------------------------------------------------*/
function ghrs_setWindowOnload(){
	var oldHandler = window.onload;
	window.onload = function() {
		if(oldHandler) oldHandler();		
		ghrs_loaded = true;
		ghrs_onload_setted = true;
		if(String(navigator.userAgent).search("Opera") > -1) ghrs_setSelectInOperaDimension();
	}
}
/*------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------------*/
/* Next function has the behaviour of add a function at the page "onload" event */
/*------------------------------------------------------------------------------*/
function ghrs_addToOnLoad(func){
	var oldHandler = window.onload;
	window.onload = function() {
		if(oldHandler) oldHandler();		
		eval(func);
	}
}
/*------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------------*/
/* The nexts functions allows us to obtain the correct name of the month, the 	*/
/* correct name of the day and the correct number of days in a month. 			*/
/*------------------------------------------------------------------------------*/
function ghrs_getMonthName(i){
	return months[i];
}

function ghrs_getDayName(i){
	return dayNames[i];
}

function ghrs_daysInMonth(month,year) {
	var months_days = [31,28,31,30,31,30,31,31,30,31,30,31];
	if (month != 2) return months_days[month - 1];
	if (year%4 != 0) return months_days[1];
	if (year%100 == 0 && year%400 != 0) return months_days[1];
	return months_days[1] + 1;
}
/*------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------------*/
/* The next functions are called from the selects elements in the page in which */
/* a  calendar is contained to set correctly day, month and year desired. 		*/
/*------------------------------------------------------------------------------*/

/* This function set correctly day, month and year of check in and check out date. */
function ghrs_setCalendarCiDay(random,prefix){
	
	// If spans do not exist, we have to create them
	if(ghrs_fixCalendarHtmlElements(random,null)) ghrs_addCalendarElements(random,null);
	
	// Loading variables placed inside an html p element's attributes.
	// The use of p element is due to diversify the dates containers.
	var ci_day = parseInt(document.getElementById("ci_"+random+"_day").innerHTML,10);
	var ci_month = parseInt(document.getElementById("ci_"+random+"_month").innerHTML,10);
	var ci_year = parseInt(document.getElementById("ci_"+random+"_year").innerHTML,10);
	var co_day = parseInt(document.getElementById("co_"+random+"_day").innerHTML,10);
	var co_month = parseInt(document.getElementById("co_"+random+"_month").innerHTML,10);
	var co_year = parseInt(document.getElementById("co_"+random+"_year").innerHTML,10);
	// Next series of checks is used to set up correcly day, month and year
	// in the p's check-in as in the p's check out dates.
	var days_in_month = ghrs_daysInMonth(ci_month,ci_year);
	ci_day = parseInt(document.getElementById('ci_'+prefix+'_day_'+random).value,10);
	if((ci_year == last_year)&&(ci_day == 31)&&(ci_month == 12)){
		co_day = ci_day;
		document.getElementById('co_'+prefix+'_day_'+random).value = co_day;
		ci_day = ci_day - 1;
		document.getElementById('ci_'+prefix+'_day_'+random).value = ci_day;
		document.getElementById('co_'+prefix+'_month_'+random).value = ci_month;
	}
	else {
		if(ci_day + 1 <= days_in_month){
			co_day = ci_day + 1;
			document.getElementById('co_'+prefix+'_day_'+random).value = co_day;
			co_month = ci_month;
			document.getElementById('co_'+prefix+'_month_'+random).value = co_month;
			co_year = ci_year;
			document.getElementById('co_'+prefix+'_year_'+random).value= co_year;
		}
		else{
			ci_day = days_in_month;
			document.getElementById('ci_'+prefix+'_day_'+random).value = ci_day;
			co_day = 1;
			document.getElementById('co_'+prefix+'_day_'+random).value = co_day;
			if((ci_month + 1) <= 12){
				co_month = ci_month + 1;
				document.getElementById('co_'+prefix+'_month_'+random).value = co_month;
				co_year = ci_year;
				document.getElementById('co_'+prefix+'_year_'+random).value= co_year;
			}
			else{
				co_month = 1;
				document.getElementById('co_'+prefix+'_month_'+random).value = co_month;
				co_year = ci_year + 1;
				document.getElementById('co_'+prefix+'_year_'+random).value= co_year;
			}
		}
	}
	
	// Saving variables placed inside an html p element's attributes and
	// in the global variables.
	document.getElementById("ci_"+random+"_day").innerHTML = ci_day;
	document.getElementById("ci_"+random+"_month").innerHTML = ci_month;
	document.getElementById("ci_"+random+"_year").innerHTML = ci_year;
	document.getElementById("co_"+random+"_day").innerHTML = co_day;
	document.getElementById("co_"+random+"_month").innerHTML = co_month;
	document.getElementById("co_"+random+"_year").innerHTML = co_year;
	document.getElementById("ci_"+random+"_temp_month").innerHTML = ci_month;
	document.getElementById("ci_"+random+"_temp_year").innerHTML = ci_year;
	document.getElementById("co_"+random+"_temp_month").innerHTML = co_month;
	document.getElementById("co_"+random+"_temp_year").innerHTML = co_year;
}

/* This function set correctly the check in month and then call the 'ghrs_setCalendarCiDay' */
/* function to change correctly also the day of the date. */
function ghrs_setCalendarCiMonth(random,prefix){
	
	// If spans do not exist, we have to create them
	if(ghrs_fixCalendarHtmlElements(random,null)) ghrs_addCalendarElements(random,null);
	
	// Loading variables placed inside an html p element's attributes.
	var ci_month = parseInt(document.getElementById("ci_"+random+"_month").innerHTML,10);
	var co_month = parseInt(document.getElementById("co_"+random+"_month").innerHTML,10);
	
	ci_month = parseInt(document.getElementById('ci_'+prefix+'_month_'+random).value,10);
	co_month = ci_month;
	document.getElementById('co_'+prefix+'_month_'+random).value = co_month;
	
	// Saving variables placed inside an html p element's attributes.
	document.getElementById("ci_"+random+"_month").innerHTML = ci_month;
	document.getElementById("co_"+random+"_month").innerHTML = co_month;
	
	if(document.getElementById('ci_'+prefix+'_day_'+random)) ghrs_setCalendarCiDay(random,prefix);
	
}

/* This function set correctly the check in year and then call the 'ghrs_setCalendarCiMonth' */
/* function to change correctly also the month and the day of the date. */
function ghrs_setCalendarCiYear(random,prefix){
	
	// If spans do not exist, we have to create them
	if(ghrs_fixCalendarHtmlElements(random,null)) ghrs_addCalendarElements(random,null);
	
	// Loading variables placed inside an html p element's attributes.
	var ci_year = parseInt(document.getElementById("ci_"+random+"_year").innerHTML,10);
	var co_year = parseInt(document.getElementById("co_"+random+"_year").innerHTML,10);
	
	ci_year = parseInt(document.getElementById('ci_'+prefix+'_year_'+random).value,10);
	co_year = ci_year;
	document.getElementById('co_'+prefix+'_year_'+random).value = co_year;
	
	// Saving variables placed inside an html p element's attributes.
	document.getElementById("ci_"+random+"_year").innerHTML = ci_year;
	document.getElementById("co_"+random+"_year").innerHTML = co_year;
	
	if(document.getElementById('ci_'+prefix+'_month_'+random))ghrs_setCalendarCiMonth(random,prefix);
	
}

/* This function set correctly the check out day. */
function ghrs_setCalendarCoDay(random,prefix){
	
	// If spans do not exist, we have to create them.
	if(ghrs_fixCalendarHtmlElements(random,null)) ghrs_addCalendarElements(random,null);
	
	// Loading variables placed inside an html p element's attributes.
	var co_day = parseInt(document.getElementById("co_"+random+"_day").innerHTML,10);
	
	co_day = parseInt(document.getElementById('co_'+prefix+'_day_'+random).value,10);
	
	// Saving variables placed inside an html p element's attributes.
	document.getElementById("co_"+random+"_day").innerHTML = co_day;
}

/* This function set correctly the check out month. */
function ghrs_setCalendarCoMonth(random,prefix){
	
	// If spans do not exist, we have to create them.
	if(ghrs_fixCalendarHtmlElements(random,null)) ghrs_addCalendarElements(random,null);
	
	// Loading variables placed inside an html p element's attributes.
	var co_month = parseInt(document.getElementById("co_"+random+"_month").innerHTML,10);
	
	co_month = parseInt(document.getElementById('co_'+prefix+'_month_'+random).value,10);
	
	// Saving variables placed inside an html p element's attributes.
	document.getElementById("co_"+random+"_month").innerHTML = co_month;
	document.getElementById("co_"+random+"_temp_month").innerHTML = co_month;
}

/* This function set correctly the check out year. */
function ghrs_setCalendarCoYear(random,prefix){
	
	// If spans do not exist, we have to create them.
	if(ghrs_fixCalendarHtmlElements(random,null)) ghrs_addCalendarElements(random,null);
	
	// Loading variables placed inside an html p element's attributes.
	var p_co = "p_co_"+random;
	var co_year = parseInt(document.getElementById("co_"+random+"_year").innerHTML,10);
	
	co_year = parseInt(document.getElementById('co_'+prefix+'_year_'+random).value,10);
	
	// Saving variables placed inside an html p element's attributes.
	document.getElementById("co_"+random+"_year").innerHTML = co_year;
	document.getElementById("co_"+random+"_temp_year").innerHTML = co_year;
}
/*------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------------*/
/* The next functions has the behaviour to reset the previous values of the 	*/
/* calendar and of the selects in case that it has been closed. 				*/
/*------------------------------------------------------------------------------*/

/* This function has the behavior to reflesh select elements. */
function ghrs_setCalendarDayBase(random,check,prefix){
	// Loading variables placed inside an html p element's attributes.
	var day = parseInt(document.getElementById(check+random+"_day").innerHTML,10);
	var month = parseInt(document.getElementById(check+random+"_temp_month").innerHTML,10);
	var year = parseInt(document.getElementById(check+random+"_temp_year").innerHTML,10);	
	
	document.getElementById(check+prefix+'_day_'+random).value = day;
	document.getElementById(check+prefix+'_month_'+random).value = month;
	document.getElementById(check+prefix+'_year_'+random).value = year;
	document.getElementById(check+random+"_month").innerHTML = month;
	document.getElementById(check+random+"_year").innerHTML = year;
	
	if(check=='ci_'){
		ghrs_setCalendarCiYear(random,prefix);
	}
	else{
		ghrs_setCalendarCoDay(random,prefix);
		ghrs_setCalendarCoMonth(random,prefix);
		ghrs_setCalendarCoYear(random,prefix);
	}
	if(document.getElementById('ci_'+prefix+'_text_'+random)){
		day = document.getElementById('ci_'+prefix+'_day_'+random).value;
		month = document.getElementById('ci_'+prefix+'_month_'+random).value;
		year = document.getElementById('ci_'+prefix+'_year_'+random).value;
		document.getElementById('ci_'+prefix+'_text_'+random).value = ghrs_dateToText(day,month,year);
	}
	if(document.getElementById('co_'+prefix+'_text_'+random)){
		day = document.getElementById('co_'+prefix+'_day_'+random).value;
		month = document.getElementById('co_'+prefix+'_month_'+random).value;
		year = document.getElementById('co_'+prefix+'_year_'+random).value;
		document.getElementById('co_'+prefix+'_text_'+random).value = ghrs_dateToText(day,month,year);
	}
	/* Enable/disable calendar input text */
	if(document.getElementById(check+prefix+'_text_'+random)){
		if(document.getElementById(check+prefix+'_text_'+random).disabled){
			document.getElementById(check+prefix+'_text_'+random).disabled = false;
		}
	}
	
	/* If the calendar is in the AB file and in the add range element, we have to refresh all ranges date */
	if(prefix=='ab_add' && $('#'+prefix+'_element')) ghrs_refreshAllABRanges('date');
}

/* This function is called when the calendar is closed correctly with a change of any kind*/
/* of data. */
function ghrs_calendarSelfClose(random,check,prefix){
	var slcElementDay = check + "day_slc_" + random;
	var slcElementMonth = check + "month_slc_" + random;
	var slcElementYear = check + "year_slc_" + random;
	var day = parseInt(document.getElementById(check+random+"_day").innerHTML,10);
	var month = parseInt(document.getElementById(check+random+"_month").innerHTML,10);
	var year = parseInt(document.getElementById(check+random+"_year").innerHTML,10);
	
	if(check=='ci_'){
		document.getElementById("ci_"+random+"_day").innerHTML = day;
		document.getElementById("ci_"+random+"_month").innerHTML = month;
		document.getElementById("ci_"+random+"_year").innerHTML = year;
		document.getElementById("ci_"+random+"_temp_month").innerHTML = month;
		document.getElementById("ci_"+random+"_temp_year").innerHTML = year;
	}
	else{
		document.getElementById("co_"+random+"_day").innerHTML = day;
		document.getElementById("co_"+random+"_month").innerHTML = month;
		document.getElementById("co_"+random+"_year").innerHTML = year;
		document.getElementById("co_"+random+"_temp_month").innerHTML = month;
		document.getElementById("co_"+random+"_temp_year").innerHTML = year;
	}
	if(document.getElementById(check+prefix+'_text_'+random)){
		if(document.getElementById(check+prefix+'_text_'+random).disabled){
			document.getElementById(check+prefix+'_text_'+random).disabled = false;
		}
	}
	document.onkeydown = document.onkeypress = null;
}
/*------------------------------------------------------------------------------*/
function ghrs_disableBackspace() {
	document.onkeydown = document.onkeypress = function (event){ 
		if(typeof event == 'undefined') event = window.event;
		if(event) {
			var keyCode = event.keyCode ? event.keyCode : event.charCode;
			if (keyCode == 8) {
				if (event.preventDefault) {
					event.preventDefault();
				}
				return false;
			}
			else {
				return true;
			}
		}
		else {
			return true;
		}
	}
}

/*------------------------------------------------------------------------------*/
/* The next functions are used from the top buttons of the calendar to set up  	*/
/* correctly the month selected. 								 				*/
/*------------------------------------------------------------------------------*/

/* This function is used from the left button to set up calendar's month correctly. */
function ghrs_sxSetsMonth(check,random,prefix){
	var month = parseInt(document.getElementById(check+random+"_temp_month").innerHTML,10);
	var year = parseInt(document.getElementById(check+random+"_temp_year").innerHTML,10);
	month -= 1;
	ghrs_setButton('dx',check,random,prefix);
	if(month==today_month && year==today_year){
		ghrs_removeButton('sx',check,random);
		ghrs_saveModify(month,year,check,random);
		ghrs_changeCalendarDays(month,year,random,check,prefix)
	}
	else{
		ghrs_setButton('sx',check,random,prefix);
		if(month < 1){
			month = 12;
			year -= 1;
		}
		ghrs_saveModify(month,year,check,random);
		ghrs_changeCalendarDays(month,year,random,check,prefix)
	}
}

/* This function is used from the right button to set up calendar's month correctly. */
function ghrs_dxSetsMonth(check,random,prefix){
	var month = parseInt(document.getElementById(check+random+"_temp_month").innerHTML,10);
	var year = parseInt(document.getElementById(check+random+"_temp_year").innerHTML,10);
	month += 1;
	ghrs_setButton('sx',check,random,prefix);
	if(month==12 && year==today_year+1){
		ghrs_removeButton('dx',check,random);
		ghrs_saveModify(month,year,check,random);
		ghrs_changeCalendarDays(month,year,random,check,prefix)
	}
	else{
		ghrs_setButton('dx',check,random,prefix);
		if(month > 12){
			month = 1;
			year += 1;
		}
		ghrs_saveModify(month,year,check,random);
		ghrs_changeCalendarDays(month,year,random,check,prefix)
	}
}

/* This function is used to display the button when it is hidden. */
function ghrs_setButton(pos,check,random,prefix){
	var elem = document.getElementById("calendar_"+ pos +"_button");
	if(pos == 'dx'){
		elem.onclick = function() { ghrs_dxSetsMonth(check, random,prefix); };
		elem.style.cursor = 'pointer';
		document.getElementById("calendar_right_arrow").style.display = "";
	}
	else{
		elem.onclick = function() { ghrs_sxSetsMonth(check, random,prefix); };
		elem.style.cursor = 'pointer';
		document.getElementById("calendar_left_arrow").style.display = "";		
	}
}

/* This function is used to hide the button when it is displayed. */
function ghrs_removeButton(pos,check,random){
	var elem = document.getElementById("calendar_"+ pos +"_button");
	if (elem) {
		elem.style.cursor = 'default';
		elem.onclick = function() { };
		ghrs_disableColumnSelection(elem, 1);
		if(pos == 'dx') document.getElementById("calendar_right_arrow").style.display = "none";
		else document.getElementById("calendar_left_arrow").style.display = "none";
	}
}

/* This function has the behaviour to save changes maded by buttons */
function ghrs_saveModify(month,year,check,random){
	document.getElementById(check+random+"_temp_month").innerHTML = month;
	document.getElementById(check+random+"_temp_year").innerHTML = year;
	document.getElementById("calendar_month").innerHTML = ghrs_getMonthName(month - 1) + ' ' + year;
}
/*------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------------*/
/* The next functions have the behaviour to set up correctly the calendar code 	*/
/* when the user change some values. 											*/
/*------------------------------------------------------------------------------*/

/* This function has the behaviour to change the calendar's table contents. */
function ghrs_changeCalendarDays(month,year,random,check,prefix){
	
	/* The code contained between '-' allow us to obtain the first day of a month /*
	/* and use it to build up the calendar correctly. */
	/*------------------------------------------------*/
	var month_names = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
	var this_month = month_names[parseInt(month,10) - 1];
	var this_date = new Date(this_month + ' 1, ' + year);
	var first_month_day = this_date.getDay();
	if(first_month_day > 0) first_month_day = first_month_day - 1;
	else first_month_day = 6;
	/*------------------------------------------------*/
	
	var current_ci_day = parseInt(document.getElementById("ci_"+random+"_day").innerHTML,10);
	var current_ci_month = parseInt(document.getElementById("ci_"+random+"_month").innerHTML,10);
	var current_ci_year = parseInt(document.getElementById("ci_"+random+"_year").innerHTML,10);
	var current_co_day = parseInt(document.getElementById("co_"+random+"_day").innerHTML,10);
	var current_co_month = parseInt(document.getElementById("co_"+random+"_month").innerHTML,10);
	var current_co_year = parseInt(document.getElementById("co_"+random+"_year").innerHTML,10);
	var quit = 1; /* Variable that check the complement of the calendar. */
		
	/* Generating empty days columns. */
	for(var i=0;i<first_month_day;i++){
		ghrs_ghrsSetTd(i, '&#160;', '', 0, '',prefix);
	}
	
	/* Generating days columns. */
	for(i; quit; i++){
		var this_day = i - first_month_day + 1;
		var params = this_day + ',\'' + random + '\'' + ', \'' + check + '\'';
		if(this_day <= ghrs_daysInMonth(month,year)){
			if((month<today_month)&&(year<=today_year)){
				ghrs_ghrsSetTd(i, this_day, 'past_day_cal', 0, '',prefix);
			}
			else{
				if((this_day==today)&&(month==today_month)&&(year==today_year)){
					if(check=='ci_'){
						ghrs_ghrsSetTd(i, this_day, 'today_day_cal_href', 1, random, check,prefix);
					}
					else{
						if((current_ci_day==today)&&(current_ci_month==today_month)&&(current_ci_year==today_year)){
							ghrs_ghrsSetTd(i, this_day, 'selected_day_cal_no_href', 0, '',prefix);
						}
						else{
							ghrs_ghrsSetTd(i, this_day, 'today_day_cal_no_href', 0, '',prefix);
						}
					}
				}
				else{
					if((this_day<today)&&(month==today_month)&&(year==today_year)){
						ghrs_ghrsSetTd(i, this_day, 'past_day_cal', 0, '',prefix);
					}
					else{
						if(check=='co_'){
							if(current_ci_year>current_co_year){
								if(year==current_co_year){
									if((month==current_co_month)&&(this_day==current_co_day)){
										ghrs_ghrsSetTd(i, this_day, 'selected_day_cal_no_href', 0, '',prefix);
									}
									else{
										ghrs_ghrsSetTd(i, this_day, 'past_day_cal', 0, '',prefix);
									}
								}
								else{
									if(month<=current_ci_month){
										if(month==current_ci_month){
											if(this_day<=current_ci_day){
												if(this_day==current_ci_day){
													ghrs_ghrsSetTd(i, this_day, 'selected_day_cal_no_href', 0, '',prefix);
												}
												else{
													ghrs_ghrsSetTd(i, this_day, 'past_day_cal', 0, '',prefix);
												}
											}
											else{
												ghrs_ghrsSetTd(i, this_day, 'future_day_cal', 1, random, check,prefix);
											}
										}
										else{
											ghrs_ghrsSetTd(i, this_day, 'past_day_cal', 0, '',prefix);
										}
									}
									else{
										ghrs_ghrsSetTd(i, this_day, 'future_day_cal', 1, random, check,prefix);
									}
								}
							}
							if(current_ci_year==current_co_year){
								if(year<current_ci_year){
									ghrs_ghrsSetTd(i, this_day, 'past_day_cal', 0, '',prefix);
								}
								else{
									if(year>current_ci_year){
										ghrs_ghrsSetTd(i, this_day, 'future_day_cal', 1, random, check,prefix);
									}
									else{
										if(current_ci_month<current_co_month){
											if(month<current_ci_month){
												ghrs_ghrsSetTd(i, this_day, 'past_day_cal', 0, '',prefix);
											}
											else{
												if(month==current_ci_month){
													if(this_day<=current_ci_day){
														if(this_day==current_ci_day){
															ghrs_ghrsSetTd(i, this_day, 'selected_day_cal_no_href', 0, '',prefix);
														}
														else{
															ghrs_ghrsSetTd(i, this_day, 'past_day_cal', 0, '',prefix);
														}
													}
													else{
														ghrs_ghrsSetTd(i, this_day, 'selected_period_cal', 1, random, check,prefix);
													}
												}
												else{
													if(month==current_co_month){
														if(this_day<=current_co_day){
															if(this_day==current_co_day){
																ghrs_ghrsSetTd(i, this_day, 'selected_day_cal', 1, random, check,prefix);
															}
															else{
																ghrs_ghrsSetTd(i, this_day, 'selected_period_cal', 1, random, check,prefix);
															}
														}
														else{
															ghrs_ghrsSetTd(i, this_day, 'future_day_cal', 1, random, check,prefix);
														}
													}
													else{
														if(month<current_co_month){
															ghrs_ghrsSetTd(i, this_day, 'selected_period_cal', 1, random, check,prefix);
														}
														else{
															ghrs_ghrsSetTd(i, this_day, 'future_day_cal', 1, random, check,prefix);
														}
													}
												}
											}
										}
										else{
											if(current_ci_month==current_co_month){
												if(month<current_ci_month){
													ghrs_ghrsSetTd(i, this_day, 'past_day_cal', 0, '',prefix);
												}
												else{
													if(month==current_ci_month){
														if(this_day<=current_ci_day){
															if(this_day==current_ci_day){
																ghrs_ghrsSetTd(i, this_day, 'selected_day_cal_no_href', 0, '',prefix);
															}
															else{
																ghrs_ghrsSetTd(i, this_day, 'past_day_cal', 0, '',prefix);
															}
														}
														else{
															if(this_day<=current_co_day){
																if(this_day==current_co_day){
																	ghrs_ghrsSetTd(i, this_day, 'selected_day_cal', 1, random, check,prefix);
																}
																else{
																	ghrs_ghrsSetTd(i, this_day, 'selected_period_cal', 1, random, check,prefix);
																}
															}
															else{
																ghrs_ghrsSetTd(i, this_day, 'future_day_cal', 1, random, check,prefix);
															}
														}
													}
													else{
														ghrs_ghrsSetTd(i, this_day, 'future_day_cal', 1, random, check,prefix);
													}
												}
											}
											else{
												if(month<current_ci_month){
													if((month==current_co_month)&&(this_day==current_co_day)){
														ghrs_ghrsSetTd(i, this_day, 'selected_day_cal_no_href', 0, '',prefix);
													}
													else{
														ghrs_ghrsSetTd(i, this_day, 'past_day_cal', 0, '',prefix);
													}
												}
												else{
													if(month==current_ci_month){
														if(this_day==current_ci_day){
															ghrs_ghrsSetTd(i, this_day, 'selected_day_cal_no_href', 0, '',prefix);
														}
														else{
															if(this_day<current_ci_day){
																ghrs_ghrsSetTd(i, this_day, 'past_day_cal', 0, '',prefix);
															}
															else{
																ghrs_ghrsSetTd(i, this_day, 'future_day_cal', 1, random, check,prefix);
															}
														}
													}
													else{
														ghrs_ghrsSetTd(i, this_day, 'future_day_cal', 1, random, check,prefix);
													}
												}
											}
										}
									}
								}
							}
							if(current_ci_year<current_co_year){
								if(year==current_ci_year){
									if(month<current_ci_month){
										ghrs_ghrsSetTd(i, this_day, 'past_day_cal', 0, '',prefix);
									}
									else{
										if(month==current_ci_month){
											if(this_day==current_ci_day){
												ghrs_ghrsSetTd(i, this_day, 'selected_day_cal_no_href', 0, '',prefix);
											}
											else{
												if(this_day<current_ci_day){
													ghrs_ghrsSetTd(i, this_day, 'past_day_cal', 0, '',prefix);
												}
												else{
													ghrs_ghrsSetTd(i, this_day, 'selected_period_cal', 1, random, check,prefix);
												}
											}
										}
										else{
											ghrs_ghrsSetTd(i, this_day, 'selected_period_cal', 1, random, check,prefix);
										}
									}
								}
								else{
									if(month<current_co_month){
										ghrs_ghrsSetTd(i, this_day, 'selected_period_cal', 1, random, check,prefix);
									}
									else{
										if(month==current_co_month){
											if(this_day==current_co_day){
												ghrs_ghrsSetTd(i, this_day, 'selected_day_cal', 1, random, check,prefix);
											}
											else{
												if(this_day<current_co_day){
													ghrs_ghrsSetTd(i, this_day, 'selected_period_cal', 1, random, check,prefix);
												}
												else{
													ghrs_ghrsSetTd(i, this_day, 'future_day_cal', 1, random, check,prefix);
												}
											}
										}
										else{
											ghrs_ghrsSetTd(i, this_day, 'future_day_cal', 1, random, check,prefix);
										}
									}
								}
							}
						}
						else{
							if((this_day==current_ci_day)&&(month==current_ci_month)&&(year==current_ci_year)){
								ghrs_ghrsSetTd(i, this_day, 'selected_day_cal', 1, random, check,prefix);
							}
							else{
								ghrs_ghrsSetTd(i, this_day, 'future_day_cal', 1, random, check,prefix);
							}
						}
					}
				}
			}
		}
		else{
			quit = 0;
		}
	}
	i--;
	while(document.getElementById("row_" + parseInt((i/7),10))){
		ghrs_ghrsSetTd(i, '&#160;', '', 0, '',prefix);
		i++;
	}
}

/* This function will set up correctly each content of each column. */
function ghrs_ghrsSetTd(pos, value, css_class, onclick, random, check,prefix){
	var link = document.getElementById("row_" + parseInt((pos/7),10)).getElementsByTagName("td")[pos%7];
	link.innerHTML = value;
	if(onclick == 1){
		link.onclick = function() {
			ghrs_calendarSetsDayDiv(value, random, check,prefix);
			if(ghrs_added_functions.length > 0){
				for(var i = 0; i < ghrs_added_functions.length; i++){
					eval(ghrs_added_functions[i]);
				}
			}
		};
		link.style.cursor = "pointer";
	}
	else{
		link.onclick = function() {};
		link.style.cursor = "default";
	}
	ghrs_setAttribute(link, 'class', css_class);
	ghrs_setAttribute(link, 'className', css_class);
	if(onclick == 0) ghrs_disableColumnSelection(link, 1);
	if(ghrs_isExplorer6()){
		if(onclick == 1){
			link.onmouseover = function() { this.className = css_class + '_hover' };
			link.onmouseout = function() { this.className = css_class };
		}
		else{
			link.onmouseover = function() {};
			link.onmouseout = function() {};
		}
	}
}

/* This function will disable the column selection with the mouse. */
function ghrs_disableColumnSelection(elem,cursor_def) {
	if (elem) {
		elem.onselectstart = function() { return false; };
		elem.unselectable = "on";
		elem.style.MozUserSelect = "none";
		if(cursor_def == 1) elem.style.cursor = "default";
	}
}

/* This function will be called when the user chooses a day in the calendar. */
function ghrs_calendarSetsDayDiv(day,random,check,prefix){
	document.getElementById(check+random+"_day").innerHTML = day;
	document.getElementById(check+random+"_month").innerHTML = document.getElementById(check+random+"_temp_month").innerHTML;
	document.getElementById(check+random+"_year").innerHTML = document.getElementById(check+random+"_temp_year").innerHTML;
	ghrs_closeCalendarDiv();
	ghrs_removeDateSelectorsAlert();
	ghrs_setCalendarDayBase(random,check,prefix);
}

/* This function has the behaviour of show or hide the calendar. */
function ghrs_buildCalendarDiv(random,check,event,prefix){
	if(ghrs_loaded){
		if(ghrs_fixCalendarHtmlElements(random,event)) ghrs_addCalendarElements(random,event);
		var calendar = document.getElementById('external_calendar_div');
		if(ghrs_isExplorer6()) var ifr = document.getElementById('calendar_iframe');
		var status = parseInt(document.getElementById("div_calendar_status").innerHTML,10);
		var current_check = document.getElementById('current_div_check').innerHTML;
		var current_random = document.getElementById('current_div_random').innerHTML;
		if(status == 1 && current_check == check && current_random == random){
			calendar.style.display = 'none';
			if(ghrs_isExplorer6()) ifr.style.display = 'none'; 
			document.getElementById("div_calendar_status").innerHTML = 0;
			ghrs_calendarSelfClose(random,check,prefix);
		}
		else{
			if(current_check != '' && current_random != '') ghrs_calendarSelfClose(current_random,current_check,prefix);
			if(document.getElementById(check+prefix+'_text_'+random)){
				if(!document.getElementById(check+prefix+'_text_'+random).disabled){
					document.getElementById(check+prefix+'_text_'+random).disabled = true;
				}
			}
			ghrs_buildDiv(random,check,event,prefix);
			document.getElementById('current_div_prefix').innerHTML = prefix;
			document.getElementById("div_calendar_status").innerHTML = 1;
			calendar.style.display = "block";
			if(ghrs_isExplorer6()){ 
				ifr.style.zIndex = 200;
				calendar.style.zIndex = ifr.style.zIndex + 1;
				ifr.style.display = 'block'; 
			}
		}
		var month_name = document.getElementById("calendar_month");
		var days_names_row = document.getElementById("days_name").getElementsByTagName("td");
		var close_span = document.getElementById("close_calendar_div").getElementsByTagName("span");
		var image_co = document.getElementById("co_"+prefix+'_calendar_'+random);
		var image_ci = document.getElementById("ci_"+prefix+'_calendar_'+random);
		ghrs_disableColumnSelection(month_name, 1);
		for(var i = 0; days_names_row[i]; i++){
			ghrs_disableColumnSelection(days_names_row[i], 1);
		}
		ghrs_disableColumnSelection(close_span[0], 0);
		ghrs_disableColumnSelection(image_co, 0);
		ghrs_disableColumnSelection(image_ci, 0);
		ghrs_stopEventPropagation(event);
	}
}

function ghrs_addCalendarElements(random,event){
	// Setting  calendar code and inseting calendar into page.
	var attributes = new Array();
	var span;
	if(event){
		if(!document.getElementById("external_calendar_div")){
			document.getElementsByTagName("body")[0].appendChild(ghrs_calendar_div);
			// Internet Explorer e Opera fix.
			if(window.attachEvent) ghrs_calendar_div.attachEvent('onclick', function() { ghrs_stopEventPropagation(event); });
		}
		if(!document.getElementById("calendar_iframe") && ghrs_isExplorer6()){
			attributes = { 'name':'calendar_iframe', 'id':'calendar_iframe', 'class':'calendar_destinations', 'src':'.', 'scrolling':'no', 'frameborder':'0', 'marginheight':'0', 'marginwidth':'0' };
			var calendar_iframe = ghrs_createElementWithAttribute('iframe',attributes,null);
			document.getElementsByTagName("body")[0].appendChild(calendar_iframe);
			self.frames['calendar_iframe'].document.write(""); 
			self.frames['calendar_iframe'].document.close();
		}
		if(!document.getElementById("span_calendar_" + random + "_status")){
			attributes = { 'id':('span_calendar_' + random + '_status'), 'style':'display : none;' };
			var calendar_iframe = ghrs_createElementWithAttribute('span',attributes,null);
			document.getElementsByTagName("body")[0].appendChild(calendar_iframe);
		}
	}
	var checks = new Array()
	checks[0] = "ci";
	checks[1] = "co";
	for(var i=0; i<checks.length; i++){
		if(!document.getElementById(checks[i] + '_' + random + "_day")){
			attributes = { 'id':(checks[i] + '_' + random + "_day"), 'style':'display : none;' };
			span = ghrs_createElementWithAttribute('span',attributes,ghrs_jsDate[checks[i]]['testDay']);
			document.getElementsByTagName("body")[0].appendChild(span);
		}
		if(!document.getElementById(checks[i] + '_' + random + "_month")){
			attributes = { 'id':(checks[i] + '_' + random + "_month"), 'style':'display : none;' };
			span = ghrs_createElementWithAttribute('span',attributes,ghrs_jsDate[checks[i]]['testMonth']);
			document.getElementsByTagName("body")[0].appendChild(span);
		}
		if(!document.getElementById(checks[i] + '_' + random + "_year")){
			attributes = { 'id':(checks[i] + '_' + random + "_year"), 'style':'display : none;' };
			span = ghrs_createElementWithAttribute('span',attributes,ghrs_jsDate[checks[i]]['year']);
			document.getElementsByTagName("body")[0].appendChild(span);
		}
		if(!document.getElementById(checks[i] + '_' + random + "_temp_month")){
			attributes = { 'id':(checks[i] + '_' + random + "_temp_month"), 'style':'display : none;' };
			span = ghrs_createElementWithAttribute('span',attributes,ghrs_jsDate[checks[i]]['testMonth']);
			document.getElementsByTagName("body")[0].appendChild(span);
		}
		if(!document.getElementById(checks[i] + '_' + random + "_temp_year")){
			attributes = { 'id':(checks[i] + '_' + random + "_temp_year"), 'style':'display : none;' };
			span = ghrs_createElementWithAttribute('span',attributes, ghrs_jsDate[checks[i]]['year']);
			document.getElementsByTagName("body")[0].appendChild(span);
		}
	}
}

function ghrs_fixCalendarHtmlElements(random,event){
	if(event){
		var cal_div = (!document.getElementById("external_calendar_div"));
		var cal_ifr = (!document.getElementById("calendar_iframe") && ghrs_isExplorer6());
		var cal_span = (!document.getElementById("span_calendar_" + random + "_status"));
		var cal_ver = cal_div || cal_ifr || cal_span;
	}
	else{
		var cal_ver = false;
	}
	var ci_span_day= (!document.getElementById("ci_" + random + "_day"));
	var ci_span_month= (!document.getElementById("ci_" + random + "_month"));
	var ci_span_year= (!document.getElementById("ci_" + random + "_year"));
	var ci_span_temp_month= (!document.getElementById("ci_" + random + "_temp_month"));
	var ci_span__temp_year= (!document.getElementById("ci_" + random + "_temp_year"));
	var co_span_day= (!document.getElementById("co_" + random + "_day"));
	var co_span_month= (!document.getElementById("co_" + random + "_month"));
	var co_span_year= (!document.getElementById("co_" + random + "_year"));
	var co_span_temp_month= (!document.getElementById("co_" + random + "_temp_month"));
	var co_span__temp_year= (!document.getElementById("co_" + random + "_temp_year"));
	var ci_ver = ci_span_day || ci_span_month || ci_span_year || ci_span_temp_month || ci_span__temp_year;
	var co_ver = co_span_day || co_span_month || co_span_year || co_span_temp_month || co_span__temp_year;
	return cal_ver || ci_ver || co_ver;
}

function ghrs_generateDivCode(table_code, img_url, msg, random){
	// Creating calendar's div in javascript.
	var attributes = { 'id':'external_calendar_div', 'class':'external_calendar_div', 'onclick':'ghrs_stopEventPropagation(event);' };
	var str='<table id="calendar_table" class="calendar_table" align="center" cellspacing="0" cellpadding="0">';
		str+='<tr>';
			str+='<td id="calendar_sx_button" class="button">';
				str+='<img id="calendar_left_arrow" style="display:none;" src="' + img_url +'/form/calendar_arrowsx-trasparent.gif" alt="" />';
			str+='</td>';
			str+='<td id="calendar_month" class="month" colspan="5">&#160;</td>';
			str+='<td id="calendar_dx_button" class="button">';
				str+='<img id="calendar_right_arrow" style="display:none;" src="' + img_url +'/form/calendar_arrowdx-trasparent.gif" alt=""/>';
			str+='</td>';
		str+='</tr>';
		str+='<tr id="days_name">';
			str+='<td>&#160;</td>';
			str+='<td>&#160;</td>';
			str+='<td>&#160;</td>';
			str+='<td>&#160;</td>';
			str+='<td>&#160;</td>';
			str+='<td>&#160;</td>';
			str+='<td>&#160;</td>';
		str+='</tr>';
		str+=table_code;
		str+='<tr>';
			str+='<td id="close_calendar_div" class="close_calendar_div" colspan="7">';
				str+='<span onclick="ghrs_closeCalendarDiv()">';
					str+=msg;
				str+='</span>';
			str+='</td>';
		str+='</tr>';
	str+='</table>';
	str+='<span id="div_calendar_status" style="display : none;">0</span>';
	str+='<span id="current_div_check" style="display : none;"></span>';
	str+='<span id="current_div_random" style="display : none;">' + random + '</span>';
	str+='<span id="current_div_prefix" style="display : none;"></span>';
	ghrs_calendar_div = ghrs_createElementWithAttribute('div',attributes,str);
}

/* This function has the main behaviour of set calendar dimensions and contents.  */
function ghrs_buildDiv(random,check,event,prefix){
	var calendar = document.getElementById('external_calendar_div');
	var day = parseInt(document.getElementById(check+random+"_day").innerHTML,10);
	var month = parseInt(document.getElementById(check+random+"_temp_month").innerHTML,10);
	var year = parseInt(document.getElementById(check+random+"_temp_year").innerHTML,10);
	
	document.getElementById('calendar_month').innerHTML = ghrs_getMonthName(month - 1) + ' ' + year;
	ghrs_calendarDaysNamesDiv();
	ghrs_changeCalendarDays(month,year,random,check,prefix);
	ghrs_setDivDimensionsAndPosition(random,check,event,prefix);/* Call to the function that set up correcly iframe's dimensions and position. */
	if(!(month==today_month && year==today_year)) ghrs_setButton('sx',check,random,prefix);
	else ghrs_removeButton('sx',check,random);
	if(!(month==12 && year==today_year+1)) ghrs_setButton('dx',check,random,prefix);
	else ghrs_removeButton('dx',check,random);
	document.getElementById('current_div_check').innerHTML = check;
	document.getElementById('current_div_random').innerHTML = random;
}

/* This function will sett the names of the calendar's days. */
function ghrs_calendarDaysNamesDiv(){
	var td_array = document.getElementById('days_name').getElementsByTagName('td');
	for(var i=0; i<7; i++){
		td_array[i].innerHTML = ghrs_getDayName(i).charAt(0);
		ghrs_setAttribute(td_array[i], 'class', 'day_name');
	}
}

/* This function has the behaviour of set up correctly the calendar's dimensions and positions. */
function ghrs_setDivDimensionsAndPosition(random,check,event,prefix){
	var calendar = document.getElementById('external_calendar_div');
	var ifr = document.getElementById('calendar_iframe');
	var input_ci_day = document.getElementById('ci_'+prefix+'_day_'+random);
	var input_co_day = document.getElementById('co_'+prefix+'_day_'+random);
	var current_img = document.getElementById(check+prefix+"_calendar_"+random);
	var current_img_offsetWidth = (current_img) ? current_img.offsetWidth : 0;
	var found_position_slc_ci = ghrs_findPosition(input_ci_day);
	var found_position_slc_co = ghrs_findPosition(input_co_day);
	var found_position_image = ghrs_findPosition(current_img);
	var pos_left_slc_ci = found_position_slc_ci[0];
	var pos_left_slc_co = found_position_slc_co[0];
	var pos_top_slc_ci = found_position_slc_ci[1];
	var pos_top_slc_co = found_position_slc_co[1];
	var pos_height_slc = input_ci_day.offsetHeight;
	if(pos_top_slc_ci>(pos_top_slc_co-pos_height_slc)&&pos_top_slc_ci<(pos_top_slc_co+pos_height_slc)){
		calendar.style.left = event.clientX - 150 + 'px';
		calendar.style.top = event.clientY + input_ci_day.offsetHeight + 'px';
	}
	else{
		calendar.style.left = event.clientX + current_img_offsetWidth + 'px';
		calendar.style.top = event.clientY - 7 + 'px';
	}
	calendar.style.width = '175px';
	calendar.style.height = '225px';
	calendar.style.left = parseInt(calendar.style.left,10) + getScrollLeft() + 'px';
	calendar.style.top = parseInt(calendar.style.top,10) + getScrollTop() + 'px';
	var browser = String(navigator.userAgent);
	if(browser.search("Firefox") > -1){
		calendar.style.borderWidth = "3px";
		if(browser.search("3.0") > -1){
			calendar.style.paddingBottom = '4px';
		}
	}
	if(ghrs_isExplorer6()){ 
		var ifr = document.getElementById('calendar_iframe');
		ifr.style.width = '179px';
		ifr.style.height = '229px';
		ifr.style.left = parseInt(calendar.style.left,10) + 'px';
		ifr.style.top = parseInt(calendar.style.top,10) + 'px';
	}
	if(browser.search("Opera") > -1){
		calendar.style.borderBottom = '0';
		calendar.style.paddingTop = '1px';
		calendar.style.paddingRight = '1px';
		calendar.style.paddingBottom = '0px';
		calendar.style.borderBottom = '1px';
	}
}

/* This function is used to close the calendar. */
function ghrs_closeCalendarDiv(){
	if(document.getElementById("external_calendar_div")){
		var calendar = document.getElementById('external_calendar_div');
		if(ghrs_isExplorer6()) var calendar = document.getElementById('external_calendar_div');
		var ifr = document.getElementById('calendar_iframe');
		var check = document.getElementById('current_div_check').innerHTML;
		var random = document.getElementById('current_div_random').innerHTML;
		var prefix = document.getElementById('current_div_prefix').innerHTML;
		calendar.style.display = 'none';
		if(ghrs_isExplorer6()) ifr.style.display = 'none';
		if(document.getElementById("div_calendar_status")) document.getElementById("div_calendar_status").innerHTML = 0;
		if(check != '') ghrs_calendarSelfClose(random,check);
		document.getElementById('current_div_check').innerHTML = '';
		document.getElementById('current_div_random').innerHTML = '';
		document.getElementById('current_div_prefix').innerHTML = '';
		
		if(document.getElementById(check+prefix+'_text_'+random)){
			if(document.getElementById(check+prefix+'_text_'+random).disabled){
				document.getElementById(check+prefix+'_text_'+random).disabled = false;
			}
		}
	}
	if(typeof ghrs_closeAutocompleteDiv=="function") ghrs_closeAutocompleteDiv();
}
/*------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------------*/
/* Next piece of code has the behavior to manage the submit of the form in the  */
/* same page of the calendar and the "view_all_destinations" list.	        */
/*------------------------------------------------------------------------------*/

/* This function is called from the submit listener to check the dates inseted. */
/* If dates are corrects the submit is forward otherwise will be showed a       */
/* warning message and the submit will be stopped.				*/
function ghrs_checkInputDate(event,random,form,display,layout){
	ghrs_list_random = random;
	find_pressed = true;
	ghrs_closeCalendarDiv();
	if(ghrs_fixCalendarHtmlElements(random,null)) ghrs_addCalendarElements(random,null);
	/*if(layout=='horizontal_locality_div') ghrs_checkErrorsSpans();*/
	ghrs_removeAlertForm(random);
	var elem;
	var current_form = document.getElementById("check_"+random);
	var ci_day = parseInt(document.getElementById("ci_"+random+"_day").innerHTML,10);
	var ci_month = parseInt(document.getElementById("ci_"+random+"_month").innerHTML,10);
	var ci_year = parseInt(document.getElementById("ci_"+random+"_year").innerHTML,10);
	var co_day = parseInt(document.getElementById("co_"+random+"_day").innerHTML,10);
	var co_month = parseInt(document.getElementById("co_"+random+"_month").innerHTML,10);
	var co_year = parseInt(document.getElementById("co_"+random+"_year").innerHTML,10);
	var error_array = new Array();
	var msg_error_array = new Array();
	var codate_error_array = new Array();
	var cidate_error_array = new Array();
	var date_error = false;
	
	if(ghrs_autocomplete){
		ghrs_resertSearch(true);
		return false; 
	}
	
	if(document.getElementById("loc_search_" + random)){
		if((!ghrs_radio_displayed || !ghrs_radio_selected) && ghrs_trimValue(document.getElementById("loc_search_" + random).value).length<3){
			if(layout=='horizontal_locality_div'){
				ghrs_checkErrorSpan('all_destinations_error','all_destinations');
				error_array.push("all_destinations");
				if(document.getElementById("loc_search_" + random).value ==''){
					msg_error_array.push(msgEmptyInputLoc);
				}
				else{
					msg_error_array.push(msgWrongInputLoc);
				}
				ghrs_setAttribute(document.getElementById("loc_search_" + random), "class", "wrong_input input_search_loc");
			}
			else {
				if(document.getElementById("loc_search_" + random).value ==''){
					alert(msgEmptyInputLoc);
				}
				else{
					alert(msgWrongInputLoc);
				}
				return false;
			}
		}
	}
	if(ci_year>co_year){
		if(layout=='horizontal_locality_div'){
			ghrs_checkErrorSpan('date_selectors_error','date_selectors');
			date_error=true;
			codate_error_array.push("co_year");	
		}
		else {
			ghrs_validateDate(event, msgInvalidDate);
			return false;
		}
	}
	if(ci_year==co_year){
		if(ci_month>co_month){
			if(layout=='horizontal_locality_div'){
				ghrs_checkErrorSpan('date_selectors_error','date_selectors');
				date_error=true;
				codate_error_array.push("co_month");	
			}
			else{
				ghrs_validateDate(event, msgInvalidDate);
				return false;
			}
		}
		if(ci_month==co_month){
			if(ci_day>=co_day){
				if(layout=='horizontal_locality_div'){
					ghrs_checkErrorSpan('date_selectors_error','date_selectors');
					date_error=true;
					codate_error_array.push("co_day");
				}
				else{
					ghrs_validateDate(event, msgInvalidDate);
					return false;
				}
			}
		}
	}
	if(ci_year == today_year){
		if(ci_month<today_month){
			if(layout=='horizontal_locality_div'){
				ghrs_checkErrorSpan('date_selectors_error','date_selectors');
				date_error=true;
				cidate_error_array.push("ci_month");
			}
			else{
				ghrs_validateDate(event, msgInvalidDate);
				return false;
			}
		}
		if(ci_month==today_month){
			if(ci_day<today){
				if(layout=='horizontal_locality_div'){
					ghrs_checkErrorSpan('date_selectors_error','date_selectors');
					date_error=true;
					cidate_error_array.push("ci_day");	
				}
				else{
					ghrs_validateDate(event, msgInvalidDate);
					return false;
				}
			}
		}
	}
	if(date_error){ 
		error_array.push("date_selectors");
		msg_error_array.push(msgInvalidDate);
	}
	if(document.getElementById("form_qty_" + random)){
		if(isNaN(parseInt(document.getElementById("form_qty_" + random).value,10)) || parseInt(document.getElementById("form_qty_" + random).value,10)<1){
			if(layout=='horizontal_locality_div'){
				ghrs_checkErrorSpan('other_selectors_error','other_selectors');
				error_array.push("other_selectors");
				msg_error_array.push(msgWrongInputRoom);
				ghrs_setAttribute(document.getElementById("form_qty_" + random), "class", "wrong_input calendar_input");
			}
			else{
				alert(msgWrongInputRoom);
				return false;
			}
		}
	}
	if(cidate_error_array.length>0){
		ghrs_setDateSelectClass("ci", "calendar_select wrong_input");
	}
	if(codate_error_array.length>0){
		ghrs_setDateSelectClass("co", "calendar_select wrong_input");
	}
	var error_length = error_array.length;
	while(error_array.length>0) ghrs_showAlertForm(error_array.pop(), msg_error_array.pop());
	if(error_length>0) return false;
	if(document.getElementById("loc_search_" + random)){
		if(!(ghrs_trimValue(document.getElementById("loc_search_" + random).value).length<3)){
			document.getElementById("loc_search_" + random).value = ghrs_trimValue(document.getElementById("loc_search_" + random).value);
		}
	}
	if(document.getElementById("loc_country")&&document.getElementById("loc_search_" + random)&&document.getElementById("loc_country")&&document.getElementById("prev_loc")){
		var loc_search_value = document.getElementById("loc_search_" + random).value;
		var previous_value = document.getElementById("prev_loc").value;
		if(previous_value==loc_search_value) document.getElementById("loc_country").disabled = false;
	}
	if(display) ghrs_displayWaitingPage(random);
	return true;
}

function ghrs_showAlertForm(idelem, msg){
	var span_alert = document.getElementById(idelem + "_error");
	span_alert.innerHTML = msg;
	span_alert.style.display = "";
	var div_alert = document.getElementById(idelem + "_error");
	ghrs_setAttribute(div_alert, "class", idelem + "_error");
}

/* This function remove all the alerts in the search form */
function ghrs_removeAlertForm(random){
	ghrs_removeAllDestinationsAlert(null);
	ghrs_removeDateSelectorsAlert();
	ghrs_removeOtherSelectorsAlert(random);
}

/* This function checks if all spans error are created */
/*function ghrs_checkErrorsSpans(){
	ghrs_checkErrorSpan('all_destinations_error','all_destinations');
	ghrs_checkErrorSpan('date_selectors_error','date_selectors');
	ghrs_checkErrorSpan('other_selectors_error','other_selectors');
}*/

/* This function create the element if absent */
function ghrs_checkErrorSpan(name,container_name){
	var span = document.getElementById(name);
	if(!span){
		// If not created, create and add it!
		span = ghrs_createElementWithAttribute('span',{'id':name});
		document.getElementById(container_name).appendChild(span);
	}
}

/* This function set the class of the dates alert */
function ghrs_setDateSelectClass(check,new_class){
	var date_elem;
	var idelem;
	var selects_array = new Array();
	selects_array.push("day");
	selects_array.push("month");
	selects_array.push("year");
	while(selects_array.length>0){
		date_elem = selects_array.pop();
		idelem = document.getElementById(check + "_default_" + date_elem + "_" + ghrs_list_random);
		if(idelem) ghrs_setAttribute(idelem,"class", new_class);
	}	
}

/* This function removes the alert of wrong destination search */
function ghrs_removeAllDestinationsAlert(random){
	if(random) ghrs_list_random = random;
	if(document.getElementById("all_destinations_error")){
		document.getElementById("all_destinations_error").style.display="none";
		/*document.getElementById("all_destinations_error").innerHTML="";*/
		ghrs_setAttribute(document.getElementById("loc_search_" + ghrs_list_random), "class", "input_search_loc");
		if(!document.getElementById("search_destinations_div") && random){ /* Modified by Pavlovich 15/09/2009 */
			document.getElementById("all_dest_loc_reg_code").value = '';
			document.getElementById("all_dest_loc_reg_code").name = '';
			document.getElementById("all_dest_loc_reg_code").disabled = true;
		}
	}
}

/* This function removes alert in the date selectors */
function ghrs_removeDateSelectorsAlert(){
	if(document.getElementById("date_selectors_error")){
		document.getElementById("date_selectors_error").style.display="none";
		/*document.getElementById("date_selectors_error").innerHTML="";*/
		var date_selectors = document.getElementById("date_selectors");
		if (date_selectors) date_selectors.removeAttribute("class");
		ghrs_setDateSelectClass("ci", "calendar_select");
		ghrs_setDateSelectClass("co", "calendar_select");
	}
}

/* This function remove alert of wrong rooms quantity */
function ghrs_removeOtherSelectorsAlert(random){
	if(document.getElementById("other_selectors_error")){
		document.getElementById("other_selectors_error").style.display="none";
		/*document.getElementById("other_selectors_error").innerHTML="";*/
		ghrs_setAttribute(document.getElementById("other_selectors_error"), "class", "calendar_input");
		ghrs_setAttribute(document.getElementById("form_qty_" + random), "class", "calendar_input");
	}
}

/* Function called at each key up to refresh alert */
function ghrs_checkLocSearch(value){
	if(value.length>2) ghrs_removeAllDestinationsAlert(null);
	else{
		ghrs_showAlertForm("all_destinations", "Insert at least 3 characters");
		ghrs_setAttribute(document.getElementById("loc_search_" + ghrs_list_random), "class", "wrong_input input_search_loc");
	}
}

/* Function used to test the search input */
function ghrs_trimValue(value){
    while (value.substring(0,1) == ' '){
        value = value.substring(1, value.length);
    }
    while (value.substring(value.length-1, value.length) == ' '){
        value = value.substring(0,value.length-1);
    }
    return value;
}

/* This function is called from the submit listener to check the dates inseted. */
/* This function is called when the inserted dates are wrong. It has the        */
/* behaviour to hide calendar */
function ghrs_validateDate(event, msg){
	ghrs_closeCalendarDiv()
	ghrs_stopEventPropagation(event);
	alert(msg);
}

function ghrs_refreshInputDate(check,text_value,random,prefix){
	
	// If spans do not exist, we have to create them
	if(ghrs_fixCalendarHtmlElements(random,null)) ghrs_addCalendarElements(random,null);
	
	text_value = String(text_value).split("/");
	day = text_value[0];
	month = text_value[1];
	year = text_value[2];
	document.getElementById(check+prefix+'_day_'+random).value =  day;
	document.getElementById(check+prefix+'_month_'+random).value = month;
	document.getElementById(check+prefix+'_year_'+random).value = year;
	if(check=='ci_'){
		ghrs_setCalendarCiYear(random,prefix);
		day = document.getElementById('co_'+prefix+'_day_'+random).value;
		month = document.getElementById('co_'+prefix+'_month_'+random).value;
		year = document.getElementById('co_'+prefix+'_year_'+random).value;
		document.getElementById('co_'+prefix+'_text_'+random).value = ghrs_dateToText(day,month,year);
	}
	else{
		ghrs_setCalendarCoDay(random,prefix);
		ghrs_setCalendarCoMonth(random,prefix);
		ghrs_setCalendarCoYear(random,prefix);
	}
	
}

function ghrs_dateToText(day,month,year){
	day=(String(day).length==2) ? day : ('0' + day);
	month=(String(month).length==2) ? month : ('0' + month);
	return day + '/' + month + '/' + year;
}
