/*
	Website scripts
	(c) Kerve Design
*/

function printPopup(pubid) {
	var new_window = window.open('/includes/printPopup.html?id='+pubid,'print_details','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=600,height=500');
}	

/* AJAX Connection strings */
//HTTPRequest string for main data
function getData(datasource,divID) {
	//Initial a HTTPXML Connection string
	var XMLHttpRequestObject = null;
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        try {
            XMLHttpRequestObject = new XMLHttpRequest( );
        } catch(e) {
            XMLHttpRequestObject = null;
        }
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        try {
            XMLHttpRequestObject = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
               XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
               XMLHttpRequestObject = null;
            }
        }
    }
    
	if(XMLHttpRequestObject) {
		XMLHttpRequestObject.open("GET", datasource);
		XMLHttpRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		XMLHttpRequestObject.onreadystatechange = function() {
			if(XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
				/* Debug ALL data being returned from the AJAX request */
				//alert(XMLHttpRequestObject.responseText);
				/* return the data parsed by the AJAX'd php file to the supplied div in 'divID' */
				if(divID != "") {
					//document.getElementById(divID).innerHTML = XMLHttpRequestObject.responseText;
					document.getElementById(divID).innerHTML = XMLHttpRequestObject.responseText;
				}
				if(navigator.appName == "Microsoft Internet Explorer") {
					if(document.all.ajax_popup_while_response) {
						document.all.ajax_popup_while_response.style.visibility = 'hidden';
					}
				} else {
					if(document.getElementById('ajax_popup_while_response')) {
						document.getElementById('ajax_popup_while_response').style.visibility = 'hidden';
						
					}
				}
			} else {
				if(navigator.appName == "Microsoft Internet Explorer") {
					if(document.all.ajax_popup_while_response) {
						document.all.ajax_popup_while_response.style.visibility = 'visible';
					}
				} else {
					if(document.getElementById('ajax_popup_while_response')) {
						document.getElementById('ajax_popup_while_response').style.visibility = 'visible';
					}
				}
			}
		}
		XMLHttpRequestObject.send(null);
	}
}


/* AJAX County search/browsing */
function searchCounty(county) {
	self.location = '/pubfinder.html?county='+county;
}


function searchPostcode() {
	var errormessage = 'Please ensure that you have completed the following:\n';
	var filter  = /^([a-zA-Z0-9_\.\-])+ (([a-zA-Z0-9\-])+\.)+$/;
	if(document.form_postcode.pub_finder_postcode.value != '') {
		self.location = '/pubfinder.html?postcode='+document.form_postcode.pub_finder_postcode.value;
	} else {
		if(document.form_postcode.pub_finder_postcode.value == '') {
			errormessage += "  > Postcode\n";
		}
		
		alert(errormessage);
	}
}

function validateTradeLogin() {
	var errormessage = 'Please ensure that you have completed the following:\n';
	if((document.login.user_email.value != '') && (document.login.user_password.value != '')) {
		document.login.submit();
	} else {
		if(document.login.user_email.value == '') {
			errormessage += "  > Username\n";
		}
		if(document.login.user_password.value == '') {
			errormessage += "  > Password\n";
		}
		alert(errormessage);
	}
}


function searchRefine(type,type_value) {
	var facilities
	facilities = ''
	
	if(document.search_refine.s_accomodation.checked) { facilities += '&s_accomodation=' + document.search_refine.s_accomodation.value }
	if(document.search_refine.s_garden.checked) { facilities += '&s_garden=' + document.search_refine.s_garden.value }
	if(document.search_refine.s_carpark.checked) { facilities += '&s_carpark=' + document.search_refine.s_carpark.value }
	if(document.search_refine.s_food.checked) { facilities += '&s_food=' + document.search_refine.s_food.value }
	if(document.search_refine.s_disabled.checked) { facilities += '&s_disabled=' + document.search_refine.s_disabled.value }
	if(document.search_refine.s_children.checked) { facilities += '&s_children=' + document.search_refine.s_children.value }
	if(document.search_refine.s_cask.checked) { facilities += '&s_cask=' + document.search_refine.s_cask.value }
	
	var url = '/includes/pub_search.php?'+type+'='+type_value+''+facilities;
	
	getData(url,'search_result');
}

/* AJAX Postcode & Facilities search/browsing */
/* Keep the order of the s_ variables, this is crucial!! */
/* If you have a new facility, add it to the end of the list - remember to update the php function for this! */
function searchFull() {
	var errormessage = 'Please ensure that you have completed the following:\n';
	/*
	var filter1  = /^([a-zA-Z0-9])+ (([a-zA-Z0-9])+\.)+$/;
	var filter2  = /^([a-zA-Z0-9])+$/;
	var filter3  = /^(([a-zA-Z0-9])+\.)+$/;
	*/
	var filter4  = /^([xX])+$/;

	if(
	   ((document.form_postcode.pub_finder_postcode.value != '') && (filter4.test(document.form_postcode.pub_finder_postcode.value) == false)) ||
	   (document.form_postcode.pub_finder_town.value != '')
	) {
		if((document.form_postcode.pub_finder_postcode.value != '') && (document.form_postcode.pub_finder_town.value != '')) {
			self.location = '/pubfinder.html?town=' + document.form_postcode.pub_finder_town.value;
		} else {
			if(document.form_postcode.pub_finder_postcode.value != '') {
				self.location = '/pubfinder.html?postcode=' + document.form_postcode.pub_finder_postcode.value;
			}
			
			if(document.form_postcode.pub_finder_town.value != '') {
				self.location = '/pubfinder.html?town=' + document.form_postcode.pub_finder_town.value;
			}
		}
	} else {
		if(document.form_postcode.pub_finder_postcode.value == '') {
			errormessage += "  > Postcode\n";
		} else {
			if((filter4.test(document.form_postcode.pub_finder_postcode.value)) == true) {
				errormessage += "  > You must enter a valid UK postcode without any spaces.\n";
			}
		}
		if(document.form_postcode.pub_finder_town.value == '') {
			errormessage += "  > Town / City\n";
		}
		alert(errormessage);
	}
}


/* Submit Pub Rating data */
function submitPubRating() {
	//check rating values
	var chosen1 = '';
	var chosen2 = '';
	var chosen3 = '';
	for (i = 0; i <document.pub_rate.rate_service.length; i++) {
		if (document.pub_rate.rate_service[i].checked) {
			chosen1 = document.pub_rate.rate_service[i].value;
		}
	}
	for (i = 0; i <document.pub_rate.rate_food.length; i++) {
		if (document.pub_rate.rate_food[i].checked) {
			chosen2 = document.pub_rate.rate_food[i].value;
		}
	}
	for (i = 0; i <document.pub_rate.rate_location.length; i++) {
		if (document.pub_rate.rate_location[i].checked) {
			chosen3 = document.pub_rate.rate_location[i].value;
		}
	}

	if((chosen1 != '') && (chosen2 != '') && (chosen3 != '')) {
		var ratings
		ratings = ''
		ratings += '&r_service=' + chosen1;
		ratings += '&r_food=' + chosen2;
		ratings += '&r_location=' + chosen3;
		
		//send ajax data call and submit results
		getData('/includes/rating_functions.php?action=submit&pubid='+document.pub_rate.pubid.value+''+ratings,'pub_finder_ratethispub_middle_input')
		
		//show results for submitted ratings
		getData('/includes/rating_functions.php?action=showresults&pubid='+document.pub_rate.pubid.value,'pub_finder_details_ratethispub_results')
	} else {
		var errormessage = 'Please ensure that you have completed the following:\n';
		
		if(chosen1 == '') {
			errormessage += "  > Rating for service\n";
		}
		if(chosen2 == '') {
			errormessage += "  > Rating for food\n";
		}
		if(chosen3 == '') {
			errormessage += "  > Rating for location\n";
		}
		
		alert(errormessage);
	}
}


function validateMessage() {
	var errormessage = 'Please ensure that you have completed the following:\n';
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if(
	   (document.booking_form.forename.value != '') &&
	   (document.booking_form.surname.value != '') &&
	   (filter.test(document.booking_form.emailaddress.value)) &&
	   (document.booking_form.address.value != '')
	  ) {
			document.booking_form.submit();
	} else {
		if(document.booking_form.forename.value == '') {
			errormessage += "  > Your first name\n";
		}
		if(document.booking_form.surname.value == '') {
			errormessage += "  > Your last name\n";
		}
		if(document.booking_form.emailaddress.value == '') {
			errormessage += "  > Email address\n";
		} else {
			if((filter.test(document.booking_form.emailaddress.value)) == false) {
				errormessage += "  > You must enter a valid email address\n";
			}
		}
		if(document.booking_form.address.value == '') {
			errormessage += "  > Your address\n";
		}
		alert(errormessage);
	}
}

function validateApplicationForm() {
	document.tennancy_form.submit();
}

function validateBookingForm() {
	var errormessage = 'Please ensure that you have completed the following:\n';
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if(
	   (document.booking_form.email.value != '') &&
	   (filter.test(document.booking_form.email.value)) &&
	   (document.booking_form.name.value != '') &&
	   (document.booking_form.address.value != '')
	  ) {
			document.booking_form.submit();
	} else {
		if(document.booking_form.name.value == '') {
			errormessage += "  > Your first name\n";
		}
		if(document.booking_form.email.value == '') {
			errormessage += "  > Email address\n";
		} else {
			if((filter.test(document.booking_form.email.value)) == false) {
				errormessage += "  > You must enter a valid email address\n";
			}
		}
		if(document.booking_form.address.value == '') {
			errormessage += "  > Your address\n";
		}
		alert(errormessage);
	}
}