
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

	function nameLookup(type, inputString, field, sBox, autoSBox) {
		if(inputString.length == 0) {
			// Hide the suggestion box.
			$(sBox).hide();
		} else {
			$.post("aj_name.php", {_m: ""+type+"", input: ""+inputString+"", field: ""+field+"", sBox: ""+sBox+""}, function(data){
				if(data.length >0) {
					$(sBox).show();
					$(autoSBox).html('<ul class="suggestionList">' + data + '</ul>');
				}
			});
		}
	} // lookup

	function nameFill(thisValue, field, sBox) {
		field.value = thisValue;
		setTimeout("$('" + sBox + "').hide();", 200);
	}

	function vetPracticeLookup(type, inputString, field, sBox, autoSBox) {
			$(sBox).hide();
		if(inputString.length < 4) {
			// Hide the suggestion box.
			$(sBox).hide();
		} else {
			$.post("aj_vetPractice.php", {_m: ""+type+"", input: ""+inputString+"", field: ""+field+"", sBox: ""+sBox+""}, function(data){
				if(data.length >0) {
					$(sBox).show();
					$(autoSBox).html('<ul class="suggestionList">' + data + '</ul>');
				}
			});
		}
	} // lookup

	function vetPracticeFill(thisValue, field, sBox) {
          document.form1.vetPracticeAdd.value = '';
          document.form1.vetPracticePCd.value = '';
          document.form1.vetPracticeTel.value = '';

          if(thisValue != '') {
            var addy = '';
            vetDetails = thisValue.split('|');

            field.value = vetDetails[0];

            vetAddDetails = vetDetails[1].split('%');
  
            for (i = 0; i < vetAddDetails.length - 2; i++ ) {
              if(vetAddDetails[i] != '') {
                document.form1.vetPracticeAdd.value += vetAddDetails[i].trim() + ', ';
                addy += vetAddDetails[i].trim() + '<BR>';
              }
            }

            document.form1.vetPracticeAdd.value += vetAddDetails[i].trim();
            addy += vetAddDetails[i].trim() + '<BR>';

            i++;

            document.form1.vetPracticePCd.value += vetAddDetails[i].trim();
            addy += vetAddDetails[i].trim();

            $('#add_label').html("Address<BR>" + addy);

            document.form1.vetPracticeTel.value = vetDetails[2];

            setTimeout("$('" + sBox + "').hide();", 200);
          }
	}

	function vetPracticeListHide(sBox) {
          setTimeout("$('" + sBox + "').hide();", 200);
  	}

  function pcodeFinderROReg(postcode, identifier, addressField, houseField, label, plabel) {
    var args_dialog  = 'status: yes; ';
        args_dialog += 'resizable: no; ';
        args_dialog += 'scroll: no; ';
        args_dialog += 'center: no; ';
        args_dialog += 'dialogHeight: 220px; ';
        args_dialog += 'dialogWidth: 480px; ';
        args_dialog += 'dialogLeft: 50px';
        args_dialog += 'dialogTop: 50px';

    if(!pcodeCheck(postcode))
      return false;

/*
    var destUrl = "ro_postcode.php?identifier=" + identifier + "&postcode=" + postcode.value;

    pcodeBox = showModalDialog(destUrl,'',args_dialog);
*/

    $(label).html('');
    $(plabel).html('');

    $.post("ro_postcode.php", {identifier: ""+identifier+"", postcode: ""+postcode.value+""}, function(data){
      if(data.length >0) {
        addressField.value = '';
        var addy = '';

        if (data.substring(0,5) == 'Error') {
          $(plabel).html("Unrecognised Postcode");
        }
        else {
        pcodeBox = data.split(',');

        houseField.value = houseField.value.trim().toUpperCase();

        addressField.value += houseField.value;
        addy = houseField.value.replace(/,/g, "<BR>");

        houseField.value.length < 5 ? addressField.value += ' ' : addressField.value += ', ';
        houseField.value.length < 5 ? addy += ' ' : addy += '<BR>';
 
        for (i = 0; i < pcodeBox.length - 2; i++ ) {
          if(pcodeBox[i] != '') {
            addressField.value += pcodeBox[i].trim().toUpperCase() + ', ';
            addy += pcodeBox[i].trim().toUpperCase() + '<BR>';
          }
        }

        addressField.value += pcodeBox[i].trim().toUpperCase();
        addy += pcodeBox[i].trim().toUpperCase() + '<BR>';

        i++;

        addy += pcodeBox[i].trim().toUpperCase();

        $(label).html("Address<BR>" + addy);

        postcode.value = pcodeBox[i].toUpperCase();
//        alert(addressField.value);
      }
      }
    });

    return true; 
  }

  function pcodeFinderRO(postcode, identifier, addressField, houseField, label, plabel) {
    var args_dialog  = 'status: yes; ';
        args_dialog += 'resizable: no; ';
        args_dialog += 'scroll: no; ';
        args_dialog += 'center: no; ';
        args_dialog += 'dialogHeight: 220px; ';
        args_dialog += 'dialogWidth: 480px; ';
        args_dialog += 'dialogLeft: 50px';
        args_dialog += 'dialogTop: 50px';

    if(!pcodeCheck(postcode))
      return false;

/*
    var destUrl = "ro_postcode.php?identifier=" + identifier + "&postcode=" + postcode.value;

    pcodeBox = showModalDialog(destUrl,'',args_dialog);
*/

    $(label).html('');
    if (plabel != '') 
      $(plabel).html('');

    $.post("ro_postcode.php", {identifier: ""+identifier+"", postcode: ""+postcode.value+""}, function(data){
      if(data.length >0) {
        addressField.value = '';
        var addy = '';
 
        if (data.substring(0,5) == 'Error') {
          if (plabel != '') 
            $(plabel).html("Unrecognised Postcode");
        }
        else {

        pcodeBox = data.split(',');

        addressField.value += houseField.value;
        addy = houseField.value;

        houseField.value.length < 5 ? addressField.value += ' ' : addressField.value += ', ';
        houseField.value.length < 5 ? addy += ' ' : addy += '<BR>';
 
        for (i = 0; i < pcodeBox.length - 2; i++ ) {
          if(pcodeBox[i] != '') {
            addressField.value += pcodeBox[i].trim().toUpperCase() + ', ';
            addy += pcodeBox[i].trim().toUpperCase() + '<BR>';
          }
        }

        addressField.value += pcodeBox[i].trim().toUpperCase();
        addy += pcodeBox[i].trim().toUpperCase() + '<BR>';

        i++;

        addy += pcodeBox[i].trim().toUpperCase();

        $(label).html("Address<BR>" + addy);

        postcode.value = pcodeBox[i].toUpperCase();
//        alert(addressField.value);
        }
      }
    });

    return true; 
  }

  function pcodeCheck(postcode) {

    var postcodeVal = postcode.value;

    if(postcodeVal.charAt(postcodeVal.length - 4) != ' ') {
      postcode.value = postcodeVal.substr(0,postcodeVal.length - 3) + ' ' +  postcodeVal.substr(postcodeVal.length - 3);
      postcodeVal = postcode.value;
    }

    if(postcodeVal.length < 6 || postcodeVal.length > 8) {
      alert('postcode is an incorrect length');
      return false;
    }

    if(postcodeVal.indexOf(' ') == -1 || postcodeVal.charAt(postcodeVal.length - 4) != ' ') {
      alert('Either the space was omitted or put it in the incorrect location in the postcode');
      return false;
    }

    if(postcodeVal.indexOf(' ') != postcodeVal.lastIndexOf(' ')) {
      alert('Only one space is required in the postcode.');
      return false;
    }

    if(postcodeVal.length - postcodeVal.indexOf(' ') != 4) {
      alert('There should be 3 characters after the space in the postcode');
      return false;
    }

    if(!isDigit (postcodeVal.charAt(postcodeVal.length - 3))) {
      alert('The first character after the space should be a number in the postcode');
      return false;
    }

    if(!isLetter (postcodeVal.charAt(postcodeVal.length - 2)) || !isLetter (postcodeVal.charAt(postcodeVal.length - 1))) {
      alert('The second and third characters after the space should be letters in the postcode');
      return false;
    }

    if(!isLetter (postcodeVal.charAt(0))) {
      alert('The first character of the postcodeVal should be a letter in the postcode');
      return false;
    }
/*
    if(!isDigit (postcodeVal.charAt(postcodeVal.indexOf(' ') - 1))) {
      alert('The last character before the space should be a number in the postcode');
      return false;
    }
*/
    return true;
  }


        function checkMicrochip(chip, label) {
          $(label).html('');

          $.post("aj_valid.php", {_m: "1", microchip: ""+chip+""}, function(data){
            if(data.length >0) {
              $(label).html(data);
            }
	  });

        }

        function checkConfirmationMicrochip(chip1, chip2, label) {
          $(label).html('');

          $.post("aj_valid.php", {_m: "2", microchip1: ""+chip1+"", microchip2: ""+chip2+""}, function(data){
            if(data.length >0) {
              $(label).html(data);
            }
	  });

        }

        function checkDob(checkDate, label) {
          $(label).html('');

          $.post("aj_valid.php", {_m: "6", checkDate: ""+checkDate+""}, function(data){
            if(data.length >0) {
              $(label).html(data);
            }
	  });

        }

        function checkDate(checkDate, label) {
          $(label).html('');

          $.post("aj_valid.php", {_m: "3", checkDate: ""+checkDate+""}, function(data){
            if(data.length >0) {
              $(label).html(data);
            }
	  });

        }

        function checkYear(checkDate, label) {
          $(label).html('');

          $.post("aj_valid.php", {_m: "10", checkDate: ""+checkDate+""}, function(data){
            if(data.length >0) {
              $(label).html(data);
            }
	  });

        }

        function checkUser(_u, label) {
          $(label).html('');
          _u = jQuery.trim(_u);

          $.post("aj_valid.php", {_m: "4", _u: ""+_u+""}, function(data){
            if(data.length >0) {
              $(label).html(data);
            }
	  });

        }

        function checkConformationEmail(email, conf_email, label) {
          $(label).html('');

          $.post("aj_valid.php", {_m: "9", email: ""+email+"", conf_email: ""+conf_email+""}, function(data){
            if(data.length >0) {
              $(label).html(data);
            }
	  });

        }

        function checkUnknownUser(_u, label, _s) {
          $(label).html('');

          $.post("aj_valid.php", {_m: "7", _u: ""+_u+""}, function(data){
            if(data.length >0) {
              if(data == '1') {
//              $(label).html(data);
                $("#userDetails").hide();
                $("#ownerDetails").hide();
                $("#otherDetails").hide();
                $("#miscDetails").hide();
                $("#foundDetails").show();

                $.post("aj_functions.php", {_m: "1", ownerEmail: ""+_u+"", _s: ""+_s+""});
              }
              else {
                $("#userDetails").show();
                $("#ownerDetails").show();
                $("#otherDetails").show();
                $("#miscDetails").show();
                $("#foundDetails").hide();
              }
            }
	  });

        }

        function checkUnknownUserName(_u, _fn, _sn, label) {
          $(label).html('');

          $.post("aj_valid.php", {_m: "8", _u: ""+_u+"", _fn: ""+_fn+"", _sn: ""+_sn+""}, function(data){
            if(data.length >0) {
              if(data == '1') {
//              $(label).html(data);
                $("#userDetails").hide();
                $("#ownerDetails").hide();
                $("#otherDetails").hide();
                $("#foundDetails").show();
              }
              else {
                $("#userDetails").show();
                $("#ownerDetails").show();
                $("#otherDetails").show();
                $("#foundDetails").hide();
              }
            }
	  });

        }

        function ocoAnimalCheck(_a, label, ln_label) {
          $(label).html('');

          $.post("aj_valid.php", {_m: "5", _a: ""+_a+""}, function(data){
            if(data.length >0) {
              $(label).html(data);
            }
            else
              $(ln_label).html('Animal not found - Please check details');
	  });
        }

        function submitData(session, form) {
          fields = form.elements;
          str = '';

          for (i = 3; i < fields.length - 1; i++) {
            if(fields[i].type == 'radio'|| fields[i].type == 'checkbox') {
              str += fields[i].name + ':' + fields[i].checked + '|';
            }
            if(fields[i].type == 'select-one') {
              str += fields[i].name + ':' + fields[i].options[fields[i].selectedIndex].text + '|';
            }
            else
              str += fields[i].name + ':' + jQuery.trim(fields[i].value) + '|';
          }

          $.post("aj_functions.php", {_m: "2",_s: ""+session+"", _f: ""+str+""}, function(data){
            if(data.length >0) {
              alert(data);
            }
	  });
        }

        function addressChange(field) {
          if(field.checked)  
            $("#addressDetails").show();
          else {
            $("#addressDetails").hide();
	  }
        }

