﻿//web service call for check box
function ajxSingleCheckBoxCall(call_id, id, waitimg_id, value)
{
    var chk = $get(id);
    var waitimg = $get(waitimg_id);
    
    chk.style.display = "none";
    waitimg.style.display = "inline";
    
    var ctrl_info = String.format("{0},{1},{2}", id, waitimg_id, chk.checked);
    
    MyWebServices.AjxSingleCheckBoxCall(call_id, value, chk.checked, ctrl_info,
                                         ajxSingleCheckBoxSucceed, ajxSingleCheckBoxFail, ctrl_info);
}

function ajxSingleCheckBoxSucceed(result, ctrl_info, methodname)
{ 
    var ctrl_infos = ctrl_info.split(",");
    var chk = $get(ctrl_infos[0]);
    var waitimg = $get(ctrl_infos[1]);
    
    if(result) chk.checked = true; else chk.checked = false;
    
    chk.style.display = "inline";
    waitimg.style.display = "none";
}

function ajxSingleCheckBoxFail(error)
{
    var ctrl_info = error.get_message();
    var ctrl_infos = ctrl_info.split(",");
    var chk = $get(ctrl_infos[0]);
    var waitimg = $get(ctrl_infos[1]);
    var checked = ctrl_infos[2];
    
    if(checked=='true') chk.checked = false; else chk.checked = true;
    chk.style.display = "inline";
    waitimg.style.display = "none";
}

//function ajxSendVideoLinkFail(error)
//{
//    $get('divMsg').innerHTML = "<a class='video_title' href='javascript:void(0);' onclick=\"$get('divSendMail').style.display ='inline';" 
//                                + "$get('divProcess').style.display = 'none';\">Send is Failed!</a>" ;
//}
//web service call for ext text button
function ajxTextButtonCall(call_id, id, txt_id, btn_id, waitimg_id, errlnk_id)
{
    var txt = $get(txt_id);
    var btn = $get(btn_id);
    var waitimg = $get(waitimg_id);
    var errlnk = $get(errlnk_id);
    
    txt.style.display = "none";
    btn.style.display = "none";
    waitimg.style.display = "inline";
    errlnk.style.display = "none";
    
    var ctrl_info = String.format("{0},{1},{2},{3}", txt_id, btn_id, waitimg_id, errlnk_id);
    
    MyWebServices.AjxTextButtonCall(call_id, id, txt.value, ctrl_info,
                        ajxTextButtonSucceed, ajxTextButtonFail, ctrl_info);
}
function ajxTextButtonEnable(ctrl_info)
{
    var ctrl_infos = ctrl_info.split(",");
    var txt = $get(ctrl_infos[0]);
    var btn = $get(ctrl_infos[1]);
    var waitimg = $get(ctrl_infos[2]);
    var errlnk = $get(ctrl_infos[3]);
    
    txt.style.display = "inline";
    btn.style.display = "inline";
    waitimg.style.display = "none";
    errlnk.style.display = "none";   
}
function ajxTextButtonSucceed(result, ctrl_info, methodname)
{ 
    var ctrl_infos = ctrl_info.split(",");
    var txt = $get(ctrl_infos[0]);
    txt.value = "Message sent.";
    
    ajxTextButtonEnable(ctrl_info);
}
function ajxTextButtonFail(error)
{
    var ctrl_info = error.get_message();
    var ctrl_infos = ctrl_info.split(",");
    var txt = $get(ctrl_infos[0]);
    var btn = $get(ctrl_infos[1]);
    var waitimg = $get(ctrl_infos[2]);
    var errlnk = $get(ctrl_infos[3]);
    
    txt.style.display = "none";
    btn.style.display = "none";
    waitimg.style.display = "none";
    errlnk.style.display = "inline";
    
    errlnk.onclick = function(){ ajxTextButtonEnable(ctrl_info); };
}

//web service call for ext radio
function ajxRadioCall(call_id, radioid, id)
{
    var rdo = $get(radioid);
    
    MyWebServices.AjxRadioCall(call_id, id, rdo.value,
        ajxRadioSucceed, ajxRadioFail, radioid);
    
    rdo.disabled  = true;
}
function ajxRadioSucceed(result, radioid, methodname)
{
    var rdo = $get(radioid);
    rdo.disabled = false;
}
function ajxRadioFail(error)
{
}

//web service call for ext checkbox
function ajxCheckBoxCall(call_id, chkboxname, id)
{
    var chkbox = $get(chkboxname);
    var checked = "false";
    if(chkbox.value = "on") checked = "true";
    
    MyWebServices.AjxCheckBoxCall(call_id, id, checked,
        ajxCheckBoxSucceed, ajxCheckBoxFail, chkboxname);
    
    chkbox.disabled  = true;
}
function ajxCheckBoxSucceed(result, chkboxname, methodname)
{
    var chkbox = $get(chkboxname);
    chkbox.disabled  = false;
    if(result) chkbox.value = "on"; else chkbox.value = "off";
}
function ajxCheckBoxFail(error)
{
}

//web service call for bool databound gridcolumn
function ajxCheckBoxCall2(call_id, img_ctrl, id, check_img, wait_img, nocheck_img)
{
    var img = $get(img_ctrl);
    if(img.src == wait_img) return;
    
    var checked = (img.src == check_img)
    img.src = wait_img;
    
    MyWebServices.AjxCheckBoxCall(call_id, id, checked,
        ajxCheckBoxSucceed2, ajxCheckBoxFail2, img_ctrl + "," + check_img + "," + nocheck_img);
}

function ajxCheckBoxSucceed2(result, ctrl_info , methodname)
{
    var infos = ctrl_info.split(',');
    var img_ctrl = infos[0];
    var check_img = infos[1];
    var nocheck_img = infos[2];   
    var img = $get(img_ctrl);
    if(result)
    {
        img.src = check_img
    }else
    {
        img.src = nocheck_img
    }
}
function ajxCheckBoxFail2(error)
{
}

//ajax simple bound column
function gridSimpleColumnBeginEdit(lblId, txtId, imgBtnOkId, imgBtnCancelId)
{
    var lbl = $get(lblId);
    var txt = $get(txtId);
    var imgOk = $get(imgBtnOkId);
    var imgCancel = $get(imgBtnCancelId);
    
    lbl.style.display = "none";
    txt.style.display = "inline";
    imgOk.style.display = "inline";
    imgCancel.style.display = "inline";
}
function gridSimpleColumnEndEdit(lblId, txtId, imgBtnOkId, imgBtnCancelId)
{
    var lbl = $get(lblId);
    var txt = $get(txtId);
    var imgOk = $get(imgBtnOkId);
    var imgCancel = $get(imgBtnCancelId);
    
    txt.value = lbl.innerHTML;
    
    lbl.style.display = "inline";
    txt.style.display = "none";
    imgOk.style.display = "none";
    imgCancel.style.display = "none";
}
//Web Service Signature for ajax grid simple column
//AjxGridSimpleColumnCall(ByVal cid As String, ByVal id As String, ByVal newValue As String) As Boolean
function ajxGridSimpleColumnCall(call_id, id, lblId, txtId, imgBtnOkId, imgBtnCancelId, imgWaitId)
{
    var lbl = $get(lblId);
    var txt = $get(txtId);
    var imgOk = $get(imgBtnOkId);
    var imgCancel = $get(imgBtnCancelId);
    var waitImg = $get(imgWaitId);
    var newval = txt.value;
    
    var ctrl_info = String.format("{0},{1},{2},{3},{4},{5},{6}", 
                        lblId, txtId, imgBtnOkId, imgBtnCancelId, imgWaitId, lbl.innerHTML, txt.value);
    ctrl_info = ctrl_info.trim();
        
    MyWebServices.AjxGridSimpleColumnCall(call_id, id, newval,
        ajxGridSimpleColumnSucceed, ajxGridSimpleColumnFail, ctrl_info);
        
    lbl.style.display = "none";
    txt.style.display = "none";
    imgOk.style.display = "none";
    imgCancel.style.display = "none";
    waitImg.style.display = "inline";
}
function ajxGridSimpleColumnSucceed(result, ctrl_info , methodname)
{
    var infos = ctrl_info.split(',');
    var lbl = $get(infos[0]);
    var txt = $get(infos[1]);
    var imgOk = $get(infos[2]);
    var imgCancel = $get(infos[3]);
    var waitImg = $get(infos[4]);
    var oldvalue = infos[5];
    var newvalue = infos[6];
    
    if(result)
    {
        lbl.innerHTML = newvalue;
        txt.value = newvalue;
    }else
    {
        lbl.innerHTML = oldvalue;
        txt.value = oldvalue;
    }
    
    lbl.style.display = "inline";
    txt.style.display = "none";
    imgOk.style.display = "none";
    imgCancel.style.display = "none";
    waitImg.style.display = "none";
}
function ajxGridSimpleColumnFail(error)
{
}

//Web Service Signature for ajax rating control
//AjxRateCall(ByVal cid As String, ByVal id As String, ByVal val As String) As Boolean
function ajxRateCall(call_id, id, ctrlName, prefix, itemCount, startIdxHdn, curValHdn, newval, emptyImg, negImg, posImg, waitImgId)
{
    var ctrl_info = String.format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9}", ctrlName, prefix, itemCount, startIdxHdn, curValHdn, newval, emptyImg, negImg, posImg, waitImgId);
    ctrl_info = ctrl_info.trim();
    
    var ctrl = $get(ctrlName);
    var waitImg = $get(waitImgId);
    waitImg.style.display = "inline";
    ctrl.style.display = "none";
    
    MyWebServices.AjxRateCall(call_id, id, newval,
        ajxRateSucceed, ajxRateFail, ctrl_info);
}
function ajxRateSucceed(result, ctrl_info , methodname)
{
    var ctrl_infos = ctrl_info.split(",");
    var ctrlName = ctrl_infos[0];
    var prefix = ctrl_infos[1];
    var itemCount = ctrl_infos[2];
    var startIdxHdn = ctrl_infos[3];
    var curValHdn = ctrl_infos[4];
    var newval = ctrl_infos[5];
    var emptyImg = ctrl_infos[6];
    var negImg = ctrl_infos[7];
    var posImg = ctrl_infos[8];
    var waitImgId = ctrl_infos[9];
    
    var ctrl = $get(ctrlName);
    var waitImg = $get(waitImgId);
    waitImg.style.display = "none";
    ctrl.style.display = "inline";
    var curValF = $get(curValHdn);
    curValF.value = newval;
    ajxRateMouseOut(prefix, itemCount, startIdxHdn, curValHdn, emptyImg, negImg, posImg);
}
function ajxRateFail(error)
{
    var msgErr = error.get_message();
    if(msgErr == "not mamber")
    {
        window.location = "login.aspx";
    }
}

function ajxRateMouseOver(prefix, itemCount, fromIdx, toIdx, emptyImg, selImg)
{
    var i = 0;
    for( i = 0; i < itemCount; i++ )
    {
        var item = $get(prefix + i);
        if( i < fromIdx || i > toIdx )
        {
            item.src = emptyImg;
        }
        else
        {
            item.src = selImg;
        }
    }
}

//get from and to index from hidden field
function ajxRateMouseOut(prefix, itemCount, startIdxHdn, curValHdn, emptyImg, negImg, posImg)
{
    var startIdxF = $get(startIdxHdn);
    var startIdx = parseInt(startIdxF.value);
    var curValF = $get(curValHdn);
    var curVal = parseInt(curValF.value);
    
    var i = 0;
    for( i = 0; i < itemCount; i++ )
    {
        var item = $get(prefix + i);
        item.src = emptyImg;
    }
    
    if( curVal < 0 )
    {
        for( i = startIdx - 1; i > startIdx + curVal - 1; i-- )
        {
            var item = $get(prefix + i);
            item.src = negImg;
        }
    }
    else
    {
        for( i = startIdx; i < startIdx + curVal; i++ )
        {
            var item = $get(prefix + i);
            item.src = posImg;
        }
    }
}

//general control behavior
function confirmDelete()
{
    return confirm('Are you sure you want to delete this? This will be deleted immediately, and there is no undo facility. Please click OK to delete or Cancel.');
}

function compare2TextBox(oStatus)
{
    currDate = new Date();
    year = document.forms["Custom"].elements["ExpYear"].value;
    month = document.forms["Custom"].elements["ExpMonth"].value;
    if (currDate.getFullYear() > year || (currDate.getFullYear() == year && currDate.getMonth() + 1 > month))
    {
        oStatus.isvalid = false;
        oStatus.errmsg = "The credit date entered is not valid";
    }
}

//change css class of many object with objects name in format 'object1,object2,object3,.....'
function changeCssClass(objsName, cssClass)
{
    var objs = objsName.split(',');
    var i = 0;
    for( i = 0; i< objs.length; i++)
    {
        document.getElementById(objs[i]).className = cssClass;
    }
}


