$(document).ready(function() 
{
    lastFocused = '';
    $("input").live('click',function(){
        
        lastFocused = $(this).attr('name');
    });

    $("textarea").live('click',function(){
        lastFocused = $(this).attr('name');
    });
    
	$("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
		},
		1: {
			// apply date format
			sorter: "shortDate"
		},
		
		// assign the third column (we start counting zero)
		4: {
			// disable it by setting the property sorter to false
			sorter: false
		},
		// assign the third column (we start counting zero)
		5: {
			// disable it by setting the property sorter to false
			sorter: false
		}
		

	}
	});

	$('#ActionsDiv').hide();
	$('#insertActions').click(function()
	{
		$('#ActionsDiv').slideToggle('slow');
	})
	
	$('#actionButton').click(function()
	{
		$('#Tab1').show();
		$('#Tab2').hide();
		$('#actionButton').addClass('active');
		$('#personalizeButton').removeClass('active');
	})
	
	$('#personalizeButton').click(function()
	{
		$('#Tab1').hide();
		$('#Tab2').show();
		$('#actionButton').removeClass('active');
		$('#personalizeButton').addClass('active');
	})
	
});

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 campaign to delete!");
		return false;
	}
	else
	{
		a=confirm("Deleting the campaign will also delete its report data and geo-map data.\nAre you sure you want to delete "+selected+" campaign(s)?\nData once deleted cannot be reverted back.");
	}
	if(a==true)
	{
		document.frmListing.submit();
	}
	return a;
}

function insertAtCursor(myField, myValue) 
{
    //console.log(lastFocused);
    lastFocused = $.trim(lastFocused);
    
    if ( lastFocused == 'areaBody' || lastFocused == 'txtSubject' ) {    
    if ( lastFocused != '' ) {
    	//myField = document.getElementsByName('"'+lastFocused+'"')[0];
    	var textbox = document.querySelector('[name="'+lastFocused+'"]');
    	if(lastFocused != 'areaBody' && lastFocused != 'areaBodyText' ) {
        	myField = document.querySelector('[name="'+lastFocused+'"]');
    	}/*if ( lastFocused == 'areaBodyText' ) {
    		myField = document.formcampaign.areaBodyText;
    	}*/
    }
    //var startPos = 0;
    /*console.log(myField);
    console.log(myField.selectionStart);*/
    //IE support
	if (document.selection) 
	{
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	}
	else if (myField.selectionStart || myField.selectionStart == '0') 
	{
		var startPos = myField.selectionStart;
		//console.log(startPos);
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
	} 
	else 
	{
		myField.value += myValue;
	}
	}
}

function previewWindow(id)
{
	newWin = window.open("preview.php?id="+id,null,"height=650,width=905,status=yes,toolbar=0,scrollbars=1,menubar=no,location=no,resizable=no" );
}

