function enviarDatos() {
	var formulario = document.form1;
    if( validaDatos() == false)
    	return;
	formulario.submit();
}

function validaDatos() {

	var formulario = document.form1;
	
	//if (Trim(formulario.razon.value) == ""){
	//	alert('Debes ingresar el campo Razon');
	//	formulario.razon.focus();
	//	return false;
	//}
	
	//if (Trim(formulario.rut.value) == ""){
	//	alert('Debes ingresar campo Rut');
	//	formulario.rut.focus();
	//	return false;
	//}

	//if (!formulario.dv.value){
	//	alert('Debes ingresar el dígito que está después del guión en tu Rut');
	//	formulario.dv.focus();
	//	return false;
	//}

    //if(!checkRutField( formulario.rut.value+"-"+formulario.dv.value, formulario.rut ) ) {
	//	formulario.rut.focus();
	//	return false;
	//}

	//if (Trim(formulario.nombre.value) == ""){
	//	alert('Debes ingresar el campo Nombre Empresa');
	//	formulario.nombre_razon_social.focus();
	//	return false;
	//}

	//if (!formulario.direccion.value){
	//	alert('Debes ingresar el campo Direccion');
	//	formulario.direccion.focus();
	//	return false;
	//}
	
	//var reg_sel = formulario.region.options[formulario.region.selectedIndex].value;
	//if ('0'==reg_sel){
	//	alert('Debes ingresar el campo Region');
	//	formulario.region.focus();
	//	return false;
	//}
	//var com_sel = formulario.comuna.options[formulario.comuna.selectedIndex].value;
	//if ('0'==com_sel){
	//	alert('Debes ingresar el campo Comuna');
	//	formulario.comuna.focus();
	//	return false;
	//}

	//if (!formulario.telefono.value){
	//	alert('Debes ingresar el campo Telefono Empresa');
	//	formulario.telefono.focus();
	//	return false;
	//}
	
	if (!formulario.contactoNombre.value){
		alert('Debes ingresar el campo Nombre de contacto');
		formulario.contactoNombre.focus();
		return false;
	}

	if (formulario.contactoRut.value || formulario.contactoDv.value){
		// si se ingresa rut o dv del contacto se debe validar
	    if(!checkRutField( formulario.contactoRut.value+"-"+formulario.contactoDv.value, formulario.contactoRut ) ) {
			formulario.contactoRut.focus();
			return false;
		}
	}

	if (!formulario.contactoTelefono.value){
		alert('Debes ingresar el campo Telefono de contacto');
		formulario.contactoTelefono.focus();
		return false;
	}
	if (!formulario.contactoEMail.value){
		alert('Debes ingresar el campo Email contacto');
		formulario.contactoEMail.focus();
		return false;
	}

	// valido formato del email
	var formato = /^([\w-\.])+@([\w-]+\.)+([A-Z]){2,4}$/;
	var comparacion = formato.test(formulario.contactoEMail.value);
	if(comparacion == false){
		alert("El e-mail del contacto ingresado no es válido");
		return false;
	}
	
	//alert(formulario.formaEntrega[0].checked);
	/*if (formulario.formaEntrega[0].checked){	
		var reg_sel = formulario.despRegion.options[formulario.despRegion.selectedIndex].value;
		if ('0'==reg_sel){
			alert('Debes ingresar el campo Region de despacho');
			formulario.v.focus();
			return false;
		}
		var com_sel = formulario.despComuna.options[formulario.despComuna.selectedIndex].value;
		if ('0'==com_sel){
			alert('Debes ingresar el campo Comuna de despacho');
			formulario.despComuna.focus();
			return false;
		}
	}	
	return false;*/
	
}

function validarEmail(valor){
	validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
	strEmail = valor.value;
	
	if (strEmail.search(validRegExp) == -1) {
	    alert("La dirección de email es incorrecta o está mal escrita. Ingrésala nuevamente");
	    valor.focus();
	    return (false);
	}else
		return (true);
}

function validarEmailDominio(valor,dominio){
	validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
	strEmail = valor.value + '@' + dominio.value;
	
	if (strEmail.search(validRegExp) == -1) {
	    alert("La dirección de email es incorrecta o está mal escrita. Ingrésala nuevamente");
	    valor.focus();
	    return (false);
	}else
		return (true);
} 


////////////////////////////////////////////////
function KeyIsNumber(evt){
var isNav = (navigator.appName.indexOf("Netscape") != -1)
var isIE = (navigator.appName.indexOf("Microsoft") != -1)

	if (isNav) {
		if ( ( evt.which >= 0 && evt.which <= 9 ) // Tab delete etc
            || (evt.which >= 48 &&  evt.which <=57) ) // [0-9]
		return true;
	return false;
	}
	else if (isIE)
		{evt = window.event;
		if ( (evt.keyCode >= 48 && evt.keyCode <= 57) )
			return true;
		return false;
		}
	else {
		alert("Su browser no es soportado por esta aplicación")
	}
	return false
}

function Trim( str ) {
	var resultStr = "";
	
	resultStr = TrimLeft(str);
	resultStr = TrimRight(resultStr);
	
	return resultStr;
}

function TrimRight( str ) {
	var resultStr = "";
	var i = 0;
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str == null) 
		return null;
	// Make sure the argument is a string
	str += "";

	if (str.length == 0) 
		resultStr = "";
	else {
		// Loop through string starting at the end as long as there
		// are spaces.
		i = str.length - 1;
		while ((i >= 0) && (str.charAt(i) == " "))
			i--;
	
		// When the loop is done, we're sitting at the last non-space char,
		// so return that char plus all previous chars of the string.
		resultStr = str.substring(0, i + 1);
	}

	return resultStr; 
}

function TrimLeft( str ) {
	var resultStr = "";
	var i = len = 0;
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str == null) 
		return null;
		// Make sure the argument is a string
		str += "";
	if (str.length == 0) 
		resultStr = "";
	else { 
		// Loop through string starting at the beginning as long as there
		// are spaces.
		// len = str.length - 1;
		len = str.length;

		while ((i <= len) && (str.charAt(i) == " "))
		i++;
		// When the loop is done, we're sitting at the first non-space char,
		// so return that char plus the remaining chars of the string.
		resultStr = str.substring(i, len);
	}
	return resultStr;
}

////////////////////////////////////////////////
function KeyIsNumber(evt){
var isNav = (navigator.appName.indexOf("Netscape") != -1)
var isIE = (navigator.appName.indexOf("Microsoft") != -1)

	if (isNav) {
		if ( ( evt.which >= 0 && evt.which <= 9 ) // Tab delete etc
            || (evt.which >= 48 &&  evt.which <=57) ) // [0-9]
		return true;
	return false;
	}
	else if (isIE)
		{evt = window.event;
		if ( (evt.keyCode >= 48 && evt.keyCode <= 57) )
			return true;
		return false;
		}
	else {
		alert("Su browser no es soportado por esta aplicación")
	}
	return false
}

function Trim( str ) {
	var resultStr = "";
	
	resultStr = TrimLeft(str);
	resultStr = TrimRight(resultStr);
	
	return resultStr;
}

function TrimRight( str ) {
	var resultStr = "";
	var i = 0;
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str == null) 
		return null;
	// Make sure the argument is a string
	str += "";

	if (str.length == 0) 
		resultStr = "";
	else {
		// Loop through string starting at the end as long as there
		// are spaces.
		i = str.length - 1;
		while ((i >= 0) && (str.charAt(i) == " "))
			i--;
	
		// When the loop is done, we're sitting at the last non-space char,
		// so return that char plus all previous chars of the string.
		resultStr = str.substring(0, i + 1);
	}

	return resultStr; 
}

function TrimLeft( str ) {
	var resultStr = "";
	var i = len = 0;
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str == null) 
		return null;
		// Make sure the argument is a string
		str += "";
	if (str.length == 0) 
		resultStr = "";
	else { 
		// Loop through string starting at the beginning as long as there
		// are spaces.
		// len = str.length - 1;
		len = str.length;

		while ((i <= len) && (str.charAt(i) == " "))
		i++;
		// When the loop is done, we're sitting at the first non-space char,
		// so return that char plus the remaining chars of the string.
		resultStr = str.substring(i, len);
	}
	return resultStr;
	
}

var _comuna = "";
var esComuna2 = "N";

var todasComunas = "N"
function buscar_comunasTodas( ) {
	//alert("buscar_comunasTodas()");
	var formulario = document.form1;
	//reg_sel = formulario.region.options[formulario.region.selectedIndex].value;
	reg_sel2 = formulario.despRegion.options[formulario.despRegion.selectedIndex].value;
	todasComunas = "S";
	esComuna2 = "N";
	var url = "ComunasList?reg_id=0&amp;reg_id2="+reg_sel2;
	top.frames['ifcomuna'].location.href=url;
}

//function buscar_comunas( ) {
//	//alert("buscar_comunas()");
//	var formulario = document.form1;
//	reg_sel = formulario.region.options[formulario.region.selectedIndex].value;
//	todasComunas = "N";
//	esComuna2 = "N";
//	var url = "ComunasList?reg_id="+reg_sel;
//	top.frames["ifcomuna"].location.href=url;
//}
function buscar_comunasDesp() {
	//alert("buscar_comunasDesp()");
	var formulario = document.form1;
	reg_sel = formulario.despRegion.options[formulario.despRegion.selectedIndex].value;
	todasComunas = "N";
	esComuna2 = "S";
	var url = "ComunasList?reg_id="+reg_sel;
	top.frames["ifcomuna"].location.href=url;
}

// esta funcion ejecuta la respuesta de la consulta a ComunasList
function procom() {
	//alert("procom()");
	var formulario = document.form1;

	//var listComunas = formulario.comuna;
	//if (esComuna2=="S"){
		listComunas = formulario.despComuna;
	//}

	// limpio combo
	var cant = listComunas.options.length;
	for( var i = 0; i < cant; i++ )
		listComunas.options[i] = null;
	listComunas.length=0;

	// obtengo comunas
	texto = top.frames["ifcomuna"].document.frmcom.com.value;
	var response = texto.split("|");

	makeOptionList(listComunas,'Seleccionar Comuna',0,0);
	for( i = 0; i < response.length-1; i++ ) {
		comunas = response[i].split("-");
		if( comunas[0] == _comuna )
			makeOptionList(listComunas,comunas[1],comunas[0],1);
		else
			makeOptionList(listComunas,comunas[1],comunas[0],0);
	}
	
	if (todasComunas=="S"){
	
		// limpio combos
		listComunas = formulario.despComuna;
		var cant = listComunas.options.length;
		for( var i = 0; i < cant; i++ )
			listComunas.options[i] = null;
		listComunas.length=0;

		// obtengo comunas
		texto = top.frames["ifcomuna"].document.frmcom.com2.value;
		var response = texto.split("|");

		makeOptionList(listComunas,'Seleccionar Comuna',0,0);
		for( i = 0; i < response.length-1; i++ ) {
			comunas = response[i].split("-");
			if( comunas[0] == _comuna )
				makeOptionList(listComunas,comunas[1],comunas[0], 1);
			else
				makeOptionList(listComunas,comunas[1],comunas[0], 0);
		}
		// selecciona las comunas que se seleccionaron inicialmente
		selComuna();		
	}
}

