﻿function customRequiredFieldValidator(source, args){ 
    var bIsValid = true;
    var bNameIsValid = false;
    var bEmailIsValid = false;
    
    
    //var elements=document..getElementsByTagName('input');
    //alert("algo");
    var bSpanNombre = true;
    var bSpanEmail = true;
    var bSpanEmailFormat = true;
    var ii = 0;
    ii = 10 /2;
    //alert('dnn_ctrlRecomendar_txtEmailFriend' + parseInt(ii-4));
    //alert(eval('dnn_ctrlRecomendar_txtEmailFriend' + parseInt(ii-4)));
    //alert(document.getElementById('dnn_ctrlRecomendar_txtEmailFriend' + parseInt(ii-4)).value);
    
    var valNombre = false;
    var valEMail = true;
    
	for(var i=0;i<ii;i++){
	    //alert(elements[i].id);
	    //alert(parseInt(ii));
	    //nombre = 'dnn_ctrlRecomendar_obj0_' + parseInt(i);
        //alert("d " + document.getElementById('dnn_ctrlRecomendar_obj0_0').value);
	    //alert("dd " + document.getElementById(nombre).value);
	    //alert("ddd " + document.getElementById(elements[i].id).value);
	    //alert(eval('dnn_ctrlRecomendar_txtEmailFriend' + i));
	    nombre = document.getElementById('dnn_ctrlRecomendar_txtNameFriend' + parseInt(i+1)).value;
	    email = document.getElementById('dnn_ctrlRecomendar_txtEmailFriend' + parseInt(i+1)).value;
	    //alert("i " + parseInt(i));
	    //alert("ii " + parseInt(i-ii));
	    //email = 'dnn_ctrlRecomendar_obj1_' + parseInt(i+ii);
	    
	    //alert(nombre);
	    //alert(email);
	    
	    if (valEMail ==true && valNombre == false){
	        //lth = trim(email).length;
	        if(trim(email).length > 0){
	            //alert('entra');
	            bEmailIsValid = true;
                if (!(/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(email))){
	            //alert("true");
	            /*bEmailIsValid = true;
	            bSpanEmailFormat = true;
	            }else{*/
	            //alert("false");
	            bEmailIsValid = false;
	            bSpanEmailFormat = false;
	            break;
	            }
            }
	    }
    }
    
       
    args.IsValid = bEmailIsValid;
    //bSpanNombre = args.IsValid;
    bSpanEmail = args.IsValid;

    //alert(bSpanNombre);
    //alert(bSpanFecha);
    
    if (valNombre){
        if (bSpanNombre){
            document.getElementById('sName').style.display = 'none';
        }else{
            document.getElementById('sName').style.display = 'inline';
        }
    }
    
    if (valEMail){
        if (bSpanEmail){
            document.getElementById('sEmail').style.display = 'none';
        }else{
            document.getElementById('sEmail').style.display = 'inline';
        }
        if (bSpanEmailFormat){
            //alert("1");
            //document.getElementById('sEmailFormat').style.display = 'none';
            //document.getElementById('sEmail').style.display = 'inline';
        }else{
            //alert("2");
            //document.getElementById('sEmailFormat').style.display = 'inline';
            //document.getElementById('sEmail').style.display = 'none';
        }
    }
}

function TosCustomRequiredFieldValidator(source, args){ 
    var bIsValid = true;
    var bNameIsValid = false;
    var bEmailIsValid = false;
    
    
    var elements=document.getElementById('dnn_ctrlRecomendar_tblTos').getElementsByTagName('input');

    var bSpanNombre = true;
    var bSpanEmail = true;
    var bSpanEmailFormat = true;
    
    ii = elements.length /2;
    
    var valNombre = false;
    var valEMail = true;
    
	for(var i=0;i<ii;i++){
	    //alert(elements[i].id);
	    //alert(parseInt(ii));
	    //nombre = 'dnn_ctrlRecomendar_obj0_' + parseInt(i);
        //alert("d " + document.getElementById('dnn_ctrlRecomendar_obj0_0').value);
	    //alert("dd " + document.getElementById(nombre).value);
	    //alert("ddd " + document.getElementById(elements[i].id).value);
	    nombre = document.getElementById(elements[i].id).value;
	    email = document.getElementById(elements[i+ii].id).value;
	    //alert("i " + parseInt(i));
	    //alert("ii " + parseInt(i-ii));
	    //email = 'dnn_ctrlRecomendar_obj1_' + parseInt(i+ii);
	    
	    //alert(nombre);
	    //alert(email);
	    
	    if (valEMail ==true && valNombre == false){
	        //lth = trim(email).length;
	        if(trim(email).length > 0){
	            //alert('entra');
	            bEmailIsValid = true;
                if (!(/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(email))){
	            //alert("true");
	            /*bEmailIsValid = true;
	            bSpanEmailFormat = true;
	            }else{*/
	            //alert("false");
	            bEmailIsValid = false;
	            bSpanEmailFormat = false;
	            break;
	            }
            }
	    }
    }
    
       
    args.IsValid = bEmailIsValid;
    //bSpanNombre = args.IsValid;
    bSpanEmail = args.IsValid;

    //alert(bSpanNombre);
    //alert(bSpanFecha);
    
    if (valNombre){
        if (bSpanNombre){
            document.getElementById('sName').style.display = 'none';
        }else{
            document.getElementById('sName').style.display = 'inline';
        }
    }
    
    if (valEMail){
        if (bSpanEmail){
            document.getElementById('sEmail').style.display = 'none';
        }else{
            document.getElementById('sEmail').style.display = 'inline';
        }
        if (bSpanEmailFormat){
            //alert("1");
            document.getElementById('sEmailFormat').style.display = 'none';
            //document.getElementById('sEmail').style.display = 'inline';
        }else{
            //alert("2");
            document.getElementById('sEmailFormat').style.display = 'inline';
            document.getElementById('sEmail').style.display = 'none';
        }
    }
}

function validateAdmForm(){ 
    //alert("llega");
    Campania = document.getElementById('dnn_ctrlRecomendar_txtCampania').value;
    Link = document.getElementById('dnn_ctrlRecomendar_txtLink').value;
    //alert(Campania.length);
    //alert(Link.length);
    var bValid = true;
    
    if(trim(Campania)==""){ 
        bValid = false;
        document.getElementById('dnn_ctrlRecomendar_RequiredFieldValidator3').style.visibility = "visible";
    }
    if(trim(Link) ==""){ 
        bValid = false;
        document.getElementById('dnn_ctrlRecomendar_RequiredFieldValidator5').style.visibility = "visible";
    }
    return bValid;    
}

function ltrim(str) { 
	for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
	return str.substring(k, str.length);
}
function rtrim(str) {
	for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
	return str.substring(0,j+1);
}
function trim(str) {
	return ltrim(rtrim(str));
}
function isWhitespace(charToCheck) {
	var whitespaceChars = " \t\n\r\f";
	return (whitespaceChars.indexOf(charToCheck) != -1);
}

