// JavaScript Document
$(document).ready( function() {

	/* behaviours for Manufacturer / Model drop-down selectors in site header */
	$("#selManufacturer").change( function() {
		var name = $(this).val();
		$("#selModel").attr("disabled", "disabled");
		$.ajax({
			type: "POST", 
			url: "/"+cTemplate+"/AJAX/lampModels.html", 
			data: "name="+encodeURIComponent(name), 
			dataType: "xml", 
			async: false,
			success: function(xml) { populateModels(xml); $("#selModel").attr("disabled", ""); }, 
			error: function() { alert(cERROR+" - AJAX failed ??"); $("#selModel").attr("disabled", ""); }	
		});
	});
	
	$("#selModel").change( function() {
		var mo = this.value;
		var ma = $("#selManufacturer").val();
		if (mo!='' && ma!='') {			
			var ma_url = encodeURIComponent(ma);
			var mo_url = encodeURIComponent(mo);
			var mo_url = mo_url.replace('%2B', '+');
			var mo_url = mo_url.replace('%2F', '%7E'); /* Replaces '/' with '~' */
			var loc = "/"+cTemplate+"/lamps/"+ma_url+"/"+mo_url+".html";
			//var loc = "/"+cTemplate+"/__view_lamp.asp?manu="+ma_url+"&item="+mo_url;
			document.location.href = loc;
		};
	});
	
	/* global behaviour for QTY entry boxes */
	$(".qty").focus( function() { 
				this.select(); this.style.textAlign='left'; 
		}).keypress( function(e) { 
				return keyCheck(e); 
		}).blur( function() { 
				 this.style.textAlign='right'; 
				 if(this.value==''){this.value='0';}; 
		});
	
	/* behaviours for the language selector */
	$("#selLanguage").change( function() {
		location.href=this.value;									   
   });
	
	// Session timeout timer
	if (IsLoggedIn) {
		setInterval('CheckSessionTime()', 1000);
	};	
	
	/** 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(''); }});
	
	
});

/*
	This function uses and AJAX call to populate the Lamp Model selector 
	based on the selected lamp manufacturer
*/
function populateModels(xml) {
	var error = $("error", xml).text();
	
	if(error != '') {
		// HACK FIX FOR GERMANY
		var error = "kein Hersteller ausgewählt...";
		// ------
		$("#selModel").empty().addOption("", error, true);
	} else {
		$("#selModel").empty().addOption("", "-- "+cCHOOSE+" --", true);
		var models = $("lamps", xml).text();
		var arrModels = models.split("|");
		$.each(arrModels, function(i) { var m = arrModels[i]; $("#selModel").addOption(m, m, false); } );
	};
}

// Only allow numeric keypresses
function keyCheck(e) {
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	if (((keycode>47) && (keycode<58) )  || (keycode==8)) { return true; }
	else return false;
}

//////////////////////////////////////////////////////////////////////////////
// ************** LOGIN TIMEOUT CODE ******************* //
//////////////////////////////////////////////////////////////////////////////

// Session timer
var startTime = new Date();


// DEBUG VALUES
//var sessionPeriod = 20; 
//var warningTime = 10; // seconds

// RELEASE VALUES
var sessionPeriod = TimeoutValue; 
var warningTime = 120;

var warning = 0;
var ended = 0;

// Session check functions
function CheckSessionTime() {
	
	now = new Date();
	
	var elapsedtime = Math.floor((now.getTime() - startTime.getTime()) / 1000);
	var timeleft = sessionPeriod - elapsedtime;
	
	
	if (timeleft > -1) {
		
		$("#TB_ajaxContent span.timeleft").html(timeleft);
		
		// Detect the time when to display a warning
		if(!warning) {
			if ((now.getTime() - startTime.getTime()) / 1000 >= sessionPeriod - warningTime) {
				warning = 1;	
				TB_show(cSESSION_WARNING, "#TB_inline?height=100&width=450&inlineId=sessionWarning", false);
			}
		}
		
		// Detect a time to display endded session message
		if (!ended) {
			if ((now.getTime() - startTime.getTime()) / 1000 >= sessionPeriod) {
				ended = 1;
				
				TB_remove();
				setTimeout("TB_show(cSESSION_TIMEOUT, '#TB_inline?height=100&width=450&inlineId=sessionEnded', false);", 500);
			}
		}
	}	
}

// Reset the page load time. Used during ajax calls to reset the session timeout timer
function ResetTimer() {
	startTime = new Date();
	warning = 0;
	ended = 0;
}

function StartTimer() {
	setInterval('CheckSessionTime()', 1000);
}

function StayAlive() {
	$.ajax({
		type: "POST",
		url: "/stayalive.html",
		data: "action=stayalive",
		success: function(msg){
			TB_remove();
			ResetTimer();
		}
	});
}

function swapBoxes() {
	TB_remove();
	setTimeout("TB_show('', '#TB_inline?height=200&width=450&inlineId=passwordContent', false);", 200);
	return false;
}

//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////








