function makeTagAttribute(name , value)
{
	if(name != null && value != null){
		return " "+name+"="+makeQoutedStr(value);
	}else{
		return "";
	}
}

function makeQoutedStr(value)
{
	return "'"+value+"'";
}

/**
* Build select list form element.
* @param string name : select list name and id
* @param array : array of options {selected:boolean, value:string, data:string}
* @param string onclick : onclick/onchange event for select list.
* @param number size
* @param boolean init : add select option
* @param boolean allow_multiple
* @param boolean no item option
* @return string
**/
var selValue = "";

function rd_buildSelectList(name, options, onclick, size, add_select_option, allow_multiple, no_item_option)
{
	selValue = '';
	var str = '';
	allow_multiple = (allow_multiple == true) ? 'allow_multiple' : '';
	size = (size > 0) ? size:'';
	var select_0 = "<select "+allow_multiple+" size=\""+size+"\" name=\""+name+"\" onclick=\""+onclick+"\">";
	var select_1 = "</select>";
	if(options.length>0)
	{
		str+= select_0;
		if(add_select_option == null || add_select_option == true)
			str+= "<option selected>"+RS_SELECT_AN_ITEM+"</option>\n";
		for(i=0;i<options.length;i++)
		{
			var option = options[i];
			var selected_text = (option.selected) ? 'selected' : '';
			if(option.selected == true)
				selValue = option.value;
			str+= "<option value=\""+option.value+"\" "+selected_text+">"+option.data+"</option>\n";
		}
		str+= select_1;
	}
	else if(no_item_option == true)
	{
		str+= select_0;
		str+= "<option selected>"+RS_NO_ITEMS+"</option>\n";
		str+= select_1;
	}
	return str;
}

/**
* Sets an input value to a select list selected index option value.
**/
function rd_setInputValueByOptionValue(inputName , selectName)
{
	select = MM_findObj(selectName);
	optionValue = select.options[select.selectedIndex].value;
	setInputValue(inputName , optionValue);
}

/**
* Transport one option to another select list
**/
function rd_transportOption(_selectA, _selectB)
{
	_selectA = document.getElementById(_selectA);
	_selectB = document.getElementById(_selectB);
	if(_selectA && _selectB)
	{
		if(_selectA.options.length>0)
		{
			if(_selectA.selectedIndex != -1 && _selectA.options[_selectA.selectedIndex].value!="")
			{
				nOption = document.createElement("option");
				nOption.text = _selectA.options[_selectA.selectedIndex].text;
				nOption.value = _selectA.options[_selectA.selectedIndex].value;
				_selectB.add(nOption,1);
				_selectA.remove(_selectA.selectedIndex);
				if(_selectA.selectedIndex<_selectA.options.length-1)
				{
					_selectA.selectedIndex = _selectA.selectedIndex+1;
				}
				else
				{
					_selectA.selectedIndex = _selectA.selectedIndex;
				}
				_selectB.selectedIndex = 1;
			}
		}
	}
	return true;
}

function rd_joinSelectOptions($selectOption)
{
	dataArray = new Array();
	for(i=0;i<$selectOption.options.length;i++){
		if($selectOption.options[i].value!="")
		dataArray.push($selectOption.options[i].value);
	}
	return dataArray.join(",");
}

function rd_removeSelectOptions(_selectList)
{
	_selectList.selectedIndex = 0;
	for(var i=0;i<_selectList.options.length;i++){
		_selectList.remove(i);
		_selectList.options[i] = null;
	}
	_selectList.options[0] = null;
}

/**
* Set a select list selected item by value;
* @return number new selected index
**/
function rd_setSelectedOptionByValue(select, value)
{
	var i = 0;
	if(select != null)
	{
		for(i=0; i<select.options.length; i++)
		{
			if(select.options[i].value == value)
			{
				select.selectedIndex = i;
				break;
			}
		}
	}
	return i;
}

/**
* Remove white space from a string
**/
function Trim(s)
{
	while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
	{
		s = s.substring(1,s.length);
	}
	while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
	{
		s = s.substring(0,s.length-1);
	}
	return s;
}

/**
* Focuse any input
**/
function rd_focusInput(input_name)
{
	var input = document.getElementById(input_name);
	if(input)
	{
		try
		{
			input.focus();
		}
		catch(e){}
	}
}

/**
* Removes initial text in text box.
**/
function removeInitialValue(textField)
{
	if(textField['initialTextValue'] == null)
	{
		textField.value = '';
		textField.initialTextValue = true;
	}
}

/**
* Toggle div display property
*
**/
function rd_toggleDivDisplay(divName, $force)
{
	var Div = rd_getElement(divName);
	if(Div)
	{
		if($force == true)
		{
			$force = 'block';
		}
		else if($force == false)
		{
			$force = 'none';
		}
		if($force != null)
		{
			Div.style.display = $force;
		}
		else
		{
			if(Div.style.display != null && Div.style.display == 'none')
				Div.style.display = 'block';
			else
				Div.style.display = 'none';
		}
	}
}

/**
* Set the innerHTML of a div layer
**/
function setDivSrc($div, content)
{
	divItem = MM_findObj($div);
	divItem.innerHTML = content;
}
/*
END DIV FUNCTIONALITY
*/
/*
IMAGE FUNCTIONALITY
*/
function rd_toggleImgDisplay($img, $display)
{
	imageObj = MM_findObj($img);
	imageObj.style.display = ($display) ? 'block' : 'none';
}

function setImgSrc($image, url)
{
	$img = MM_findObj($image);
	$img.src = url;
}
/*
END IMAGE FUNCTIONALITY
*/
/**
* Sets an unput value
**/
function setInputValue(input, str)
{
	var $input = rd_getElement(input);
	if($input)
	{
		$input.value = str;
	}
}

/**
* Get Any Element with a unique name
* @return object : if element does not exist return default value
**/
function rd_getElement(element_name, default_value)
{
	var element = false;
	if(element_name == undefined)
		return element;
	if(document)
		element = document.getElementById(element_name);
	if(!element)
		element = MM_findObj(element_name);
	if(!element)
		element = default_value;
	return element;
}

/**
* Set window location string
* @param string : url
* @return void
**/
function rd_sendWindow(url)
{
	if(url != '')
	{
		window.location.href = url;
	}
}

/**
 * Find X co-ordinate of an HTML element
 * @return number
 **/
function rd_findPosX(obj)
{
	var curleft = 0;
	if(obj.offsetParent)
	while(1)
	{
		curleft += obj.offsetLeft;
		if(!obj.offsetParent)
		break;
		obj = obj.offsetParent;
	}
	else if(obj.x)
	curleft += obj.x;
	return curleft;
}

/**
 * Find Y co-ordinate of an HTML element
 * @return number
 **/
function rd_findPosY(obj)
{
	var curtop = 0;
	if(obj.offsetParent)
	while(1)
	{
		curtop += obj.offsetTop;
		if(!obj.offsetParent)
		break;
		obj = obj.offsetParent;
	}
	else if(obj.y)
	curtop += obj.y;
	return curtop;
}

/**
* Generic form save javascript event
* runs rd_preformSaveButtonAction function
*
* @param object form
* @param boolean submit_form if set to true, submit the form
**/
function rd_saveForm(form, submit_form)
{
	rd_preformSaveButtonAction(form);
	return check(form, form.elements.length, submit_form);
}

function rd_preformEntityFormSubmitButtonAction( form, submit_form)
{
	return rd_preformSubmitButtonAction(form, submit_form);
}

/**
* Submit the form
*
* @param object form
* @param boolean submit_form if set to true, submit the form else return result (boolean)
**/
function rd_preformSubmitButtonAction(form, submit_form)
{
	return check(form, form.elements.length, submit_form);
}

/**
* Generic form button save event
* sets the return to screen variable to 'true']
*
* @param object form
**/
function rd_preformSaveButtonAction(form, value)
{
	if(value == null)
	{
		value = 'true';
	}
	if(form != null)
	{
		if(form[RETURN_TO_SCREEN_IDENTIFIER] != null)
		{
			form.return_to_screen.value = value;
		}
	}
}
function openWindow($url)
{
	open($url);
}

function toggleDisplay(divName,$force)
{
	Div = eval(divName);
	if($force != null){
		Div.style.display = ($force) ? 'block' : 'none';
	}else{
		if(Div.style.display != null && Div.style.display == 'none'){
			Div.style.display = 'block';
		}else{
			Div.style.display = 'none';
		}
	}
}

function rd_openSiteCenteredPopUp(title, include_id, width, height, url_addon)
{
	url_addon = (url_addon!=null) ? url_addon:"";
	return popUpCenteredWindow(CMS_FILENAME_TPL_MODAL_IMAGE_PREVIEW+"?title="+title+"&data="+include_id+url_addon, width, height) ;
}

function site_preview_image(img, width, height)
{
	width = (width>200) ? width : 200;
	height = (height>200) ? height : 200;
	if(height>screen.availHeight || width>screen.availWidth){
		$scroll = "1";
	}else{
		$scroll = "1";
	}
	rd_openSiteCenteredPopUp(RS_PREVIEW_IMAGE, "MODAL_IMAGE_PREVEIW", 800, 600,"&img="+escape(img)+"&scroll="+$scroll);
}

var rd_submitted_form_element = null;
var rd_form_submitted = false;
/**
* So that forms will only submit once.
*
* @param object _form_element
* @return void
**/
function rd_singleSubmit(form_element)
{
	if(form_element != null)
	{
		rd_submitted_form_element = form_element;

		if(rd_form_submitted == false)
		{
			rd_submitted_form_element.submit();
			rd_form_submitted = true;
		}
	}
}

function hideSelects()
{
	/**
	var categoryselect = rd_getElement('product_select');
	if(categoryselect)
		categoryselect.style.visibility = 'hidden';
	**/
}
function showSelects()
{
	/**
	var categoryselect = rd_getElement('product_select');
	if(categoryselect)
		categoryselect.style.visibility = 'visible';
	**/
}
