/**
	KintekCMS Javascript Common
   
	VERSION:			1.0
	LAST MODIFIED:		24 - 09 - 09
	MODIFIED BY:		Madhava
   
*/

/**
	Version 1.0
	
	Documentation:
	
	- Auto lineup_icons for famfamfam icons
	- auto delete confirms
	- live confirm class which now uses attr('confirm')
	
	Future Improvements
	- Make alertMessage use Notices
*/

// All praise be to jQuery
$(function() {
	
	// Fix famfam silk icons alignment
	$('img[src*=images/icons/]').addClass('lineup_icon');
	
	// Confirm certain link clicks
	$('.confirm').live('click', function(e) {
		link = $(this);
		if (typeof(link.attr('confirm')) != 'undefined') {
			return confirm(link.attr('confirm'));
		} else {
			return confirm('Really?');
		}
	});

	// Apply delete links
	$(".delete").live('click',attachAjaxDeleteEvent);

	// Focus on username in login form
	$("#loginForm").find("#user_name").focus();

	// Submit login form
	$("#loginLink").click(function(e) {
		e.preventDefault();
		$("#loginForm").submit();
	});

	// Basic Ajax Links
	$('.ajaxLink').click(function(e) {
		e.preventDefault();
		$.post($(this).attr('href'), function(d) {
			alert(d);
		});
	});

	// Form validation
	$("form").submit(function() {
		var alertMessage = "";
		$(this).find(".required").each(function() {
			if ($(this).val()=="" || $(this).val()==$(this).attr("rel")) {
				alertMessage += $(this).attr("rel")+" is required\n";
			}
		});
		if ($(this).find("#re_password").length!=0 && $(this).find("#re_password").val()!=$(this).find("#password").val()) {
			alertMessage += "Passwords do not match\n";
		}
		if ($(this).find("#re_password").length!=0 && $(this).find("#re_password").val()!=$(this).find("#password").val()) {
			alertMessage += "Passwords do not match\n";
		}
		if ($(this).find("input[name=paymentMethod]").length && $(this).find("input[name=paymentMethod]:checked").length==0) {
			alertMessage += "You must select a Payment Type\n";
		}
		if ($(this).find("input[name=postageOption]").length && $(this).find("input[name=postageOption]:checked").length==0) {
			alertMessage += "You must select a Postage Method\n";
		}
		if ($(this).find(".iAgree").length != $(this).find(".iAgree:checked").length) {
			alertMessage += "You must agree to our Terms and Conditions\n";
		}
		if (alertMessage.length) {
			alert(alertMessage);
			return false;
		}
		return true;
	});
	
	// Apply date inputs	
	$('.datepicker').datepicker({ dateFormat: 'dd-mm-yy' });
	

	// Very simple Ajax link
	$(".ajax").click(function(e) {
		e.preventDefault();
		$.post($(this).attr("href"), function(d) {
			alert(d);
		});
	});
	
	// Apply tablesorters
	$("table.sortableTable").tablesorter({
		sortList: [[1,0]]
	});
	
	// Help!
	$("#helpLink").click(function(e) {
		e.preventDefault();
		$("#info,.info").slideToggle();
	});
	
	$('table.rowme').each(function() {
		$(this).find('tr:even').addClass('stripe');
	});
	
}); 


function attachAjaxDeleteEvent(e) {
	e.preventDefault();
	var parentRow;
	if ($(this).parents("tr")[0]) {
		parentRow = $(this).parents("tr")[0];
	}
	else if ($(this).parents("li")[0]) {
		parentRow = $(this).parents("li")[0];
	}
	if (confirm("Really Delete?")) {
		$.get($(this).attr("href"), function(d) {
			if (d=="Success") {
				$(parentRow).hide();
			}
			else alert("UNEXPECTED RESPONSE FROM SERVER: "+d);
		});
	}
}

function noticeMessage(notices)
{
	var noticeIcon = '<span class="ui-icon ui-icon-check" style="float: left; margin-right: 0.3em; margin-top:2px;"/>';
	$('#noticeMessage').html('');
	for(var i in notices)
	{
		$('#noticeMessage').append('<p>' + noticeIcon + notices[i] + '</p>');
	}
	$('#noticeMessage').dialog({
		close: function() { $(this).dialog('destroy') }
	});
}

function errorMessage(errors)
{
	var errorIcon = '<span class="ui-icon ui-icon-alert" style="float: left; margin-right: 0.3em; margin-top:2px;"/>';
	$('#errorDialog').html('');
	for(var i in errors)
	{
		if(errors[i].field)
		{
			$('#errorDialog').append('<p>' + errorIcon + errors[i].field +errors[i].reason + '</p>');
		}
		else{
			$('#errorDialog').append('<p>' + errorIcon + errors[i]);
		}
	}
	$('#errorDialog').dialog({
		close: function() { 
			$('#errorDialog').html(''); 
			$(this).dialog('destroy'); 
		}
	});	
}

function alertMessage(message) {
	alert(message);
}