﻿//
// +--------------------------------------------------------------------+
// | codelibrary    											      	|
// +--------------------------------------------------------------------+
// | Copyright (c) 2008 Softime										    |
// +--------------------------------------------------------------------+
// | This file is part of a Softime Web System. Any kind of copy or use |
// | without the explicit permission from Softime is prohibited.        |
// | For more information about the licence of this software,          	|
// | contact us at info@softime.es                                      |
// +--------------------------------------------------------------------+
// | Author: Wan Xianjia <wanxj@softime.es>                             |
// +--------------------------------------------------------------------+
//
//example:
//new ValidateForm('loginForm',[checkEmail,checkEmpty]);

var ValidateForm = Class.create({
	initialize: function(formId, funcArr, options) {
		this.idForm		= formId;
		this.illegalArr = [];
		this.funcArr 	= funcArr;
		this.options 	= options? options : [];
		
		$(formId).onsubmit = this.validate.bindAsEventListener(this);
	},
	
	validate: function() {
		if(!this.funcArr || this.funcArr.length < 1) return;
		for(var i = 0; i < this.funcArr.length; i++){
			if(typeof this.funcArr[i] != 'function') return false;
//			alert(typeof this.funcArr[i]);
			var option = (this.options)[i] ? (this.options) [i] : {};
			var result = this.funcArr[i](this.idForm, option);
			if(result){
				this.illegalArr.push(result);
			}
		}
		
		return this.throwErr();
	},
	
	throwErr: function() {
		if(this.illegalArr.length > 0){
			alert(this.illegalArr[0].errMsg);
			this.illegalArr[0].focus();
			this.illegalArr = [];
			return false;
		}
		showSearchWaiting();
		return true;
		}
});


//function ValidateForm(formId,funcArr,options){ 
//	this.idForm = formId;
//	this.illegalArr = [];
//	this.funcArr = funcArr;
//	this.options = options? options : [];
//	
//	$(formId).onsubmit = this.validate.bindAsEventListener(this);
//}

//ValidateForm.prototype.validate = function(){
//	if(!this.funcArr || this.funcArr.length<1) return;
//	for(var i=0;i<this.funcArr.length;i++){
//		if(typeof this.funcArr[i] != 'function') return false;
//		//alert(typeof this.funcArr[i]);
//		var option = (this.options)[i]?(this.options)[i] : {};
//		var result = this.funcArr[i](this.idForm, option);
//		if(result){
//			this.illegalArr.push(result);
//		}
//	}
//	return this.throwErr();
//}

//ValidateForm.prototype.throwErr = function(){
//	if(this.illegalArr.length>0){
//		alert(this.illegalArr[0].errMsg);
//		this.illegalArr[0].focus();
//		this.illegalArr = [];
//		return false;
//	}
//	return true;
//}

function checkEmail(idForm){
	var isEmail = Element.select($(idForm), '.isEmail');
	if(!isEmail) return;
	//var rg = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/i;
	var rg = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/i;
	for(var i = 0;i < isEmail.length;i++){
		if (!rg.test(isEmail[i].value)){
			isEmail[i].errMsg = 'El correo es obligatorio.';
			return isEmail[i];
		}
	}
}

function checkEmpty(idForm){
	var isNotEmpty = Element.select($(idForm), '.isNotEmpty');
	//alert('#'+arguments[0] + ' .isNotEmpty');
	if(!isNotEmpty) return;
	for(var i=0;i<isNotEmpty.length;i++){
		if(isNotEmpty[i].value==''){
			isNotEmpty[i].errMsg = 'Este campo no puede estar vacío.';
			return isNotEmpty[i];
		}
	}
}

// check phone number format
function checkNum(idForm){
	var isNum = Element.select($(idForm), '.isNum');
	var rg = /(^(\d{2,4}[-_?—]?)?\d{3,8}([-_?—]?\d{3,8})?([-_?—]?\d{1,7})?$)|(^0?1[35]\d{9}$)/i;
	if(!isNum) return;
	for(var i = 0;i < isNum.length;i++){
		if (!rg.test(isNum[i].value)){
			isNum[i].errMsg = 'El teléfono debe ser un número.';
			return isNum[i];
		}
	}
}

//check cif number format
function checkOnlyNum(idForm){
	var isNum = Element.select($(idForm), '.isOnlyNum');
	var rg = /^[a-z0-9]+$/i ;
	if(!isNum) return;
	for(var i = 0;i < isNum.length;i++){
		if( !(rg.test(isNum[i].value)) ) {
			isNum[i].errMsg = 'Only numbers and letters';
			return isNum[i];
		}
	}
}


/* options
 * {
 * 		sClassName: str
 * }
 * 		check least one field in form is not empty
 */
function checkEmptyLeast (idForm, options) {
	//var isEmptyLeast = document.getElementsByClassName('emptyLeast',$(tobeValid));
	sClassName = options.sClassName ? options.sClassName : 'isEmptyLeast';
	var isEmptyLeast = Element.select($(idForm), '.'+sClassName);
	if(!isEmptyLeast) return;
	var a;
	for(var i = 0; i < isEmptyLeast.length; i++) {
		if(isEmptyLeast[i].value != "") {
			a = true;
			if(a) return;
		}
	}
	if(a === undefined) {
			isEmptyLeast[0].errMsg = 'Al menos debe completarse un campo.';
			return isEmptyLeast[0];
	}
}

function checkLeastChecked(idForm) {
	var isCheckedLeast = Element.select($(idForm), '.isCheckedLeast');
	if(!isCheckedLeast) return;
	var a;
	for(var i = 0; i < isCheckedLeast.length; i++) {
		if(isCheckedLeast[i].checked == true) {
			a = true;
			if(a) return;
		}
	}
	if(a == undefined) {
		isCheckedLeast[0].errMsg = 'Al menos una opcin debe estar seleccionada.';
		return isCheckedLeast[0];
	}
}

function checkSelcValue(idForm){
	var isSelector = Element.select($(idForm), '.isSelector');
	//alert('#'+arguments[0] + ' .isNotEmpty');
	if(!isSelector) return;
	for(var i=0;i<isSelector.length;i++){
		if(isSelector[i].value==''){
			isSelector[i].errMsg = 'Debe seleccionar una opcin.';
			return isSelector[i];
		}
	}
}