//
//  forms.js
//  Version 1.0
//  Written by Stefan Habel
//  Last modified: 18.05.2008
//


//
// Ajax helper class.
//
window.Ajax = {
    xmlHttp: null
}


//
// Creates an XMLHTTP object to be used in requests.
//
// \return A new XMLHTTP object.
//
window.Ajax.createXmlHttpObject = function ()
{
    var xmlHttp = null;
    if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (ex) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (ex) {
                xmlHttp = null;
            }
        }
    }
    return xmlHttp;
}


//
//
//
window.highlightRequiredTextField = function ( field )
{
    // check if a value is given
    if (field.value == "")
        // make background light red
        field.style.backgroundColor = "#ffcccc";
    else
        // make background white
        field.style.backgroundColor = "white";
}


//
//
//
window.requiredFieldMissing = function ( field, messageIfMissing )
{
    if (field.value == "") {
        alert(messageIfMissing);
        field.focus();
        return true;
    }
    return false;
}


//
// Returns if the given e-mail address is valid.
//
// \param address   the e-mail address to check for validity
//
window.checkEmailAddress = function ( address )
{
    // check if the given e-mail address is correct
    var user = "([a-zA-Z0-9][a-zA-Z0-9_.-]*|\"([^\\\\\x80-\xff\015\012\"]|\\\\[^\x80-\xff])+\")";
    var domain = "([a-zA-Z0-9][a-zA-Z0-9._-]*\\.)*[a-zA-Z0-9][a-zA-Z0-9._-]*\\.[a-zA-Z]{2,5}";
    var isValidEmail = new RegExp("^" + user + "\@" + domain + "$");
    var isValid = address != "" && (isValidEmail.test(address));
    return isValid;
}


//
// Callback function for the form XML HTTP object.
//
// \param client    the XML HTTP object
// \param targetId  the ID of the content element to display the response text in
//
window.form_OnReadyStateChange = function ( client, targetId )
{
    if (client.readyState == 4) {
        RemoveAllChildren(targetId);
        CreateHtmlNodes(targetId, client.responseText);
    }
}

