//************************************************************************
// 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);
			if (sStr.toLowerCase() != "http://")
			{
				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
	if (!sep) sep = "/";
	var myDate = trim(field.value);

	//check if exists
	if (myDate.length == 0) return -1;
		
	//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 -2; //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 isVaildTime(field)
{
	var myTime = trim(field.value);
	
	//check if exists
	if (myTime.length == 0) return -1;
	
	//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 -2; //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);

	if (validstring == -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;
}
//***************************************************************************
