function handleClick(obj) {
    if (obj.id == 'qr_a_0') {
        // print ticket
        submitFrm(document.forms['code_form']);
        window.open('ticket.html?' + escape($('#AgileCODE').get(0).src));
    }
}


function randomString() {
        var chars = "abcdefghiklmnopqrstuvwxyz";
        var string_length = 8;
        var randomstring = '';
        for (var i=0; i<string_length; i++) {
                var rnum = Math.floor(Math.random() * chars.length);
                randomstring += chars.substring(rnum,rnum+1);
        }
        return randomstring;
}

function updateImage(val, send_email) {
    if (send_email == undefined) send_email = '';

    var p = sel_item == 'qr_cnt_type_map' ? '/mappy_proxy.php' : '/img.php';

    var url = p  + '?e=' + send_email + '&d=' + escape(val);

    $('#AgileCODE').get(0).src = url;

    if (sel_item == 'qr_cnt_type_map') {
        $('#AgileCODE').get(0).style.width = '180px';
        $('#AgileCODE').get(0).style.height = '180px';
    } else {
        $('#AgileCODE').get(0).style.width = '';
        $('#AgileCODE').get(0).style.height = '';
    }

    $('#permalink').get(0).href = url;
    $('#img_link').get(0).href = url;

    $('#txt_dyn').get(0).innerHTML = '&lt;img src="http://theurl.cc/' + randomString() + '" alt="AgileCODES"  /&gt;';
}

function initImage() {
    updateImage('AgileCODES');
}

function submitFrm(frm) {
    if (formatcontent(frm)) {
        updateImage(frm.qr_cnt_frmt.value, $('#send_email').get(0).checked ? 'Y' : '');
    }
    return false;
}

function addInputRow(lbl) {
    var num = $('#options_tbl tr').length;
    var idd = 'qr_cnt_' + num;

    addInputRowHtml(idd, lbl, '<input class="txt" type="text" value="" name="' + idd + '" id="' + idd + '">');
}

function addInputRowHtml(idd, lbl, html_txt) {
    $('#options_tbl_tbody').append('<tr><td><label for="' + idd + '">' + lbl + '</label></td><td>' + html_txt + '</td></tr>');
}

function cct(idd) {
        sel_item = idd;

        var cnt = $('#options_tbl_tbody').get(0);
        while (cnt.firstChild) {
                cnt.removeChild(cnt.firstChild);
        }

        switch(idd) {
                case 'qr_cnt_type_sms':
                        addInputRow('Nr');
                        addInputRowHtml('qr_cnt_1', 'Message', '<textarea id="qr_cnt_1" name="qr_cnt_1" onkeydown="textCounter(this,160)" onkeyup="textCounter(this,160)"></textarea><br><span id="counter"><strong id="cc">160</strong> characters left</span>');
                break;

                case 'qr_cnt_type_text':
                        addInputRowHtml('qr_cnt_0', 'Free text', '<textarea id="qr_cnt_0" name="qr_cnt_0" onkeydown="textCounter(this,160)" onkeyup="textCounter(this,160)"></textarea><br><span id="counter"><strong id="cc">250</strong> characters left</span>');
                break;

                case 'qr_cnt_type_tel':
                        addInputRow('Phone number');
                break;

                case 'qr_cnt_type_url':
                        addInputRow('URL');
                break;

                case 'qr_cnt_type_ticket':
                        addInputRowHtml('qr_cnt_0', 'Ticket Type', '<select class="txt"><option value="General">General<option value="VIP">VIP<option value="Press">Press</select>');
                        addInputRow('Show');
                        addInputRow('Venue');
                        addInputRow('Seat');
                        addInputRow('Price');

                        /*newl = document.createTextNode('View Ticket');

                        newo=document.createElement('a');
                        newo.setAttribute('href', 'javascript:void(0)');
                        if (isIE) {
                            newo.onclick = function() { handleClick(this); }
                        } else {
                            newo.setAttribute('onclick','handleClick(this);');
                        }
                        newo.name='qr_a_0';
                        newo.id='qr_a_0';

                        newo.appendChild(newl);
                        cnt.appendChild(newo);*/
                break;

                case 'qr_cnt_type_voucher':
                    addInputRow('Description');
                    addInputRow('Amount');
                    addInputRow('Expiration');
                    addInputRow('Transaction #');
                break;

                case 'qr_cnt_type_vcard':
                    addInputRow('Title');
                    addInputRow('First Name');
                    addInputRow('Last Name');
                    addInputRow('Organization');
                    addInputRow('Email');
                    addInputRow('Telephone');
                    addInputRow('URL');
                    addInputRow('Address');
                break;

                case 'qr_cnt_type_map':
                    addInputRow('Address');
                    addInputRow('Zip');
                break;

                default:
                break;

        }

        $('#options_tbl tbody tr:even').addClass('even');
}

