/* 
	Oxxigeno Forms Library
	- require commons.js
	
	This library has been created for generic forms validation.
*/

var ALPHABETIC_CHARS = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
var NUMBERS = ['1','2','3','4','5','6','7','8','9','0'];

var NIF_CHARACTERS = ['T','R','W','A','G','M','Y','F','P','D','X','B','N','J','Z','S','Q','V','H','L','C','K','E','T'];
var PHONE_SPECIAL_CHARACTERS = ['+','-','(',')','.',' '];
var MAIL_EXTENSIONS = ['es','com','info'];  // Not used


// Verify if the field identified by id is empty.
function isEmptyField(id) {
	var field = get(id);
	if (field == null) return true;
	return (isEmpty(field.value));
}


// Verify if the field identified by id is an integer.
function isIntegerField(id) {
	var field = get(id);
	if (field == null) return false;
	if (isInteger(field.value) == null)
		return false;
	else
		return isInteger(field.value);
}


// Verify if the field identified by id is a string. 
// NOTE: Is not possible to know in a formulary, always is String.
function isStringField(id) {
	var field = get(id);
	if (field == null) return false;
	return (typeof field.value == 'string');
}


// Maxlength imposed to the text of a textarea element.
function imposeMaxLength(id, maxLength){
	var field = get(id);
	if (field == null) return false;
	if(field.value.length > maxLength)
		field.value = field.value.substring(0, maxLength - 1);
 }


// Verify if there is some field selected in a select.
function isSomethingSelected(id) {
	var field = get(id);
	if (field == null) return false;
	return (field.selectedIndex == 0)?false:true;
}

// Verify if the field identified by id is an email valid.
function isValidEmail(id){
	var field = get(id);
	if (field == null) return false;
	return (!(/\w{1,}[@]([\w\-]{1,}[.]){1,3}[\w\-]{2,4}$/.test(field.value)))?false:true
}


// Verify if the field identified by id is an phone valid (max 15 characters).
function isValidPhone(id){
	//var field = get(id);
	//if (field == null) return false;
	//var phone = field.value;
	
	//for (i=0; i<PHONE_SPECIAL_CHARACTERS.length; i++) {
	//	while (phone.indexOf(PHONE_SPECIAL_CHARACTERS[i]) != -1) {
	//		phone = phone.replace(PHONE_SPECIAL_CHARACTERS[i], '');
	//	}
	//}
	//return (!(/^\d{9,}$/.test(phone)))?false:true;
	return true;
}


// Verify if the field identified by id is an nif valid (only valid for Spain).
function isValidNIF(id){
	var field = get(id);
	if (field == null) return true;
	var nif = field.value.toUpperCase();
	// Verify nif format.
	if( !(/^\d{8}[A-Z]$/.test(nif)) ) {
		return false;
	}
	// Verify if is possible the combination.
	if(field.value.charAt(8) != NIF_CHARACTERS[(nif.substring(0, 8))%23]) {
  		return false;
	}	
}


// Verify if the field identified by id is a valid date in (dd/mm/yyyy) or (mm/dd/yyyy) determined by language
function isValidDate(id, language) {
	var field = get(id);
	if (field == null) return true;
	var date = field.value;
	if (language == null) language = 'en';
	
	if (language=='es')
		return (!(/^(3[01]|0?[1-9]|[12][0-9])\/(0?[1-9]|1[012])\/20[0-9]{2}/.test(date)))?false:true 
	else 
		return (!(/^(0?[1-9]|1[012])\/(3[01]|0?[1-9]|[12][0-9])\/20[0-9]{2}/.test(date)))?false:true;
}


// Verify if the field identified by id is an cif valid.
function isValidCif(cif){
/* CIF == T1 T2 T3 (sin espacios) = 9 caracteres.
 * T1 :: Primer caracter. Es una letra
 * T2 :: Siguientes 7 caracteres (posiciones 2 a 8). Numeros
 * T3 :: Ultimo caracter (pos. 9): Letra o numero
 */
 	
 	var field = get(id);
	if (field == null) return false;
	var cif = field.value;
 	
	if(cif.length != 9) return false;
	cif = cif.toUpperCase();
//T1
	cad = "ABCDEFGHKLMNPQSX";
	if(cad.indexOf(cif.charAt(0)) == -1) return false;
//T3
	cad = "KPQS";
	cad2 = "ABCDEFGHIJ";
	//si el primer caracter es una letra (IN cad), el ultimo caracter debe ser una letra (IN cad2)
	if((cad.indexOf(cif.charAt(0)) != -1) 
		&& (cad2.indexOf(cif.charAt(8)) == -1)) return false;
	
	cad = "ABEH";
	cad2 = "0123456789";
	//si el primer caracter es una letra (IN cad), el ultimo caracter sera un numero (IN cad2)
	if((cad.indexOf(cif.charAt(0)) != -1) 
		&& (cad2.indexOf(cif.charAt(8)) == -1)) return false
	
	//para cualquier otra clave de entidad el ultimo digito podra se numero o letra
	cad = "QWERTYUIOPASDFGHJKLZXCVBNM0123456789";
	if(cad.indexOf(cif.charAt(9)) == -1) return false;
	
//T2: calcular lar el digito de control (pos. 9) a partir de T2 y comprobar que coincide
	t2 = cif.substring(1,8);
	sumA = 0;
	for(i = 1; i < 7; i+=2) sumA += parseInt(t2.charAt(i));
	sumB = 0;
	aux = 0;
	
	for(i = 0; i < 7; i+=2){
		aux = parseInt(t2.charAt(i)) * 2;
		sumB += parseInt(aux.toString().charAt(0));
		if(aux >= 10) sumB += parseInt(aux.toString().charAt(1));
	}
	C = (10 -((sumA + sumB) % 10)) % 10; //se vuelve a hallar el modulo por si sale 10.
	control = "";
	controlAux = "";
	cad = "KPQS";
	cad2 = "ABCDEFGHIJ";
	cad3 = "ABEH";
	if(cad.indexOf(cif.charAt(0)) != -1) control = cad2.charAt(C-1);
	else if(cad3.indexOf(cif.charAt(0)) != -1) control = C.toString();
	else if("X" == cif.charAt(0)) control = letraNIF(parseInt(t2));//String.fromCharCode(64 + C);
	else{
		control = cad2.charAt(C-1);
		controlAux = C.toString();
	}
	//comprobar que el digito de control calculado se corresponde con el digito de control del CIF dado
	if((control != cif.charAt(8)) && (controlAux != cif.charAt(8))) return false;

	return true;
}


// Clean a field of formulary.
function cleanField(id){
	var field = get(id);
	if (field == null) return;
	field.value = '';
}


// Focus a field of formulary.
function focusField(id){
	var field = get(id);
	if (field == null) return;
	field.focus();
}

// Focus an object that is not a formulary field.
function specialFocus(id){
	var obj = get(id);
	if(obj == null) return;
	self.location = "#" + obj.id;
}

// Clean a field of formulary and then focus it.
function cleanAndFocusField(id){
	cleanField(id);
	focusField(id);
}


// For print info in a specified element.
function printInfo(elementId, text){
	var element = get(elementId);
	if (element==null) return;
	element.innerHTML = text;
}

/* 
 * Clear a Inputs Radio Group
 * name: Group name.
 * indexDefault: Index of elements active by default.
 *               Not specify not to activate any item. 
 */
function clearRadioGroup(name, indexDefault){
	radioGroup = getElementsGroup(name);
	if(indexDefault == null){
		for(i = 0; i < radioGroup.length; i++)
			radioGroup[i].checked = false;
	}else{
		radioGroup[indexDefault].checked = true;
	}
}