function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function CheckHideElem(RadioInputValue,HideValue,HideElement) {

   var elemHide = document.getElementById(HideElement);
   
   if(RadioInputValue == HideValue) {
      elemHide.style.display = 'none';
   }else{
      elemHide.style.display = 'block';
   }
}

function CheckShowHideElem(RadioInputValue,HideValue,HideElement,ShowElement) {

   var elemHide = document.getElementById(HideElement);
   var elemShow = document.getElementById(ShowElement);
   
   if(RadioInputValue == HideValue) {
      elemHide.style.display = 'none';
      elemShow.style.display = 'block';
   }else{
      elemHide.style.display = 'block';
      elemShow.style.display = 'none';
   }
}

function validation1(frm){
// validate level 1 questions
   var i;
   var isSelect;
   if(frm.firstname.value.length == 0){
      alert('Please enter your first name.');
      frm.firstname.focus();
      return false;
   }
   if(frm.surname.value.length == 0){
      alert('Please enter your last name.');
      frm.surname.focus();
      return false;
   }
   if(frm.email.value.length == 0){
      alert('Please enter your e-mail address.');
      frm.email.focus();
      return false;
   }
   if(!IsEmail(frm.email.value)){
      alert('Please make sure you have entered a valid e-mail address.\nA valid e-mail address may contain all alphanumeric characters and . - _');
      frm.email.focus();
      return false;
   }
   if(frm.email.value != frm.emailconf.value){
      alert('Please check that your confirmation e-mail address matches your e-mail address.');
      frm.emailconf.focus();
      return false;
   }
   isSelect = 0;
   for(i=0;i < frm.scotrel.length; i++){
      if(frm.scotrel[i].checked){
         isSelect=1;
      }
   }
   if(isSelect == 0){
      alert('Please indicate which option is most relevant to you.');
      frm.scotrel[0].focus();
      return false;
   }
   if(frm.country.selectedIndex == 0){
         alert('Please select your country of residence');
         frm.country.focus();
         return false;
   }
   if(frm.country.options[frm.country.selectedIndex].value == 'UK'){
      // uk specific questions
      isSelect = 0;
      for(i=0;i < frm.ukloyal.length; i++){
         if(frm.ukloyal[i].checked){
            isSelect=1;
         }
      }
      if(isSelect == 0){
         alert('Please choose the statement which most applies to you.');
         frm.ukloyal[0].focus();
         return false;
      }
   }else{
      // non uk specific questions
      isSelect = 0;
      for(i=0;i < frm.nonukloyal.length; i++){
         if(frm.nonukloyal[i].checked){
            isSelect=1;
         }
      }
      if(isSelect == 0){
         alert('Please choose the statement which most applies to you.');
         frm.nonukloyal[0].focus();
         return false;
      }
      isSelect = 0;
      for(i=0;i < frm.usstat.length; i++){
         if(frm.usstat[i].checked){
            isSelect=1;
         }
      }
      if(isSelect == 0){
         alert('Please indicate if you have booked a holiday or not yet.');
         frm.usstat[0].focus();
         return false;
      }
   }

}

function validation2(frm)

{

 // validate consumer questions 
 // most fields are optional

// make sure interests are populate

   var myChecks = document.getElementsByName("interests");
   var jsSelect = 0;
   var j = 0;

   for(j=0; j < myChecks.length; j++)
   {
      if(myChecks[j].checked)
      {
         jsSelect=1;
      }
   }

   if(jsSelect != 1) 
   {
      alert('Please enter holiday interests');
      frm.interests[0].focus();
      return false;   
   }

   jsSelect=0;

   for(j=0; j < myChecks.length; j++)
   {
      if((myChecks[j].value == 'OT') && (myChecks[j].checked))
      {
         jsSelect=1;
      }
   }

   if((jsSelect == 1) && (frm.interestother.value.length == 0))
   {
      alert('Please enter an other interest.');
      frm.interestother.focus();
      return false;   
   }
        
// check address1 entered

   if (frm.address1.value.length == 0)
   {
	alert('Please enter address line 1');
	frm.address1.focus();
	return false;
   }

// check city entered
   if (frm.city.value.length == 0)
   {
	alert('Please enter town/city');
	frm.city.focus();
	return false; 
   }


// validate postcode if not null when country = UK,US or CA

   if(frm.country.value == 'UK')
   {
      if(frm.postcode.value.length > 0)
      {
         if(!IsPostcode(trim(frm.postcode.value)))
         {
            alert('Please enter a valid UK postcode.');
            frm.postcode.focus();
            return false;
         }
      }
      else
      {
	    alert('Please enter a postcode');
            frm.postcode.focus();
            return false;
      }
   }

   if(frm.country.value == 'US')
   {
      if(frm.postcode.value.length > 0)
      {
         if(!IsUSZipCode(trim(frm.postcode.value)))
         {
            alert('Please enter a valid US zip code.');
            frm.postcode.focus();
            return false;
         }
      }
      else
      {
	    alert('Please enter a postcode');
            frm.postcode.focus();
            return false;
      }
   }

   if(frm.country.value == 'CA')
   {
      if(frm.postcode.value.length > 0)
      {
         if(!IsCAPostcode(trim(frm.postcode.value)))
         {
            alert('Please enter a valid Canadian postcode.');
            frm.postcode.focus();
            return false;
         }
      }
      else
      {
	    alert('Please enter a postcode');
            frm.postcode.focus();
            return false;
      }
   }
   
} 

function validation3(frm){
// validate gatekeeper questions
// some fields are optional

// make sure a group description is selected
   if(frm.groupdescr.selectedIndex == 0){
         alert('Please select a description for your interest group');
         frm.groupdescr.focus();
         return false;
   }
   
// make sure other info is provided if 'other' option selected from groupdescr drop down
   if(frm.groupdescr.options[frm.groupdescr.selectedIndex].value == 'OT'){
      if(frm.groupdescrother.value.length == 0){
         alert('Please describe your interest group');
         frm.groupdescrother.focus();
         return false;
      }
   }

// make sure a name for the association has been entered
   if(frm.groupname.value.length == 0){
      alert('Please enter the name of your group');
      frm.groupname.focus();
      return false;
   }

// make sure a role for the group has been entered
   if(frm.grouprole.selectedIndex == 0){
      alert('Please select your role within the group');
      frm.grouprole.focus();
      return false;
   }

// make sure a number of members for the group has been entered
   if(frm.grouppop.selectedIndex == 0){
      alert('Please select number of members within the group');
      frm.grouppop.focus();
      return false;
   }

// make sure other info is provided if 'other' option selected from grouprole drop down
   if(frm.grouprole.options[frm.grouprole.selectedIndex].value == 'E'){
      if(frm.grouproleother.value.length == 0){
         alert('Please describe your role');
         frm.grouproleother.focus();
         return false;
      }
   }

// validate society postcode if not null and country = UK, US or CA
   if(frm.gkcountry.value == 'UK'){
      if(frm.socpostcode.value.length > 0){
         if(!IsPostcode(frm.socpostcode.value)){
            alert('Please enter a valid UK postcode for your society.');
            frm.socpostcode.focus();
            return false;
         }
      }
   }
   if(frm.gkcountry.value == 'US'){
      if(frm.socpostcode.value.length > 0){
         if(!IsUSZipCode(frm.socpostcode.value)){
            alert('Please enter a valid US zip code for your society.');
            frm.socpostcode.focus();
            return false;
         }
      }
   }
   if(frm.gkcountry.value == 'CA'){
      if(frm.socpostcode.value.length > 0){
         if(!IsCAPostcode(frm.socpostcode.value)){
            alert('Please enter a valid Canadian postcode for your society.');
            frm.socpostcode.focus();
            return false;
         }
      }
   }
   
// validate society e-mail address if not null
   if(frm.socemail.value != null) {
      if(!IsEmail(frm.socemail.value)){
         alert('Please make sure you have entered a valid e-mail address.\nA valid e-mail address may contain all alphanumeric characters and . - _');
         frm.socemail.focus();
         return false;
      }
   }
   if(frm.socemail.value != frm.confsocemail.value){
      alert('Please check that your confirmation e-mail address matches your e-mail address.');
      frm.confsocemail.focus();
      return false;
   }
   
// validate postcode if not null and country = UK, US or CA
   if(frm.country.value == 'UK'){
   
         if(!IsPostcode(trim(frm.postcode.value))){
            alert('Please enter a valid UK postcode.');
            frm.postcode.focus();
            return false;
         }

   }
   if(frm.country.value == 'US'){

         if(!IsUSZipCode(trim(frm.postcode.value))){
            alert('Please enter a valid US zip code.');
            frm.postcode.focus();
            return false;
         }

   }
   if(frm.country.value == 'CA'){

         if(!IsCAPostcode(trim(frm.postcode.value))){
            alert('Please enter a valid Canadian postcode.');
            frm.postcode.focus();
            return false;
         }

   }

}