/* $Id: quickform_adv.js 13202 2007-04-09 23:45:10Z pvandijk $ */
var hexChars = "#0123456789ABCDEF";

var VISA       = 1;
var MASTERCARD = 2;
var AMEX       = 4;
var DINERS     = 8;
var BANKCARD   = 16;

var qf_color_tbl = null;
var qf_color_fld = null;
var qf_color     = null;
var qf_allow_transparent = false;



// function for SF_COLOR control

function QF_update_allow_transparent(qf_color_obj) {
    qf_allow_transparent = (qf_color_obj.onfocus.toString().indexOf('QF_setColorTbl(this, true);') > -1);
}

function QF_changeColor(ofld) {

    closeInfoBubble();
    var qf_new_color = false;

    if (qf_color_fld == null || ofld.id != qf_color_fld.id || ofld.value != qf_color) {
        qf_color_tbl = ofld.parentNode.parentNode.parentNode.parentNode;
        qf_color_fld = ofld;
        var qf_tmp_color = ofld.value;

        QF_update_allow_transparent(qf_color_fld);

        var trans_str = 'transparent', is_transparent = false;

        if (qf_allow_transparent) {
            qf_new_color = trans_str;
            is_transparent = true;
        }

        if (qf_tmp_color.length) {
            if (qf_tmp_color.substr(0, 1) == '#') {
                if (qf_tmp_color.length == 7) {
                    try {
                        if (ns && qf_tmp_color.match(/[#a-f0-9]{7}/i) == null) {
                            customInfoBubble(ofld, qf_tmp_color  + ' is an invalid color');
                            return false;
                        } else {
                            qf_new_color = qf_tmp_color;
                        }
                    } catch (e) {
                        customInfoBubble(ofld, qf_tmp_color  + ' is an invalid color');
                    }
                }
            } else if (qf_tmp_color == trans_str) {
                ofld.value = '';
            } else {
                customInfoBubble(ofld, 'Colors must start with #');
            }
        }
    }

    if (qf_new_color !== false && qf_new_color != qf_color) {
        // Color update worked (must be a valid color)
        qf_color = qf_new_color;
        qf_color_tbl.rows[0].cells[0].style.backgroundColor = qf_color;
        qf_color_tbl.rows[0].cells[0].style.backgroundImage = (qf_new_color == trans_str ? 'url(\'/assets/images/std/128x128/color_picker_transparent.gif\')' : 'none');

        // Run the event
        try {
            var cmd = qf_color_tbl.getAttribute('torun').replace('this.value', 'qf_color');
            eval(cmd);
        } catch (e) {
            //customInfoBubble(ofld, 'An error has occured');
        }
    } else {
        // BB-1227
        // When one tries to add / edit color for weblinks on the settings tab, if the color
        // selected is same for both the textboxes, then the new color is not
        // reflected before the textbox. Just a hack to get things done.
        qf_color = qf_new_color;
        qf_color_tbl.rows[0].cells[0].style.backgroundColor = qf_color;
        qf_color_tbl.rows[0].cells[0].style.backgroundImage = (qf_new_color == trans_str ? 'url(\'/assets/images/std/128x128/color_picker_transparent.gif\')' : 'none');
    }
}

function QF_openColorPopup(e) {
    if (qf_color_tbl) {
        QF_update_allow_transparent(qf_color_fld);
        var picker_path = document_root + '/wysiwyg/color_picker.php' + (qf_allow_transparent ? '?t' : '');

        if (ns) {
            owin = openWindow(e, picker_path, 173, 185);
            owin.focus();
        } else {
            c = showModalDialog(picker_path, qf_color_tbl, 'dialogWidth:' + getModalDialogWindowWidth(180) + '; dialogHeight:' + getModalDialogWindowHeight(230) + '; help:0; status:yes; scroll:no;');

            if (c) {
                qf_color_fld.value = c;
                QF_changeColor(qf_color_fld);
            }
        }
    }
}


function QF_setColorByFieldName(fld_name, color) {
    if (document.getElementById(fld_name)) {
        var ofld = document.getElementById(fld_name);
        ofld.value = color.toUpperCase();
        QF_changeColor(ofld);
    }
}


function QF_setColorTbl(color_tbl) {
    qf_color_tbl = color_tbl;
    qf_color_fld = qf_color_tbl.getElementsByTagName('INPUT')[0];
    qf_allow_transparent = (arguments.length > 1 && arguments[1] === true);
}


function QF_setColorValue(color_val) {
    if (qf_color_tbl) {
        qf_color_fld.value = color_val;
        qf_color_fld.focus();
        QF_changeColor(qf_color_fld);
    }
}






//
// Next / Prev Step Functions
//
function step(which, e){

    if(e) {
        obj = e.currentTarget;
    } else {
        obj = window.event.srcElement;
    }


    obj = (step.arguments.length==1) ? obj : false;

    if( document.getElementById('step_' + which) ){

        //at_step = which;

        if (obj != false) {
            oldTbl = getStepTable(obj);
            oldTbl.style.display = 'none';
        } else {
            hideStepTables();
        }

        tbl = document.getElementById('step_' + which);
        tbl.style.display = '';

        // Walk through each row and cell and look for the first field to bring focus to
        var fnd_fld_focus = false;
        for (var y=0; y<tbl.rows.length; y++) {
            for (var z=0; z<tbl.rows[y].cells.length; z++) {
                if (tbl.rows[y].cells[z].firstChild) {
                    if (typeof(tbl.rows[y].cells[z].firstChild.tagName) != "undefined") {

                        switch (tbl.rows[y].cells[z].firstChild.tagName.toLowerCase()) {
                            case 'input':
                                if (!fnd_fld_focus) {
                                    fld = tbl.rows[y].cells[z].firstChild;
                                    if ((fld.type == 'text') || (fld.type == 'file')) {
                                        fld.focus();
                                        fnd_fld_focus = true;
                                    }
                                }
                                break;
                            case 'select':
                            case 'textarea':
                                if (!fnd_fld_focus) {
                                    fnd_fld_focus = true;
                                    tbl.rows[y].cells[z].firstChild.focus();
                                }
                                break;
                        }
                    }
                }
            }
        }

    }


    // If you ever need a custom thing ran on step screens create a
    // function on the page and call it on_step, it will then get run
    // with each step and passed the step number.

    if (typeof(on_step) != "undefined") {
        on_step(which);
    }
}

function hideStepTables(){
    var all_tbls = document.getElementsByTagName('TABLE');
    for(i=0;i<all_tbls.length;i++){
        if(all_tbls[i].id.indexOf('step')!=-1) {
            all_tbls[i].style.display='none';
            all_tbls[i].style.display='';
            all_tbls[i].style.display='none';
        }
    }
}




function checkPasswords(doc){
    if (doc.password.value != doc.password_c.value) {
        alert('Passwords do not Match');
        return false;
    } else {
        return true;
    }
}



function getStepTable(par){
    while(par != null && par.id.indexOf('step')==-1){
        par = par.parentNode;
    }
    return par;

}





function showCcvPopup(e) {
	openWindow(e, '/assets/images/ccv_whatis.gif', 260,360);
}

function getCreditCardType(cc_num) {

    var card_type  = false;
    var first_four = parseInt(cc_num.substring(0,4));

    if (first_four == 5610) {
        card_type = BANKCARD;
    } else if ((first_four >= 5100) && (first_four <= 5599)) {
        card_type = MASTERCARD;
    } else if ((first_four >= 4000) && (first_four <= 4999)) {
        card_type = VISA;
    } else if (((first_four >= 3400) && (first_four <= 3499)) || ((first_four >= 3700) && (first_four <= 3799))) {
        card_type = AMEX;
    }

    return card_type;
}


function creditCardIsValid(cc_num, cc_type) {

    var total = 0;
    var flag  = 0;

    if (cc_num.length == 16 || cc_num.length == 15) {
        var j = 0;
        for(var i = (cc_num.length - 1); i >= 0; i--) {
            j++;
            if ((j % 2) == 0) {
                var digits = parseInt(cc_num.charAt(i)) * 2;
                if (digits > 9) {
                    digits -= 9;
                }

                total += digits;
            } else {
                total = total + parseInt(cc_num.charAt(i));
            }
        }

        if ((total % 10) == 0) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }

}



function skipNextField(obj,e) {
    if((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105)) {
        if(obj.value.length == obj.size) {
            next_box = obj.nextSibling;
            while(next_box.tagName  != 'INPUT') {
                next_box = next_box.nextSibling;
            }
            next_box.focus();
        }
    }

}



// toggle relevant disable div when switching tabs on a tabbed qf:
function qfSwitchTab(tab) {
    if(typeof(qf_disable_div_tabs) != 'undefined') {
        for (var i in qf_disable_div_tabs) {
            if (typeof(qf_disable_div_tabs[i]) == 'function') continue;
            if (qf_disable_div_tabs[i] == tab) {

                var check = document.getElementById("toggle_" + i);
                var div = qf_disable_divs[i];

                var cb_checked = false;

                if (!check) {

                    check = document.getElementById(i);
                    if(!check) {
                        // no element found, continue;
                        continue;
                    }

                    // :SHONK:
                    // if were not using toggle_$id, then reversed the checkedness.

                    cb_checked = check.checked;

                } else {

                    // normal checkedness - qf toggle checkbox.
                    cb_checked = !check.checked;

                }


                if (cb_checked) {
                    div.show();
                } else {
                    div.hide();
                }
            } else {
                var div = qf_disable_divs[i]; div.hide();
            }
        }
    }
}
