/*===============================================================
Largo minimo para palabra a buscar
=================================================================*/
var MIN_FIELD_LENGTH = 2;

/*===============================================================
Valida que las palabras ingresadas en strText sean de largo mayor que MIN_FIELD_LENGTH
=================================================================*/
function ValidaLargo( Lang, strText ){
var arrayOfStrings;
var lngLargo;
	arrayOfStrings = strText.split( " " );
	for ( var i = 0; i < arrayOfStrings.length; i++){
		if ( arrayOfStrings[i].length  < MIN_FIELD_LENGTH ){
			if ( Lang=="esp" )			
				alert( 'Usted debe ingresar al menos ' + MIN_FIELD_LENGTH +' caracteres (letras o números) en las cajas de texto.' );
			if ( Lang=="por" )			
				alert( 'Você deve digitar pelo menos ' + MIN_FIELD_LENGTH +' caracteres (letras ou números) nos campos de texto.' );
			if ( (Lang!="esp") && (Lang!="por") )
				alert( 'You must enter at least ' + MIN_FIELD_LENGTH +' characters (letters or numbers) in the text boxes.' );
			return false;
		}
	}
	return true;
}// fin ValidaLargo

/*===============================================================
Valida que las palabras ingresadas en strText sean de largo mayor que MIN_FIELD_LENGTH
=================================================================*/
function ValidaLargoNEW( Lang, field ){
var arrayOfStrings;
var lngLargo, FixedStr;
var Str = String(field);
var regEx = /\s\s+/g; 
	
	FixedStr = Str.replace(regEx, ' ');	// Reemplazo  2 o más espacios   por   espacio
	FixedStr = Trim(FixedStr);
	//alert ('-' + FixedStr + '-');
	
	arrayOfStrings = FixedStr.split( " " );
	//alert ('ultimo:' + arrayOfStrings[arrayOfStrings.length-1]);
	//alert ('largo:' + arrayOfStrings.length);
	
	if (arrayOfStrings.length == 1){
		if (arrayOfStrings[0].length < 2 ){
			
			if (Lang=="esp") 
				alert ('Si sólo ingresa una palabra en el campo de Palabra(s) Clave(s), ésta debe tener al menos 2 caracteres.');
			if (Lang=="por") 
				alert ('Você precisa inserir pelo menos dois caracteres no campo de Palavra(s)-Chave. Por favor, tente novamente.');
			if (Lang!="esp" && Lang!="por") 
				alert('If you only enter one word in the Keyword(s) field, it must have at least 2 characters. Please go back and review your Keyword(s).');				
			return false;
		}
	}
	else{
		if (document.frmbasico.wordstype[1].selected ){ //ANY selected 
			for ( var i = 0; i < arrayOfStrings.length; i++){
				if (arrayOfStrings[i].length == 1 ){
					if (Lang=="esp") 
						alert("Al buscar con la opción \"Cualquier Palabra\", todas las palabras ingresadas deben tener al menos 2 caracteres.");
					if (Lang=="por") 
						alert("Quando você pesquisa por \"Qualquer Palavra\", deve inserir pelo menos dois caracteres. Por favor, tente novamente.");
					if (Lang!="esp" && Lang!="por") 
						alert("When you search with the option \"Any Words\", all words you enter must contain at least 2 characters.  Please go back and review your Keyword(s).");						
					//alert('dos caracteres de largo 1 juntos');
					return false;
				}

			}				
		}	
	}
	return true;
		
}// fin ValidaLargo


/*===============================================================
Cambia el action del formulario
=================================================================*/
function RedirectForm( ) {
	document.frmbasico.action="results.asp";
	document.frmbasico.submit( );
} // Fin RedirectForm

/*===============================================================
Retorna una copia del string sin espacios en blanco a la izquierda
=================================================================*/
function LTrim( str ){
	var whitespace = new String( " \t\n\r" );
	var s = new String( str );
	if ( whitespace.indexOf( s.charAt( 0 ) ) != -1 ) {
	    var j=0, i = s.length;
	    while ( j < i && whitespace.indexOf( s.charAt( j ) ) != -1 )
	        j++;
		    s = s.substring( j, i );
	}
	return s;
}// Fin LTrim

/*===============================================================
Retorna una copia del string sin espacios en blanco a la derecha
=================================================================*/						  
function RTrim( str ){
	var whitespace = new String( " \t\n\r" );
	var s = new String( str );
	if ( whitespace.indexOf( s.charAt( s.length - 1 ) ) != -1 ) {
	    var i = s.length - 1;       // Get length of string
	    while ( i >= 0 && whitespace.indexOf( s.charAt( i ) ) != -1)
	        i--;
	    s = s.substring( 0, i+1 );
	}
	return s;
}// Fin RTrim

/*===============================================================
Retorna una copia del string sin espacios en blanco a ambos lados
=================================================================*/
function Trim( str ){
	return RTrim( LTrim( str ) );
}// Trim
	
//**************************************************************************************
//retorna verdadero si value es numerico
//**************************************************************************************
function isNumber( value ){
	if( isNaN( value ) || (value=="" ) ){
		return false;
	}else{
		return true;
	}
}// Fin isNumber

//**************************************************************************************
//retorna el largo del campo
//**************************************************************************************
function getLength( field ){
	var TrimStr = Trim( field.value );
	var s = new String( TrimStr );
	return s.length;
}//Fin getLength

//**************************************************************************************
//retorna verdadero si el valor de value corresponde a campo con caracteres validos
//**************************************************************************************
function isValidField(Lang, field, fieldName){
	if( field.value.indexOf("'") >= 0  ){
		if (Lang=="esp") 
			alert("Caracter apóstrofe ( \' ) no permitido en " + fieldName + ".");
		if (Lang=="por") 
			alert("Apóstrofo ( \' ) não é permitido em " + fieldName + ".");
		if (Lang!="esp" && Lang!="por") 
			alert("Apostrophes ( \' ) are not allowed in the " + fieldName + " field.");	
		//alert("Caracter apostrofe ( \' ) no permitido en " + fieldName);
		return false;
	}
	if( field.value.indexOf('"') >= 0  ){
		if (Lang=="esp") 
			alert("Caracter comillas ( \" ) no permitido en " + fieldName + ".");
		if (Lang=="por") 
			alert("Aspas ( \" ) não são permitidas em " + fieldName + ".");
		if (Lang!="esp" && Lang!="por") 
			alert("Quotation marks ( \" ) are not allowed in the " + fieldName + " field.");	
		//alert("Caracter comillas ( \" ) no permitido en " + fieldName);
		return false;
	}
	if ( !ValidaLargoNEW( Lang, field.value ) ) return false;
	else return true;
}//Fin isValidField

//**************************************************************************************
// Realiza la validacion de todos los campos comunes a todas las consultas (keywords, notkeyword y wordstype)
//**************************************************************************************
function ValidaBasic( Lang ){
	if ( getLength( document.frmbasico.keywords )  == 0 ){
		if ( Lang == "esp" ) 
			alert( "Usted debe ingresar al menos una palabra (letras o números) para buscar." );
		if ( Lang == "por" ) 
			alert( "Você deve digitar pelo menos uma palavra  (letras ou números) para efetuar a busca." );
		if ( (Lang != "esp") && (Lang != "por" ) )
			alert( "You must enter at least one word (letters or numbers) to begin the search." );	
		return false;
	}else{
		var objKeyw = "Keyword";
		var objNotKeyw = "Not Keyword";
		if ( !isValidField( Lang, document.frmbasico.keywords, objKeyw ) ) return false;
		else
			if ( getLength( document.frmbasico.notkeyword )  > 0 )
				if ( !isValidField( Lang, document.frmbasico.notkeyword, objNotKeyw ) )
					return false;
	}
	return true;
}//Fin ValidaBasic

//**************************************************************************************
// Realiza la validacion de los campos de Business Opportunities
//**************************************************************************************
function ValidaBusinessOpp( Lang ){
	if ( ValidaBasic( Lang ) ){
		var intCont

		//Validacion del Tipo de busqueda
		intCont = 0;		
		while  ( intCont < 6 ){
			if ( document.frmbasico.chkBOSType[ intCont ].checked )
				break;
			intCont ++;
		}
		if ( intCont == 6 ){			
			if ( Lang == "esp" ) 
				alert( 'Debe seleccionar al menos un Tipo de Oportunidad de Negocio.' );
			if ( Lang == "por" ) 
				alert( 'Você deve selecionar pelo menos um Tipo de oportunidade de negócio.' );
			if ( Lang != "esp" && Lang != "por" ) 
				alert( ' You must select at least one Type of business opportunity.' );
			return;
		}
		//Validacion del Campo de busqueda
		intCont = 0;
		while  ( intCont < 5 ){
			if ( document.frmbasico.chkBOSField[ intCont ].checked )
				break;
			intCont ++;
		}
		if ( intCont == 5 ){
			if ( Lang == "esp" ) 
				alert( 'El Campo es requerido para activar el motor de búsqueda..  Por favor seleccione al menos una de las opciones dadas.' );
			if ( Lang == "por" ) 
				alert( 'O Campo dados é obrigatório para ativar a busca. Por favor, selecione pelo menos uma das opções indicadas.' );
			if ( Lang != "esp" && Lang != "por" ) 
				alert( 'The Field is required to activate the search engine.  Please select at least one of the options given.' );
			return;
		}
		//Validacion del Pais de busqueda
		/*intCont = 0;
		while  ( intCont < 7 ){
			if ( document.frmbasico.Countrychk[ intCont ].checked )
				break;
			intCont ++;	
		}
		if ( intCont == 7 ){
			if ( Lang == "esp" ) 
				alert( 'El campo País es requerido para activar el motor de búsqueda.  Por favor seleccione al menos una de las opciones dadas.' );
			if ( Lang == "por" ) 
				alert( 'O campo País é obrigatório para ativar a busca. Por favor, selecione pelo menos uma das opções indicadas.' );
			if ( Lang != "esp" && Lang != "por" ) 
				alert( 'The Country field is required to activate the search engine.  Please select at least one of the options given.' );
			return;
		}else*/
		RedirectForm( );
	}
}//Fin ValidaBusinessOpp

//**************************************************************************************
// Realiza la validacion de los campos de Job Opportunities
//**************************************************************************************
function ValidaJobOpp( Lang ){
	if ( ValidaBasic( Lang ) ){
		var intCont

		//Validacion del Campo de busqueda
		intCont = 0;		
		while  ( intCont < 5 ){
			if ( document.frmbasico.chkJobField[ intCont ].checked )
				break;
			intCont ++;
		}
		if ( intCont == 5 ){
			if ( Lang == "esp" ) 
				alert( 'El campo es requerido para activar el motor de búsqueda..  Por favor seleccione al menos una de las opciones dadas.' );
			if ( Lang == "por" ) 
				alert( 'O campo Dados é obrigatório para ativar a busca. Por favor, selecione pelo menos uma das opções indicadas.' );
			if ( Lang != "esp" && Lang != "por" ) 
				alert( 'The Field is required to activate the search engine.  Please select at least one of the options given.' );
			return;
		}
		
		//Validacion del Pais de busqueda
		/*intCont = 0;
		while  ( intCont < 7 ){
			if ( document.frmbasico.Countrychk[ intCont ].checked )
				break;
			intCont ++;	
		}
		if ( intCont == 7 ){
			if ( Lang == "esp" ) 
				alert( 'El campo País es requerido para activar el motor de búsqueda.  Por favor seleccione al menos una de las opciones dadas.' );
			if ( Lang == "por" ) 
				alert( 'O campo País é obrigatório para ativar a busca. Por favor, selecione pelo menos uma das opções indicadas.' );
			if ( Lang != "esp" && Lang != "por" ) 
				alert( 'The Country field is required to activate the search engine.  Please select at least one of the options given.' );
			return;
		}else*/
			RedirectForm( );		
	}
}//Fin ValidaJobOpp

//**************************************************************************************
// Realiza la validacion de los campos de Company Listing
//**************************************************************************************
function ValidaCompanyListing( Lang ){
	if ( ValidaBasic( Lang ) ){
		var intCont

		//Validacion del Categoria de busqueda
		intCont = 0;		
		while  ( intCont < 7 ){
			if ( document.frmbasico.optSearchBy[ intCont ].checked )
				break;
			intCont ++;
		}
		if ( intCont == 7 ){
			if ( Lang == "esp" ) 
				alert( 'The Category field is required to activate the search engine.  Please select one of the six Categories.' );
			if ( Lang == "por" ) 
				alert( 'O campo Categoria é obrigatório para ativar a busca. Por favor, selecione uma das seis Categorias.' );
			if ( Lang != "esp" && Lang != "por" ) 
				alert( 'El campo Categoría es requerido para activar el motor de búsqueda. Por favor elija una de las seis categorías.' );
			return;
		}

		//Validacion del Pais de busqueda
		/*intCont = 0;
		while  ( intCont < 7 ){
			if ( document.frmbasico.Countrychk[ intCont ].checked )
				break;
			intCont ++;	
		}
		if ( intCont == 7 ){
			if ( Lang == "esp" ) 
				alert( 'El campo País es requerido para activar el motor de búsqueda.  Por favor seleccione al menos una de las opciones dadas.' );
			if ( Lang == "por" ) 
				alert( 'O campo País é obrigatório para ativar a busca. Por favor, selecione pelo menos uma das opções indicadas.' );
			if ( Lang != "esp" && Lang != "por" ) 
				alert( 'The Country field is required to activate the search engine.  Please select at least one of the options given.' );
			return;
		}else*/
			RedirectForm( );
	}
}//Fin ValidaCompanyListing

//**************************************************************************************
// Realiza la validacion de los campos de Importaciones y Exportaciones
//**************************************************************************************
function ValidaImportExport( Lang ){
	if ( ValidaBasic( Lang ) ){
		var intCont

		//Validacion del Tipo de busqueda
		intCont = 0;		
		while  ( intCont < 2 ){
			if ( document.frmbasico.chkIESearchBy[ intCont ].checked )
				break;
			intCont ++;
		}
		if ( intCont == 2 ){
			if ( Lang == "esp" ) 
				alert( 'El Campo de Búsqueda es requerido para activar el motor de búsqueda. Por favor seleccione al menos una de las opciones dadas.' );
			if ( Lang == "por" ) 
				alert( 'O Campo da Busca é obrigatório para ativar a busca. Por favor, selecione pelo menos uma das opções indicadas.' );
			if ( Lang != "esp" && Lang != "por" ) 
				alert( 'The Search Field is required to activate the search engine.  Please select at least one of the options given.' );
			return;
		}else
			RedirectForm( );
	}
}//Fin ValidaImportExport

//**************************************************************************************
// Realiza la validacion de los campos de Newsroom
//**************************************************************************************
function ValidaNewsRoom( Lang ){
	if ( ValidaBasic( Lang ) ){
		var intCont
		
		//Validacion de la Seccion de busqueda
		intCont = 0;		
		while  ( intCont < 5 ){
			if ( document.frmbasico.chkNewsSection[ intCont ].checked )
				break;
			intCont ++;
		}
		if ( intCont == 5 ){
			if ( Lang == "esp" ) 
				alert( 'El campo Sección es requerido para activar el motor de búsqueda. Por favor seleccione al menos una de las opciones dadas.' );
			if ( Lang == "por" ) 
				alert( 'O campo Seção é obrigatório para ativar a busca. Por favor, selecione pelo menos uma das opções indicadas.' );
			if ( Lang != "esp" && Lang != "por" ) 
				alert( 'The Section field is required to activate the search engine.  Please select at least one of the options given.' );
			return;
		}

		//Validacion del Campo de busqueda
		intCont = 0;
		while  ( intCont < 2 ){
			if ( document.frmbasico.chkNewsSearchBy[ intCont ].checked )
				break;
			intCont ++;	
		}
		if ( intCont == 2 ){
			if ( Lang == "esp" ) 
				alert( 'El Campo de Búsqueda es requerido para activar el motor de búsqueda..  Por favor seleccione al menos una de las opciones dadas.' );
			if ( Lang == "por" ) 
				alert( 'O Campo da Busca é obrigatório para ativar a busca. Por favor, selecione pelo menos uma das opções indicadas.' );
			if ( Lang != "esp" && Lang != "por" ) 
				alert( 'The Search Field is required to activate the search engine.  Please select at least one of the options given.' );
			return;
		}

		//Validacion del Pais de busqueda
		/*intCont = 0;
		while  ( intCont < 7 ){
			if ( document.frmbasico.Countrychk[ intCont ].checked )
				break;
			intCont ++;	
		}
		if ( intCont == 7 ){
			if ( Lang == "esp" ) 
				alert( 'El campo País es requerido para activar el motor de búsqueda.  Por favor seleccione al menos una de las opciones dadas.' );
			if ( Lang == "por" ) 
				alert( 'O campo País é obrigatório para ativar a busca. Por favor, selecione pelo menos uma das opções indicadas.' );
			if ( Lang != "esp" && Lang != "por" ) 
				alert( 'The Country field is required to activate the search engine.  Please select at least one of the options given.' );
			return;
		}*/

		//Validacion del Lenguaje de busqueda
		intCont = 0;
		while  ( intCont < 4 ){
			if ( document.frmbasico.chkNewsLang[ intCont ].checked )
				break;
			intCont ++;	
		}

		if ( intCont == 4 ){
			if ( Lang == "esp" ) 
				alert( 'Debe seleccionar al menos un lenguaje.' );
			if ( Lang == "por" ) 
				alert( 'Você deve selecionar pelo menos um idioma.' );
			if ( Lang != "esp" && Lang != "por" ) 
				alert( 'You Must select at least one language to activate the search.' );
			return;
		}else
			RedirectForm( );
	}
}//Fin ValidaNewsRoom

//**************************************************************************************
// Realiza la validacion de los campos de Product Catalogs
//**************************************************************************************
function ValidaProductCatalogs( Lang ){
	if ( ValidaBasic( Lang ) ){
		var intCont

		//Validacion del Campo de busqueda
		intCont = 0;
		while  ( intCont < 3 ){
			if ( document.frmbasico.chkProdSearchBy[ intCont ].checked )
				break;
			intCont ++;	
		}
		if ( intCont == 3 ){
			if ( Lang == "esp" ) 
				alert( 'El Campo de Búsqueda es requerido para activar el motor de búsqueda..  Por favor seleccione al menos una de las opciones dadas.' );
			if ( Lang == "por" ) 
				alert( 'O Campo da Busca é obrigatório para ativar a busca. Por favor, selecione pelo menos uma das opções indicadas.' );
			if ( Lang != "esp" && Lang != "por" ) 
				alert( 'The Search Field is required to activate the search engine.  Please select at least one of the options given.' );
			return;
		}

		//Validacion del Pais de busqueda
		/*intCont = 0;
		while  ( intCont < 7 ){
			if ( document.frmbasico.Countrychk[ intCont ].checked )
				break;
			intCont ++;	
		}
		if ( intCont == 7 ){
			if ( Lang == "esp" ) 
				alert( 'El campo País es requerido para activar el motor de búsqueda.  Por favor seleccione al menos una de las opciones dadas.' );
			if ( Lang == "por" ) 
				alert( 'O campo País é obrigatório para ativar a busca. Por favor, selecione pelo menos uma das opções indicadas.' );
			if ( Lang != "esp" && Lang != "por" ) 
				alert( 'The Country field is required to activate the search engine.  Please select at least one of the options given.' );
			return;
		}else*/
		RedirectForm( );
	}
}//Fin ValidaProductCatalogs



/*===============================================================
Retorna a la pag. QueryCompanies si se cumplen condiciones
=================================================================*/
function RetornaAAsp(lang)
{ 

	var strTipoBusqueda;
	var intCodigoPais;
	var strNombreCampo;
	var url;

	if ((Trim(document.FormSearchGeneric.keywords.value)=="") || (document.FormSearchGeneric.keywords.value.length==0))
	{
		if (lang=="esp") {
			alert ('Debe ingresar al menos una palabra a buscar.');
			return false;
		}
		if (lang=="por") {
			alert ('Voce deve inserir no mínimo uma palavra chave para efetuar a busca.');
			return false;
		}
		if (lang!="esp" && lang!="por") {
			alert ('You must insert at least one keyword to search.');
			return false;
		}				
	}

	url ='/SE/Results.asp';
	switch (document.FormSearchGeneric.optCategory.selectedIndex){
		case 0 : 
			//Buscando Actividades
			url ='/SE/MakeQueryActivities.asp';
			break;	
		case 1 :
			//Buscando Nombres de Compañías
			document.FormSearchGeneric.optsearchby.value='mecolegalname';			
			break;			
		case 2 :
			//Buscando Contactos
			document.FormSearchGeneric.optsearchby.value='contname';
			break;

	}
	document.FormSearchGeneric.action=url;
	document.FormSearchGeneric.submit(); 	
	
	/*if (document.FormSearchGeneric.optCategory.selectedIndex==0)	
		url ='/SE/MakeQueryActivities.asp';    //url ='/rc/search/MakeQueryActivities.asp';
	else
		url ='/SE/Results.asp'; 

	document.FormSearchGeneric.action=url;
	document.FormSearchGeneric.submit(); */
		
		
	//if (document.FormSearchGeneric.optCategory.selectedIndex==3)	
		//url ='/rc/ie/companies.asp'; // && '?hscode=' &&  (Trim(document.FormSearchGeneric.keyword.value));    /*url ='/rc/search/MakeQueryBuzz.asp';*/
		

} 

//**********************************************************************************************************/
//*	Salta a una direccion URL
//**********************************************************************************************************/

function SaltarAPagina(pag) {
	location.href = pag;
}

/*===============================================================
Retorna a la pag. QueryActivities si se cumplen condiciones
=================================================================*/
function RetornaAAspMakeQueryActivities(lang)
{ 

	var strTipoBusqueda;
	var intCodigoPais;
	var strNombreCampo;
	

	if ((Trim(document.FormSearchActivities.keywords.value)=="") || (document.FormSearchActivities.keywords.value.length==0))
	{
		if (lang=="esp") {
			alert ('Debe ingresar al menos una palabra a buscar.');
			return;
		}
		if (lang=="por") {
			alert ('Voce deve inserir no mínimo uma palavra chave para efetuar a busca.');
			return;
		}
		if (lang!="esp" && lang!="por") {
			alert ('You must insert at least one keyword to search.');
			return;
		}				
	}	
	
	
	
	/*if ((Trim(document.FormSearchCompanies.keyword.value)=="") || (document.FormSearchCompanies.keyword.value.length==0))
	{
		alert ('Debe ingresar al menos una palabra a buscar');
		return;
	}*/
		
		
//	document.formulario.action="MakeQuery.asp?keyword=" && document.formulario.keyword.value && "&#38;" && "tipobusqueda=" && strTipoBusqueda && "&#38;" && "codigopais=" && intCodigoPais && "&#38;" &&"nombrecampo=" && strNombreCampo && ""; 
//	document.formulario.action="makequerycompanies.asp";
	document.FormSearchActivities.submit(); 
} 