﻿//----------------------------------------------------------------------------------
//Programmer: Cedric De Walsche
//Company: Galveston.com & Companies inc
//Date: 4/29/2010
//Functions added to operate ajax calendar:
//fShowCal(m, y, o, p) takes month,year,textbox name and allow previous dates vars and opens calendar through ajax in div
//fShowCalYear(y, o, p) takes year,textbox name and allow previous dates vars and opens 1 year of months trough ajax in div
//fHideCal() hides the calendar div
//SetValue(d, o) takes date,textbox name and sets date in textbox
//GetCordinates(obj) takes obj of textbox where calendar date will be shown in and 
//                  gets the xy coordinates of the textbox so fShowCal can calculate
//                  where to show the caledar div
//----------------------------------------------------------------------------------


//global vars to hold coordinates of textbox for use in other functions
var X = 0;
var Y = 0;

function fShowCal(m, y, o, p) {
    //check if m = ''
    if (m == '') {
        //check if there is already a date in the date textbox and use the month from it
        var txt = document.getElementById(o);
        var d = new Date(txt.value);
        m = d.getMonth() + 1;
    }

    //check if m = ''
    if (y == '') {
        //check if there is already a date in the date textbox and use the month from it
        var txt = document.getElementById(o);
        var d = new Date(txt.value);
        y = d.getFullYear();
    }
    
    var obj = document.getElementById("divPopup");
    obj.style.visibility = 'visible';
    obj.style.position = "absolute";
    obj.style.left = X + "px";
    obj.style.top = Y + 20 + "px";
    //call ajax responseText
    ChangeRes("/calendar.aspx?m=" + m + "&y=" + y + "&o=" + o + "&p=" + p);
}

function fShowCalYear(y, o, p) {
    var obj = document.getElementById("divPopup");
    obj.style.visibility = 'visible';
    obj.style.position = "absolute";
    obj.style.left = X + "px";
    obj.style.top = Y + 20 + "px";
    //call ajax responseText
    ChangeRes("/calendaryear.aspx?y=" + y + "&o=" + o + "&p=" + p);
}

function fHideCal() {
    var obj = document.getElementById("divPopup");
    obj.style.visibility = 'hidden';
}

function SetValue(d, o) {
    var cal = document.getElementById(o);
    cal.value = d;
    fHideCal();
}

function SetBlankValue(o) {
    var cal = document.getElementById(o);
    cal.value = "";
    fHideCal();
}

function GetCordinates(obj) {
    X = obj.offsetLeft;
    Y = obj.offsetTop;
    while (obj.offsetParent) {
        X = X + obj.offsetParent.offsetLeft;
        Y = Y + obj.offsetParent.offsetTop;
        if (obj == document.getElementsByTagName("body")[0]) {
            break;
        }
        else {
            obj = obj.offsetParent;
        }
    }
}


function ChangeRes(URL) {
    var req = null;

    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        if (req.overrideMimeType) {
            req.overrideMimeType('text/xml');
        }
    }
    else if (window.ActiveXObject) {
        try {
            req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) { }
        }
    }

    req.onreadystatechange = function() {
        if (req.readyState == 4 || req.readyState == "complete") {
            if (req.status == 200) {
                try {
                    document.getElementById("divPopup").innerHTML = req.responseText;
                }
                catch (e) {
                    // IE fails unless we wrap the string in another element.
                    //Clear outer div first
                    document.getElementById("divPopup").innerHTML = '';
                    var wrappingElement = document.createElement('div');
                    wrappingElement.innerHTML = req.responseText;
                    document.getElementById("divPopup").appendChild(wrappingElement);
                }
            }
            else {
                document.getElementById("divPopup").innerHTML = "Error: returned status code " + req.status + " " + req.statusText;
            }
        }
    };
    req.open("GET", URL, true);
    req.send(null);
}

