var req;
var dfecha;

function navigate(month,year) {
        var url = "calendar_interno.php?month="+month+"&year="+year+"&xtra="+Date();
        if(window.XMLHttpRequest) {
                req = new XMLHttpRequest();
        } else if(window.ActiveXObject) {
                req = new ActiveXObject("Microsoft.XMLHTTP");
        }
        req.open("GET", url, true);
        req.onreadystatechange = callback;
        req.send(null);
}

function callback() {        
        obj = document.getElementById("calendar");
        setFade(0);
        
		if(req.readyState == 4) {
                if(req.status == 200) {
                        response = req.responseText;
                        obj.innerHTML = response;
                        fade(0);
                } else {
                        alert("There was a problem retrieving the data:\n" + req.statusText);
                }
        }
}

function fade(amt) {
	if(amt <= 100) {
		setFade(amt);
		amt += 10;
		setTimeout("fade("+amt+")", 5);
    }
}

function setFade(amt) {
	obj = document.getElementById("calendar");
	
	amt = (amt == 100)?99.999:amt;
  
	// IE
	obj.style.filter = "alpha(opacity:"+amt+")";
  
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = amt/100;
  
	// Mozilla and Firefox
	obj.style.MozOpacity = amt/100;
  
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = amt/100;
}
var dfecha="";
var ddoc="";
var dhora="";

function show_citas(fecha){
		//alert(fecha)
		dfecha = fecha;
		document.getElementById('sistema_cita_interno').style.display = "block"
		document.getElementById('contenido_cita').style.display = "block"
		document.getElementById('contenido_fotos').style.display = "block"
}

function cerrar_citas(div){
		document.getElementById(div).style.display = "none"
		document.getElementById('contenido_cita').style.display = "none"
		document.getElementById('avail').style.display = "none"		
		document.getElementById('contenido_fotos').innerHTML = cuerpo_fotos
		document.getElementById('contenido_fotos').style.display = "none"
		for(i=0;i<=maxdiv;i++){
			document.getElementById('texto'+i).className = "cita_text_white"
			document.getElementById('foto'+i).className =  "opacity_foto_selected"
		}
}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    //alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}

function check_addr(form){
	if(! echeck(form.email.value)){
			alert("Email Invalido.")
	}else{
		url_site = "check_addr.php?addr=" + form.email.value + "&doctor="+ ddoc+"&fecha=" + dfecha + "&hora="+ dhora
		//alert(url_site);
		//prompt("aaa",url_site);
		respuesta = ajaxpage3(url_site);
		//alert(respuesta)
		
		if(respuesta == 1){
				show_gracias(4);
		}else{
				show_registry_form(form.email.value,ddoc,dfecha,dhora);
		}
	}
}

function show_gracias(paso){
		gracias_form = "<img src='images/cita_"+ paso +".png' alt='1' width='16' height='16' /> <span class='cita_text_white'>Solicitud Guardada</span><br />"
		gracias_form += "                    <table width='489' border='0' cellspacing='0' cellpadding='0'>"
		gracias_form += "                      <tr>"
		gracias_form += "					                          <td valign='top' class='cita_text_white'>Muchas Gracias, muy pronto recibir&aacute; confirmaci&oacute;n de su cita.</td>"
		gracias_form += "                        </tr>"
		gracias_form += "                    </table>";
		document.getElementById('contenido_fotos').innerHTML = gracias_form
		document.getElementById('avail').style.display = "none";
}

function show_registry_form(email,doctor,fecha,hora){
		gracias_form = "<img src='images/cita_4.png' alt='1' width='16' height='16' /> <span class='cita_text_white'>Por favor registrarse</span><br />"
		gracias_form += "                    <form name='gstryfrm'><table width='489' border='0' cellspacing='2' cellpadding='2'>"
		gracias_form += "						<input type='hidden' name='doctor' value='"+ doctor +"' />"
		gracias_form += "						<input type='hidden' name='fecha' value='"+ fecha +"' />"
		gracias_form += "						<input type='hidden' name='hora' value='"+ hora +"' />"
		gracias_form += "                      <tr>"
		gracias_form += "					     <td valign='top' class='cita_text_white'>Email</td>"
		gracias_form += "					     <td valign='top' class='cita_text_white'><input type='text' name='email' value='"+email+"' /></td>"
		gracias_form += "					     <td valign='top' class='cita_text_white' width='5'>&nbsp;</td>"
		gracias_form += "					     <td valign='top' class='cita_text_white'>Nombre</td>"
		gracias_form += "					     <td valign='top' class='cita_text_white'><input type='text' name='nombre' value='' /></td>"
		gracias_form += "                        </tr>"
		gracias_form += "                      <tr>"
		gracias_form += "					     <td valign='top' class='cita_text_white'>Sexo</td>"
		gracias_form += "					     <td valign='top' class='cita_text_white'><input type='radio' name='sexo' value='0' />M <input type='radio' name='sexo' value='1' />F</td>"
		gracias_form += "					     <td valign='top' class='cita_text_white' width='5'>&nbsp;</td>"
		gracias_form += "					     <td valign='top' class='cita_text_white'>Tel&eacute;fono</td>"
		gracias_form += "					     <td valign='top' class='cita_text_white'><input type='text' name='telefono' value='' /></td>"
		gracias_form += "                        </tr>"
		gracias_form += "                      <tr>"
		gracias_form += "					     <td valign='top' class='cita_text_white'>Direcci&oacute;n</td>"
		gracias_form += "					     <td valign='top' class='cita_text_white' colspan=4><input type='text' name='direccion' value='' size='62' /></td>"
		gracias_form += "                        </tr>"
		gracias_form += "                      <tr>"
		gracias_form += "                        <td colspan=5 align='center'><a href='javascript:void(0)' onClick='submit_rgstry(document.gstryfrm)'><img src='images/b_continuar.png' alt='Continuar' border='0' /></a></td>"
		gracias_form += "                      </tr>"
		gracias_form += "                    </table></form>";
		document.getElementById('contenido_fotos').innerHTML = gracias_form
}

function submit_rgstry(form){
	if(! echeck(form.email.value)){
		alert("Email no Valido!");
	}else if(form.nombre.value.length < 3){
		alert("Favor escribir un nombre valido.")
	}else{
		url_site = "add_addr.php?addr=" + form.email.value + "&doctor="+ form.doctor.value+"&fecha=" + form.fecha.value + "&hora="+ form.hora.value;
		if(form.sexo[0].checked){
				sexo =  0
		}else{
				sexo = 1
		}
		url_site += "&nombre="+form.nombre.value +"&sexo="+ sexo +"&telefono=" + form.telefono.value + "&direccion="+ form.direccion.value;
		
		//prompt("aaa",url_site);
		respuesta = ajaxpage3(url_site);
		//alert(respuesta)
		if(respuesta == 1){
				show_gracias(5);
		}else{
				alert("Ha ocurrido un error por favor intente luego!.")
		}
	}
}

function show_avail(doctor,iddiv){
		//alert(doctor + "-" + dfecha)
		url_site = "show_avail.php?fecha=" + dfecha +"&doctor="+doctor;
		//prompt("xxx",url_site);
		texto = ajaxpage3(url_site);
		
		document.getElementById('avail').style.display = "block"
		document.getElementById('avail').innerHTML = texto
		for(i=0;i<maxdiv;i++){
				if(iddiv == i){
						document.getElementById('texto'+i).className = "cita_text_white"
						document.getElementById('foto'+i).className =  "opacity_foto_selected"
				}else{
						document.getElementById('texto'+i).className = "cita_text_white_select"
						document.getElementById('foto'+i).className =  "opacity_foto"
				}
		}
}

function reserve_fecha(form){
		dfecha=form.fecha.value;
		ddoc=form.doctor.value;
		checked = "";
		for (var i=0; i<form.hora_disp.length; i++) {
			if (form.hora_disp[i].checked) {
				checked = form.hora_disp[i].value
			}
		}
		if(checked != ""){
			dhora=checked;
			//alert(dhora)
			document.getElementById('contenido_fotos').innerHTML = correo_form
		}else{
			alert("Selecione una hora para la cita!");
		}
		document.getElementById('avail').style.display = "none";
}

function ajaxpage3(url) {

    var http_request = false;

	url = url + "&to=" + Date()
	

        if (document.all)
   {
      // IE version
      try {
      var xml = new ActiveXObject("Microsoft.XMLHTTP");
      xml.Open( "GET", url, false);
      xml.Send()
      
      } catch (e) {
      var xml = new ActiveXObject("MSXML2.XMLHTTP.4.0");
      xml.Open( "GET", url, false);
      xml.Send()
     
      }
   }
   else
   {
      // Mozilla/Netscrap 6+ version
            var xml=new XMLHttpRequest();
            xml.open("GET",url,false);
            xml.send(null);
   }

	return xml.responseText;

}

function escribir_fecha(){
		//document.write dfecha + "88";
}