/* THIS FUNCTION CHANGES THE BACKGOUND COLOUR OF MENU*/
//var imagePath = '/iwo';
var imagePath = '';
function changeLinkStyles(IDs) {
	  
	 var ids = IDs.split(',');
	 if(ids[1]) {// if it has sublinks e.g current exhibition ..
		  var subLink = ids[1];
		  var subLinkDiv = ids[1]+'_div';
		  var topLink = ids[0];
		  var topLinkDiv = ids[0]+'_div';

	 }
	
	  else { var topLink = ids[0];   var topLinkDiv = ids[0]+'_div'; }// if it is only the top link e.g. Home, Exhibition  
	  //alert(ids[1]);
	 // alert(topLink); 
	 // var arrow = topLink+'RedArrow';
	 // var Block = topLink+'Block';
	  var a = ['home', 'about', 'membership', 'events', 'news',  'jobs','ask','products','links','company','business','contact'];
	  for (var i = 0; i < a.length; i++) {
			if (topLink == a[i]) {
				$(topLink).style.color  = '#004583';
				$(topLinkDiv).style.background = '#bbdde4';
				if(subLinkDiv) $(subLinkDiv).style.background = '#bbdde4';
				if(subLink) $(subLink).style.color  = '#004583' ;
			}
			//startEffect ($(Block));
    }
	
}

/* THE MOUSE OVER FUNCTIONS */
function highlight(IDs) {
	 var image = IDs;
	 var ids = image.split('_');
	// alert (ids[0]);
	// var colour = ids[0];
	// alert(imageNo)
	if ( ids[0]== 'gray'){ //it is a gray plus sign
		$(image).src =imagePath+'/images/siteImages/PlusOrange.jpg' ;
	}
}
 
 
 function unHighlight(IDs) {
	  var image = IDs;
	 var ids = image.split('_');

	if ( ids[0]== 'gray'){ //it is a gray plus sign
		$(image).src =imagePath+'/images/siteImages/PlusGray.jpg' ;
	}
	 
	
 }



function startEffect(anyDiv) {
	var anyDiv = anyDiv;
  //startTop   = $('pagCopy').offsetTop;
  //startLeft  = $('pagCopy').offsetLeft;
	new Rico.Effect.FadeTo( anyDiv , 0.02, 500, 10 );
}


function resetEffect() { 
	new Rico.Effect.FadeTo( 'pagCopy', 1, 500, 50 );
}



function $() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }

  return elements;
}

/* A FUNCTION TO CHANGE THE COLOUR OF FOOTER LINKS*/
function changeFooterLinkStyles(IDs) {
	  
	  var ids = IDs;
	  var a = ['disclaimer', 'jobs', 'terms', 'privacy'];
	  for (var i = 0; i < a.length; i++) {
			
			if (ids == a[i]) {
				
				 $(ids).style.color  = '#6C6E70' ;
			}
			//startEffect ($(Block));
    }
}

function getAreaOps(page, selValue){
	var page = page ; var selValue = selValue;
	//alert(selValue);
	if (selValue==2){
		$('subCatId').style.display='inline';
		getSecondLevelFilterOps(page, selValue);
	}
	else 
		$('areaForm').submit();
}

function printPage(page){
	page = document;
	print(page);
}


/*
This is a generic function 
It alerts users to select a proper option and than submits the form
*/
function validateFormAndSubmit(selectedOption){
	var slectionID = selectedOption;
	var selectDropDown = $(slectionID)[0].text;
	var selction = $(slectionID).value;
	
	alert(selction);

	if (!$(slectionID)[2]) {
		// checks if there is any data in= the drop down menu
		alert("There is no " +selectDropDown.substring(9,50)+' added  yet');
	}
	
	else if (selction == "-----")	{
		alert("Please select a " +selectDropDown.substring(9,50));
		$(slectionID).focus();
		return true
	}
	
	else {
		//document.forms[1].submit();
		//var ss = document.forms[0].name;
		//alert(ss);
	}
}

/* Gets the options for the second filter at top of most pages. */
function getSecondLevelFilterOps(page, selValue, status){
	//var cat = page;
	new Ajax.Request('/libs/GetSecondLevelFilterOps.php', {
		method: 'get',
  		parameters: 'page=' + page + '&selValue=' + selValue + '&status=' + status,
  		onSuccess: function(transport){

			xmlDoc = transport.responseXML;
			var xmlOptions = xmlDoc.getElementsByTagName('optionItem');
	
			//if(cat=='jobCat') var selectElement = document.getElementById('jobCat');
			if(document.getElementById('jobCat')) 
				var selectElement = document.getElementById('jobCat');
			else 
				var selectElement = document.getElementById('subCatId');
			//selectElement.innerHTML = "<option value='-----'>Please choose</option>"; 
			
			selectElement.innerHTML = "<option value='-----'>View All</option>";
			if (window.ActiveXObject){//if it is IE
				var o =  new Option();
				o.text = "View All";
				selectElement.add(o);
			}
			
			for(var i = 0; i < xmlOptions.length; i++){
				var o = new Option(
						xmlOptions[i].getElementsByTagName('title')[0].firstChild.data,
						xmlOptions[i].getElementsByTagName('id')[0].firstChild.data,
						false,
						false
				);
				selectElement.options[selectElement.options.length] = o;
			}
    	}
  	});
}

/* Retrieves the names of contacts in the specified region */
function getContactsInRegion(pageTitle,regionID){
	
	var action = pageTitle.substring( 0, pageTitle.indexOf(' '));
	
	if( action == "ADD" )
	{
		return;
	}
	else
	{
		new Ajax.Request('/libs/GetContactsInRegion.php', {
			method: 'get',
  			parameters: 'regionID=' + regionID,
  			onSuccess: function(transport){

            	xmlDoc = transport.responseXML;
				var xmlOptions = xmlDoc.getElementsByTagName('optionItem');
	
				var selectElement = document.getElementById('nameList');
				selectElement.innerHTML = "<option value='-----'>Please choose</option><option value='-----'>-----</option>";
			
				if (window.ActiveXObject){//if it is IE
					var o =  new Option();
					o.text = "Please choose";
					selectElement.add(o);
					o =  new Option();
					o.text = "-----";
					selectElement.add(o);
				}
			
				for(var i = 0; i < xmlOptions.length; i++){
				
					var o = new Option(
						xmlOptions[i].getElementsByTagName('name')[0].firstChild.data,
						xmlOptions[i].getElementsByTagName('id')[0].firstChild.data,
						false,
						false
					);
				
					selectElement.options[selectElement.options.length] = o;
				}
			
			    //Make the drop down list and the text preceeding it visible.
				document.getElementById('nameRow').style.display='inline';
				document.getElementById('contactName').style.display='inline';
    		}
  		});
	}
}

/* 
Sets the value of text boxes according to the contents of an XML document. If there is no
value for a textbox in the XML document an exception is raised. Therefore, the setting of
each textbox is carried out within a try, catch block. If an exception was not raised the
textbox is set to the value in the XML document. If an exception was raised the value of 
the textbox is set to null.
*/

function populateFields(xmlOptions,fields)
{
	var i;
	
	for( i=0; i < fields.length; i++ )
	{
		try
		{
			//Retrieve the value for the textbox from the XML document. If a value cannot be found for the textbox
			//set the textbox to empty.
			document.getElementById(fields[i]).value = xmlOptions[0].getElementsByTagName(fields[i])[0].firstChild.data;
		}
		catch( err )
		{
			try
			{
				document.getElementById(fields[i]).value = "";
			}
			catch( error )
			{
			}
		}
	}
}

/*
Retrieve details for the user with an account_id of accountID from the database and populate the form with its data.
*/

function getContactDetails(accountID,pageTitle){
	
	new Ajax.Request('/libs/GetContactDetails.php', {
		method: 'get',
  		parameters: 'accountID=' + accountID,
  		onSuccess: function(transport){

			xmlDoc = transport.responseXML;
			
			var xmlOptions = xmlDoc.getElementsByTagName('contact');
			
			fields = new Array('forename','surname','company','position','address','telephone','email');
			
			//Populate the text fields with data from the database or set to null.
			populateFields(xmlOptions,fields);
			
			//If the current page is used for removing a contact then gray out the text fields.
			var action = pageTitle.substring( 0, pageTitle.indexOf(' '));
			
			if( action == "DELETE" )
			{
				var i = 0;
	
				for( i=0; i < fields.length; i++ )

				{
					document.getElementById(fields[i]).disabled = true;
				}
				
				document.getElementById('forenameHidden').value = 
					xmlOptions[0].getElementsByTagName('forename')[0].firstChild.data;
				
				document.getElementById('surnameHidden').value = 
					xmlOptions[0].getElementsByTagName('surname')[0].firstChild.data;
			}
  		}
	});
}

function getJobInfo(jobID,pageTitle)
{
	new Ajax.Request('/libs/GetJobInfo.php', {
		method: 'get',
  		parameters: 'jobID=' + jobID,
  		onSuccess: function(transport){
			
			xmlDoc = transport.responseXML;
			
			document.getElementById('jobID').value = jobID;
			
			var xmlOptions = xmlDoc.getElementsByTagName('job');
			
			$disableFields = 0;
			
			//If the current page is used for removing a contact then gray out the text fields.
			var action = pageTitle.substring( 0, pageTitle.indexOf(' '));
			
			if( action == "MARK" )
			{
				$disableFields = 1;
			}
			
			var salaryFromDatabase = xmlOptions[0].getElementsByTagName('salary')[0].firstChild.data;
			
			var salary = "";
			var salaryExtras = "";
			var idxSalaryExtra = salaryFromDatabase.indexOf( '+' );
			
			if( idxSalaryExtra > 0 )
			{
				var numChars = salaryFromDatabase.length - idxSalaryExtra + 2;
				salaryExtras = salaryFromDatabase.substr( idxSalaryExtra + 2, numChars );
				salary = salaryFromDatabase.substr( 0, idxSalaryExtra - 1 );
				document.getElementById('salaryExtras').value = salaryExtras;
			}
			else
			{
				salary = salaryFromDatabase;
			}
			
			var idx = salary.indexOf('~');
			var specificSal = new RegExp("\\d", "i");
			
			var salaryFrom;
			var salaryTo;
				
			if( idx != -1 )
			{
				salaryFrom = salary.substring( 0, idx );
				salaryFrom = salaryFrom.replace( '&pound;','' );
				salaryTo = salary.substring( idx + 1, salary.indexOf('K') );
				salaryTo = salaryTo.replace( '&pound;','' );
				
				for( var i = 0; i < document.editContactForm.salaryFrom.length; i++ )
				{
					if( document.editContactForm.salaryFrom.options[i].value == salaryFrom )
					{
						document.editContactForm.salaryFrom.options[i].selected = true;
					}
				}
				
				for( var i = 0; i < document.editContactForm.salaryTo.length; i++ )
				{
					if( document.editContactForm.salaryTo.options[i].value == salaryTo )
					{
						document.editContactForm.salaryTo.options[i].selected = true;
					}
				}
				
				salary = "range";
			}
			else if( specificSal.test( salary ) )
			{
				document.getElementById('specificSalary').value = salary;
				salary = "Specific";
			}
			
			var radioGrp = document.editContactForm.salary;
			
			for( var i = 0; i < radioGrp.length; i++ )
			{	
				if( radioGrp[i].value == salary )
				{
					radioGrp[i].checked = true;
				}
			}
			
			salaryExpandCollapse('salaryTable','range','specificSalaryHdn','Specific');
			
			fields = new Array('contactName','contactTelephone','contactFax','contactEmail','contactWebsite','jobTitle','jobCompany');
			
			//Populate the text fields with data from the database or set to null.
			populateFields(xmlOptions,fields);
			
			var oEditor = FCKeditorAPI.GetInstance('notes');
			oEditor.SetHTML(xmlOptions[0].getElementsByTagName('notes')[0].firstChild.data);
			
			try
			{
				var lenTags = xmlOptions[0].getElementsByTagName('jobRegion').length;
				
				for( var i = 0; i < lenTags; i++ )
				{
					var lenDropDown = document.getElementById('location').length;
					
					for( var j = 0; j < lenDropDown; j++ )
					{
						if( document.editContactForm.location.options[j].value == 
							xmlOptions[0].getElementsByTagName('jobRegion')[i].firstChild.data )
						{
							document.editContactForm.location.options[j].selected = true;
						}
					}
				}
			}
			catch( err )
			{
			}
			
			try
			{
				var len = xmlOptions[0].getElementsByTagName('jobCategory').length;
				
				for( var i = 0; i < len; i++ )
				{
					var lenDropDown = document.getElementById('category').length; 
					
					for( var j = 0; j < lenDropDown; j++ )
					{
						if( document.editContactForm.category.options[j].value == 
							xmlOptions[0].getElementsByTagName('jobCategory')[i].firstChild.data )
						{
							document.editContactForm.category.options[j].selected = true;
						}
					}
				}
			}
			catch( err )
			{
			}
			
			try
			{
				for( var i = 0; i < document.editContactForm.status.length; i++ )
				{
					if( document.editContactForm.status.options[i].value == xmlOptions[0].getElementsByTagName('selectedStatus')[0].firstChild.data )
					{
						document.editContactForm.status.options[i].selected = true;
					}
				}
			}
			catch( err )
			{
				for( var i = 0; i < document.editContactForm.status.length; i++ )
				{
					if( document.editContactForm.status.options[i].value == "Please choose" )
					{
						document.editContactForm.status.options[i].selected = true;
					}
				}
			}
			
			try
			{
				document.getElementById('jobReference').value = xmlOptions[0].getElementsByTagName('jobReference')[0].firstChild.data;
				
				if( $disableFields )
				{
					document.getElementById('jobReference').disabled = true;
				}
			
				document.editContactForm.showReference.checked = true;
				checkBoxExpandCollapse('showReference','jobReference');
			}
			catch( err )
			{
				document.getElementById('jobReference').value = "";
			}
			
			var jobStart = xmlOptions[0].getElementsByTagName('dateAdded')[0].firstChild.data;
			var dateComponents = jobStart.split("-");
			
			for( var i = 0; i < document.editContactForm.jobStartYear.length; i++ )
			{
				if( document.editContactForm.jobStartYear.options[i].value == dateComponents[0] )
				{
					document.editContactForm.jobStartYear.options[i].selected = true;
				}
			}
			
			for( var i = 0; i < document.editContactForm.jobStartMonth.length; i++ )
			{
				if( document.editContactForm.jobStartMonth.options[i].value == dateComponents[1] )
				{
					document.editContactForm.jobStartMonth.options[i].selected = true;
				}
			}
			
			for( var i = 0; i < document.editContactForm.jobStartDay.length; i++ )
			{
				if( document.editContactForm.jobStartDay.options[i].value == dateComponents[2] )
				{
					document.editContactForm.jobStartDay.options[i].selected = true;
				}
			}
			
			try
			{
				var jobClose = xmlOptions[0].getElementsByTagName('closingDate')[0].firstChild.data;
				
				if( jobClose != "0000-00-00" )
				{
					var dateComponents = jobClose.split("-");
			
					for( var i = 0; i < document.editContactForm.jobCloseYear.length; i++ )
					{
						if( document.editContactForm.jobCloseYear.options[i].value == dateComponents[0] )
						{
							document.editContactForm.jobCloseYear.options[i].selected = true;
						}
					}
			
					for( var i = 0; i < document.editContactForm.jobCloseMonth.length; i++ )
					{
						if( document.editContactForm.jobCloseMonth.options[i].value == dateComponents[1] )
						{
							document.editContactForm.jobCloseMonth.options[i].selected = true;
						}
					}
			
					for( var i = 0; i < document.editContactForm.jobCloseDay.length; i++ )
					{
						if( document.editContactForm.jobCloseDay.options[i].value == dateComponents[2] )
						{
							document.editContactForm.jobCloseDay.options[i].selected = true;
						}
					}
				
					document.editContactForm.jobClose.checked = true;
					checkBoxExpandCollapse('jobClose','selectClosingDate');
				}
			}
			catch( err )
			{
				document.editContactForm.jobClose.checked = false;
				checkBoxExpandCollapse('jobClose','selectClosingDate');
			}
			
			try
			{
				document.getElementById('logo').src = "../../../../images/jobImages/" + xmlOptions[0].getElementsByTagName('logo')[0].firstChild.data;
			}
			catch( err )
			{
			}
			
			if( $disableFields )
			{
				var i = 0;
	
				for( i=0; i < fields.length; i++ )
				{
					document.getElementById(fields[i]).disabled = true;
				}
			}
		}
	});
}

function getDownloadInfo(categoryID)
{
	new Ajax.Request('/includes/GetDownloadInfo.php', {
		method: 'get',
  		parameters: 'categoryID=' + categoryID,
  		onSuccess: function(transport){
			
			xmlDoc = transport.responseXML;
			
			//document.getElementById('jobID').value = jobID;
			
			//var xmlOptions = xmlDoc.getElementsByTagName('job');
		}
	});
}

function processTextField(dataName,dataValue)
{
	try
	{
		//Retrieve the value for the textbox from the XML document. If a value cannot be found for the textbox
		//set the textbox to empty.
		document.getElementById(dataName).value = dataValue;
	}
	catch( err )
	{
		try
		{
			document.getElementById(dataName).value = "";
		}
		catch( error )
		{
		}
	}
}

function processOptionList(dataName,dataValue)
{
	try
	{
		var len = document.getElementsByName(dataName)[0].options.length;
		
		for( var i = 0; i < len; i++ )
		{
			if( document.getElementsByName(dataName)[0].options[i].value == dataValue )
				document.getElementsByName(dataName)[0].options[i].selected = true;
		}
	}
	catch( err )
	{
		for( var i = 0; i < len; i++ )
		{
			if( document.getElementsByName(dataName)[0].options[i].value == "Please choose" )
			{
				document.getElementsByName(dataName)[0].options[i].selected = true;
			}
		}
	}
}

function processRadioList(dataName,dataValue)
{
	var radioGrp = document.getElementsByName(dataName);
	
	var checked = 0;
	
	for( var i = 0; i < radioGrp.length; i++ )
	{	
		if( radioGrp[i].value == dataValue )
		{
			radioGrp[i].checked = true;
			checked = 1;
		}
	}
	
	if( !checked )
		radioGrp[0].checked = true;
}

function processChk(dataName,dataValue)
{
	var idxComma = dataValue.indexOf(',');
	var id = dataValue.substring(0,idxComma);
	idxComma++;
	var len = dataValue.length - idxComma;
	var status = dataValue.substring(idxComma,dataValue.length);
	
	if( status == "1" )
	{
		document.getElementById(dataName).checked = true;
		document.getElementById(id).style.display = "";
	}
	else
	{
		document.getElementById(dataName).checked = false;
		document.getElementById(id).style.display = "none";
	}
}

function expandCollapse(dataName,dataValue)
{
	if( dataValue === undefined )
	{
		var test = document.getElementById(dataName).style.display;
		var regexDisplay = new RegExp("inline", "i");
	
		if( ( test.length == 0 ) || ( regexDisplay.test(test) ) )
			document.getElementById(dataName).style.display = "none";
		else
			document.getElementById(dataName).style.display = "";
	}
	else
	{
		if( dataValue == 1 )
			document.getElementById(dataName).style.display = "";
		else
			document.getElementById(dataName).style.display = "none";
	}
}

function processImg(dataName,dataValue)
{
	try
	{
		var display = 0;
		
		if( dataValue.length != 0 )
		{
			// Set the src of the image so that it displays.
			document.getElementById(dataName).src = dataValue;
			display = 1;
		}
		
		// Set a hidden input field to the value of the src so that
		// the image can be redisplayed from PHP.
		var hiddenImgTextField = dataName + "Txt";
		
		var hiddenImgArea = dataName + "Hdn";
		expandCollapse( hiddenImgArea, display );
		
		document.getElementById(hiddenImgTextField).value = dataValue;
	}
	catch( error )
	{
	}
}

/*
 * This function will not return until (at least) the specified number of milliseconds have passed.
 * It does a busy-wait loop.
 */

function pause( numberMillis ) 
{
   var now = new Date();
   var exitTime = now.getTime() + numberMillis;
   
   while( true ) 
   {
      now = new Date();
      
	  if( now.getTime() > exitTime )
          return;
    }
}

function selectCheckBoxes( htmlIdName, idstring )
{
	var categories = document.getElementsByName( htmlIdName );
	var categoriesLen = categories.length;
	
	var ids = idstring.split( "," );
	var idsLen = ids.length;
	
	for( var i = 0; i < idsLen; i++ )
	{
		for( var j = 0; j < categoriesLen; j++ )
		{
			if( categories[j].value == ids[i] )
				categories[j].checked = true;
		}
	}
}

function createCheckBoxes( mainCategoryDiv, subCategoryDiv, dropDownList, invokingDropDown, primaryKey, 
	configFile, recentlyExecutedSqlFilename, pageTitle, polymor )
{
	var htmlIdName = mainCategoryDiv + "ChkArr[]";
	
	var regexStatus = new RegExp( "[A-Z]", "i" );
	
	var ids = "";
	var checkedStatus = true;
						
	if( regexStatus.test( polymor ) )
		checkedStatus = polymor;
	else
		ids = polymor;
	
	if( checkedStatus == false )
	{
		var subCat = document.getElementById( subCategoryDiv );
		subCat.style.display = "none";
		subCat.innerHTML = "";
		return;
	}
	
	new Ajax.Request( '/libs/PopulateDropDownList.php', {
		method: 'get',
		parameters: 'primaryKey=' + primaryKey + '&' + 'targetDropDownList=' + dropDownList + '&' + 
			'sourceDropDownList=' + invokingDropDown + '&' + 'filename=' + recentlyExecutedSqlFilename + 
			'&' + 'configFile=' + configFile,
  		onComplete: function( transport ){
			
			xmlDoc = transport.responseXML;
			var xmlOptions = xmlDoc.getElementsByTagName('optionItem');
			
			var subDiv = document.getElementById( subCategoryDiv );
			
			if( xmlOptions.length > 5 )
			{
				subDiv.style.height = "93px";
				
				if( window.ActiveXObject )
				{
					subDiv.style.overflowX = "hidden";
					subDiv.style.overflowY = "scroll";
				}
				else
					subDiv.style.overflow = "-moz-scrollbars-vertical";
			}
			else
			{
				subDiv.style.height = "auto";
				subDiv.style.width = "auto";
			}
			
			for( var i = 0; i < xmlOptions.length; i++ ){
				
				var checkBoxName = document.createTextNode
					( xmlOptions[i].getElementsByTagName('title')[0].firstChild.data );

				var cb = document.createElement( "input" );
				cb.type = "checkbox";
				cb.name = htmlIdName;
				cb.id = htmlIdName;
				cb.value = xmlOptions[i].getElementsByTagName('id')[0].firstChild.data;
				
				subDiv.appendChild( checkBoxName );
				subDiv.appendChild( cb );
				var br = document.createElement( "br" );
				subDiv.appendChild( br );
			}
			
			if( ids.length > 0 )
				selectCheckBoxes( htmlIdName, ids );
		}
  	});
}

function populateDropDownLists( array, invokingDropDown, primaryKey, configFile, $recentlyExecutedSqlFilename, 
	pageTitle )
{
	var dropDownListToHidden = array.split( "|" );
	
	var lenDropDownListToHidden = dropDownListToHidden.length;
	
	var action = pageTitle.substring( 0, pageTitle.indexOf(' '));
	
	if( action == "ADD" )
		lenDropDownListToHidden = 1;
	
	for( var i = 0; i < lenDropDownListToHidden; i++ )
	{
		var idxFirstTilde = dropDownListToHidden[i].indexOf( '~' );
		var dropDownList = dropDownListToHidden[i].substr( 0, idxFirstTilde );
		idxFirstTilde++;
		var idxNextTilde = dropDownListToHidden[i].indexOf( '~', idxFirstTilde );
		var numChars = dropDownListToHidden[i].length - idxFirstTilde;
		var hiddenData = dropDownListToHidden[i].substr( idxFirstTilde, numChars );
		
		populateDropDownList( dropDownList, hiddenData, invokingDropDown, primaryKey, configFile, 	
			$recentlyExecutedSqlFilename );
	}
}

function populateDropDownList( dropDownList, hiddenData, invokingDropDown, primaryKey, configFile, 			
	recentlyExecutedSqlFilename, pageTitle )
{
	if( pageTitle !== undefined )
	{
		var action = pageTitle.substring( 0, pageTitle.indexOf(' '));
		
		if( action == "ADD" )
			return;
	}
	
	new Ajax.Request( '/libs/PopulateDropDownList.php', {
		method: 'get',
  		parameters: 'primaryKey=' + primaryKey + '&' + 'targetDropDownList=' + dropDownList + '&' + 
			'sourceDropDownList=' + invokingDropDown + '&' + 'filename=' + recentlyExecutedSqlFilename + '&' +
			'configFile=' + configFile,
  		onSuccess: function( transport ){
			
			xmlDoc = transport.responseXML;
			var xmlOptions = xmlDoc.getElementsByTagName('optionItem');
			
			var selectElement = document.getElementById(dropDownList);
			
			if( selectElement.options.length != 2 )
			{
				selectElement.innerHTML = "<option value='" + selectElement.options[0].value + "'>" + 
					selectElement.options[0].value + "</option>" + "<option value='-----'>" + 
					selectElement.options[1].value + "</option>";
			
				if( window.ActiveXObject ){//if it is IE
					var o =  new Option();
					o.text = selectElement.options[0].value;
					selectElement.add(o);
					o =  new Option();
					o.text = selectElement.options[1].value;
					selectElement.add(o);
				}
			}
			
			for( var i = 0; i < xmlOptions.length; i++ ){
				var o = new Option(
					xmlOptions[i].getElementsByTagName('title')[0].firstChild.data,
					xmlOptions[i].getElementsByTagName('id')[0].firstChild.data,
					false,
					false
				);
				
				selectElement.options[selectElement.options.length] = o;
			}
			
			var len = document.getElementsByName(dropDownList)[0].options.length;
			
			// Greater than two because there is the default selected option which is there
			// purely to explain to the user what to do and then a divider/spacer between that
			// option and the valid options.
			
			if( len > 2 )
				//Make the drop down list and the text preceeding it visible.
				document.getElementById(hiddenData).style.display='inline';
			else
				document.getElementById(hiddenData).style.display='none';
		}
  	});
}

function populateBusinessDirectoryElements( mainCategory, subCategory, invokingDropDown, primaryKey, 
	configFile, recentlyExecutedSqlFilename, pageTitle, categoryCount )
{
	var categories = document.getElementsByName( mainCategory );
	var categoriesLen = categories.length;
	
	if( categoriesLen != 0 )
		categoriesLen = categoryCount;
	
	for( var i = 0; i < categoriesLen; i++ )
	{
		if( categories[i].checked == true )
		{
			categories[i].checked = false;
			var subCat = categories[i].nextSibling.nextSibling.nextSibling.firstChild;
			subCat.style.display = "none";
			subCat.innerHTML = "";
		}
	}
	
	var mainCatId = document.getElementById( 'mainCategoryOpt' ).value;
	
	for( var i = 0; i < categoriesLen; i++ )
	{
		if( categories[i].value == mainCatId )
		{
			categories[i].checked = true;
		}
	}
	
	populateAllElements( primaryKey, configFile );
	
	new Ajax.Request( '/libs/PopulateDropDownList.php', {
		method: 'get',
  		parameters: 'primaryKey=' + primaryKey + '&' + 'targetDropDownList=' + mainCategory + '&' + 
			'sourceDropDownList=' + invokingDropDown + '&' + 'filename=' + 
			recentlyExecutedSqlFilename + '&' + 'configFile=' + configFile,
  		onSuccess: function( transport ){
			
			xmlDoc = transport.responseXML;
			var mainXmlOptions = xmlDoc.getElementsByTagName('optionItem');
			
			var categoryDetails = Array();
		
			for( var i = 0; i < mainXmlOptions.length; i++ )
			{
				var main_cat_id = mainXmlOptions[i].getElementsByTagName('main_cat_id')[0].firstChild.data;
				var main_cat_name = mainXmlOptions[i].getElementsByTagName('title')[0].firstChild.data;
				var sub_cat_id = mainXmlOptions[i].getElementsByTagName('sub_cat_id')[0].firstChild.data;
				
				var updated = 0;
				
				for( var j = 0; j < categoryDetails.length; j++ )
				{
					if( categoryDetails[j].substr( 0, categoryDetails[j].indexOf( '~' ) ) == main_cat_id )
					{
						categoryDetails[j] += "," + sub_cat_id;
						updated = 1;
					}
				}
				
				if( !updated )
					categoryDetails[categoryDetails.length] = main_cat_id + "~" + main_cat_name + "~" + sub_cat_id;
			}
			
			var len = categoryDetails.length;
			
			for( var i = 0; i < len; i++ )
			{
				var temp = categoryDetails[i].split( '~' );
				
				var mainCatId = temp[0];
				var mainCatName = temp[1];
				var subCatIds = temp[2];
				
				for( var j = 0; j < categoriesLen; j++ )
				{
					if( categories[j].value == mainCatId )
						categories[j].checked = true;
				}
				
				var divName = mainCatName + "sub";
				 
				createCheckBoxes( mainCatName, divName, 'subCategoryOpt', 'mainCategoryOpt', 
					mainCatId, configFile, recentlyExecutedSqlFilename, pageTitle, subCatIds );
			}
		}
  	});
}

function populateAllElements(primaryKey,configFile)
{
	new Ajax.Request('/libs/PopulateAllElements.php', {
		method: 'get',
  		parameters: 'primaryKey=' + primaryKey + '&' + 'configFile=' + configFile,
  		onSuccess: function(transport){
			
			var nodes = transport.responseXML.documentElement.childNodes;
			
			for( i = 0; i < nodes.length; i++ ) 
			{
				try
				{
					if( nodes.item(i).firstChild.nodeValue == "null" )
						nodes.item(i).firstChild.nodeValue = "";
					
					if( nodes.item(i).nodeName != "#text" )
					{
						var regexTxt = new RegExp("txt", "i");
						var regexOpt = new RegExp("opt", "i");
						var regexRdo = new RegExp("rdo", "i");
						var regexChk = new RegExp("chk", "i");
						var regexHdn = new RegExp("hdn", "i");
						var regexFck = new RegExp("fck", "i");
						var regexArr = new RegExp("arr", "i");
						var regexImg = new RegExp("img", "i");
						
						if (regexTxt.test(nodes.item(i).nodeName))
							processTextField( nodes.item(i).nodeName, nodes.item(i).firstChild.nodeValue );
						else if (regexOpt.test(nodes.item(i).nodeName))
							processOptionList( nodes.item(i).nodeName, nodes.item(i).firstChild.nodeValue );
						else if (regexRdo.test(nodes.item(i).nodeName))
							processRadioList( nodes.item(i).nodeName, nodes.item(i).firstChild.nodeValue );
						else if (regexChk.test(nodes.item(i).nodeName))
							processChk( nodes.item(i).nodeName, nodes.item(i).firstChild.nodeValue );
						else if (regexHdn.test(nodes.item(i).nodeName))
							expandCollapse( nodes.item(i).nodeName, nodes.item(i).firstChild.nodeValue );
						else if (regexFck.test(nodes.item(i).nodeName))
						{
							var oEditor = FCKeditorAPI.GetInstance(nodes.item(i).nodeName);
							oEditor.SetHTML(nodes.item(i).firstChild.nodeValue);
						}
						else if (regexArr.test(nodes.item(i).nodeName))
						{
							var nodeName;
							
							nodeName = nodes.item(i).nodeName;
							nodeName += "[]";
							
							processOptionList( nodeName, nodes.item(i).firstChild.nodeValue );
						}
						else if (regexImg.test(nodes.item(i).nodeName))
							processImg( nodes.item(i).nodeName, nodes.item(i).firstChild.nodeValue );
					}
				}
				catch( err )
				{
				}
			}
		}
	});
}

function getCategoryImages( categoryType )
{
	new Ajax.Request('/libs/GetCategoryImages.php', {
		method: 'get',
  		parameters: 'status=' + categoryType,
  		onSuccess: function(transport){

            xmlDoc = transport.responseXML;
			var xmlOptions = xmlDoc.getElementsByTagName('optionItem');
			
			var selectElement = document.getElementById('currentImages');
			selectElement.innerHTML = "<option value='-----'>Please choose</option>" +
				"<option value='-----'>-----</option>";
			
			if (window.ActiveXObject){//if it is IE
				var o =  new Option();
				o.text = "Please choose";
				selectElement.add(o);
				o =  new Option();
				o.text = "-----";
				selectElement.add(o);
			}
			
			for(var i = 0; i < xmlOptions.length; i++){
				
				var o = new Option(
					xmlOptions[i].getElementsByTagName('name')[0].firstChild.data,
					xmlOptions[i].getElementsByTagName('id')[0].firstChild.data,
					false,
					false
				);
				
				selectElement.options[selectElement.options.length] = o;
			}
			
			//Make the drop down list and the text preceeding it visible.
			document.getElementById('currentImagesHdn').style.display='inline';
			document.getElementById('currentImagesTxt').style.display='inline';
    	}
  	});
}