// JavaScript Document
$(document).ready( function() {
	
//Add odd class to odd table rows for zebra striping
	$("tr:nth-child(odd)").addClass("odd");	
	
	
//Text resizing 
	$.ajaxSetup({
		type: "POST",
		url: "/textsize/",
		dataType: "html",
		async: false
	});
	$("a.size1").click( function() { $("body").removeClass().addClass("size1"); $.ajax({ data: "action=textsize&size=1" }); return false; });
	$("a.size2").click( function() { $("body").removeClass().addClass("size2"); $.ajax({ data: "action=textsize&size=2" }); return false; }); 
	$("a.size3").click( function() { $("body").removeClass().addClass("size3"); $.ajax({ data: "action=textsize&size=3" }); return false; });
	
	
//Sifr (Flash replacement)
	initSIFR();
	
//Equalize column heights
	var leftHeight = 0;
	var rightHeight = 0;
	
	leftHeight = $(".leftColumn").height();
	rightHeight = $(".rightColumn").height();
	
	if (rightHeight > leftHeight) {
		$(".leftColumn").height(rightHeight);
	}
	
	if (leftHeight > rightHeight) {
		$(".rightColumn").height(leftHeight);
	}

// apply login form behavious
	$("#frmLogin").bind("submit", function() { return false; });
	$("#btnLoginSubmit").click( function() { callLoginC2K(); });
	$(".clearonfocus").bind("focus", function() { $(this).val(""); });
	
});


function ajaxContent(sURL) {
	
	var current = $.cookie("ajax_page");
	if (sURL != current) {
		$.ajax({
		   type: "GET",
		   url: sURL,
		   dataType: "html",
		   async: false,
		   success: function(html) { $("#holder").html(html); $.cookie("ajax_page", sURL); initSIFR(); },
		   error: function() { alert("error!"); } 
		});
	}
}

function initSIFR() {	
	$.sifr({
		path: '/sifr/',
		save: true
	});	
	$('h1').sifr({ font: 'frutiger65' });
}

/*
	This function uses and AJAX call to login in the user.
	Other functions ar called following a successful login to handle interface updates
*/

function callLoginC2K() {
	var sLoading = "<img src=\"/gfx/icon_loading.gif\" width=\"16\" height=\"16\" alt=\"authenticating...\" />&nbsp;Authenticating...";
	//$("#authenticateC2K").html(sLoading);
	
	var u = $("#txtUsername").val();
	var p = $("#txtPassword").val();
	var a = $("#txtAccountNo").val();
	var t = $("#txtTypeC2K").val();
	var s = (typeof $("#chkRemember").attr('checked') != 'undefined') ? true : false;
		
	$.ajax({
		type: "GET",
		url: "/default.asp?action=login&username="+u+"&password="+p+"&account="+a+"&type="+t,
		dataType: "xml",
		success: function(xml) {  
			if(s) { $.cookie('txtUsernameC2K', u, {expires: 365 }); $.cookie('txtAccountC2K', a, {expires: 365 }); $.cookie('txtPasswordC2K', p, {expires: 365 });}
			handleLoginC2K(xml); },
		error: function() { $("#authenticateC2K").html(""); alert("AJAX error"); }
	});
}

function handleLoginC2K(xml) {
	var error = $("error", xml).text();
	var success = $("success", xml).text();
	if (error != '') { $("#authenticateC2K").html(""); alert(error); }
	else { 
		$.ajax({
			type: "POST",
			url: "/default.asp",
			data: "action=logindetails",
			dataType: "html",
			success: function(html) { $("#login").replaceWith(html); },
			error: function() { alert("AJAX error"); }
		});	
		
	};
}

