﻿// Copyright Zerone Consulting Pvt. Ltd. 2009
//
// All rights are reserved. Reproduction or transmission in whole or in part,in
// any form or by any means, electronic, mechanical or otherwise, is prohibited
// without the prior written consent of the copyright owner.
//
// Filename      :	Utilities.js
// Purpose       :  Utilities.js
// Creation Date :	29/October/2009
// Author        :	Naveenkumar C N
// 
// Change History
// Changed By :
// Date :
// Purpose :


// function to get a parameter value from query string
// Author        :	Naveenkumar C N
function getValueFromQueryString(qry, keyword) {
    var tmpLen = qry.substring(0, qry.indexOf("?")).length + 1;
    qry = qry.substring(tmpLen, qry.length);
    var query = qry;
    var parms = query.split('&');
    for (var i = 0; i < parms.length; i++) {
        var pos = parms[i].indexOf('=');
        if (pos > 0) {
            var key = parms[i].substring(0, pos).toLowerCase();
            var val = parms[i].substring(pos + 1);
            if (key == keyword.toLowerCase())
                return val;
        }
    }
    return null;
}

// function to get a parameter value from query string
// Author        :	Naveenkumar C N
function getFullValueFromQueryString(qry, keyword) {
    try {

        qry = $.URLDecode(qry);

        if (qry.indexOf("?") < 0) {
            return "";
        }
        if (qry.indexOf("ReturnUrl") < 0) {
            return "";
        }

        var tmpLen = qry.substring(0, qry.indexOf("?")).length + 1;
        qry = qry.substring(tmpLen, qry.length);
        qry = qry.substring(qry.indexOf(keyword) + keyword.length + 1);
        return RemoveDuplicateParams(qry);
    }
    catch (exc) {
        return "";
    }
}

// function to get query string
// Author        :	Naveenkumar C N
function getQueryString(qry) {

    if (qry.indexOf("?") >= 0) {
        var tmpLen = qry.substring(0, qry.indexOf("?")).length;
        qry = qry.substring(tmpLen, qry.length);
        return qry;
    }
    else {
        return "";
    }
}


function RemoveDuplicateParams(qry) {
    if (qry.indexOf("?") >= 0) {
        var page = qry.substring(0, qry.indexOf("?") + 1);
        qry = qry.substring(qry.indexOf("?") + 1);
        var params = qry.split('&');

        var newParams = "";

        for (var i = 0; i < params.length; i++) {
            var parm = params[i].split('=');

            if (newParams.indexOf(parm[0]) < 0) {
                newParams += parm[0] + "=" + parm[1] + "&";
            }
        }

        if (newParams.substring(newParams.length, newParams.length - 1) == "&") {
            newParams = newParams.substring(0, newParams.length - 1);
        }

        return page + "?" + newParams;
    }
    else {
        return qry;
    }
}


// function to show customized alert
// this function requires 3 references
// 1. jquery-1.3.2.js
// 2. jquery.jgrowl.js
// 3. jquery.jgrowl.css
// we can change apperance of alert box in style sheet
// Author        :	Naveenkumar C N
function ShowAlert(header, message) {
    // position has te following values
    // top-left, top-right, bottom-left, bottom-right, center
    // default position is top-right
    //$.jGrowl.defaults.position = 'bottom-right';
    $.jGrowl(message, { header: header, sticky: true });
}

// function to navigate from popup
// Author        :	Naveenkumar C N
function RedirectFromPopup(url) {
    if (parent != null) {
        parent.location.href = url;
    }
    else {
        location.href = url;
    }
}

// function to BlockScreen
// Author        :	Naveenkumar C N
function BlockScreen() {
    $.blockUI
    (
        {
            css:
            {
                backgroundColor: 'Transparent',
                border: 'none',
                width: 'auto',
                left: '49%'
            },
            message: '<div class="dvProgress">&nbsp;</div>',
            fadeIn: 0,
            fadeOut: 0,
            onBlock: function() {

            }
        }
    );

}

// function to UnBlockScreen
// Author        :	Naveenkumar C N
function UnblockScreen() {
    try {
        $.unblockUI();
    }
    catch (e) {
    }
}

// function to trim
// Author        :	Naveenkumar C N
String.prototype.trim = function() {
    return this.replace(/\s+/g, "");
}

// function to Show Popup
// Author        :	Naveenkumar C N
function ShowPopup(page, width, height) {

    var left = parseInt((screen.availWidth / 2) - (width / 2));
    var top = parseInt((screen.availHeight / 2) - (height / 2));
    var windowFeatures = "width=" + width + ",height=" + height + ",status=yes,scrollbars=yes,resizable=yes,left=" + left +
     ",top=" + top + "screenX=" + left + ",screenY=" + top;
    window.open(page, "popup", windowFeatures).focus();
}

function ShowDialoguePopUp(page, width, height) {

    var left = parseInt((screen.availWidth / 2) - (width / 2));
    var top = parseInt((screen.availHeight / 2) - (height / 2));
    var windowFeatures = "width=" + width + ",height=" + height + ",status=yes,scrollbars=yes,resizable=yes,left=" + left +
     ",top=" + top + "screenX=" + left + ",screenY=" + top;
    window.open("ShowDialog.aspx?ucpath=" + page, "popup", windowFeatures).focus();
}

// function to Set Cookie
// Author        :	Naveenkumar C N
function SetCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

// function to Get Cookie
// Author        :	Naveenkumar C N
function GetCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");

        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);

            if (c_end == -1) {
                c_end = document.cookie.length;
            }
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}


// function to insert specified text at cursor potision in a text area
// Author        :	Naveenkumar C N
function insertAtCursor(myField, myValue) {
    //IE support
    if (document.selection) {
        myField.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
    }
    //MOZILLA/NETSCAPE support
    else if (myField.selectionStart || myField.selectionStart == '0') {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos)
        + myValue
        + myField.value.substring(endPos, myField.value.length);
    }
    else {
        myField.value += myValue;
    }
}

// function to show free text
// Author        :	Naveenkumar C N
// references    :  jquery-1.3.2.js
function showFreeText(ddl, txt, valId) {
    $("#" + ddl).change(function() {

        ShowFreeTextOnChange(ddl, txt, valId);

    });
}

function ShowFreeTextOnChange(ddl, txt, valId)
{
    var val = document.getElementById(valId);
    var IsOtherSelected = false;

    $("#" + ddl + " option:selected").each
        (
            function() {
                if (!IsOtherSelected && $(this).text().toLowerCase() == "other") {
                    IsOtherSelected = true;
                }
            }
        );


    if (IsOtherSelected) {
        if ($("#" + txt).val().trim().length <= 0 ||
                $("#" + txt).val() == "[[0]]") {
            $("#" + txt).val("");
        }
        ValidatorEnable(val, true)
        $("#" + txt).css("display", "block");
        try {
            $("#" + txt)[0].focus();
            $("#" + txt)[0].select();
        }
        catch (e) {
        }
    }
    else {

        if ($("#" + txt).val().trim().length <= 0 ||
                $("#" + txt).val() == "" ||
                $("#" + txt).val() == "[[0]]") {
            $("#" + txt).val("[[0]]");
        }

        ValidatorEnable(val, false)
        $("#" + txt).css("display", "none");
    }
}

// function to show calender
// Author        :	Naveenkumar C N
// references    :  jquery-1.3.2.js
//               :  jquery-ui-1.7.2.custom.min.js
//               :  timepicker.js
function showCalender(txt, fromYear, toYear) {

    $('#' + txt).datepicker({
        duration: '',
        showTime: false,
        constrainInput: true,
        dateFormat: "yy-mm-dd",
        changeMonth: true,
        changeYear: true,
        minDate: new Date(fromYear, 0, 1),
        maxDate: new Date(toYear, 11, 31),
        yearRange: "-200:+200",
        onSelect: function() {
            this.fireEvent && this.fireEvent('onchange') || $(this).change();
        }

    });

}


// function to show calender
// Author        :	Naveenkumar C N
// references    :  jquery-1.3.2.js
//               :  jquery-ui-1.7.2.custom.min.js
//               :  timepicker.js
function showCalenderAndTime(txt, fromYear, toYear) {

    $('#' + txt).datepicker({
        duration: '',
        showTime: true,
        constrainInput: true,
        dateFormat: "yy-mm-dd",
        changeMonth: true,
        changeYear: true,
        minDate: new Date(fromYear, 0, 1),
        maxDate: new Date(toYear, 11, 31),
        yearRange: "-200:+200",
        onSelect: function() {
            this.fireEvent && this.fireEvent('onchange') || $(this).change();
        }

    });

}

// function to show confirmation Message
// Author        :	Jerin Simon 
function fnConfirmDelete(message) {
    if (confirm(message)) {
        return true;
    }
    else {
        return false;
    }
}

function validateFormDate(txtBox, errorMessage) {
    var result = true;

    try {
        for (i = 0; i < Page_Validators.length; i++) {
            var val = Page_Validators[i];

            if (val.evaluationfunction(val) == false) {
                result = false;
            }
        }
    }
    catch (exc) {
    }
    var txt = $('#' + txtBox);

    if (txt.val().trim().length <= 0) {
        var len = txt.parent()[0].getElementsByTagName("SPAN").length;

        if (len <= 0) {
            var spn = document.createElement("SPAN");
            $(spn).attr("class", "spnJQDateError");
            $(spn).html(errorMessage);
            $(spn).css
            (
                {
                    color: 'red',
                    display: 'inline'
                }
            );
            txt.parent().append($(spn));
        }
        else {
            $(txt.parent()[0].getElementsByTagName("SPAN")[0]).css("display", "inline");
        }
        result = false;
    }
    else {
        $(txt.parent()[0].getElementsByTagName("SPAN")[0]).css("display", "none");
    }

    return result;
}

function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + // ',' +
        num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + num + '.' + cents);
}

function OpenInNewWindow(url) {
    window.open(url);
}

function SetIFrameHeightAuto(obj) {
    try {
        if (obj.self != top) {

            if (document.URL != null && document.URL.toLowerCase().indexOf('/candidate/cvgeneral.aspx') > 0 ||
                document.URL != null && document.URL.toLowerCase().indexOf('/candidate/cvsecondaryeducation.aspx') > 0 ||
                document.URL != null && document.URL.toLowerCase().indexOf('/candidate/cvtertiaryeducation.aspx') > 0 ||
                document.URL != null && document.URL.toLowerCase().indexOf('/candidate/cvcourses.aspx') > 0 ||
                document.URL != null && document.URL.toLowerCase().indexOf('/candidate/cvmembership.aspx') > 0 ||
                document.URL != null && document.URL.toLowerCase().indexOf('/candidate/cvskills.aspx') > 0 ||
                document.URL != null && document.URL.toLowerCase().indexOf('/candidate/cvworkhistory.aspx') > 0 ||
                document.URL != null && document.URL.toLowerCase().indexOf('/candidate/cvreferences.aspx') > 0 ||
                document.URL != null && document.URL.toLowerCase().indexOf('/candidate/cvemploymentpreferences.aspx') > 0 ||
                document.URL != null && document.URL.toLowerCase().indexOf('/candidate/cvsummary.aspx') > 0 ||
                document.URL != null && document.URL.toLowerCase().indexOf('setifht=true') > 0) {
                window.frameElement.style.height = document.body.scrollHeight + 20 + 'px';
            }
        }
    }
    catch (e) { }
}
