/* ============================================================================
* scripts.js, v1.0.003, 2008-05-20
*
* Author:	Scott Park (scott@firefallpro.com), except where noted.
*
* Desc.:	Provides general utility functions
*
* Contents:	trim()
*			ltrim()
*			rtrim()
*			show()
*			hide()
*			showByClass()
*			hideByClass()
*			changeClassByElement
*			changeClassByClass
*
* Requires: n/a
*
* http://www.firefallpro.com
* ========================================================================== */

// trim(), ltrim(), and rtrim(): Credit: http://www.somacon.com/p355.php
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

// Shows an element, element ID or series of comma separated element IDs
function show(id) {
	var element;
	
	// Makes the element visible
	function showElement(e) {
		
		// Block Level Tags
		if (e.tagName.toLowerCase() in {"p":"","div":"","hr":"","h1":"","h2":"","h3":"","h4":"","h5":"","h6":"","address":"","blockquote":"","center":"","del":"","ins":"","noscript":"","pre":""}) {
			e.style.display = "block";
		
		// Table
		} else if (e.tagName.toLowerCase() == "table") {
			try {
				e.style.display = "table-row";
			} catch(error) {
				e.style.display = "block";
			}
		
		// Table Row
		} else if (e.tagName.toLowerCase() == "tr") {
			try {
				e.style.display = "table-row";
			} catch(error) {
				e.style.display = "block";
			}
		
		// Table Cell
		} else if (e.tagName.toLowerCase() == "td") {
			try {
				e.style.display = "table-cell";
			} catch(error) {
				e.style.display = "block";
			}
		
		// Inline Tags
		} else {
			e.style.display = "inline";
		}
	}
	
	// Validate Input
	if (!id) {
		alert("The element has not been specified in show() or does not exist");
		return false;
	}
	
	// String
	if ((typeof id) == "string") {
		id = id.split(",");
		for(i=0; i < id.length; i++) {
			
			// Check for the element
			if (!(element = document.getElementById(id[i].trim()))) {
				alert("The element ID \""+id[i].trim()+"\"specified in show() does not exist");
			} else {
				showElement(element);
			}
		}

	// Object
	} else {
		showElement(id);
	}
	return true;
}

// Hides an element, element ID or series of comma separated element IDs
function hide(id) {
	var element;
	
	// Validate Input
	if (!id) {
		alert("The element has not been specified in hide() or does not exist");
		return false;
	}
	
	// String
	if ((typeof id) == "string") {
		id = id.split(",");
		for(i=0; i < id.length; i++) {
			
			// Check for the element
			if (!(element = document.getElementById(id[i].trim()))) {
				alert("The element ID \""+id[i].trim()+"\" specified in hide() does not exist");

			// Hide
			} else {
				element.style.display = "none";
			}
		}
		
	// Object
	} else {
		id.style.display = "none";
	}
	return true;
}

// Shows a tag by a class
function showByClass(tag,c,within) {
	var elements;
	
	// Validate Input
	if (!tag) {
		alert("A tag has not been specified in showByClass()");
		return false;
	}
	if (!c) {
		alert("A class has not been specified in showByClass()");
		return false;
	}
	if (!within) {
		d = document;
	} else if (!(d = document.getElementById(within.trim()))) {
		alert("The element ID \""+within+"\"specified in hideByClass() does not exist");
		return false;
	}
	
	// Find elements
	elements = d.getElementsByTagName(tag.toLowerCase());

	// Show matching elements
	for(i=0; i < elements.length; i++) {
		if (elements[i].className == c) {
			show(elements[i]);
		}
	}
	return true;
}


// Hides a tag by a class
function hideByClass(tag,c,within) {
	var elements;
	
	// Validate Input
	if (!tag) {
		alert("A tag has not been specified in hideByClass()");
		return false;
	}
	if (!c) {
		alert("A class has not been specified in hideByClass()");
		return false;
	}
	if (!within) {
		d = document;
	} else if (!(d = document.getElementById(within.trim()))) {
		alert("The element ID \""+within+"\"specified in hideByClass() does not exist");
		return false;
	}
	
	// Find elements
	elements = d.getElementsByTagName(tag.toLowerCase());

	// Hide matching elements
	for(i=0; i < elements.length; i++) {
		if (elements[i].className == c) {
			elements[i].style.display = "none";
		}
	}
	return true;
}

// Changes the class of an element, element ID or series of comma separated element IDs
function changeClassByElement(id,c) {
	var element;
	
	// Validate Input
	if (!id) {
		alert("The element has not been specified in changeClassByElement() or does not exist");
		return false;
	}
	if (!c) c = "";
	
	// String
	if ((typeof id) == "string") {
		id = id.split(",");
		for(i=0; i < id.length; i++) {
			
			// Check for the element
			if (!(element = document.getElementById(id[i].trim()))) {
				alert("The element ID \""+id[i].trim()+"\"specified in changeClassByElement() does not exist");
			} else {
				element.className = c;
			}
		}

	// Object
	} else {
		element = id;
		element.className = c;
	}
	return true;
}

// Changes the class of a tag by a class
function changeClassByClass(tag,find,replace,within) {
	var elements;
	var d;
	
	// Validate Input
	if (!tag) {
		alert("A tag has not been specified in changeClassByClass()");
		return false;
	}
	if (!find) {
		alert("A class to find has not been specified in changeClassByClass()");
		return false;
	}
	if (!replace) replace = "";
	
	if (!within) {
		d = document;
	} else if (!(d = document.getElementById(within.trim()))) {
		alert("The element ID \""+within+"\"specified in changeClassByClass() does not exist");
		return false;
	}
	
	// Find Elements
	elements = d.getElementsByTagName(tag.toLowerCase());

	// Hide matching elements
	for(i=0; i < elements.length; i++) {
		if (elements[i].className == find) {
			elements[i].className = replace;
		}
	}
	return true;
}

// Customized Form Checking
function check_form(form) {
	
	// Define email regex
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;

	// Individual Application (Page 1)
	if (form.id == "individual_application_1") {
	
		// E-mail
		if (form.email.value == "" || !form.email.value.match(emailExp)) {
			alert("Please fill in your E-mail Address.");
			form.email.focus();
			return false;
		}
		
		// Username
		if (form.username.value == "") {
			alert("Please fill in your Username.");
			form.username.focus();
			return false;
		}
		
		// Password
		if (form.password.value == "") {
			alert("Please fill in your Password.");
			form.password.focus();
			return false;
		}
		if (form.password.value != form.password_check.value) {
			alert("Your password verification does not match. Please make sure to re-enter your password correctly.");
			form.password_check.focus();
			return false;
		}
		
		// Birth Month
		if (form.birth_month.value == "" || form.birth_month.value == "MM") {
			alert("Your Month of Birth is required, please fill it in.");
			form.birth_month.focus();
			return false;
		}
		
		// Birth Day
		if (form.birth_day.value == "" || form.birth_day.value == "DD") {
			alert("Your Day of Birth is required, please fill it in.");
			form.birth_day.focus();
			return false;
		}
		
		// Birth Year
		if (form.birth_year.value == "" || form.birth_year.value == "YYYY") {
			alert("Your Year of Birth is required, please fill it in.");
			form.birth_year.focus();
			return false;
		}
		
		// Age Verification
		var now = new Date();
		var dob = new Date();
		var dobMin = new Date();
		
		dob.setYear(form.birth_year.value);
		dob.setMonth(form.birth_month.value - 1);
		dob.setDate(form.birth_day.value);
		dobMin.setYear(now.getFullYear() - 21);
		
		if (dob.getTime() > dobMin.getTime()) {
			alert("You must be 21 years or older to register.");
			form.dob_month.focus();
			return false;
		}
		
		
		// Name
		if (form.full_name.value == "") {
			alert("Please fill in your Full Name.");
			form.full_name.focus();
			return false;
		}
		
		// Driver's License
		
			// Number
			if (form.license_number.value == "") {
				alert("Please fill in your Driver's License Number.");
				form.license_number.focus();
				return false;
			}
			
			// Country
			if (form.license_country.value == "") {
				alert("Please fill in your Driver's License Country.");
				form.license_country.focus();
				return false;
			}
			
			// State
			if (form.license_state.value == "") {
				alert("Please fill in your Driver's License State.");
				form.license_state.focus();
				return false;
			}
			
			// Expiration Month
			if (form.lic_expiration_month.value == "" || form.lic_expiration_month.value == "MM") {
				alert("Your License Expiration Month is required, please fill it in.");
				form.lic_expiration_month.focus();
				return false;
			}
			
			// Expiration Day
			if (form.lic_expiration_day.value == "" || form.lic_expiration_day.value == "DD") {
				alert("Your License Expiration Day is required, please fill it in.");
				form.lic_expiration_day.focus();
				return false;
			}
			
			// Expiration Year
			if (form.lic_expiration_year.value == "" || form.lic_expiration_year.value == "YYYY") {
				alert("Your License Expiration Year is required, please fill it in.");
				form.lic_expiration_year.focus();
				return false;
			}
		
		// Mailing Address
			
			// Address Line 1
			if (form.mail_addr_line1.value == "") {
				alert("Line 1 of your Mailing Address is required, please fill it in.");
				form.mail_addr_line1.focus();
				return false;
			}
				
				// City
			if (form.mail_city.value == "") {
				alert("The City of your Mailing Address is required, please fill it in.");
				form.mail_city.focus();
				return false;
			}
		
			// Daytime Phone Number
			if (form.daytime_phone.value == "") {
				alert("Your daytime phone is required, please fill it in.");
				form.daytime_phone.focus();
				return false;
			}
			
			// Mobile Phone Number
			if (form.mobile_phone.value == "") {
				alert("Your mobile phone is required, please fill it in.");
				form.mobile_phone.focus();
				return false;
			}
		
		// Billing
			
			// Address Line 1
			if (form.billing_addr_line1.value == "") {
				alert("Line 1 of your Billing Address is required, please fill it in.");
				form.billing_addr_line1.focus();
				return false;
			}
			
			// City
			if (form.billing_city.value == "") {
				alert("The City of your Billing Address is required, please fill it in.");
				form.billing_city.focus();
				return false;
			}
			
			// Credit Card Number
			if (form.credit_card.value == "") {
				alert("Your Credit Card Number is required, please fill it in.");
				form.credit_card.focus();
				return false;
			}
			
			// Security Code
			if (form.cvv.value == "") {
				alert("Your Security Code is required, please fill it in.");
				form.cvv.focus();
				return false;
			}
			
			// Expiration Month
			if (form.cc_expiration_month.value == "" || form.cc_expiration_month.value == "MM") {
				alert("Your Credit Card Expiration Month is required, please fill it in.");
				form.cc_expiration_month.focus();
				return false;
			}
			
			// Expiration Year
			if (form.cc_expiration_year.value == "" || form.cc_expiration_year.value == "YYYY") {
				alert("Your Credit Card Expiration Year is required, please fill it in.");
				form.cc_expiration_year.focus();
				return false;
			}
		
		// Member Agreement
		if (!form.agree_check.checked) {
			alert("You must agree to the terms and conditions before continuing.");
			form.agree_check.focus();
			return false;
		}

	// Family Application (Page 2)
	} else if (form.id == "family_application_2") {

		// E-mail
		if (form.email.value == "" || !form.email.value.match(emailExp)) {
			alert("Please fill in your E-mail Address.");
			form.email.focus();
			return false;
		}
		
		// Username
		if (form.username.value == "") {
			alert("Please fill in your Username.");
			form.username.focus();
			return false;
		}
		
		// Password
		if (form.password.value == "") {
			alert("Please fill in your Password.");
			form.password.focus();
			return false;
		}
		if (form.password.value != form.password_check.value) {
			alert("Your password verification does not match. Please make sure to re-enter your password correctly.");
			form.password_check.focus();
			return false;
		}
		
		// Birth Month
		if (form.birth_month.value == "" || form.birth_month.value == "MM") {
			alert("Your Month of Birth is required, please fill it in.");
			form.birth_month.focus();
			return false;
		}
		
		// Birth Day
		if (form.birth_day.value == "" || form.birth_day.value == "DD") {
			alert("Your Day of Birth is required, please fill it in.");
			form.birth_day.focus();
			return false;
		}
		
		// Birth Year
		if (form.birth_year.value == "" || form.birth_year.value == "YYYY") {
			alert("Your Year of Birth is required, please fill it in.");
			form.birth_year.focus();
			return false;
		}
		
		// Age Verification
		var now = new Date();
		var dob = new Date();
		var dobMin = new Date();
		
		dob.setYear(form.birth_year.value);
		dob.setMonth(form.birth_month.value - 1);
		dob.setDate(form.birth_day.value);
		dobMin.setYear(now.getFullYear() - 21);
		
		if (dob.getTime() > dobMin.getTime()) {
			alert("You must be 21 years or older to register.");
			form.dob_month.focus();
			return false;
		}
		
		
		// Name
		if (form.full_name.value == "") {
			alert("Please fill in your Full Name.");
			form.full_name.focus();
			return false;
		}
		
		// Driver's License
			
			// Number
			if (form.license_number.value == "") {
				alert("Please fill in your Driver's License Number.");
				form.license_number.focus();
				return false;
			}
			
			// Country
			if (form.license_country.value == "") {
				alert("Please fill in your Driver's License Country.");
				form.license_country.focus();
				return false;
			}
			
			// State
			if (form.license_state.value == "") {
				alert("Please fill in your Driver's License State.");
				form.license_state.focus();
				return false;
			}
			
			// Expiration Month
			if (form.lic_expiration_month.value == "" || form.lic_expiration_month.value == "MM") {
				alert("Your License Expiration Month is required, please fill it in.");
				form.lic_expiration_month.focus();
				return false;
			}
			
			// Expiration Day
			if (form.lic_expiration_day.value == "" || form.lic_expiration_day.value == "DD") {
				alert("Your License Expiration Day is required, please fill it in.");
				form.lic_expiration_day.focus();
				return false;
			}
			
			// Expiration Year
			if (form.lic_expiration_year.value == "" || form.lic_expiration_year.value == "YYYY") {
				alert("Your License Expiration Year is required, please fill it in.");
				form.lic_expiration_year.focus();
				return false;
			}
		
		// Contact Information
			
			// Address Line 1
			if (form.mail_addr_line1.value == "") {
				alert("Line 1 of your Mailing Address is required, please fill it in.");
				form.mail_addr_line1.focus();
				return false;
			}
			
			// City
			if (form.mail_city.value == "") {
				alert("The City of your Mailing Address is required, please fill it in.");
				form.mail_city.focus();
				return false;
			}
			
			// Daytime Phone Number
			if (form.daytime_phone.value == "") {
				alert("Your daytime phone is required, please fill it in.");
				form.daytime_phone.focus();
				return false;
			}
			
			// Mobile Phone Number
			if (form.mobile_phone.value == "") {
				alert("Your mobile phone is required, please fill it in.");
				form.mobile_phone.focus();
				return false;
			}
		
		// Member Agreement
		if (!form.agree_check.checked) {
			alert("You must agree to the terms and conditions before continuing.");
			form.agree_check.focus();
			return false;
		}

	// Send to a Friend
	} else if (form.id == "friend_form") {
		
		// E-mail From
		if (form.email_from.value == "" || !form.email_from.value.match(emailExp)) {
			alert("Please fill in your e-mail address.");
			form.email_from.focus();
			return false;
		}
	
		// E-Mail To
		if (form.email_to.value == "" || !form.email_to.value.match(emailExp)) {
			alert("Please fill in your friend's e-mail address.");
			form.email_to.focus();
			return false;
		}
		
		// Message
		if (form.message.value == "" && form.message_custom.value == "") {
			alert("Please choose a message or type your own.");
			form.message.focus();
			return false;
		}
		
	// Send to a Friend
	} else if (form.id == "friend_form_preview") {
	
		// Message
		if (form.message.value == "") {
			alert("Please fill in a message.");
			form.message.focus();
			return false;
		}
	
	// Customer Inquiry
	} else if (form.id == "form_inquiry") {
	
		// Category
		if (form.category.value == "") {
			alert("Please select a category.");
			form.category.focus();
			return false;
		}
		
		// E-Mail
		if (form.inquiry_email.value == "" || !form.inquiry_email.value.match(emailExp)) {
			alert("Please fill in your e-mail address.");
			form.inquiry_email.focus();
			return false;
		}
		
		// Comments
		if (form.comments.value == "") {
			alert("Please fill in your comments.");
			form.comments.focus();
			return false;
		}
	
	// Customer Feedback
	} else if (form.id == "form_feedback") {
		
		// E-Mail
		if (form.feedback_email.value != "" && !form.feedback_email.value.match(emailExp)) {
			alert("Please fill in a valid e-mail address.");
			form.feedback_email.focus();
			return false;
		}

		// Comments
		if (form.feedback.value == "") {
			alert("Please fill in your comments.");
			form.feedback.focus();
			return false;
		}
	
	} else {
		alert("Unknown form: \""+form.id+"\"");
		return false;
	}
	
	return true;
}

// IE6 Flicker Fix. Credits: Dan Popa (http://www.mister-pixel.com/#Content__state=is_that_simple)
try {
	document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

// Invoices Page
function showInvoices(section,id) {
	var i;
	i = document.getElementById(id);
	
	// Show
	if (i.className == "collapsed" || i.className == "alt collapsed") {
		hideByClass("table","day");
		changeClassByClass("li","expanded","collapsed",section);
		changeClassByClass("li","alt expanded","alt collapsed",section);
		show(id+"_detail");
		if (i.className == "alt collapsed") i.className = "alt expanded";
		if (i.className == "collapsed") i.className = "expanded";
	
	// Hide
	} else {
		hideByClass("table","day");
		changeClassByClass("li","expanded","collapsed",section);
		changeClassByClass("li","alt expanded","alt collapsed",section);
	}
}

function showInvoiceDetail(section,id) {
	var i;
	i = document.getElementById(id);
	
	// Show
	if (i.className == "collapsed" || i.className == "alt collapsed") {
		hideByClass("tr","detail");
		changeClassByClass("tr","expanded","collapsed",section);
		changeClassByClass("tr","alt expanded","alt collapsed",section);
		show(id+"_detail");
		if (i.className == "alt collapsed") i.className = "alt expanded";
		if (i.className == "collapsed") i.className = "expanded";
	
	// Hide
	} else {
		hideByClass("tr","detail");
		changeClassByClass("tr","expanded","collapsed",section);
		changeClassByClass("tr","alt expanded","alt collapsed",section);
	}
}

function showInvoiceDetail2(id) {
	var i;
	i = document.getElementById(id);
	
	// Show
	if (i.className == "collapsed" || i.className == "alt collapsed") {
		hideByClass("tr","detail");
		changeClassByClass("tr","expanded","collapsed");
		changeClassByClass("tr","alt expanded","alt collapsed");
		show(id+"_detail");
		if (i.className == "alt collapsed") i.className = "alt expanded";
		if (i.className == "collapsed") i.className = "expanded";
	
	// Hide
	} else {
		hideByClass("tr","detail");
		changeClassByClass("tr","expanded","collapsed");
		changeClassByClass("tr","alt expanded","alt collapsed");
	}
}