$(function () {
    //Ocultar Capa Div
    $("#HideContent").hide();
    
    //Valores originales para validar si escribio en la  caja de texto
    var field_values = {
        //id        :  value
        'txt_nombre': '',
        'txt_ciudad': '',
        'txt_mail': '',        
        'txt_mensaje': ''
    };
    
    //Limpiar los campos    
    $('#btn_clear').click(function (event) {
        event.preventDefault();
        var fields = $('#mail input[type=text], #mail textarea');
        fields.each(function () {
            $(this).val("");
        });
        $('#mail input[tabindex=1]').focus();
    });
    
    //Mail
    $('#btn_mail').click(function () {
        var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;        
        var fields = $('#mail input[type=text], #mail textarea');
        var error = 0;
        fields.each(function () {
            var value = $(this).val();            
            //ctl00_WUC_Banner1_WUC_Mgx1_WUC_Icons1_txt_mail
            if (((value.length < 1) || (value == field_values[$(this).attr('id')])) || ($(this).attr('id') == 'ctl00_WUC_Banner1_WUC_Mgx1_WUC_Icons1_txt_mail' && !emailPattern.test(value))) {
                $(this).effect("shake", { times: 2 }, 40);                
                error++;
            } else {
            }
        });

        if (!error) {
            
                var dotCounter = 0;
                var intId = setInterval(addDot,1000);
                function addDot(){
                    if (dotCounter < 10) {
                        dotCounter++;
                        $('#dots').append('.');
                    } else {
                        clearInterval(intId);  
                    }
                }

                
            alert("Su mensaje ha sido enviado!..");
            
            var _arg = '';            
            fields.each(function () {               
                _arg = _arg + '|' +  $(this).val();
            });            
            _txt = _arg.substring (1, _arg.length);            
            
            $.ajax({
                type: "POST",
                //Page Name (in which the method should be called) and method name
                url: "Mail.aspx/mail",
                // If you want to pass parameter or data to server side function you can try line
                data: "{'args':'" + _txt + "'}",
                //else If you don't want to pass any value to server side function leave the data to blank line below
                //data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                    //Got the response from server and render to the client
                    //alert(msg.d);
                }
            });
            fields.each(function () {
                $(this).val("");
            });
        }
        else return false;
    });
    
    //Atencion al Cliente
    $('#btn_atecli').click(function () {    
        var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
        var fields = $('#atecli input[type=text], #atecli textarea');
        var error = 0;
        fields.each(function () {
            var value = $(this).val();            
            //ctl00$_contentPlaceHolderBody$WUC_Panel1$WUC_atencion1$txt_email
            if (((value.length < 1) || (value == field_values[$(this).attr('id')])) || ($(this).attr('id') == 'ctl00__contentPlaceHolderBody_WUC_Page1_WUC_atencion1_txt_email' && !emailPattern.test(value))) {
                $(this).effect("shake", { times: 2 }, 40);
                error++;
            } else {                
            } 
        });
        
        if (!error) {
            alert("Su mensaje ha sido enviado!..");
            
            var _arg = '';
            fields.each(function () {
                _arg = _arg + '|' +  $(this).val();
            });
            var _txt;
            _txt = _arg.substring (1, _arg.length);
            
            $.ajax({
                type: "POST",
                //Page Name (in which the method should be called) and method name
                url: "Default.aspx/atecli",
                // If you want to pass parameter or data to server side function you can try line
                data: "{'args':'" + _txt + "'}",
                //else If you don't want to pass any value to server side function leave the data to blank line below
                //data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                    //Got the response from server and render to the client
                    //alert(msg.d);                 
                }
            });
            fields.each(function () {
                $(this).val("");
            });
            $("#_cap").hide();
            $("#Div").hide();
            $("#link").toggle(200);
        }
        else return false;
    });
    
    // Bonos y convenios
    $('#btn_bonos').click(function () {
        var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;        
        var fields = $('#bonos input[type=text], #bonos textarea');
        var error = 0;
        fields.each(function () {
            var value = $(this).val();            
            //ctl00$_contentPlaceHolderBody$WUC_Panel1$WUC_atencion1$txt_email
            if (((value.length < 1) || (value == field_values[$(this).attr('id')])) || ($(this).attr('id') == 'ctl00__contentPlaceHolderBody_WUC_Page1_WUC_bonos1_txt_mail' && !emailPattern.test(value))) {
                $(this).effect("shake", { times: 2 }, 40);
                error++;
            } else {
            }
        });

        if (!error) {
            alert("Su mensaje ha sido enviado!..");
            
            var _arg = '';
            //ctl00$_contentPlaceHolderBody$WUC_Panel1$WUC_bonos1$rbl_option
            var option = $(":radio[name='ctl00$_contentPlaceHolderBody$WUC_Page1$WUC_bonos1$rbl_option']:checked");
            var texto = $('label', option.parent());
            var _rbl = texto.html();
            fields.each(function () {               
                _arg = _arg + '|' +  $(this).val();
            });
            var _txt;
            _txt = _arg.substring (1, _arg.length);
            _txt = _txt + '|' + _rbl;
            
            $.ajax({
                type: "POST",
                //Page Name (in which the method should be called) and method name
                url: "Default.aspx/bonos",
                // If you want to pass parameter or data to server side function you can try line
                data: "{'args':'" + _txt + "'}",
                //else If you don't want to pass any value to server side function leave the data to blank line below
                //data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                    //Got the response from server and render to the client
                    //alert(msg.d);
                }
            });
            fields.each(function () {
                $(this).val("");
            });            
        }
        else return false;
    });
    
    //Contacto
    $('#btn_contact').click(function () {    
        var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
        var fields = $('#contact input[type=text], #contact textarea');
        var error = 0;
        fields.each(function () {
            var value = $(this).val();            
            //ctl00$_contentPlaceHolderBody$WUC_Panel1$WUC_atencion1$txt_email
            if (((value.length < 1) || (value == field_values[$(this).attr('id')])) || ($(this).attr('id') == 'ctl00__contentPlaceHolderBody_WUC_Page1_WUC_contacto1_txt_email' && !emailPattern.test(value))) {
                $(this).effect("shake", { times: 2 }, 40);
                error++;
            } else {                
            } 
        });
        
        if (!error) {
            alert("Su mensaje ha sido enviado!..");
            
            var _arg = '';
            fields.each(function () {
                _arg = _arg + '|' +  $(this).val();
            });
            var _txt;
            _txt = _arg.substring (1, _arg.length);
            
            $.ajax({
                type: "POST",
                //Page Name (in which the method should be called) and method name
                url: "Default.aspx/contacto",
                // If you want to pass parameter or data to server side function you can try line
                data: "{'args':'" + _txt + "'}",
                //else If you don't want to pass any value to server side function leave the data to blank line below
                //data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                    //Got the response from server and render to the client
                    //alert(msg.d);                 
                }
            });
            fields.each(function () {
                $(this).val("");
            });
            $("#_cap").hide();
            $("#Div").hide();
            $("#link").toggle(200);
        }
        else return false;
    });
    
    //Retornar a preguntas
    $('._retornar').click(function (event) {
        event.preventDefault();
        var fields = $('#atecli input[type=text], #atecli textarea');
        fields.each(function () {
            $(this).val("");
        });            
        $("#_cap").hide();
        $("#Div").hide();
        $("#link").toggle(200);
    });
    
    //Validate datatype for controls
    // NOTE: Backspace=8, tab=9, Enter=13, '0'=48, '9'=57, comma=188, decimal point=110
    $("._num").keypress(function(event) {    
		// Allow only backspace and delete		
		if (event.which == 8 || event.which == 32  || event.which == 45) {
			// let it happen, don't do anything
		}
		else {
			// Ensure that it is a number and stop the keypress			
			if ((event.which < 48 || event.which > 58)) {
				event.preventDefault();	
			}	
		}
	});
	
    //Validate datatype for controls
    $("._let").keypress(function(event) {    
		// Allow only backspace and delete
		if (event.keyCode == 46 || event.keyCode == 8) {
			// let it happen, don't do anything
		}
		else {
			// Ensure that it is a number and stop the keypress			
			if ((event.which > 47 && event.which < 58)) {
				event.preventDefault();	
			}	
		}
	});
	
	//Validate datatype for controls
    // NOTE: Backspace=8, tab=9, Enter=13, '0'=48, '9'=57, comma=188, decimal point=110
    $(".txt_fch").keypress(function(event) {    
		// Allow only backspace and delete		
		if (event.which == 8 || event.which == 32  || event.which == 47) {
			// let it happen, don't do anything
		}
		else {
			// Ensure that it is a number and stop the keypress			
			if ((event.which < 48 || event.which > 58)) {
				event.preventDefault();	
			}	
		}
	});
});
