var error = "false";

//inclusions.php: Check the date span of the dates selected
function check_span(f)
{
	var theForm = document.getElementById("get_value");
	
	var start_day = document.getElementById("start_day").value;
	var start_month_temp = document.getElementById("start_month").value;
	var start_month = start_month_temp - 1;
	var start_year = document.getElementById("start_year").value;
	
	var date1_temp = new Date(start_year, start_month, start_day);
	
	var end_day = document.getElementById("end_day").value;
	var end_month_temp = document.getElementById("end_month").value;
	var end_month = end_month_temp - 1;
	var end_year = document.getElementById("end_year").value;
	
	var date2_temp = new Date(end_year, end_month, end_day);
	
	var one_day=1000*60*60*24
	
	var day_span = Math.ceil((date2_temp.getTime()-date1_temp.getTime())/(one_day));
	
	if(day_span > 35)
	{
		alert("Selected date range cannot be greater than 35 days.");
	}
	else
	{
		theForm.submit();
	}
}

//inclusions.php: Submit the override inclusions form
function submit_override(f, btn_value)
{
	var e = btn_value;
	document.images[e].style.visibility = "visible";
	theForm = document.getElementById("over_ride");
	theForm.submit();
}

//register.php: Validate registration form for null and numeric
function validate_registration(f)
{
	var Numeric;
	Numeric = "^[0-9]+$";
	
	theForm = document.getElementById("register");
	/*
	if(theForm.prop_name.value == '')
	{
		alert("Property Name cannot be empty");
		theForm.prop_name.focus();
	}
	else if(theForm.prop_type.value == '')
	{
		alert("Property Type cannot be empty");	
		theForm.prop_type.focus();
	}
	else if(theForm.prop_total.value == '')
	{
		alert("Number of rooms cannot be empty");	
		theForm.prop_total.focus();
	}
	else if(!theForm.prop_total.value.match(Numeric))
	{
		alert("Number of rooms must be numeric");	
		theForm.prop_total.focus();
	}
	else if(theForm.prop_rooms.value == '')
	{
		alert("Number of room types cannot be empty");	
		theForm.prop_rooms.focus();
	}
	else if(!theForm.prop_rooms.value.match(Numeric))
	{
		alert("Number of room types must be numeric");	
		theForm.prop_rooms.focus();
	}
	else if(theForm.prop_address.value == '')
	{
		alert("Property Address cannot be empty");	
		theForm.prop_address.focus();
	}	
	else if(theForm.prop_suburb.value == '')
	{
		alert("Property Suburb cannot be empty");	
		theForm.prop_suburb.focus();
	}
	else if(theForm.prop_postcode.value == '')
	{
		alert("Property Postcode cannot be empty");	
		theForm.prop_postcode.focus();
	}
	else if(!theForm.prop_postcode.value.match(Numeric))
	{
		alert("Property Postcode must be numeric");	
		theForm.prop_postcode.focus();
	}
	else if(theForm.prop_state.value == '')
	{
		alert("Property State cannot be empty");	
		theForm.prop_state.focus();
	}
	else if(theForm.prop_country.value == '')
	{
		alert("Property Country cannot be empty");	
		theForm.prop_country.focus();
	}
	else if(theForm.manager_name.value == '')
	{
		alert("Manager Name cannot be empty");	
		theForm.manager_name.focus();
	}
	else if(theForm.manager_email.value == '')
	{
		alert("Manager Email cannot be empty");	
		theForm.manager_email.focus();
	}
	else if(theForm.res_name.value == '')
	{
		alert("Reservation Name cannot be empty");	
		theForm.res_name.focus();
	}
	else if(theForm.res_email.value == '')
	{
		alert("Reservation Email cannot be empty");	
		theForm.res_email.focus();
	}
	else if(theForm.res_phone1.value == '')
	{
		alert("Reservation Phone cannot be empty");	
		theForm.res_phone1.focus();
	}
	else if(document.getElementById('captcha').value == "") 
	{
		alert("Verification Code cannot be empty");
		document.getElementById('captcha').focus();
	}	
	else if(document.getElementById('cap_code').value != document.getElementById('captcha').value) 
	{
		alert("Verification code did not match the Captcha Image");
		document.getElementById('captcha').focus();
	}	
	else
	{*/
		theForm.submit();
	//}
}

//property_details.php: Validate property details form for null and numeric
function validate_property(f)
{
	var Numeric;
	Numeric = "^[0-9]+$";
	
	theForm = document.getElementById("register_step1");
	
	if(theForm.prop_name.value == '')
	{
		alert("Property Name cannot be empty");
		theForm.prop_name.focus();
	}
	else if(theForm.prop_type.value == '')
	{
		alert("Property Type cannot be empty");	
		theForm.prop_type.focus();
	}
	else if(theForm.prop_total.value == '')
	{
		alert("Number of rooms cannot be empty");	
		theForm.prop_total.focus();
	}
	else if(!theForm.prop_total.value.match(Numeric))
	{
		alert("Number of rooms must be numeric");	
		theForm.prop_total.focus();
	}
	else if(theForm.prop_address.value == '')
	{
		alert("Property Address cannot be empty");	
		theForm.prop_address.focus();
	}	
	else if(theForm.prop_suburb.value == '')
	{
		alert("Property Suburb cannot be empty");	
		theForm.prop_suburb.focus();
	}
	else if(theForm.prop_postcode.value == '')
	{
		alert("Property Postcode cannot be empty");	
		theForm.prop_postcode.focus();
	}
	else if(!theForm.prop_postcode.value.match(Numeric))
	{
		alert("Property Postcode must be numeric");	
		theForm.prop_postcode.focus();
	}
	else if(theForm.prop_state.value == '')
	{
		alert("Property State cannot be empty");	
		theForm.prop_state.focus();
	}
	else if(theForm.prop_country.value == '')
	{
		alert("Property Country cannot be empty");	
		theForm.prop_country.focus();
	}
	else if(theForm.prop_password.value == '')
	{
		alert("Password cannot be empty");	
		theForm.prop_password.focus();
	}
	else if(theForm.prop_password.value != theForm.prop_cpassword.value)
	{
		alert("Confirm Password did not match password");	
		theForm.prop_cpassword.focus();
	}
	else if(theForm.manager_name.value == '')
	{
		alert("Manager Name cannot be empty");	
		theForm.manager_name.focus();
	}
	else if(theForm.manager_email.value == '')
	{
		alert("Manager Email cannot be empty");	
		theForm.manager_email.focus();
	}
	else if(theForm.sales_name.value == '')
	{
		alert("Sales Name cannot be empty");	
		theForm.sales_name.focus();
	}
	else if(theForm.sales_email.value == '')
	{
		alert("Sales Email cannot be empty");	
		theForm.sales_email.focus();
	}
	else if(theForm.accounts_name.value == '')
	{
		alert("Accounts Name cannot be empty");	
		theForm.accounts_name.focus();
	}
	else if(theForm.accounts_email.value == '')
	{
		alert("Accounts Email cannot be empty");	
		theForm.accounts_email.focus();
	}
	else if(theForm.res_name.value == '')
	{
		alert("Reservation Name cannot be empty");	
		theForm.res_name.focus();
	}
	else if(theForm.res_email.value == '')
	{
		alert("Reservation Email cannot be empty");	
		theForm.res_email.focus();
	}
	else if(theForm.res_phone1.value == '')
	{
		alert("Reservation Phone cannot be empty");	
		theForm.res_phone1.focus();
	}	
	else
	{
		theForm.submit();
	}
}

//room_details.php: Validate room details. ESP if the value of the input fields is "room"
function validate_room(f)
{
	var valid = true;
	
	var inputs = new Array();

	theForm = document.getElementById("register_step2");
	inputs = theForm.getElementsByTagName("input");
	var input_length = inputs.length;
	
	for(var i=0; i < input_length; i++ )
	{			
		var ctl = inputs[i];
		if(inputs[i].type == "text")
		{
			if(ctl.value == "room")
			{
				alert("Please enter a value");
				valid = false;
				//ctl.style.background="yellow";
				ctl.focus();
				break;
			}
		}
	}
	if(valid == true)
	{
		theForm.submit();  
		//alert("AVAILABILITY CLEAR");
	}
}

//login_details.php: Validate login details form
function validate_loginform(f)
{
	theForm = document.getElementById("loginform");
	if(theForm.username.value == '')
	{
		alert("Username cannot be empty");
		theForm.username.focus();
	}
	else if(theForm.password.value == '')
	{
		alert("Password cannot be empty");	
		theForm.password.focus();
	}
	else
	{
		theForm.submit();
	}
}

//login_details.php: Validate login details header
function validate_loginform2(f)
{
	theForm = document.getElementById("loginform2");
	if(theForm.username.value == '')
	{
		alert("Username cannot be empty");
		theForm.username.focus();
	}
	else if(theForm.password.value == '')
	{
		alert("Password cannot be empty");	
		theForm.password.focus();
	}
	else
	{
		theForm.submit();
	}
}

function validate_addroom(f, btn_value)
{
    var e = btn_value;
	document.images[e].style.visibility = "visible";
	theForm = document.getElementById("addroom");
	theForm.submit();
}

function validate_logindetails(f)
{
	theForm = document.getElementById("register_step3");
	theForm.submit();
}

//roommanager.php: Validate availability for null and numeric
function validate_availability(f, btn_value)
{
	if(page_load == false)
	{
		alert("Please allow the page to load completely before making any changes");
	}
	else
	{
		var e = btn_value;	
		
		document.images["aval1"].style.visibility = "visible";
		for(var b = 1; b < site_names.length; b++)
		{
			var update_img = site_names[b].concat("1");
			document.images[update_img].style.visibility = "visible";
		}
		
		var Numeric;
		Numeric = "^[0-9]+$";
	
		var valid = true;
		
		var inputs = new Array();	
	
		//var theForm = document.getElementById("update_all");
	
		inputs = document.getElementsByTagName("input");
		var input_length = inputs.length;
		
		for(var i=0; i < input_length; i++ )
		{			
			var ctl = inputs[i];
			if(inputs[i].type == "text")
			{			
				var ctl_id = ctl.getAttribute("id");			
				if(!ctl.value)
				{
					document.images["aval1"].style.visibility = "hidden";
					for(var b = 1; b < site_names.length; b++)
					{
						var update_img = site_names[b].concat("1");					
						document.images[update_img].style.visibility = "hidden";
					}
					
					alert("One of the field is empty. Click OK to go to that field");
					valid = false;
					//ctl.style.background="yellow";
					error = "true";
					ctl.focus();				
					break;
				}
				else if(isNaN(ctl.value) == "true")
				{
					document.images["aval1"].style.visibility = "hidden";
					for(var b = 1; b < site_names.length; b++)
					{
						var update_img = site_names[b].concat("1");					
						document.images[update_img].style.visibility = "hidden";
					}
					alert("One of the field is not a number. Click OK to go to that field");
					valid = false;
					//ctl.style.background="yellow";
					error = "true";
					ctl.focus();
					break;
				}
				else if(ctl_id.match(/r.*?/))
				{				
					if(ctl.value == 0)
					{
						document.images["aval1"].style.visibility = "hidden";
						for(var b = 1; b < site_names.length; b++)
						{
							var update_img = site_names[b].concat("1");					
							document.images[update_img].style.visibility = "hidden";
						}
						alert("Price cannot be 0. Click OK to go to that field");
						valid = false;
						//ctl.style.background="yellow";
						error = "true";
						ctl.focus();
						break;
					}
					if(ctl.value < min_rate_prop)
					{
						document.images["aval1"].style.visibility = "hidden";
						for(var b = 1; b < site_names.length; b++)
						{
							var update_img = site_names[b].concat("1");					
							document.images[update_img].style.visibility = "hidden";
						}
						alert("Rate cannot be less than "+min_rate_prop+". Click OK to go to that field");
						valid = false;
						//ctl.style.background="yellow";
						error = "true";
						ctl.focus();
						break;
					}
				}
			}
		}
		if(valid == true)
		{
			doWork();
			//alert("AVAILABILITY CLEAR");
		}
	}
}

//roommanager.php: Validate rates for null and numeric
function validate_price(f, btn_value)
{
	var Numeric;
	Numeric = "^[0-9]+$";

	var valid = true;
	var color = "FF0000";
	
	var inputs = new Array();

	theForm = document.getElementById("updateprice");
	inputs = theForm.getElementsByTagName("input");
	var input_length = inputs.length;
	
	document.getElementById("btn").value = btn_value;
	alert(input_length);
	//inputs[10].focus();
	for(var i=0; i < input_length; i++ )
	{			
		var ctl = inputs[i];
		alert(inputs[i]);
		if(inputs[i].type == "text")
		{
			if(!inputs[i].value)
			{
				alert("Please enter a value");
				valid = false;
				//ctl.style.background="yellow";
				ctl.focus();
				setBackground(x, ctl);
				break;
			}
			else if(!inputs[i].value.match(Numeric))
			{
				alert("Please enter a number");
				valid = false;
				//ctl.style.background="yellow";
				ctl.focus();
				setBackground(x, ctl);
				break;
			}
			if(ctl.match(/r.*?/))
			{
				alert(min_rate_prop);
				if(inputs[i].value < min_rate_prop)
				{
					alert("Rate cannot be less than "+min_rate_prop);
					valid = false;
					//ctl.style.background="yellow";
					ctl.focus();
					setBackground(x, ctl);
					break;
				}
			}
		}
	}
	if(valid == true)
	{
		theForm.submit();  
		//alert("PRICE CLEAR");
	}
}

//bulkupdates.php: Validate values for null and numeric
function validate(f, btn_value)
{
    var e = btn_value;
	document.images[e].style.visibility = "visible";
	
	theForm = document.getElementById("bulkupdate");
	
	//alert(theForm.name);
	
	var room_checked = false;
	var days_checked = false;
	var options_checked = "0";
	var sites_checked = false;
    
	var Numeric;
	Numeric = "^[0-9]+$";
	
	var room_length = document.getElementById('bulkupdate').room.length;

	for(var i = 0; i < room_length; i++)
	{
		if(document.getElementById('bulkupdate').room[i].checked)
		{
			i = room_length;
			room_checked = true;			
		}		
	}
	for(var i = 0; i < 7; i++)
	{
		if(document.getElementById('bulkupdate').day[i].checked)
		{
			i = 8;
			days_checked = true;			
		}		
	}
	/*
	for(var i = 0; i < 4; i++)
	{
		if(document.getElementById('bulkupdate').change[i].checked)
		{
			i = 8;
			options_checked = true;			
			var selected_option = document.bulkupdate.change[i].value;
			alert();
			//var selected_option = document.getElementById('bulkupdate').change[i].checked.value;
		}		
	}*/
	
	//alert(selected_option);
	if(days_checked == false)
	{
		document.images[e].style.visibility = "hidden";
		alert("Please select atleast one day");
	}
	else if(room_checked == false)
	{
		document.images[e].style.visibility = "hidden";
		alert("Please select atleast one room type");
	}	
	else if(document.getElementById('bulkupdate').change_aval.checked)
	{		
		if(!document.getElementById('bulkupdate').availability.value.match(Numeric))
		{
			document.images[e].style.visibility = "hidden";
			alert("The availability value should be numeric");
			document.getElementById('bulkupdate').availability.focus();
		}
		else
		{
			document.getElementById( 'bulkupdate' ).submit();
		}
		//options_checked = "1";		
	}
	else if(document.getElementById('bulkupdate').change_rate.checked)
	{		
		if(!document.getElementById('bulkupdate').rate.value.match(Numeric))
		{
			document.images[e].style.visibility = "hidden";
			alert("The rate value should be numeric");
			document.getElementById('bulkupdate').rate.focus();
		}
		else
		{
			document.getElementById( 'bulkupdate' ).submit();
		}		
		//options_checked = "1";
	}
	else if(document.getElementById('bulkupdate').change_minstay.checked)
	{
		if(!document.getElementById('bulkupdate').minstay.value.match(Numeric))
		{
			document.images[e].style.visibility = "hidden";
			alert("The minimum stay value should be numeric");
			document.getElementById('bulkupdate').minstay.focus();
		}
		else
		{
			document.getElementById( 'bulkupdate' ).submit();
		}
		//options_checked = "1";
	}
	else if(document.getElementById('bulkupdate').change_stopsell.checked)
	{
		//options_checked = "1";
		document.getElementById( 'bulkupdate' ).submit();
	}
	/*
	else if(options_checked == "0")
	{
		document.images[e].style.visibility = "hidden";
		alert("Please select one option to change");
	}*/
	else
	{
		//theForm.submit();
		alert("ok");
	}
	
}

function validate_adjustrates(f, btn_value)
{
	document.getElementById( 'adjust_rates' ).submit();
}

//default_inclusions.php: Validate values for special characters onchange
function validate_value(entered)
{
	//alert(entered);
	var Characters;
	Characters = "/$&";
	
	var strToSearch = document.getElementById(entered).value;
	for (var i = 0; i < strToSearch.length; i++)
	{
		if (Characters.indexOf(strToSearch.charAt(i)) != -1)
		{
			alert ("The following characters are not permitted - / $ & - Please re-enter and continue");
			document.getElementById(entered).focus();
			//setBackgroundFocus(document.getElementById(entered));
			return false;
		}
		else
		{
			setBackgroundChange(document.getElementById(entered));	
		}
	}
}

//Default_inclusions.php: Validate entertainment for '&'
function validate_ent(entered)
{
	//alert(entered);
	var Characters;
	Characters = "&";
	
	var strToSearch = document.getElementById(entered).value;
	for (var i = 0; i < strToSearch.length; i++)
	{
		if (Characters.indexOf(strToSearch.charAt(i)) != -1)
		{
			alert ("The following characters are not permitted - & - Please re-enter and continue");
			document.getElementById(entered).focus();
			//setBackgroundFocus(document.getElementById(entered));
			return false;
		}
		else
		{
			setBackgroundChange(document.getElementById(entered));	
		}
	}
}

//default_inclusions.php: Validate values for numeric and null
function validate_default(f, btn_value)
{
    var e = btn_value;
	document.images[e].style.visibility = "visible";
    
	var Numeric;
	Numeric = "^[0-9]+$";

	var valid = true;
	
	var inputs = new Array();

	theForm = document.getElementById("default_inclusions");
	inputs = theForm.getElementsByTagName("input");
	var input_length = inputs.length;
	
	for(var i=0; i < input_length; i++ )
	{			
		var ctl = inputs[i];
		if(inputs[i].type == "text")
		{
			if(!inputs[i].value)
			{
                document.images[e].style.visibility = "hidden";
				alert("Please enter a value");
				valid = false;
				//ctl.style.background="yellow";
				ctl.focus();
				setBackgroundFocus(ctl);
				break;
			}
			else if(!inputs[i].value.match(Numeric))
			{
                document.images[e].style.visibility = "hidden";
				alert("Please enter a number");
				valid = false;
				//ctl.style.background="yellow";
				ctl.focus();
				setBackgroundFocus(ctl);
				break;
			}
		}
	}
	if(valid == true)
	{
		theForm.submit();  
		//alert("PRICE CLEAR");
	}
}

function validate_default_readyrooms(f, btn_value)
{
    var e = btn_value;
	document.images[e].style.visibility = "visible";
    
	var Numeric;
	Numeric = "^[0-9]+$";

	var valid = true;
	
	var inputs = new Array();

	theForm = document.getElementById("default_inclusions");
	inputs = theForm.getElementsByTagName("input");
	var input_length = inputs.length;
	
	for(var i=0; i < input_length; i++ )
	{			
		var ctl = inputs[i];
		if(inputs[i].type == "text")
		{
			if(!inputs[i].value)
			{
                document.images[e].style.visibility = "hidden";
				alert("Please enter a value");
				valid = false;
				//ctl.style.background="yellow";
				ctl.focus();
				setBackgroundFocus(ctl);
				break;
			}
			else if(!inputs[i].value.match(Numeric))
			{
                document.images[e].style.visibility = "hidden";
				alert("Please enter a number");
				valid = false;
				//ctl.style.background="yellow";
				ctl.focus();
				setBackgroundFocus(ctl);
				break;
			}
		}
	}
	
	var inputs = new Array();
	
	inputs = theForm.getElementsByTagName("textarea");
	var input_length = inputs.length;
	
	for(var i=0; i < input_length; i++ )
	{			
		var ctl = inputs[i];
		//alert(ctl.value);
		var Characters;
		Characters = "/$&";
		
		var char_flag = false;
		
		var strToSearch = ctl.value;
		for (var j = 0; j < strToSearch.length; j++)
		{
			if (Characters.indexOf(strToSearch.charAt(j)) != -1)
			{
				alert ("The following characters are not permitted - /$& - Please re-enter and continue");
				ctl.focus();
				
				//setBackgroundFocus(document.getElementById(ctl));
				valid = false;
				document.images[e].style.visibility = "hidden";
				
				j = strToSearch.length+1;
				i = input_length+1;
			}
		}
	}
	
	if(valid == true)
	{
		theForm.submit();  
		//alert("PRICE CLEAR");
	}
}

function validate_default_ent(f, btn_value)
{
    var e = btn_value;
	document.images[e].style.visibility = "visible";
    
	var Numeric;
	Numeric = "^[0-9]+$";

	var valid = true;
	
	var inputs = new Array();

	theForm = document.getElementById("default_inclusions");
	inputs = theForm.getElementsByTagName("input");
	var input_length = inputs.length;
	
	for(var i=0; i < input_length; i++ )
	{			
		var ctl = inputs[i];
		if(inputs[i].type == "text")
		{
			if(!inputs[i].value)
			{
                document.images[e].style.visibility = "hidden";
				alert("Please enter a value");
				valid = false;
				//ctl.style.background="yellow";
				ctl.focus();
				setBackgroundFocus(ctl);
				break;
			}
			else if(!inputs[i].value.match(Numeric))
			{
                document.images[e].style.visibility = "hidden";
				alert("Please enter a number");
				valid = false;
				//ctl.style.background="yellow";
				ctl.focus();
				setBackgroundFocus(ctl);
				break;
			}
		}
	}
	
	var inputs = new Array();
	
	inputs = theForm.getElementsByTagName("textarea");
	var input_length = inputs.length;
	
	for(var i=0; i < input_length; i++ )
	{			
		var ctl = inputs[i];
		//alert(ctl.value);
		var Characters;
		Characters = "&";
		
		var char_flag = false;
		
		var strToSearch = ctl.value;
		for (var j = 0; j < strToSearch.length; j++)
		{
			if (Characters.indexOf(strToSearch.charAt(j)) != -1)
			{
				alert ("The following characters are not permitted - & - Please re-enter and continue");
				ctl.focus();
				
				//setBackgroundFocus(document.getElementById(ctl));
				valid = false;
				document.images[e].style.visibility = "hidden";
				
				j = strToSearch.length+1;
				i = input_length+1;
			}
		}
	}
	
	if(valid == true)
	{
		theForm.submit();  
		//alert("PRICE CLEAR");
	}
}