// JavaScript Document
$(document).ready(function(){
	
	$("a.external").click(function(){
		window.open($(this).attr('href'));
		return false;
	});
	
	$(".showInstruction").each(function(){
		var instruction = $(this).next().html();
		$(this).next().remove();
		$(this).parent().append("<div class=\"input_instructions\" style=\"display:none\"><div class=\"input_instruction_arrow\"></div>" + instruction + "</div>");
	})
	$(".showInstruction").focus(function(){
		$(this).next().show();   
	});
	$(".showInstruction").blur(function(){
		$(this).next().hide();
	});
	
	
	/**Start Form Label*/
	$("input:text.form_label, textarea.form_label").each(function(){
		if($(this).val().length == 0 || $(this).val() == $(this).attr('title')){
			$(this).val($(this).attr('title'));
			$(this).addClass('empty');
		} else {
			$(this).addClass('not_empty');
		}
		
		$("label[for="+$(this).attr("id")+"]").parent().remove();
		//$("label[for="+$(this).attr("id")+"]").remove();
		$(".inline").css('display','inline-block');
	});
	$("input:text, .required").each(function(){
		$(this).css("border", "#7F9DB9 1px solid");				  
	});
	$("input:text.form_label, textarea.form_label").focus(function(){
		if($(this).hasClass('not_empty')){
			return;
		} else {
			$(this).addClass('dim');
		}
	});
	$("input:text.form_label, textarea.form_label").keydown(function(){
		if($(this).hasClass('dim')){
			$(this).val('');
			$(this).addClass('not_empty');
			$(this).removeClass('empty');
			$(this).removeClass('dim');
		}
	});
	$("input:text.form_label, textarea.form_label").change(function(){
		$(this).addClass('not_empty');
		$(this).removeClass('empty');
		$(this).removeClass('dim');
	});
	$("input:text.form_label, textarea.form_label").blur(function(){
		if($(this).val().length == 0 || $(this).hasClass('empty')){
			$(this).removeClass('not_empty');
			$(this).removeClass('dim');
			$(this).addClass('empty');
			$(this).val($(this).attr('title'));
			if($(this).hasClass("required")){
				$(this).css("border", "#C00 2px solid");	
			}
		} else {
			$(this).addClass('not_empty');
			$(this).removeClass('empty');
			/// Border Not required
			$(this).css("border", "#7F9DB9 1px solid");
		}
	});
	$("select.required").change(function(){
		if($(this).val().length == 0){
			$(this).css("border", "#C00 2px solid");	
		} else {
			/// Border Not required
			$(this).css("border", "#7F9DB9 1px solid");
		}
	});
	$("form").submit(function(){
		$form_status = true;
		
		$(".form_label.empty").each(function(){
			$(this).val('');
		});
		$first_missing_element = false;
		$(".required").each(function(){
			if($(this).val().length == 0){
				if($first_missing_element == false){
					$first_missing_element = true;
					$id = $(this).attr("id");
					$form_status = false;
				}
				$(this).val($(this).attr('title'));
				$(this).css("border", "#C00 2px solid");
			}
		});
		
		$(".validate-email").each(function(){
			$regex = new RegExp(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$/);
			if(!$regex.test($(this).val())){
				$(this).css("border", "#C00 2px solid");
				if($first_missing_element == false){
					$first_missing_element = true;
					$id = $(this).attr("id");
					$form_status = false;
				}
			}			
		});
		
		$(".validate-numbers-only").each(function(){
			$regex = new RegExp(/^\d+$/);
			if(!$regex.test($(this).val()) && $(this).val().length != 0 ){
				$(this).css("border", "#C00 2px solid");
				if($first_missing_element == false){
					$first_missing_element = true;
					$id = $(this).attr("id");
					$form_status = false;
				}
			}			
		});
		
		$(".validate-cc-number").each(function(){
			$regex = new RegExp(/(^\d{12,19}$|^xxxx\-xxxx\-xxxx\-\d{4}$)/);
			if(!$regex.test($(this).val())){
				$(this).css("border", "#C00 2px solid");
				if($first_missing_element == false){
					$first_missing_element = true;
					$id = $(this).attr("id");
					$form_status = false;
				}
			}
		});
		
		
		if(!$form_status){
			$element = document.getElementById($id);
			scrollToTarget($element);
			$($element).focus();
		}
		if(!$form_status){
			alert("Please verify all highlighted fields ");	
		}
	
		return $form_status;
	});
	/** End Form Label **/
	
		function scrollToTarget(target){
			var targetOffset = $(target).offset().top;
			$('html,body').animate({scrollTop: targetOffset}, 1000);
		}
		
		$(".validate-email").blur(function(){
			$regex = new RegExp(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$/);
			if(!$regex.test($(this).val())){
				$(this).css("border", "#C00 2px solid");
			}			
		});
		
		$(".linked_option").each(function(){
			$(this).parent().css('display','none');
		})
		
		$(".validate-numbers-only").blur(function(){
			$regex = new RegExp(/^\d+$/);
			if(!$regex.test($(this).val())){
				$(this).css("border", "#C00 2px solid");
			}
		});
		
		$(".validate-cc-number").blur(function(){
			$regex = new RegExp(/(^\d{12,19}$|^xxxx\-xxxx\-xxxx\-\d{4}$)/);
			if(!$regex.test($(this).val())){
				$(this).css("border", "#C00 2px solid");
			}			
		});
		$("#donation_amount").trigger('onchange');
		
		$(".toggle_next_sibling").click(function(){
			$(this).parent().next().toggle("normal");
			return false;
		});
});

function checkOptions(element, match_option, show_elements){
	if($(element).val() == match_option){
		$.each(show_elements, function(){
			showOption(this);
		});
	} else {
		$.each(show_elements, function(){
			hideOption(this);
		});
	}
}
function showOption(id){
	$('#'+id).parent().show();
	$('#'+id).focus();
}
function hideOption(id){
	$('#'+id).parent().hide();
}

//Apply Drop Down Styles for IE 6
if(jQuery.browser.msie){
		
	//Apply Drop Down Styles for IE 6
	var msie_version = (parseInt(jQuery.browser.version));
	
	if(msie_version == 6){
		$(".our_story").hover(function(){
			$(this).children('ul:first').addClass("msiefix");
			//$("ul#main-nav li.home_nav ul").bgiframe();
		}, function(){
			$(this).children('ul:first').removeClass("msiefix");}
		);
		$(".whats_happening").hover(function(){
			$(this).children('ul:first').addClass("msiefix");
			//$("ul#main-nav li.home_nav ul").bgiframe();
		}, function(){
			$(this).children('ul:first').removeClass("msiefix");}
		);
	}
}

