﻿function sleep(delay) 
    { 
        var start = new Date().getTime(); 
        while (new Date().getTime() < start + delay); 
    } 
    
    /* create an array of days which need to be disabled */
    var disabledDays = null;
    var indicador = -1;

    function ajx()
	{
		var xmlhttp;
		if (window.XMLHttpRequest)
		  {// code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else
		  {// code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		return xmlhttp;
	}
	
	function  getDisabledDays(mes, dia, ano){
	    //INICIA aJAX		
		//crear objeto Ajax	
			var jx=ajx();			
			//firebug:s		
            if (typeof(jx.onprogress)=='object')
            { //if firebug bug:
                jx.onload = jx.onerror = jx.onabort = function(){
                                                                f=jx.responseText;	
				                                                };
            } 
            else 
            {//normal:other browsers
                jx.onreadystatechange = function(){
                                                    if (jx.readyState == 4)
                                                    {
                                                    f=jx.responseText;
                                                    }
                                                   };
            }
			//firebug:e	
			var rnd=Math.random();
			jx.open("POST","/handlers/addToCart/serviceExcludedDates.ashx",false);
			//jx.open("GET","/testingCutsomToolkit/excludedDates.ashx?rnd="+rnd+"&m="+m+"&a="+a+"&i="+i+"&t="+t,false);
			jx.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			jx.send("m="+(mes+1)+"&a="+ano+"&id="+jQuery('#hdnIdServicio').val());		
		//TERMINA AJAX
		disabledDays = f.split(',');
        indicador = mes;
	}
    var c = 1; //variable que sirve para validar que la peticion se haga solo para el mes que se muestra
    /* utility functions */
    function closedDays(date) {
        if (date.getMonth() != indicador && c > 1) {
            c = 1;
        }
        //console.log('c:' + c + ',date:' + date.getDate());
        if (c == date.getDate()) {
            c++;
            //console.log('indicador:' + indicador);
            if (indicador != date.getMonth()){
                getDisabledDays(date.getMonth(),date.getDate(),date.getFullYear());
            }
            while(indicador != date.getMonth()){sleep(200);}
            //console.log('paso 3');
            var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
	        //console.log('Checking (raw): ' + (m+1) + '-' + d + '-' + y);
	        for (i = 0; i < disabledDays.length; i++) {
	            //console.log(disabledDays[i]);
		        if(jQuery.inArray((m+1) + '-' + d + '-' + y,disabledDays) != -1 || new Date().getTime() + 1*24*60*60*1000 >= date) {
			        //console.log('bad:  ' + (m+1) + '-' + d + '-' + y + ' / ' + disabledDays[i]);
			        return [false];
                }
            }
	        //console.log('good:  ' + (m+1) + '-' + d + '-' + y);
	        return [true];  
        }else{
            c = 1;
            return [false];  
        }
    }
    function noWeekendsOrHolidays(date) {
	    var noWeekend = jQuery.datepicker.noWeekends(date);
	    return noWeekend[0] ? closedDays(date) : noWeekend;
    }
   
    function pageLoad()
    {
        var isAsyncPostback = Sys.WebForms.PageRequestManager.getInstance().                           get_isInAsyncPostBack();
        if (isAsyncPostback) {
            var idFecha = jQuery('#hdnIdFecha').val();
            jQuery("#"+idFecha).datepicker({
                minDate: -1,
                maxDate: '+24M',
                changeMonth: true,
                changeYear: true,
                constrainInput:  true,
                beforeShowDay:  closedDays
            });
        }
    }      