Hyprocure = window.Hyprocure || {};
Hyprocure.webPaths = {relative :'/grahamiii/'};
function getObject(id)
{
	return document.getElementById(id);
}
function getInputEquipo(id){
	return "<input type='text' id='"+id+"' name='"+id+"' class='inputSmallObligatorio'>*";
}
function getList(id,name,pk2,pk3)
{
	getObject('divSelect'+name).style.borderLeft = "1px solid #455969";
	getObject('divSelect'+name).innerHTML = "";
	var url = "AdmAjax.php?accion=getList"+encodeURIComponent(name)+"&pk="+encodeURIComponent(id)+"&pk2="+encodeURIComponent(pk2)+"&pk3="+encodeURIComponent(pk3);
	buscarDatos(url,'desplegarDatosList','divSelect'+name);
	if(name=="Country"){
		getObject('divSelectState').innerHTML = "";
	}
	if(name=="State"||name=="Country"){
		getObject('divSelectCity').innerHTML = "";
	}
	if(name=="City"||name=="State"||name=="Country"){
		getObject('divSelectDoctor').innerHTML = "";
	}
	if(name=="Country"){
		getObject('divSelectState').style.borderLeft = "1px solid #455969";
		getObject('divSelectCity').style.borderLeft = "1px solid #455969";
		getObject('divSelectDoctor').style.borderLeft = "1px solid #455969";
	}
	if(name=="State"){
		getObject('divSelectCity').style.borderLeft = "1px solid #455969";
		getObject('divSelectDoctor').style.borderLeft = "1px solid #455969";
	}
	if(name=="City"){
		getObject('divSelectDoctor').style.borderLeft = "1px solid #455969";
	}
}
function desplegarDatosList(respuesta,id)
{
	getObject(id).style.borderLeft = "1px solid #B5BDC3";
	getObject(id).innerHTML = respuesta;
}
function getComboRegion(swAll)
{
	txtOption = swAll=="si"?"All":"Select";
	var url = "AdmAjax.php?accion=getComboRegion";
	buscarDatos(url,'desplegarDatos','divSelectRegion');
	getObject('divSelectCountry').innerHTML = "<select id='Country' name='Country' class='inputSmallCombo'><option value=''>"+txtOption+"</option></select>";
	getObject('divSelectState').innerHTML = "<select id='State' name='State' class='inputSmallCombo'><option value=''>"+txtOption+"</option></select>";
	getObject('divSelectCity').innerHTML = "<select id='City' name='City' class='inputSmallCombo'><option value=''>"+txtOption+"</option></select>";
	
}
function getComboCountry(obj,swAll)
{
	txtOption = swAll=="si"?"All":"Select";
	if(obj.value!=""){
		var url = "AdmAjax.php?accion=getComboCountry&region="+obj.value;
		buscarDatos(url,'desplegarDatos','divSelectCountry');
		getObject('divSelectState').innerHTML = "<select id='State' name='State' class='inputSmallCombo'><option value=''>"+txtOption+"</option></select>";
		getObject('divSelectCity').innerHTML = "<select id='City' name='City' class='inputSmallCombo'><option value=''>"+txtOption+"</option></select>";
	}else{
		getObject('divSelectCountry').innerHTML = "<select id='Country' name='Country' class='inputSmallCombo'><option value=''>"+txtOption+"</option></select>";
		getObject('divSelectState').innerHTML = "<select id='State' name='State' class='inputSmallCombo'><option value=''>"+txtOption+"</option></select>";
		getObject('divSelectCity').innerHTML = "<select id='City' name='City' class='inputSmallCombo'><option value=''>"+txtOption+"</option></select>";
	}
}

function getComboState(obj,swAll)
{
	txtOption = swAll=="si"?"All":"Select";
	if(obj.value!=""){
		var url = "AdmAjax.php?accion=getComboState&country="+obj.value;
		buscarDatos(url,'desplegarDatos','divSelectState');
		getObject('divSelectCity').innerHTML = "<select id='City' name='City' class='inputSmallCombo'><option value=''>"+txtOption+"</option></select>";
	}else{
		getObject('divSelectState').innerHTML = "<select id='State' name='State' class='inputSmallCombo'><option value=''>"+txtOption+"</option></select>";
		getObject('divSelectCity').innerHTML = "<select id='City' name='City' class='inputSmallCombo'><option value=''>"+txtOption+"</option></select>";
	}
}

function getComboState2(obj,swAll,pre)
{
	txtOption = swAll=="si"?"All":"Select";
	if(obj.value!=""){
		var url = "AdmAjax.php?accion=getComboState2&country="+obj.value;
		buscarDatos(url,'desplegarDatos','divSelectState' + pre);
		getObject('divSelectCity'+ pre).innerHTML = "<select id='City'" + pre + " name='City' class='inputSmallCombo'><option value=''>"+txtOption+"</option></select>";
	}else{
		getObject('divSelectState'+ pre).innerHTML = "<select id='State" + pre + "' name='State' class='inputSmallCombo'><option value=''>"+txtOption+"</option></select>";
		getObject('divSelectCity'+ pre).innerHTML = "<select id='City" + pre + "' name='City' class='inputSmallCombo'><option value=''>"+txtOption+"</option></select>";
	}
}

function getComboCity(obj,swAll)
{
	if(obj.value!=""){
		var url = "AdmAjax.php?accion=getComboCity&state="+obj.value;
		buscarDatos(url,'desplegarDatos','divSelectCity');
	}else{
		txtOption = swAll=="si"?"All":"Select";
		getObject('divSelectCity').innerHTML = "<select id='City' name='City' class='inputSmallCombo'><option value=''>"+txtOption+"</option></select>";
	}
}

function getComboCity2(obj,swAll,pre)
{
	if(obj.value!=""){
		var url = "AdmAjax.php?accion=getComboCity2&state="+obj.value;
		buscarDatos(url,'desplegarDatos','divSelectCity' + pre );
	}else{
		txtOption = swAll=="si"?"All":"Select";
		getObject('divSelectCity' + pre).innerHTML = "<select id='City" + pre + "' name='City' class='inputSmallCombo'><option value=''>"+txtOption+"</option></select>";
	}
}


function getComboCertification()
{
	var url = "AdmAjax.php?accion=getComboCertification";
	buscarDatos(url,'desplegarDatos','divSelectCertification');
}
function verOcultarDivs(idDiv1,idDiv2,idInput,tipo)
{
	if(tipo=='Input'){
		getObject(idDiv1).innerHTML = getInputEquipo(idInput);
		getObject(idDiv2).innerHTML = "";//idInput=="Appareil"?getComboAppareil('Select'):(idInput=="Marque"?getComboMarque2('Select'):getComboType2('Select'));
	}else{
		getObject(idDiv2).innerHTML = getInputEquipo('Input'+idInput);
		getObject(idDiv1).innerHTML = idInput=="Certification"?getComboCertification():(idInput=="Region"?getComboRegion("no"):(idInput=="Country"?getComboCountry(getObject('Region'),'no'):(idInput=="State"?getComboState(getObject('Country'),'no'):getComboCity(getObject('State'),'no'))));
	}
	getObject(idDiv1).style.display='block';
	getObject(idDiv2).style.display='none';
}

function cambiarOpcionesSelect(idSelect, opciones)
{
	    var select=document.getElementById(idSelect);
	    if (!select) return false;
	    if (select.selectedIndex < 0) var seleccionado='';
	    else  var seleccionado=select.options[select.selectedIndex].value;	    
	    for (i=select.options.length-1; i >=0 ; i--)
	        select.options[i]=null;         	        
	    if (typeof(opciones)=='string') {
	    	   select.innerHTML=opciones;	    	   
	    	   return true;
	    }
	    if (typeof(opciones)=='object') {
	    	   var con=0;
	    	   if (arguments[2]) {
	    	   	   var aux=document.createElement('option');
	    	   	   var txt=document.createTextNode(arguments[2]);
	    	   	   aux.appendChild(txt);	    	   	   
	    	   	   aux.setAttribute('value','');
	    	   	   select.appendChild(aux);	    	
	    	       con++; 
	    	   }	    	   
	    	   if (isNaN(opciones.length)) {	
	    	       for (opcion in opciones) {
	    	       	   var aux=document.createElement('option');
	    	   	       var txt=document.createTextNode(opciones[opcion]);
	    	   	       aux.appendChild(txt);	    	   	   
	    	   	       aux.setAttribute('value',opcion);
	    	   	       if (opcion==seleccionado)aux.setAttribute('selected',true);
	    	   	       select.appendChild(aux);	    	   	       
                 con++;
	    	       }	    
	    	   }
	    	   else {
	    	   	   for(var i=0;i<opciones.length;i++) {
	    	   	   	   var aux=document.createElement('option');
	    	   	       var txt=document.createTextNode(opciones[i].text);
	    	   	       aux.appendChild(txt);
	    	   	       aux.setAttribute('value',opciones[i].value);
	    	   	       if (opcion==seleccionado)aux.setAttribute('selected',true);
	    	   	       select.appendChild(aux);	    	   	 
	    	   	   	   con++;	    	   	       
	    	       }
	    	   }		    	   	    	   
	    	   return true;
	    }
	    return null;
}


function cambiarStyle(id,clase)
{
	elements = document.getElementsByClassName(clase);
	var i=0;
	for(i;i<elements.length;i++){
		if(elements[i]){
			elements[i].className=clase+" disabledHref";
		}
	}
	document.getElementById(id).className=clase+" enabledHref";
}
/**
set the input type checkbox checked or not checked 
@param 1 value for attribute 
@param 2 state of checkbox 
@param 3 attibute for comparation 
*/
function checkAllCheckbox()
{
	   type=(arguments[0])?arguments[0]:'forSelect';
	   chk=(arguments[1])?true:false;
	   attr=(arguments[2])?arguments[2]:'name';
	   elements = document.getElementsByTagName('input');	
	   for(var i=0;i<elements.length;i++){	   	   
		      elemattr=(attr.toLowerCase()=='class')?elements[i].className:elements[i].getAttribute(attr);
		      elemattr=(elemattr)?elemattr.toLowerCase():elemattr;
		      ischeckbox=(elements[i].getAttribute('type'))?elements[i].getAttribute('type').toLowerCase():'';
		      if(ischeckbox=='checkbox' && elemattr==type.toLowerCase()) {
			         elements[i].checked=chk;
		      }
	   }	
}
/**

@param 1
@param 2
@param 3
*/
function getAllCheckbox()
{
	   var type=(arguments[0])?arguments[0]:'forSelect';	   
	   var attr=(arguments[1])?arguments[1]:'name';
	   var elements = document.getElementsByTagName('input');
	   var retu='';
	   for(var i=0;i<elements.length;i++) {
		      elemattr=(attr.toLowerCase()=='class')?elements[i].className:elements[i].getAttribute(attr);
		      elemattr=(elemattr)?elemattr.toLowerCase():elemattr;
		      ischeckbox=(elements[i].getAttribute('type'))?elements[i].getAttribute('type').toLowerCase():'';		      
		      if(ischeckbox=='checkbox' && elemattr==type.toLowerCase() && elements[i].checked==true) {
			         retu = retu + elements[i].value + '*'; 
		      }
	   }
	   return retu;	
}

/* objeto Calvi */    
Calvi = window.Calvi || {debug:true};

//adicionar los elementos que faltan los que no se pueden cambiar son los siguientes(EnteroP,fecha,mail) los demas se puede cambiar
Calvi.validarEr={numeral:  /^\d+$/
	  ,entero: /^(?:\+|-)?\d+$/ 
   ,natural: /^\+?\d+$/
   ,enteroP: /^\+?[1-9]+\d*$/
   ,mail: /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/
   ,noVacio:/\S+/  
   ,real: /(?:\+|-)?[0-9]+\.?[0-9]*/
   ,fecha: /^(?:0?([1-9])|(?:[12][0-9])|(?:3[01]))\/(?:0?(?:[1-9])|(?:[1][0-2]))\/(?:19|20)[0-9]{2}$/
   ,fechaI: /^(?:0?(?:[1-9])|(?:[1][0-2]))\/(?:0?([1-9])|(?:[12][0-9])|(?:3[01]))\/(?:19|20)[0-9]{2}$/
   ,horaMinuto: /^(?:(?:[01]?[0-9])|2[0-3]):(?:[0-5]?[0-9])$/
   ,fechaHora: /^(0?([1-9])|([12][0-9])|(3[01]))\/(0?([1-9])|([1][0-2]))\/(19|20)[0-9]{2} /
   ,mailr: /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
   ,web:  /^(http|ftp)(s)?:\/\/[a-zA-Z0-9-_:/.?&amp;=]+$/
   ,web:  /^((http|ftp)(s)?:\/\/)?[a-zA-Z0-9-_:/.?&amp;=]+$/  };
/**
 argumentos [0]=texto,[1]=expresion regular,[2]=mensaje que se mostrara si no cumple la condicion */
Calvi.validar=function() {
	 this.ers=Calvi.validarEr;
	 numParametros=arguments.length;
	 if (numParametros<1)return false;
  text=arguments[0];
  var er=(numParametros>1)?arguments[1]:'noVacio';
  if(!this.ers[er] && Calvi.debug)  alert('Regular Expresion not found!!!');
  er=(this.ers[er])?this.ers[er]:this.ers.noVacio;  
  if (arguments[2] && !er.test(text))
     alert (arguments[2]);
  return (er.test(text));  
};  