﻿   function check() {
        document.frmMyForm.cinemacode.value = document.frmMyForm.cinema.getAttribute("data");
        document.frmMyForm.txtSessionId.value = document.frmMyForm.showtime.getAttribute("data");

        if (document.frmMyForm.cinemacode.value == "") {
            alert("请选择电影院");
            return false;
        }
   
        if (document.frmMyForm.txtSessionId.value == "") {
            alert("请选择观影时间");
            return false;
        }
        

        return true;
    }

function submitform() {
    if (check())
        document.frmMyForm.submit();
}

jQuery.fn.sort = function() { 
        return this.pushStack( jQuery.makeArray( [].sort.apply( this, arguments ) ) ); 
}; 

    loadcinema();

    function loadcinema() {
        //AJAX LOAD Cinema data
        AjaxCall("getCinema", {}, function (xml) {
            $("#cinema_list").find("li").remove();

            var sb = "";
            $(xml).find("Item").sort(function(a, b){
				var aid = $(a).attr("id");
				var bid = $(b).attr("id");
				return bid - aid;
			}).each(function (index) {
                var id = $(this).attr("id");
                var value = $(this).attr("name");
                sb += "<li id=\"cinema" + index + "\" data=\"" + id + "\" onmouseover=\"mover(this,'cinema_list')\" onclick=\"sele(this,'cinema', 'loadfilm')\">" +
                     value + "</li>";
                //var e = document.createElement("LI");
                //e.setAttribute("id", "cinema" + index);
                //e.setAttribute("data", id);
                //e.setAttribute("onmouseover", "mover(this, 'cinema_list')");
                //e.setAttribute("onclick", "sele(this,'cinema', 'loadfilm')");
                //e.innerHTML = value;
                //document.getElementById("cinema_list").appendChild(e);
            });
            $("#cinema_list").html(sb);

            $("#cinema").attr("disabled", false);
        });

        
    }

    function loadfilm(cinema) {
        //alert("cinema " + cinema.getAttribute("value"));

        AjaxCall("getFilm", { "cid": $("#cinema").attr("data") }, function (xml) {
            $("#film_list").find("li").remove();

            var sb = "";
            $(xml).find("Item").each(function (index) {
                var id = $(this).attr("id");
                var value = $(this).attr("name");
                sb += "<li id=\"film" + index + "\" data=\"" + id + "\" onmouseover=\"mover(this,'film_list')\" onclick=\"sele(this,'film', 'loadtime')\">" +
                     value + "</li>";
                //var e = document.createElement("LI");
                //e.setAttribute("id", "film" + index);
                //e.setAttribute("data", id);
                //e.setAttribute("onmouseover", "mover(this, 'film_list')");
                //e.setAttribute("onclick", "sele(this,'film', 'loaddate')");
                //e.innerHTML = value;
                //document.getElementById("film_list").appendChild(e);
            });

            $("#film_list").html(sb);
            $("#film").attr("disabled", false);
            turnit('film');
        });
    }
	
    function loaddate(film) {
        //alert("film " + film.getAttribute("value"));
        var cid = $("#cinema").attr("data");
        var fid = $("#film").attr("data");
		
		$("#t1").css("display", "none");
		$("#t2").css("display", "none");
		$("#t3").css("display", "none");
		
		datelist = new Array();
        AjaxCall("getDate", { "cid": cid, "fid": fid }, function (xml) {            
            var tt = "";
			$(xml).find("Item").each(function () {
				var id = $(this).attr("id");
                var now = new Date();
				now.setHours(0,0,0,0);
				
				var strs = id.split("-");
				var dt = new Date(strs[0], strs[1]-1, strs[2]);
				
				var days = (dt - now)/(24*60*60*1000);
								
				$("#t" + (days+1)).css("display", "block");
				$("#t" + (days+1)).css("cursor", "pointer");
				$("#t" + (days+1)).click(function(){	
					$("#bookingdate").val(id);					
					loadtime();
				});
				if (tt == ""){
					tt = "#t" + (days+1);
				}
            });
			if (tt != ""){
				$(tt).click();
			}
        });        
    }
      
    function showdatepick(mindate, maxdate) {
        WdatePicker({ el: 'bookingdate', minDate: mindate, maxDate: maxdate, onpicked: function () { loadtime() },dateFmt:'yyyy-M-d' });
          //alert("here");
    }

    function loadtime() {
        //alert($("#cinema").attr("data") + " " + $("#film").attr("data") + " " + $("#bookingdate").val());

        var cid = $("#cinema").attr("data");
        var fid = $("#film").attr("data");
        //var sdate = $("#bookingdate").val();

        AjaxCall("getTime", { "cid": cid, "fid": fid}, function (xml) {
            $("#showtime_list").find("li").remove();

            var sb = "";
            $(xml).find("Item").each(function (index) {
                var id = $(this).attr("id");
                var value = $(this).attr("name");
                sb += "<li id=\"time" + index + "\" data=\"" + id + "\" onmouseover=\"mover(this,'showtime_list')\" onclick=\"sele(this,'showtime', 'enablebook()')\">" +
                    $("#bookingdate").val() + " " + value + "</li>";
                //var e = document.createElement("LI");
                //e.setAttribute("id", "time" + index);
                //e.setAttribute("data", id);
                //e.setAttribute("onmouseover", "mover(this, 'showtime_list')");
                //e.setAttribute("onclick", "sele(this,'showtime', 'enablebook()')");
                //e.innerHTML = $("#bookingdate").val() + " " + value;

                //document.getElementById("showtime_list").appendChild(e);
            });
            $("#showtime_list").html(sb);

            $("#showtime").attr("disabled", false);
            turnit('showtime');
        });
    }

    function  turnit(ss){
        if ($("#" + ss + "div").css("display") == "none") {
            $("#" + ss + "div").css("display", "");
            for (var i=0;i<$("#" + $("#" + ss).attr("id") + "_list").find("li").length;i++){
                if ($("#" + $("#" + ss).attr("id") + i).html() == $("#" + ss).val())
                    $("#" + $("#" + ss).attr("id") + i).attr("className", 'ss');
                else
                    $("#" + $("#" + ss).attr("id") + i).attr("className", '');
            }
        } else {
            $("#" + ss + "div").css("display", "none");  
        }
    }

    function sele(tid, ss, fn) {
        $("#" + ss + "div").css("display", "none");
        $("#" + ss).val(tid.innerHTML);
        $("#" + ss).attr("data", tid.getAttribute("data"));

        //alert(fn);
        if ( fn != undefined){
            var f = eval(fn);
            if (typeof (eval(f)) == 'function') {
                f(tid);
            }
        }
       
    }

    function mover(tid, list) {
        $("#" + list).find("li").each(function () {
            $(this).attr("className", "");
        });
        tid.className='ss'
        if(typeof(prevObj)!='undefined')
            prevObj.bgColor='';
        prevObj=tid;
    }

    function CPos(x, y) {
        this.x = x;
        this.y = y;
    }

    function GetObjPos(ATarget) {
        var target = ATarget;
        var pos = new CPos(target.offsetLeft, target.offsetTop);

        var target = target.offsetParent;
        while (target) {
            pos.x += target.offsetLeft;
            pos.y += target.offsetTop;

            target = target.offsetParent
        }

        return pos;
    }

    function enablebook() {
        //$("#booking").attr("className", "normalimg");
        //document.getElementById("booking").setAttribute("onclick", "submitform()");
        $("#booking").click(function () {
            submitform();
        });
    }

    function showLoading() {
        var divobj = document.getElementById("div_loading");
        if (divobj == undefined) {
            divobj = document.createElement("DIV");
            divobj.id = "div_loading";
            divobj.className = "loading";
            divobj.innerHTML = "<img src='images/loading.gif'/>数据载入中，请稍候...";
            document.body.appendChild(divobj);
        }
        divobj.style.display = "block";
    }

    function hideLoading() {
        var divobj = document.getElementById("div_loading");
        if (divobj != undefined) {
            divobj.style.display = "none";
        }
    }

    function AjaxCall(method, params, successfunc, failfunc, sync) {
        if (params != null) {
            for (var p in params) {
                if (typeof (params[p]) != "function") {
                    params[p] = encodeURIComponent(params[p]);
                }
            }
        }
        showLoading();
        $.ajax({
            url: TicketUrl + 'myServlet.aspx?op=' + method + '&rand=' + new Date().getTime(),
            type: 'POST',
            dataType: 'xml',
            async: sync == undefined ? true : sync,
            data: params,
            timeout: 60000,
            error: function () {
                alert('读取数据发生错误，请稍后尝试刷新本页面');
                hideLoading();
            },
            success: function (xml) {
                try {
                    if (xml.getElementsByTagName("ErrorMsg").length == 1) {
                        if (failfunc != undefined) failfunc(xml.getElementsByTagName("ErrorMsg")[0].text);
                        else {
                            alert(xml.getElementsByTagName("ErrorMsg")[0].text);
                        }
                        return;
                    }
               
                    if (successfunc != undefined) successfunc(xml);
                } finally {
                    hideLoading();
                }
            }
        });
    }
