function nuevoAjax(){
	var xmlhttp=false;
 	try {
 		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 	} catch (e) {
 		try {
 			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 		} catch (E) {
 			xmlhttp = false;
 		}
  	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
 		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

function cargarSubtipo(id)
{
var mod=0;
ajax=nuevoAjax();

ajax.open("GET", "/ajaxtipo.php?id="+id, true);
ajax.onreadystatechange=function() 
{ 
if (ajax.readyState==4)
{ 
document.getElementById("subtipo").innerHTML=ajax.responseText;
} 
}
ajax.send(null);    
}
function enviarEmail(returnArray){
	var contenedor 	= document.getElementById('message');
	var sent="";
	for (var i = 0; i < returnArray.length; i++) {
		var field = returnArray[i].split(":");
		var objName = field[0];
		var objValue = document.getElementById(field[0]);
		var strValidate = field[1];
		var error = field[2];
		if (validar(strValidate,objValue,error,contenedor)){
		  sent=sent+"&"+objName+"="+objValue.value;
		}else{
		  return false;
		}
	}
	
	ajax=nuevoAjax();
	ajax.open("POST", "enviar.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			contenedor.innerHTML = ajax.responseText;
	 	}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send(sent);
}

function validar(strValidateStr,objValue,strError,contenedor) 
{ 
    
    var epos = strValidateStr.search("="); 
    var  command  = ""; 
    var  cmdvalue = ""; 
    if(epos >= 0) 
    { 
     command  = strValidateStr.substring(0,epos); 
     cmdvalue = strValidateStr.substr(epos+1); 
    } 
    else 
    { 
     command = strValidateStr; 
    } 
    switch(command) 
    {
        case "no": 
         { 
           return true; 
           break;             
         }//case required  
        case "req": 
        case "required": 
         { 
           if(eval(objValue.value.length) == 0) 
           { 
              if(!strError || strError.length ==0) 
              { 
                strError = objValue.name + " : Required Field"; 
              }//if 
              contenedor.innerHTML = strError; 
	      objValue.focus();
              return false; 
           }//if 
           break;             
         }//case required 

        case "maxlength": 
        case "maxlen": 
          { 
             if(eval(objValue.value.length) >  eval(cmdvalue)) 
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = objValue.name + " : "+cmdvalue+" characters maximum "; 
               }//if 
               contenedor.innerHTML = strError + "\n[Current length = " + objValue.value.length + " ]"; 
	      objValue.focus(); 
               return false; 
             }//if 
             break; 
          }//case maxlen 
        case "minlength": 
        case "minlen": 
           { 
             if(eval(objValue.value.length) <  eval(cmdvalue)) 
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = objValue.name + " : " + cmdvalue + " characters minimum  "; 
               }//if               
               contenedor.innerHTML = strError + "\n[Current length = " + objValue.value.length + " ]"; 
	      objValue.focus(); 
               return false;                 
             }//if 
             break; 
            }//case minlen 
        case "alnum": 
        case "alphanumeric": 
           { 
              var charpos = objValue.value.search("[^A-Za-z0-9]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
               if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name+": Only alpha-numeric characters allowed "; 
                }//if 
                contenedor.innerHTML = strError + "\n [Error character position " + eval(charpos+1)+"]"; 
	      objValue.focus(); 
                return false; 
              }//if 
              break; 
           }//case alphanumeric 
        case "num": 
        case "numeric": 
           { 
              var charpos = objValue.value.search("[^0-9]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name+": Only digits allowed "; 
                }//if               
                contenedor.innerHTML = strError + "\n [Error character position " + eval(charpos+1)+"]"; 
	      objValue.focus(); 
                return false; 
              }//if 
              break;               
           }//numeric 
        case "alphabetic": 
        case "alpha": 
           { 
              var charpos = objValue.value.search("[^A-Za-z]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                  if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name+": Only alphabetic characters allowed "; 
                }//if                             
                contenedor.innerHTML = strError + "\n [Error character position " + eval(charpos+1)+"]"; 
	      objValue.focus();
                return false; 
              }//if 
              break; 
           }//alpha 
	case "alnumhyphen":
			{
              var charpos = objValue.value.search("[^A-Za-z0-9\-_]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                  if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name+": characters allowed are A-Z,a-z,0-9,- and _"; 
                }//if                             
                contenedor.innerHTML = strError + "\n [Error character position " + eval(charpos+1)+"]"; 
	      objValue.focus(); 
                return false; 
              }//if 			
	      break;
	}
        case "email": 
          { 
               if(!validateEmailv2(objValue.value)) 
               { 
                 if(!strError || strError.length ==0) 
                 { 
                    strError = objValue.name+": Enter a valid Email address "; 
                 }//if                                               
                 contenedor.innerHTML = strError; 
		  objValue.focus(); 
                 return false; 
               }//if 
           break; 
          }//case email 
        case "url": 
          { 
               if(!validateURL(objValue.value)) 
               { 
                 if(!strError || strError.length ==0) 
                 { 
                    strError = objValue.name+": Ingrese una URL valida(http://www.) "; 
                 }//if                                               
                 contenedor.innerHTML = strError; 
	      objValue.focus(); 
                 return false; 
               }//if 
           break; 
          }//case email 		  
        case "lt": 
        case "lessthan": 
         { 
            if(isNaN(objValue.value)) 
            { 
              alert(objValue.name+": Should be a number "); 
              return false; 
            }//if 
            if(eval(objValue.value) >=  eval(cmdvalue)) 
            { 
              if(!strError || strError.length ==0) 
              { 
                strError = objValue.name + " : value should be less than "+ cmdvalue; 
              }//if               
              contenedor.innerHTML = strError; 
	      objValue.focus(); 
              return false;                 
             }//if             
            break; 
         }//case lessthan 
        case "gt": 
        case "greaterthan": 
         { 
            if(isNaN(objValue.value)) 
            { 
              alert(objValue.name+": Should be a number "); 
              return false; 
            }//if 
             if(eval(objValue.value) <=  eval(cmdvalue)) 
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = objValue.name + " : value should be greater than "+ cmdvalue; 
               }//if               
               contenedor.innerHTML = strError; 
	      objValue.focus();
               return false;                 
             }//if             
            break; 
         }//case greaterthan 
        case "regexp": 
         { 
		 	if(objValue.value.length > 0)
			{
	            if(!objValue.value.match(cmdvalue)) 
	            { 
	              if(!strError || strError.length ==0) 
	              { 
	                strError = objValue.name+": Invalid characters found "; 
	              }//if                                                               
	              contenedor.innerHTML = strError; 
	      objValue.focus(); 
	              return false;                   
	            }//if 
			}
           break; 
         }//case regexp 
        case "dontselect": 
         { 
            if(objValue.selectedIndex == null) 
            { 
              //alert("BUG: dontselect command for non-select Item"); 
              return false; 
            } 
            if(objValue.selectedIndex == eval(cmdvalue)) 
            { 
             if(!strError || strError.length ==0) 
              { 
              strError = objValue.name+": Por favor seleccione una opcion "; 
              }//if                                                               
              contenedor.innerHTML = strError; 
	      objValue.focus();
              return false;                                   
             } 
             break; 
         }//case dontselect 
		 case "checkbox":
		 {
		     if(objValue.checked == false){
					contenedor.innerHTML = strError; 
					return false;
			} 
		break;
		}
        case "iguales": 
         { 
            if(objValue.value != document.getElementById(cmdvalue).value) 
            { 
             if(!strError || strError.length ==0) 
              { 
              strError = objValue.name+": Debe ser igual a "+cmdvalue; 
              }//if                                                               
              contenedor.innerHTML = strError; 
	      objValue.focus();
              return false;                                   
             } 
             break; 
         }//case dontselect 		 
    }//switch 
    return true; 
}

function validateEmailv2(email)
{
// a very simple email validation checking. 
// you can add more complex email checking if it helps 
    if(email.length <= 0)
	{
	  return true;
	}
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null) return false;
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) 
      {
	    var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
	    if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
return false;
}

function validateURL(url)
{
// a very simple email validation checking. 
// you can add more complex email checking if it helps 
    if(url.length <= 0)
	{
	  return true;
	}
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(url);

}
