//Locations Search
var whitespace = " \t\n\r";
var mPrefix = "Please enter a ";
var mSuffix = " in the provided field.";
var numPrefix = "You did not enter a valid number into the ";
var numSuffix = " field. Please enter it now.";
var textPrefix = " field can only contain alphabetic characters. Please enter it now.";
var textNumPrefix = " field can only contain alphanumeric, -, /, . & and # characters. Please enter it now.";
var oForm;

//tesxt describing the field names
var sSuburb = "Town/Suburb "
var sState = "state";


function checkLocationsForm(form) {
	return (	checkLocationsSuburb (oForm['location.address.suburb'].value,sSuburb,false) &&
				checkLocationsState (oForm['location.address.state'].value)
			)
	}		

function checkLocationsState (state) {
	if (state == "") {
		  alert ("Please select a State from the drop-down list");
		  return false;
	} else {
	return true;
	}
}

function checkLocationsSuburb (theField, s, emptyOK) {
	// Will only allow letters, - , and only one single quote (')
   var re = /^[a-zA-Z .-]+'{0,1}[a-zA-Z .-]+$/; 
   if (checkLocationsSuburb.arguments.length == 2) emptyOK = false;
   if ((emptyOK == true) && (isEmpty(oForm['location.address.suburb'].value))) return true;
   if (isWhitespace(oForm['location.address.suburb'].value)) return warnLocationsEmpty (theField, s);
   if (re.test(theField) == false) {
         warnInvalidSuburb (theField, s);
         return false;
      }
	else return true;
}

function warnLocationsEmpty (theField, s) {
	oForm['location.address.suburb'].focus();
   alert(mPrefix + s + mSuffix);
	return false;
}

// Check whether string s is empty.

function isEmpty(s) {
	return ((s == null) || (s.length == 0))
}



// Returns true if string s is empty or whitespace characters only.

function isWhitespace (s) {
	var i;
	// Is s empty?
	if (isEmpty(s)) return true;
   // Search through string's characters one by one until we find a
	// non-whitespace character. When we do, return false; if we don't, return true.
    for (i = 0; i < s.length; i++) {
		// Check that current character isn't whitespace.
		var c = s.charAt(i);
      if (whitespace.indexOf(c) == -1) return false;
	 }
    // All characters are whitespace.
    return true;
}

function warnInvalidSuburb (theField, s) {
	oForm['location.address.suburb'].focus();
   alert("The Suburb field can only contain letters.");
   return false;
}

//Submits the form after all validation
function submit_locations_form(form) {
	oForm = form;
	if (checkLocationsForm(oForm)) {
		javascript:globalWindowOpen('','nablocations',800,600,1,0,0,1,1,0,0,1,1);
		form.submit();
		dcsMultiTrack('DCS.dcsuri','Locations Tool','DCS.dcssip','nab.com.au','WT.ti','Locations Tool');
		return true;
	} else {
		return false;
	}
}