/**
    * Login.js (should be)
    *
    * This script is only for login.aspx. Requires and depends on another script called browserInfo.
    * Most of the elements referenced from this script are within login.aspx - therefore if you are
    * copying and modifying this code, please take extra care with variable and references.
    *
    * TODO: MAKE THIS FILE A CLASS
    */

// Predefined image paths
// IMPORTANT: Make sure you check paths upon editing
var imageArray = new Array("../ADMIN/images/NEW/login/check/not_supported.jpg", // 0: not supported
		                "../ADMIN/images/NEW/login/check/supported.jpg",     // 1: supported
		                "../ADMIN/images/NEW/login/check/warning.jpg",       // 2: warning
		                "../ADMIN/images/NEW/login/check/windows.gif",       // 3: microsoft windows
		                "../ADMIN/images/NEW/login/check/unix.gif",          // 4: unix
		                "../ADMIN/images/NEW/login/check/mac.gif");          // 5: mac os

// Browser information
var userOS = BrowserInfo.OS;
var userBrowser = BrowserInfo.browser + " " + BrowserInfo.version;
var userJS = hasJS();
var userResolution = checkResolution();

/**
    * browserInfo.js already contains an object called BrowserInfo and 
    * has the following functions:
    *  - BrowserInfo.browser returns a string
    *  - BrowserInfo.version returns an integer
    *  - BrowserInfo.OS returns a string
    * and are all client side operations.
    */

// user OS and user Browser incl. version number
function loadPage() {                
	// When user loads this page every control is disabled. Only when
	// JavaScript is enabled, will the controls be enabled.
	disableAllForms();
    
	if (checkCompatibility() == true) {
		enableAllForms();
	} else {
		if (screen.width < 1024) {
		    document.getElementById("rwText").innerHTML = "You screen size is too small. " +
		                                                    "Please change your screen resolution to 1024x768 and try again. " +
		                                                    "If you need assistance, please contact Amblique at (02) 9888 8888";
		    document.getElementById("resolutionWarning").style.display = "block";
		}
	}
}

// Check if browser has javascript, supported OS and supported screen resolution
function checkCompatibility() {
	if (document.getElementById) {
		var labelJS = document.getElementById("JavaScript");
		var labelOS = document.getElementById("OS");
		var labelRes = document.getElementById("Resolution");
        
		// Javascript
		if (userJS == true) {
		    labelJS.innerHTML = "Supported";
		    document.getElementById("CheckJavaScript").src = this.imageArray[1];
		} else {
		    labelJS.innerHTML = "Not supported";
		    document.getElementById("CheckJavaScript").src = this.imageArray[0];
		}
        
		// OS Platform
		labelOS.innerHTML = userOS;
        
		if (userOS.indexOf("Windows") == 0) {
		    // Windows
		    document.getElementById("OSLogo").src = this.imageArray[3];
		    document.getElementById("CheckPlatform").src = this.imageArray[1];
		} else if (userOS.indexOf("Mac") == 0) {
		    // Mac OS
		    document.getElementById("OSLogo").src = this.imageArray[5];
		    document.getElementById("CheckPlatform").src = this.imageArray[1];
		} else if (userOS.indexOf("Linux") == 0) {
		    // Unix based
		    document.getElementById("OSLogo").src = this.imageArray[4];
		    document.getElementById("CheckPlatform").src = this.imageArray[3];
		} else {
		    document.getElementById("OSLogo").src = this.imageArray[2];
		    document.getElementById("CheckPlatform").src = this.imageArray[3];
		}
        
		// Resolution
		labelRes.innerHTML = userResolution;
        
		if (screen.width < 1024) {
		    document.getElementById("CheckResolution").src = this.imageArray[0];
		} else {
		    document.getElementById("CheckResolution").src = this.imageArray[1];
		}
        
		// Both
		if (this.userJS == false || screen.width < 1024) {
		    return false;
		} else {
		    return true;
		}
	}
}

// Disable access to all forms
function disableAllForms() {
	var emailField = document.getElementById("tbEmail");
	var passwordField = document.getElementById("tbPassword");
    
	emailField.value = 'Disabled';
	passwordField = '';
    
	emailField.disabled = true;
	passwordField.disabled = true;
    
	document.getElementById("tbEmail").style.display = "none";
	document.getElementById("tbPassword").style.display = "none";
	document.getElementById("btnLoging2").style.display = "none";
}

// Enable access to all forms
function enableAllForms() {	
	var emailField = document.getElementById("tbEmail");
	var passwordField = document.getElementById("tbPassword");
    
	emailField.value = '';
	passwordField = '';
    
	emailField.disabled = false;
	passwordField.disabled = false;
    
	document.getElementById("tbEmail").style.display = "";
	document.getElementById("tbPassword").style.display = "";
	document.getElementById("btnLoging2").style.display = "";
}

// This obviously has Javascript enabled
function hasJS() {
    return true;
}

// Returns screen resolution in string format: "1234 x 567"
function checkResolution() {
	return screen.width + " x " + screen.height;
} 
