//
// mandatory-fields.js
//
// jQuery script to alert the user about mandatory fields in a form.
//
// Author: Johannes Marbach
//
// Copyright (c) 2008, rapidrabbit GbR.
// All rights reserved.
//


// Globals
var mandatories = new Array('vorname', 'name', 'street', 'zip', 'city',
                            'country', 'phone', 'email', 'Abiturnote',
                            'Datenschutzbestimmungen','Standort');
var titles = new Object();
titles['vorname'] = 'Vorname';
titles['name'] = 'Nachname';
titles['street'] = 'Stra&szlig;e';
titles['zip'] = 'PLZ';
titles['city'] = 'Ort';
titles['country'] = 'Land';
titles['phone'] = 'Rufnummern';
titles['email'] = 'E-Mail';
titles['firstexampoints'] = 'Punktzahl 1. Staatsexamen';
titles['prakt_select'] = 'Praktikumszeitraum';
titles['Standort'] = 'Büro(s)';
titles['Gebiet'] = 'Tätigkeitsgebiet(e)';
var msg_container_id = 'error-message-container';
var unfilled = new Array();
var unfilled_count;


// Setup after the DOM is loaded
jQuery(document).ready(function()
{
    $("#error-message-container").hide();
    $('input[@name=submit]').bind('click', function()
    {
        if (! check_mandatory_fields())
        {
            $("#error-message-container").show();
            print_error_message();
            return false;
        }
    });
});


function check_mandatory_fields()
/// Checks if user has filled out all mandatory fields.
{
    unfilled_count = 0;
    
    for (var i = 0; i < mandatories.length; ++i)
    {
        var object = $('input[@id=' + mandatories[i] + ']');
        if(object[0] == undefined)
        	object = $('select[@id=' + mandatories[i] + ']'); 
       	if(object[0] != undefined){      	
        	if (object.attr('type') == 'checkbox' && ! object.attr('checked') ||
            	! object.val().strip().length)
        	{
            	unfilled[unfilled_count++] = i;
        	}
        }else{
        	object = $('input[@id^=' + mandatories[i] + ']');
       	 	if(object[0] != undefined){
       			var checked = false;
        		object.each(function(index){
        			if($(this).attr('checked')){
        				checked = true;
        			}
        		});
        		if(!checked)
        			unfilled[unfilled_count++] = i;
        	}
        }
    }
    
    return (unfilled_count ? false : true);
}


function print_error_message()
/// Notifies the user about unfilled mandatory fields.
{
    // Build message string
    var msg = '<ul><li class="error-intro">Folgende Felder wurden nicht korrekt ausgefüllt:</li>';
    
    for (var i = 0; i < unfilled_count; ++i)
    {
        msg += '<li>';
        if (titles.hasOwnProperty(mandatories[unfilled[i]]))
            msg +=  titles[mandatories[unfilled[i]]];
        else
            msg += mandatories[unfilled[i]];
        msg += '</li>';
    }
    
    msg += '</ul><br/>';
    
    // Insert message and scroll back to top
    $('#' + msg_container_id).html(msg);
    scroll(0, 0);
}


String.prototype.strip = function()
/// Extends string objects with a strip function.
{
    return this.replace(/^\s+|\s+$/g, '');
}

