/**
 * register.js - javascript functions needed for the register screen
 *
 * Project: TACC Foundation web application
 * Author: Andrew Le, Alchemist Creative
 * Copyright 2008. See Paragraphs 3 and 6 in contract for terms of use.
 *
 **/

Event.observe(window, 'load', function() {
    $('registration-form').observe('submit', hash_pass); 
    $('schoolName').observe('keyup', autocomplete);
    $('autocomplete').observe('click', getSchool);
});
 
function hash_pass() 
{
    var theForm = $('registration-form');
    var thePass = theForm['password'].value;
    var thePassConfirm = theForm['confirm-password'].value;
    
    if (thePass != "" && thePassConfirm != "") {
        theForm['password'].value = hex_sha1(thePass);
        theForm['confirm-password'].value = hex_sha1(thePassConfirm);
    }
}

function autocomplete(event)
{
    new Ajax.Request("http://tacc-art.org/gallery/json/", {
		method: 'post',
		parameters: { 
		    "schoolName": $('schoolName').value,
		    "stateId": $('stateId').value
		},
		
		onLoading: function () {
		},
		
		onSuccess: function(transport){
		    if (transport.responseText != -1) {
		        $('autocomplete').update(transport.responseText);   
		        $('autocomplete').show();
		    } else if (transport.responseText == -1) {
		        $('autocomplete').hide();
		    }
		}
	});
}

function getSchool(event)
{
    var theSchool = event.target;
    $('schoolName').value = theSchool.innerHTML;
    $('schoolId').value = parseInt(theSchool.identify());
    $('autocomplete').hide();
}