$(function() { 
	//log in
	var loginOptions = {
		url: "/home/inc/loginout.php?t=ajax",
		target: '#head_error_div',   // target element(s) to be updated with server response 
		beforeSubmit: beforeLoginSubmit,
		success: afterLoginSubmit  // post-submit callback 
	};
	$('#loginForm').ajaxForm(loginOptions); 
	function beforeLoginSubmit(){
		$('.head_login_spinner').fadeIn('fast');
		return true;
	}
	function afterLoginSubmit(responseText){
		$('.head_login_spinner').fadeOut('fast');
		if(responseText=='Success!')
		window.location.href=window.location.href
		return true;
	}
	
	//logout 
	var logoutOptions = {
		url: "/home/inc/loginout.php?t=ajax",
		target: '#head_login_logout',   // target element(s) to be updated with server response 
		beforeSubmit: beforeLoginSubmit,//same as log in
		success: afterLogoutSubmit  // post-submit callback 
	};
	$('#logoutForm').ajaxForm(logoutOptions); 
	function afterLogoutSubmit(responseText){
		$('.head_login_spinner').fadeOut('fast');
		if(responseText=='You have been logged out')
		window.location.href=window.location.href
		return true;
	}

	//FORMAT PHONE NUMBERS
	function formatPhoneNumber(phoneField) { 
		var input = $(phoneField).val();
		var regex1 = /[\D]/g;
		 
		if (input.length > 0) {
			cleaninput = input.replace(regex1, '');
			area = cleaninput.substr(0,3);
			exch = cleaninput.substr(3,3);
			num = cleaninput.substr(6,4);
			modifiedinput = area+"-"+exch+"-"+num;
			$(phoneField).val(modifiedinput);
		}
	};
	//Format phone number when updated
	$(".phoneNumber").blur(function() {
		formatPhoneNumber($(this));
	});
	
	//ALL CAPS
	/*function allCaps(textarea) {
		$(textarea).val($(textarea).val().toUpperCase());
	}
	//make address fields all caps on update
	$(".allcaps").blur(function() {
		allCaps($(this));
	});*/
	
	//highlight fields
	$('.updateMemberField').hover(
		function(){
			 $(this).addClass('inplace_hover'); //mouseover
		},
		function(){
			 $(this).removeClass('inplace_hover'); //mouseover
	});

	//select proper region for county
	$('#county').change(function(){
		if($("#region").hasClass("dropdown")) return false;
		var newCounty = $('#county').val();
		$.post("/admin/inc/ajaxreturn.php?return=region", { county: newCounty },
			function(data){ 
				$("#region").empty().append(data);
		});
	});
	
	//if state other than AR is selected, disable county
	//can't disable form field b/c ajax form won't submit disabled fields
	$('#state').change(function(){
		var newState = $('#state').val();
		if(newState == 'AR'){
			if($('#county').val() == "OUT OF STATE") $('#county').val('');
			$('#county').removeAttr("disabled");
		}else{
			$('#county').val("OUT OF STATE");
			//$('#county').attr("disabled", true);
		}
	});
	
	//calendar dropdown
	$('#calMonthDropdown').change(function(){
		var jumpMonth = $('#calMonthDropdown').val();
		var baseURL = "/home/calendar/";
		location.href = baseURL + jumpMonth;
	});
	
	//toggle detail div
	$('.showDetail').click(function(){
		var detailID = $(this).attr('href');
		
		$(detailID).slideToggle('normal');
		if($(this).text() == "More>>"){
			$(this).text("<<Less");
		}else{
			$(this).text("More>>");
		}
		return false;
	});
	
	//show details for administrative position
	$('.showPositionDetail').click(function(){
		var detailID = $(this).attr('href');

		$(detailID).fadeIn('normal');
		//window.location.hash = detailID.replace('#','');
		return false;
	});
	//hide details for administrative position
	$('.hidePositionDetail').click(function(){
		var detailID = $(this).attr('rel');
		//alert(detailID);
		$('#'+detailID).fadeOut('normal');
		return false;
	});
	//check for hash in url
	var hash = window.location.hash;
	$('.showPositionDetail').each(function(){
		var detailID = $(this).attr('href');
		if(hash==detailID){
			$(detailID).fadeIn('normal');
		}
	});
	
	//zebra table
	$(".zebraTable tbody tr:nth-child(even)").addClass("evenRow");

	$('.emailAddress').livequery(function(){
		$('.emailAddress').emailProt();
	});

	//open links to external sites in a new window/tab
	$("a[href*='http://']:not([href*='"+location.hostname+"'])").attr("target","_blank");

});

function simplePreload(){
	var args = simplePreload.arguments;

	document.imageArray = new Array(args.length);
	for(var i=0; i<args.length; i++){
		document.imageArray[i] = new Image;
		document.imageArray[i].src = args[i];
	}
}
//<a class="emailAddress" rel="executive.secretary*arkarpa.org">[optional text]</a>
jQuery.fn.emailProt = function(e) {
	$(this).each(function(){
		e = this.rel.replace('*','@');
		this.href = 'mailto:' + e;
		if(!$(this).text()) $(this).text(e);
	});
};


function trim(str){
	return str.replace(/^\s+|\s+$/g, '');
}

