// contacts page initiated functionality
	String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };


var registration = {
	message : '',

	hasClass : function(object, className)
	{
		if (!object.className) return false;
		return (object.className.search('(^|\\s)' + className + '(\\s|$)') != -1);
	},


	removeClass : function(object,className) 
	{
		if (!object) return;
		object.className = object.className.replace(new RegExp('(^|\\s)'+className+'(\\s|$)'), RegExp.$1+RegExp.$2);
	},


	addClass : function(object,className) 
	{
		if (!object || registration.hasClass(object, className)) return;
		if (object.className) 
		{
			object.className += ' '+className;
		} 
		else 
		{
			object.className = className;
		}
	},

	updateAccount : function()
	{
		var valid = false;
		var failbox = document.getElementById('failbox');
		var faillist = document.getElementById('faillist');
		faillist.innerHTML = '';
		registration.message = '';
		
		var changepw = false;
		
		if (document.getElementById('Name').value.length >0)
		{
			valid = true;
		}
		else
		{
			registration.message = '<LI>Required: Please enter your full name</LI>';
		}
	
		
		if (document.getElementById('passwordOld').value.length >0)
		{
			changepw = true
		}
		else if (document.getElementById('pwd').value.length >0)
		{
			registration.message = '<LI>Required: Please enter your original system password before trying to change the password.</LI>';
		}
		
		var validpw = true;
		var validplength = true;
	
		
		if (changepw) validplength = registration.validatePassword();
		if (validplength && changepw) validpw = registration.validatePasswords();
		
		
		var validuser = registration.validateUser();
		
		
		if (!valid || !validpw || !validuser || !validplength) faillist.innerHTML += registration.message;
		
		if (!validpw) faillist.innerHTML += "<LI>The two passwords entered did not match, please re-enter them carefully.</LI>";
		
				

		if (!valid || !validpw || !validuser ||  !validplength)
		{
			failbox.style.display = 'block';
		
		}
		else
		{
			//alert('valid');
		document.forms[0].submit();
		}

	}, 
	
	saveform : function()
	{
		var valid = false;
		var failbox = document.getElementById('failbox');
		var faillist = document.getElementById('faillist');
		faillist.innerHTML = '';
		registration.message = '';
		document.getElementById('terms').nextSibling.className = '';
				
		valid = registration.validate('content');
		
		var validpw = true;
		var validplength = true;
	
		
		if (valid) validplength = registration.validatePassword();
		if (validplength && valid) validpw = registration.validatePasswords();
		
		
		var validuser = registration.validateUser();
		
		var validterms = document.getElementById('terms').checked;
		

		if (!valid || !validuser || !validplength) faillist.innerHTML += registration.message;
		
		if (!validpw) faillist.innerHTML += "<LI>The two passwords entered did not match, please re-enter them carefully.</LI>";
		
		if (!validterms)
		{
			faillist.innerHTML += "<LI>Please agree to, and read, the <a href=\"terms.php\">terms & conditions</A> before signing up.</LI>";
			document.getElementById('terms').nextSibling.className = "requiredAlert";
		}
		

		if (!valid || !validpw || !validuser || !validterms || !validplength)
		{
			failbox.style.display = 'block';
		
		}
		else
		{
			//alert('valid');
		document.forms[0].submit();
		}

	}, 

	validate : function (containerId)
	{
		var inputs = document.getElementById(containerId).getElementsByTagName( 'input' );  
		var success = true;
		
		for (var i = 0; i < inputs.length; i++) 
		{
			inputs[i].value = inputs[i].value.trim();
			registration.removeClass(inputs[i],'inputAlert');
			if (inputs[i].nextSibling.nodeName == 'span') inputs[i].nextSibling.className = 'required';
	
	
			if (inputs[i].previousSibling.previousSibling && inputs[i].previousSibling.previousSibling.title.indexOf('Required:') != -1)
			{
				if (inputs[i].value.length == 0)
				{	
					success=false;
					registration.addClass(inputs[i],'inputAlert');
					if (inputs[i].nextSibling.nodeName == 'span') inputs[i].nextSibling.className = 'requiredAlert';
					 registration.message += '<li>' + inputs[i].previousSibling.previousSibling.title + '</li>';
				}
			}
		}
		
		return success;
	},
	
	validatePassword : function()
	{
		var success = true;
		if (document.getElementById('pwd').value.length < 6 || document.getElementById('pwd').value.length > 20)
		{
			registration.message +="<LI>Password must be more than 6 characters long, and less than 20</LI>";
			success = false;
		}
	return success;
	},
	
	validatePasswords : function()
	{
		var success = true;
		p1 = document.getElementById('pwd');
		p2 = document.getElementById('password2');
		registration.removeClass(p1,'inputAlert');
		registration.removeClass(p2,'inputAlert');
		p1.nextSibling.className = "required";
		p2.nextSibling.className = "required";
	
		if (p1.value != p2.value) 
		{
			registration.addClass(p1,'inputAlert');
			registration.addClass(p2,'inputAlert');
			p1.nextSibling.className = "requiredAlert";
			p2.nextSibling.className = "requiredAlert";
			p1.value = '';
			p2.value = '';
			success = false;
		}
	return success;
	},
	
	
	validateUser : function()
	{
		var mobile = document.getElementById('Mobile');
		var un = document.getElementById('Username');
		mobile.className = '';
		un.className = 'small'; 
		
		if(un.value.length == 0 && mobile.value.length == 0)
		{
			registration.message  += "<LI>Required: <B>EITHER</B> a short username <B>OR</B> a mobile telephone number to identifiy you to text message recipients.</LI>";
			mobile.className = 'inputAlert';
			un.className = 'small inputAlert';
			return false;
		}
		else 
		{
			if (mobile.value.length != 0)
			{
				// (OLD: if the username's blank, then and we're here, then the mobile must be filled in, hence validate IT:)
				// NEW: if the mobile's filled in, we use IT, it must be blank to use username...
				var vm = registration.validateMobile();
				if(!vm)
				{
					registration.message  += "<LI>Please enter a valid mobile telephone number.</LI>";
					return false;		
				}
				else
				{
					return true;
				}
			}
			else return true;
		}
	},
	
	validateMobile : function()
	// additional routine for checking valid mo ph no
	{

	
		var mobile = document.getElementById('Mobile');
		
		
		mobile.className = '';
			
		var num = mobile.value;
		num = num.replace(/[\(\)\.\-\_\,\[\]\+]/g, '');
		num = num.replace(/[a-z]/g, '');
		num = num.replace(/[A-Z]/g, '');
		num = num.trim();
		mobile.value = num;
	
		if (num.length >= 21 || num.length <= 10 || num.substring(0,1) != '0') 
		{
			mobile.className = 'inputAlert';
			
			return false;
		}
		else
		{
			return true;
		}
	}
}

var doc =
{
	alter : function (action)
	{
		credit = document.getElementById('credit');
		valu = document.getElementById('alter').value;
		
		if (action == 'add')
		{
			credit.value = parseFloat(credit.value) + parseFloat(valu)
		}
		else if (action == 'sub')
		{
			credit.value = parseFloat(credit.value) - parseFloat(valu)
		}
		
	}
	
}

var payments = 
{

	callPayment : function ()
	{
		picked = false;
		radios = document.getElementsByName('payment[]');
		for (var i = 0; i < radios.length; i++) 
		{
			if (radios[i].checked)
			{
				picked = true;
				document.getElementById('amount').value = radios[i].value;
			}
		}
		if (picked)
		{
			
			document.forms[0].submit();
			//alert (document.getElementById('amount').value);
		}
		else
		{
			alert ('Please select a payment amount before continuing.');
		}
	
	}
}
	
