
//
// CONFIG VARIABLES
//

var domain = "http://localhost/appeal/";

// skin captcha
var RecaptchaOptions = {
   theme : 'clean'
};

//
// JOIN US
//

function join_us()
{
	window.location.href = "https://services.associa.co.uk/webJoin/?CCode=CAMRA&RtnUrl=http://www.camraappeal.org.uk";
}

function benefits()
{
	window.location.href = "http://www.camra.org.uk/joinus";
}

//
// COMMENT FUNCTIONS
//

var xmlhttpComments;
var number_comments;
var initial_number_comments;
var total_number_comments;

function post_comment()
{	
	// check t&c checkbox
	if (document.getElementById("terms_checkbox").checked == true)
	{	
		document.comment_form.submit();
			return true;
	}
	else
	{
		alert("You must agree to the terms and conditions before posting");
			return false;	
	}
		return false;
}

function read_comments()
{
	window.location.href = "/appeal/comments.php";
}

function leave_comment()
{
	window.location.href = "/appeal/postcomment.php";
}

function report_a_comment()
{
	window.location.href = "mailto:tom.blakemore@camra.org.uk";
}

function get_more_comments()
{
	// increment comments
	number_comments += initial_number_comments;
	
	xmlhttpComments = GetXmlHttpObject();
	
	if (xmlhttpComments == null) {
		alert ("Browser does not support HTTP Request");
			return;
	}
	
	// var url = domain + "xml/comments.php?n=" + number_comments;
	var url = domain + "php/display_comments.php?n=" + number_comments;
	
	xmlhttpComments.onreadystatechange = stateChangedComments;
	xmlhttpComments.open("GET", url, true);
	xmlhttpComments.send(null);
}

function stateChangedComments()
{
	if (xmlhttpComments.readyState == 4)
	{
		// get result
		var text = xmlhttpComments.responseText;
		
		// set comments div
		var comments_div = document.getElementById("comments");
		
		// display comments
		comments_div.innerHTML = text;
	}
}

//
// DONATION FUNCTIONS
//

var dummy0, dummy1 = 0;

function go_home()
{
	window.location.href = "/appeal";
}

function goto_donation_page()
{
	window.location.href = "/appeal/donate.php";
}

function enter_amount_change(value)
{
	if (value.length > 0)
	{
		document.getElementById("other_check").checked = "checked";
	}
}

function other_radio_chosen(value)
{	
	if (value == "other")
	{
		// enable text box
		document.getElementById("enter_donation_amount").focus();
		dummy1 = 1;
	}
	else
	{
		//delete the cells
		if (dummy1 == 1)
		{
			// disable text box
			
			dummy1 = 0;
		}
	}
}

function make_donation()
{	
		// get any set donation values
		var donation_membership_number  = document.make_donation_form.donation_membership_number.value;
		
		// check if it's set
		if ( ( donation_membership_number == null) || ( donation_membership_number == "") )
		{
			document.make_donation_form.donation_membership_number.value = "0";
		}
		
		// get any set donation values
		var donation_value = document.make_donation_form.donation_value.value;
		
		// check if it's set
		if ( ( donation_value == null) || ( donation_value == "") )
		{
			// text donation
			text_donation = document.make_donation_form.enter_donation_amount.value;
			
			// get amount (radio buttons)	
			if (document.make_donation_form.donation_amount)
			{
				radioObj = document.make_donation_form.donation_amount;
				for (i = 0; i < radioObj.length; i++)
				{
					if (radioObj[i].checked == true)
					{
						var value = radioObj[i].value;
					}
				}
				
				if (value == "other")
				{
					value = text_donation;
				}
			}
		}
		else
		{
			value = donation_value;
		}
		
		// check the integrity of the value
		if (isNaN(value))
		{
			document.make_donation_form.enter_donation_amount.value = "";
			document.make_donation_form.enter_donation_amount.focus();
				alert("Please enter a valid amount for your donation");
					return false;
		}
		else
		{	
			// initialise decimal length
			var decimal_length = 0;
			
			// set an integer version of the value
			var valueInt = parseInt(value);
			
			// check if it's an integer
			if ( valueInt.toString() != value.toString() )
			{
				// check the value is of correct format
				var split_value = value.split(".");
				var decimal_length = split_value[1].length;
			}
						
			// check that the amount is a number
			if (decimal_length <= 2)
			{
				// set donation value
				document.make_donation_form.donation_value.value = value;
				
				// check to see value of vale
				if (parseInt(value) > 499.99)
				{
					window.location.href = "/appeal/largedonation.php?donation_amount=" + value;
				}
				else
				{
					document.make_donation_form.submit();
						return true;
				}
			}
			else
			{
				document.make_donation_form.enter_donation_amount.value = "";
				document.make_donation_form.enter_donation_amount.focus();
					alert("Please enter a valid amount for your donation");
						return false;
			}
		}
	
		return false;
}

//
// AUCTION
//

function return_to_auction()
{
	window.location.href = "/appeal/auction.php";	
}

function place_bid()
{
	var result = false;
	
	// get form varibales
	var bid = document.getElementById("bid_amount");
	var name = document.getElementById("bid_name");
	var email = document.getElementById("bid_email");
	var telephone = document.getElementById("bid_telephone");
	
	// check bid amount
	if ( checkBidAmount(bid.value) )
	{
		// check name
		if ( checkName(name.value) )
		{
			// check email address
			if ( checkEmailAddress(email.value) )
			{
				// check telephone number
				if ( checkTelephoneNumber(telephone.value) )
				{
					result = true;
				}	
				else
				{
					alert("Please enter your telephone number");
						telephone.select();
							telephone.focus();
				}
			}
			else
			{
				alert("Please enter a valid email address");
					email.select();
						email.focus();
			}
		}
		else
		{
			alert("Please enter your name");
				name.select();
					name.focus();
		}
	}
	else
	{
		alert("Please enter a valid bid amount");
			bid.select();
				bid.focus();
	}
	
	if (result)
	{
		document.place_bid_form.submit();
	}
	
	return result;
}

// check bid amount
function checkBidAmount(value)
{
	// check for zero value
	if ( ( value == null) || ( value == "") )
	{
		return false;
	}
	else
	{
		// strip the pound sign at the front if one exists
		if ( isNaN( value.substr(0, 1) ) )
		{
			value = value.substr(1);
		}
		
		// check the integrity of the value
		if ( isNaN(value) )
		{
			return false;
		}
		else
		{	
			// check against the highest bid
			if ( parseFloat(document.getElementById("highest_bid").innerHTML) >= parseFloat(value) )
			{
				return false;
			}
			
			// initialise decimal length
			var decimal_length = 0;
			
			// set an integer version of the value
			var valueInt = parseInt(value);
			
			// check if it's an integer
			if ( valueInt.toString() != value.toString() )
			{
				// check the value is of correct format
				var split_value = value.split(".");
				var decimal_length = split_value[1].length;
			}
			
			// check that the amount is a number
			if ( decimal_length <= 2 )
			{
				if ( (decimal_length == 0) || (decimal_length == 2) )
				{
					return true;
				}
				else if ( decimal_length == 1 )
				{
					return false;
				}
			}
			else
			{
				return false;
			}
		}
	}
}

// email address validator
function checkEmailAddress(value)
{
	// Array holds the regular expressions for the valid postcodes
	var pcexp = new Array();
	
	// Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
	pcexp.push (new RegExp ("^([a-zA-Z0-9._-]+@[a-zA-Z0-9-.]+\.[a-zA-Z.]{2,6})$","i"));
	
	// Load up the string to check
	var emailAddress = value;
	
	// Assume we're not going to find a valid postcode
	var valid = false;
	
	// validate
	for (var i=0; i<pcexp.length; i++)
	{
		if (pcexp[i].test(emailAddress))
		{
			// switch validation flag
			valid = true;
			
			// break from loop
			break;
		}
	}
	
	// Return with either the reformatted valid email address or the original invalid email address
	if (valid)
	{
		return true;
	}
	else
	{
		return false;
	}
}

// function to check telephone number
function checkName(value)
{
	return simpleStringChecker(value);
}

// function to check telephone number
function checkTelephoneNumber(value)
{	
	return simpleStringChecker(value);
}

// simple string checker
function simpleStringChecker(value)
{
	// check if string length is greater than zero
	if (value.toString().length > 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

//
// GENERAL
//

function GetXmlHttpObject()
{
	if (window.XMLHttpRequest) // code for IE7+, Firefox, Chrome, Opera, Safari
	{
		return new XMLHttpRequest();
	}
	
	if (window.ActiveXObject) // code for IE6, IE5
	{
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	return null;
}
