function TickBoxClick (FormName,TickBox,Hidden)
{
	with (FormName)
	{
		if (TickBox.checked)
		{
			Hidden.value = 1;
		}
	
		else
		{
			Hidden.value = 0;
		}
	}
}
		
function RefreshMenuPage(strFormLocation, strProcess)
{		
	if (strFormLocation != null)
	{
		if (strFormLocation.closed)
		{
			top.window.close();
		}
			
		else
		{
			strFormLocation.focus();
			if (strFormLocation.document.FilterForm != null) {
			    strFormLocation.document.FilterForm.SelectList.value = "";
				strFormLocation.document.FilterForm.Process.value = strProcess;
				strFormLocation.document.FilterForm.action = strFormLocation.document.FilterForm.action + "?" + strFormLocation.document.FilterForm.QueryString.value;
				strFormLocation.document.FilterForm.submit();
			}
			top.window.close();
		}
	}
	else
	{
		top.window.close();
	}	
}

function PopulateHidden (FormName,Hidden,Value)
{
	with (FormName)
	{
		Hidden.value = Value;
	}
}

//This is used by the draw drop down list function to populate the hidden value when there is an "Other" option
//and the user types text
function PopulateHiddenDropDown(objDropDown, objOther, objSave) {
    if (objOther.value.length > 0)
        objDropDown.selectedIndex = objDropDown.length - 1

    if (objOther.value.length > 0)
        objSave.value = objOther.value
    else
        objSave.value = objDropDown.value
}

//This function is used for setting default values in a form. I.e. it may be used to 
//put the current date and time in.
function InitialiseField(objField,objType,ArrayArgs)
{	
	var tmpString; 
	
	switch (objType.toUpperCase())
	{	
		case 'DATE':	
					
			if (objField.value.length == 0 || !chkdate(objField.value))
			{
				tmpString = new Date();
				objField.value = tmpString.getDate() + "/" + (tmpString.getMonth() + 1) + "/" + tmpString.getFullYear();
			}											
			break;
		
		case 'TIME':
			if (objField.value.length == 0 || !chkTime(objField.value))
			{
				tmpString = new Date();
				if (tmpString.getMinutes() < 10)
					objField.value = tmpString.getHours() + ":0" + tmpString.getMinutes();
				else
					objField.value = tmpString.getHours() + ":" + tmpString.getMinutes();
			}	
			break;
	}
}

//this function loops through the form (FormName) and finds all checkboxes that start
//with strChkStart. It then either ticks or Unticks them depending on what strProcess
//was.
function SelectAllClick(FormName, strChkStart, strProcess)
{	
	
	for (var i = 0; i<FormName.elements.length; i++)
	{		
		if (FormName.elements[i].type == "checkbox")
		{
			if (FormName.elements[i].name.slice(0,strChkStart.length) == strChkStart && !FormName.elements[i].disabled)
			{
				if (strProcess == "SelectAll")
				{
					//if (!FormName.elements[i].disabled)	
						FormName.elements[i].checked=true;
				}	
				else
				{
					FormName.elements[i].checked=false;
			    }
			}
		}
	}	
}

//function used to initialise check boxes on a page, i.e. if the page maintains a list of
//values (objArray) that should be checked, this function checks all checkboxes in FormName to see
//if the part after the strChkStart e.g. ("chk") - chk1, chk2 ... chkn is stored in the array,
//if it is, it will set it to checked
function InitialiseTickBoxes(FormName, objArray, strChkStart)
{	
	var SearchResult;
	
	var tmpArray = new SingleDynamicArray();
	tmpArray.StoreArray(objArray);
	
	for (var i = 0; i<FormName.elements.length; i++)
	{		
		if (FormName.elements[i].name != null)
		{
			if (FormName.elements[i].name.slice(0,strChkStart.length) == strChkStart)
			{
				SearchResult = tmpArray.SearchArray(FormName.elements[i].name.slice(strChkStart.length),1,"string");
				if (SearchResult[0] != -1)
				{
				    FormName.elements[i].checked = true;
				    if(document.getElementById(FormName.elements[i].id.replace(strChkStart, 'tr'))!=null)
				        AdminHighlightRow1(document.getElementById(FormName.elements[i].id.replace(strChkStart, 'tr')), MenuTableColorArray);
				    
				}
			}
		}
	}	
}

var LastCheckBoxClickedIndex = null;

//GlobalEventObject  is used in menu pages .Its used to shift select checkboxes.
var GlobalEventObject = null;
function GetEventObject(e) {
    if (!e) var e = window.event;
    GlobalEventObject = e;
} 
function SelectBoxClick(chkBox,SelArray,FormSelectList) {
    
	var tmpchkObj = null;
	var lStart, lEnd;
	var target = '';
	var tagName = ''
	var RowChkBox = chkBox;
	
	//Get the tagName of the object on which user clicked. TR,TD, INPUT etc
	if (GlobalEventObject != null) {
	    target = GlobalEventObject.target || GlobalEventObject.srcElement;
	    tagName = target.tagName;
	}
	tagName = tagName.toUpperCase();
	//If tagName is not hyperlink or IMG in which case we do not want to select the row
	//then select the checkbox (if not INPUT) and highlight row
	if (tagName != 'A' && tagName != 'IMG') {
        
        //Get the checkbox object for current row 
	    if (chkBox.tagName != 'INPUT')
	        RowChkBox = document.getElementById(chkBox.id.replace('tr', 'chk'));
        
        // if user clicked on any element other than CheckBOX then tick/untick the checkbox
	    if (chkBox.tagName != 'INPUT') {
	        if (RowChkBox.disabled == true)
	            return;
	        RowChkBox.checked = !RowChkBox.checked;
	    }
	    
	    //Get the current index of the RowID in the IDs Array
	    var currentIndex = PageRecordIDs.SearchArray(RowChkBox.name.slice(3), 1, "string")[0];
        
        //if this is first click then store then currentIndex into LastCheckBoxClicked
	    if (LastCheckBoxClickedIndex == null)
	        LastCheckBoxClickedIndex = currentIndex;

	    //Check if user pressed clicked along with selecting row then find start and end index and loop through all records 
        // and tick/untick	        
	    if (GlobalEventObject != null && GlobalEventObject.shiftKey && LastCheckBoxClickedIndex != currentIndex) {
            
            //Set lStart and LEnd as user can select from top to bottom or bottom to top
	        if (parseInt(LastCheckBoxClickedIndex) < parseInt(currentIndex)) {
	            lStart = LastCheckBoxClickedIndex;
	            lEnd = currentIndex;
	        }
	        else {
	            lStart = currentIndex;
	            lEnd = LastCheckBoxClickedIndex;
	        }

            //loop through RecordIds and select rows
	        for (var i = lStart; i <= lEnd; i++) {
	            tmpchkObj = document.getElementById('chk' + PageRecordIDs.myArray[i]);

	            if (tmpchkObj != null) {
	                tmpchkObj.checked = RowChkBox.checked;
	                UpdateSelectList(tmpchkObj, SelArray, FormSelectList);
	                if (document.getElementById(tmpchkObj.id.replace('chk', 'tr'))!=null)
	                {
                        if(tmpchkObj.checked)
                            AdminHighlightRow1(document.getElementById(tmpchkObj.id.replace('chk', 'tr')), MenuTableColorArray);
	                    else
	                        AdminHighlightRow2(document.getElementById(tmpchkObj.id.replace('chk', 'tr')), MenuTableColorArray);
                    }	                    
	            }
	        }

	    }
	    else
	        UpdateSelectList(RowChkBox, SelArray, FormSelectList);

        //Store the last index
	    LastCheckBoxClickedIndex = PageRecordIDs.SearchArray(RowChkBox.name.slice(3), 1, "string")[0];
        
        if(document.getElementById(RowChkBox.id.replace('chk', 'tr'))!=null)
	    {
	        //Highlighted row by checking is checkbox is checked.
	        if (RowChkBox.checked) 
	            AdminHighlightRow1(document.getElementById(RowChkBox.id.replace('chk', 'tr')), MenuTableColorArray);
	        else 
	            AdminHighlightRow2(document.getElementById(RowChkBox.id.replace('chk', 'tr')), MenuTableColorArray);
	    }
        
        //Stop event to bubble upwards in the document nodes
	    if (GlobalEventObject != null) {

	        GlobalEventObject.cancelBubble = true;
	        if (GlobalEventObject.stopPropagation) GlobalEventObject.stopPropagation();

	    }    
	}
}

//This function is used to Select / deselect checkboxes in Detail View Menu Pages .
function DetailViewMenuPageSelectRecord(strType, e, RecordID, FilterForm,SelArray,AllIDsArray) {
    var target = '';
    var tagName = ''
    if (!e) var e = window.event;

    if (e != null) {
        target = e.target || e.srcElement;
        tagName = target.tagName;
    }

    tagName = tagName.toUpperCase();
    if (tagName != 'A' && tagName != 'IMG') {
        with (FilterForm) {
            var obj = document.getElementById('chk' + RecordID);

            if (obj != null) {

                if (strType.toUpperCase() == '')
                    obj.checked = !obj.checked;

                SelectBoxClick(obj, SelArray, FilterForm.SelectList);
                for (var i = 0; i < AllIDsArray.myArray.length; i++) {
                    
                    var tmpObj = document.getElementById('chk' + AllIDsArray.myArray[i]);

                    if (document.getElementById('StatusRow' + AllIDsArray.myArray[i]) != null) {
                        if (tmpObj.checked)
                            document.getElementById('StatusRow' + AllIDsArray.myArray[i]).style.backgroundColor = 'lightgrey';
                        else
                            document.getElementById('StatusRow' + AllIDsArray.myArray[i]).style.backgroundColor = '';
                    }                            
                }
            }
        }
    }

    if (e != null) {

        e.cancelBubble = true;
        if (e.stopPropagation) e.stopPropagation();

    }
}

//Function used for select boxes in tables, it checks the Array SelArray to see if it already
//contains the value of chkBox and either adds or removes it.
function UpdateSelectList(chkBox, SelArray, FormSelectList) {
    var SearchResult;

    SearchResult = SelArray.SearchArray(chkBox.name.slice(3), 1, "string");
    if (SearchResult[0] == -1) {
        if (chkBox.checked == true) {
            SelArray.push(chkBox.name.slice(3));
        }
    }
    else {
        if (chkBox.checked == false) {
            SelArray.DeleteElement(SearchResult[0]);
        }
    }
    FormSelectList.value = SelArray.ShowArray();

}

//Function used for select boxes in tables, copies the AllObjArray into SelArray and populates the form
//FormName (SelectList) with the new array values
function SelectAll(SelArray,AllObjArray,FormSelectList,TickBoxForm)
{
	SelArray.StoreArray(AllObjArray.GetArray());
	InitialiseTickBoxes(TickBoxForm,SelArray.GetArray(),"chk");
	FormSelectList.value = SelArray.ShowArray();
}

function PopulateHyperlink (FormName,HyperlinkText,HyperlinkURL,HyperlinkTitle,Hyperlink,CheckTitle)
{
	with (FormName)
	{
		//Populate the URL part of the Hidden Hyperlink field
		Hyperlink.value = HyperlinkURL.value;
	
		//Populate the Title (#Title) part of the Hidden Hyperlink field
		if (CheckTitle == true)
		{
			if (HyperlinkTitle.value != "")
			{
				if (HyperlinkURL.value == "")
				{
					strErrors += "- You have entered a Hyperlink Title but no Hyperlink URL\n\n";
				}
				Hyperlink.value += "#" + HyperlinkTitle.value;
			}	
		}
	
		//Check to see if the user has entered text with no hyperlink URL
		if ((HyperlinkText.value != "") && (Hyperlink.value == ""))
		{
			strErrors += "- You have entered some Hyperlink Text but no Hyperlink URL\n\n";
		}
	
		//Check to seee if the user has entered a hyperlink but no display text
		if (Hyperlink.value != "")
		{
			if (HyperlinkText.value == "")
			{
				strErrors += "- You have entered a Hyperlink with no Hyperlink Text\n\n";
			}
		}
	}
}

function JSDateCompare (Date1, strCompare, Date2)
{
	var ReturnVal, CDate1, CDate2;
	
	ReturnVal = false
	
	if (Date1 != "" && Date2 != "")
	{
		CDate1 = new Date (ApplyFormatting (Date1,"Date(IETF)"));
		CDate2 = new Date (ApplyFormatting (Date2,"Date(IETF)"));
		
		switch (strCompare)
		{
			case ">":
				if (CDate1.getTime() > CDate2.getTime())
					ReturnVal = true
				break;
				
			case ">=":
				if (CDate1.getTime() >= CDate2.getTime())
					ReturnVal = true
				break;
				
			case "<":
				if (CDate1.getTime() < CDate2.getTime())
					ReturnVal = true
				break;
				
			case "<=":
				if (CDate1.getTime() <= CDate2.getTime())
					ReturnVal = true
				break;
				
			case "==":
				if (CDate1.getTime() == CDate2.getTime())
					ReturnVal = true
				break;
		}		
	}
	return(ReturnVal);
}

// OffsetValue has been added to this function ..This function would also being called without specifying the offset value.
// In that case we check if it undefined and if yes then we just ignore it.
function CheckDates(objStartDate, objStartTime, objStartDateTime, objEndDate, objEndTime, objEndDateTime, AutoPopulate, OffSetValue)
{
	var DateResult, StartDateCompare, EndDateCompare, strStartDate, strStartTime, strEndDate, strEndTime;

	var StartDateFieldID, EndDateFieldID;
	/*
	This is how we will do it but we will need to set the title in all places in the
	system as the Google tool bar uses this attribute to set the "You can auto fill this..."
	StartDateFieldID = element.getAttribute('title');

	if (StartDateFieldID == null || StartDateFieldID == '')
	StartDateFieldID = objStartDate.id;
	*/

	StartDateFieldID = objStartDate.getAttribute('FieldID');

	if (StartDateFieldID == null || StartDateFieldID == '')
	    StartDateFieldID = objStartDate.id;

	EndDateFieldID = objEndDate.getAttribute('FieldID');

	if (EndDateFieldID == null || EndDateFieldID == '')
	    EndDateFieldID = objEndDate.id;
	        
	strStartDate = objStartDate.value;
	strEndDate = objEndDate.value;
		    
	//This check will put the start / end dates in the correct format, i.e. if they have 
	//a year as 07 it will convert it to 2007
	if (strStartDate.length > 0 && chkdate(strStartDate))
	    strStartDate = chkdate(strStartDate)
	
	if (strEndDate.length > 0 && chkdate(strEndDate))
	    strEndDate = chkdate(strEndDate)
	
	if (objStartTime != "")
		strStartTime = objStartTime.value;
	else
		strStartTime = "";
		
	if (objEndTime != "")
		strEndTime = objEndTime.value;
	else
		strEndTime = "";
		
	//Check if all fields are valid - they may be empty, but do nothing unless they are all valid.
	if (chkdate(strStartDate) && chkdate(strEndDate) && chkTime(strStartTime) && chkTime(strEndTime))
	{
		if (AutoPopulate)
		{
			//Check the user has entered a start and end date, if not, populate the end date
			if (strEndDate.length == 0 && strStartDate.length > 0)
		        strEndDate = strStartDate;
						
			//This puts an end time in if the end time is empty and the start time isn't AND
			//the start and end dates are on the same day (very important)
			
			//Don't do this if start date is empty
			if (strStartDate.length > 0)
			{
				//First convert them to date fields
				StartDateCompare = new Date(ApplyFormatting (strStartDate,"Date(IETF)"));
				EndDateCompare = new Date(ApplyFormatting (strEndDate,"Date(IETF)"));
				
				if (StartDateCompare.getTime() == EndDateCompare.getTime() && (strEndTime.length == 0 && strStartTime.length > 0))
				    strEndTime = strStartTime;
			}
			
			//Check the user has entered a start and end date, if not, populate the start date
			if (strEndDate.length > 0 && strStartDate.length == 0)
			{
			    strStartDate = strEndDate;
					
				if (strStartTime.length == 0 && strEndTime.length > 0)
				    strStartTime = strEndTime;
			}
		}
		
		//Now compare the dates to make sure the start date is before the end date
		if (strStartDate.length > 0 && strEndDate.length > 0)
		{
			StartDateCompare = ApplyFormatting (chkdate (strStartDate),"Date(IETF)");
			EndDateCompare = ApplyFormatting (chkdate (strEndDate),"Date(IETF)");
			
			if (strStartTime.length > 0)
			{
			    strStartTime = chkTime(strStartTime)
				StartDateCompare += " " + strStartTime;
		    }
		    
		    if (strEndTime.length > 0)
		    {
		        strEndTime = chkTime(strEndTime)
			    EndDateCompare += " " + strEndTime;
		    }
		    
			StartDateCompare = new Date(StartDateCompare);
			EndDateCompare = new Date(EndDateCompare);
			
			if (StartDateCompare.getTime() > EndDateCompare.getTime())
			{
			    strError += "- The " + EndDateFieldID + " is earlier than the " + StartDateFieldID + "\n\n";
			}
		}	
    	
	    objStartDate.value = strStartDate;
	    objEndDate.value = strEndDate;
    			
	    if (objStartTime != "")
		    objStartTime.value = strStartTime;
    			
	    if (objEndTime != "")
		    objEndTime.value = strEndTime;
    			
	    if (strStartDate != "" && strStartTime != "")
		    strStartDate += " " + strStartTime;
    			
	    if (strEndDate != "" && strEndTime != "")
		    strEndDate += " " + strEndTime;
    				
	    //Populate hidden variables with date + time - Take into account any offset, i.e. if the user is in a
	    //different country to the system
	    var dateObj;
	    if (objStartDateTime != "")
	    {   
		    if (typeof(OffSetValue) != 'undefined')
		    {
			    if ( strStartTime != "" ) 
			    {
				    dateObj = new Date( ApplyFormatting (strStartDate,"Date(IETF)"));
				    dateObj = new Date(dateObj.getTime() + (OffSetValue*60000))
				    strStartDate = dateObj.getDate() + "/" + (dateObj.getMonth() + 1) + "/" + dateObj.getFullYear() + ' ' + dateObj.getHours() + ":" + dateObj.getMinutes();
			    }
		    }
		    objStartDateTime.value = strStartDate;	
	    }	
	    if (objEndDateTime != "")
	    {    		
		    if (typeof(OffSetValue) != 'undefined')
		    {
			    if ( strEndTime != "" ) 
			    {
				    dateObj = new Date( ApplyFormatting (strEndDate,"Date(IETF)"));
				    dateObj = new Date(dateObj.getTime() + (OffSetValue*60000))
				    strEndDate = dateObj.getDate() + "/" + (dateObj.getMonth() + 1) + "/" + dateObj.getFullYear() + ' ' + dateObj.getHours() + ":" + dateObj.getMinutes();
			    }
		    }
    		
		    objEndDateTime.value = strEndDate;	
	    }		
	}
}

//This function populates the hidden datetime field with the date and time fields
// OffsetValue has been added to this function ..This function would also being called without specifying the offset value.
// In that case we check if it undefined and if yes then we just ignore it.
function PopulateDateTime(objDate, objTime, objDateTime)
{

	var dateObj, strDate;
	
	strDate = objDate.value
	
	if (strDate.length > 0)
	{
		if (chkdate(strDate))
		{	
		    strDate = chkdate(strDate)
			if (objTime.value.length > 0)
			{
				if (chkTime(objTime.value))
				{	
					objDateTime.value = strDate + " " + objTime.value;
				}	
			}
			else
				objDateTime.value = strDate;
		}
	}
	else
		objDateTime.value = "";	
}

//This function populates the end date with the stat date plus the time interval
//i.e. for project response times and action durations.
function CalculateTimeInterval(objStartDate, objStartTime, objEndDate, objEndTime, TimeInterval, AutoPopulateTime)
{
	var ReturnDate, tmpResult;
	
	InitialiseField(objStartDate,"Date","");
    
    //if we want to auto populate time to current time.
	if (AutoPopulateTime) {
	    //If we are adding 0 hours then do not initialise the end time.
	    //if (TimeInterval != 0)
	    InitialiseField(objStartTime, "Time", "");
	}
	
	tmpResult = chkdate(objStartDate.value);
	
	if (tmpResult != "empty")
	{
		//Get the date into the IETF Date Standard (e.g 1 Jan 2005 17:00)
		ReturnDate = ApplyFormatting (tmpResult,"Date(IETF)");
		
		tmpResult = chkTime(objStartTime.value);
		if(tmpResult)
		{
			if((tmpResult != "empty"))
			{
				ReturnDate += " " + tmpResult;
			}
		}
		
		//Parse the date as a Date() variable
		ReturnDate = new Date(ReturnDate);
		
		//Add on the time interval (Hours)
		ReturnDate = new Date (ReturnDate.getTime() + (TimeInterval * 60000));
		
		//Populate the calculated end date
		objEndDate.value = ReturnDate.getDate() + "/" + (ReturnDate.getMonth() + 1) + "/" + ReturnDate.getFullYear();
		
		//Check for preseedintg the minutes with a zero
		if (ReturnDate.getMinutes() < 10)
			objEndTime.value = ReturnDate.getHours() + ":0" + ReturnDate.getMinutes();
		else
			objEndTime.value = ReturnDate.getHours() + ":" + ReturnDate.getMinutes();
	}
}

//This function is used to set the objOptionGroup to the last item in it (which should say other)
function OtherKeyPress(objAction,objOther,objOptionGroup)
{
	if (objAction.type.toLowerCase()=="text")
	{
		if (objOther.value.length > 0)
		{
			objOptionGroup.selectedIndex = objOptionGroup.length - 1;
		}
	}
	else
	{
		objOther.value = "";
	}
	
	if ((objOptionGroup.selectedIndex == objOptionGroup.length - 1))
		objOther.required = true;
	else
		objOther.required = false;
}

function DeleteRecord(objForm,strMessage)
{
	var strMsg
	
	if (strMessage == null || strMessage == "")
		strMsg = "Warning!\nAre you sure you want to permanently delete this record?";
	else
		strMsg = strMessage;
		
	if (confirm(strMsg))
	{
		objForm.submit();
	}
}

function DeleteFile(FormName,ImageField,TextArea)
{
	with (FormName)
	{
		ImageField.value = "";
	}
	
	with (window)
	{
		TextArea.innerHTML = 'No File Selected';
	}
}

//Deletes Options from the option group (objName)
function DeleteOptions (objName)
{
	NewOption = new Option();
	
	var OptionLength;
	OptionLength = objName.length;
	
	for (var i=0; i < OptionLength; i++)
	{
		objName.remove(NewOption,0);
	}
}
	
//Adds the options stored in OptionArray to the option group (objName)
//N.B. OptionArray is a 2 dimensional array (0 = Values, 1 = Display text)
function AddOptions(OptionArray,objName)
{
	//For Netscape browsers, the length of the select (objName) is higher than IE and Firefox
	//NetscapeIndex is a variable used for compatibility of all browsers
	var NetscapeIndex = 0;
	
	if(navigator.userAgent.indexOf("Netscape")>-1)
		NetscapeIndex = 1;
	
	var AllowAdd=0;
		
	if(objName.length-NetscapeIndex>0)
		AllowAdd = 1;
	
	if(AllowAdd==1){
		//Store objName in a temp array  	
		var tmpobjName = new Array(1);
					
		var tmpArray = new Array(1);				
		tmpobjName[0] = tmpArray;	

		var tmpArray = new Array(objName.length);
		tmpobjName[1] = tmpArray;	
	
		
		for (var i=0;i<tmpobjName[1].length;i++)
		{			
			tmpobjName[0][i] = objName.options[i].value;
			tmpobjName[1][i] = objName.options[i].text;
		}
		
		//Clear objName
		DeleteOptions (objName);
	}

	//Copy all options in Option Array (to be added) to start of objName
	for (var i=0; i < OptionArray[0].length; i++)
	{			
		NewOption = new Option();
		NewOption.text = OptionArray[1][i];
		NewOption.value = OptionArray[0][i];
		objName.options[i] = NewOption;
		
		//Old method
		//objName.options.add(NewOption,i);
	}	

	if(AllowAdd==1){
		//Copy all existing options from tmpobjName into END OF objName
		var i=objName.length;
				
		for (var j=0; j<tmpobjName[1].length; j++)
		{			
			NewOption = new Option();
			NewOption.text = tmpobjName[1][j];
			NewOption.value = tmpobjName[0][j];
			objName.options[i++] = NewOption;
			
			//Old method
			//objName.options.add(NewOption,i);
		}
	}
}
				
//This function adds a selected user option to a hiden variable (objIDs)
//it accepts a drop down list object, and array (must by ITS - SingleDynamicArray)
//and the hidden IDs field
function AddOptionPickerOption(objDropDown,ArrayOptions,objIDs)
{ 
	if (objDropDown.value != '' && ArrayOptions.SearchArray(objDropDown.value,0,'')[0] == -1)
	{
		ArrayOptions.push(objDropDown.value);
	}
	objIDs.value=ArrayOptions.GetArray();
}

//This function removes a selected user option from an array of selected ids
//it accepts a drop down list object, and array (must by ITS - SingleDynamicArray)
function RemoveOptionPickerOption(objDropDown,ArrayOptions)
{
    //Quick search to make sure it already exists in the options array
	if (ArrayOptions.SearchArray(objDropDown.value,0,'')[0] != -1)
	{
	    //Make sure this option is able to be removed
	    if (objDropDown[objDropDown.selectedIndex].AllowRemove != 0)
		    ArrayOptions.DeleteElement(ArrayOptions.SearchArray(objDropDown.value,0,'')[0]);
	}
}

//This function returns the Form data as string. 
function GetFormPostData(objForm) {
    var params = ''

    for (var i = 0; i < objForm.length; i++) {
        var element = objForm.elements[i];
        if (params != '')
            params += '&';
        params += element.name + '=' + escape(element.value);
    }
    return params;
}

//This function loops through the options of a radio button and returns
//the selected one. It is required as we can't reference radio buttons with the .value
//property and they don't have a selectedIndex either.
function GetRadioButtonValue(objRadio)
{
	for (var i = 0; i < objRadio.length; i++)
	{
		if (objRadio[i].checked)
			return (objRadio[i].value);
	}
}

//JS Trim Function
function JSTrim(str){
	str = str.replace(/\u00a0/g,'');
	return(str.replace(/^\s*|\s*$/g,''));
}

//this function set the values of the Address fields on the page.
function PopulateAddressFields(objForm, strPrefix, AddressArray, strValue, strPage)
{
    var AddressIds = AddressArray[0];
    var RecordIndex = 0;
    var tmpObj  = null;
    var ContactName , Address1, Address2, Address3, Town, County, CountryID, PostCode, Telephone, Fax, Mobile, Email, WebsiteAddress;
    var tmpArray ;
    
    ContactName = '';
    Address1 = '';
    Address2 = '';
    Address3 = '';
    Town = '';
    County = '';
    CountryID = '';
    PostCode = '';
    Telephone = '';
    Fax = '';
    Mobile = '';
    Email = '';
    WebsiteAddress = '';
    
    if (strValue != '')
    {
        
        if (strValue == '0')
        {   
            Address1 = '';
            Address2 = '';
            Address3 = '';
            Town = '';
            County = '';
            CountryID = '';
            PostCode = '';
            Telephone = '';
            Fax = '';
            Mobile = '';
            Email = '';
            WebsiteAddress = '';
            
        }
        else
        {
            for( var i = 0 ;i < AddressIds.length ;i++)
            {
                if (AddressIds[i] == strValue)
                {
                    RecordIndex = i;
                    break;    
                }
            }
            
            if (RecordIndex >= 0)
            {
                //Only if user selected Contact AS Invoice Address
                if (strValue.charAt(0) == 'C')
                {
                   //Contact Name
                   tmpArray = AddressArray[14];
                   if (tmpArray != null)
                        ContactName = tmpArray[RecordIndex];
                  
                }
                
                //Address1
                tmpArray = AddressArray[1];
                if (tmpArray != null)
                    Address1 = tmpArray[RecordIndex];
                
                //Address2
                tmpArray = AddressArray[2];
                if (tmpArray != null)
                    Address2 = tmpArray[RecordIndex];

                //Address3
                tmpArray = AddressArray[3];
                if (tmpArray != null)
                    Address3 = tmpArray[RecordIndex];
                
                //Town
                tmpArray = AddressArray[4];
                if (tmpArray != null)
                    Town = tmpArray[RecordIndex];
                
                //County
                tmpArray = AddressArray[5];
                if (tmpArray != null)
                    County = tmpArray[RecordIndex];

                //CountryID 
                tmpArray = AddressArray[6];
                if (tmpArray != null)
                {
                    CountryID = tmpArray[RecordIndex];
                    if (CountryID == 0 )
                        CountryID = '';
                }    
                    
                //Postcode
                tmpArray = AddressArray[7];
                if (tmpArray != null)
                    PostCode = tmpArray[RecordIndex];                                    

                //Telephone
                tmpArray = AddressArray[8];
                if (tmpArray != null)
                    Telephone = tmpArray[RecordIndex];
                    
                //Fax
                tmpArray = AddressArray[9];
                if (tmpArray != null)
                    Fax = tmpArray[RecordIndex];
                
                //Mobile
                tmpArray = AddressArray[10];
                if (tmpArray != null)
                    Mobile = tmpArray[RecordIndex];

                //Email
                tmpArray = AddressArray[11];
                if (tmpArray != null)
                    Email = tmpArray[RecordIndex];
                
                //WebsiteAddress
                tmpArray = AddressArray[12];
                if (tmpArray != null)
                    WebsiteAddress = tmpArray[RecordIndex];
            }                    
                                                                                
           
            
        }
        
        // Setting thr values on the page
        if (RecordIndex >= 0 )
        {
            
           //Contact Name
           tmpObj = objForm.elements['S' + strPrefix + 'ContactName'];
           if( tmpObj != null)
           {
                if (strValue.charAt(0) == 'C' || (strValue.charAt(0) == 'A' && tmpObj.value == '') || strPage == 'InsertVacancy')
                    tmpObj.value = ContactName;
           }    
            
            //Address1
            tmpObj = objForm.elements['S' + strPrefix + 'Address1'];
            if( tmpObj !=null)
                tmpObj.value = Address1;
            
            //Address2
            tmpObj = objForm.elements['S' + strPrefix + 'Address2'];
            if( tmpObj !=null)
                tmpObj.value = Address2;
                
            //Address3    
            tmpObj = objForm.elements['S' + strPrefix + 'Address3'];
            if( tmpObj !=null)
                tmpObj.value = Address3;

            //Town
            tmpObj = objForm.elements['S' + strPrefix + 'Town'];
            if( tmpObj !=null)
                tmpObj.value = Town;
            
            //County
            tmpObj = objForm.elements['S' + strPrefix + 'County'];
            if( tmpObj !=null )
                tmpObj.value = County;
            
            //CountryID
            tmpObj = objForm.elements['N' + strPrefix + 'CountryID'];
            if( tmpObj !=null)
                tmpObj.value = CountryID;
            
            //Postcode
            tmpObj = objForm.elements['S' + strPrefix + 'PostCode'];
            if( tmpObj !=null)
                tmpObj.value = PostCode;
            
            //Telephone
            tmpObj = objForm.elements['S' + strPrefix + 'Telephone'];
            if( tmpObj !=null && tmpObj.value == '')
                tmpObj.value = Telephone;

            //Fax
            tmpObj = objForm.elements['S' + strPrefix + 'Fax'];
            if( tmpObj !=null && tmpObj.value == '')
                tmpObj.value = Fax;
            
            //Mobile
            tmpObj = objForm.elements['S' + strPrefix + 'Mobile'];
            if( tmpObj !=null && tmpObj.value == '')
                tmpObj.value = Mobile;
            
            //Email
            tmpObj = objForm.elements['S' + strPrefix + 'Email'];
            if( tmpObj !=null && tmpObj.value == '')
                tmpObj.value = Email;

            //WebSiteAddress
            tmpObj = objForm.elements['S' + strPrefix + 'WebsiteAddress'];
            if( tmpObj !=null && tmpObj.value == '')
                tmpObj.value = WebsiteAddress;
        }                
    }
}

function ShowAddress( objDropDown, AddressArray, PopupName)
{

    var divObj = document.getElementById(PopupName);
    var AddressIds = AddressArray[0];
    var RecordIndex = -1;
    var strPopupHTML = '';
    if (divObj != null)
    {
        if (AddressIds.length > 0 && objDropDown.value != '' && objDropDown.value != '0' )
        {
            
            for( var i = 0 ;i < AddressIds.length ;i++)
            {
                if (AddressIds[i] == objDropDown.value)
                {
                    RecordIndex = i;
                    break;    
                }
            }
            
            if (RecordIndex >=0 )
            {
                //Only if user selected Contact AS Invoice Address
                if (objDropDown.value.charAt(0) == 'C')
                {
                   //Contact Name
                   tmpArray = AddressArray[14];
                   if (tmpArray != null && tmpArray[RecordIndex] != '')
                   {
                        strPopupHTML = '<b>Contact Name:</b> ' + tmpArray[RecordIndex] + '<BR>';
                   }   
                }
                
                //Address1
                tmpArray = AddressArray[1];
                if (tmpArray != null && tmpArray[RecordIndex] != '')
                {
                    strPopupHTML += '<b>Address 1:</b> ' + tmpArray[RecordIndex] + '<BR>';
                }    
                
                //Address2
                tmpArray = AddressArray[2];
                if (tmpArray != null && tmpArray[RecordIndex] != '')
                {
                    strPopupHTML += '<b>Address 2:</b> ' + tmpArray[RecordIndex] + '<BR>';
                }    
                
                //Address3
                tmpArray = AddressArray[3];
                if (tmpArray != null && tmpArray[RecordIndex] != '')
                {
                    strPopupHTML += '<b>Address 3:</b> ' + tmpArray[RecordIndex] + '<BR>';
                }    
                
                //Town
                tmpArray = AddressArray[4];
                if (tmpArray != null && tmpArray[RecordIndex] != '')
                {
                    strPopupHTML += '<b>Town:</b> ' + tmpArray[RecordIndex] + '<BR>';
                }    
                
                //County
                tmpArray = AddressArray[5];
                if (tmpArray != null && tmpArray[RecordIndex] != '')
                {
                    strPopupHTML += '<b>County:</b> ' + tmpArray[RecordIndex] + '<BR>';
                }    
                
                //CountryName
                tmpArray = AddressArray[13];
                if (tmpArray != null && tmpArray[RecordIndex] != '')
                {
                    strPopupHTML += '<b>Country:</b> ' + tmpArray[RecordIndex] + '<BR>';
                }
                
                    
                //Postcode
                tmpArray = AddressArray[7];
                if (tmpArray != null && tmpArray[RecordIndex] != '')
                {
                    strPopupHTML += '<b>Postcode:</b> ' + tmpArray[RecordIndex] + '<BR>';
                }    
                                                

                //Telephone
                tmpArray = AddressArray[8];
                if (tmpArray != null && tmpArray[RecordIndex] != '')
                {
                    strPopupHTML += '<b>Telephone:</b> ' + tmpArray[RecordIndex] + '<BR>';
                }    
                
                    
                //Fax
                tmpArray = AddressArray[9];
                if (tmpArray != null && tmpArray[RecordIndex] != '')
                {
                    strPopupHTML += '<b>Fax:</b> ' + tmpArray[RecordIndex] + '<BR>';
                }    
                
                
                //Mobile
                tmpArray = AddressArray[10];
               if (tmpArray != null && tmpArray[RecordIndex] != '')
                {
                    strPopupHTML += '<b>Mobile:</b> ' + tmpArray[RecordIndex] + '<BR>';
                }    
                

                //Email
                tmpArray = AddressArray[11];
                if (tmpArray != null && tmpArray[RecordIndex] != '')
                {
                    strPopupHTML += '<b>Email:</b> ' + tmpArray[RecordIndex] + '<BR>';
                }    
                
                
                //WebsiteAddress
                tmpArray = AddressArray[12];
                if (tmpArray != null && tmpArray[RecordIndex] != '')
                {
                    strPopupHTML += '<b>Website Address:</b> ' + tmpArray[RecordIndex] + '<BR>';
                }    
                
                
                if (strPopupHTML !='')
                {
                    strPopupHTML = '<span style=\"float:right;Cursor:pointer;\" onclick=\"HideAddress(\'' + PopupName +'\');\"><b>Close</b></span><BR>'+ strPopupHTML ;
                    divObj.style.position = 'absolute';
                    divObj.style.display = '';
					divObj.style.top = getY(objDropDown)+ 32;
					divObj.style.left = getX(objDropDown) + 375;
					divObj.innerHTML = strPopupHTML;
					//alert(divObj.outerHTML);
                }
            }
        }
        else
        {
            HideAddress(PopupName);
        }
    }
   
}   

function HideAddress(PopupName)
{
    var divObj = document.getElementById(PopupName);
    if ( divObj != null)
    {
        divObj.innerHTML = '';
        divObj.style.display = 'none';
        
    }
}

/*
This function will which address to use. Either company or contac/Vacancies/Invoice etc.
    -   objForm
    -   strPrefix , prefix of fields eg Invoice
    -   strCompantAddress .. Html of the Company Address 
    -   AddressDIV  .. name of DIV which shows which address will be used
    -   strType .. place where the function is being called.
*/
function CheckAddressToUse(objForm, strPrefix, strCompanyAddress, AddressDIV, strType )
{
    var objInfo, tmpObj;
    objInfo = document.getElementById(AddressDIV);
    var address1,address2, address3,town,county,postcode,country;
    var Msg1, Msg2, Msg3;
    // Msg1 = Address below
    // Msg2 = No Address
    // Msg3 = Company Address
    
    if (strType.toUpperCase() == 'VACANCIES')
    {
        Msg1 = 'This vacancy will use the address below.';
        Msg2 = 'No address has been setup for this vacancy.';
        Msg3 = 'This vacancy will use the company address.';
    }
    else if (strType.toUpperCase() == 'VACANCYINVOICE')
    {
        Msg1 = 'This vacancy will use the address below as the invoice address.';
        Msg2 = 'No invoice address has been setup for this vacancy.';
        Msg3 = 'This vacancy will use the company address as the invoice address.';
    }
    else if (strType.toUpperCase() == 'CONTACTS')
    {
        Msg1 = 'This contact will use the address below.';
        Msg2 = 'No address has been setup for this contact.';
        Msg3 = 'This contact will use the company address.';
    }
    else if (strType.toUpperCase() == 'INVOICE')
    {
        Msg1 = 'This invoice will be sent to the address below.';
        Msg2 = 'No address has been setup for this invoice.';
        Msg3 = 'This invoice will be sent to the company address.';
    }
        
    address1 = '';
    address2 = '';
    address3 = '';
    town = '';
    county = '';
    postcode = '';
    country = ''
    
    with(document.MainForm)
    {
        if (objInfo !=null)
        {
            //Address1
            tmpObj = objForm.elements['S' + strPrefix + 'Address1'];
            if( tmpObj != null)
                address1 = tmpObj.value;
            
            //Address2
            tmpObj = objForm.elements['S' + strPrefix + 'Address2'];
            if( tmpObj != null)
                address2 = tmpObj.value;
                
            //Address3
            tmpObj = objForm.elements['S' + strPrefix + 'Address3'];
            if( tmpObj != null)
                address3 = tmpObj.value;        
            
            //Town
            tmpObj = objForm.elements['S' + strPrefix + 'Town'];
            if( tmpObj != null)
                town = tmpObj.value;
            
            //County
            tmpObj = objForm.elements['S' + strPrefix + 'County'];
            if( tmpObj != null)
                county = tmpObj.value;        
            
            //postcode
            tmpObj = objForm.elements['S' + strPrefix + 'PostCode'];
            if( tmpObj != null)
                postcode = tmpObj.value;
            
            //CountryID
            tmpObj = objForm.elements['N' + strPrefix + 'CountryID'];
            if( tmpObj != null)
                country = tmpObj.value;
                    
            if( address1 != '' || address2 != '' || address3 != '' || town != '' || county != '' || postcode != '' || country != '')
            {
               objInfo.innerHTML = Msg1;
            }
            else
            {
                if (strCompanyAddress == '')
                    objInfo.innerHTML = Msg2;
                else
                    objInfo.innerHTML = Msg3;
            }
        }    
    }
}

/*
This function is for removing the default arial tags that we tried using
to set the default font of text editors.
We should get this working to check for required fields etc.
*/
function RemoveFieldHTML(objDbField, Options)
{
    var objDiv = document.createElement("div");
    var str ='';
   
   objDiv.innerHTML = objDbField.value;
   
    if (typeof(objDiv.text) != 'undefined')
	    str = objDiv.text;
    else if (typeof(objDiv.textContent) != 'undefined')
	    str = objDiv.textContent;
    else if (typeof(objDiv.innerText) != 'undefined')
	    str = objDiv.innerText;
        
    str = JSTrim(str);
        	
    if (str == '')
    {
        objDbField.value = ''; 
    } 
} 

//This function populates the selected skills in a hidden field 
//This field will be used by process form
function PopulateSelectedSkills(objForm){
	var tmpString = "";
	for(var i=0; i<SelectedSkillsArray.length; i++){	
		if(((SelectedSkillsArray[i][0] == "0") && (SelectedSkillsArray[i][7] == "Delete")) || (SelectedSkillsArray[i][7] == ""))
			continue;
		else{
			if(tmpString!="")
				tmpString += "-";
			
			//CSV of the following fields:
			//ColumnID(e.g. CandidateSkillID), SkillID, From, To, Level, Process
			tmpString += SelectedSkillsArray[i][0] + ',' +
						 SelectedSkillsArray[i][2] + ',' +
						 SelectedSkillsArray[i][4] + ',' +
						 SelectedSkillsArray[i][5] + ',' +
						 SelectedSkillsArray[i][6] + ',' +
						 SelectedSkillsArray[i][7] 
		}
	}
	
	objForm.XOmitSelectedSkills.value = tmpString;
}
