﻿/* --- MyTextBox --- */

function MyTextBox_Validate(source,arguments) { 
    arguments.IsValid = true; 
    var message = ''; 
    var validator = source; /*document.getElementById(source.id);*/ 
    var controlToValidate = document.getElementById(validator.getAttribute('MyTextBox_TextBox')); 
    var validationMessage = validator.getAttribute('MyTextBox_ValidationMessage'); 
    var watermarkText = validator.getAttribute('MyTextBox_WatermarkText'); 
    var lastFailedDomain = validator.getAttribute('MyTextBox_LastFailedDomain'); 
    var standardMessage = null; 
    var value = controlToValidate.value; 
    if (value == watermarkText) 
        value = ''; 
    var mandatory = (validator.getAttribute('MyTextBox_Mandatory') == 'true'); 
    if (arguments.IsValid && mandatory) { 
        arguments.IsValid = !(new RegExp('^(\ )*$')).test(value); 
        if (!arguments.IsValid) 
            standardMessage = MYCONTROLS_RESOURCES['errRequired']; 
    } 
    var regex = validator.getAttribute('MyTextBox_RegEx'); 
    if (arguments.IsValid && (value != '') && regex) { 
        arguments.IsValid = (new RegExp(regex)).test(value); 
        if (!arguments.IsValid) 
            standardMessage = MYCONTROLS_RESOURCES['valFieldUpdate']; 
        else { 
            if ((lastFailedDomain != '') && (lastFailedDomain != null) && (lastFailedDomain.toLowerCase() == value.split('@')[1].toLowerCase())) { 
                standardMessage = MYCONTROLS_RESOURCES['valFieldUpdate']; 
                arguments.IsValid = false; 
            } 
        } 
    } 
    var mustBeEqualTo = validator.getAttribute('MyTextBox_MustBeEqualTo'); 
    if (arguments.IsValid && mustBeEqualTo) { 
        var controlToValidate2 = document.getElementById(mustBeEqualTo); 
        arguments.IsValid = (!controlToValidate2) || (controlToValidate2.value == value); 
        if (!arguments.IsValid) 
            validationMessage = 'No coincide'; 
    } 
    if (!arguments.IsValid) { 
        message = htmlEncode((validationMessage !== null) && (validationMessage.length > 0) ? validationMessage : standardMessage); 
    } 
    if (arguments.IsValid) { 
        var onClientValidationOk = validator.getAttribute('MyTextBox_OnClientValidationOk'); 
        if (onClientValidationOk) 
            eval(onClientValidationOk + '(controlToValidate)'); 
    } 
    else if (controlToValidate.className.indexOf('boxtexterror') < 0) 
        controlToValidate.className += ' boxtexterror'; 
}

function MyTextBox_getText(id) { 
    var info = document.getElementById(id).info; 
    var validator = document.getElementById(info); 
    var watermarkText = validator.getAttribute('MyTextBox_WatermarkText'); 
    var controlToValidate = document.getElementById(validator.getAttribute('MyTextBox_TextBox')); 
    var value = controlToValidate.value; 
    if (value == watermarkText) 
        value = ''; 
    return value; 
} 

function MyTextBox_setText(id,text) { 
    var info = document.getElementById(id).info; 
    var validator = document.getElementById(info); 
    var controlToValidate = document.getElementById(validator.getAttribute('MyTextBox_TextBox')); 
    controlToValidate.value = text; 
} 

function MyTextBox_setStyle(myTextBox,name,value) { 
    myTextBox.style[name] = value; 
}

function MyTextBox_Capitalize(source,type) { 
    if (type == 1) 
        source.value = source.value.toUpperCase(); 
    else if (type == 2) 
        source.value = source.value.toLowerCase(); 
    else if (type == 3) { 
        var separators = [' ', '.', ',', ';', ':']; 
        for (var i=0; i<separators.length; i++) { 
            var words = source.value.split(separators[i]); 
            for (var j=0; j<words.length; j++) 
                if (words[j].length > 0) { words[j] = words[j].charAt(0).toUpperCase() + words[j].substr(1); } 
            source.value = words.join(separators[i]); 
        } 
    } 
}


/* --- MyCheckBox --- */

function MyCheckBox_Validate(source,arguments) { 
    arguments.IsValid = true; 
    var message = ''; 
    var validator = source; /*document.getElementById(source.id);*/ 
    var controlToValidate = document.getElementById(validator.getAttribute('MyCheckBox_CheckBox')); 
    var validationMessage = validator.getAttribute('MyTextBox_ValidationMessage'); 
    var standardMessage = null; 
    var mandatory = (validator.getAttribute('MyCheckBox_Mandatory') == 'true'); 
    if (arguments.IsValid && mandatory) { 
        arguments.IsValid = (controlToValidate.checked); 
        if (!arguments.IsValid) 
            standardMessage = MYCONTROLS_RESOURCES['errRequired']; 
    } 
    if (!arguments.IsValid) { 
        message = htmlEncode((validationMessage !== null) && (validationMessage.length > 0) ? validationMessage : standardMessage); 
    } 
    if (arguments.IsValid) { 
        var onClientValidationOk = validator.getAttribute('MyCheckBox_OnClientValidationOk'); 
        if (onClientValidationOk) 
            eval(onClientValidationOk + '(controlToValidate)'); 
    } 
}


/* --- MyDropDownList --- */

function MyDropDownList_Validate(source,arguments) { 
    arguments.IsValid = true; 
    var message = ''; 
    var validator = source; /*document.getElementById(source.id); */ 
    var controlToValidate = document.getElementById(validator.getAttribute('MyDropDownList_DropDownList')); 
    var validationMessage = validator.getAttribute('MyDropDownList_ValidationMessage'); 
    var standardMessage = null; 
    var mandatory = (validator.getAttribute('MyDropDownList_Mandatory') == 'true'); 
    if (arguments.IsValid && mandatory) { 
        arguments.IsValid = !(new RegExp('^(\ )*$')).test(controlToValidate.value); 
        if (!arguments.IsValid) 
            standardMessage = MYCONTROLS_RESOURCES['errRequired']; 
    } 
    if (!arguments.IsValid) { 
        message = htmlEncode((validationMessage !== null) && (validationMessage.length > 0) ? validationMessage : standardMessage); 
    } 
}


/* --- MyGenre--- */

function MyGenre_setGenre(id,male) { 
    var info = $get($get(id).getAttribute('AdditionalInfo')); 
    var maleControl = document.getElementById(info.getAttribute('MyGenre_MaleRadioButton')); 
    var femaleControl = document.getElementById(info.getAttribute('MyGenre_FemaleRadioButton')); 
    maleControl.checked = (male === true); 
    femaleControl.checked = (male === false); 
} 

function MyGenre_Validate(source,arguments) { 
    arguments.IsValid = true; 
    var message = ''; 
    var validator = source; /*document.getElementById(source.id);*/ 
    var controlToValidateMale = document.getElementById(validator.getAttribute('MyGenre_MaleRadioButton')); 
    var controlToValidateFemale = document.getElementById(validator.getAttribute('MyGenre_FemaleRadioButton')); 
    var validationMessage = validator.getAttribute('MyGenre_ValidationMessage'); 
    var standardMessage = null; 
    var mandatory = (validator.getAttribute('MyGenre_Mandatory') == 'true'); 
    arguments.IsValid = (controlToValidateMale.checked || controlToValidateFemale.checked); 
    if (!arguments.IsValid) { 
        standardMessage = MYCONTROLS_RESOURCES['errRequired']; 
        message = htmlEncode((validationMessage !== null) && (validationMessage.length > 0) ? validationMessage : standardMessage); 
        controlToValidateMale.parentNode.className = 'male genderRequired'; 
        controlToValidateFemale.parentNode.className = 'female genderRequired'; 
        setTimeout('MyGenre_setNormalStyle("'+controlToValidateMale.id+'","'+controlToValidateFemale.id+'");',500); 
    } 
    if (arguments.IsValid) { 
        var onClientValidationOk = validator.getAttribute('MyGenre_OnClientValidationOk'); 
        if (onClientValidationOk) 
            eval(onClientValidationOk + '(controlToValidate)'); 
    } 
}

function MyGenre_setNormalStyle(male,female) { 
    var controlToValidateMale = document.getElementById(male); 
    var controlToValidateFemale = document.getElementById(female); 
    controlToValidateMale.parentNode.className = 'male'; 
    controlToValidateFemale.parentNode.className = 'female'; 
}
