function ValidateForm(form)
{

	var x = form.elements;
	for (var i=0;i<x.length;i++)
	{
		EleObj=x[i].id;
		LastIndex = EleObj.substring(EleObj.length-2,EleObj.length)
		switch(LastIndex)
		{
			case "_R":
				if(ValidateForm_Required(x[i],x[i].title)==false)
					return false;
			break;
			case "_P":
				if(ValidateForm_PostCode(x[i],x[i].title)==false)
					return false;
			break;
			case "_E":
				if(ValidateForm_Email(x[i],x[i].title)==false)
					return false;
			break;
			case "_I":
				if(ValidateForm_Required(x[i],x[i].title)==false)
					return false;
				if(ValidateForm_Numeric(x[i],'0123456789',x[i].title)==false)
					return false;
			break;
			case "_N":
				if(ValidateForm_Numeric(x[i],'0123456789',x[i].title)==false)
					return false;
					
			break;
		}		
	}
return true;
}

function ValidateForm_Required(Ctrl,msg)
{
	if(trimString(Ctrl.value) == "")
	{
		alert(msg);
		Ctrl.focus();
		return false;
	}
	return true;
}
function ValidateForm_PostCode(str,msg)
{
	
	if(postit(str.value)==false)
	{
		//alert(msg);
		str.focus();
		return false;
	}
	return true;
}
function ValidateForm_Checked(Ctrl,msg)
{
	if(Ctrl.checked == false)
	{
		alert(msg);
		Ctrl.focus();
		return false;
	}
	return true;
}
function ValidateForm_Numeric(Ctrl,valid_chars,msg)
{
	if(chkNumericValidate(Ctrl.value,valid_chars) == false)
	{
		alert(msg);
		Ctrl.focus();
		return false;
	}
	return true;
}
function chkNumericValidate(strString,strValidChars)
{
   var strChar;
   var blnResult = true; 
   for (i = 0; i < strString.length && blnResult == true; i++)
   {
	  strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
      {
    	   blnResult = false;
      }
   }
   return blnResult;
}
function ValidateForm_Email(Ctrl,msg)
{
	if(chkEmailValidate(Ctrl.value) == false)
	{
		alert(msg);
		Ctrl.focus();
		return false;
	}
	return true;
}

function ValidateForm_Confirm(Ctrl1,Ctrl2,msg)
{
	if(Ctrl1.value != Ctrl2.value)
	{
		alert(msg);
		Ctrl2.focus();
		return false;
	}
	return true;
}

function chkEmailValidate(str)
{
	return(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(str));
}
function trimString (str)
{
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function AddressSame(Obj)
{
	if(Obj.checked==true)
	{
		document.getElementById('sfname_R').value = document.getElementById('bfname_R').value;
		document.getElementById('slname_R').value = document.getElementById('blname_R').value;
		document.getElementById('sfaddress_R').value = document.getElementById('bfaddress_R').value;
		document.getElementById('ssaddress').value = document.getElementById('bsaddress').value;
		document.getElementById('sphone_R').value = document.getElementById('phone_R').value;
		document.getElementById('scity_R').value = document.getElementById('city_R').value;
		document.getElementById('scounty_R').value = document.getElementById('county_R').value;
		document.member.spincode.value=document.member.pincode.value;
		document.getElementById('scountry').value = document.getElementById('country').value;		
		
	}
	
}

function postit(STR)
{ 
//check postcode format is valid
 
test = STR;
 
  size = test.length
 test = test.toUpperCase(); //Change to uppercase
 while (test.slice(0,1) == " ") //Strip leading spaces
  {test = test.substr(1,size-1);size = test.length
  }
 while(test.slice(size-1,size)== " ") //Strip trailing spaces
  {test = test.substr(0,size-1);size = test.length
  }
 //document.details.pcode.value = test; //write back to form field
 
 if (size < 6 || size > 8){ //Code length rule
  alert(test + " is not a valid postcode - wrong length");
  //document.details.pcode.focus();
  return false;
  }
 if (!(isNaN(test.charAt(0)))){ //leftmost character must be alpha character rule
   alert(test + " is not a valid postcode - cannot start with a number");
   //document.details.pcode.focus();
   return false;
  }
 if (isNaN(test.charAt(size-3))){ //first character of inward code must be numeric rule
   alert(test + " is not a valid postcode - alpha character in wrong position");
   //document.details.pcode.focus();
   return false;
  }
 if (!(isNaN(test.charAt(size-2)))){ //second character of inward code must be alpha rule
   alert(test + " is not a valid postcode - number in wrong position");
   //document.details.pcode.focus();
   return false;
  }
 if (!(isNaN(test.charAt(size-1)))){ //third character of inward code must be alpha rule
   alert(test + " is not a valid postcode - number in wrong position");
   //document.details.pcode.focus();
   return false;
  }
 if (!(test.charAt(size-4) == " ")){//space in position length-3 rule
   alert(test + " is not a valid postcode - no space or space in wrong position");
   //document.details.pcode.focus();
   return false;
   }
 count1 = test.indexOf(" ");count2 = test.lastIndexOf(" ");
 if (count1 != count2){//only one space rule
   alert(test + " is not a valid postcode - only one space allowed");
   //document.details.pcode.focus();
   return false;
  }
//alert("Postcode Format OK");
return true;
}
//  End -->


