// JavaScript Document
valido = true;
msj = "Por favor, ingrese los siguientes datos:\n\r";
focusto = "";

function validaForm()
{
	var frm = document.forms[0];
	
	var arrInput = new Array('txtRazon', 'txtNombre', 'txtRubro', 'txtDireccion', 'txtRuc', 'txtTelefono', 'txtCelular', 'txtInteresado', 'txtCargo', 'txtEmail');
	var arrMsjs  = new Array('Razón social', 'Nombre comercial', 'Rubro', 'Dirección', 'RUC', 'Teléfono', 'Celular', 'Nombre del interesado', 'Cargo', 'Correo electrónico');

	for (i = 0; i < arrInput.length; i++) {
		validator(arrInput[i], arrMsjs[i]);
	}
	
	if (getThis('cboProvincia') == "-1") {
		msj += "\t- Provincia.\n\r";
		if (valido) {
			focusto = getThat('cboProvincia');
			valido = false;
		}
	}
	
	if (parseInt(getThis('cboProvincia')) == 2 && getThis('cboDistrito') == "-1") {
		msj += "\t- Distrito.\n\r";
		if (valido) {
			focusto = getThat('cboDistrito');
			valido = false;
		}
	}
	
	if (valido && !checkEmail(getThis('txtEmail'))) {
		msj += "\t- Correo electrónico correcto.\n\r";
		if (valido) {
			focusto = getThat('txtEmail');
			valido = false;
		}
	}
	
	if (valido === true) {
		//frm.action = location.href.replace(/#/, "") + '/contacto-submit';
		frm.submit();
	} else {
		alert(msj);
		focusto.focus();
		restartVars();
	}
}

function restartVars() {
	valido = true;
	msj = "Por favor, ingrese los siguientes datos:\n\r";
	focusto = "";
}

function checkEmail(txt) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(txt)) {
		return true;
	} else {
		return false;
	}
}

function validator(elInput, elMsj) {
	//if (getThat(elInput).type == "text" || getThat(elInput).type == "textarea") {
	$arrTypes = Array("text", "textarea", "file");
	if ($arrTypes.inArray(getThat(elInput).type)) {
		//alert(getThat(elInput).id + ' ' + getThat(elInput).type);
		if (getThis(elInput) == "") {
			msj += "\t- " + elMsj + ".\n\r";
			if (valido) {
				focusto = getThat(elInput);
				valido = false;
			}
		}
	} else if (getThat(elInput).type == "select-one") {
		//alert(getThat(elInput).id);
		if (getThis(elInput) == "-1") {
			msj += "\t- " + elMsj + ".\n\r";
			if (valido) {
				focusto = getThat(elInput);
				valido = false;
			}
		}
	}
}

function getThis(elInput) {
	return document.getElementById(elInput).value;
}

function getThat(elInput) {
	return document.getElementById(elInput);
}

Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};

function validaCaracteres(e)
{
	tecla = (document.all) ? e.keyCode : e.which;
	if (tecla == 8 || tecla == 0 || tecla == 13) return true;
	patron = /[A-Za-z0-9á-ú.ñÑ_(){}[] ]/;
	te = String.fromCharCode(tecla);
	return patron.test(te);
}

function validaEmail(e)
{
	tecla = (document.all) ? e.keyCode : e.which;
	if (tecla == 8 || tecla == 0 || tecla == 13) return true;
	patron = /[A-Za-z0-9@._ -]/;
	te = String.fromCharCode(tecla);
	return patron.test(te);
}

function validaTelefono(e)
{
	tecla = (document.all) ? e.keyCode : e.which;
	if (tecla == 8 || tecla == 0) return true;
	patron = /(^[0-9-]$)/;
	te = String.fromCharCode(tecla);
	return patron.test(te);
}

function chgVisibility(provincia) {
	if (provincia == 2) {
		tr = document.getElementById('tabla').getElementsByTagName('tr')[5];
		tr.style.display = '';
	} else {
		tr = document.getElementById('tabla').getElementsByTagName('tr')[5];
		tr.style.display = 'none';
	}
}