
	//Global variables
	var addedRows = new Array();		//needed for the functions addRow(), removeAddedRows() and removeRow()
	

	/**
	@description				removes all options for the select box
	@param object selectBox		selectBox object
	*/
	function removeOptions(selectbox){
		for(i=selectbox.options.length-1;i>=0;i--)
			selectbox.remove(i);
	}


	/**
	@description				adds a new option field to a select box
	@param object selectBox		selectBox object
	@param array optionField	key is value of option and array value is description
	*/
	function addOption(selectBox,optionFields){
		for (var key in optionFields) {
			var optionField = document.createElement("option");
			optionField.text= optionFields[key];
			optionField.value= key;
			try {
				selectBox.add(optionField, null);
			}
			catch(ex) {
				selectBox.add(optionField);
			}
		}
	}


	/** 
	@description		removes the values/description of the affiliateLinks select
						and replaces them with the values of optionFields
	@param array		keys are the values, values are the descriptions of the select box
	*/
	function changeAffiliates(optionFields, affiliateId) {
		
		var selectBox = document.getElementById('affiliateId');
		removeOptions(selectBox);
		addOption(selectBox,optionFields);

		if(affiliateId!==false) {
			var i = 0;
			for ( key in optionFields ) {
				if(key==affiliateId) {
					selectBox.selectedIndex = i;
					break;
				}
				i++;
			}
		}
	} 


	/*				Old functions					*/
	function addSelectBox(parent, name, optionFields) {

		var select = document.createElement('SELECT');
		select.name = name;
		select.id = name;
		parent.appendChild(select);
	
		for (keyVar in optionFields) {
			var option = document.createElement('option');
			option.value = keyVar;
			option.text = optionFields[keyVar];
			select.options.add(option);
		}

	}


	function addEntryOutput(optionFields) {
	
		var iteration = 1;
		var name = 'entry';
		var totalElements = document.forms['output'].elements.length;
		for(var i=0; i<totalElements; i++) {
			if(document.forms['output'].elements[i].name==(name + iteration)) {
				iteration++;
				i = 0;
			}
		}
		if(iteration>3)		return false;
		addSelectBox(document.getElementById('outputDiv'), (name + iteration), optionFields);

	}



	function deleteEntryOutput() {
	
		var name = false;
		var totalElements = document.forms['output'].elements.length - 1;

		if(totalElements<4)		return false;

		for(var i=totalElements; i>=0; i--) {
			var string = document.forms['output'].elements[i].name;
			if(string!=undefined && string.search(/entry[2-9]{1}/)!=-1) {
				name = document.forms['output'].elements[i].name;
				break;
			}
		}

		if(name!==false) {
			parentObject = document.getElementById('outputDiv');
			parentObject.removeChild(document.getElementById(name));
			return true;
		}
		return false;
	}
	/*				End old functions					*/


	/**
	@description							adds a new row with 2 colums to table [tableId], one with description
											one with a formElement
	@param string tableId					id of the table
	@param string rowId						id of the to be set row to identify
	@param string description				description that goes in the first column
	@param string className					name of the class
	@param object formElement				form element
	*/
	function addRow(tableId, rowIndex, rowId, description, className, formElement) {

		var tbl = document.getElementById(tableId);

		if(rowIndex===false)
			var rowIndex = tbl.rows.length;

		var row = tbl.insertRow(rowIndex);
		row.id = rowId;
		addedRows[addedRows.length] = rowId;
  
		var firstCell = row.insertCell(0);  
		firstCell.className = className;
		firstCell.align = 'right';
		firstCell.appendChild(document.createTextNode(description));

		var secondCell = row.insertCell(1);
		secondCell.className = className;
		secondCell.appendChild(formElement);

	}
 

	function addRowHeading(tableId, rowIndex, rowId, heading, headingType, className) {

		var tbl = document.getElementById(tableId);

		if(rowIndex===false)
			var rowIndex = tbl.rows.length;

		var row = tbl.insertRow(rowIndex);
		row.id = rowId;
		addedRows[addedRows.length] = rowId;

		var h2 = document.createElement(headingType);
		h2.appendChild(document.createTextNode(heading));
		h2.className = className;
  
		var firstCell = row.insertCell(0);  
		firstCell.className = className;
		firstCell.align = 'center';
		firstCell.colSpan = 2;
		firstCell.appendChild(h2);

	}


	function createInputField(name, value, size, maxSize) {

		var input = document.createElement('input');
		input.type = 'text';
		input.name = name;
		input.value = value;
		input.size = size;
		input.maxlength = maxSize;
		return input;

	}


	function createCheckbox(name, value, checked) {

		var input = document.createElement('input');
		input.type = 'checkbox';
		input.name = name;
		input.value = value;
		input.checked = checked;
		return input;

	}


	function createTextArea(name, value, cols, rows, wrap) {

		var textarea = document.createElement('textarea');
		textarea.name = name;
		textarea.value = value;
		textarea.cols = cols;
		textarea.rows = rows;
		textarea.wrap = wrap;
		return textarea;

	}


	function createFileField(name) {

		var fileField = document.createElement('input');
		fileField.type = 'file';
		fileField.name = name;
		return fileField;

	}


	function createSelectBox(name, size, multiple, optionFields) {

		var selectBox = document.createElement("SELECT");
		selectBox.name= name;
		selectBox.size= size;
		
		addOption(selectBox,optionFields);
		return selectBox;

	}


	/**
	@description						removes all rows added by the function addRow() to table tableId
	@param string tableId			id of the table
	*/
	function removeAddedRows(tableId) {

		var tbl = document.getElementById(tableId);
		var lastRow = tbl.rows.length;
		for(var i=(tbl.rows.length-1); i>=0; i--) {
			if(searchArray(addedRows, tbl.rows[i].id)!==false)
				tbl.deleteRow(i);
		}
		addedRows = new Array();
	}


	/**
	@description			remove row with id attribute rowId from table tableId
	*/
	function removeRow(tableId, rowId) {
		
		var tbl = document.getElementById(tableId);
		var lastRow = tbl.rows.length;
		for(var i=(tbl.rows.length-1); i>=0; i--) {
			if(tbl.rows[i].id==rowId)	tbl.deleteRow(i);
		}
		if(addedRows.length==0)		return true;

		var newAddedRows = new Array();
		for(var i=0; i<addedRows.length; i++) {
			if(addedRows[i]!=rowId)		newAddedRows[newAddedRows.length] = addedRows[i];
		}
		addedRows = newAddedRows;
	}


	/**
	@description				disables/enables the thumbnail settings depending on value in table tableId
	@param string tableId		id of table
	@param string value			photos/videos
	*/
	function disableThmFields(tableId, type, values) {

		switch(type) {

			case 'photos':

				var formElement = createSelectBox('croppingMethod'
												  , 1
												  , false
												  , {'- - Select - -':'- - Select - -'
													, 'Best Fit':'Best Fit'
													, 'Fixed dimensions':'Fixed dimensions'})
				addRow(tableId, false, 'croppingMethod', 'Cropping method: ', '', formElement);		
				var formElement = createInputField('thmWidth', values['thmWidth'], 4, 4);
				addRow(tableId, false, 'thmWidthRow', 'Thumbnails width: ', '', formElement);
				var formElement = createInputField('thmHeight', values['thmHeight'], 4, 4);
				addRow(tableId, false, 'thmHeightRow', 'Thumbnails height: ', '', formElement);
				var formElement = createInputField('thmPercentage', values['thmPercentage'], 3, 3);
				addRow(tableId, false, 'thmPercentageRow', 'Thumbnails percentage: ', '', formElement);
				break;

			default: 
			case 'streamingVideos':
			case 'videos':
				removeAddedRows(tableId);
				break;

		}
	}



	function mailTemplates(tableId, addTemplate, type, name, values) {

		if(addTemplate==true) {
			addRowHeading(tableId, false, type + 'Heading', name, 'h2', 'dataMailTemplate');
			var formElement = createCheckbox(type + 'Required', 1, values['required']==1 ? true : false);
			addRow(tableId, false, type + 'Required', 'Required: ', 'data', formElement);
			var formElement = createInputField(type + 'Amount', values['amount'], 4, 10);
			addRow(tableId, false, type + 'Amount', 'Amount: ', 'data', formElement);
			var formElement = createTextArea(type + 'Template', values['template'], 60, 10, 'soft');
			addRow(tableId, false, type + 'Template', 'Template: ', 'data', formElement);		
		} else {			
			removeRow(tableId, type + 'Heading');
			removeRow(tableId, type + 'Required');
			removeRow(tableId, type + 'Amount');
			removeRow(tableId, type + 'Template');
		}
	}


	/**
	@description				reverse the status of a checkbox with elementName
								in the form element that's related to object
	@param HtmlObject object	form element
	@param string elementName	name of the checkbox element which needs to be reverse					
	*/
	function reverseCheckboxStatus(object, elementName) {
		var formRef = object.form;
		for(var i=0; i<formRef.elements.length; i++) { 	
			if(formRef.elements[i].name==elementName) {
				if(formRef.elements[i].checked===true)
					formRef.elements[i].checked = false;
				else	formRef.elements[i].checked = true;

			}
		}
	}


	/**
	@description			inserts text at the place of the cursor inside the textarea with id areaId
	*/
	function insertTextTextArea(textAreaId, text) { 

		var txtarea = document.getElementById(textAreaId); 
		var scrollPos = txtarea.scrollTop; 
		var strPos = 0; 
		var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ? "ff" : (document.selection ? "ie" : false ) ); 
		if (br == "ie") { 
			txtarea.focus(); 
			var range = document.selection.createRange(); 
			range.moveStart ('character', -txtarea.value.length); 
			strPos = range.text.length; 
		} else if (br == "ff") 
			strPos = txtarea.selectionStart; 
		var front = (txtarea.value).substring(0,strPos); 
		var back = (txtarea.value).substring(strPos,txtarea.value.length); 
		txtarea.value=front+text+back; 
		strPos = strPos + text.length; 
		if (br == "ie") { 
			txtarea.focus();
			var range = document.selection.createRange(); 
			range.moveStart ('character', -txtarea.value.length); 
			range.moveStart ('character', strPos); 
			range.moveEnd ('character', 0); 
			range.select(); 
		} else if (br == "ff") { 
			txtarea.selectionStart = strPos; 
			txtarea.selectionEnd = strPos; 
			txtarea.focus(); 
		} 
		txtarea.scrollTop = scrollPos; 

	} 