﻿/* ~/App_Scripts/Common.js
* Copyright : © 2009-2012  BuyerDay™
* Author    : Sithira Weerabahu
* CreatedOn : 03-July-2009
*/
var isAlertLoaded = false;
var btn_OK_CANCEL = 'OK_CANCEL';
var btn_RETRY_CANCEL = 'RETRY_CANCEL';
var btn_OK = 'OK';
var btn_YES_NO = 'YES_NO';
var btn_selection = null;
var alertCallBackMethod = null;
var opt_OK = 'OK';
var opt_CANCEL = 'CANCEL';
var opt_RETRY = 'RETRY';
var opt_YES = 'YES';
var opt_NO = 'NO';
var alert_SUCCESS = 'SUCCESS';
var alert_ERROR = 'ERROR';
var alert_WARNING = 'WARNING';
var alert_NORMAL = 'NORMAL';

function changeCssClass(control, classname) { /// changes the css class of the given control
    control.setAttribute('class', classname);
    control.setAttribute('className', classname);
}

function setDisplayText(control, defaulttext, cleartext) {
    if (cleartext) {
        if (control.value == defaulttext) {
            if (defaulttext == ' Password') {
                try {
                    control.type = 'password';
                    control.value = '';
                } catch (e) { }
            } else { 
                control.value = '';
            }
        }
    } else { 
        if (control.value == '' || (defaulttext != ' Password' && control.value == defaulttext)) {
            if (defaulttext == ' Password') {
                try {
                    control.type = 'text';
                    control.value = defaulttext;
                } catch (e) { }
            } else { 
                control.value = defaulttext;
           }
        }
    }
}

function onLoad() {
    if (document.getElementById("<%# txtEmailAddress.ClientID%>")) setDisplayText(document.getElementById('<%# txtEmailAddress.ClientID%>'), ' Email Address', false);
    if (document.getElementById('<%# txtPassword.ClientID%>')) setDisplayText(document.getElementById('<%# txtPassword.ClientID%>'), ' Password', false);
    isAlertLoaded = false;
}

function loadLogin() {
    if (isAlertLoaded) return false; /// exit the function call when the alert is displaying
    window.location = 'Login.aspx';
}

function loadSignUp() {
    if (isAlertLoaded) return false; /// exit the function call when the alert is displaying
    window.location = 'SignUp.aspx';
}

function showAlert(titletext, bodytext, buttons, showclose, alerttype, callbackmethod) {
    var blanket = document.getElementById('dvBlanket');
    var alertBorder = document.getElementById('dvAlertBorder');
    if (document.documentElement &&
        document.documentElement.scrollHeight) {
        blanket.style.height = (document.documentElement.scrollHeight - 20) + 'px';
    }
    blanket.style.display = 'block';
    alertBorder.style.display  = 'block';
    isAlertLoaded = true;
    btn_selection = buttons;
    alertCallBackMethod = callbackmethod;

    document.getElementById('dvAlertTitle').innerHTML = titletext;
    document.getElementById('dvAlertBody').innerHTML = bodytext;
    document.getElementById('dvAlertClose').style.display = showclose ? 'block' : 'none';

    var btn1 = document.getElementById('btnAlertButton1');
    var btn2 = document.getElementById('btnAlertButton2');
    switch (btn_selection) {
        case btn_OK:
            btn1.style.display = '';
            btn1.value = 'OK';
            btn2.style.display = 'none';
            break;
        case btn_OK_CANCEL:
            btn1.style.display = '';
            btn1.value = 'OK';
            btn2.style.display = '';
            btn2.value = 'Cancel';
            break;
        case btn_RETRY_CANCEL:
            btn1.style.display = '';
            btn1.value = 'Retry';
            btn2.style.display = '';
            btn2.value = 'Cancel';
            break;
        case btn_YES_NO:
            btn1.style.display = '';
            btn1.value = 'Yes';
            btn2.style.display = '';
            btn2.value = 'No';
            break;
    }

    var alertBody = document.getElementById('dvAlertBody');
    var alertTitle = document.getElementById('dvAlertTitle');
    switch (alerttype) {
        case alert_SUCCESS:
            alertBody.style.color='Green' ;
            break;
        case alert_ERROR:
            alertBody.style.color = 'Red';
            break;
        case alert_WARNING:
            alertBody.style.color = 'Blue';
            break;
        case alert_NORMAL:
            alertBody.style.color = 'Black';
            break;
    }
    var scrollTop = document.documentElement.scrollTop > document.body.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
    var scrollLeft = document.documentElement.scrollLeft > document.body.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft;
    var innerHeight;
    var innerWidth;
    if (window.innerHeight)
        innerHeight = window.innerHeight;
    else
        innerHeight = document.documentElement.offsetHeight;

    if (window.innerWidth)
        innerWidth = window.innerWidth;
    else
        innerWidth = document.documentElement.offsetWidth;

    alertBorder.style.top = ((innerHeight - alertBorder.scrollHeight) / 2 + scrollTop) + 'px';
    alertBorder.style.left = ((innerWidth - alertBorder.scrollWidth) / 2  + scrollLeft) + 'px';
    setTimeout(setFocusToButton, 100);
}

function setFocusToButton() {
    var btn1 = document.getElementById('btnAlertButton1');
    try { btn1.focus(); } catch (e) { }
}

function hideAlert() {
    document.getElementById('dvAlertBorder').style.display = 'none';
    isAlertLoaded = false;
}

function alertClick(buttonid) {
    var optioncode;
    switch (btn_selection) {
        case btn_OK:
            optioncode = opt_OK;
            break;
        case btn_OK_CANCEL:
            optioncode = buttonid == 1? opt_OK: opt_CANCEL;
            break;
        case btn_RETRY_CANCEL:
            optioncode = buttonid == 1 ? opt_RETRY: opt_CANCEL;
            break;
        case btn_YES_NO:
            optioncode = buttonid == 1 ? opt_YES: opt_NO;
            break;
    }
    hideAlert();
    if (alertCallBackMethod) alertCallBackMethod(optioncode);
}

function limitLength(control, limit) {
    if (control.value.length > limit)
        control.value = control.value.substring(0, limit);
}

function disableEnterKey(e) {
    var key;
    if (window.event)
        key = window.event.keyCode;//IE
    else
        key = e.which;//firefox
    return (key != 13);
}

function initLogin(e) {
    var key;
    if (window.event)
        key = window.event.keyCode;//IE
    else
        key = e.which; //firefox
        
    if (key == 13) {
        setTimeout(callLogin, 10);
        return false;
    }
    return true;
}

function displayMemberDetails(memberId) {
    TMantra.CommonServices.GetMemberDetails(memberId, onDisplayMember);
}

function displayMemberSummary(memberId) {
    TMantra.CommonServices.GetMemberSummary(memberId, onDisplayMember);
}

function displayMemberSummary(memberId) {
    TMantra.CommonServices.GetMemberSummary(memberId, onDisplayMember);
}

function onDisplayMember(result) {
    showAlert('Buyerday | View Member Details',
              result,
              btn_OK,
              true,
              alert_WARNING,
              null);
}

function RFVEvaluateIsValid(controltovalidate, initialvalue) { /// RequiredFieldValidator
    return (VTrim(VGetValue(controltovalidate)) != VTrim(initialvalue))
}

function REVEvaluateIsValid(controltovalidate, validationexpression) { /// RegularExpressionValidator
    var value = VGetValue(controltovalidate);
    if (VTrim(value).length == 0)
        return true;
    var rx = new RegExp(validationexpression);
    var matches = rx.exec(value);
    return (matches != null && value == matches[0]);
}
function VTrim(s) {
    var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
    return (m == null) ? "" : m[1];
}
function VGetValue(id) {
    var control;
    control = document.getElementById(id);
    if (typeof (control.value) == "string") {
        return control.value;
    }
    return VGetValueRecursive(control);
}
function VGetValueRecursive(control) {
    if (typeof (control.value) == "string" && (control.type != "radio" || control.checked == true)) {
        return control.value;
    }
    var i, val;
    for (i = 0; i < control.childNodes.length; i++) {
        val = VGetValueRecursive(control.childNodes[i]);
        if (val != "") return val;
    }
    return "";
}
