function validateForm(theForm)
{

	var form_ok = true
		
	$( 'fieldset', theForm ).each(function()
	{
		var fieldset = $(this);
		var fs_error = false;
		
		$('div.required input', fieldset ).each(function()
		{
			if( $(this).val() == '' )
			{
				$(this).attr('style','border: 1px solid #A66;background:#FFF5F5');
				fs_error=true;
			}
			else
			{
				$(this).attr('style','');
			}
		});
		
		var either_a = 'not-set';
		$('div.either-a input', fieldset ).each(function()
		{
			if( $(this).val() == '' )
			{
				$(this).attr('style','border: 1px solid #A66;background:#FFF5F5');
				if( either_a == 'not-set' )
				{
					either_a = false;
				}
			}
			else
			{
				$(this).attr('style','');
				either_a = true;
			}
		});		
		if( either_a == false )
		{
			fs_error=true;
		}

		$('div.required select', fieldset ).each(function()
		{
			if( $(this).val() == '' )
			{
				$(this).attr('style','border: 1px solid #A66;background:#FFF5F5');
				fs_error=true;
			}
			else
			{
				$(this).attr('style','');
			}
		});				

		if(fs_error == true)
		{
			form_ok = false;
			fieldset.children('.errorcontainer').removeClass('notice');
			fieldset.children('.errorcontainer').addClass('error');
			$theForm.unblockUI();
		}
		else
		{
			fieldset.children('.errorcontainer').removeClass('error');
			fieldset.children('.errorcontainer').addClass('notice');
		}
	});
	
	return form_ok;
	
}

function limitChars(textid, limit) {
	var field = $('#'+textid); 
	var infodiv = $('#'+textid+'_info');
	if( field.val() != undefined )
	{
		var textlength = field.val().length;
		if(textlength > limit)
		{
			infodiv.html('You cannot write more then '+limit+' characters!');
			field.val(field.val().substr(0,limit));
			infodiv.addClass('infodivwarn');
			infodiv.removeClass('infodivok');
			return false;
		}
		else
		{
			infodiv.html('You have '+ (limit - textlength) +' characters left.');
			infodiv.addClass('infodivok');
			infodiv.removeClass('infodivwarn');
			return true;
		}
	}
	return false;
}

  function xmlRequestObject() {
          var agt         = navigator.userAgent.toLowerCase();
          var is_major    = parseInt(navigator.appVersion);
          var is_minor    = parseFloat(navigator.appVersion);
          var is_ie       = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
          var is_ie3      = (is_ie && (is_major < 4));
          var is_ie4      = (is_ie && (is_major == 4) && (agt.indexOf("msie 4")!=-1) );
          var is_ie4up    = (is_ie && (is_major >= 4));
          var is_ie5      = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1) );
          var is_ie5_5    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5") !=-1));
          var is_ie5up    = (is_ie && !is_ie3 && !is_ie4);
          var is_ie5_5up  = (is_ie && !is_ie3 && !is_ie4 && !is_ie5);
          var is_ie6      = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );
          var is_ie6up    = (is_ie && !is_ie3 && !is_ie4 && !is_ie5 && !is_ie5_5);
          if (is_ie5) return new ActiveXObject('Msxml2.XMLHTTP');
          else if(is_ie5_5up) return new ActiveXObject('Microsoft.XMLHTTP');
          var o = new XMLHttpRequest();
          if (typeof(o) != 'undefined') return o;
          alert("couldn't instantiate XMLHttpRequest object");
          return false;
  }
                          
  function getAddressFromPostcode(postcode){
          
          var address = new Array();
          address['street1']  = '';
          address['district'] = '';
          address['town']     = '';
          address['county']   = '';
          address['country']  = '';
          address['mailsort'] = '';
          address['gride']    = '';
          address['gridn']    = '';
          address['error']    = "";
  
          var xmlhttp  = new xmlRequestObject();

          xmlhttp.open("GET", "/actions/postcode-lookup.html?postcode=" + escape(postcode) ,false);
          xmlhttp.send(null);
  
          if (xmlhttp.status == 200)
          {
			xmldata     = xmlhttp.responseXML;
			xml_address = xmldata.getElementsByTagName('address').length; 
			xml_error   = xmldata.getElementsByTagName('error').length; 

                  if( xml_address > 0 )
                  {
				for(var i = 0; i < xml_address; i++)
				{
					for( var j=0; j < xmldata.getElementsByTagName('address')[i].attributes.length; j++ )
					{
						if(xmldata.getElementsByTagName('address')[i].attributes[j].name)
						{
						      address[xmldata.getElementsByTagName('address')[i].attributes[j].name] = xmldata.getElementsByTagName('address')[i].attributes[j].value;
						}
					}
				}                  	
                  }
                  else if ( xml_error > 0 )
            	{
            		if(xmldata.getElementsByTagName('error')[0])
	                  {
	                          address['error']     = xmldata.getElementsByTagName('error')[0].firstChild.data;
	                  }
	                  else
	                  {
	                  	address['error'] = 'Unknown Error (AW1)';
	                  }
            	}
            	else
            	{
                  	address['error'] = 'Unknown Error (AW2)';
            	}
          	
                  return address;
          }
          else
          {
                  address['error']     = "Unknown Error: " + xmlhttp.status;
                  return address;
          }
  }
  
/* To Title Case 1.1.1
 * David Gouch <http://individed.com>
 * 23 May 2008
 * License: http://individed.com/code/to-title-case/license.txt
 *
 * In response to John Gruber's call for a Javascript version of his script: 
 * http://daringfireball.net/2008/05/title_case
 */

String.prototype.toTitleCase = function() {
    return this.replace(/([\w&`'‘’"“.@:\/\{\(\[<>_]+-? *)/g, function(match, p1, index, title) {
        if (index > 0 && title.charAt(index - 2) !== ":" &&
        	match.search(/^(a(nd?|s|t)?|b(ut|y)|en|for|i[fn]|o[fnr]|t(he|o)|vs?\.?|via)[ \-]/i) > -1)
            return match.toLowerCase();
        if (title.substring(index - 1, index + 1).search(/['"_{(\[]/) > -1)
            return match.charAt(0) + match.charAt(1).toUpperCase() + match.substr(2);
        if (match.substr(1).search(/[A-Z]+|&|[\w]+[._][\w]+/) > -1 || 
        	title.substring(index - 1, index + 1).search(/[\])}]/) > -1)
            return match;
        return match.charAt(0).toUpperCase() + match.substr(1);
    });
};
