/* 
========================================================================================================= CREDITS
Copyright 	: Copyright (c) 2009 Larry Daughenbaugh & The Red Star Bar & Grill. All Rights Reserved.
Author(s) 	: Larry Daughenbaugh - ldaughenbaugh@systemsynapse.com
Date      	: 3/13/2009
Notes     	: JavaScript file for handling various events ... onLoad, onClick, etc
========================================================================================================= CHANGE LOG
Date		Name			Desc
---			---				---
========================================================================================================= BEGIN JAVASCRIPT
*/
/* ====================================================================================================== MULTIPLE ONLOAD HANDLER */
var onLoadFunctions = new Array();
var iloadFunction = 0;

// Pass each function that needs to load
function addOnLoad(func) {
    onLoadFunctions[iloadFunction] = func;
    iloadFunction++;
}
// Loops through all of the functions that were added
function loadAllFunctions() {
    for(i=0; i < onLoadFunctions.length; i++) {
        eval(onLoadFunctions[i]+"()");
    }
}
// Load all of the functions that you've set
window.onload = loadAllFunctions;

/* ====================================================================================================== DROPDOWN JUMP MENU */
function jumpMenu(targ,selObj,restore){
    eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
    if (restore) selObj.selectedIndex=0;
}

/* ====================================================================================================== CALL FUNCTIONS TO BE LOADED */
addOnLoad("preloadImages");
addOnLoad("loadJumpMenus");
addOnLoad("loadToolTips");
addOnLoad("attachDeleteAlerts");
addOnLoad("validateForms");

/* ====================================================================================================== IMAGE PRELOADER */
function preloadImages() {
    // Array of any images you want to preload 
    var ImagesToPreload = new Array();
        ImagesToPreload[0] = '/images/navHistoryOn.gif'
        ImagesToPreload[1] = '/images/navMenuOn.gif'
        ImagesToPreload[2] = '/images/navEventsOn.gif'
        ImagesToPreload[3] = '/images/navPrivatePartiesOn.gif'
        ImagesToPreload[4] = '/images/navPhotosOn.gif'
        ImagesToPreload[5] = '/images/navLocationOn.gif'
        ImagesToPreload[6] = '/images/navContactOn.gif'


	var graphics = new Array();
    for (iLoad=0; iLoad < ImagesToPreload.length; iLoad++) {
        graphics[iLoad] = new Image();
        graphics[iLoad].src = ImagesToPreload[iLoad];
    }
}

/* ====================================================================================================== IMAGE PRELOADER */
function loadJumpMenus() {
	if (document.getElementById("showSection") != null) {
		document.getElementById("showSection").onchange = function() {
			jumpMenu('parent',this,0);
		}
    }
}

/* ====================================================================================================== LOAD TOOL TIPS */
function loadToolTips() {
	// REMEMBER USERNAME TOOLTIP
	if (document.getElementById("tip0") != null) {
		var tipObj = document.getElementById("tooltip");
		var tip0 = document.getElementById("tip0");
		tip0.onmouseover = function() {
			enabletip = true;
			showTip(tipObj, "The remember username option will write a cookie to your local machine that will be used on future visits. With this option checked you will no longer have to enter your username. All that will be required will be your password.");
		}
		tip0.onmouseout = function() {
			enabletip = false;
			tipObj.style.visibility = "hidden";
			tipObj.style.left = "-9999px";
		}
		tip0.onclick = function() { return false; }
	}
	// VISIBLE TOOLTIP
	if (document.getElementById("tip_visible") != null) {
		var tipObj = document.getElementById("tooltip");
		var tip_visible = document.getElementById("tip_visible");
		tip_visible.onmouseover = function() {
			enabletip = true;
			showTip(tipObj, "The visible option determines whether this item is available to the general public. You can add items and hide them from the general public by selecting \"No.\"");
		}
		tip_visible.onmouseout = function() {
			enabletip = false;
			tipObj.style.visibility = "hidden";
			tipObj.style.left = "-9999px";
		}
		tip_visible.onclick = function() { return false; }
	}
}

/* ====================================================================================================== ALERTS FOR DELETE BUTTONS */
function attachDeleteAlerts() {
	if (document.getElementById("delete0") != null) {
		var i = 0;
		var deleteAlert = document.getElementById("delete"+i);
		while (deleteAlert) {
			deleteAlert.onclick = function() {
				var LeaveStatus = confirm('Are you sure you want to permanently delete this item?');
				if (LeaveStatus) {
					return true;
				} else {
					return false;
				}
			}
			i++;
			deleteAlert = document.getElementById("delete"+i);
		}
    }
	if (document.getElementById("logout0") != null) {
		var i = 0;
		var logoutAlert = document.getElementById("logout0");
		while (logoutAlert) {
			logoutAlert.onclick = function() {
				var LeaveStatus = confirm('Are you sure you want to log out?');
				if (LeaveStatus) {
					return true;
				} else {
					return false;
				}
			}
			i++;
			logoutAlert = document.getElementById("logout"+i);
		}
    }
}

/* ====================================================================================================== VALIDATE FORMS */
function validateForms() {
	// CHECK FOR VALID NUMBER
	function isNumeric(sText) {
		var ValidChars = "0123456789";
		var IsNumber = true;
		
		for (i = 0; i < sText.length && IsNumber == true; i++)  {
			Char = sText.charAt(i);
			if (ValidChars.indexOf(Char) == -1)  {
				IsNumber = false;
			}
		}
		return IsNumber;
	}

	// LOGIN FORM
	if (document.getElementById("login") != null) {
		var loginForm = document.getElementById("login");
		loginForm.onsubmit = function() {
			if (loginForm.username.value == '') {
				alert("Please enter your Username.");
				loginForm.username.focus();
				return false;
			}
			if (loginForm.password.value == '') {
				alert("Please enter your Password.");
				loginForm.password.focus();
				return false;
			}
			return true;
		}
	}
	// MENU FORM
	if (document.getElementById("menu") != null) {
		var menuForm = document.getElementById("menu");
		menuForm.onsubmit = function() {
			if (menuForm.title.value == '') {
				alert("Please enter the Title/Headline.");
				menuForm.title.focus();
				return false;
			}
			ddOption = menuForm.section.selectedIndex
			if (menuForm.section.options[ddOption].value == "") {
				alert("Please select a Menu Section.");
				return false;
			}
			return true;
		}
	}
	// HISTORY FORM
	if (document.getElementById("history") != null) {
		var historyForm = document.getElementById("history");
		historyForm.onsubmit = function() {
			if (historyForm.title.value == '') {
				alert("Please enter the Title/Headline.");
				historyForm.title.focus();
				return false;
			}
			if (historyForm.description.value == '') {
				alert("Please enter the Description.");
				historyForm.description.focus();
				return false;
			}
		}
	}
	// EVENTS
	if (document.getElementById("events") != null) {
		var eventsForm = document.getElementById("events");
		eventsForm.onsubmit = function() {
			if (eventsForm.title.value == '') {
				alert("Please enter the Title/Headline.");
				eventsForm.title.focus();
				return false;
			}
			if (eventsForm.description.value == '') {
				alert("Please enter the Description.");
				eventsForm.description.focus();
				return false;
			}
		}
	}
	// CONTACT
	if (document.getElementById("contact_form") != null) {
		var contactForm = document.getElementById("contact_form");
		contactForm.onsubmit = function() {
			if (contactForm.Name.value == '') {
				alert("Please enter your Name.");
				contactForm.Name.focus();
				return false;
			}
			if (contactForm.Phone.value == '') {
				alert("Please enter your Phone Number.");
				contactForm.Phone.focus();
				return false;
			}
			if (contactForm.Email.value == '') {
				alert("Please enter your Email Address.");
				contactForm.Email.focus();
				return false;
			}
			sEmail = contactForm.Email.value;
			if(sEmail != "" && sEmail.search(/^[\w-_.]*[\w-_.]@[\w].+[\w]+[\w]$/) == -1){
				alert("Please enter a VALID Email Address.");
				contactForm.Email.focus();
				return false;
			}
			invalidChars = " /:,;";
			for (i=0; i<invalidChars.length; i++) {
				badChar = invalidChars.charAt(i);
				if (sEmail.indexOf(badChar,0) > -1) {
					alert("Please enter a VALID Email Address.");
					contactForm.Email.focus();
					return false;
				}
			}
		}
	}
	// PRIVATE PARTIES
	if (document.getElementById("private_parties") != null) {
		var partiesForm = document.getElementById("private_parties");
		partiesForm.onsubmit = function() {
			if (partiesForm.Name.value == '') {
				alert("Please enter your Name.");
				partiesForm.Name.focus();
				return false;
			}
			if (partiesForm.Phone.value == '') {
				alert("Please enter your Phone Number.");
				partiesForm.Phone.focus();
				return false;
			}
			if (partiesForm.Email.value == '') {
				alert("Please enter your Email Address.");
				partiesForm.Email.focus();
				return false;
			}
			sEmail = partiesForm.Email.value;
			if(sEmail != "" && sEmail.search(/^[\w-_.]*[\w-_.]@[\w].+[\w]+[\w]$/) == -1){
				alert("Please enter a VALID Email Address.");
				partiesForm.Email.focus();
				return false;
			}
			invalidChars = " /:,;";
			for (i=0; i<invalidChars.length; i++) {
				badChar = invalidChars.charAt(i);
				if (sEmail.indexOf(badChar,0) > -1) {
					alert("Please enter a VALID Email Address.");
					partiesForm.Email.focus();
					return false;
				}
			}
			if (partiesForm.eventDate.value == '') {
				alert("Please enter the Date of the Event.");
				partiesForm.eventDate.focus();
				return false;
			}
			if (partiesForm.guestNumber.value == '') {
				alert("Please enter the Number of Guests.");
				partiesForm.guestNumber.focus();
				return false;
			}
			if (partiesForm.eventType.value == '') {
				alert("Please enter the Type of Event.");
				partiesForm.eventType.focus();
				return false;
			}
		}
	}
}