var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e)
{
    var keyCode = (isNN) ? e.which : e.keyCode;
    var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
    if(input.value.length >= len && !containsElement(filter,keyCode))
    {
        input.value = input.value.slice(0, len);
        input.form[(getIndex(input)+1) % input.form.length].focus();
	}
	function containsElement(arr, ele) {
	    var found = false, index = 0;
	    while(!found && index < arr.length)
	    if(arr[index] == ele)
	    	found = true;
	    else
		    index++;
	    return found;
	}
	function getIndex(input) {
	    var index = -1, i = 0, found = false;
	    while (i < input.form.length && index == -1)
	        if (input.form[i] == input)index = i;
	        else i++;
	    return index;
	}
	return true;
}
function updateList(listId, json) {
    for (var key in json ) {
        addOption(listId, json[key], key);
    }
}

function updateProgramList(request, json) {
    var listId = $('course');
    listId.length = 0;

    if ( $('campus').value == 0 || $('campus').value == "undefined" ) {
        addOption(listId, '-- Select a Campus first --');
    } else {
        addOption(listId, ' -- Select a Program -- ');
        addOption(listId, 'Not Sure', 'NS');
        updateList(listId, json);
    }
    $('course-loading').style.display = 'none';
}

function updateCity(city)
{
	if(city != null && city != "")
	{
		$('city').value = city;
	}
}

function updateState(state)
{
	updateDropdown('state', state);
}

function updateCountry(country)
{
	updateDropdown('country', country);
}

function updateDropdown(drop, value)
{
	if(value != null && value != "")
	{
		for(var i=0; i<$(drop).length;i++)
		{
			if($(drop).options[i].value == value)
			{
				$(drop).selectedIndex = i;
			}
		}
	}
}

function updateCampusList(request, json) {
    var listId = $('campus');

    listId.length = 0;

    if ( $('zipcode').value == 0 || $('zipcode').value == "undefined" ) {
        addOption(listId, '-- Enter a Zip Code first --');
    } else {
        addOption(listId, '-- Select a Campus --');
        updateList(listId, json['campuses']);
        updateCity(json['city']);
        updateState(json['state']);
        updateCountry(json['country']);
    }
    $('campus-loading').style.display = 'none';
}

/*function showLoading(listId) {
    listId.length = 0;

    addOption(listId, "Loading ...");
    return true;
}*/
function showLoading(el)
{
	el.style.display = 'inline';
}

function addOption(list, text, value) {
    //alert (text + " -- " + value);

    option = document.createElement('option');
    option.text = text;
    if ( value != "undefined" ) {
        option.value = value;
    }

    try {
        list.add(option, null); // Standards compliant
    }
    catch (ex) {
        list.add(option); // IE isn't compliant
    }
}

function gradDate() {
	if($F('form-adkey') == 'HSINTRNT')
		$('graddate-field').show();
	else
		$('graddate-field').hide();
}
Event.observe(window, 'load', function()
{
	new Form.Element.EventObserver("zipcode", function(element, value)
	{
		new Ajax.Request('/contact/campus_search', {asynchronous:true, evalScripts:false, onCreate:function(request, json)
		{
			showLoading($('campus-loading'));
		}, onComplete:function(request, json)
		{
			updateCampusList(request, json)
		}, parameters:'zipcode=' + $F('zipcode'), method: 'GET'})
	});
	new Form.Element.EventObserver("campus", function(element, value)
	{
		new Ajax.Request('/contact/program_search', {asynchronous:true, evalScripts:false, onCreate:function(request, json)
		{
			showLoading($('course-loading'));
		}, onComplete:function(request, json)
		{
			updateProgramList(request, json);
		}, parameters:'campus=' + $F('campus'), method: 'GET'})
	});
	
	gradDate();
	
	Event.observe('form-adkey', 'change', function() {
		gradDate();
	});
	Event.observe('contact-form', 'submit', function(e)
	{
		
		$('form_submit').setStyle({backgroundPosition: 'center bottom'});
		$('form_submit').disabled = true;
	});
});
