Event.observe(window, 'load', wish_init, false);

function wish_init() {


		if ($('address_zip') && !$('geniehug')) {
				
				pe = new PeriodicalExecuter(getChaptersForZip,1);
				zip_code_field = $('address_zip');
		}

		try {
				$$('UL#skills LI.skill SPAN').each(function(skill_item) {
								skill_item.onmouseover= showSkill;
								skill_item.onmouseout=hideSkill;
						});
		} catch(err) {
		}

		try {
				$$('UL INPUT.other_skills').each(function(element) {
								element.onchange = checkSkillCheckbox;
						});
				$$('UL INPUT.other_skills_checkbox').each(function(element) {
								element.onclick = clearOtherSkillField;
						});
		} catch (err) {
		}

		try {
				$('expires_check').onclick = clearDonationExpirationField;
				$('expires').onkeypress = checkDonationExpires;
		} catch(err) {
		}
				
		try {
				new Validation('volunteer_form',{focusOnError : false} );
		} catch(err) {
		}

		try {
				new Validation('donate_form',{focusOnError : false});
		} catch(err) {
		}

 
}


/**
 * This function is executed by a PeriodicalExecuter instantiated by
 * wish_init whenever we're on a page with an element id of
 * address_zip. It issues an Ajax request if the following are true:
 *
 * + zip code field is at least 5 characters
 * + zip code field has changed since the last execution
 *
 * current_zip_code retains the field value between executions.
 * zip_code_field is an optimization to avoid repeated getElementById
 * lookups
 */
var current_zip_code = undefined;
var zip_code_field = undefined
				 
function getChaptersForZip() {
		var zip_code = zip_code_field.value;

		if (zip_code.length == 0) {
				$('chapter_list').innerHTML = '&nbsp;';
		}

		if (zip_code.length < 5) {
				return;
		}

		if (zip_code == current_zip_code) {
				return;
		}

		var container = 'chapter_list';
		new Ajax.Updater(container, WEBROOT + 'chapter/lookup/' + zip_code);
		current_zip_code = zip_code;
}

function showSkill(e) {
		if (!e) var e = window.event;
		
		var el = Event.element(e);
		title = el.innerHTML;

		body = el.nextSibling.innerHTML;


		$('skill_rollover_title').innerHTML = title;
		$('skill_rollover_body').innerHTML = body;

		with ($('skill_rollover_container').style) {
				top = (findPosY(el) - 10) + 'px';
				left = (Event.pointerX(e) + 50) + 'px';
				display = 'block';
		}

}

function hideSkill() {
		$('skill_rollover_container').style.display = 'none';
}


function 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;
}

function 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;
}

function clearDonationExpirationField(e) {
		if (!e) var e = window.event;
		
		var el = Event.element(e);

		if (!el.checked) {
				$('expires').value = '';
		} else {
				$('expires').focus();
		}
}

function checkDonationExpires(e) {
		if (!e) var e = window.event;
		
		var el = Event.element(e);

		if (el.value.length > 0) {
				$('expires_check').checked = true;
		}
}

function checkSkillCheckbox(e) {
		if (!e) var e = window.event;

		var el = Event.element(e);

		target_id = el.id.replace('other_', '');
		
		$(target_id).checked = (el.value.length > 1);
}

function clearOtherSkillField(e) {
		if (!e) var e = window.event;

		var el = Event.element(e);
		
		target_id = "other_" + el.id;
		if (el.checked) {
				$(target_id).value = '';
				$(target_id).focus();
		} else {
				$(target_id).value = '';
		}
}

function checkForm(form) {

        var thebutton=document.getElementById('SubmitButton');
        thebutton.onclick=function() {return false}

}

