jQuery.noConflict();

function log(msg) {
	try {
		console.log(msg);
	}
	
	catch (e) {
		// console is not supported.
	}
}

jQuery(document).ready(function() {

	// Initialize search form submit.
	jQuery('a#submitSearch').click(function() {
		jQuery('#searchForm').submit();
	});
		
	jQuery('#signIn').show();

	// Initialize "sign in" open.
	jQuery('#signInOpen').click(function() {
		if (jQuery('#signIn').css('visibility') == 'visible') {
			jQuery('#signIn').css('visibility','hidden');
			jQuery('#signIn').css('z-index','-1');
			jQuery('#header').append(jQuery('#signIn'));
		} else {
			jQuery('#signIn').css('visibility','visible');
			jQuery('#signIn').css('z-index','100');
			jQuery('body').append(jQuery('#signIn'));
		}
	})
	
	// Initialize "sign in" close button.
	jQuery('#signInClose').click(function() {
		jQuery('#signIn').css('visibility','hidden');
		jQuery('#signIn').css('z-index','-1');
		jQuery('#header').append(jQuery('#signIn'));
	});

	// Initialize zip code open.
	jQuery('#zipCodeOpen').click(function() {
		jQuery('#zipCodeText').val("");
		jQuery('#invalidZipCodeFormat').hide();
		jQuery('#zipCode').show();
	})

	// Initialize zip code close arrow (for initial zip code overlay).
	jQuery('#zipCode .arrowUp').click(function() {
		jQuery('#zipCode').hide();
	});

	// Initialize zip code close arrow (for "invalid zip code" box).
	jQuery('#zipCodeInvalid .arrowUp').click(function() {
		jQuery('#zipCodeInvalid').hide();
	});

	// Initialize zip code close button (for "invalid zip code" box).
	jQuery('#zipCodeInvalidClose').click(function() {
		jQuery('#zipCodeInvalid').hide();
	});

	// Override form submit
	jQuery('#zipCodeForm').submit(function() {
		jQuery('#zipCodeFormSubmit').trigger("click");
		return false;
	});
	
	// form submit
	jQuery('#zipCodeFormSubmit').click(function() {

		// validate zip code
		var zipCodeEntry = jQuery('#zipCodeText').val();
		var zipExp = /^\d{5}([\-]\d{4})?$/;

		// if this is not a proper 5-digit or 9-digit zip code...
		if (zipCodeEntry.search(zipExp) == -1) {
			jQuery('#invalidZipCodeFormat').show();
		} else {

			jQuery.ajax({
				type: 'POST',
				url: contextPath + '/ajax/zipredirect',
		        dataType : 'json',
				data: jQuery('#zipCodeForm').serialize(),

				error: function(XMLHttpRequest, textStatus, errorThrown) {
					// TODO: handle errors.
				},
				success: function(data) {
					
					switch(data.type) {
						case "unknown":
							jQuery('#zipCode').hide();
							jQuery('#zipCodeInvalid').show();
							
							jQuery('#zipCodeClose').click(function() {
								jQuery('#invalidZipCodeMessage').hide();
								jQuery('#zipCodeFormContainer').show();
							});
							break;
							
						case "migratedZipCodes":
//							window.location = window.location + '?zipcode=' + data.zipcode;
							window.location = contextPath + '/allied-internet.html?zipcode=' + data.zipcode;
							break;
							
						default:
							if (data.redirect == undefined) {
								data.redirect = false;
							}
							window.location = contextPath + '/allied-internet/unauthorized-zip-code.html?zipcode=' + data.zipcode + '&redirect=' + data.redirect + '&url=' + encodeURIComponent(data.url);
							break;
					}					
				}			
			});

		}
	});

	// Login Form: if user hits "username" field...
	jQuery('.loginForm .inputUsername').click(function() {
		// clear out the input field
		if (jQuery(this).val() == 'username') {
			jQuery(this).val('');
		}
		// if the password field is blank, switch it out with the text field with the "password" text filled in
		if (jQuery(this).siblings('.inputPassword').val() == '') {
			jQuery(this).siblings('.inputPassword').hide();
			jQuery(this).siblings('.inputPasswordInitial').show();
		}
		
		return false;
	});

	// Login Form: if user hits initial "password" text field...
	jQuery('.loginForm .inputPasswordInitial').focus(function() {
		// clear out the input field (or, more accurately, replace this text field with a password field)
		jQuery(this).hide();
		jQuery(this).siblings('.inputPassword').show();
		jQuery(this).siblings('.inputPassword').focus();
		// if the username field is blank, fill it with the "username" text filled in
		if (jQuery(this).siblings('.inputUsername').val() == '') {
			jQuery(this).siblings('.inputUsername').val('username');		
		}
		
		return false;
	});

	// Login Form: if user hits "password" password field...
	jQuery('.loginForm .inputPassword').click(function() {
		// if the username field is blank, fill it with the "username" text filled in
		if (jQuery('.loginForm .inputUsername').val() == '') {
			jQuery('.loginForm .inputUsername').val('username');		
		}
		return false;
	});

});


function setHiddenField(selId, hidId)
{
    var selObj = document.getElementById(selId);
    var hidObj = document.getElementById(hidId);

    hidObj.value = selObj.value;
    return true;
}

function checkLoginForm(form) {
	// set variable to blank
	var loginErrorMessage = "";
	// if one or the other of the fields is missing.....
	if (form.userName.value.length == 0) {
		loginErrorMessage = "Please enter username";	
	} else if (form.password.value.length == 0) {
		loginErrorMessage = "Please enter password";	
	}
	// if there is a valid login error message...
	if (loginErrorMessage != "") {
		jQuery(form).siblings(".loginMessage").html(loginErrorMessage);
		//document.getElementById("loginMessage").innerHTML = loginErrorMessage;
		return false;
	}
	return true;
}

