function trims(text){
	var c = '';
	var inicio = -1;
	var fin = -1;
	var str = text;		

	for(var i = 0; i < str.length; i++){  
		c = str.substring(i, i + 1); 
		if (inicio == -1 && c != ' ') inicio = i;     
		if (inicio != -1 && c != ' ') fin = i + 1;  
	}

	if (inicio == -1) text = '';                               
	if (inicio != -1 && fin == -1) text = str.substring(inicio, inicio + 1);
	if (inicio != -1 && fin != -1) text = str.substring(inicio, fin);
		
	return(text);
}                                  

function trim(obj){
	
	var c;
	var inicio = -1;
	var fin = -1;
	var str = obj.value;		

	for(var pos = 0; pos < str.length; pos ++){
		c = str.charAt(pos);

		if (inicio == -1 && c != " ") inicio = pos;
		if (inicio != -1 && c != " ") fin = pos + 1;
	}

	if (inicio == -1) obj.value = "";                               
	if (inicio != -1 && fin == -1) obj.value = str.substring(inicio, inicio + 1);

	if (inicio != -1 && fin != -1) obj.value = str.substring(inicio, fin);
}                                  


function buscador(w,h, pagina){
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	miw = window.open(pagina,'ventanacentrada','width='+ w + ', height=' + h +' , scrollbars=no, left=' + LeftPosition + ', top=' + TopPosition);
	miw.focus();
}


function esFecha(obj, msg)

{
var str = obj.value;
var format=0;
var month=0;
var day=0;
var year=0;
var err=false;
if(str == "")

    {

    alert(msg);
    marcarObj(obj);

    return false;

    }
//   if(str.length<6  || str.length>8)
//    err=true;

if(str.length>=8  && str.length<=10)
    {
    // dd/mm/yy format
    if(str.charAt(2) == "/" && str.charAt(5) == "/" && str.length == 10)
        {
        // Check that day is a number.
        for (var i = 0; i < 2; i++) 
        { var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; }
        // Check that month is a number.
        for (var i = 3; i < 5; i++) 
        { var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; }
        // Check that year is a number.
        for (var i = 6; i < 10; i++) 
        { var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; }
        // Set variables
        if(err==false)
        { month=eval(str.substring(3,5)); day=eval(str.substring(0,2)); year=eval(str.substring(6,str.length)); }
        }
}

// Check that month is between 1 &12.
if(month<=0 || month>=13)
    err=true;

// Check that day is right depending on month.
if( month==2 && ((year/4)==parseInt(year/4)) )
    { if(day<=0 || day>29) err=true; }
if( month==2 && ((year/4)!=parseInt(year/4)) )
    { if(day<=0 || day>28) err=true; }
if( month==4 || month==6 || month==9 || month==11 )
    { if(day<=0 || day>30) err=true; }
if( month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12 )
    { if(day<=0 || day>31) err=true; }

// Check that year is OK
//if(year<=0 || year>99) err=true;

// If anything appears incorrect, display error message.
if(err==true)
    {
    alert(msg);
    marcarObj(obj);
    return false;
    }

//   if(year > 2003 == true) {
//		if (month > 8 == false) {
//		if (day > 11 == false) {
//			  alert('La fecha debe ser la de compra');
//			  marcarObj(obj);
//			  return false;
	//	}
//	}
//   }

// If all is OK, return true.
return true;

}


function completado(obj, msg){
	var a_completar = 0;
	var sin_completar = 0;

	if (typeof(obj.length) != "undefined"){ 
		for(var i = 0; i < obj.length; i ++){
			trim(obj[i]);
			++ a_completar;

			if (obj[i].value == "") ++ sin_completar;
		}

		if (a_completar == sin_completar){
			if (msg != null){ 
				alert(msg);
				obj[0].focus();
			}
			return(false);
		}
	}
	else{		
		trim(obj);

		if (obj.value == ""){
			if (msg != null){
				alert(msg);
				obj.focus();
			}
			return(false);
		}
	}

	return(true);		
}

function esEmail(obj){
	if (link_check(obj) == false){
		marcarObj(obj);
		return(false);
	}
	else{
		return(true);
	}
}

function esIgual(obj, obj2, msg){
	var str = obj.value;
	var str2 = obj2.value;
	
	if (str != str2){
		alert(msg);
		marcarObj(obj2);
		return(false);
	}
	return(true);
}

function esLongMenor(obj, cant, msg){
	var str = obj.value;
	
	if (str.length < cant){
		alert(msg);
		marcarObj(obj);
		return(false);
	}
	return(true);
}

function esCorrecto(obj, msg){
	var str = obj.value;
	
	if ((str <= 90000) | (str >= 450001)) {
		alert(msg);
		marcarObj(obj);
		return(false);
	}
	return(true);
}

function esNumero(obj, msg){
	var  str = obj.value;
	var letra;

	//verifico caracter por caracter si es un valor númerico

	for (var pos = 0; pos < str.length; pos ++){
		letra = str.charAt(pos);

		if (isNaN(letra) || letra == " "){
			if (msg != null){
				alert(msg);
				marcarObj(obj);
			}

			return(false);
		}
	}
		
	return(true);
}

function marcarObj(obj){
	obj.select();
	obj.focus();
}

function trim(obj){
	var c;
            var inicio = -1;
	var fin = -1;
	var str = obj.value;		

	for(var pos = 0; pos < str.length; pos ++){
		c = str.charAt(pos);

		if (inicio == -1 && c != " ") inicio = pos;
		if (inicio != -1 && c != " ") fin = pos + 1;
	}

	if (inicio == -1) obj.value = "";                               
	if (inicio != -1 && fin == -1) obj.value = str.substring(inicio, inicio + 1);

	if (inicio != -1 && fin != -1) obj.value = str.substring(inicio, fin);
}                                  



function jtrim(a){
	var x, y, z, str;

	str = "";
		
	for(z = a.length - 1; z >= 0; z --){	//determinig the end point
		if(a.charAt(z) != " ") break;
	}

	for(y = 0; y <= z; y ++){		//determing the start point
		if(a.charAt(y) != " ") break;
	}

   	for(x = y; x <= z; x ++){         	//inputting in the variable str
		str += a.charAt(x);
     	} 

	return str;
}

function link_check(obj_name){
	return left_test_mail(obj_name);
}

function jchar(sent_email){
	var valid_flag = false;
 	var a_to_z = "abcdefghijklmnopqrstuvwxyz";
 	var ascii_arr = a_to_z.toUpperCase();
	 	
	ascii_arr += a_to_z;
	ascii_arr += ".-_@";
	var loc;
	var last_amp = sent_email.lastIndexOf("@");

   	for(b = 0; b < sent_email.length; b++){
		loc = ascii_arr.indexOf(sent_email.charAt(b));

          		if (loc > -1){
			valid_flag = true;
		}

      		if(sent_email.charAt(b) == " "){		// no spaces in between email address
			alert("No puede tener espacios en la dirección de email.");	
	          		return false; 
		}        
			
              	if (isNaN(sent_email.charAt(b)) == false){			
				if(b >= sent_email.lastIndexOf(".")){	//no numbers allowed for domain extensions
					alert("No puede tener números en las extensiones de dominio.");
              	        			return false;
				}
		                  	else{
					valid_flag=true;
				}
		}
   	    }                   

     	return true;
}

function left_test_mail(obj_name){
	var add = jtrim(obj_name.value);
 	var at = parseInt(add.indexOf("@"));
 	var dot = parseInt(add.indexOf("."));  
	 	
	if(at == -1 || dot == -1){	//@ and . should be present
		alert("La Dirección de e-mail ingresada no es válida.");
  		return false;
	}

 	if(add.lastIndexOf("@") > at){	//only one @ allowed
		alert("La Dirección de e-mail ingresada no es válida.");			
  		return false; 
	}

 	if(add.length <= 5){		//total length should be greater than 5
		alert("La Dirección de e-mail ingresada no es válida.");
  		return false; 
	}

 	if(at > add.lastIndexOf(".")){	//@ should be before last .
		alert("La Dirección de e-mail ingresada no es válida.");
  		return false; 
	}

 	if(at < 1){          //@ should not be the first character  
		alert("La Dirección de e-mail ingresada no es válida.");
  		return false;
	}
	
	if(add.lastIndexOf(".") + 2 >= add.length){	//.should have at least two more characters following it
		alert("La Dirección de e-mail ingresada no es válida.");
  		return false;
	}

 	if(at+ 1== add.lastIndexOf(".")){      		//at least one character between @ and .
		alert("La Dirección de e-mail ingresada no es válida.");
  		return false;
	}

 	if(jchar(add) == false){
  		return false;
	} 

	return true;
}

function getN(str){	// regresa un numero valido
	var n;
	var x;
	var s = "";

	for (x = 0; x < str.length; x ++)			
		if (!isNaN(n = str.charAt(x))) s += n;

		
	return(s);
}
	
function next() {			// objeto1[, objeto2, valor]	los ultimos 2 parametros son opcionales
	var args = next.arguments;		
	var obj1 = args[0];

	if (IE4) obj1.value = getN(obj1.value);		// si es Internet Explorer

	if (NS4){					// si es Netscape
		var err = "No se permiten letras en el Nro. de C.U.I.T.";
		if (!esNumero(obj1, err)) obj1.value = "";
	}

	if (args.length == 3){
		var obj2 = args[1];
		var val = args[2];

		if (obj1.value >= val) obj2.focus();
	}
}

