Event.observe(window,'load',function(){
	// Handle p_link clicks on LI elements
	$$(".p_link").each(function(el){
		var obj = $(el);
		if(obj.down("a")){
			var loc = obj.down("a").href;
			obj.observe("click",function(pEvent){
				window.location.href = loc;
			});
		}
	});
});

function showPrivacy(){
	var element = $('privacy_policy');
	var dims = element.getDimensions();
	var top_offset = (window.pageYOffset) ? window.pageYOffset :  document.body.scrollTop;

	if(document.documentElement.scrollTop){
		// Supported in IE7 and Firefox
		top_offset = document.documentElement.scrollTop;
	}

	var w = 0;	var h = 0;

	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		w = window.innerWidth;
		h = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}

	x_pos = Math.round((w * 0.5 - dims.width / 2 ));
	y_pos = top_offset + Math.round( h * 0.5 - dims.height / 2 );

	new Effect.Move('privacy_policy',{ x: x_pos, y: y_pos, mode: 'absolute', duration: 0.0 });
	new Effect.Appear('privacy_policy', {duration: 0.2});

}

function moveShowPopupDiv(div_id){
	var element = $(div_id);
	if(!element) return false;

	var dims = element.getDimensions();
	var top_offset = (window.pageYOffset) ? window.pageYOffset :  document.body.scrollTop;

	if(document.documentElement.scrollTop){
		// Supported in IE7 and Firefox
		top_offset = document.documentElement.scrollTop;
	}

	var w = 0;	var h = 0;

	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		w = window.innerWidth;
		h = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}

	x_pos = Math.round((w * 0.5 - dims.width / 2 ));
	y_pos = top_offset + Math.round( h * 0.5 - dims.height / 2 );

	new Effect.Move(div_id,{ x: x_pos, y: y_pos, mode: 'absolute', duration: 0.0 });
	new Effect.Appear(div_id, {duration: 0.4});

	return false;
}

function largeButton(li){
	if(li.innerHTML.search("CART") > 0){
		$('popup_cart_error_pop').hide();
	}
	else if(li.innerHTML.search("CHECKOUT") > 0){
		document.checkout.submit();
	}
}

function checkCountry(country){
	if(country == 'CAN' || country == 'USA'){
		$('state_required').show();
		$('zip_required').show();

		if(country == 'CAN'){
			$('state_text').hide();
			$('province_text').show();
		}else{
			$('state_text').show();
			$('province_text').hide();
		}

	}else{
		$('state_required').hide();
		$('zip_required').hide();
	}
}

function dropRecurringNote(div_id){
	if($(div_id).style.display == 'none'){
		var tips = $$('.tooltip');
		for(var i=0;i<tips.length;i++){
			if(tips[i].style.display != 'none'){
				tips[i].style.display = 'none';
			}
		}
		$(div_id).appear();
	}
	else{
		Effect.DropOut(div_id);
	}
}

function showPictureGallery(page_id,page,pagetype)
{
	//$('pictureGallery').innerHTML = 'Loading...';
	var url = 'ajax.pictureGallery.php';
	var params = 'page_id='+page_id+'&page='+page+'&pagetype='+pagetype;
	var ajax = new Ajax.Updater(
			'pictureGallery',
			url,
			{method: 'get', parameters: params, evalScripts: true});
}

function showNewsArchives(page_id,page)
{
	//$('pictureGallery').innerHTML = 'Loading...';
	var url = 'ajax.newsArchives.php';
	var params = 'page_id='+page_id+'&page='+page;
	var ajax = new Ajax.Updater(
			'news_archives',
			url,
			{method: 'get', parameters: params, evalScripts: true});
}

function validateGeneralForm(oForm){
	var f = $(oForm);

	if($('one_time_gen').checked == true){
		if($('one_time_amount_gen').value=='other'){
			// check input box
			if(isNaN( $('txt_onetime_gen').value) || $('txt_onetime_gen').value == ""){
				$('errorWindowMsg').innerHTML = 'You must enter a real number.';
				openAlertPopup();
				return false;
			}
		} else {
			// check value
		}
	} else if($('recurring_gen').checked == true){
		if($('recurring_amount_gen').value=='other'){
			// check input box
			if(isNaN( $('txt_recurring_gen').value) || $('txt_recurring_gen').value == ""){
				$('errorWindowMsg').innerHTML = 'You must enter a real number.';
				openAlertPopup();
				return false;
			}
		} else {
			// check value
		}
	} else {
		$('errorWindowMsg').innerHTML = 'You must select an option.';
		openAlertPopup();
		return false;
	}

	return true;
}

function popUpGiftOptions_general(){

	$('donate_popup').show();

	return true;
}

function popupAmounts_general(oRadio){

	if(oRadio.id == "one_time_gen"){
		$("one_time_amount_gen").writeAttribute("name","amount"); // overwrite select box name
		$("recurring_amount_gen").writeAttribute("name",""); // overwrite select box name
		$("recurring_amount_gen").selectedIndex = 0; // select first option in other box
		$('txt_recurring_gen').writeAttribute('name','');
		$('txt_recurring_gen').hide();
		$("general_recurring").hide();
		$("general_one_time").show();
	} else if(oRadio.id == "recurring_gen"){
		$("one_time_amount_gen").writeAttribute("name",""); // overwrite select box name
		$("one_time_amount_gen").selectedIndex = 0; // select first option in other box
		$("recurring_amount_gen").writeAttribute("name","amount"); // overwrite select box name
		$('txt_onetime_gen').writeAttribute('name','');
		$('txt_onetime_gen').hide();
		$("general_one_time").hide();
		$("general_recurring").show();
	}
}

// If value of 'other' selected, swaps 'name' of Select box with next element (i.e. a text box)
// so, on form submit, the amount will be pulled from the input box instead of select element.
function showOtherAmount(selbox){
	var s = $(selbox);
	var s2 = s.next();

	// alert(s.value);

	if(s.value == 'other'){
		s2.show();
		s2.writeAttribute('name',s.name);
		s.writeAttribute('name','');
	}
	else{
		if(s2.name==''){ return true; }
		s.writeAttribute('name',s2.name);
		s2.writeAttribute('name','');
		s2.hide();
	}

	// console.log('s,s2: %s \n %s',s.name,s2.name);
}

// Hide each element that has class "strClass"
function hideClass(strClass){

	$$('.' + strClass).each(function(s){s.hide();});
}

function validateDonation(formid){
	var f = $(formid);
	if($('one_time').checked == true){
		if($('one_time_amount').value=='other'){
			// check input box
			if(isNaN( $('txt_onetime').value) || $('txt_onetime').value == ""){
				$('errorWindowMsg').innerHTML = "You must enter a real number.";
				openAlertPopup();
				return false;
			}
		} else {
			// check value
		}
	} else if($('recurring').checked == true){
		if($('recurring_amount').value=='other'){
			// check input box
			if(isNaN( $('txt_recurring').value) || $('txt_recurring').value == ""){
				$('errorWindowMsg').innerHTML = "You must enter a real number.";
				openAlertPopup();
				return false;
			}
		} else {
			// check value
		}
	}

	return true;
}

function validateProgramForm(message){
	var success = false;
	Form.getInputs('options_form','radio','product_option_id').each(
		function(input){
			if(input.checked)
				success = true;
	});

	if(success){
		document.options_form.submit();
	}
	else{
		$('errorWindowMsg').innerHTML = message;
		openAlertPopup();
	}
}

// msg is an array of error msg strings (for language translation)
function validateWeddingForm(oForm,msg){
	var f = $(oForm);

	if($('wedding_date').getValue() == ""){
		$('errorWindowMsg').innerHTML = msg['choose_date'];
		openAlertPopup();
		//<?=$alert_pop->getOpenEvent()?>;
		return false;
	}

	var success1 = false;
	var dolAmt = 0;
	f.getInputs('radio','product_option_id').each(
		function(input){
			if(input.checked){
				success1 = true;
			}
			dolAmt = getDolAmt(input.next().innerHTML);


	});

	if(!success1){
		$('errorWindowMsg').innerHTML = msg['choose_option'];
		openAlertPopup();
		return false;
	}



	return true;

}

function openAlertPopup(){
	displayDialog('popup_alert_pop',{'fade': false});
	return false;
}

function setPopupData(title,content){
	var titledata = $$("#popup_alert_pop_handle .padded .title");
	var contentdata = $("errorWindowMsg");

	titledata.each(function(s) {
		s.innerHTML = title;
	});

	contentdata.innerHTML = content;
}

function getMouseCoordsFromEvent(e){
	var posx = 0;
	var posy = 0;
	if (!e){
		var e = window.event;
	}
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	// posx and posy contain the mouse position relative to the document
	// Do something with this information

	return new Array(posx,posy);
}

function ajaxContentPop(content_name){
	var aDiv = $("ajax_content_popup");

	moveShowPopupDiv(aDiv);

	aDiv.down(".loading").show();
	aDiv.down(".results").update("").hide();

	new Ajax.Request("/ajax.getcontent.php?id="+content_name,{
		onSuccess: function(transport){
			aDiv.down(".loading").hide();
			aDiv.down(".results").show().update(transport.responseText);
		}
	});
}