/**
 * Javascript functions used in the consolidated splash page
 * 
 * @package airsite
 * @subpackage splash
 * @author Pat 'omni' Adams <padams@airimba.com>
 */


/**
 * Set up the javascript handlers for the splash page
 */
window.onload = function() {
	// If the splash page has a visible login form, focus on the username field
	if (document.getElementById('username')) {
		document.getElementById('username').focus();
	}

	// Add the form validation handler to the login form, if it exists
	if (document.getElementById('loginform')) {
		document.getElementById('loginform').onsubmit = validate_login;
	}

	// Add the dropdown validation to the signup form, if it exists
	if (document.getElementById('new_account_form')) {
		document.getElementById('new_account_form').onsubmit = validate_splash_dropdown;
	}
	if (document.getElementById('daily_form')) {
		document.getElementById('daily_form').onsubmit = validate_splash_dropdown;
	}

	// If we have the free login page, add a submit handler to it
	if (document.getElementById('free_loginform')) {
		document.getElementById('free_loginform').onsubmit = function() {
			// Log the user in as a free user
			document.free_loginform.username.value = "freeuser@airimba.com";
			document.free_loginform.password.value = "k8aH90qwwh";
		}
	}

	// For the splash pages with an apartment dropdown field (ie, the 
	// watertower properties), add an onchange handler so that if they pick an
	// apartment complex it will assign that value to the daily access location 
	// id
	if (document.getElementById('apartment_dropdown')) {
		document.getElementById('apartment_dropdown').onchange = function() {
			document.getElementById('locationid').value = document.getElementById('apartment_dropdown').value;
		}
	}

	// Set up the onclick handler for the ACM link, if it exists (some splash 
	// pages do not have the ACM link)
	if (document.getElementById('acm_link')) {
		document.getElementById('acm_link').onclick = function() {
			window.open('http://www.airimba.com/acm.html','','toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=400,height=400');
			return false;
		}
	}

	if (document.getElementById('policy_link2')) {
		document.getElementById('policy_link2').onclick = function() {
			window.open('policiesS.html','','toolbar=yes,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=640,height=480');
			return false;
		}
	}

	// Set up the onclick popups for the Q&A, Policies, and Contact Us links
/*
	document.getElementById('faq_link').onclick = function() {
		window.open('faqS.php.htm','','toolbar=yes,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=640,height=480');
		return false;
	}
	document.getElementById('policy_link').onclick = function() {
		window.open('policiesS.html','','toolbar=yes,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=640,height=480');
		return false;
	}
*/
	document.getElementById('contact_link').onclick = function() {
		window.open('contactusS.html','','toolbar=yes,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=640,height=480');
		return false;
	}
	
	if (document.getElementById('close_button')) {
		document.getElementById('close_button').onclick = function() {
			document.getElementById('notice').style.display = 'none';
			return false;
		}
		document.getElementById('ssid_link').onclick = function() {
			document.getElementById('notice').style.display = 'none';
		}
	}
}

/**
 * If there is a dropdown field (for Denton and West Lafayette
 * splash pages for example), make sure that the user picks a 
 * location. If the dropdown is not set, alert the user and 
 * return false.
 * 
 * @return bool Whether to proceed for not
 */
function validate_splash_dropdown() {
	// If the apartment selector dropdown exists, check its value
	if (document.getElementById('apartment_dropdown') && (0 == document.getElementById('apartment_dropdown').value)) {
		alert("Please choose a property before continuing.");
		return false;
	}
  else {
    if (document.getElementById('apartment_dropdown')) {
      document.getElementById('daily_form').req_sublocationid.value = document.getElementById('new_account_form').req_sublocationid.value;
    }
  }
	return true;
}


/**
 * Append '@airimba.com' to a username if it isn't already there. Pops up an 
 * alert if the user did not fill in both their username and password
 */
function validate_login() {
	if (('' != document.getElementById('username').value) && ('' != document.getElementById('password').value)) {
		if (-1 == document.getElementById('username').value.indexOf('@')) {
			document.getElementById('username').value = document.getElementById('username').value + '@airimba.com';
		}
	} else {
		alert('You must enter your username and password');
		return false;
	}
}

