﻿var currencysign = "RM ";
var maxseatoption = 6;

function contactinfo() {
    this.name = null;
    this.email = null;
    this.contactno = null;

    this.newcontact = newcontact;
    function newcontact(n, e, c) {
        this.name = n;
        if (e.indexOf("proxymail.facebook.com") < 0) this.email = e;
        else this.email = "";
        this.contactno = c;
    }
}

function ticketinfo() {
    this.tblwidth = "480px";
    this.namewidth = "165px";
    this.emailwidth = "170px";
    this.contactwidth = "100px";
    
    this.Adultclassid = 1;
    this.Disabledclassid = 2;
    this.Seniorclassid = 3;
    this.Studentclassid = 4;

    this.eventid = null;
    this.ticketclassid = null;
    this.showtimeid = null;
    this.ticketclassname = null;
    this.showdate = null;
    this.showtime = null;
    this.seatavailable = null;
    this.adultprice = null;
    this.disabledprice = null;
    this.seniorprice = null;
    this.studentprice = null;
    this.disablebooking = false;
    
    this.pickedadult = null;
    this.pickeddisabled = null;
    this.pickedsenior = null;
    this.pickedstudent = null;
    this.cfee = 0;
    this.canpay = false;
    
    this.contactinfoArr = null;

    this.initTicketInfo = initTicketInfo;
    function initTicketInfo(eventid, ticketclassid, showtimeid, ticketclassname, displayedshowdate, displayedshowtime, seatavailable,
        seriesmaxavailable, adultprice, disabledprice, seniorprice, studentprice, pickedadult, pickeddisabled, pickedsenior, pickedstudent, disablebooking) {
        
        this.eventid = eventid;
        this.ticketclassid = ticketclassid;
        this.showtimeid = showtimeid;
        this.ticketclassname = ticketclassname;
        this.showdate = displayedshowdate;
        this.showtime = displayedshowtime;
        this.seatavailable = seatavailable;
        this.seriesmaxavailable = seriesmaxavailable;
        this.adultprice = adultprice;
        this.disabledprice = disabledprice;
        this.seniorprice = seniorprice;
        this.studentprice = studentprice;

        this.pickedadult = pickedadult;
        this.pickeddisabled = pickeddisabled;
        this.pickedsenior = pickedsenior;
        this.pickedstudent = pickedstudent;

        this.disablebooking = disablebooking;
    }

    this.totalpicked = gettotalpicked;
    function gettotalpicked(typeid) {
        var returnval = parseInt(this.pickedadult, 10) + parseInt(this.pickeddisabled, 10) + parseInt(this.pickedsenior, 10) + parseInt(this.pickedstudent, 10);
        switch (parseInt(typeid)) {
            case this.Adultclassid: returnval -= parseInt(this.pickedadult, 10);
                break;
            case this.Disabledclassid: returnval -= parseInt(this.pickeddisabled, 10);
                break;
            case this.Seniorclassid: returnval -= parseInt(this.pickedsenior, 10);
                break;
            case this.Studentclassid: returnval -= parseInt(this.pickedstudent, 10);
                break;
        }
        return returnval;
    }

}

function buyticketcontrol() {

    this.freeticketcount = 1;
    this.freeticketcondition = 4;

    this.Adultclassid = 1;
    this.Disabledclassid = 2;
    this.Seniorclassid = 3;
    this.Studentclassid = 4;

    this.uniqueid = null;
    this.containerid = null;
    this.ticketinfoArr = null;
    this.totalPickedValue = null;
    this.totalPickedQty = null;
    this.viewcartflag = false;
    this.viewcartbuttonshown = false;
    this.defaultname = "";
    this.defaultemail = "";
    this.defaultcontactno = "";

    this.init = init;
    function init(uniqueid, parentcontainerid) {
        this.uniqueid = uniqueid;
        this.containerid = parentcontainerid;
        this.totalPickedValue = 0;
        this.totalPickedQty = 0;
        kakiseniWeb.ticketingAJAX.getdefaultcontactinfo(this.uniqueid, defaultcontact_callback);
    }

    this.defaultcontact_callback = defaultcontact_callback;
    function defaultcontact_callback(res) {
        if (res.value != null) {
            var temparr = res.value.split("{recordsep}");
            var controlobj = eval(temparr[0]);
            controlobj.defaultname = temparr[1];
            controlobj.defaultemail = temparr[2];
            controlobj.defaultcontactno = temparr[3];
        } 
    }

    this.resetSelector = resetSelector;
    function resetSelector(obj, eventid, sourcecontrolindex) {
        //index : 1 - date, 2 - time, 3 - qty
        var timeval = "";
        //var ticketqtys = "";
        if (sourcecontrolindex == 1) {
            var timeobj = document.getElementById("time_" + this.uniqueid);
            if (timeobj.value.length > 0) {
                timeval = timeobj.value;
                //ticketqtys = this.selectedticketqty();//eventid_timeid_classid{infieldsep}type{infieldsep}qty{fieldsep}
            }
            document.getElementById("time_" + this.uniqueid).parentNode.parentNode.innerHTML = this.buildTimeSelector(obj.value, eventid, timeval);
            if (document.getElementById("time_" + this.uniqueid).options[document.getElementById("time_" + this.uniqueid).selectedIndex].getAttribute("disablebooking") == "False") {
                timeval = document.getElementById("time_" + this.uniqueid).value;
                document.getElementById("tbl" + this.uniqueid).parentNode.innerHTML = this.buildClassSelector(timeval, eventid);
                this.showCurrentPrice();
                try {
                    document.getElementById(this.containerid).rows[2].style.display = "";
                } catch (ex) { }
                try {
                    document.getElementById("moreticketmsg").style.display = "";
                } catch (ex) { }
            } else {
                document.getElementById("tbl" + this.uniqueid).parentNode.innerHTML = this.waitlistmsghtml();
            }
        } else if (sourcecontrolindex == 2) {
            if (obj.options[obj.selectedIndex].getAttribute("disablebooking") == "False") {
                //ticketqtys = this.selectedticketqty(); //eventid_timeid_classid{infieldsep}type{infieldsep}qty{fieldsep}
                document.getElementById("tbl" + this.uniqueid).parentNode.innerHTML = this.buildClassSelector(obj.value, eventid);
                this.showCurrentPrice();
                try {
                    document.getElementById(this.containerid).rows[2].style.display = "";
                } catch (ex) { }
                try {
                    document.getElementById("moreticketmsg").style.display = "";
                } catch (ex) { }
            } else {
                document.getElementById("tbl" + this.uniqueid).parentNode.innerHTML = this.waitlistmsghtml();
            }
        } else if (sourcecontrolindex == 3) {
            this.showCurrentPrice(obj);
            if (!this.ticketinfoArr[0].canpay) {
                for (var i = 0; i < this.ticketinfoArr.length; i++) {
                    if (this.ticketinfoArr[i].eventid + "_" + this.ticketinfoArr[i].showtimeid == eventid) {
                        this.resetseatcontacts(i, eventid, obj);
                        break;
                    }
                }
            }
        }
    }

    this.waitlistmsghtml = waitlistmsghtml;
    function waitlistmsghtml() {
        try {
            document.getElementById(this.containerid).rows[2].style.display = "none";
        } catch (ex) { }
        try {
            document.getElementById("moreticketmsg").style.display = "none";
        } catch (ex) { }
        return "<table id=\"tbl" + this.uniqueid + "\" style=\"width:100%;\"><tr><td><div style=\"float:right;padding-right:10px;padding-top:12px;color:orange;text-align:justify;\" id=\"loginmsgdiv\">Booking for this show time is no longer available. But you are strongly advised to go for the show anyway. Unclaimed bookings are released 30 mins before showtime. So we'll do our best to find you a seat!</div></td></tr></table>";
    }

    this.resetseatcontacts = resetseatcontacts;
    function resetseatcontacts(index, selectedid, obj) {
        var tblobj = document.getElementById("tbl" + this.uniqueid);
        var parentRowIndex = obj.parentNode.parentNode.rowIndex;
        var colspansize = obj.parentNode.parentNode.cells.length;
        var totalpicked = this.ticketinfoArr[index].totalpicked(obj.getAttribute("type")) + parseInt(obj.value,10);
        var i=0;
        var currIndex = 0;
        while (++i <= totalpicked) {
            currIndex = parentRowIndex + i;
            var newcell;
            if (tblobj.rows[currIndex].cells.length > 1) {
                var newrow = tblobj.insertRow(currIndex);
                newcell = newrow.insertCell(0);
                newcell.colSpan = colspansize;
                newcell.style.textAlign = "center";
            } else {
                newcell = tblobj.rows[currIndex].cells[0];
            }
            var name = "", email = "", contactno = "";
            if (this.ticketinfoArr[index].contactinfoArr != null) {
                if (this.ticketinfoArr[index].contactinfoArr.length >= i) {
                    name = this.ticketinfoArr[index].contactinfoArr[i - 1].name;
                    email = this.ticketinfoArr[index].contactinfoArr[i - 1].email;
                    contactno = this.ticketinfoArr[index].contactinfoArr[i - 1].contactno;
                } else if ((i>1) && (this.lazyentry)) {
                    name = this.ticketinfoArr[index].contactinfoArr[0].name;
                    email = this.ticketinfoArr[index].contactinfoArr[0].email;
                    contactno = this.ticketinfoArr[index].contactinfoArr[0].contactno;
                }
            } else if ((i==1) || (this.lazyentry)) {
                name = this.defaultname;
                email = this.defaultemail;
                contactno = this.defaultcontactno;
            }
            var tempstr = "";
            if (name.length == 0) {
                tempstr += "<input actualcontrol=\"0\" onfocus=\"traceact(this);\" type=\"text\" style=\"width:" + this.namewidth + ";color:silver;\" onkeypress=\"return false\" value=\"  Name\" id=\"name" + selectedid + "_" + i + "f\" />";
                tempstr += "<input actualcontrol=\"1\" onblur=\"tracefront(this);\" type=\"text\" style=\"width:" + this.namewidth + ";display:none;\" maxlength=\"100\" value=\"" + name + "\" id=\"name" + selectedid + "_" + i + "\" />&nbsp;&nbsp;&nbsp;";
                tempstr += "<input actualcontrol=\"0\" onfocus=\"traceact(this);\" type=\"text\" style=\"width:" + this.emailwidth + ";color:silver;\" onkeypress=\"return false\" value=\"  Email\" id=\"email" + selectedid + "_" + i + "f\" />";
                tempstr += "<input actualcontrol=\"1\" onblur=\"tracefront(this);\" type=\"text\" style=\"width:" + this.emailwidth + ";display:none;\" maxlength=\"255\" value=\"" + email + "\" id=\"email" + selectedid + "_" + i + "\" />&nbsp;&nbsp;&nbsp;";
                tempstr += "<input actualcontrol=\"0\" onfocus=\"traceact(this);\" type=\"text\" style=\"width:" + this.contactwidth + ";color:silver;\" onkeypress=\"return false\" value=\"  Mobile No.\" id=\"contactno" + selectedid + "_" + i + "f\" />";
                tempstr += "<input actualcontrol=\"1\" onblur=\"tracefront(this);\" type=\"text\" style=\"width:" + this.contactwidth + ";display:none;\" maxlength=\"50\" value=\"" + contactno + "\" id=\"contactno" + selectedid + "_" + i + "\" />";
            } else {
                tempstr += "<input actualcontrol=\"0\" onfocus=\"traceact(this);\" type=\"text\" style=\"width:" + this.namewidth + ";color:silver;display:none;\" onkeypress=\"return false\" value=\"  Name\" id=\"name" + selectedid + "_" + i + "f\" />";
                tempstr += "<input actualcontrol=\"1\" onblur=\"tracefront(this);\" type=\"text\" style=\"width:" + this.namewidth + ";\" maxlength=\"100\" value=\"" + name + "\" id=\"name" + selectedid + "_" + i + "\" />&nbsp;&nbsp;&nbsp;";
                tempstr += "<input actualcontrol=\"0\" onfocus=\"traceact(this);\" type=\"text\" style=\"width:" + this.emailwidth + ";color:silver;display:none;\" onkeypress=\"return false\" value=\"  Email\" id=\"email" + selectedid + "_" + i + "f\" />";
                tempstr += "<input actualcontrol=\"1\" onblur=\"tracefront(this);\" type=\"text\" style=\"width:" + this.emailwidth + ";\" maxlength=\"255\" value=\"" + email + "\" id=\"email" + selectedid + "_" + i + "\" />&nbsp;&nbsp;&nbsp;";
                if (contactno.length == 0) {
                    tempstr += "<input actualcontrol=\"0\" onfocus=\"traceact(this);\" type=\"text\" style=\"width:" + this.contactwidth + ";color:silver;\" onkeypress=\"return false\" value=\"  Mobile No.\" id=\"contactno" + selectedid + "_" + i + "f\" />";
                    tempstr += "<input actualcontrol=\"1\" onblur=\"tracefront(this);\" type=\"text\" style=\"width:" + this.contactwidth + ";display:none;\" maxlength=\"50\" value=\"" + contactno + "\" id=\"contactno" + selectedid + "_" + i + "\" />";
                } else {
                tempstr += "<input actualcontrol=\"0\" onfocus=\"traceact(this);\" type=\"text\" style=\"width:" + this.contactwidth + ";color:silver;display:none;\" onkeypress=\"return false\" value=\"  Mobile No.\" id=\"contactno" + selectedid + "_" + i + "f\" />";
                tempstr += "<input actualcontrol=\"1\" onblur=\"tracefront(this);\" type=\"text\" style=\"width:" + this.contactwidth + ";\" maxlength=\"50\" value=\"" + contactno + "\" id=\"contactno" + selectedid + "_" + i + "\" />";
                }
            }
            if (i == 1) {
                var tempstyle = "";
                if (totalpicked == 1) tempstyle = "display:none;";
                if (this.lazyentry)
                    tempstr += "<br/><div id=\"chk_" + this.uniqueid + "\" style=\"float:left;" + tempstyle + "\"><input type=\"checkbox\" id=\"lazyentry_" + this.uniqueid + "\" checked=\"true\" onclick=\"" + this.uniqueid + ".togglelazyentry(this.checked, this);\" /> <label for=\"lazyentry_" + this.uniqueid + "\">Copy my details into the rows below</label></div>";
                else
                    tempstr += "<br/><div id=\"chk_" + this.uniqueid + "\" style=\"float:left;" + tempstyle + "\"><input type=\"checkbox\" id=\"lazyentry_" + this.uniqueid + "\" onclick=\"" + this.uniqueid + ".togglelazyentry(this.checked, this);\" /> <label for=\"lazyentry_" + this.uniqueid + "\">Copy my details into the rows below</label></div>";
                tempstr = "<span style=\"white-space:nowrap;text-align:left;\">" + tempstr + "</span>";
            }
            if (totalpicked - i > 0) tempstr += "<div class=\"hozline_thin_dotted\" style=\"margin-bottom:0px;\"></div>";
            if (newcell.innerHTML.length == 0) newcell.innerHTML = tempstr;
            else if (i == 1) {
                if (totalpicked == 1) document.getElementById("chk_" + this.uniqueid).style.display = "none";
                else document.getElementById("chk_" + this.uniqueid).style.display = "";
            }
        }
        i += parentRowIndex;
        while (i<tblobj.rows.length) {
            if (tblobj.rows[i].cells.length == 1) {
                tblobj.deleteRow(i);
            } else {
                break;
            }
        }
    }

    this.togglelazyentry = togglelazyentry;
    function togglelazyentry(flag, obj) {
        this.lazyentry = flag;
        if (flag) {
//            alert(obj.parentNode.tagName);
//            alert(obj.parentNode.parentNode.tagName);
//            alert(obj.parentNode.parentNode.parentNode.tagName);
            var parentobj = obj.parentNode.parentNode.parentNode;
            var tboxes = parentobj.getElementsByTagName("input");
            var name = "", email = "", mobile = "";
            name += tboxes[1].value;
            email += tboxes[3].value;
            mobile += tboxes[5].value;
            var tblobj = document.getElementById("tbl" + this.uniqueid);
            for (var i = parentobj.parentNode.rowIndex; i < tblobj.rows.length - 1; i++) {
                if (tblobj.rows[i].cells.length == 1 ) {
                    var tboxes = tblobj.rows[i].cells[0].getElementsByTagName("input");
                    if ((tboxes[1].value.length == 0) && (tboxes[3].value.length == 0) && (tboxes[5].value.length == 0)) {
                        if (name.length > 0) {
                            tboxes[1].value = name;
                            tboxes[0].style.display = "none";
                            tboxes[1].style.display = "";
                        }
                        if (email.length > 0) {
                            tboxes[3].value = email;
                            tboxes[2].style.display = "none";
                            tboxes[3].style.display = "";
                        }
                        if (mobile.length > 0) {
                            tboxes[5].value = mobile;
                            tboxes[4].style.display = "none";
                            tboxes[5].style.display = "";
                        }
                    }
                }
            }
        }
    }
    
    this.showCurrentPrice = showCurrentPrice;
    function showCurrentPrice(obj) {
//        if (obj) {
//            //replace(currencysign,"").myParseFloat()
//        } else {
        //        }
        var tblobj = document.getElementById("tbl" + this.uniqueid);
        if (tblobj) {
            var rowtotal = 0;
            var eventtotal = 0;
            var eventQty = 0;

            //hardcode: only able handle 2 free tick (lowest price from selection)
            var prices = new Array();
            var controlint = 1;
            var colcount = 1;
            if (document.getElementById("discountrow_" + +this.uniqueid)) ++controlint;
            //hardcode: only able handle 2 free tick (lowest price from selection)

            try {
                document.getElementById("studentmsgspan").style.display = "none";
            } catch (ex) { }
            for (var i = 1; i < tblobj.rows.length - controlint; i++) {
                var currrow = tblobj.rows[i];
                rowtotal = 0;
                colcount = currrow.cells.length;
                for (var j = 1; j < currrow.cells.length - 1; j++) {
                    var currqty = parseInt(currrow.cells[j].firstChild.value, 10);
                    if (currqty > 0) {
                        var price = currrow.cells[j].firstChild.getAttribute("price").myParseFloat();
                        rowtotal += currrow.cells[j].firstChild.value.myParseFloat() * price;
                        eventQty += currqty;
                        if (currrow.cells[j].firstChild.getAttribute("type") == this.Studentclassid) {
                            try {
                                document.getElementById("studentmsgspan").style.display = "";
                            } catch (ex) { }
                        }
                        if (prices.length == 0) {
                            for (var l = 0; l < parseInt(currrow.cells[j].firstChild.value, 10); l++) {
                                prices.splice(0, 0, price);
                            }
                        } else {
                            for (var k = 0; k < prices.length; k++) {
                                if (prices[k] > price) {
                                    for (var l = 0; l < parseInt(currrow.cells[j].firstChild.value, 10); l++) {
                                        prices.splice(k, 0, price);
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
                currrow.cells[currrow.cells.length - 1].innerHTML = currencysign + roundVal(rowtotal, false);
                eventtotal += rowtotal;
            }

            //hardcode: only able handle 2 free tick (lowest price from selection)
            var discountVal = 0;
            if ((this.freeticketcount > 0) && (eventQty >= this.freeticketcondition)) {
                var freeqty = Math.floor(eventQty / this.freeticketcondition);
                for (var i = 0; i < freeqty; i++) {
                    discountVal += prices[i];
                }
                eventtotal -= discountVal;
            }
            if (discountVal > 0) {
                if (!document.getElementById("discountrow_" + +this.uniqueid)) {
                    var currrow = tblobj.insertRow(tblobj.rows.length - 1);
                    currrow.setAttribute("id", "discountrow_" + +this.uniqueid);
                    var currcell = currrow.insertCell(0);
                    currcell.colSpan = colcount - 1;
                    currcell.style.textAlign = "right";
                    currcell.className = "total";
                    currcell.innerHTML = "Discount";
                    
                    currcell = currrow.insertCell(1);
                    currcell.style.textAlign = "right";
                    currcell.className = "total";
                    currcell.style.whiteSpace = "nowrap";
                }
                var currrow = document.getElementById("discountrow_" + +this.uniqueid);
                currrow.cells[currrow.cells.length - 1].innerHTML = "- " + currencysign + roundVal(discountVal, false);
            } else {
                if (document.getElementById("discountrow_" + +this.uniqueid)) {
                    tblobj.deleteRow(document.getElementById("discountrow_" + +this.uniqueid).rowIndex);
                }
            }
            //hardcode: only able handle 2 free tick (lowest price from selection)

            tblobj.rows[tblobj.rows.length - 1].cells[tblobj.rows[tblobj.rows.length - 1].cells.length - 1].innerHTML = currencysign + roundVal(eventtotal, false);
            this.totalPickedValue = eventtotal;
            this.totalPickedQty = eventQty;
            if (this.viewcartflag) updatetotalcart();
        }
    }

    this.buildDateSelector = buildDateSelector;
    function buildDateSelector(eventid) {
        var prevdate = "";
        var dateoption = "";
        for (var i = 0; i < this.ticketinfoArr.length; i++) {
            if (prevdate != this.ticketinfoArr[i].showdate) {
                prevdate = this.ticketinfoArr[i].showdate;
                dateoption += "<option value=\"" + prevdate + "\"";
                if (this.ticketinfoArr[i].pickedadult > 0 || this.ticketinfoArr[i].pickeddisabled > 0 || this.ticketinfoArr[i].pickedsenior > 0 ||
                    this.ticketinfoArr[i].pickedstudent > 0) dateoption += " selected";
                dateoption += ">" + prevdate + "</option>";
            }
        }
        dateoption = "<select onchange=\"makecartchange(" + eventid + ");" + this.uniqueid + ".resetSelector(this," + eventid + ",1);\" name=\"date_" + this.uniqueid + "\" id=\"date_" + this.uniqueid + "\">" + dateoption + "</select>";
        return dateoption;
    }

    this.buildTimeSelector = buildTimeSelector;
    function buildTimeSelector(selecteddate, eventid, defaultvalue) {
        var timeoption = "";
        var prevshowtimeid = "";
        var sumavai = 0;
        if (selecteddate.length > 0) {
            for (var i = 0; i < this.ticketinfoArr.length; i++) {
                if (this.ticketinfoArr[i].showdate == selecteddate) {
                    if (prevshowtimeid != this.ticketinfoArr[i].showtimeid) {
                        if (prevshowtimeid.length > 0) {
                            if (parseInt(sumavai, 10) < maxseatoption) timeoption = timeoption.replace("{sumavai}", sumavai);
                            else timeoption = timeoption.replace("{sumavai}", ">" + maxseatoption);
                        }
                        timeoption += "<option disablebooking=\"" + this.ticketinfoArr[i].disablebooking + "\" value=\"" + this.ticketinfoArr[i].eventid + "_" + this.ticketinfoArr[i].showtimeid + "\"";
                        if (defaultvalue == this.ticketinfoArr[i].eventid + "_" + this.ticketinfoArr[i].showtimeid) timeoption += " selected";
                        timeoption += ">" + this.ticketinfoArr[i].showtime + "&nbsp;&nbsp;&nbsp;";
                        if (this.ticketinfoArr[i].disablebooking == "False") timeoption += "Seats Avai:{sumavai}</option>";
                        else timeoption += "Wait List On Site Only</option>";
                        sumavai = parseInt(this.ticketinfoArr[i].seatavailable, 10);
                        prevshowtimeid = this.ticketinfoArr[i].showtimeid;
//                    } else {
//                        sumavai += parseInt(this.ticketinfoArr[i].seatavailable, 10);
                    }
                }
            }
            if (prevshowtimeid.length > 0) {
                if (parseInt(sumavai, 10) < parseInt(maxseatoption,10)) timeoption = timeoption.replace("{sumavai}", sumavai);
                else timeoption = timeoption.replace("{sumavai}", ">" + maxseatoption);
            }
        } else {
            timeoption = "<option>--pls select date--</option>";
        }
        timeoption = "<select onchange=\"makecartchange(" + eventid + ");" + this.uniqueid + ".resetSelector(this," + eventid + ",2);\" name=\"time_" + this.uniqueid + "\" id=\"time_" + this.uniqueid + "\">" + timeoption + "</select>";
        //alert(timeoption);
        timeoption = "<div style=\"float:left;\">" + timeoption + "</div><div style=\"float:right;\">";
//        if (this.lazyentry) {
//            timeoption += "<input type=\"checkbox\" id=\"lazyentry_" + this.uniqueid + "\" checked=\"true\" onclick=\"" + this.uniqueid + ".lazyentry=this.checked;\" /> <label for=\"lazyentry_" + this.uniqueid + "\">Lazy Entry</label>";
//        } else {
//            timeoption += "<input type=\"checkbox\" id=\"lazyentry_" + this.uniqueid + "\" onclick=\"" + this.uniqueid + ".lazyentry=this.checked;\"/> <label for=\"lazyentry_" + this.uniqueid + "\">Lazy Entry</label>";
//        }
        timeoption += "&nbsp;</div>";
        return timeoption;
    }

    this.lazyentry = false;
    
    this.buildClassSelector = buildClassSelector;
    function buildClassSelector(selectedid, eventid) {//eventid_timeid_classid{infieldsep}type{infieldsep}qty{fieldsep}
        var returnhtml = "";
        if (selectedid.length > 0) {
            var flag = false;
            var qtyselector = "";
            var rowqselector = "";
            var colspan = 1;
            var rowclass = "generictableevenrow";
            for (var i = 0; i < this.ticketinfoArr.length; i++) {
                if (this.ticketinfoArr[i].eventid + "_" + this.ticketinfoArr[i].showtimeid == selectedid) {
                    var defaultvalues = "";
                    if (this.ticketinfoArr[i].pickedadult > 0) {
                        defaultvalues = selectedid + "_" + this.ticketinfoArr[i].ticketclassid + "{infieldsep}" + this.Adultclassid + "{infieldsep}" + this.ticketinfoArr[i].pickedadult + "{fieldsep}";
                    }
                    if (this.ticketinfoArr[i].pickeddisabled > 0) {
                        defaultvalues += selectedid + "_" + this.ticketinfoArr[i].ticketclassid + "{infieldsep}" + this.Disabledclassid + "{infieldsep}" + this.ticketinfoArr[i].pickeddisabled + "{fieldsep}";
                    }
                    if (this.ticketinfoArr[i].pickedsenior > 0) {
                        defaultvalues += selectedid + "_" + this.ticketinfoArr[i].ticketclassid + "{infieldsep}" + this.Seniorclassid + "{infieldsep}" + this.ticketinfoArr[i].pickedsenior + "{fieldsep}";
                    }
                    if (this.ticketinfoArr[i].pickedstudent > 0) {
                        defaultvalues += selectedid + "_" + this.ticketinfoArr[i].ticketclassid + "{infieldsep}" + this.Studentclassid + "{infieldsep}" + this.ticketinfoArr[i].pickedstudent + "{fieldsep}";
                    }
                    if (!flag) {
                        var j = maxseatoption;
                        if (j > parseInt(this.ticketinfoArr[i].seatavailable, 10)) {
                            j = parseInt(this.ticketinfoArr[i].seatavailable, 10);
                        }
                        for (var k = 0; k <= j; k++) {
                            qtyselector += "<option value=\"" + k + "\">" + k + "</option>";
                        }
                        qtyselector = "<select onchange=\"makecartchange(" + eventid + ");" + this.uniqueid + ".resetSelector(this,'" + selectedid + "',3);\" {option}>" + qtyselector + "</select>";
                        returnhtml += "<tr class=\"generictabletitlerow\"><td>Seat Type</td>";
                        if (this.ticketinfoArr[i].adultprice >= 0) {
                            ++colspan;
                            returnhtml += "<td>Adult</td>";
                            rowqselector += "<td>" + qtyselector.replace("{option}", "{option} type=\"" + this.Adultclassid + "\" ") + "</td>";
                        }
                        if (this.ticketinfoArr[i].disabledprice >= 0) {
                            ++colspan;
                            returnhtml += "<td>Disabled</td>";
                            rowqselector += "<td>" + qtyselector.replace("{option}", "{option} type=\"" + this.Disabledclassid + "\" ") + "</td>";
                        }
                        if (this.ticketinfoArr[i].seniorprice >= 0) {
                            ++colspan;
                            returnhtml += "<td>Senior</td>";
                            rowqselector += "<td>" + qtyselector.replace("{option}", "{option} type=\"" + this.Seniorclassid + "\" ") + "</td>";
                        }
                        if (this.ticketinfoArr[i].studentprice >= 0) {
                            ++colspan;
                            returnhtml += "<td>Student</td>";
                            rowqselector += "<td>" + qtyselector.replace("{option}", "{option} type=\"" + this.Studentclassid + "\" ") + "</td>";
                        }
                        returnhtml += "<td";
                        if (this.ticketinfoArr[i].canpay == false) returnhtml += " style=\"display:none;\"";
                        returnhtml += ">&nbsp;</td></tr>";
                        flag = true;
                    }
                    if (rowclass == "generictableevenrow") rowclass = "generictableoddrow";
                    else rowclass = "generictableevenrow";
                    returnhtml += "<tr class=\"" + rowclass + "\"><td>" + this.ticketinfoArr[i].ticketclassname + "</td>";
                    //_classid{infieldsep}type
                    var tempstr = rowqselector;
                    if (defaultvalues.length > 0) {
                        var controlstr = "_" + this.ticketinfoArr[i].ticketclassid + "{infieldsep}" + this.Adultclassid + "{infieldsep}";
                        var tempint = defaultvalues.indexOf(controlstr);
                        if (tempint > 0) {
                            tempint += controlstr.length;
                            var selectedqty = defaultvalues.substr(tempint, defaultvalues.indexOf("{", tempint) - tempint);
                            tempstr = tempstr.replace(qtyselector.replace("{option}", "{option} type=\"" + this.Adultclassid + "\" "),
                            qtyselector.replace("{option}", "{option} type=\"" + this.Adultclassid + "\" ").replace("<option value=\"" +
                            selectedqty + "\">", "<option value=\"" + selectedqty + "\" selected>"));
                        }
                        var controlstr = "_" + this.ticketinfoArr[i].ticketclassid + "{infieldsep}" + this.Disabledclassid + "{infieldsep}";
                        var tempint = defaultvalues.indexOf(controlstr);
                        if (tempint > 0) {
                            tempint += controlstr.length;
                            var selectedqty = defaultvalues.substr(tempint, defaultvalues.indexOf("{", tempint) - tempint);
                            tempstr = tempstr.replace(qtyselector.replace("{option}", "{option} type=\"" + this.Disabledclassid + "\" "),
                            qtyselector.replace("{option}", "{option} type=\"" + this.Disabledclassid + "\" ").replace("<option value=\"" +
                            selectedqty + "\">", "<option value=\"" + selectedqty + "\" selected>"));
                        }
                        var controlstr = "_" + this.ticketinfoArr[i].ticketclassid + "{infieldsep}" + this.Seniorclassid + "{infieldsep}";
                        var tempint = defaultvalues.indexOf(controlstr);
                        if (tempint > 0) {
                            tempint += controlstr.length;
                            var selectedqty = defaultvalues.substr(tempint, defaultvalues.indexOf("{", tempint) - tempint);
                            tempstr = tempstr.replace(qtyselector.replace("{option}", "{option} type=\"" + this.Seniorclassid + "\" "),
                            qtyselector.replace("{option}", "{option} type=\"" + this.Seniorclassid + "\" ").replace("<option value=\"" +
                            selectedqty + "\">", "<option value=\"" + selectedqty + "\" selected>"));
                        }
                        var controlstr = "_" + this.ticketinfoArr[i].ticketclassid + "{infieldsep}" + this.Studentclassid + "{infieldsep}";
                        var tempint = defaultvalues.indexOf(controlstr);
                        if (tempint > 0) {
                            tempint += controlstr.length;
                            var selectedqty = defaultvalues.substr(tempint, defaultvalues.indexOf("{", tempint) - tempint);
                            tempstr = tempstr.replace(qtyselector.replace("{option}", "{option} type=\"" + this.Studentclassid + "\" "),
                            qtyselector.replace("{option}", "{option} type=\"" + this.Studentclassid + "\" ").replace("<option value=\"" +
                            selectedqty + "\">", "<option value=\"" + selectedqty + "\" selected>"));
                        }
                    } else {
                        var selectedqty = this.ticketinfoArr[i].pickedadult;
                        if (parseInt(selectedqty, 10) > 0) {
                            tempstr = tempstr.replace(qtyselector.replace("{option}", "{option} type=\"" + this.Adultclassid + "\" "),
                                qtyselector.replace("{option}", "{option} type=\"" + this.Adultclassid + "\" ").replace("<option value=\"" +
                                selectedqty + "\">", "<option value=\"" + selectedqty + "\" selected>"));
                        }
                        var selectedqty = this.ticketinfoArr[i].pickeddisabled;
                        if (parseInt(selectedqty, 10) > 0) {
                            tempstr = tempstr.replace(qtyselector.replace("{option}", "{option} type=\"" + this.Disabledclassid + "\" "),
                                qtyselector.replace("{option}", "{option} type=\"" + this.Disabledclassid + "\" ").replace("<option value=\"" +
                                selectedqty + "\">", "<option value=\"" + selectedqty + "\" selected>"));
                        }
                        var selectedqty = this.ticketinfoArr[i].pickedsenior;
                        if (parseInt(selectedqty, 10) > 0) {
                            tempstr = tempstr.replace(qtyselector.replace("{option}", "{option} type=\"" + this.Seniorclassid + "\" "),
                                qtyselector.replace("{option}", "{option} type=\"" + this.Seniorclassid + "\" ").replace("<option value=\"" +
                                selectedqty + "\">", "<option value=\"" + selectedqty + "\" selected>"));
                        }
                        var selectedqty = this.ticketinfoArr[i].pickedstudent;
                        if (parseInt(selectedqty, 10) > 0) {
                            tempstr = tempstr.replace(qtyselector.replace("{option}", "{option} type=\"" + this.Studentclassid + "\" "),
                                qtyselector.replace("{option}", "{option} type=\"" + this.Studentclassid + "\" ").replace("<option value=\"" +
                                selectedqty + "\">", "<option value=\"" + selectedqty + "\" selected>"));
                        }
                    }

                    if (this.ticketinfoArr[i].adultprice >= 0) {
                        tempstr = tempstr.replace(" type=\"" + this.Adultclassid + "\"", " type=\"" +
                            this.Adultclassid + "\" price=\"" + this.ticketinfoArr[i].adultprice + "\"");
                    }
                    if (this.ticketinfoArr[i].disabledprice >= 0) {
                        tempstr = tempstr.replace(" type=\"" + this.Disabledclassid + "\"", " type=\"" +
                            this.Disabledclassid + "\" price=\"" + this.ticketinfoArr[i].disabledprice + "\"");
                    }
                    if (this.ticketinfoArr[i].seniorprice >= 0) {
                        tempstr = tempstr.replace(" type=\"" + this.Seniorclassid + "\"", " type=\"" +
                            this.Seniorclassid + "\" price=\"" + this.ticketinfoArr[i].seniorprice + "\"");
                    }
                    if (this.ticketinfoArr[i].studentprice >= 0) {
                        tempstr = tempstr.replace(" type=\"" + this.Studentclassid + "\"", " type=\"" +
                            this.Studentclassid + "\" price=\"" + this.ticketinfoArr[i].studentprice + "\"");
                    }
                    returnhtml += tempstr.replace(/{option}/g, "key=\"" +
                        this.ticketinfoArr[i].eventid + "_" + this.ticketinfoArr[i].showtimeid + "_" + this.ticketinfoArr[i].ticketclassid +
                        "\"") + "<td style=\"text-align:right;width:80px;";
                    if (this.ticketinfoArr[i].canpay == false) returnhtml += "display:none;";
                    returnhtml += "\">" + currencysign + "0.00</td></tr>";

                    //show contact if any
                    if (this.ticketinfoArr[i].contactinfoArr != null) {
                        for (var k = 0; k < this.ticketinfoArr[i].contactinfoArr.length; k++) {
                            var tempstr = "";
                            tempstr += "<input actualcontrol=\"0\" onfocus=\"traceact(this);\" type=\"text\" style=\"width:" + this.namewidth + ";color:silver;display:none;\" onkeypress=\"return false\" value=\"  Name\" id=\"name" + selectedid + "_" + k + "f\" />";
                            tempstr += "<input actualcontrol=\"1\" onblur=\"tracefront(this);\" type=\"text\" style=\"width:" + this.namewidth + ";\" maxlength=\"100\" value=\"" + this.ticketinfoArr[i].contactinfoArr[k].name + "\" id=\"name" + selectedid + "_" + k + "\" />&nbsp;&nbsp;&nbsp;";
                            tempstr += "<input actualcontrol=\"0\" onfocus=\"traceact(this);\" type=\"text\" style=\"width:" + this.emailwidth + ";color:silver;display:none;\" onkeypress=\"return false\" value=\"  Email\" id=\"email" + selectedid + "_" + k + "f\" />";
                            tempstr += "<input actualcontrol=\"1\" onblur=\"tracefront(this);\" type=\"text\" style=\"width:" + this.emailwidth + ";\" maxlength=\"255\" value=\"" + this.ticketinfoArr[i].contactinfoArr[k].email + "\" id=\"email" + selectedid + "_" + k + "\" />&nbsp;&nbsp;&nbsp;";
                            if (this.ticketinfoArr[i].contactinfoArr[k].contactno.length == 0) {
                                tempstr += "<input actualcontrol=\"0\" onfocus=\"traceact(this);\" type=\"text\" style=\"width:" + this.contactwidth + ";color:silver;\" onkeypress=\"return false\" value=\"  Mobile No.\" id=\"contactno" + selectedid + "_" + k + "f\" />";
                                tempstr += "<input actualcontrol=\"1\" onblur=\"tracefront(this);\" type=\"text\" style=\"width:" + this.contactwidth + ";display:none;\" maxlength=\"50\" value=\"" + this.ticketinfoArr[i].contactinfoArr[k].contactno + "\" id=\"contactno" + selectedid + "_" + k + "\" />";
                            } else {
                                tempstr += "<input actualcontrol=\"0\" onfocus=\"traceact(this);\" type=\"text\" style=\"width:" + this.contactwidth + ";color:silver;display:none;\" onkeypress=\"return false\" value=\"  Mobile No.\" id=\"contactno" + selectedid + "_" + k + "f\" />";
                                tempstr += "<input actualcontrol=\"1\" onblur=\"tracefront(this);\" type=\"text\" style=\"width:" + this.contactwidth + ";\" maxlength=\"50\" value=\"" + this.ticketinfoArr[i].contactinfoArr[k].contactno + "\" id=\"contactno" + selectedid + "_" + k + "\" />";
                            }
                            if (k == 0) {
                                var tempstyle = "";
                                if (this.ticketinfoArr[i].contactinfoArr.length == 1) tempstyle = "display:none;";
                                if (this.lazyentry)
                                    tempstr += "<br/><div id=\"chk_" + this.uniqueid + "\" style=\"float:left;" + tempstyle + "\"><input type=\"checkbox\" id=\"lazyentry_" + this.uniqueid + "\" checked=\"true\" onclick=\"" + this.uniqueid + ".togglelazyentry(this.checked, this);\" /> <label for=\"lazyentry_" + this.uniqueid + "\">Copy my details into the rows below</label></div>";
                                else
                                    tempstr += "<br/><div id=\"chk_" + this.uniqueid + "\" style=\"float:left;" + tempstyle + "\"><input type=\"checkbox\" id=\"lazyentry_" + this.uniqueid + "\" onclick=\"" + this.uniqueid + ".togglelazyentry(this.checked, this);\" /> <label for=\"lazyentry_" + this.uniqueid + "\">Copy my details into the rows below</label></div>";
                                tempstr = "<span style=\"white-space:nowrap;text-align:left;\">" + tempstr + "</span>";
                            }
                            tempstr = "<tr><td colspan=\"" + colspan + "\" style=\"text-align:center;\">" + tempstr;
                            if (this.ticketinfoArr[i].contactinfoArr.length - k > 1) tempstr += "<div class=\"hozline_thin_dotted\" style=\"margin-bottom:0px;\"></div>";
                            tempstr += "</td></tr>";
                            returnhtml += tempstr;
                        } 
                    }
                }
            }
            if (returnhtml.length > 0) {
                returnhtml = "<table align=\"right\" style=\"width:100%;\" id=\"tbl" + this.uniqueid + "\" cellspacing=\"0\" cellpadding=\"2\">" + returnhtml;
                returnhtml += "<tr style=\"";
                if (this.ticketinfoArr[0].canpay == false) returnhtml += "display:none;";
                else returnhtml += "height:22px;";
                returnhtml += "\"><td style=\"border-top:1px solid silver;";
                if (this.ticketinfoArr[0].canpay == false) returnhtml += "display:none;";
                var temp = this.tblwidth.substr(0, this.tblwidth.length - 2);
                temp = parseFloat(temp) - 80;
                returnhtml += "\" colspan=\"" + colspan + "\" class=\"total\"><div style=\"white-space:nowrap;width:" + temp + "px;\">";
                if (this.ticketinfoArr[0].cfee.myParseFloat() > 0) {
                    returnhtml += "<div style=\"float:left;font-style:italic;font-size:10px;white-space:nowrap;\">Convenience Fee of " + currencysign + this.ticketinfoArr[0].cfee +
                        " is applicable on top of total ticket price</div>";
                }
                returnhtml += "<div style=\"float:right;\">TOTAL&nbsp;</div></div></td><td style=\"border-top:1px solid silver;text-align:right;";
                if (this.ticketinfoArr[0].canpay == false) returnhtml += "display:none;";
                returnhtml += "white-space:nowrap;\" class=\"total\">" + currencysign + "0.00</td></table>";
            }
        }
        //alert(returnhtml);
        return returnhtml;
    }

    this.recordPickedQty = recordPickedQty;
    function recordPickedQty() {
        var tblobj = document.getElementById("tbl" + this.uniqueid);
        for (var i = 1; i < tblobj.rows.length - 1; i++) {
            if (tblobj.rows[i].cells.length > 1) {
                var j = 0;
                while (tblobj.rows[i].cells[++j]) {
                    var obj = tblobj.rows[i].cells[j].firstChild;
                    //if (parseInt(obj.value, 10) > 0) {
                    if (obj.value) {
                        for (var k = 0; k < this.ticketinfoArr.length; k++) {
                            //alert("asd" + this.ticketinfoArr[k].eventid + "_" + this.ticketinfoArr[k].showtimeid);
                            //alert(obj.getAttribute("key"));
                            if (this.ticketinfoArr[k].eventid + "_" + this.ticketinfoArr[k].showtimeid + "_" + this.ticketinfoArr[k].ticketclassid == obj.getAttribute("key")) {
                                switch (obj.getAttribute("type")) {
                                    case this.Adultclassid:
                                        this.ticketinfoArr[k].pickedadult = obj.value;
                                        break;
                                    case this.Disabledclassid:
                                        this.ticketinfoArr[k].pickeddisabled = obj.value;
                                        break;
                                    case this.Seniorclassid:
                                        this.ticketinfoArr[k].pickedsenior = obj.value;
                                        break;
                                    case this.Studentclassid:
                                        this.ticketinfoArr[k].pickedstudent = obj.value;
                                        break;
                                }
                                this.ticketinfoArr[k].contactinfoArr = extractContactArray(tblobj, i);
                                break;
                            }
                        }
                    }
                    //}
                }
            }
        }
    }

    this.selectedticketqty = selectedticketqty;
    function selectedticketqty() {
        var returnval = "";
        var contactstr = "";
        var tblobj = document.getElementById("tbl" + this.uniqueid);
        for (var i = 1; i < tblobj.rows.length - 1; i++) {
            if (tblobj.rows[i].cells.length > 1) {
                if (returnval.length > 0) returnval += "{mainfieldsep}" + contactstr + "{recordsep}";
                contactstr = "";
                var j = 0;
                while (tblobj.rows[i].cells[++j]) {
                    var obj = tblobj.rows[i].cells[j].firstChild;
                    //if (parseInt(obj.value, 10) > 0) {
                    if (obj.value) {
                        //alert(obj.getAttribute("key") + "{infieldsep}" + obj.getAttribute("type") + "{infieldsep}" + obj.value + "{fieldsep}");
                        returnval += obj.getAttribute("key") + "{infieldsep}" + obj.getAttribute("type") + "{infieldsep}" + obj.value + "{fieldsep}";
                    }
                    //}
                }
            } else {
                var tboxes = tblobj.rows[i].cells[0].getElementsByTagName("input");
                for (var j = 0; j < tboxes.length; j++) {
                    if (tboxes[j].getAttribute("actualcontrol") == "1") {
                        if (tboxes[j].value.length == 0) {
                            alert("Please fill up all names, valid emails and mobile numbers to proceed.\n\nThis is to verify that bookings are done for actual individuals. Kakiseni values participation of each person coming to watch the show.");
                            tboxes[j - 1].focus();
                            return "";
                        } else if (tboxes[j].getAttribute("id").indexOf("email") >= 0) {
                            if (!checkEmail(tboxes[j].value)) {
                                alert("Please fill up all names, valid emails and mobile numbers to proceed.\n\nThis is to verify that bookings are done for actual individuals. Kakiseni values participation of each person coming to watch the show.");
                                tboxes[j].focus();
                                tboxes[j].select();
                                return "";
                            }
                        }
                        contactstr += tboxes[j].value + "{infieldsep}";
                    }
                }
                contactstr += "{fieldsep}";
            }
        }
        if (returnval.length > 0) returnval += "{mainfieldsep}" + contactstr + "{recordsep}";
        //alert(returnval);
        return returnval;
    }

    this.addtocart = addtocart;
    function addtocart() {
        var returnval = this.selectedticketqty();
        if (returnval.length > 0) {
            kakiseniWeb.ticketingAJAX.addtocart(this.uniqueid + "{recordsep}" + returnval, bt_addtocartcallback);
        }
    }

    this.bt_addtocartcallback = bt_addtocartcallback;
    function bt_addtocartcallback(res) {
        if (res.value.length > 0) {
            var temparr = res.value.split("{fieldsep}");
            var controlobj = eval(temparr[0]);
            controlobj.confirmcard(temparr[1]);
        }
    }

    this.confirmcard = confirmcard;
    function confirmcard(param) {
        //if (this.viewcartflag) return;
        if (param.indexOf("seattaken") >= 0) {
            alert("Insufficient available seats to fulfill request.\nPlease note that seat availibility has changed since previous connection to server.");
            kakiseniWeb.ticketingAJAX.getbasicparam(param, reinit_cartparam);
        } else {
            var tblobj = document.getElementById(this.containerid);
            unsavedcartchanges = unsavedcartchanges.replace("," + param + ",", "");//alert(unsavedcartchanges);
            try {
                document.getElementById("updatecartbutton_" + param).style.borderColor = "transparent";
            } catch (ex) { }

            if (this.viewcartflag == false) {
                this.recordPickedQty();
                //document.getElementById("contacttip" + this.uniqueid).style.display = "";
                if (this.viewcartbuttonshown == false) {
                    var obj = document.createElement("a");
                    obj.setAttribute("href", "mycart.aspx");
                    obj.innerHTML = "<img src=\"images/btn_checkout.png\" style=\"border:none;\" />";

                    tblobj.rows[2].cells[0].firstChild.firstChild.src = "images/btn_updatecart.png";
                    tblobj.rows[2].cells[0].insertBefore(obj, tblobj.rows[2].cells[0].firstChild);

                    this.viewcartbuttonshown = true;
                } else if (parseInt(this.totalPickedQty, 10) == 0) {
                    tblobj.rows[2].cells[0].removeChild(tblobj.rows[2].cells[0].firstChild);
                    tblobj.rows[2].cells[0].firstChild.firstChild.src = "images/btn_addtocart.png";
                    this.viewcartbuttonshown = false;
                } 
            }
            resetBookingTimer();
        }
    }
}

//initTicketInfo(eventid, ticketclassid, showtimeid, ticketclassname, displayedshowdate, displayedshowtime, seatavailable,
//seriesmaxavailable, adultprice, disabledprice, seniorprice, studentprice, pickedadult, pickeddisabled, pickedsenior,
//pickedstudent) {
//eventid,maxseriesavai,classid_audienceid_price,classid_eventtimeid_feeclass_date_time_qtyavai_(audid_qty)
function processTicketInfo(rawinfo, containerid, myuniqueid, viewcartflag, convfee, canpay, tblwidth, namewidth, emailwidth, contactwidth) {
    var temparr = rawinfo.split("{recordsep}");
    var eventid = temparr[0];
    var maxseriesval = temparr[1];

    var ticketpicker = new buyticketcontrol();
    if (tblwidth) {
        ticketpicker.tblwidth = tblwidth;
        ticketpicker.namewidth = namewidth;
        ticketpicker.emailwidth = emailwidth;
        ticketpicker.contactwidth = contactwidth;
    }
    ticketpicker.ticketinfoArr = new Array();
    var temparr2 = temparr[3].split("{fieldsep}");
    var priceinfo = "{fieldsep}" + temparr[2];
    var pickedflag = false;
    var initcontactarr = null;
    for (var i = 0; i < temparr2.length - 1; i++) {
        var temparr3 = temparr2[i].split("{infieldsep}");
        var pickedadult = 0;
        var pickeddisabled = 0;
        var pickedsenior = 0;
        var pickedstudent = 0;

        var adultprice = -1;
        var disabledprice = -1;
        var seniorprice = -1;
        var studentprice = -1;

        if (temparr3[6].length == 0) {
            temparr3[6] = ticketpicker.Adultclassid + "_0__";
            temparr3[6] += ticketpicker.Disabledclassid + "_0__";
            temparr3[6] += ticketpicker.Seniorclassid + "_0__";
            temparr3[6] += ticketpicker.Studentclassid + "_0__{contactsep}";
        }
        var temparr4 = temparr3[6].split("{contactsep}");
        initcontactarr = null;
        if (temparr4.length > 2) {
            initcontactarr = new Array();
            for (var j = 1; j < temparr4.length; j++) {
                if (temparr4[j].length > 0) {
                    var contactfieldarr = temparr4[j].split("{cinfieldsep}");
                    initcontactarr[j - 1] = new contactinfo();
                    initcontactarr[j - 1].newcontact(contactfieldarr[0], contactfieldarr[1], contactfieldarr[2]);
                }
            }
        }//alert(initcontactarr);
        var temparr4 = temparr4[0].split("__");
        for (var j = 0; j < temparr4.length - 1; j++) {
            var temparr5 = temparr4[j].split("_");

            var controlstr = "{fieldsep}" + temparr3[0] + "{infieldsep}" + temparr5[0] + "{infieldsep}";
            var controlprice = -1;
            var tempint = priceinfo.indexOf(controlstr); //alert(controlstr + "\n" + priceinfo + "\n" + tempint);
            if (tempint >= 0) {
                tempint += controlstr.length;
                controlprice = priceinfo.substr(tempint, priceinfo.indexOf("{", tempint) - tempint);
            }

            if (temparr5[0] == ticketpicker.Adultclassid) {
                pickedadult = temparr5[1];
                adultprice = controlprice;
            }
            if (temparr5[0] == ticketpicker.Disabledclassid) {
                pickeddisabled = temparr5[1];
                disabledprice = controlprice;
            }
            if (temparr5[0] == ticketpicker.Seniorclassid) {
                pickedsenior = temparr5[1];
                seniorprice = controlprice;
            }
            if (temparr5[0] == ticketpicker.Studentclassid) {
                pickedstudent = temparr5[1];
                studentprice = controlprice;
            }
            if ((pickedflag == false) && ((pickedadult > 0) || (pickeddisabled > 0) || (pickedsenior > 0) || (pickedstudent > 0)))
                pickedflag = true;
        }
        ticketpicker.ticketinfoArr[i] = new ticketinfo();
        ticketpicker.ticketinfoArr[i].initTicketInfo(eventid, temparr3[0], temparr3[1], temparr3[2],
            temparr3[3], temparr3[4], temparr3[5], maxseriesval, adultprice, disabledprice, seniorprice,
            studentprice, pickedadult, pickeddisabled, pickedsenior, pickedstudent, temparr3[7]);
        ticketpicker.ticketinfoArr[i].contactinfoArr = initcontactarr;
        //alert("test" + convfee);
        if (convfee != null) ticketpicker.ticketinfoArr[i].cfee = convfee;
        if (canpay != null) if (canpay == "yes") ticketpicker.ticketinfoArr[i].canpay = true; else ticketpicker.ticketinfoArr[i].canpay = false;
    }
    ticketpicker.init(myuniqueid, containerid);
    var tblobj = document.getElementById(containerid);

    if (tblobj.rows.length == 2) {
        var studentmsg = "<span style=\"color:red;display:none;\" id=\"studentmsgspan\"><br/>Kindly note that you are required to present some form of student ID at the show venue upon collection of ticket.</span>";
        var newrow = tblobj.insertRow(2);
        var newcell = newrow.insertCell(0);
        newcell.colSpan = 2;
        newcell.style.textAlign = "right";
        newcell.innerHTML = "";
        if (viewcartflag) {
            ticketpicker.viewcartflag = true;
            newcell.innerHTML += "<a href=\"#\" style=\"color:gray;\" id=\"updatecartbutton_" + eventid + "\" onclick=\"" + myuniqueid + ".addtocart();return false;\"><img src=\"images/btn_updatecart.png\" style=\"border:none;\" /></a>" + studentmsg;
        } else {
            ticketpicker.viewcartflag = false;
            newcell.innerHTML += "<a href=\"#\" onclick=\"" + myuniqueid + ".addtocart();return false;\"><img src=\"images/btn_addtocart.png\" style=\"border:none;\" /></a>" + studentmsg;
            if (pickedflag) ticketpicker.confirmcard(eventid);
        }
        //tblobj.style.border = "1px solid silver";

        tblobj.rows[0].cells[0].innerHTML = ticketpicker.buildDateSelector(eventid);
        tblobj.rows[0].cells[1].innerHTML = ticketpicker.buildTimeSelector("", eventid, "");
        tblobj.rows[1].cells[0].innerHTML = "<table id=\"tbl" + ticketpicker.uniqueid + "\"></table>";
        ticketpicker.resetSelector(document.getElementById("date_" + ticketpicker.uniqueid), eventid, 1); ;
    }
    return ticketpicker;
}

function updatetotalcart() {
    try {
        if (eventidlist) {
            var totalcart = 0;
            var totalpicked = 0;
            var temparr = eventidlist.split(",");
            var cfee = 0;
            var canpay = false;
            for (var i = 0; i < temparr.length - 1; i++) {
                totalcart += eval("ticketpicker" + temparr[i]).totalPickedValue.myParseFloat();
                totalpicked += eval("ticketpicker" + temparr[i]).totalPickedQty;
                cfee = eval("ticketpicker" + temparr[i]).ticketinfoArr[0].cfee;
                canpay = eval("ticketpicker" + temparr[i]).ticketinfoArr[0].canpay;
            }
            if (totalcart.myParseFloat() > 0) totalcart = totalcart.myParseFloat() + cfee.myParseFloat(); //alert(totalcart);
            if (canpay) {
                if (cfee.myParseFloat() > 0)
                    document.getElementById("totalcart").innerHTML = "<span style=\"font-weight:bold;font-size:20pt;color:Navy;\">TOTAL Payable : " + currencysign + roundVal(totalcart, false) + "</span><br/><span style=\"font-size:10px\">inclusive of Convenience Fee (" + currencysign + cfee + ")</span>";
                else
                    document.getElementById("totalcart").innerHTML = "<span style=\"font-weight:bold;font-size:20pt;color:Navy;\">TOTAL Payable : " + currencysign + roundVal(totalcart, false) + "</span>";
            } else {
                document.getElementById("totalcart").innerHTML = "TOTAL Booked Seat : " + totalpicked;
            }
            if ((totalcart.myParseFloat() > 0) || (totalpicked.myParseFloat() > 0)) {
                document.getElementById("checkoutbuttonrow").style.display = "";
            } else {
                document.getElementById("checkoutbuttonrow").style.display = "none";
            }
        }
    } catch (ex) { }
}

var unsavedcartchanges = ",";
var checkingout = false;
function checkout(obj) {
    if (checkingout) return;
    if (document.getElementById("txtcontactno")) {
        if (document.getElementById("txtcontactno").value.trim().length == 0) {
            alert("Please enter Your Contact No.");
            document.getElementById("txtcontactno").focus();
            return;
        } 
    }
    var flag = true;
    try {
        if (pendingupdateseries) {
            if (pendingupdateseries.length > 1) flag = false;
        }
    } catch (ex) { }
    if (flag && (unsavedcartchanges.length > 1)) flag = false;
    if (flag) {
        checkingout = true;
        document.forms[0].action = "/checkout.aspx";
        document.forms[0].submit();
    }
    else {
        alert("One or more changes in cart are not updated.\nPlease review cart list (highlighted in red)");
    }
}

function makecartchange(eventid) {
    if (unsavedcartchanges.indexOf(eventid) < 0) {
        unsavedcartchanges += eventid + ",";
        try {
            document.getElementById("updatecartbutton_" + eventid).style.borderWidth = "1px";
            document.getElementById("updatecartbutton_" + eventid).style.borderStyle = "solid";
            document.getElementById("updatecartbutton_" + eventid).style.borderColor = "red";
        } catch (ex) { }
    }
}

function reinit_cartparam(res) {
    if (res.value.length > 0) {
        if (res.value.indexOf("Error: ") == 0) {
            alert(res.value);
        } else {
            var tempArr = res.value.split("{uniquerecordsep}");
            eval(tempArr[1]) = processTicketInfo(tempArr[0], eval(tempArr[1]).containerid, tempArr[1], false, null, null);
        }
    }
}

var glo_defaultTimeoutTick;
var currRemainingTick;
function resetBookingTimer() {
    currRemainingTick = glo_defaultTimeoutTick;
    setCookie("ckBookingTimeout", currRemainingTick, 1);
    buildCartContent("cartitemcount");
}
var previnstance = null;

function bookingTimer(objid) {
    var obj = document.getElementById(objid);
    if (document.getElementById("cartitemcount").innerHTML != "0") {
        if (obj) {
            --currRemainingTick;
            if (currRemainingTick < 0) { currRemainingTick = 0; return; }
            setCookie("ckBookingTimeout", currRemainingTick, 1);
            if (currRemainingTick == 0) {
                ticketBookingTimeout();
            } else {
                var seconds = currRemainingTick % 60;
                var minutes = (currRemainingTick - seconds) / 60;
                if (parseInt(seconds, 10) < 10) seconds = "0" + seconds;
                
                var ttip = "<span class=\"tooltip\"";
                if (parseInt(minutes, 10) < 3) ttip += " style=\"display:block;\"";
                //alert(ttip);
                ttip += "><span class=\"top\"></span><span class=\"middle\" style=\"white-space:normal;\">";
                ttip += "The seats offered are held for 10 minutes. Please complete the transaction within this time.<br/>";
                ttip += "<div style=\"color:#f26522;font-weight:bold;text-align:center;font-size:16pt;\">" + minutes + ":" + seconds + " mins left</div>";
                ttip += "</span><span class=\"bottom\"></span></span>";
                obj.innerHTML = "<font style=\"color:Red;\"><img src=\"/images/icon_timer.png\" alt=\"\" width=\"16\" height=\"16\" align=\"absmiddle\" />TIMEOUT: " + minutes + ":" + seconds + "</font>" + ttip;
                try {
                    clearTimeout(previnstance);
                } catch (ex) { }
                previnstance = setTimeout("bookingTimer(\"" + objid + "\");", 1000);
            }
        } 
    }
}

function ticketBookingTimeout() {
    kakiseniWeb.ticketingAJAX.bookingTimeoutHandler(1, 0);
    alert("Seats booking timeout");
    if (document.location.href.indexOf("admin") > 0) document.location.href = "/mycart.aspx?to=1";
    else document.location.href = "mycart.aspx?to=1";
}

function buildCartContent(containerid) {
    if ((glo_defaultTimeoutTick != null) && (glo_defaultTimeoutTick != "undefined")) {
        if (document.getElementById(containerid)) {
            kakiseniWeb.ticketingAJAX.getBookedSeatCount(containerid, buildCartContent_callback);
        }
    } else {
        document.getElementById(containerid).innerHTML = "..initializing";
        setTimeout("buildCartContent('" + containerid + "');", 500);
    }
}

function buildCartContent_callback(res) {
    if (res.value.length > 0) {
        var tempArr = res.value.split("{infieldsep}");
        var obj = document.getElementById(tempArr[0]);
        obj.innerHTML = tempArr[1];
        if (tempArr[1] == "0") document.getElementById("viewcart").style.display = "none";
        else document.getElementById("viewcart").style.display = "";
        if (parseInt(tempArr[1], 10) > 0) {
            var tempval = getCookie("ckBookingTimeout");
            if ((tempval == null) || (tempval == "undefined") || (tempval == "NaN") || (tempval.length == 0))
                resetBookingTimer();
            else
                currRemainingTick = parseInt(tempval, 10);
            bookingTimer("bookingTimerTag");
        } else {
            document.getElementById("bookingTimerTag").innerHTML = "";
        }
    }
}

function traceact(obj) {
    obj.style.display = "none";
    var idstr = obj.getAttribute("id");
    idstr = idstr.substr(0,idstr.length-1);
    document.getElementById(idstr).style.display = "";
    setTimeout("document.getElementById(\"" + idstr + "\").focus();", 100);
}

function tracefront(obj) {
    obj.value = obj.value.trim();
    if (obj.value.length == 0) {
        obj.style.display = "none";
        document.getElementById(obj.getAttribute("id") + "f").style.display = "";
    }
}

function extractContactArray(tblobj, rowIndex) {
    var returnArr = new Array();
    var arrSize = -1;
    for (var i = rowIndex + 1; i < tblobj.rows.length; i++) {
        if (tblobj.rows[i].cells.length == 1) {
            returnArr[++arrSize] = new contactinfo();
            var fieldArr = new Array();
            var k =-1;
            var tboxes = tblobj.rows[i].cells[0].getElementsByTagName("input");
            for (var j = 0; j < tboxes.length; j++) {
                if (tboxes[j].getAttribute("actualcontrol") == "1") fieldArr[++k] = tboxes[j].value;
            }
            returnArr[arrSize].newcontact(fieldArr[0], fieldArr[1], fieldArr[2]);
        } else {
            break;
        }
    }
    return returnArr;
}
