/*************************************
Avram's unobtrusive, accessible popups
*************************************
* the link should look like this:
* <a href="path/to/popup.html" target="_blank" class="popup w###h###">popup</a>
* (replace the '#'s after the 'w' with the desired width,
* and the '#'s after the 'h' with the desired height of the popup)
*/
function init_popups() {
    popup_links = $$("a.popup");
    popup_links.invoke('observe', 'click', openPopup);
}

function openPopup(e) {
    var link = Event.element(e);
    
    if (paramClass = $w(link.className)[1]) {
        params = paramClass.split(/[wh]/);
        if (params.size() > 2)
            params.splice(0,1);
        //alert(params);
        width = params[0];
        height = params[1];
        //alert("width: "+width+", height: "+height);
        href = link.readAttribute("href")
        window.open(href,"popup","width="+width+",height="+height+",resizable=0,status=0,location=0,menubar=0");
    }
    Event.stop(e);
}

// *** end: handle popup links ***

function init_popupCloseBtn() {
    if ($('CloseWindow')) {
        $('CloseWindow').observe('click', function() {
            window.close();
        });
    }
}

function init_customDonation() {
    var rdb_Custom = $('uxGiveCustom');
    var txt_Custom =  $('uxCustomDonationAmount');
    if (txt_Custom) {
        txt_Custom.observe('keyup', function() {
            if (txt_Custom.value.length > 0) {
                rdb_Custom.checked = true;
            } else {
                rdb_Custom.checked = false;
            }
        });
    }
}

// on click of checkbox: are any checked--> disable/enable interest checkbox accordingly
// if any interest checkboxes are checked, maintaned enabled

function init_checkBoxes(){
	if(!$('uxNoContact')){ // make sure we have our form elements
		return;
	}
    $$('.contact_check input[type="checkbox"]').invoke('observe', 'click', contactCheck_Click);
     
    chkLstContactMethods = $$('.contact_check input[type="checkbox"]');
    chkLstInterests = $$('#uxInterests input[type="checkbox"]');
    var anyContactChecked = anythingChecked(chkLstContactMethods);
    var anyInterestChecked = anythingChecked(chkLstInterests);

    // set the initial enable/disable for data on load
     enableCheckboxes(chkLstInterests, anyContactChecked);
     
     Event.observe($('uxNoContact'), 'click', noContact_Click)
}
function contactCheck_Click(e){
    var el = e.target || e.srcElement;
    //var el = e.element();
    
    var anyContactChecked = anythingChecked(chkLstContactMethods);
    var anyInterestChecked = anythingChecked(chkLstInterests);
   
	enableCheckboxes(chkLstInterests, anyContactChecked);
   
	if(el.checked){
		$('uxNoContact').checked = false;
	}

}
function anythingChecked(arCheckboxes){
    var anyChecked = false;
    for(var i=0; i < arCheckboxes.length; i++){
        if (arCheckboxes[i].checked){
            anyChecked = true
            continue;
        }
    }
    return anyChecked;
    
}
function enableCheckboxes(arCheckboxes, bEnable){
    arCheckboxes.each(
        function(chk){
            chk.disabled = (bEnable)? '' : 'disabled' ;
        }
   );
}

function noContact_Click(e) {
   var el = e.target || e.srcElement;
    //var el = Event.findElement(e, 'INPUT');
   
    if(el.checked){
        toggleCheckboxes($$('#uxInterests input[type="checkbox"]'), false);
        toggleCheckboxes($$('.contact_check input[type="checkbox"]'), false);
        enableCheckboxes($$('#uxInterests input[type="checkbox"]'), false)	        
    }
}

function toggleCheckboxes(arCheckboxes, bChecked){
    arCheckboxes.each(
        function(chk){
            chk.checked = (bChecked);
        }
   );
}


function init_all() {
    init_checkBoxes();
    init_popups();
    init_popupCloseBtn();
    init_customDonation();
}
Event.observe( window, 'load', init_all);
