/*
	RADIOBUTTON.XML 5.0
	download at: http://acidmartin.wemakesites.net/?pageId=SkinnableRadiobutton
	created by: Martin Ivanov
	email: acid_martin@yahoo.com, martin@acidmartin.com
	personal website: http://acidmartin.wemakesites.net/
	portfolio: http://wemakesites.net
	
	IMPORTANT REGULATIONS:
	
	1. IF YOU ARE USING THE OBFUSCATED VERSION OF RADIOBUTTON.XML, IT IS FORBIDDEN TO DEOBFUSCATE AND MODIFY THIS SRCIPT WITHOUT AUTHOR'S WRITTEN PERMISSION. OTHERWISE SUCH ACTIONS WILL BE CONSIDERED VIOLATIONS OF THE COPYRIGHT LAWS AND LEGAL STEPS WILL BE TAKEN.
	
	2. IF YOU HAVE PURCHASED THE SOURCE CODE OF RADIOBUTTON.XML, YOU CANNOT USE THIS SCRIPT ON WEBPAGES WITHOUT
	OBFUSCATING IT. OTHERWISE SUCH ACTIONS WILL BE CONSIDERED VIOLATIONS OF THE COPYRIGHT LAWS AND LEGAL STEPS WILL BE TAKEN.
*/

var RadioButtonXmlName = 'RadioButton.XML';
var RadioButtonXmlVersion = ' 5.0\n--------------------------\n\n';
var RadioButtonXmlErrorMessage = 'ERROR: Cannot create RadioButton List:\n\n';
var FullErrorMessage = RadioButtonXmlName + RadioButtonXmlVersion + RadioButtonXmlErrorMessage
var TrueOrFalse = '\nIt should be either "true" or "false".';
var RadioButtonXmlUrl = 'http://acidmartin.wemakesites.net/?pageId=SkinnableRadiobutton';
var BuyRadioButtonXmlUrl = 'http://acidmartin.wemakesites.net/BuyRadioButtonXml50.html';

function selectRadioButton(RadioButtonPlaceHolder, LabelId)
{
	var radioButtonLabels = $(RadioButtonPlaceHolder).getElementsByTagName('label');
	for(i = 0; i < radioButtonLabels.length; i++)
	{
		if(!radioButtonLabels[i].childNodes[0].getAttribute('disabled'))
		{
			radioButtonLabels[i].className = '';
		}
	}
	$(LabelId).className = ' selectedRadioButton';
}

function renderRadioButtonList(radioButtonListFeedFile, RadioListLoader)
{
	var radioButtonListPlaceHolder = $(RadioListLoader);
	var RadioButtonXmlIsDemo = false;
	if(RadioButtonXmlIsDemo)
	{
		if(confirm(RadioButtonXmlName + RadioButtonXmlVersion + 'This is a demo version of RadioButton.XML.\nWould you like to buy the full version?'))
		{
			window.open(BuyRadioButtonXmlUrl, '_blank');
		}
	}
	
	if($(RadioListLoader) == null)
	{
		alert(FullErrorMessage + 'RadioButtonList placeholder with ID "' + RadioListLoader + '" was not found in the context of the page.\nPlease, check your page\'s DOM.');
		return false;
	}
	if(window.ActiveXObject)
	{
		radios = new ActiveXObject("Microsoft.XMLDOM");
		radios.async = false;
		radios.load(radioButtonListFeedFile);
	}
	else if(document.implementation && document.implementation.createDocument)
	{
		radios = document.implementation.createDocument("","",null);
		radios.async = false;
		radios.load(radioButtonListFeedFile);
	}
	var radioButtonGroupEnabled = radios.getElementsByTagName('radiobuttonxml')[0].getAttribute('enabled');
	if(radioButtonGroupEnabled == "false")
	{
		return false;
	}
	if(radioButtonGroupEnabled != "false" && radioButtonGroupEnabled != "true")
	{
		alert(FullErrorMessage + '"' + radioButtonGroupEnabled + '" is not a valid value of the "enabled" property of the <radiobuttonxml/> tag.' + TrueOrFalse);
		return false;
	}
	var RadioButtonXmlBaseStylesheet = document.createElement('link');
	RadioButtonXmlBaseStylesheet.setAttribute('type', 'text/css');
	RadioButtonXmlBaseStylesheet.setAttribute('rel', 'stylesheet');
	RadioButtonXmlBaseStylesheet.setAttribute('href', radioButtonXmlScriptsFolder + '/RadioButton.XML/Scripts/RadioButton.css');
	var radioButtonListOrientation = radios.getElementsByTagName('radiobuttonxml')[0].getAttribute('orientation');
	var listwidth = radios.getElementsByTagName('radiobuttonxml')[0].getAttribute('listwidth');
	var listheight = radios.getElementsByTagName('radiobuttonxml')[0].getAttribute('listheight');
	var radioButtonListSkinName = radios.getElementsByTagName('radiobuttonxml')[0].getAttribute('skin');
	var RadioButtonXmlSkinFile = document.createElement('link');
	RadioButtonXmlSkinFile.setAttribute('type', 'text/css');
	RadioButtonXmlSkinFile.setAttribute('rel', 'stylesheet');
	RadioButtonXmlSkinFile.setAttribute('href', radioButtonXmlScriptsFolder + '/RadioButton.XML/Skins/' + radioButtonListSkinName + '/Styles.css');
	document.getElementsByTagName('head')[0].appendChild(RadioButtonXmlSkinFile);
	document.getElementsByTagName('head')[0].appendChild(RadioButtonXmlBaseStylesheet);
	var radioButtonListFieldset = document.createElement('fieldset');
	var radioButtonListLegend = document.createElement('legend');
	var radioButtonListLegendText = radios.getElementsByTagName('radiobuttonxml')[0].getAttribute('legendtext');
	var radioButtonListDisplayLegend = radios.getElementsByTagName('radiobuttonxml')[0].getAttribute('displaylegend');
	var radioButtonListDisplayFieldset = radios.getElementsByTagName('radiobuttonxml')[0].getAttribute('displayfieldset');
	var radioButtonsListUL = document.createElement('ul');
	var radioButtonsListItems = radios.getElementsByTagName('radiobuttonitem');
	var defaultCheckedItem = radios.getElementsByTagName('radiobuttonxml')[0].getAttribute('defaultcheckeditem');
	radioButtonListPlaceHolder.className = 'radiobuttonxml ' + radioButtonListSkinName;
	radioButtonsListUL.setAttribute('id', radioButtonListPlaceHolder.id + '_List');
	radioButtonListFieldset.setAttribute('id', radioButtonListPlaceHolder.id + '_FieldSet');
	radioButtonListLegend.setAttribute('id', radioButtonListPlaceHolder.id + '_Legend');
	radioButtonListPlaceHolder.appendChild(radioButtonListFieldset);
	$(radioButtonListFieldset.id).appendChild(radioButtonListLegend);
	radioButtonListFieldset.appendChild(radioButtonsListUL);
	$(radioButtonListLegend.id).innerHTML = radioButtonListLegendText;
	if(listwidth == 'auto')
	{
		$(radioButtonListPlaceHolder.id + '_FieldSet').style.width = listwidth;
	}
	else
	{
		try
		{
			radioButtonListPlaceHolder.style.width = listwidth + 'px';
		}
		catch(e)
		{
			alert(FullErrorMessage + '"' + listwidth + '" is not a valid value of the "listwidth" of the <radiobuttonxml /> tag. It should be an integer or "auto".');
			radioButtonListPlaceHolder.style.display = 'none';
			return false;
		}
	}
	if(listheight == 'auto')
	{
		radioButtonListPlaceHolder.style.height = listheight;
	}
	else
	{
		try
		{
			radioButtonListPlaceHolder.style.height = listheight + 'px';
		}
		catch(e)
		{
			alert(FullErrorMessage + '"' + listheight + '" is not a valid value of the "listheight" of the <radiobuttonxml /> tag. It should be an integer or "auto".');
			radioButtonListPlaceHolder.style.display = 'none';
			return false;
		}
	}
	if(radioButtonListDisplayLegend == "false")
	{
		$(radioButtonListLegend.id).style.display = 'none';
	}
	if(radioButtonListDisplayLegend != "false" && radioButtonListDisplayLegend != "true")
	{
		alert(FullErrorMessage + '"' + radioButtonListDisplayLegend + '" is not a valid value of the "displaylegend" property of the <radiobuttonxml/> tag.' + TrueOrFalse);
		radioButtonListPlaceHolder.style.display = 'none';
	}
	if(radioButtonListDisplayFieldset == "false")
	{
		$(radioButtonListFieldset.id).style.border = '0';
	}
	if(radioButtonListDisplayFieldset != "false" && radioButtonListDisplayFieldset != "true")
	{
		alert(FullErrorMessage + '"' + radioButtonListDisplayFieldset + '" is not a valid value of the "displayfieldset" property of the <radiobuttonxml/> tag.' + TrueOrFalse);
		radioButtonListPlaceHolder.style.display = 'none';
	}
	if(radioButtonListOrientation == "horizontal")
	{
		radioButtonListPlaceHolder.className += ' horizontalRadioButtonList';
	}
	if(radioButtonListOrientation == "vertical")
	{
		radioButtonListPlaceHolder.className += ' verticalRadioButtonList';
	}
	if(radioButtonListOrientation != "vertical" && radioButtonListOrientation != "horizontal")
	{
		alert(FullErrorMessage + '"' + radioButtonListOrientation + '" is not a valid value of the "orientation" property of the <radiobuttonxml/> tag.\nIt should be either "vertical" or "horizontal".');
		radioButtonListPlaceHolder.style.display = 'none';
	}
	for (i = 0; i < radioButtonsListItems.length; i++)
	{
		var RadioInputEnabledState = radioButtonsListItems[i].getAttribute('enabled');
		var onclientclick = radioButtonsListItems[i].getAttribute('onclientclick');
		var RadioInputEnabled = '';
		var RadioInputLabelEnabled = '';
		if(RadioInputEnabledState == "true")
		{
			RadioInputEnabled = '';
			RadioInputLabelEnabled = 'class=""';
		}
		if(RadioInputEnabledState == "false")
		{
			RadioInputEnabled = 'disabled="disabled"';
			RadioInputLabelEnabled = 'class="disabledRadioButtonLabel"';
		}
		if(RadioInputEnabledState != "false" && RadioInputEnabledState != "true")
		{
			alert(FullErrorMessage + '"' + RadioInputEnabledState + '" is not a valid value of the "enabled" property of the <radiobuttonitem/> tag.' + TrueOrFalse);
		radioButtonListPlaceHolder.style.display = 'none';
		}
		var radioButtonsListItemsLI = document.createElement('li');
		radioButtonsListItemsLI.setAttribute('id', radioButtonListPlaceHolder.id + '_List_Item' + i) 
		$(radioButtonsListUL.id).appendChild(radioButtonsListItemsLI);
		radioButtonsListItemsLI.innerHTML = '<label ' + RadioInputLabelEnabled + ' id="' + radioButtonListPlaceHolder.id + '_List_Item' + i + '_label" for="' + radioButtonListPlaceHolder.id + '_List_Item' + i + '_input" title="' + radioButtonsListItems[i].getAttribute('text') + '"><input class="realRadioInput" ' + RadioInputEnabled + ' onclick="selectRadioButton(\'' + radioButtonListPlaceHolder.id + '\', \'' + radioButtonListPlaceHolder.id + '_List_Item' + i + '_label\'); ' + onclientclick + ';" type="radio" value="' + radioButtonsListItems[i].getAttribute('itemvalue') + '" name="' + radios.getElementsByTagName('radiobuttonxml')[0].getAttribute('groupname') + '" id="' + radioButtonListPlaceHolder.id + '_List_Item' + i + '_input" title="' + radioButtonsListItems[i].getAttribute('text') + '" />' + radioButtonsListItems[i].getAttribute('text') + '</label>'
	}
	try
	{
		$(RadioListLoader + '_List_Item' + defaultCheckedItem + '_input').checked = 'checked';
		$(RadioListLoader + '_List_Item' + defaultCheckedItem + '_label').className = 'selectedRadioButton';
	}
	catch(e)
	{
		alert(FullErrorMessage + 'RadioButton Item with id "' + defaultCheckedItem + '" does not exist.\nPlease, check the value of the "defaultcheckeditem" property of the <radiobuttonxml /> tag.');
	}
	if(($(RadioListLoader + '_List_Item' + defaultCheckedItem + '_input').disabled) && ($(RadioListLoader + '_List_Item' + defaultCheckedItem + '_input').parentNode.className == 'selectedRadioButton'))
	{
		alert(FullErrorMessage + 'You cannot have both disabled and checked radiobutton in the list.\nPlease, change the "enabled" property of item ' + i + ' in the <radiobuttonitem /> tag or the index of the "defaultcheckeditem" of the <radiobuttonxml /> tag.');
		radioButtonListPlaceHolder.style.display = 'none';
	}
}

/*if(window.attachEvent)
{
	window.attachEvent('onload', function(e)
		{
			initRadioButtons();
		}
	);
}
else
{
	window.addEventListener('load', function(e)
		{
			initRadioButtons();
		}, true	
	);
}*/

function $(stringId)
{
	return document.getElementById(stringId);
}

function disableRadioButtonItem(RadioButtonPlaceHolder, ItemIndex)
{
	if($(RadioButtonPlaceHolder + '_List_Item' + ItemIndex + '_input').checked == false)
	{
		$(RadioButtonPlaceHolder + '_List_Item' + ItemIndex + '_input').setAttribute('disabled', 'disabled')
		$(RadioButtonPlaceHolder + '_List_Item' + ItemIndex + '_label').className = 'disabledRadioButtonLabel';
	}
	if($(RadioButtonPlaceHolder + '_List_Item' + ItemIndex + '_input').checked == true)
	{
		$(RadioButtonPlaceHolder + '_List_Item' + ItemIndex + '_input').setAttribute('disabled', 'disabled')
		$(RadioButtonPlaceHolder + '_List_Item' + ItemIndex + '_input').checked = false;
		$(RadioButtonPlaceHolder + '_List_Item' + ItemIndex + '_label').className = 'disabledRadioButtonLabel';
	}
}

function enableRadioButtonItem(RadioButtonPlaceHolder, ItemIndex)
{
	$(RadioButtonPlaceHolder + '_List_Item' + ItemIndex + '_input').removeAttribute('disabled')
	$(RadioButtonPlaceHolder + '_List_Item' + ItemIndex + '_label').className = '';
}

function addRadioButtonItem(RadioButtonPlaceHolder, ItemText, ItemValue, ItemEnabled)
{
	var newRadioButtonItem = document.createElement('li');
	var numberOfListItems = $(RadioButtonPlaceHolder).getElementsByTagName('li');
	var newItemId = numberOfListItems.length + parseInt('1') - parseInt('1');
	var groupName = $(RadioButtonPlaceHolder).getElementsByTagName('input')[0].getAttribute('name');
	newRadioButtonItem.setAttribute('id', RadioButtonPlaceHolder + '_List_Item' + newItemId);
	newRadioButtonItem.innerHTML = '<label title="'+ ItemText +'" id="' + RadioButtonPlaceHolder + '_List_Item' + newItemId + '_label"><input id="' + RadioButtonPlaceHolder + '_List_Item' + newItemId + '_input" type="radio" name="' + groupName + '" class="realRadioInput" onclick="selectRadioButton(\'' + RadioButtonPlaceHolder + '\', \'' + RadioButtonPlaceHolder + '_List_Item' + newItemId + '_label\');" value="' + ItemValue + '" />' + ItemText + '</label>';
	$(RadioButtonPlaceHolder + '_List').appendChild(newRadioButtonItem);
	if(!ItemEnabled)
	{
		$(RadioButtonPlaceHolder + '_List_Item' + newItemId + '_label').className = 'disabledRadioButtonLabel';
		$(RadioButtonPlaceHolder + '_List_Item' + newItemId + '_input').setAttribute('disabled', 'disabled');
	}
}

function removeRadioButtonItem(RadioButtonPlaceHolder, ItemIndex)
{
	try
	{
		$(RadioButtonPlaceHolder + '_List_Item' + ItemIndex).style.display = 'none';
	}
	catch(e)
	{
		alert(RadioButtonXmlName + RadioButtonXmlVersion + 'ERROR: Cannot perform the removeRadioButtonItem operation.\nRadioButton with ID "' + ItemIndex + '" does not exist in the context of the "' + RadioButtonPlaceHolder + '" placeholder.\nPlease, check your page\'s DOM.');
	}
}

function uncheckRadioButtonGroup(RadioButtonPlaceHolder)
{
	var allCheckBoxes = $(RadioButtonPlaceHolder).getElementsByTagName('label');
	for(i = 0; i < allCheckBoxes.length; i++)
	{
		allCheckBoxes[i].className = '';
		allCheckBoxes[i].childNodes[0].checked = false;
	}
}