function trim(cadena)
{
	for(i=0; i<cadena.length; )
	{
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(i+1, cadena.length);
		else
			break;
	}

	for(i=cadena.length-1; i>=0; i=cadena.length-1)
	{
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(0,i);
		else
			break;
	}
	
	return cadena;
}

function vpreregistro(){
	if(trim(document.pre_reg.razonsocial.value) == ""){
		alert("La razon social es obligatoria");
		return false;
	}
	if(document.pre_reg.slc_ministry.value == "0"){
		alert("El ministerio es obligatorio");
		return false;
	}
	if(trim(document.pre_reg.cedula.value) == ""){
		alert("El numero de cedula es obligatorio");
		return false;
	}
	if(trim(document.pre_reg.apaterno.value) == ""){
		alert("El apellido paterno es obligatorio");
		return false;
	}
	if(trim(document.pre_reg.pnombre.value) == ""){
		alert("El primer nombre es obligatorio");
		return false;
	}	
	if(trim(document.pre_reg.mail.value) == ""){
		alert("El mail es obligatorio");
		return false;
	}
	if(trim(document.pre_reg.telefono.value) == ""){
		alert("El telefono es obligatorio");
		return false;
	}	
	if(trim(document.pre_reg.login.value) == ""){
		alert("El nombre de usuario es obligatorio");
		return false;
	}
	if(trim(document.pre_reg.password.value) == ""){
		alert("El password es obligatorio");
		return false;
	}
	if(trim(document.pre_reg.password.value) != ""){
		if(trim(document.pre_reg.password.value) != trim(document.pre_reg.cpassword.value))
		{
			alert("Repita el password");
			document.pre_reg.password.value = "";
			document.pre_reg.cpassword.value = "";
			return false;
		}
	}	
	return true;
}

function ControlSelection(form, idsel)
	{
		var limit = 1;
		var counter = 0;
		for(i = 0; i < form.elements.length; i++)
		{
			if(form.elements[i].type == 'checkbox')
			{
				if(form.elements[i].id == idsel)
				{
					if(form.elements[i].checked == true)
					{
						counter++;
					}
				}
			}
		}
		
		if(counter > limit)
		{
			counter = counter - 1;
			return false;
			
		}
		return true;
	}

function transfer_submit()
{
	if(document.edit.potencialMId.value!='')
	{ 
		if(trim(document.edit.motivo.value) != '')
		{ 
			if(trim(document.edit.fechaCambio.value) != '')
			{
				var indice = document.edit.potencialMId.selectedIndex; 
				if(confirm('Est\u00E1 seguro de Traspasar la OSC al ' + document.edit.potencialMId.options[indice].text +' ?'))
				{ 
					document.edit.editar.value=1; 
					document.edit.submit(); 
				}
			}
			else
			{
				alert('El campo Fecha en que se definio el Traspaso es obligatorio');
			} 
		} 
		else
		{ 
			alert('El campo motivo es obligatorio'); 
		} 
	} 
	else
	{ 
		alert('Seleccione un Ministerio para hacer el traspaso'); 
	}
}

function transferdesrec_submit()
{
	if(trim(document.edit.motivo.value) != '')
	{ 
		if(trim(document.edit.fechaCambio.value) != '')
		{
			if(confirm('Est\u00E1 seguro de Rechazar el Traspaso de la OSC al ' + document.edit.potencialMId.value +'?'))
			{ 
				document.edit.editar.value=1; 
				document.edit.submit(); 
			}
		}
		else
		{
			alert('El campo Fecha en que se definio el Rechazo de Traspaso es obligatorio');
		}
	} 
	else
	{ 
		alert('El campo motivo es obligatorio'); 
	} 
}

function transferdesace_submit()
{
	if(trim(document.edit.motivo.value) != '')
	{ 
		if(trim(document.edit.fechaCambio.value) != '')
		{
			if(confirm('Est\u00E1 seguro de Aceptar el Traspaso de la OSC al ' + document.edit.potencialMId.value +'?'))
			{ 
				document.edit.editar.value=2;				 
				document.edit.submit(); 
			}
		}
		else
		{
			alert('El campo Fecha en que se definio la Aceptaci\u00F3n de Traspaso es obligatorio');
		}
	} 
	else
	{ 
		alert('El campo motivo es obligatorio'); 
	} 
}

function ControlSelectionUnit(obj, idsel)
{
	if (!obj.checked) return
    	elem=document.getElementsByName(idsel);
    for(i=0;i<elem.length;i++) 
        elem[i].checked=false;
    obj.checked=true;
}

function validar_contactos()
{
 	if ((document.defaultmi.apellidop.value!="")||(document.defaultmi.apellidom.value!="")||(document.defaultmi.pnombre.value!="")||(document.defaultmi.snombre.value!="")||(document.defaultmi.telefonoc.value!="")||(document.defaultmi.mailc.value!="")||(document.defaultmi.cedula.value!=""))
 	{
 		
 		if (document.defaultmi.cedula.value==""){
 			alert('El n\xfamero de C\xe9dula del Contacto es obligatorio');
 			return;
 		}
 		if ((document.defaultmi.apellidop.value=="") && (document.defaultmi.apellidom.value=="")) {
 			alert('Debe ingresar al menos un apellido del Contacto');
 			return;
 		}
 		if (document.defaultmi.pnombre.value==""){
 			alert('Debe ingresar el primer nombre del Contacto');
 			return;
 		}
 		if (document.defaultmi.telefonoc.value==""){
 			alert('Debe ingresar el telefono del Contacto');
 			return;
 		}
 		
 		document.defaultmi.submit();
 		
 	} else
 	{
 		document.defaultmi.submit();
 	}
 	
}


function validate_general_information_form(){
	var validate = true;
	var error = 'Corregir los siguiente errores por favor:\n\t Datos Generales.';
	if(trim(document.defaultmi.social_reason.value) == ""){
		error += "\n\t\t * La razon social es obligatoria";
		validate =  false;
	}
	if(trim(document.defaultmi.constitution_date.value) == ""){
		error += "\n\t\t * La Fecha de Constituci\xf3n es obligatoria";
		validate =  false;
	}else{
		var myDate = new Date();
		var day = myDate.getDate() < 10 ? '0' + myDate.getDate() : myDate.getDate();
		var today = myDate.getFullYear()+'-'+(myDate.getMonth()+1)+'-'+day;
		if (document.defaultmi.constitution_date.value > today ) {
			error += "\n\t\t * La Fecha de Constituci\xf3n no puede ser mayor a la actual";
			validate =  false;
		}
	}
	if(document.defaultmi.ministry.value == ''){
		error += "\n\t\t * El Ministerio es obrigatorio";
		validate =  false;
	}
	if(document.defaultmi.authorization_document.value == ''){
		error += "\n\t\t * El tipo de documento es obrigatorio";
		validate =  false;
	}
	if(trim(document.defaultmi.number_document.value) == ""){
		error += "\n\t\t * El numero de documento es obrigatorio";
		validate =  false;
	}
	if(document.defaultmi.province.value == ''){
		error += "\n\t\t * Selecione una provincia";
		validate =  false;
	}	
	if(document.defaultmi.canton.value == ''){
		error += "\n\t\t * Selecione un canton";
		validate =  false;
	}
	if(trim(document.defaultmi.main_street.value) == ""){
		error += "\n\t\t * La calle principal es obligatoria";
		validate =  false;
	}
	if (!validate) alert(error); 
	return validate;
}

function numCheckbox(obj,state){
	var i;
	var count=0;
	for (i=0;i<obj.length;i++) 
		if (obj[i].checked == state) count++;
	return count;
}	

function chk_provinces()
{
	var i;
	
	if (!document.defaultmi.provincias.checked){
		
		for (i=0;i<document.defaultmi.chkpro.length;i++) 
		{
			document.defaultmi.chkpro[i].checked = false;
		}

		}	
}

