serializeForm = function(theform) {
	var els = theform.elements;
	var len = els.length;
	var queryString = "";
	this.addField = 
		function(name,value) { 
			if (queryString.length>0) { 
				queryString += "&";
			}
			queryString += encodeURIComponent(name) + "=" + encodeURIComponent(value);
		};
	for (var i=0; i<len; i++) {
		var el = els[i];
		if (!el.disabled) {
			switch(el.type) {
				case 'text': case 'password': case 'hidden': case 'textarea': 
					this.addField(el.name,el.value);
					break;
				case 'select-one':
					if (el.selectedIndex>=0) {
						this.addField(el.name,el.options[el.selectedIndex].value);
					}
					break;
				case 'select-multiple':
					for (var j=0; j<el.options.length; j++) {
						if (el.options[j].selected) {
							this.addField(el.name,el.options[j].value);
						}
					}
					break;
				case 'checkbox': case 'radio':
					if (el.checked) {
						this.addField(el.name,el.value);
					}
					break;
			}
		}
	}
	return queryString;
};

function divRequest(url,div){
	var div=div;
	$.ajax({
		url:url,
		type:'GET',
		success:function(msg){
			$("#"+div).html(msg);
			
		}
	});
}

function submitForm(form){
	$.ajax({
		url:form.action,
		type:'POST',
		data:serializeForm(form),
		success:function(msg){
			createLightBox(msg,3000);
		//alert(msg);
		}
	});
}

$(document).ready(function(){
							   			
	$("#newsletter_sendoutbutton").click(function(){
												  

		if (confirm('Are you sure you wan\'t to send out newsletter?')){

			current_action=document.form1.act.value;
			document.form1.act.value='newsletter';
		
			LightBox('Sending newsletter. Please wait until done...','',20000000000);
			$.ajax({
				url:'index.php?c=ajax&template=blank',
				type:'POST',
				data:serializeForm(document.form1),
				success:function(msg){
					document.form1.act.value=current_action;
					
					eval("var response="+msg);
					if (response.success){
						LightBox('','Newsletter sent successfully!');
					} else if (response.errors){
						LightBox(response.errors.join(';'));	
					}

											
				}
			});
		}
	});
	
	//blog archive
	$(".blog_archive a.header").bind("click",function(){
		var target_month=$("div.month",this.parentNode);
		
		if (target_month.css('display')=='none')
			target_month.slideDown("fast");
		else
			target_month.slideUp("fast");													  	
	});

	$(".blog_archive .month_item a.header").bind("click",function(){
		var target_month=$("div.items",this.parentNode);
		
		if (target_month.css('display')=='none')
			target_month.slideDown("fast");
		else
			target_month.slideUp("fast");													  	
	});
	
});
