var Dims, Dimsx, Dimsy, shiftx, shifty, manX, manY;
var eloldtemp, Hothertemp, desttemp;
var temp, temp2, temp3;
var DaysinM, intD, DispYears;
var PrintDay, DispDays, DayDelay;
var MonthLengths, MonthNames, Gotbyuser;
var givenMonth, givenYear, givenDay;
var curMonth, curYear, curDay;
var nowMonth, nowYear, nowDay;
var givenDate, datenow;
var futureMax, pastMax;
var XMLOutput, PrevMDays, NextMDays;
var DebugOut;

    
function RunDateTool(){    

    DispDays = "";
    DayDelay = 1;
    PrintDay = 1;
    /*
    MonthLengths = new Array();
	MonthLengths[0] = 31;    MonthLengths[1] = 28;    MonthLengths[2] = 31;
    MonthLengths[3] = 30;    MonthLengths[4] = 31;    MonthLengths[5] = 30;
    MonthLengths[6] = 31;    MonthLengths[7] = 31;    MonthLengths[8] = 30;
    MonthLengths[9] = 31;    MonthLengths[10] = 30;    MonthLengths[11] = 31;
   */
   
    MonthNames = new Array("JanButton","FebButton","MarButton","AprButton","MayButton","JunButton","JulButton","AugButton","SepButton","OctButton","NovButton","DecButton");   
   
    datenow = new Date();
    nowDay = datenow.getDate();
    nowMonth = datenow.getMonth() + 1;
    nowYear = datenow.getFullYear();
    
    futureMax = 11;
    pastMax = 3;

    //default to now
    givenDay = nowDay;
    givenMonth = nowMonth;
    givenYear = nowYear;

    //check to see if form was pre-filled
    if( $("SelMonth").present() ){
        givenMonth = Number($F("SelMonth")); 
    }else{
        document.getElementById("SelMonth").value = givenMonth;
    }

    if( $("SelYear").present() ){
        givenYear = Number($F("SelYear"));
    }else{
        document.getElementById("SelYear").value = nowYear;
    }
    
    
    document.getElementById("CalMonthDisp").className = MonthNames[givenMonth-1];

    DrawCalander();
    PrevMonthDis();
    NextMonthDis();
 
}

function DrawCalander(){
    DispDays = "";
    DayDelay = 1;
    PrintDay = 1;
    
    curMonth = Number($F("SelMonth"));
    curYear = Number($F("SelYear"));
    
    datenow = new Date(curYear,curMonth ,1);
    datenow = new Date(datenow.getFullYear(),datenow.getMonth(),datenow.getDate() - 1);
	DaysinM = datenow.getDate();
	
	datenow = new Date(curYear,curMonth - 1,1);
    datenow = new Date(datenow.getFullYear(),datenow.getMonth(),datenow.getDate() - 1);
	PrevMDays = datenow.getDate();
 
    NextMDays = 1;
    
    
    DayDelay = DelayCalc(curMonth ,curYear);
    
    for(intD = 0; intD < 42; intD++){
        if(intD == 0 || intD == 7 || intD == 14 || intD == 21 || intD == 28 || intD == 35){
            DispDays = DispDays + "<div class='Cal-Row'>";
        }
        if(DayDelay > 0){
                temp = PrevMDays - DayDelay + 1;
                DispDays = DispDays + "<div id='Box" + intD + "' class='Cal-Blank' ><a href='javascript:GoPrev(" + temp + ")' onmouseover='BorderOn(\"Box" + intD + "\")' onmouseout='BorderOff(\"Box" + intD + "\")'>" + temp + "</a></div>";
                //DispDays = DispDays + "<div class='Cal-Blank'>&nbsp;</div>";
                DayDelay = DayDelay - 1;
        }
        else{
            if(PrintDay <= DaysinM){
				if(PrintDay === givenDay && nowMonth === curMonth && nowYear === curYear){
					DispDays = DispDays + "<div id='Box" + intD + "' class='Cal-Today' ><a href='javascript:GoTo(" + PrintDay + ")' >" + PrintDay + "</a></div>";
				}
				else{
					DispDays = DispDays + "<div id='Box" + intD + "' class='Cal-Day' ><a href='javascript:GoTo(" + PrintDay + ")' onmouseover='BorderOn(\"Box" + intD + "\")' onmouseout='BorderOff(\"Box" + intD + "\")'>" + PrintDay + "</a></div>";
                }
                PrintDay = PrintDay + 1;
                
            }
            else{
                DispDays = DispDays + "<div id='Box" + intD + "' class='Cal-Blank' ><a href='javascript:GoNext(" + NextMDays + ")' onmouseover='BorderOn(\"Box" + intD + "\")' onmouseout='BorderOff(\"Box" + intD + "\")'>" + NextMDays + "</a></div>";
                //DispDays = DispDays + "<div class='Cal-Blank'>&nbsp;</div>";
                NextMDays++;
            }
        }
        if(intD == 6 || intD == 13 || intD == 20 || intD == 27 || intD == 34 || intD == 41){
            DispDays = DispDays + "</div>";
            //if(PrintDay > DaysinM){break;}
        }
        
    }
    document.getElementById("DaysMain").innerHTML = DispDays;
    
    GetXML();
}

function GetXML(){    
	var CalZip;
	CalZip = $F("CalZip");
    temp = $F("SelMonth");
    temp = temp + "/1";
    temp = temp + "/" + $F("SelYear");
    var RequestURL = "/asp/xml/HomeCalendarXML.asp?dt=" + temp + "&zip=" + CalZip;
    var wRequest = new Sys.Net.WebRequest();
    wRequest.set_url(RequestURL);
    wRequest.set_httpVerb("GET");
    
    wRequest.add_completed(RunGrab);
    
    var executor = wRequest.get_executor(); 

    wRequest.invoke(); 
}

function RunGrab(executor){
    if(executor.get_responseAvailable()){
        XMLOutput = executor.get_xml();
    }

    temp = Number($F("SelMonth"));
    temp2 = Number($F("SelYear"));
    DayDelay = DelayCalc(temp,temp2);
    
    var j = Number($F("CalendarCat"));
    var XOut = XMLOutput.getElementsByTagName('Day');

    for(i = 0; i < XOut.length; i++){
    
        if(XOut.item(i).attributes[j].value == 1 || j == 0){
            temp = XOut.item(i).attributes[0].value;
            temp = temp - 1 + DayDelay;
            temp = "Box" + temp;
            document.getElementById(temp).style.backgroundColor = "#99ccff"; 
        }
    }
}

  
function GoTo(day){
    var month = $F("SelMonth");
    var year = $F("SelYear");
    var zip = $F("CalZip");
    var catagoryID = $F("CalendarCat");

    temp = "../asp/webcast-auctions-calendar.asp?sd=" 
            + month + "/" + day + "/" + year
            + "&m="
            + month 
            + "&y="
            + year
            + "&z="
            + zip
            + "&cat="
            + catagoryID
            ;
//    alert(temp);
    window.location = temp;
}
function GoPrev(day){
    var month = Number($F("SelMonth"));
    var year = Number($F("SelYear"));
    var zip = $F("CalZip");
    var catagoryID = $F("CalendarCat");
    if(month < 2){
        month = 12;
        year = year - 1;
    }else{
        month = month - 1;
    }

    temp = "../asp/webcast-auctions-calendar.asp?sd=" 
            + month + "/" + day + "/" + year
            + "&m="
            + month 
            + "&y="
            + year
            + "&z="
            + zip
            + "&cat="
            + catagoryID
            ;
    //alert(temp);
    window.location = temp;
}
function GoNext(day){
    var month = Number($F("SelMonth"));
    var year = Number($F("SelYear"));
    var zip = $F("CalZip");
    var catagoryID = $F("CalendarCat");
 
    if(month > 11){ 
        month = 1;
        year = year + 1; 
    }else{ 
        month = month + 1; 
    }
    
    temp = "../asp/webcast-auctions-calendar.asp?sd=" 
            + month + "/" + day + "/" + year
            + "&m="
            + month 
            + "&y="
            + year
            + "&z="
            + zip
            + "&cat="
            + catagoryID
            ;
    //alert(temp);
    window.location = temp;
} 

function CalZipSort(){
    DrawCalander();
}


function HighlightChange(){
    DrawCalander();
}


function BorderOn(inpt1){
    document.getElementById(inpt1).style.border = "solid 1px #ccc";
}

function BorderOff(inpt1){
    document.getElementById(inpt1).style.border = "solid 1px #999";
}

    
function PrevMonth(){
    var month = Number($F("SelMonth"));
    
    if(pastMax > 0){
        pastMax--;
        futureMax++;
        month = month - 1;
        
        if(month == 0){
            document.getElementById("CalMonthDisp").className = MonthNames[11];
            document.getElementById("SelMonth").value = 12;
            PrevYear();
        }else{
            document.getElementById("CalMonthDisp").className = MonthNames[month-1];
            document.getElementById("SelMonth").value = month;
        }
        
        PrevMonthDis();
        NextMonthDis();
        DrawCalander();
    }
}
function NextMonth(){
    var month = Number($F("SelMonth"));
    
    if(futureMax > 0){
        pastMax++;
        futureMax--;
        month = month + 1;
        
        if(month > 12){
            document.getElementById("CalMonthDisp").className = MonthNames[0];
            document.getElementById("SelMonth").value = 1;
            NextYear();
        }else{
            document.getElementById("CalMonthDisp").className = MonthNames[month-1];
            document.getElementById("SelMonth").value = month;
        }
        
        PrevMonthDis();
        NextMonthDis();
        DrawCalander();
    }
}


function PrevYear(){
    temp = document.getElementById("SelYear").value;
    temp = temp - 0;
    temp = temp - 1;

    document.getElementById("SelYear").value = temp;
    //document.getElementById("dispYear").innerHTML = temp;

    PrevMonthDis();
    NextMonthDis();
    //DrawCalander();
}
function NextYear(){
    temp = document.getElementById("SelYear").value;
    temp = temp - 0;
    temp = temp + 1;
    
    document.getElementById("SelYear").value = temp;
    //document.getElementById("dispYear").innerHTML = temp;
    
    PrevMonthDis();
    NextMonthDis();
    //DrawCalander();
}



function PrevMonthDis(){
    if(pastMax <= 0){
        document.getElementById("PrevMonthButton").innerHTML = '<span style="cursor:default;">&nbsp;</span>'
        document.getElementById("PrevMonthButton").style.width="14px" 
        document.getElementById("PrevMonthButton").style.height="22px" 
    } 
    else{
        document.getElementById("PrevMonthButton").innerHTML = '<a href="javascript:PrevMonth();" class="clickable"></a>'
        document.getElementById("PrevMonthButton").style.width="14px" 
        document.getElementById("PrevMonthButton").style.height="22px" 
    }
}
function NextMonthDis(){
    if(futureMax <= 0){
        document.getElementById("NextMonthButton").innerHTML = '<span style="cursor:default;">&nbsp;</span>';
        document.getElementById("NextMonthButton").style.width="14px" 
        document.getElementById("NextMonthButton").style.height="22px" 
    }
    else{
        document.getElementById("NextMonthButton").innerHTML = '<a href="javascript:NextMonth();" class="clickable"></a>';
        document.getElementById("NextMonthButton").style.width="14px" 
        document.getElementById("NextMonthButton").style.height="22px" 
    }
}

function DelayCalc(inpt1,inpt2){
    var l = Math.floor((14 - inpt1)/12);
    var m = inpt2 - l;
    var n = inpt1 + 12*l - 2;
    var o = (1 + m + Math.floor(m/4) - Math.floor(m/100) + Math.floor(m/400) + Math.floor((31*n)/12)) % 7;
    return o;
}

