// JavaScript Document

$(document).ready(function(){
	
	//Open External Link in New Window
	$('.external').attr('target', '_blank');

	//make homepage 3 columns equal height
	//find the height for each column
	var leftColHeight = $("#content_left").height();
	var rightColHeight = $("#sidebar_right_wrapper").height();
	
	//alert('the left col is '+leftColHeight+', the center is '+centerColHeight+', and the right is '+rightColHeight);
	
	//usually, the center column is the tallest.
	//set the tallestCol to the center col as a default
	var tallestCol = leftColHeight;
	
	//check to see if right column is taller, if so, reset the tallestCol variable
	if (rightColHeight > tallestCol) {
		tallestCol = rightColHeight;
	}
	
	//alert ('the tallest column is '+tallestCol);
	
	//apply the tallestCol size to all three columns
	$("#content_left").height(tallestCol);
	$("#sidebar_right_wrapper").height(tallestCol);
	
	hideIt();
	
	//validator will allow between 9 and 19 digit numbers with a hard character limit between 10 and 20
	jQuery.validator.addMethod('phoneSimple', function(value) {
var numbers = value.split(/\d/).length - 1;
return (10 <= numbers && numbers <= 20 && value.match(/^(\+){0,1}(\d|\s|\(|\)|\-){10,20}$/)); }, 'Please enter a valid phone number.');

	// custom validatation for US phones
	jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
		phone_number = phone_number.replace(/\s+/g, ""); 
		return this.optional(element) || phone_number.length > 9 &&
			phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
	}, "Please enter a valid phone number");
	
	// custom validation for US and INT'L phone
	jQuery.validator.addMethod('phoneINTL', function(value) {
return (value.match(/^((\+){0,1}\d\s)?\(?([0-9]{3})\)?\s*[\. -]?\s*([0-9]{3})\s*[\. -]?\s*([0-9]{4})\s?((ext|x)\s*\.?:?\s*([0-9]+))?$/)); }, 'Please enter a valid phone number (Intl format accepted + ext: or x:)');

	jQuery.validator.addMethod('northAmericanZips', function(value) {
	return ( value.match(/^\d{5}\-\d{4}$/) || value.match(/^\d{5}$/) || value.match(/^[a-z]{1}\d{1}[a-z]{1}\s\d{1}[a-z]{1}\d{1}$/i) ); }, 'Please enter a valid zip code (ie. 00000, 00000-0000, A0A 0A0)');
	
	// validate signup form on keyup and submit
	var validator = $("#signup").validate({
		rules: {
			firstname: "required",
			lastname: "required",
			email: { 
				required: true, 
				email: true 
			},
			companyname: "required",
			workphone: {
				required: true
				//phoneUS: true
				//phoneSimple:true
			},
			affiliation: "required",
			interest: "required",
			represent: "required",
			member: "required",
			zipcode: {
				//required: false,
				//digits: true,
				minlength: 5
				//northAmericanZips: true
			},
			country: "required"
		},
		messages: { 
			firstname: "Enter your first name",
			lastname: "Enter your last name",
			email: {
				required: "Enter your email address",
				minlength: "Enter a valid email address"
			},
			companyname: "Enter your company name",
			workphone: "Enter a phone number",
			affiliation: "Choose your industry affiliation",
			interest: "Choose your main show interest",
			represent: "Choose who you represent",
			member: "Select your membership",
			zipcode: {
				//digits: "Enter a valid zip code",
				minlength: "Enter a valid zip code"
			},
			country: "Select your country"
		},
		// the errorPlacement has to take the table layout into account 
		errorPlacement: function(error, element) { 
			if ( element.is(":radio") ) 
				error.appendTo( element.parent().parent() ); 
			else if ( element.is(":checkbox") ) 
				error.appendTo ( element.next() ); 
			else 
				error.appendTo( element.parent() ); 
		}
	});
	
});//end doc ready

function hideIt() {
	var signup;
	signup = getUrlVars()["signup"];
	//alert(signup);
	//if ( signup != 1 || signup != 0) ) {
	if (signup==undefined) {
		$('#home #content_wrapper').hide();
		$('#home #footer_wrapper').hide();
		$('#home #header_email_links_container').hide();
		$('#home #nav li').hide();
		//alert('hide content');
	}
}

//reveals the hidden content on the page
function showIt() {
	$('#home #content_wrapper').slideDown(1000);
	$('#home #footer_wrapper').slideDown("slow");
	$('#home #header_email_links_container').fadeIn("fast");
	$('#home #nav li').fadeIn("fast");
	//alert('click');
}

//used to look up URL GET variables
function getUrlVars() {
	var vars = {};
	var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
		vars[key] = value;
	});
	return vars;
}