// JavaScript Document
$(document).ready( function() {

	/** Disable links /#/ **/
	$("a").click(function() { 
		if($(this).attr("href") == "/#/") {
			return false;
		}
		});
	
	
	var accountTxt = $("#txtAccountNo").val();
	var usernameTxt = $("#txtUsername").val();
	var passwordTxt = $("#txtPassword").val();
	
	$("#txtAccountNo").focus(function() { if($(this).val()==accountTxt) { $(this).val(''); }});
	$("#txtUsername").focus(function() { if($(this).val()==usernameTxt) { $(this).val(''); }});
	$("#txtPassword").focus(function() { if($(this).val()==passwordTxt) { $(this).val(''); }});
	
	// apply login form behavious
	$("#frmLogin").bind("submit", function() { return false; });
	$("#btnLoginSubmit").click( function() { callLoginC2K(); });
	
});


/*
	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/gfx_loading3.gif\"  alt=\"authenticating...\" />";
	$("#login_progress").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') ? false : true;

	$.ajax({
		type: "GET",
		url: "/?action=login&username="+u+"&password="+p+"&account="+a,
		dataType: "xml",
		success: function(xml) {  
			if(s) { $.cookie('txtUsernameC2K', u, {expires: 365 }); $.cookie('txtAccountC2K', a, {expires: 365 }); $.cookie('txtPasswordC2K', p, {expires: 365 });}
			handleLoginC2K(xml);
			$("#login_progress").html(""); 
		},
		error: function() { $("#login_progress").html(""); alert("AJAX error 1"); }
	});
}

function handleLoginC2K(xml) {
	
	var error = $("error", xml).text();
	var success = $("success", xml).text();
	
	if (error != '') { $("#authenticateC2K").html(""); alert(error); }
	else {	
		$.ajax({
			type: "POST",
			url: "http://" + location.host,
			data: "action=logindetails",
			dataType: "html",
			success: function(html) { 
				$("#login").html(html);
				//UpdateStockAndPrice();
				// Have to reload page because if the difference in what is displayed
				
				if(window.location.pathname=="/") {
					location.replace("http://" + location.host + '?RELOAD_RESULTS=true');
				}
				else {
					window.location.href=window.location.href;
				}
			},
			error: function() { alert("AJAX error 2"); }
		});	
		
	};
}


function UpdateStockAndPrice() {
	
	if(IS_LOGGED_ON!='False') {
		
		var c2kcodes = '';
		
		$("table tr.result:visible table").each(function() {					
			c2kcodes += $(this).attr("id") + "|";
		});
		
		$("table tr.result:visible table .price").html("<img src=\"/gfx/gfx_loading3.gif\" alt=\"loading\" align=\"absmiddle\" />");
		$("table tr.result:visible table .stock").html("<img src=\"/gfx/gfx_loading3.gif\" alt=\"loading\" align=\"absmiddle\" />");
		//console.log(c2kcodes);
		
		if (c2kcodes!='') {
			$.ajax({
			   type: "POST",
			   url: "/__ajax/__get_productdetail.asp",
			   data: "c=" + c2kcodes,
			   dataType: "xml",
			   cache: true,
			   success: function(xml){
				  handleResults(xml);
			   },
			   error: function() { 
				$("table tr.result:visible table .price").html("N/A");
				$("table tr.result:visible table .stock").html("N/A");
			   }
			});
		}	
		
	}
	
}

function handleResults(xml) {
	
	//console.log("handling...");
	if($("Item", xml).length==0) {
		//$("table tr.result:visible table .stock").html("N/A");
		$("table tr.result:visible table .price").html("N/A");		
	}
	else {
		$("Item", xml).each( function() { 
			var sDistrib = $("DistributorItemIdentifier", this).text();
			var sPrice = $("UnitPriceAmount", this).text();
			
			if (sPrice != "") {
				sPrice = CURRENCY_CODE + "&nbsp;" + sPrice;	
			}
			else {
				sPrice = "N/A";
			}
			
			var sStock = $("AvailabilityTotal", this).text();
			
			$("#" + sDistrib + " .stock").html(sStock);
			$("#" + sDistrib + " .price").html(sPrice);
		});
	}
}




