function focusPage(strField)
{
	eval('document.'+ strField +'.focus()')
}

function validDate(lngYear, lngMonth, lngDay) {
	var aryMonthNormal	= new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	var aryMonthLeap	= new Array(31,29,31,30,31,30,31,31,30,31,30,31);

	var blnIsValid		= true;

	if (lngYear % 4 == 0 && (lngYear % 100 != 0 || lngYear % 400 == 0)) {
		if (lngDay < 1 || lngDay > aryMonthLeap[lngMonth - 1]) { blnIsValid = false; }
	} else {
		if (lngDay < 1 || lngDay > aryMonthNormal[lngMonth - 1]) { blnIsValid = false; }
	}

	if (blnIsValid) {
		var dteNow	= new Date();
		var dteToday	= new Date(dteNow.getYear(), dteNow.getMonth(), dteNow.getDate());
		var dteParam	= new Date(lngYear, lngMonth - 1, lngDay);

		if (dteToday > dteParam) { blnIsValid = false; }
	}

	return blnIsValid;
}

var whitespace = " tnr";

function isWhitespace (s) {
	var i;
		for (i = 0; i < s.length; i++)
		{
				var c = s.charAt(i);
				if (whitespace.indexOf(c) == -1) return false;
		}
		return true;
}

function isEmail (strEmail) {
	var re = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;
	if (!re.test(strEmail)) {
		return true;
	} else {
		return false;
	}
}

function fnReplaceChars(strString, strReplace, strWith) {
	temp = '' + strString;

	while (temp.indexOf(strReplace)>-1) {
		pos = temp.indexOf(strReplace);
		temp = '' + (temp.substring(0, pos) + strWith + temp.substring((pos + strReplace.length), temp.length));
	}
	return temp;
}

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function fnIsDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		return "Please enter a valid date. The date format should be : mm/dd/yyyy"
	}
	if (strMonth.length<1 || month<1 || month>12){
		return "Please enter a valid month in the date"
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return "Please enter a valid day in the date"
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return "Please enter a valid 4 digit year in the date between "+minYear+" and "+maxYear
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return "Please enter a valid date"
	}
	return ""
}

function fnIsValidTime(timeStr, strDescription) {
	// Checks if time is in HH:MM:SS AM/PM format.
	// The seconds and AM/PM are optional.

	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

	var matchArray = timeStr.match(timePat);
	if (matchArray == null) {
		return "Please enter a valid format for the " + strDescription + ".";
	}
	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];
	ampm = matchArray[6];

	if (second=="") { second = null; }
	if (ampm=="") { ampm = null }

	if (hour < 0  || hour > 23) {
		return "The hour in the " + strDescription + " must be between 0 and 23";
	}

	if  (hour > 12 && ampm != null) {
		return "You can't specify AM or PM for military time.";
	}
	if (minute<0 || minute > 59) {
		return "The minute in the " + strDescription + " must be between 0 and 59.";
	}
	if (second != null && (second < 0 || second > 59)) {
		return "The second in the " + strDescription + " must be between 0 and 59.";
	}
return ""
}

function fnChecked(objCheckbox, lngNo) {
	var lngCheckCount = 0;
	for (var i = 0; i < lngNo; i++) {
		if (eval(objCheckbox + i + '.checked')) {
			lngCheckCount++;
		}
	}
	if (lngCheckCount > 0) {
		return true;
	} else {
		return false;
	}
}

function fnHighlightDataLabel(strCell, strClassType) {
	var objCell = document.getElementById('cell' + strCell);
	objCell.className = 'DataLabel' + strClassType;
}

function checkPostCode (toCheck) {

  // Permitted letters depend upon their position in the postcode.
  var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
  var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
  var alpha3 = "[abcdefghjkstuw]";                                // Character 3
  var alpha4 = "[abehmnprvwxy]";                                  // Character 4
  var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5
  

  // Array holds the regular expressions for the valid postcodes
  var pcexp = new Array ();

  // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Expression for postcodes: ANA NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

  // Expression for postcodes: AANA  NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Exception for the special postcode GIR 0AA
  pcexp.push (/^(GIR)(\s*)(0AA)$/i);
  
  // Standard BFPO numbers
  pcexp.push (/^(bfpo)(\s*)([0-9]{1,4})$/i);
  
  // c/o BFPO numbers
  pcexp.push (/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);

  // Load up the string to check
  var postCode = toCheck;

  // Assume we're not going to find a valid postcode
  var valid = false;
  
  // Check the string against the types of post codes
  for ( var i=0; i<pcexp.length; i++) {
    if (pcexp[i].test(postCode)) {
    
      // The post code is valid - split the post code into component parts
      pcexp[i].exec(postCode);
      
      // Copy it back into the original string, converting it to uppercase and
      // inserting a space between the inward and outward codes
      postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
      
      // If it is a BFPO c/o type postcode, tidy up the "c/o" part
      postCode = postCode.replace (/C\/O\s*/,"c/o ");
      
      // Load new postcode back into the form element
      valid = true;
      
      // Remember that we have found that the code is valid and break from loop
      break;
    }
  }
  
  // Return with either the reformatted valid postcode or the original invalid 
  // postcode
  if (valid) {return postCode;} else return false;
}

function PopupScreenCentre(url,name,width,height,scrollbars) {

	var titleBarHeight, windowBorderWidth
	titleBarHeight = 24
	windowBorderWidth = 4

	var screenWidth, screenHeight
	screenWidth = 800
	screenHeight = 600

	if (window.screen) {
		if (window.screen.availWidth) {
			// ok browser has the appropriate properties we need to centre it
			screenWidth = window.screen.availWidth
			screenHeight = window.screen.availHeight
		}
	}

	var windowWidth = windowBorderWidth + width + windowBorderWidth
	var windowHeight = titleBarHeight + height + windowBorderWidth

	var left = (screenWidth - windowWidth) / 2
	var top = (screenHeight - windowHeight) / 2

	var winPopup = window.open(url,name,'left='+left+',top='+top+',screenX='+left+',screenY='+top+',width='+width+',height='+height+',scrollbars='+scrollbars+',resizable=0,toolbar=0,location=0,directories=0,status=1,menubar=0,copyhistory=0')
	winPopup.focus();
}

