﻿// -- Sembo.Common -- //
Type.registerNamespace('Sembo');

Sembo.isNumeric = function(value)
{
    /// <summary>
    /// Gets wether the specified value is a valid numeric value.
    /// </summary>
    /// <returns> true if the specified value is a valid numeric value.</returns>
    if (value === null || value === '' || value === undefined || value * 1 != value)
    {
        return false;
    }
    else
    {
        return true;
    }
}

Sembo.firstLetterUppercase = function (inputText)
{
    /// <summary>
    /// Makes first letter uppercase
    /// </summary>
    /// <returns>The string with first letter uppercase</returns>
	return inputText.substring(0,1).toUpperCase() + inputText.substring(1,inputText.length);
}

Sembo.setInnerText = function(element, text)
{
    /// <summary>
    /// Sets the inner text of the specified dom element,
    /// handles browser compatibility.
    /// </summary>
    if (element.innerText !== undefined)
    {
        Sembo.setInnerText = function(x, y) { x.innerText = y };
        Sembo.setInnerText(element, text);
    }    
    else
    {
        Sembo.setInnerText = function(x, y) { x.textContent = y };
        Sembo.setInnerText(element, text);
    }
}

Sembo.getInnerText = function(element)
{
    /// <summary>
    /// Gets the inner text of the specified dom element,
    /// handles browser compatibility.
    /// </summary>
    if (element.innerText !== undefined)
    {
        Sembo.getInnerText = function(x) { return x.innerText; };
        return Sembo.getInnerText;
    }
    else
    {
        Sembo.getInnerText = function(x) { return x.textContent; };
        return Sembo.getInnerText;
    }
}

Sembo.ThrowHelper =
{
    argumentNullException: function(argument, argumentName) {
        /// <summary>
        /// Throws an exception if the specified argument is null.
        /// </summary>

        if (argument === null || argument === undefined) throw Error.argumentNull(argumentName, "The argument was null.");
    }
}

// -- Sembo.Dom -- //
Type.registerNamespace("Sembo.Dom");

Sembo.Dom =
{
    getSelectedValue: function(select) {
        /// <summary>
        /// Gets the value of the selected option in the specified
        /// select-element.
        /// </summary>

        Sembo.ThrowHelper.argumentNullException(select, "select");

        return select.options[select.selectedIndex].value;
    },

    setSelectedValue: function(select, value) {
        /// <summary>
        /// Sets the selected option in the specified select to the first
        /// option that has the value that is specified.
        /// </summary>
        Sembo.ThrowHelper.argumentNullException(select, "select");

        for (var i = 0; i < select.options.length; i++) {
            if (select.options[i].value === value) {
                select.selectedIndex = i;
                return;
            }
        }

        throw {
            type: "ArgumentOutOfRangeException",
            message: "The specified value was not found in the specified select."
        };
    },

    removeChildAt: function(index, parentElement) {
    /// <summary>
    /// Removes the child node with the specified index.
    /// </summary>
    
        var child = parentElement.childNodes[index];
        parentElement.removeChild(child);
    },

    removeAllChildren: function(element) {
    /// <summary>
    /// Removes all the child nodes of the specified element.
    /// </summary>
    
        for (var i = 0; i < element.childNodes.length; i++) {
            Sembo.Dom.removeChildAt(i, element);
        }
    },

    setInnerText: function(element, text) {
    /// <summary>
    /// Sets the inner text of the specified element.
    /// </summary>
    
        Sembo.setInnerText(element, text);
    },

    getInnerText: function(element) {
    /// <summary>
    /// Get the inner text of the specified element.
    /// </summary>
    
        return Sembo.getInnerText(element);
    },

    createNamedElement: function(type, name) {
    /// <summary>
    /// Creates an element with the specified name specified in its
    /// name attribute.
    /// </summary>
        
        var element = null;

        // Try the IE way; this fails on standards-compliant browsers
        try {
            element = document.createElement('<' + type + ' name="' + name + '">');
        } catch (e) { }

        if (!element || element.nodeName != type.toUpperCase()) {
            // Non-IE browser; use canonical method to create named element
            element = document.createElement(type);
            element.name = name;
        }

        return element;
    }
}



// -- Sembo.Common.Validations -- //
Type.registerNamespace("Sembo.Validations");

Sembo.Validations.validateChildAges = function(numberOfChildren, childAgeString)
{
    /// <summary>
    /// Validates that the specified string contains a collection
    /// valid child ages.
    /// </summary>
    /// <param name="childAgeString" optional="false" type="String">The string to validate.</param> 
    /// <param name="numberOfChildren" optional="false" type="integer">The number of child ages the string should contain.</param> 
    /// <returns>True if the string is a valid child age string.</returns>
        
    if (numberOfChildren != 0)
    {
        var ages = childAgeString.split(",");
           
        if (ages.length != numberOfChildren)
        {   
            return false;
        }
        
        for (var age in ages)
        {
            if (!Sembo.isNumeric(ages[age]))
            {
                return false;
            }
        }
    }
    
    return true;
}


Sembo.Validations.validateDate = function(dateString)
{   
    /// <summary>
    /// Validates that the specified string is a date
    /// </summary>
    /// <param name="dateString" optional="false" type="String">The string to validate.</param> 
    /// <returns>True if the string is a valid date.</returns>
        
    if (Date.parseLocale(dateString) == null) 
    {
	    return false;
    }
    else
    {
	    return true;
    }
}

Sembo.Validations.validateDateTextBox = function(source, eventArgs) 
{
    /// <summary>
    /// Can be used in a custom validator for a text box.
    /// </summary>

    eventArgs.IsValid = Sembo.Validations.validateDate(eventArgs.Value);
}
     
     
Sembo.Validations.compareDates = function(firstDate,secondDate,oneday)
{
    /// <summary>
    /// Checks if firstDate is before secondDate.
    /// </summary>
    /// <param name="firstDate" optional="false" type="String">The first string to validate.</param> 
    /// <param name="secondDate" optional="false" type="String">The second string to validate.</param> 
    /// <param name="oneday" optional="false" type="Boolean">If the dates can't be the same.</param> 
    /// <returns>true if the dates represents a valid combination.</returns>

    firstDate = Date.parseLocale(firstDate);
    secondDate = Date.parseLocale(secondDate);
    
    if (firstDate == null || secondDate == null) 
    {
        return false;
    }
    
    if (oneday && firstDate < secondDate)
    {
        return false;
    }
    
    if (firstDate <= secondDate)
    {
        return false;
    }
        
    return true;
}

Sys.Debug.trace("Common.js (Sembo) has been loaded.");
   

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();