var ImagesPath = null;
var cDate = new Date();cDate.setFullYear(cDate.getFullYear(), cDate.getMonth(), cDate.getDate());
var SelectedDay = new Date();
var NewDate=false;
var FullMonthName = false;
var FullDayName = false;
var SelectedField = null;
var TextButtons = true;
var CloseButton;
var Dates = [];
//Months names
var Months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
var months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
//Days Names
var DaysNames = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
var daysNames = ["Mo","Tu","We","Thu","Fr","Sa","Su"];
var aDays;
var HideCalendar = false;
var Cal;
var CalContent;
var Blocker;

function initCal()
{
    Blocker = document.getElementById("frmBlocker");
    Cal = document.getElementById("divCalendar");
    CalContent = document.getElementById("CalCont");
    Cal.onmouseover = function(){HideCalendar=false;}
    Cal.onmouseout = function(){if(Cal.style.visibility!="hidden")HideCalendar=true;}
    document.onclick = function(){if(HideCalendar){HideCal();}}
}
function SetDate(counter)
{
    SetNewDate(Dates[counter]);
    HideCal();
}
function SetDefault()
{
    var Today = new Date();Today.setHours(2,0,0,0);Today.setTime(Today.getTime()+(60*60*24*1000));
    SelectedField.value = Today.getDate()+"/"+(Today.getMonth()+1)+"/"+Today.getFullYear();
    CheckFields();
    HideCal();
}
//function ShowCal(cont,date)
//{
//    if(Cal==undefined)initCal();
//    SelectedField = GetElementById(cont);
//    if(date==null)
//        date = SelectedField.value;
//    if((GetElementById("DepDay")!=null)&&(cont.toString().toLowerCase()=="fromdate"))
//        cont = GetElementById("DepDay");
//    if((GetElementById("RetDay")!=null)&&(cont.toString().toLowerCase()=="todate"))
//        cont = GetElementById("RetDay");

//    var pos = findPos(cont);
//    var cMonth = false;
//    var StartDate = new Date();
//    var Today = new Date();
//    Today.setHours(0,0,0,0);
//    var Header = "";
//    if(Cal.getAttribute("Header")!=null)
//        Header = Cal.getAttribute("Header");
//    if(Cal.getAttribute("TextButtons")!=null)
//        TextButtons = Cal.getAttribute("TextButtons").toLowerCase()=="true";
//    if(Cal.getAttribute("FullDayName")!=null)
//        FullDayName = Cal.getAttribute("FullDayName").toLowerCase()=="true";
//    if(Cal.getAttribute("FullMonthName")!=null)
//        FullMonthName = Cal.getAttribute("FullMonthName").toLowerCase()=="true";
//    Dates = new Array();
//    var counter = 0;
//    if(TextButtons)
//        CloseButton = "x";
//    else
//        CloseButton = "<img title=\"Close Calendar\" border=\"0\" id=\"btn_close\" src=\""+ImagesPath+"btn_close_out.gif\" onmouseout=\"MouseOut(this)\" onmouseover=\"MouseOver(this)\" />";
//    if(date != null)
//    {
//        if(date.toString().indexOf('/')>0)
//        {
//            cDate.setFullYear(date.split('/')[2], (parseInt(date.split('/')[1], 10)-1), date.split('/')[0]);
//        }
//        else{
//        if(!isNaN(parseInt(date, 10))){cDate.setTime(parseInt(date, 10));}else{cDate = new Date();}}
//        if(!NewDate)SelectedDay.setFullYear(cDate.getFullYear(), cDate.getMonth(), cDate.getDate());
//    }
//    SelectedDay.setHours(2,0,0,0);
//    StartDate.setFullYear(cDate.getFullYear(), cDate.getMonth(), 1);
//    StartDate.setHours(2,0,0,0);
//    var strCal = "<table class=\"CalTable\" cellpadding=\"0\" cellspacing=\"0\">";
//    // Cal Header
//    strCal += "<tr><td colspan=\"7\"><table style=\"width:100%\" cellpadding=\"0\" cellspacing=\"0\"><td class=\"CalHeader\">" + Header + "</td><td class=\"Close\"><a href=\"javascript: HideCal();\">" + CloseButton + "</a></td></tr></table></td></tr>";
//    // Cal Nav
//    strCal += "<tr><td colspan=\"4\" class=\"CalNavMonths\">"+GetMonths(StartDate.getMonth())+"</td><td colspan=\"3\" class=\"CalNavYears\">"+GetYears(StartDate.getFullYear())+"</td></tr>";
//    NewDate=false;
//    // Cal Days Header
//    if(FullDayName){aDays=DaysNames;}else{aDays=daysNames;}
//    strCal += "<tr><td class=\"CalDaysHeader\">" + aDays[0] + "</td><td class=\"CalDaysHeader\">" + aDays[1] + "</td><td class=\"CalDaysHeader\">" + aDays[2] + "</td><td class=\"CalDaysHeader\">" + aDays[3] + "</td><td class=\"CalDaysHeader\">" + aDays[4] + "</td><td class=\"CalDaysHeader\">" + aDays[5] + "</td><td class=\"CalDaysHeader\">" + aDays[6] + "</td>";
//    // Cal Body
//    if(StartDate.getDay()==1){StartDate.setTime(StartDate.getTime() - (7*24*60*60*1000));}else if(StartDate.getDay()==0){StartDate.setTime(StartDate.getTime() - (6*24*60*60*1000));}else{StartDate.setTime(StartDate.getTime() - ((StartDate.getDay() - 1)*24*60*60*1000));}
//    for(row=0; row<6; row++){
//        strCal += "<tr>";
//        for(col=0; col<7; col++)
//        {
//            Dates[counter] = StartDate.getDate() + "/" + (StartDate.getMonth()+1) + "/" + StartDate.getFullYear();
//            if(StartDate.getDate()==1)cMonth = !cMonth;
//            if((StartDate.getDay()==6) || (StartDate.getDay()==0))
//                if(cMonth)cssClass = "Weekends";else cssClass = "OtherWeekends";
//            else
//                if(cMonth)cssClass = "CurrentMonth";else cssClass = "OtherMonth";
//            if((StartDate.getFullYear() == Today.getFullYear())&&(StartDate.getMonth() == Today.getMonth())&&(StartDate.getDate() == Today.getDate())){cssClass="Today";}
//            if((StartDate.getFullYear() == SelectedDay.getFullYear())&&(StartDate.getMonth() == SelectedDay.getMonth())&&(StartDate.getDate() == SelectedDay.getDate())){cssClass="SelectedDay";}
//            if((StartDate.getFullYear() < Today.getFullYear()) 
//               || (((StartDate.getMonth()-Today.getMonth())>=12) && (StartDate.getFullYear() > Today.getFullYear()))
//               || ((StartDate.getFullYear() - Today.getFullYear())>1)
//               || ((StartDate.getFullYear() == Today.getFullYear()) && (StartDate.getMonth() < Today.getMonth()))
//               || ((StartDate.getFullYear() == Today.getFullYear()) && (StartDate.getMonth() == Today.getMonth()) && (StartDate.getDate() <= Today.getDate())))
//                strCal += "<td class=\"Disabled\">" + StartDate.getDate() + "</td>";
//            else
//                strCal += "<td class=\"" + cssClass + "\"><a class=\"" + cssClass + "\" href=\"javascript: SetDate('" + counter + "');\">" + StartDate.getDate() + "</a></td>";
//            StartDate.setTime(StartDate.getTime() + (24*60*60*1000));
//            counter++;
//        }
//        strCal += "</tr>";
//    }
//    //strCal += "<tr><td colspan=\"7\" class=\"TodayLink\"><a class=\"TodayLink\" href=\"javascript: SetDefault();\">Set default</a></td></tr>";
//    strCal += "</table>";
//    CalContent.innerHTML = strCal;
//    CalContent.style.left = "0px";
//    CalContent.style.top = "0px";
//    Cal.style.left = pos[0] + "px";
//    Cal.style.top = pos[1] + cont.offsetHeight + cont.style.height + "px";
//    Cal.style.visibility = "visible";
//}
function ShowCal(cont, date) {
    if (Cal == undefined) initCal();
    SelectedField = GetElementById(cont);
    if (date == null)
        date = SelectedField.value;
    if ((GetElementById("DepDay") != null) && (cont.toString().toLowerCase() == "fromdate"))
        cont = GetElementById("DepDay");
    if ((GetElementById("RetDay") != null) && (cont.toString().toLowerCase() == "todate"))
        cont = GetElementById("RetDay");

    var pos = findPos(cont);
    var cMonth = false;
    var StartDate = new Date();
    var Today = new Date();
    Today.setHours(0, 0, 0, 0);
    var Header = "";
    if (Cal.getAttribute("Header") != null)
        Header = Cal.getAttribute("Header");
    if (Cal.getAttribute("TextButtons") != null)
        TextButtons = Cal.getAttribute("TextButtons").toLowerCase() == "true";
    if (Cal.getAttribute("FullDayName") != null)
        FullDayName = Cal.getAttribute("FullDayName").toLowerCase() == "true";
    if (Cal.getAttribute("FullMonthName") != null)
        FullMonthName = Cal.getAttribute("FullMonthName").toLowerCase() == "true";
    Dates = new Array();
    var counter = 0;
    if (TextButtons)
        CloseButton = "x";
    else
        CloseButton = "<img title=\"Close Calendar\" border=\"0\" id=\"btn_close\" src=\"" + ImagesPath + "btn_close_out.gif\" onmouseout=\"MouseOut(this)\" onmouseover=\"MouseOver(this)\" />";
    if (date != null) {
        if (date.toString().indexOf('/') > 0) {
            cDate.setFullYear(date.split('/')[2], (parseInt(date.split('/')[1], 10) - 1), date.split('/')[0]);
        }
        else {
            if (!isNaN(parseInt(date, 10))) { cDate.setTime(parseInt(date, 10)); } else { cDate = new Date(); } 
        }
        if (!NewDate) SelectedDay.setFullYear(cDate.getFullYear(), cDate.getMonth(), cDate.getDate());
    }
    SelectedDay.setHours(2, 0, 0, 0);
    StartDate.setFullYear(cDate.getFullYear(), cDate.getMonth(), 1);
    StartDate.setHours(2, 0, 0, 0);
    var strCal = "<table class=\"CalTable\" cellpadding=\"0\" cellspacing=\"0\">";
    // Cal Header
    //Commented for the Cal change
    //strCal += "<tr><td colspan=\"7\"><table style=\"width:100%\" cellpadding=\"0\" cellspacing=\"0\"><td class=\"CalHeader\">" + Header + "</td><td class=\"Close\"><a href=\"javascript: HideCal();\">" + CloseButton + "</a></td></tr></table></td></tr>";
    strCal += "<tr><td colspan=\"7\"><table style=\"width:100%\" cellpadding=\"0\" cellspacing=\"0\"><td class=\"CalHeader\">" + Header + "</td><td class=\"Close\"><a href=\"javascript: HideCal();\">" + CloseButton + "</a></td></table></td></tr>";
    // Cal Nav
    strCal += "<tr><td colspan=\"4\" class=\"CalNavMonths\">" + GetMonths(StartDate.getMonth()) + "</td><td colspan=\"3\" class=\"CalNavYears\">" + GetYears(StartDate.getFullYear()) + "</td></tr>";
    NewDate = false;
    // Cal Days Header
    if (FullDayName) { aDays = DaysNames; } else { aDays = daysNames; }
    //Commented for Cal
    //strCal += "<tr><td class=\"CalDaysHeader\">" + aDays[0] + "</td><td class=\"CalDaysHeader\">" + aDays[1] + "</td><td class=\"CalDaysHeader\">" + aDays[2] + "</td><td class=\"CalDaysHeader\">" + aDays[3] + "</td><td class=\"CalDaysHeader\">" + aDays[4] + "</td><td class=\"CalDaysHeader\">" + aDays[5] + "</td><td class=\"CalDaysHeader\">" + aDays[6] + "</td>";
    strCal += "<tr><td class=\"CalDaysHeader\">" + aDays[0] + "</td><td class=\"CalDaysHeader\">" + aDays[1] + "</td><td class=\"CalDaysHeader\">" + aDays[2] + "</td><td class=\"CalDaysHeader\">" + aDays[3] + "</td><td class=\"CalDaysHeader\">" + aDays[4] + "</td><td class=\"CalDaysHeader\">" + aDays[5] + "</td><td class=\"CalDaysHeader\">" + aDays[6] + "</td></tr>";
    // Cal Body
    if (StartDate.getDay() == 1) { StartDate.setTime(StartDate.getTime() - (7 * 24 * 60 * 60 * 1000)); } else if (StartDate.getDay() == 0) { StartDate.setTime(StartDate.getTime() - (6 * 24 * 60 * 60 * 1000)); } else { StartDate.setTime(StartDate.getTime() - ((StartDate.getDay() - 1) * 24 * 60 * 60 * 1000)); }
    for (row = 0; row < 6; row++) {
        strCal += "<tr>";
        for (col = 0; col < 7; col++) {
            Dates[counter] = StartDate.getDate() + "/" + (StartDate.getMonth() + 1) + "/" + StartDate.getFullYear();
            if (StartDate.getDate() == 1) cMonth = !cMonth;
            if ((StartDate.getDay() == 6) || (StartDate.getDay() == 0))
                if (cMonth) cssClass = "Weekends"; else cssClass = "OtherWeekends";
            else
                cssClass = "CurrentMonth";
            // Calc problem
            //if(cMonth)cssClass = "CurrentMonth";else cssClass = "OtherMonth";
            if ((StartDate.getFullYear() == Today.getFullYear()) && (StartDate.getMonth() == Today.getMonth()) && (StartDate.getDate() == Today.getDate())) { cssClass = "Today"; }
            if ((StartDate.getFullYear() == SelectedDay.getFullYear()) && (StartDate.getMonth() == SelectedDay.getMonth()) && (StartDate.getDate() == SelectedDay.getDate())) { cssClass = "SelectedDay"; }
            if ((StartDate.getFullYear() < Today.getFullYear()) || (((StartDate.getMonth() - Today.getMonth()) >= 12) && (StartDate.getFullYear() > Today.getFullYear())) || ((StartDate.getFullYear() - Today.getFullYear()) > 1) || ((StartDate.getFullYear() == Today.getFullYear()) && (StartDate.getMonth() < Today.getMonth())) || ((StartDate.getFullYear() == Today.getFullYear()) && (StartDate.getMonth() == Today.getMonth()) && (StartDate.getDate() <= Today.getDate())))
                strCal += "<td class=\"Disabled\">" + StartDate.getDate() + "</td>";
            else
                strCal += "<td class=\"" + cssClass + "\"><a class=\"" + cssClass + "\" href=\"javascript: SetDate('" + counter + "');\">" + StartDate.getDate() + "</a></td>";
            StartDate.setTime(StartDate.getTime() + (24 * 60 * 60 * 1000));
            counter++;
        }
        strCal += "</tr>";
    }
    //strCal += "<tr><td colspan=\"7\" class=\"TodayLink\"><a class=\"TodayLink\" href=\"javascript: SetDefault();\">Set default</a></td></tr>";
    strCal += "</table>";
    CalContent.innerHTML = strCal;
    CalContent.style.left = "0px";
    CalContent.style.top = "0px";
    Cal.style.left = pos[0] + "px";
    Cal.style.top = pos[1] + cont.offsetHeight + cont.style.height + "px";
    //Cal.style.visibility = "visible";
    Cal.style.display = "block";
}
function GetMonths(value)
{
    var cMonths;
    var strMonths = "<select class=\"CalEdit\" id=\"Months\" onchange=\"GetDate()\">";
    if(FullMonthName)cMonths=Months;else cMonths=months;
    var selected = "";
    for(i=0;i<cMonths.length;i++){if(value==i){selected="selected";}else{selected="";}strMonths+="<option value=\""+i+"\" "+selected+">"+cMonths[i]+"</option>";}
    strMonths+="</select>";
    return strMonths;
}
function GetYears(value)
{
    var Today=new Date();var Year = Today.getFullYear();var selected="";
    var strYears = "<select class=\"CalEdit\" id=\"Years\" onchange=\"GetDate()\">";
    for(i=0;i<2;i++){if(value==(Year+i)){selected="selected";}else{selected="";}strYears+="<option value=\""+(Year+i)+"\" "+selected+">"+(Year+i)+"</option>";}
    strYears+="</select>";
    return strYears;
}
function GetDate()
{
    NewDate=true;var lstMonths=GetElementById('Months');var lstYears=GetElementById('Years');
    if((lstMonths!=null) && (lstYears!=null)){ShowCal(SelectedField.id, "1/"+(parseInt(lstMonths.options[lstMonths.selectedIndex].value,10)+1)+"/"+lstYears.options[lstYears.selectedIndex].value);}
}
function HideCal()
{
    HideCalendar = false;
    if(Cal != null)
        //Cal.style.visibility = "hidden";
        Cal.style.display = "none";
}
function findPos(obj) 
{
	var curleft = 0;
	var curtop = 0;
	var IncludeDiv = true;
	if (obj.offsetParent)
	{
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
	    while (obj = obj.offsetParent) 
	    {
	            curleft += obj.offsetLeft;
	            curtop += obj.offsetTop;
	    }
	}
	return [curleft,curtop];
}
function IsIE()
{
    if(window.navigator.userAgent.indexOf("MSIE") > -1)
        return true;
    else
        return false;
}
