 // handle weird characters

 //<![CDATA[
 	var rfIndex       = new Object;
 	rfIndex.populated = false;


 /*
	Purpose: When a suburb is typed in, jump to a matching suburb in the list
 */
 function rfJump () {
 	if (!document.getElementById) return;
 		var text   = document.getElementById('suburblist');
 		var select = document.getElementById('suburblist2');
 		var text2 = document.getElementById('pcidlist');
 		if (!rfIndex.populated) rfBuildIndex();
 		if (!rfIndex.populated) rfPCIDBuildIndex();
 			var suburb = text.value.match(/,*([^,]+)$/);
 			if (suburb) {
 			var name = suburb[1].toUpperCase().replace(/^\s*/, '').replace(/\s*$/, '');
 			for (var i = rfIndex[name.charAt(0)]; i < select.options.length; i++) {
 			if (select.options[i].text.toUpperCase().indexOf(name) == 0) {
 				select.selectedIndex = i;
 				break;
 			}
 			else {
 				select.selectedIndex = -1;
			}
	 	}
 	}
 }

 /*
	Purpose: Click on the suburb to copy it to text field.
 */
 function rfCopy () {
 	if (!document.getElementById) return false;
 	var text    = document.getElementById('suburblist');
 	var select  = document.getElementById('suburblist2');
        var text2  = document.getElementById('pcidlist');
 	text.focus();
 	var suburb = select.options[select.selectedIndex];
 
 	// check list to see if suburb already in it
 	var textSuburbs = text.value.split(/\s*,\s*/);
 	if (!textSuburbs) return false;
 	var pattern = new RegExp('\s*' + suburb.text + '\s*$');
 	for (var i = 0; i < textSuburbs.length; i++) {
 		// if it's in the list don't do anything
 		if (pattern.exec(textSuburbs[i])) return false;
 	}
 
 	// check PCID list to see if suburb already in it
 	var pcidSuburbs = text2.value.split(/\d*,\d*/);
 	if (!pcidSuburbs) return false;
 	var pattern = new RegExp('\d*' + suburb.value + '\d*$');
 	for (var i = 0; i < pcidSuburbs.length; i++) {
 		// if it's in the list don't do anything
 		if (pattern.exec(pcidSuburbs[i])) return false;
 	}

	// if SUBURB not in the list, put it there
 	var newvalue = text.value.replace(
	 /(^|,)([^,]*)$/,
	 "$1 " + suburb.text + ', '
	 );
 	text.value = newvalue;

        // if PCID not in the list, put it there
 	var newvalue2 = text2.value.replace(
	 /(^|,)([^,]*)$/,
	 "$1 " + suburb.value + ', '
	 );
 	text.value = newvalue;
        text2.value = newvalue2;
        text.focus();
 	return false;
 }

 /*
        Purpose: Sync PCID field with suburb field.
 */
 function rfSync () {

        var text    = document.getElementById('suburblist');
        var select = document.getElementById('suburblist2');
        var text2  = document.getElementById('pcidlist');

   // count how many suburbs in both fields, if different then clear the pcid field and re-build
   var textSuburbs = text.value.split(/\s*,\s*/);
      var sleng = textSuburbs.length;
   var pcidSuburbs = text2.value.split(/\d*,\d*/);
      var pleng = pcidSuburbs.length;
      pleng = pleng-1;

   if(sleng!=pleng) {
      // clear the pcids
      text2.value="(";
      //rebuild the pcids
        if (!textSuburbs) return false;
        for (var i = 0; i < textSuburbs.length; i++) {
                //match the suburb and get its matching value in the 'suburblist2' id in the form
                for (var j = 0; j < select.length; j++) {
 			var name1 = textSuburbs[i].toUpperCase().replace(/^\s*/, '').replace(/\s*$/, '');
 			var name2 = select.options[j].text.toUpperCase().replace(/^\s*/, '').replace(/\s*$/, '');
                	if(name1 == name2) {
				text2.value= text2.value + "," + select.options[j].value;
                                //var suburb = select.options[select.selectedIndex];
				//text2.value= text2.value + ", suburb.value";
				break;
                        }
                	//if (pattern.exec(textSuburbs[i])) return false;
		}
        }
	text2.value= text2.value + ")";
   } else {
      text2.value = "(" + text2.value + ")";
   }

 }

 /*
	Purpose: Match suburb on every character typed in i.e. the jump
 */
 function rfBuildIndex () {
 	if (!document.getElementById) return;
 	var select = document.getElementById('suburblist2');
 	for (var i = select.options.length; i--;) {
 		rfIndex[select.options[i].text.toUpperCase().charAt(0)] = i;
 	}
 	rfIndex.populated = true;
 }

 function rfPCIDBuildIndex () {
        if (!document.getElementById) return;
        var select = document.getElementById('pcidlist');
        for (var i = select.options.length; i--;) {
                rfIndex[select.options[i].text.toUpperCase().charAt(0)] = i;
        }
        rfIndex.populated = true;
 }

 //]]>


function remove_XS_whitespace(item)
{
  var tmp = "";
  var item_length = item.value.length;
  var item_length_minus_1 = item.value.length - 1;
  for (index = 0; index < item_length; index++)
  {
    if (item.value.charAt(index) != ' ')
    {
      tmp += item.value.charAt(index);
    }
    else
    {
      if (tmp.length > 0)
      {
        if (item.value.charAt(index+1) != ' ' && index != item_length_minus_1)
        {
          tmp += item.value.charAt(index);
        }
      }
    }
  }
  item.value = tmp;
}
//  End -->

