
function getId(ids) { return document.getElementById(ids); }

function DGrid_OnMouse(d, t, a, css_item, css_itemAlt, css_itemOn) {
    if (t == 0) {
        d.className = css_itemOn;
        d.style.zIndex = 1000;
    }
    else {
        if (a == 'AltItem') d.className = css_itemAlt; else d.className = css_item;
    }
}

function DGrid_ChangeSorting(DGrid_id, unique_id, isSortDesc) {
    var element = getId(DGrid_id + '_IsSortDesc');
    if (element != null)
        element.value = isSortDesc;
    element = getId(DGrid_id + '_IsCtrlPressed');
    if (element != null)
        element.value = event.ctrlKey;

    __doPostBack(unique_id, '');
    return false;
}

String.prototype.trim = function() {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}


/*------check-boxes---------*/
var dvCheckBoxM_Class = "btMailCheckBox";
var dvCheckBoxM_Class_ = "btMailCheckBox_";
function dvCheckBoxClick(it, id) {
    it.className = (it.className == dvCheckBoxM_Class ? dvCheckBoxM_Class_ : dvCheckBoxM_Class);
    document.getElementById(id).value = !(it.className == dvCheckBoxM_Class);
    return false;
}


/*------check-radio-boxes---------*/
var dvRadioBox_Class = "ccRadioBox";
var dvRadioBox_Class_ = "ccRadioBox_";
var dvCheckBox_Class = "ccCheckBox";
var dvCheckBox_Class_ = "ccCheckBox_";
var dvCheckBox_ClassErr = "ccCheckBox_error";

function dvCheckBoxIsCheck(idBox, EndId) {
    var a = $(idBox + '_' + EndId + "_");
    if (a.className !== dvCheckBox_Class_) { a.className = dvCheckBox_ClassErr; return false; }
    return true;
}

function dvRadioCheckBox(idBox, startId, EndId, css, css_) {
    //uncheck all elements in box
    if (idBox != '') {
        $(idBox).getElements('input[id^=' + startId + ']').setProperty('value', 'false');
        var dox = $(idBox).getElements('a[id^=' + startId + ']');
        dox.each(function(el) { el.className = css; });
    }
    //check/uncheck current element
    var i = $(startId + EndId); var a = $(startId + EndId + "_");
    var acl = a.onclick; a.onclick = function() { return false };
    var nval = "true"; var ncss = css_;
    if (idBox == '') {
        nval = (i.getProperty('value') == 'true' ? 'false' : 'true');
        ncss = (nval == 'true' ? css_ : css);
    }
    i.setProperty('value', nval);
    //a.removeClass(css_);
    //a.addClass(ncss);
    a.className = ncss;
    a.setOpacity(0.5);
    //(function(){a.toggleClass(dvRadioBox_Class_);}).delay(150);
    a.effect('opacity', { duration: 150, transition: Fx.Transitions.Quart.easeIn, onComplete: function() { a.onclick = acl; } }).start(.5, 1);
    return false;
}

function dvRadioBox(idBox, startId, EndId) {
    return dvRadioCheckBox(idBox, startId, EndId, dvRadioBox_Class, dvRadioBox_Class_);
}

function dvCheckBox(idBox, startId, EndId) {
    return dvRadioCheckBox('', startId, EndId, dvCheckBox_Class, dvCheckBox_Class_);
}


/*------change scroller -------*/
function dvOnTop(itn) {

    var winScroller;
    if (typeof (itn) != 'undefined') {
        winScroller = new Fx.Scroll(window);
        var it = $(itn); var pos = it.getPosition();
        var y = pos.y - 100; if (y < 0) y = 0;
        winScroller.scrollTo(0, y).chain(function() { winScroller.toElement(itn); });
    }
    else {
        winScroller = new Fx.Scroll(window, { duration: 1000, transition: Fx.Transitions.Quart.easeOut });
        winScroller.scrollTo(0, 0);
    }
    return false;
}

/*------check date format -------*/
// Declaring valid date character, minimum year and maximum year
var dtCh = "/";
var minYear = 1900;
var maxYear = 2100;

function isInteger(s) {
    var i;
    for (i = 0; i < s.length; i++) {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag) {
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary(year) {
    // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28);
}
function DaysArray(n) {
    for (var i = 1; i <= n; i++) {
        this[i] = 31
        if (i == 4 || i == 6 || i == 9 || i == 11) { this[i] = 30 }
        if (i == 2) { this[i] = 29 }
    }
    return this
}

function isDate(dtStr) {
    var daysInMonth = DaysArray(12)
    var pos1 = dtStr.indexOf(dtCh)
    var pos2 = dtStr.indexOf(dtCh, pos1 + 1)
    var strMonth = dtStr.substring(0, pos1)
    var strDay = dtStr.substring(pos1 + 1, pos2)
    var strYear = dtStr.substring(pos2 + 1)
    strYr = strYear
    if (strDay.charAt(0) == "0" && strDay.length > 1) strDay = strDay.substring(1)
    if (strMonth.charAt(0) == "0" && strMonth.length > 1) strMonth = strMonth.substring(1)
    for (var i = 1; i <= 3; i++) {
        if (strYr.charAt(0) == "0" && strYr.length > 1) strYr = strYr.substring(1)
    }
    month = parseInt(strMonth)
    day = parseInt(strDay)
    year = parseInt(strYr)
    if (pos1 == -1 || pos2 == -1) {
        alert("The date format should be : mm/dd/yyyy")
        return false
    }
    if (strMonth.length < 1 || month < 1 || month > 12) {
        alert("Please enter a valid month")
        return false
    }
    if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]) {
        alert("Please enter a valid day")
        return false
    }
    if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear) {
        alert("Please enter a valid 4 digit year between " + minYear + " and " + maxYear)
        return false
    }
    if (dtStr.indexOf(dtCh, pos2 + 1) != -1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false) {
        alert("Please enter a valid date")
        return false
    }
    return true
}

function checkEmail(element) {
    var email = document.getElementById(element);
    var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(email.value)) {
        alert('Please provide valid email address');
        email.focus;
        return false;
    }
    return true;
}

function checkDate(element) {
    if (document.getElementById(element).value != '') {
        if (!isDate(document.getElementById(element).value)) {
            document.getElementById(element).focus();
            return false;
        }
    }
    return true;
}

function checkDateFormat(element1, element2) {
    if ($(element1) == null) {
        return true;
    }


    if (document.getElementById(element1).value != '') {
        if (!isDate(document.getElementById(element1).value)) {
            document.getElementById(element1).focus();
            return false;
        }
    }

    if (document.getElementById(element2).value != '') {
        if (!isDate(document.getElementById(element2).value)) {
            document.getElementById(element2).focus();
            return false;
        }
    }

    return true;
}

function checkInputValidation() {
    var i;

    //var textboxes=$('input[type=text].InputFld');

    var textboxes = $(document.body).getElements('input.InputFld[type=text]');

    for (i = 0; i < textboxes.length; i++) {
        if (document.getElementById(textboxes[i].id).value.replace(/(^\s+|\s+$)/g, '') === '') {
            alert("Make sure all fields are completed");
            return false;
        }
    }
    return true;
}

// disables the button specified and sets its style to a disabled "look".
function disableButtonOnClick(oButton, sButtonText, sCssClass) {
    // set button to disabled so you can't click on it.

    oButton.disabled = true;

    // change the text of the button.

    //  oButton.value = sButtonText; 

    // IE uses className for the css property.

    // oButton.setAttribute('className', sCssClass); 

    // Firefox, Safari use class for the css property.  (doesn't hurt to do both).

    // oButton.setAttribute('class', sCssClass);
}

function PhoneValidate(value) {
    var phoneRe = /^(\+(\d)+\s*)?(\((\+)?(\d+)\)[-\s]?)?(\d)([-\s\d]+)?(\d)$/; ;

    if (phoneRe.test(value)) {
        return true;
    } else {
        return false;
    }
}
