/*
	name			: ClassBehaviours, the javascript framework based on class-name parsing
	update			: 9.11.9
	author			: Maurice van Creij
	dependencies	: jquery.classbehaviours.js
	info			: http://www.classbehaviours.com/

    This file is part of jQuery.classBehaviours.

    ClassBehaviours is a javascript framework based on class-name parsing.
    Copyright (C) 2008  Maurice van Creij

    ClassBehaviours is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ClassBehaviours is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ClassBehaviours. If not, see http://www.gnu.org/licenses/gpl.html.
*/

/*
	<table>
		<tr>
			<td>
				<input class="radio disabledByRadio enable_Select1,Select2,Select3" selected="selected" type="radio" id="Radio1adad" name="frmMeter" value="0"/>
				<select disabled="disabled" id="Select1" class="small" name="frmDay"><option>aa</option></select>
				<select disabled="disabled" id="Select2" class="mediumEls" name="frmMonth"><option>bb</option></select>
				<select disabled="disabled" id="Select3" class="tall" name="frmYear"><option>cc</option></select>
			</td>
		</tr>
		<tr>
			<td>
				<input class="radio disabledByRadio disable_Select1,Select2,Select3" type="radio" id="Radio2adasd" name="frmMeter" value="1"/>
				<label class="no" for="frmMeter1">Onbekend, ik wil de energie op mijn oude adres nog niet stopzetten.</label>
			</td>
		</tr>
	</table>
*/

	// create the jQuery object if it doesn't already exist
	if(typeof(jQuery)=='undefined') jQuery = function(){};

	// create the root classbehaviours object if it doesn't already exist
	if(typeof(jQuery.classBehaviours)=='undefined') jQuery.classBehaviours = function(){};

	// create the handlers child object if it doesn't already exist
	if(typeof(jQuery.classBehaviours.handlers)=='undefined') jQuery.classBehaviours.handlers = function(){}

	// move a link's click event to the parent node
	jQuery.classBehaviours.handlers.disabledByRadio = {
		// properties
		name: 'disabledByRadio',
		index: 0,
		// methods
		start: function(node){
			// give the node an id
			node.id = (node.id==null) ? this.name + this.index++ : node.id ;
			// change events on the radio button
			node.onchange = this.delay;
			node.onclick = this.delay;
		},
		// events
		delay: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			setTimeout("jQuery.classBehaviours.handlers.disabledByRadio.update(document.getElementById('" + objNode.id + "'))", 250);
		},
		update: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			var dbr = jQuery.classBehaviours.handlers.disabledByRadio;
			// if this the radio button is checked
			if(objNode.checked){
				// get the id's to be enabled
				var enableIdCsv = jQuery.classBehaviours.utilities.getClassParameter(objNode, 'enable', null);
				if(enableIdCsv!=null){
				    var enableIds = enableIdCsv.split(',');
				    var theData = "set=1";
				    $.ajax({
				        type: "POST",
				        url: $("#theLocalPath").val() + "scripts/ExpressCheckout.ashx",
				        data: theData,
				        success: function (returnObj) { }
				    });
					for(var a=0; a<enableIds.length; a++){
						if(document.getElementById(enableIds[a]).disabled!=null){
							document.getElementById(enableIds[a]).disabled = '';
						}else{
							document.getElementById(enableIds[a]).className = document.getElementById(enableIds[a]).className.replace('disabled', 'enabled');
						}
					}
				}
				// get the id's to be disabled
				var disableIdCsv = jQuery.classBehaviours.utilities.getClassParameter(objNode, 'disable', null);
				if(disableIdCsv!=null){
				    var disableIds = disableIdCsv.split(',');
				    var theData = "set=0";
				    $.ajax({
				        type: "POST",
				        url: $("#theLocalPath").val() + "scripts/ExpressCheckout.ashx",
				        data: theData,
				        success: function (returnObj) { }
				    });
					for(var a=0; a<disableIds.length; a++){
						if(document.getElementById(disableIds[a]).disabled!=null){
							document.getElementById(disableIds[a]).disabled = 'disabled';
						}else{
							document.getElementById(disableIds[a]).className = document.getElementById(disableIds[a]).className.replace('enabled', 'disabled');
						}
					}
				}
			}
		}
	}

	// add this addon to the jQuery object
	if(typeof(jQuery.fn)!='undefined'){
		// extend jQuery with this method
		jQuery.fn.disabledByRadio = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.disabledByRadio.start(this);
				}
			);
		};
		// set the event handler for this jQuery method
		$(document).ready(
			function(){
				$(".disabledByRadio").disabledByRadio();
			}
		);
	}

