//Array extensions
Array.prototype.push = function (){
	for(var i = 0; i < arguments.length; i++){
		this[this.length] = arguments[i];
	}
	return this.length;
}
Array.prototype.splice = function(){
	var deleteLength = (arguments.length>1)?arguments[1]:0;

	var tempArray = new Array;
	for(var i=0; i<this.length; i++){
		tempArray.push(this[i]);
	} //duplicate array
	this.length = arguments[0]; //Truncate array to start of splice
	if (arguments.length>2){
		for(var i=2; i<arguments.length; i++){
			this.push(arguments[i]);
		}
	} //insert values
	for(var i=(arguments[0]+deleteLength); i<tempArray.length; i++){
		this.push(tempArray[i]);
	}//add array elements after end of splice

	var retArray = new Array();
	for(var i=arguments[0]; i<(arguments[0]+deleteLength); i++){
		retArray.push(tempArray[i]);
	}
	return retArray; //return array elements within splice
}


function popUp(oLink){

	/* target modification - added so that internal links will open in the same window, external links will open in a new window */
	if (oLink.target == "link")
	{
		var arSiteURL = document.location.toString().replace("http://","").split("/");
		var arLinkURL = oLink.href.toString().replace("http://","").split("/");
		if (arSiteURL.length >= 1 && arLinkURL.length >=1 && arSiteURL[0] == arLinkURL[0]) {
			oLink.target = "_self";
		}
	}
	/*end target modification*/
	jsPopUp(oLink.href,oLink.target);
	return false;
}
var oWindows = new Object();
function jsPopUp(sURL, sTarget){
	var sParams;
	switch(sTarget){
		case 'photo': 
			sParams = "menubar=no,location=no,scrollbars=yes,resizable=yes,status=no,width=580,height=500";
			break;
		default:
			sParams = "";
			//sParams = "menubar=yes,location=yes,toolbar=yes,scrollbars=yes,resizable=yes,directories=yes,status=yes,width=740,height=580";
			break;
	}
	oWindows[sTarget] = window.open(sURL, sTarget, sParams);
	if (oWindows[sTarget]){
		if (!oWindows[sTarget].closed){
			oWindows[sTarget].focus();
		}
	}
}

function checkOpener(sURL, sTarget){
	//checks that the popup has an opener, and if not (if it has been directly referenced, redirect to the opener with a command to pop itself up.
	if (!window.opener){
		jsPopUp(document.location, sTarget);
		document.location = sURL;
	}
}
function openInOpener(sURL, bClose){
	if (window.opener){
		if (window.opener.closed){
			jsPopUp(sURL, '_blank');
		} else {
			window.opener.location = sURL;
			window.opener.focus();
		}
	} else {
		jsPopUp(sURL, '_blank');
	}
	if (bClose){
		window.close();
	}
	return false;
}

function rollover(oImg, sState){
	var aImg = oImg.src.split('/');
	var aImgLast = aImg[aImg.length-1].split('_');
	aImgLast[0] = sState;
	aImg[aImg.length-1] = aImgLast.join('_');
	oImg.src = aImg.join('/');
}





//************************************************************************
// Required Functions: NONE
// Called As: isSpace(document.frm.fieldname.value)

function isSpace(inChar) 
{
  return (inChar == ' ' | inChar == '\t' || inChar == '\n');
}
//************************************************************************
// trims a form element
// Requires Functions: isSpace()
// Called As: trim(document.frm.fieldname.value)

function trim(tmpStr) 
{
  var atChar;
 
  if (tmpStr.length > 0) atChar = tmpStr.charAt(0);

  while (isSpace(atChar)) 
  {
    tmpStr = tmpStr.substring(1, tmpStr.length);
    atChar = tmpStr.charAt(0);
  }

  if (tmpStr.length > 0) atChar = tmpStr.charAt(tmpStr.length-1);

  while (isSpace(atChar)) 
  {
    tmpStr = tmpStr.substring(0,( tmpStr.length-1));
    atChar = tmpStr.charAt(tmpStr.length-1);
  }

  return tmpStr;
}
//************************************************************************
//checks if a string is empty
// Required Functions: trim()
// Called As: isEmpty(document.frm.fieldname)

function isEmpty(inStr)
{
	if(trim(inStr.value).length == 0)
		return true;
	else
		return false;
}
//************************************************************************
//checks if a textbox is empty - replaced by checkTextBox
// Required Functions: trim()
// Called As: isEmptyText(document.frm.fieldname)

function isEmptyText(field)
{	
	var strS = trim(field.value);
	if (strS.length == 0 || strS == "")	return true;
	return false;
} 

//************************************************************************
// checks if at least one of a set of radios is selected 
// Required Functions: NONE
// Called As: isEmptyRadio(document.frm.fieldname)
function isEmptyRadio(field)
{
	if (field.length)
	{
	 	for (i=0; i < field.length; i++)
		{
			if (field[i].checked)
				return false;
		}
	}
	else 
		if (field.checked)
			return false;

	return true;
}

//************************************************************************
// checks if a valid option has been selected 
// Required Functions: NONE
// Hint: document.frm.fieldname.options[document.frm.fieldname.selectedIndex].value
// Called As: isEmptySelect(document.frm.fieldname)
function isEmptySelect(field)
{
	var strS = "";
	iIndex = field.selectedIndex;
	if (iIndex == -1) 
	{
		field.selectedIndex = -1;
		return true;
	}
	if (iIndex != null)
	{
		strS = field[iIndex].value;
	}	
	if (trim(strS) == "") return true;
	
	return false;
}

//************************************************************************
//checks if the checkbox is selected or at least one of the array is selected
// Required Functions: NONE
// Called As: isEmptyCheck(document.frm.fieldname)
function isEmptyCheck(field)
{
	if (field.length)
	{
		for (i=0; i < field.length; i++)
		{
			if (field[i].checked)
				return false;
		}
	} 
	else 
	{
		if (field.checked)
			return false;
	}
	return true;
}

//***************************************************************************
// Determines if its a valid integer
// Required Functions: NONE
// Called As: isEmptyCheck(document.frm.fieldname.value)
function isValidInteger(string) {

    var Chars = "0123456789";
 
    for (var i = 0; i < string.length; i++) 
	{
       if (Chars.indexOf(string.charAt(i)) == -1)
	    return false;
    }
    return true;
}

//***************************************************************************
// Determines if its a valid Postcode
// Required Functions: isValidInteger()
// Called As: isValidPostcode(document.frm.fieldname.value)
function isValidPostcode(string)
{
	if (!isValidInteger(string))
		return false;
	if (parseInt(string.length,10) != 4)
		return false;
	
	return true;
}
//***************************************************************************
// validate numbers
function isValidNumber(string) {

    var Chars = "0123456789.";
 
    for (var i = 0; i < string.length; i++) 
	{
       if (Chars.indexOf(string.charAt(i)) == -1)
	    return false;
    }
    return true;
}
//***************************************************************************
// validate phone numbers
function isValidPhoneNumber(string) {

    var Chars = "0123456789(). ";
 
    for (var i = 0; i < string.length; i++) 
	{
       if (Chars.indexOf(string.charAt(i)) == -1)
	    return false;
    }
    return true;
}
//************************************************************************
// Determines if its a valid Email Address
// Required Functions: NONE
// Called As: isValidPostcode(document.frm.fieldname.value)
function isValidEmail(str)
{
	str += '';
	namestr= '';
	domainstr = '';
	namestr = str.substring(0,str.indexOf("@")); // get all the info before the "@"
	domainstr = str.substring(str.indexOf("@")+1,str.length); // get all the info after the "@"
	tailstr = str.substring(str.indexOf(".")+1,str.length) // after the first "." how many chars are they

	if( (namestr.length == 0) || (domainstr.indexOf(".") <= 0) || (domainstr.length == 0) || (domainstr.indexOf("@") != -1) || tailstr.length == 0) return false;
	
	return true;
}
//************************************************************************
// open a url in a new window for preview
// remember you will need to put this form in the page yo call this JS function from
//<form name="locator" action="location.cfm" method="post" target="locatorWin">
//	<input type="hidden" name="url_string" value="">
//</form>
// and you will need the location.cfm page also to display the page
// Required Functions: as stated above
// Called As: openWinLink(document.frm.fieldname.value)
function openWinLink(urlLink)
{

	tempFormValue = "document.frm."+urlLink+".value";
	tempFormValue = eval(tempFormValue);
    firstChar = tempFormValue.charAt(0);

	if(firstChar =="/")
	{
		// testing a local link
		sendUrl = "http://"+location.hostname+tempFormValue;
		document.locator.url_string.value= sendUrl;
		document.locator.submit();
		return true;	
	}
	else
	{
		// testing a http link
		var VarHTTP = document.frm[urlLink].value;
		if (document.frm[urlLink].value=="")
		{
			alert('You must enter a valid URL to show');
			return false;
		}
		else
		{
			sStr = VarHTTP.substring(0,7);
			sStr2 = VarHTTP.substring(0,8);
			if (sStr.toLowerCase() != "http://" && sStr2.toLowerCase() != "https://")
			{
				alert('You must enter "http://" before: '+VarHTTP);	
				return false;
			}
		}	
		document.locator.url_string.value=document.frm[urlLink].value;
		document.locator.submit();
		return true;
	}
	return false;
}

//************************************************************************
// Required Functions: NONE
// Called As: y2k(document.frm.fieldname.value)
function y2k(number) { return (number < 1000) ? number + 1900 : number; }

//************************************************************************
// checks if date passed is a valid dd/mm/yyyy format
//returns 
	//-1 if a missing date, 
	//-2 if invalid separators, 
	//0 if invalid date, 
	//1 for valid date
// Required Functions: y2k() and trim()
// Called As: isValidDate(document.frm.fieldname)
		
function isValidDate(field) 
{

	//set default separator
	var myDate = trim(field.value);

	//check if exists
	if (myDate.length == 0) return 0;
		
	//check existence of correct separator	
	var sep = "/";
	var index1 = myDate.indexOf("/");
	
	if (index1 == -1) 
	{
		index1 = myTime.indexOf("-");
		if (index1 != -1) sep = "-"
	}	
	
	if (index1 == -1) return 0; //invalid separator
		
 	var dateArrayFrom = myDate.split(sep);
	var date = dateArrayFrom[0];
	var month = dateArrayFrom[1];
	var year = dateArrayFrom[2];
	 
    var myDateObj = new Date(year,month-1,date);
 	if (year == y2k(myDateObj.getYear()) && (month-1 == myDateObj.getMonth()) && (date == myDateObj.getDate())) 
       return 1;
    else
		return 0; //reason = 'Valid format but an invalid date.';
       
}
//************************************************************************
// checks if time passed is a valid format
//returns
	//	-1 if missing time, 
	//	-2 for invalid separators, 
	//	0 if invalid time, 
	//	1 if correct time
// Required Functions: trim()
// Called As: isVaildTime(document.frm.fieldname)

function isValidTime(field)
{
	var myTime = trim(field.value);
	
	//check if exists
	if (myTime.length == 0) return 0;
	
	//check existence of correct separator
	var sep = ":";
	var index1 = myTime.indexOf(":");
	
	if (index1 == -1) 
	{
		index1 = myTime.indexOf(".");
		if (index1 != -1) sep = "."
	}	
	
	if (index1 == -1) return 0; //invalid separator
	
   	var timeArrayFrom = myTime.split(sep);
	hours = timeArrayFrom[0]
	mins = timeArrayFrom[1]
	totalHM = parseInt(hours)+parseInt(mins);
	
	 if(!trim(myTime))//empty
 		return 0;
	 if(parseInt(totalHM) > 1259)
 		return 0;
	 if(parseInt(hours) > 12)// 12 hours time base
	 	return 0;
	 if(parseInt(mins) > 59)// 60 min time base
 		return 0;
	 if( isNaN(hours) || isNaN(mins) )// min or hour value is not a number
 		return 0;
	 if( (hours.length == 0) || (hours.length > 2) ) //hours can have only two digit at the max
 		return 0;
	 if( (mins.length == 0) || (mins.length > 2) )// mins can have two digit at the max
 		return 0;
	
	// ** not required cckdate.cfm converts .'s to :'s anyway
	//field.value = myTime.replace(sep,":"); //first convert any other separators to : format - 
		
	// passed all the test hence it is a valid time
	return 1;
	
}

//************************************************************************
//used for date addition and subtraction
function makeDateInMS(tDate, sep)
{
	if (!sep) sep = "/";
	var dateArrayFrom = tDate.split(sep);
	var date = dateArrayFrom[0];
	var month = dateArrayFrom[1];
	var year = dateArrayFrom[2];
 	//alert(tDate);
    var rDate = new Date(year,month-1,date);
	//alert(rDate);
	return rDate.getTime();
}	
//************************************************************************
// used to calculate days between start date and end date - including startdate in count
function getTotalDays(startdate, enddate)
{
	if (!isValidDate(startdate) || !isValidDate(enddate))
		return 0;
	
	var sDate = makeDateInMS(startdate);
	var eDate = makeDateInMS(enddate);
	
	if (sDate >= eDate)
		return -1;
		
	var TotalTime = eDate - sDate;
	var TotalDays = parseInt((TotalTime / (60*60*24*1000)) + 1);
	return TotalDays;
}
//************************************************************************
//returns the value currently selected in an array of radio buttons
function getSelectedRadioValue(field)
{
	if (field.length)
	{
		for (i=0; i < field.length; i++)
		{
			if (field[i].checked)
				return trim(field[i].value);
		}
	} else {
		if (field.checked)
			return trim(field.value);
	}
	return "";
}

//***************************************************************************
//use check boxes as radio boxes
function checkChoice(field, i) 
{
	if (field[i].checked == true) 
	{
		for (j = 0; j < field.length; j++)
			{
				if (j != i)
				{
					field[j].checked = false;
				}
    		}	
	}
}
//call it this way
//loop through and create a number of checkboxes... "#FieldValue#_small" is the unquie names #jcount# is the position in the loop
//<input type="Checkbox" name="#FieldValue#_small" value="#i#" onclick="checkChoice(document.frm.#FieldValue#_small,#jcount#)">

//***************************************************************************
// used in related artilce sections
// when user select the drop down box, its appropriate radio button is checked.
function SelectToRadioFocuss(formObject)
{
// formObject.checked = true;
 formObject.click();
}
//***************************************************************************
// add http infront of url
// 
function addhttp(Formvalue)
{
	// check to see if the http:// exists in the string else add it
	rExp = /http:/gi;
	validstring = document.frm[Formvalue].value.search(rExp);

	rExp2 = /https:/gi;
	validstring2 = document.frm[Formvalue].value.search(rExp2);

	if (validstring == -1 && validstring2 == -1)
		document.frm[Formvalue].value = "http://"+document.frm[Formvalue].value;
}

//************************************************************************
function setHiddenFromSelect(FieldToGet, FieldToSet, listSep)
{
	if (!listSep) listSep = ",";
	var length = FieldToGet.length;
	var strS = "";
	for (i=0; i < length; i++)
	{
		if (trim(FieldToGet.options[i].value) != "")
		{
			if (strS == "")
				strS = trim(FieldToGet.options[i].value);
			else 
				strS += listSep + trim(FieldToGet.options[i].value);
		}
	}
	FieldToSet.value = strS;
}

//***************************************************************************
function validateListSeparators(string) {

    var Chars = "~^";
 
    for (var i = 0; i < string.length; i++) 
	{
       if (Chars.indexOf(string.charAt(i)) != -1)
	    return true;
    }
    return false;
}
//***************************************************************************
// this function is used to convert 'dd/mm/yyyy' string to a date object that can be compared
function convertDate(str) {
	var sep = "/";
 	var dateArrayFrom = str.split(sep);
	var date = dateArrayFrom[0];
	var month = dateArrayFrom[1];
	var year = dateArrayFrom[2];
			
	tDate = new Date(year, month-1, date);
	return tDate;
}
//***************************************************************************
// used to compare 2 date variables, needs convertDate() function
// takes in 2 date strings, 
// returns 
//		1 if date1 > date2, 
//		0 if date1 == date2, 
//		-1 if date1 < date2, 
function CompareDate(date1, date2)
{

	tdate1 = convertDate(date1); // check to make sure pdate is after cdate
	tdate2 = convertDate(date2);
	
	if (tdate1 > tdate2)
		return 1;

	if (tdate1 == tdate2)
		return 0;
			
	if (tdate1 < tdate2)
		return -1;
}

//***************************************************************************
// help to create releated drop downs
function toggleSelectBox(selectObj, itemArray, MesgChild, MesgNoChild) 
{
var i, j;
var showMesg;
	// empty existing items
	for (i = selectObj.options.length; i >= 0; i--) 
	{
		selectObj.options[i] = null; 
	}
	showMesg = (itemArray != null) ? MesgChild : MesgNoChild;
	if (showMesg == null) 
	{
		j = 0;
	}
	else 
	{
		selectObj.options[0] = new Option(showMesg,'',0,0);
		j = 1;
	}
	if (itemArray != null) 
	{
		// add new items
		for (i = 0; i < itemArray.length; i++) 
		{
			if (itemArray[i][1] != null) 
			{
				selectObj.options[j] = new Option(itemArray[i][0],itemArray[i][1],false,false); //(text, Value, bha, bha)
			}
		j++;
		}
		// select first item (prompt) for sub list
		selectObj.options[0].selected = true;
   }
}
//***************************************************************************


/* START JS Browser detect code */
var bow="n";
var bow1="n";

bName = navigator.appName;
bVer = parseInt(navigator.appVersion);

if (bName == "Netscape" && bVer >= 3)
{
	bow = "ok";
	bow1 = "ok";
}
else if (bName == "Microsoft Internet Explorer" && bVer > 3)
{
	bow = "ok";
	bow1="ok";
}
else if (bName == "Microsoft Internet Explorer" && bVer >=2)
{
	bow = "ok";
}

var IS_AOL=0;

if (navigator.userAgent)
{
	if (navigator.userAgent.indexOf("AOL") >=0)
	{
		IS_AOL=1;
		bow1="n";
	}
}
else
{
	bow="n"
	bow1="n";
}
/* END JS Browser detect code */

function openWindow(f, t, m, l, s, r, w, h, st, tool, d)
{
	var fn_filename = "";
	var fn_target = "_blank";
	var fn_menubar = "yes";
	var fn_location = "yes";
	var fn_scrollbars = "yes";
	var fn_resizable = "yes";
	var fn_width = "600";
	var fn_height = "400";
	var fn_status = "yes";
	var fn_toolbar = "yes";
	var fn_directories = "yes";
	
	if (arguments.length > 0)
	{
		if (f != "" || f != null) fn_filename = f;
		if (t != "" || t != null) fn_target = t;
		if (m == "no") fn_menubar = m;
		if (l == "no") fn_location = l;
		if (s == "no") fn_scrollbars = s;
		if (r == "no") fn_resizable = r;
		if (w > 0) fn_width = w;
		if (h > 0) fn_height = h;
		if (st == "no") fn_status = st;
		if (tool == "no") fn_toolbar = tool;
		if (d == "no") fn_directories = d;
	}
	
	self.name="mainWin";
	
    if (bow!="ok") return (true);
	
	argumentString = 'menubar='+fn_menubar+',location='+fn_location+',scrollbars='+fn_scrollbars+',resizable='+fn_resizable+',width='+fn_width+',height='+fn_height+',status='+fn_status+',toolbar='+fn_toolbar+',directories='+fn_directories;
	
	remote =  window.open(fn_filename,fn_target,argumentString);

    if (remote == null) return true;

    if(bow1 == "ok"){
		window.setTimeout('focusWin()',1000);
	}
	return (false);
}

function focusWin(){
	if(!remote.closed){
		remote.focus();
	}
}
//***************************************************************************
// list functions
function listFindNoCase(listString, searchString, delimeter)
{
	var searchList = '';
	var searchStr = '';
	var del = ',';
	var	arSearchItem = '';
	if (listString != "" || listString != null) searchList = listString;
	if (searchString != "" || searchString != null) searchStr = searchString;	
	if (delimeter != "" || delimeter != null) del = delimeter;	
	
	arSearchItem = searchList.split(del)
	for(i=0; i<arSearchItem.length; i++){
		if(searchStr.toUpperCase() == arSearchItem[i].toUpperCase())
			return true;
	}
	return false;
}
//***************************************************************************




