var alphanumchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzÁáÉéÍíÓóÚúñÑÀÂÈÊàâèêôû0123456789";
var alphachars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzÁáÉéÍíÓóÚúñÑÀÂÈÊàâèêôû";

function isAlphanumeric(s, valid_chars) {
	var allValid = true;
	var ch = '';
	for (i = 0;  i < s.length;  i++) {
		ch = s.charAt(i);
		for (j = 0;  j < valid_chars.length;  j++)
			if (ch == valid_chars.charAt(j))
				break;
			if (j == valid_chars.length) {
				allValid = false;
			break;
		}
	}
	if (s.match("--")){
		allValid = false;
	}
	return allValid;
}


function LTrim(str) {
    var s = new String(str);
    var whitespace = new String(' \t\n\r');

    // Strip all leading white-space characters
    if (whitespace.indexOf(s.charAt(0)) != -1) {

        var j=0, i = s.length;
    
        // Iterate from the left until we have no more whitespace...
        while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
            j++;

        // Get the substring from the first non-whitespace 
        // character to the end of the string...
        s = s.substring(j, i);
    }
    return s;
}

function RTrim(str) {
    var s = new String(str);
    var whitespace = new String(' \t\n\r');

    // Strip all trailing white-space characters
    if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {

        var i = s.length - 1;

        // Iterate from the right until we have no more whitespace...
        while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
            i--;

        // Get the substring from the beginning of the string to
        // where the last non-whitespace character
        s = s.substring(0, i+1);
    }
    return s;
}

function Trim(str) {
    // Strip away all leading and trailing white-space characters
    return RTrim(LTrim(str));
}

//used to control max input of characters for password
function textCounter(field, maxlimit) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, field.value.length - 1)	
	//remove the last character entered because it's the maxlimit + 1 character
	alert(form.jsalert1.value + maxlimit + form.jsalert2.value);
	return true;
	}
}



function ValidateForm(form) {
	var str1 = str2 = '';


	// ALIAS
	if(form.alias != null) {
		// required; must be 3 to 15 characters in length...
		form.alias.value = Trim(form.alias.value);
		str = form.alias.value;
		str1 = str.toLowerCase();
		if(str == '' || str.length < 3) {
			alert('No has escrito el usuario o el usuario es menor a 3 caracteres');
			form.alias.focus();
			return false;
		}
		// must contain alphanumeric characters only...
		if(!isAlphanumeric(str, alphanumchars + ' ~`!@$*^(),.?') || (str1.charAt(0)=='c' && str1.charAt(1)=='m')) {
			alert('El usuario solo puede contener caracteres alfanuméricos\n' + alphanumchars );
			form.alias.focus();
			form.alias.select();
			return false;
		}
	}
	// PASSWORD
	if(form.ppp != null) {
		form.ppp.value = Trim(form.ppp.value);
		str = form.ppp.value;
		if (form.name != 'formPrfUpd') {
			if(str == '') {
				alert('No has escrito la contraseña');
				form.ppp.focus();
				return false;
			}
		}
		// must contain alphanumeric characters only...
		if(!isAlphanumeric(str, alphanumchars + '~`!@#$*^(),.?')) {
			alert('La contraseña solo puede contener caracteres alfanuméricos\n' + alphanumchars);
			form.ppp.value="";
			form.ppp2.value="";
			form.ppp.select();
			form.ppp.focus();
			return false;
		} 
	}
	// PASSWORD2
	if(form.ppp2 != null){
		form.ppp.value = Trim(form.ppp.value);
		form.ppp2.value = Trim(form.ppp2.value);
		str1 = form.ppp.value;
		str2 = form.ppp2.value;
		if (!(str1==str2)){
			alert('Las contraseñas no concuerdan, por favor intenta de nuevo');
			return false;
			form.ppp.value="";
			form.ppp2.value="";
			form.ppp.focus();
			return false;
		}
	}

	
	// All is valid, submit form
	return true;
}
