function isValidChar(refVar, strValidChars) {
 // C. Crawford 21/07/2004
 // Allow only certain characters in a string
 // example usage:	isValidChar('132 \'-.', 'abcdefghijklmnopqrstuvwxyz1234567890 \'-.')"	
 // alert("In isValidChar");
 var validChars = strValidChars ;
 var strRefVar = '' + refVar
 for (var i = 0; i < validChars.length; i++) {
   if (validChars.indexOf(strRefVar.substring(i, i + 1).toLowerCase()) == -1) {
   // alert("Invalid Character " + strRefVar.substring(i, i + 1));
   return false;
  }
 }
 // alert("Passed");
 return true;
} 

function highlightField(field) {
field.focus();
field.select();
}

function hasNumbers(str) {
var numError = false;
//Check string for numbers
for(var i=0;i<str.length;i++) {
	if(!isNaN(str.charAt(i))) numError=true;
	}
return numError;
}

function isNumeric(str) {
var numError = true;
//Check string for numbers
for(var i=0;i<str.length;i++) {
	if(isNaN(str.charAt(i))) numError=false;
	}
return numError;
}

function isAlpha(str) {
var alphaError = true;
//Check string for numbers
for(var i=0;i<str.length;i++) {
	if(str.charAt(i) < "A" || str.charAt(i) > "z") alphaError=false;
	}
return alphaError;
}

function isAlphaAndNumeric(str) {
var alphaNumError = true;
for(var i=0;i<str.length;i++) {
		if(!hasNumbers(str.charAt(i)) && !isAlpha(str.charAt(i))) {
		alphaNumError=false;
		}
	}
return alphaNumError;
}

function trimSpaces(str) {
var tempStr="";
for(var i=0;i<str.length;i++) {
	if(str.charAt(i)!=" ") tempStr+=str.charAt(i);
	}
return tempStr;
}

function trimOutsideSpaces(str) {
var tempStr="";
var foundLeadingSpc=false;
var foundTrailingSpc=false;
var trailIndex=0;
//find last index of first trailing space	
for(var i=str.length-1;i>=0;i--) {
		if(str.charAt(i)!=" " && !foundTrailingSpc) {
		foundTrailingSpc=true;
		trailIndex=(i+1);
		}
	}
//remove leading space(s) and stop at trailIndex value
for(var i=0;i<trailIndex;i++) {
	if(str.charAt(i)!=" ") foundLeadingSpc=true;
	if(str.charAt(i)!=" " && !foundLeadingSpc) tempStr+=str.charAt(i);
	if(foundLeadingSpc) tempStr+=str.charAt(i);
	}
return tempStr;
}

//usage: <string>,<valid char1>,<valid char2>,<valid char3>,etc.(any num of params)
//e.g. isValidSequence(" 0123456789","0","1","2","3","4","5","6","7","8","9"," ")
function isValidSequence(refStr) {
for (var i=0; i<refStr.length; i++) {
	if (!arrayContainsElement(isValidSequence.arguments,isValidSequence.arguments[0].charAt(i))) return false;
	}
return true;
}

function arrayContainsElement(array, elem) {
for (var i=0; i<array.length; i++) {
	if(array[i] == elem) return true;
	}
return false;
}

function isValidSequenceAndAlpha(refStr) {
for (var i=0; i<refStr.length; i++) {
	if (!arrayContainsElement(isValidSequenceAndAlpha.arguments,isValidSequenceAndAlpha.arguments[0].charAt(i)) && !isAlpha(isValidSequenceAndAlpha.arguments[0].charAt(i))) return false;
	}
return true;
}

function spawn(URL,winName,features) {
//features example - note you must pass in width & height first (IE 4+ Mac bug) width=620,height=460,directories=no,location=no,menubar=no,scrollbars=yes,toolbar=no,status=no,resizable=no,top=0,left=0
var newWindow=window.open(URL,winName,features);
}

//
// JS to remove '/' char from the search string
// + upper case
// + mid (colapse multiple spaces to 1), left and right trim (whitespace)
//
function submitSearch(formRef) {
	var searchText = formRef.searchTerms.value;
	var rExp = /\//gi;
	var newSearchText = searchText.replace(rExp, "");
	newSearchText = newSearchText.toUpperCase();
	newSearchText = trim(newSearchText," ");
	formRef.searchTerms.value = newSearchText;
	return true;
}

/**
* Javascript trim, ltrim, rtrim
* http://www.webtoolkit.info/
**/
function trim(str, chars) {
    var s= ltrim(rtrim(str, chars), chars);
    return s.replace(new RegExp("[\\s]+", "g"), " ");
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

// Allow FireFox to add bookmark to sidebar as well
// as IE 'AddFavorite'
function addBookmark(title, url) {
	if (window.sidebar) {
		window.sidebar.addPanel(title, url,"");
	} else if( document.all ) {
		window.external.AddFavorite( url, title);
	} else if( window.opera && window.print ) {
		return true;
	} 
} 

/* Argosie show and hide for registration form postcode field */

// Setup namespace
if(!argos) var argos = {};

// begin: argos.utils
if (!argos.utils) argos.utils = {};
argos.utils.postcode = {

	init : function() {
		// hide postcode by default, only if United Kingdom isnt already selected
		if ($('#country').length == 0) return;
		if ($('#country')[0].value == "Ireland") {
			$('#pCodeWrapper').hide();
		}
		argos.utils.postcode.dropdownEvent();
	
	}, // end init()
	
	dropdownEvent : function() {
		
		$('#country').change(
			function() {
				// onchange, check to see if Ireland is not selected
				argos.utils.postcode.loopOptions();
			}
		);
	
	}, // end dropdownEvent()
	
	loopOptions : function() {
	
		$("#country option").each(function(i){
				// check if value is not Ireland 
				if ((this.selected == true) && (this.value != "Ireland"))  {
					// if not Ireland, show postcode
					$('#pCodeWrapper').show();
					return false; // exit loop
				} else {
					// hide postcode for Ireland
					$('#pCodeWrapper').hide();
					// clear input field at the same time as hiding
					$('#pCodeWrapper input')[0].value = "";
					
				}
			}
		);
	} //end loopOptions
		
}


$(document).ready(function() {
	argos.utils.postcode.init();
});

