$(document).ready(function(){

    $(".hideit").hide();

	$("#accordion").accordion({

	autoheight: false

	});



	

	$("table").tablesorter({

	// pass the headers argument and assing a object

	headers: {

		// assign the fifth column (we start counting zero)

		0: {

			// disable it by setting the property sorter to false

			sorter: false

		},

		// assign the third column (we start counting zero)

		4: {

			// disable it by setting the property sorter to false

			sorter: "shortDate"

		},		

		5: {

			// disable it by setting the property sorter to false

			sorter: false

		}



	}

	});





});







function showHideCustomFields(element)

{

	if (element.checked)

	{

		$(".hideit").show();

	}

	else

	{

		$(".hideit").hide();	

	}

}



function reloadContacts(val)

{

	window.location.href="contacts.php?addrid="+val;

}



function checkAll()

{

  $("form#frmListing INPUT[type='checkbox']").attr('checked', $('#chkAll').is(':checked')); 

}



function seldelete()

{

	var selected=0;

		formx = document.frmListing;

		for(var i=1;i<formx.length;i++)

		{

			if(formx.elements[i].type=="checkbox")

			{

				if((formx.elements[i].checked) && (formx.elements[i].name.indexOf("chkAll")<0))

						{

							selected++;

						}

			}

		}

	if(selected==0)

	{

		alert("Please select atleast 1 contact to un-subscribe!");

		return false;

	}

	else

	{

		a=confirm("Are you sure you want to un-subscribe "+selected+" contacts(s)?");

	}

	if(a==true)

	{

		document.frmListing.submit();

	}

	return a;

}



function selundelete()

{

	var selected=0;

		formx = document.frmListing;

		for(var i=1;i<formx.length;i++)

		{

			if(formx.elements[i].type=="checkbox")

			{

				if((formx.elements[i].checked) && (formx.elements[i].name.indexOf("chkAll")<0))

						{

							selected++;

						}

			}

		}

	if(selected==0)

	{

		alert("Please select atleast 1 contact to subscribe (undo un-subscribe)!");

		return false;

	}

	else

	{

		a=confirm("Are you sure you want to subscribe (undo un-subscribe) "+selected+" contacts(s)?");

	}

	if(a==true)

	{

		document.frmListing.delOrUndel.value='undel';

		document.frmListing.submit();

	}

	return a;

}



function ajaxFileUpload()

{

	$("#loading")

	.ajaxStart(function(){

		$(this).show();

		$('#ajaxpreloader').html("");

	})

	.ajaxComplete(function(){

		$(this).hide();

	});



	var addrid = document.getElementById("addridImport").value;



	$.ajaxFileUpload

	(

		{

			url:'doajaxfileupload.php?addrid='+addrid,

			secureuri:false,

			fileElementId:'fileToUpload',

			dataType: 'json',

			success: function (data, status)

			{

				if(typeof(data.error) != 'undefined')

				{

					if(data.error != '')

					{

						//alert(data.error);

						$('#ajaxpreloader').html(data.error);

					}else

					{

						$('#ajaxpreloader').html(data.msg);

					}

				}

			},

			error: function (data, status, e)

			{

				alert(e);

			}

		}

	)

	

	return false;

}



function checkExport()

{

	var selected=0;

	formx = document.frmExport;

	for(var i=0;i<formx.length;i++)

	{

		if(formx.elements[i].type=="checkbox")

		{

			if(formx.elements[i].checked)

			{

				selected++;

			}

		}

	}

	if(selected==0)

	{

		alert("Please select atleast 1 address book to export!");

		return false;

	}

	else

	{

		return true;

	}

}