$(document).ready(function() {
	// Muestra y oculta los menús
	$('ul li:has(ul)').hover(
		function(e)
		{
			$(this).find('ul').slideDown();
		},
		function(e)
		{
			$(this).find('ul').slideUp();
		}
	);
});

function ResumenMaxLength(sender, args) {
    args.IsValid = args.Value.length <= 980;
}

function AspiracionesMaxLength(sender, args) {
    args.IsValid = args.Value.length <= 140;
}

function setResumenLength(txtId, counterId,msg1,msg2,msg3,msg4,msg5) {
    var lbl = document.getElementById(counterId);
    var txt = document.getElementById(txtId);
    var len = txt.value.length;
    
    var msg = "";
    lbl.setAttribute("style", "float:right;");

    if (len >= 1 && len <= 200) {
        msg = msg1;
    }
    if (len >= 201 && len <= 300) {
        msg = msg2;
    }
    if (len >= 301 && len <= 600) {
        msg = msg3;
    }
    if (len >= 601 && len <= 980) {
        msg = msg4;

    }
    if (len > 980) {
        msg = msg5;
        lbl.setAttribute("style", "color:#FF0000;float:right;");
    }

    lbl.innerHTML = msg;
}

function setAspiracionLength(txtId, counterId, msg1, msg2, msg3, msg4, msg5) {
    var lbl = document.getElementById(counterId);
    var txt = document.getElementById(txtId);
    var len = txt.value.length;

    var msg = "";
    lbl.setAttribute("style", "float:right;");

    if (len >= 1 && len <= 20) {
        msg = msg1;
    }
    if (len >= 21 && len <= 40) {
        msg = msg2;
    }
    if (len >= 41 && len <= 100) {
        msg = msg3;
    }
    if (len >= 101 && len <= 140) {
        msg = msg4;

    }
    if (len > 140) {
        msg = msg5;
        lbl.setAttribute("style", "color:#FF0000;float:right;");
    }

    lbl.innerHTML = msg;
}

function validateLODP(src, arg) {
    //var chk1 = document.getElementById("ctl00_ctnMainContent_ctl00_chkLOPD");
    var chk2 = document.getElementById("ctl00_ctnForms_ctl00_chkLOPD");
    if (chk2 != null) {
        arg.IsValid = chk2.checked;
    }
    else {
        arg.IsValid = false;
    }
}

function validateLODP2(src, arg) {
    var chk = document.getElementById("ctl00_ctnForms_ctl00_chkLOPD");
    if (chk != null) {
        arg.IsValid = chk.checked;
    }
    else {
        arg.IsValid = false;
    }
}

function HideTooltip() {
    var tooltip = $("#tooltip");

    tooltip.fadeOut('fast');
}

function CheckNumericOnly(evt,msg) {
    evt = (evt) ? evt : window.event
    var charCode = (evt.which) ? evt.which : evt.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        status = msg
        return false
    }
    status = ""
    return true

}

function CheckCharactersOnly(evt,msg) {
    evt = (evt) ? evt : window.event
    var charCode = (evt.which) ? evt.which : evt.keyCode

    if (charCode == 8 || charCode == 9) { status = ""; return true; }
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        status = ""
        return true
    }
    status = msg
    return false

}

function CheckNameCharactersOnly(evt, msg) {
    evt = (evt) ? evt : window.event
    var charCode = (evt.which) ? evt.which : evt.keyCode

    if (charCode == 8 || charCode==9) {status = "";return true;}
    if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode!=186 && charCode!=170) {
        status = ""
        return true
    }
    status = msg
    return false

}

function ShowTooltip(controlId, message) {
    var tooltip = $("#tooltip");

    var control = $("#" + controlId);

    var left = control.offset().left + control.outerWidth() - 5 + "px";
    var top = control.offset().top - 102 + 5 + "px";

    tooltip.animate({ "left": left, "top": top });
    tooltip.html("&nbsp;");
    tooltip.fadeIn('normal', function() { tooltip.html(message); });
}

function OnChangeFU(fu, txtId) {
    var txt = document.getElementById(txtId);
    var filename = fu.value.replace(/^.*\\/, '')

    txt.value = filename;
}

function OnChangeSector(sector, txtId) {

    var txt = document.getElementById(txtId);
    if (sector.value == "sel") {
        txt.value = 0;
    }
}

function OnChangeSector2(sector, txtId1, txtId2) {

    var txt1 = document.getElementById(txtId1);
    var txt2 = document.getElementById(txtId2);
    if (sector.value == "sel") {
        txt1.value = 0;
        txt2.value = 0;
    }
}

function Question(message) {
   return confirm(message);
}
/* custom Select */

var selectWidth = "100";

document.write('<style type="text/css">input.styled { display: none; } select.styled { position: relative; width: ' + selectWidth + '%; opacity: 0; filter: alpha(opacity=0); z-index: 5;}</style>');

var Custom = {
    init: function() {
        var inputs = document.getElementsByTagName("select"), span = Array(), textnode, option, active;
        for (a = 0; a < inputs.length; a++) {
            if (inputs[a].className == "styled") {
                option = inputs[a].getElementsByTagName("option");
                if (option.length > 0) {
                    if (option[0].childNodes.length > 0) {
                        active = option[0].childNodes[0].nodeValue;
                        textnode = document.createTextNode(active);
                        for (b = 0; b < option.length; b++) {
                            if (option[b].selected == true) {
                                textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
                            }
                        }
                        span[a] = document.createElement("span");
                        span[a].className = "select";
                        span[a].id = "select" + inputs[a].name;
                        span[a].appendChild(textnode);
                        inputs[a].parentNode.insertBefore(span[a], inputs[a]);
                        //inputs[a].onchange = Custom.choose;
                    }
                }
            }
        }
    }
}

function ApplyMobileMask(obj) {

    var input = document.getElementById(obj.id);

    var str = input.value;

    str = str.replace(/ /g, "");
    
    if (str.length > 0) {
        if (str[0] != '+') {
            str = '+' + str;
        }
    }

    if (str.length > 3) {
        if (str[3] != ' ') {
            str1 = str.substr(0, 3);
            str2 = str.substr(3);

            str = str1 + ' ' + str2;
        }
    }

    if (str.length > 7) {
        if (str[7] != ' ') {
            str1 = str.substr(0, 7);
            str2 = str.substr(7);

            str = str1 + ' ' + str2;
        }
    }

    if (str.length > 11) {
        if (str[11] != ' ') {
            str1 = str.substr(0, 11);
            str2 = str.substr(11);

            str = str1 + ' ' + str2;
        }
    }

    input.value = str;
}

function ApplyPhoneMask(obj) {

    var input = document.getElementById(obj.id);

    var str = input.value;

    str = str.replace(/ /g, "");
    
    if (str.length > 0) {
        if (str[0] != '+') {
            str = '+' + str;
        }
    }

    if (str.length > 3) {
        if (str[3] != ' ') {
            str1 = str.substr(0, 3);
            str2 = str.substr(3);

            str = str1 + ' ' + str2;
        }
    }

    if (str.length > 6) {
        if (str[6] != ' ') {
            str1 = str.substr(0, 6);
            str2 = str.substr(6);

            str = str1 + ' ' + str2;
        }
    }

    if (str.length > 10) {
        if (str[10] != ' ') {
            str1 = str.substr(0, 10);
            str2 = str.substr(10);

            str = str1 + ' ' + str2;
        }
    }

    input.value = str;
}
function setCaretTo(obj, pos) {
    if (obj.createTextRange) {
        var range = obj.createTextRange();
        range.move('character', pos);
        range.select();
    } else if (obj.selectionStart) {
        obj.focus();
        obj.setCaretTo(pos, pos);
    }
}

/*
* MASCARA PARA EL TELEFONO 
* Tab General (OnLoad)
*/
//Mask("mobilephone", "+## ### ### ###");
//Mask("telephone1", "+## ## ### ####");
//Mask("telephone2", "+## ## ### ####");
//Mask("fax", "+## ## ### ####");




// Mask Phones Funtions.
function Mask(fieldId, mask) {
    field = document.getElementById(fieldId);
    field.mask = mask.split("");
    field.regex = new RegExp(escapeRegEx(mask.replace(/#/gi, "").split("")), "gi");
    field.title += " " + mask;
    if (field.attachEvent!==undefined) {
        field.attachEvent("onchange", MaskOnFieldChange);    
    }
    else if (field.addEventListener!==undefined) {
    field.addEventListener("change", MaskOnFieldChange, false);
    }
    
}

function escapeRegEx(chars) {
    var regChars = "+_)(*^$[]-?{}"; //Add all regexp chars id needed   
    var regExprs = "";
    var run2Index = chars.length - 1;

    for (var i = 0; i < run2Index; i++) Concat(chars[i], "|");

    Concat(chars[run2Index]);

    function Concat(c, d) {
        regExprs += ((regChars.indexOf(c) != -1) ? "\\" : "") + c + d;
    }

    return regExprs;
}

function MaskOnFieldChange() {
    var field;
    
    if (window.event===undefined) {
        field = this;
        
    }
    else{
        field = event.srcElement;
    }
    
    if (field.value == null) return;

    //Especifico para el movil y los fijos españoles, la función no sirve para otros tipos
    if ((field.value.charAt(0) == "6" || field.value.charAt(0) == "9") && field.value.length >= 9)
        field.value = "+34 " + field.value;

    var arrDataValue = field.value.replace(field.regex, "").split("");
    var arrResult = [];

    for (var i = 0, j = 0; i < field.mask.length; i++)
        arrResult[i] = (field.mask[i] != "#") ? field.mask[i] : arrDataValue[j++];

    field.value = arrResult.join("");
}

function isdefined(variable) {
    return (typeof (window[variable]) == "undefined") ? false : true;
}

function test() {
    
}


window.onload = Custom.init;


