document._domino_target = "_blank";
function _doClick(v, o, t) {
  var returnValue = false;
  var url="/usc/WWW_USC.nsf/vw-dynamic-index/80256d0300503a9780256b650051e114?OpenDocument&Click=" + v;
  if (o.href != null) {
    o.href = url;
    returnValue = true;
  } else {
    if (t == null)
      t = document._domino_target;
    window.open(url, t);
  }
  return returnValue;
}

function Trim(s) 
		{
		  // Remove leading spaces and carriage returns
		  
		  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
		  {
		    s = s.substring(1,s.length);
		  }

		  // Remove trailing spaces and carriage returns

		  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
		  {
		    s = s.substring(0,s.length-1);
		  }
		  return s;
		}

		function validatePostCode() {
			var postcode=document.getElementById("postcode").value;	
			postcode=Trim(postcode);		
			var pos=postcode.indexOf(" ");
			if (pos==-1) {
				alert("Please supply a valid postcode format and include the SPACE");
				return false;
			}
			var suffix=postcode.substring(pos+1, postcode.length);
			var prefix=postcode.substring(0, pos);
			prefix=Trim(prefix);
			// Format is LD DLL, LLD DLL, LDD DLL, LLDD DLL
			if (postcode.length > 8) {
				alert("Postcode length should not exceed 8 alphanumerics.");				
				return false;
			}
			
			
			if (suffix.length==3 || suffix.length==4)  {
				if (prefix.length>=2 && prefix.length<=4)  {
					return true;
				} else {
					alert(prefix.length+" of "+postcode);
					alert("The Postcode prefix should be alphanumeric between two and four in length");
					return;
				}
				
			} else {
				alert("Postcodes should have a suffix with either three or four alpha numerics (Length: "+suffix.length+")");
				return false;
			}
			
		}
		
function postCodeSearch() {
	
	var pc=document.getElementById("postcode");
	if (pc==null) {
		pc=document.all.postcode;
	}
	if (pc!=null) {
		pc.value=pc.value.toUpperCase();
		if ( validatePostCode())  {
			var loc=document.location.href.toLowerCase();
			var pos=loc.indexOf(".nsf")+4;
			document.location.href=loc.substring(0,pos)+"/fm-postcode-search?openform&postcode="+pc.value;
		}
	}
	
}

function zipSearch() {
	var zc=document.getElementById("zipcode");
	if (zc!=null) {
		if (zc!="") {
			zc=document.all.zipcode;
			var loc=document.location.href.toLowerCase();
			var pos=loc.indexOf(".nsf")+4;
			document.location.href=loc.substring(0,pos)+"/fm-WebPostCode?openform&zipcode="+zc.value;
		} else {
			alert("Please make sure you add a valid zip code first !");
		}

	}
}

function postCodeZipSearch(code, typ) {
	// Use the search on salvationarmyusa.org
	window.location = "http://www.salvationarmyusa.org/ndos/app/zipcode.jsf?zip="+code;return;
	/**
		typ = 0 for postcode, 1 for zip
	*/
	var loc=document.location.href;
	var dbName="usc/WWW_USC.nsf";
	var user="Anonymous";
	var append='';
	var pos=loc.indexOf(".nsf")+4;
	if (loc.indexOf("-txt") !=-1) {
			append='txt';
	}
	if (loc.indexOf("-stc") !=-1) {
			append='src';		
	}
	var prefix='';
	if (loc.indexOf(".nsf")!=-1) {
		prefix=loc.substring(0,pos);
	} else {
		prefix="/"+dbName;
		window.status=prefix;
	}
	
	switch (typ) {
		case 0:
			// Lets remove the spaces from this postcode
			code = code.replace(" ", "");
			// and lets convert it to uppercase
			code = code.toUpperCase();
			if (validatePostcode(code)==true) {
				document.location.href=prefix+"/fm-postcode-search"+append+"?openform&postcode="+code;
			} else {
				alert("Please enter a valid postal code");
			}
			break;
		case 1:
			if(code==""){
				alert("Please enter your ZIP code or location");
			}else{
				window.location.href=prefix+"/fm-ndos?OpenForm&go=1&zip="+code;
			}
			break;
	}
	return;
}	
function validatePostcode(postcode) {
	// Lets ensure this postcode is BS 7666 valid (minus the spaces)
	var regex= new RegExp("(GIR 0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW])[0-9][ABD-HJLNP-UW-Z]{2})");	
			
	if (regex.test(postcode)){
	     return true;
	}else{
		return false;
	}
}
