//
// Submit the Customer Registration Page. Make sure the "terms and conditions" check box is checked.
//

function submitCustomerRegistrationForm() {
    if (!document.forms['RegisterCustomerForm'].acceptTerms.checked) {
	alert("Please check the box to indicate you accept the terms and conditions.");
	return;
    }

    document.forms['RegisterCustomerForm'].passwordConfirmation.value = document.forms['RegisterCustomerForm'].password.value;
    document.forms['RegisterCustomerForm'].submit();
    return true;
}

function gotoHome() {
	
	var showTimeSelect = document.forms['SelectTicketForm']['showTime'];
    var selectedShow = showTimeSelect.selectedIndex;
    var pageURL = window.location.href;
    var action;
    
    action = pageURL.substring(0, pageURL.lastIndexOf("/"));
	action = action+"/Welcome.do"
	window.location.href = action;
    
}

function gotoSelectionHome() {
	var pageURL = window.location.href;
    var action;
    if(requiredSeatingModule == 'true'){
	    var x=window.confirm("Current selected seats will be lost, Do you want to continue ?")
	    if (x){
	    	action = pageURL.substring(0, pageURL.lastIndexOf("/"));
	    	action = action+"/SelectTicketPage.do"
	    	window.location.href = action;
	    }else{
	    	return;
	    }
    }else{
       	action = pageURL.substring(0, pageURL.lastIndexOf("/"));
    	action = action+"/SelectTicketPage.do"
    	window.location.href = action;
    }
}
//
// Update the ticket options page when a specific show is selected
//

function updateTicketOptions() {
		
	var showTimeSelect = document.forms['SelectTicketForm']['showTime'];
    var selectedShow = showTimeSelect.selectedIndex;
    var pageURL = window.location.href;
    var action;

    if (pageURL.indexOf('show') != -1) {
		action = pageURL.substring(0, pageURL.lastIndexOf("/"));
		action = action+"/SelectTicketPage.do?show="+selectedShow;
		window.location.href = action;
	} else {
		action = pageURL.substring(0, pageURL.lastIndexOf("/"));
		action = action+"/SelectTicketPage.do?show="+selectedShow;
		window.location.href = action;
	}
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}



function chooseSeats() {
   var form = document.forms['SelectTicketForm'];
   var myArguments = new Array();
   
   for (i = 0; i < form.length; i++) {
	var formelement = form.elements[i];
	if (formelement.type == 'select-one' && formelement.name.indexOf("ticketClass") == 0) {
		myArguments[0] = formelement[formelement.selectedIndex].value;
		if(myArguments[0] != "0")
			break;
	}
   }	
   
   if(myArguments[0].trim() == "0"){
	   alert("Please select No.of seats required");
	   return;
   }
   
   var showTimeSelect = document.forms['SelectTicketForm']['showTime'];
   var selectedShow = showTimeSelect.selectedIndex;
   
   myArguments[1] = document.getElementById("selectedTicketClass").value;
   
   if(document.getElementById("selectedSeats").value == null || document.getElementById("selectedSeats").value =="" || document.getElementById("selectedSeats").value == "undefined" || document.getElementById("selectedSeats").value == 'cancel')
   	myArguments[2] = "N";
   else
   	myArguments[2] = document.getElementById("selectedSeats").value;	
   
   returnvalue = window.showModalDialog("/LoadSeatLayout.do?show="+selectedShow+"&selectedTicketClass="+myArguments[1], myArguments, 'dialogHeight:410px;dialogWidth:850px;edge:sunken;status:no');
   document.getElementById("selectedSeats").value = returnvalue;
   if(returnvalue != 'cancel')
	   submitEventDetailForm();
}

//
// Submit event detail form, with ticket classes and quantities
//

function submitEventDetailForm() {
    var form = document.forms['SelectTicketForm'];
	if(requiredSeatingModule == 'true'){
	    // Call this if seating module is enabled for the show
		if(document.getElementById("selectedSeats").value == null || document.getElementById("selectedSeats").value ==""){
			alert("Please select your seat numbers.");
		    return;
		}       
		
		if(document.getElementById("selectedSeats").value == 'undefined'){
			alert('Do not close the window directly, Use Cancel or Proceed to Payment button.');
			return;
		}
		var arr = document.getElementById("selectedSeats").value.split(",");
    }
else{     
      var myArguments = new Array();
   
      for (i = 0; i < form.length; i++) {
	      var formelement = form.elements[i];
	      if (formelement.type == 'select-one' &&         formelement.name.indexOf("ticketClass") == 0) {
		myArguments[0] = formelement [formelement.selectedIndex].value;
		if(myArguments[0] != "0")
			break;
	}
   }	
   
   if(myArguments[0].trim() == "0"){
	   alert("Please select No.of seats required");
	   return;
  }
}
   form.submit();
   return;
}

//
// Open JavaScript window with e-ticket
//

function printETicket(orderId) {
    window.open("ticket.jsp?orderId="+orderId, "IndianStage", "location=0,status=0,scrollbars=1, width=800, height=600");
}

function setSelectedTicketClass(selectedClass){
	document.getElementById("selectedTicketClass").value = selectedClass;
}
//
// When a quantity of tickets is selected, set all the other quantites to zero.
//

function setTicketClassZeroValues(index) {
    var form = document.forms['SelectTicketForm'];

    for (i = 0; i < form.length; i++) {
	var formelement = form.elements[i];

	if (formelement.type == 'select-one' && formelement.name.indexOf("ticketClass") != -1 && 
	    formelement.name != "ticketClass["+index+"]") {
	    formelement.selectedIndex = 0;
	}
    }
}
function getHTTPObject() {
 var xmlhttp;

 if(window.XMLHttpRequest){
 xmlhttp = new XMLHttpRequest();
 }
 else if (window.ActiveXObject){
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 if (!xmlhttp){
 xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
 }

}
 return xmlhttp;


}
var http = getHTTPObject(); // We create the HTTP Object


function getLocation(){
 
 if(document.RegisterCustomerForm.state.value==0)
 {
 document.RegisterCustomerForm.city.options.length=1;
 return;
 }

 var url = "/getcity.do";
 url =url+"?region=";
 var rid = document.RegisterCustomerForm.state.value;
 http.open("GET", url + escape(rid), true);
 http.onreadystatechange = handleHttpResponse;
 http.send(null);
 }

function getonLoadCity(){
	 if(document.RegisterCustomerForm.state.value==0)
	 {
	 document.RegisterCustomerForm.city.options.length=1;
	 return;
	 }

	 var url = "/getcity.do";
	 url =url+"?region=";
	 var rid = document.RegisterCustomerForm.state.value;
	 //alert(url+rid);
	 http.open("GET", url + escape(rid), true);
	 http.onreadystatechange = loadCities;
	 http.send(null);
}

function loadCities() {
	 if (http.readyState == 4) {
		 if(http.status==200) { 
		 document.RegisterCustomerForm.city.options.length=1;
		 var root = http.responseXML.getElementsByTagName("cities")[0];
			 if(root!=null)
			 {
				 var citylength=root.childNodes.length;
				 for(i=0;i<citylength;i++)
				 {
					 var opt=document.createElement("option");
					 opt.value=root.childNodes[i].childNodes[1].childNodes[0].nodeValue;
					 opt.text=root.childNodes[i].childNodes[0].nodeValue;
					 var value = root.childNodes[i].childNodes[0].nodeValue;
					 document.RegisterCustomerForm.city.options.add(opt);
				 }
				 document.RegisterCustomerForm.city.value = selectedCity;
			 }
		 }
	 }
	 document.RegisterCustomerForm.othersTxtBox.style.visibility='hidden';	
}

function handleHttpResponse() {
 if (http.readyState == 4) {
 if(http.status==200) { 
 document.RegisterCustomerForm.city.options.length=1;
 var root = http.responseXML.getElementsByTagName("cities")[0];
 if(root!=null)
 {
 var citylength=root.childNodes.length;

 for(i=0;i<citylength;i++)
 {
 var opt=document.createElement("option");
 opt.value=root.childNodes[i].childNodes[1].childNodes[0].nodeValue;
 opt.text=root.childNodes[i].childNodes[0].nodeValue;
 var value = root.childNodes[i].childNodes[0].nodeValue;
 document.RegisterCustomerForm.city.options.add(opt);
 }
 }
 }
 }
 }

//to fetch the show times of the show with seating
var obj;
function getViewFullSeatingShowTime(){
         
	 obj = getHTTPObject();
	
	if(document.getElementById("showName").value == 0){
		document.getElementById("showName") = 1;
		return;
	}
	var url = "getshowtimes.do";
	url = url+"?type=";
	var name = document.getElementById("showName").value;
//alert(name);
	url += name;

//alert(url);
	
	obj.open("GET", url, true);
	obj.onreadystatechange = getshowDetails;
	obj.send(null);
}

function getshowDetails(){	
		if(obj.readyState == 4){
			if(obj.status == 200){
				//alert("displaying response text");
				document.getElementById("showDateTime").options.length=1;
				 var Timelist = obj.responseXML.getElementsByTagName("ShowsTime")[0];			 
				 if(Timelist!=null)
				 {
				 var show=Timelist.childNodes.length;
	           // alert(show);
				 for(i=0;i<show;i++)
				 {			 
				 var opt=document.createElement("option");
				 opt.value=Timelist.childNodes[i].childNodes[1].childNodes[0].nodeValue;
				 opt.text=Timelist.childNodes[i].childNodes[0].nodeValue;
				 var value = Timelist.childNodes[i].childNodes[0].nodeValue;		 
				 document.getElementById("showDateTime").options.add(opt);
				 }
			   }
			}
		    }
		}



//function to read the xml file and the seats boobked and blocked...
var obj1;
function LoadSeats() {
obj1 = getHTTPObject();

var showId = document.getElementById("showName").value;
var showDate = document.getElementById("showDateTime").value;

if(showDate != 0) {
var url = "getSeatingLayout.do";
url = url+"?eventId=";
url += showId;
url = url+"&eventDate=";
url += showDate;
obj1.open("GET", url, true);
obj1.send(null);
}
}

function LoadSeatingConfig(){
       var showName = document.getElementById("showName").value;
       var showDate = document.getElementById("showDateTime").value;

       if(showName == 0 || showDate == 0){
         alert("Please select both show Name and Show Date");
         return;
       }
        
       window.showModalDialog("seatjsp/seatLayout.jsp",'null',"dialogHeight:350px;dialogWidth:850px;edge:sunken;status:no");
}

//functions For New Offline Orders

var ob;
function getShowNames(){
	ob = getHTTPObject();	
			
          document.NewOfflineOrderForm.showId.options.length=1;
	    document.NewOfflineOrderForm.ticketCategory.options.length=1;
          document.NewOfflineOrderForm.showDate.options.length=1;
          document.NewOfflineOrderForm.seatsSelected.value = "";
          document.NewOfflineOrderForm.seatIdentifier.value="";
          document.NewOfflineOrderForm.ticketAvail.value = "";
          document.getElementById("ticketAvail").style.visibility = 'hidden';


		var url = "getshowNames.do";
		url = url+"?cityId=";
		var id = document.NewOfflineOrderForm.cityName.value;
		url += id;
		ob.open("GET", url, true);
		ob.onreadystatechange = showsNames;
		ob.send(null);
}


function showsNames(){ 
	if(ob.readyState == 4){	
      if(ob.status == 200){		
			 var showlist = ob.responseXML.getElementsByTagName("ShowsName")[0];
//alert(showlist);
			 if(showlist != null){
			 var showlength=showlist.childNodes.length;
//alert("showlength :" + showlength);
                  if(showlength > 0){
			 for(i=0;i<showlength;i++){
                   var showname = ob.responseXML.getElementsByTagName("showname")[i];
      //alert(showname);           					 
			 var opt=document.createElement("option");
			 opt.value=showname.childNodes[0].childNodes[0].nodeValue;
//alert(opt.value);
			 opt.text=showname.childNodes[1].childNodes[0].nodeValue;
			 var value = showname.childNodes[0].childNodes[0].nodeValue;   		 
			 document.NewOfflineOrderForm.showId.options.add(opt);			  
			}
                } else if(showlength == 0) {
                   var opt=document.createElement("option");
			 opt.value = "0";
			 opt.text = "No Shows In City";
			 var value = "0";   		 
			 document.NewOfflineOrderForm.showId.options.add(opt);
               }		 
		} 
	 }	
   }
}

var obj;
function getShowTimes(){
   obj = getHTTPObject();
    
       document.NewOfflineOrderForm.showDate.options.length=1;      
       document.NewOfflineOrderForm.ticketCategory.options.length=1;
       document.getElementById("seating").style.visibility = 'hidden';
       document.NewOfflineOrderForm.seatsSelected.value = "";
       document.NewOfflineOrderForm.seatIdentifier.value="";
       document.NewOfflineOrderForm.ticketAvail.value = "";
       document.getElementById("ticketAvail").style.visibility = 'hidden';
          

       var url = "getshowDateTime.do";
       var showId = document.NewOfflineOrderForm.showId.value;	
       var cityId = document.NewOfflineOrderForm.cityName.value;       
       url = url+"?showId=";
       url += showId;  
       url = url+"&cityId=";
       url += cityId; 
       obj.open("GET", url, true);
       obj.onreadystatechange = showDateTime;
       obj.send(null);
}


function showDateTime(){
	if(obj.readyState==4){
	   if(obj.status==200){
         var showdatelst=obj.responseXML.getElementsByTagName("ShowDates")[0];
         if(showdatelst!=null){
           var len=showdatelst.childNodes.length;
           for(i=0;i<len;i++){					 
			 var opt=document.createElement("option");
			 opt.value=showdatelst.childNodes[i].childNodes[1].childNodes[0].nodeValue;
			 opt.text=showdatelst.childNodes[i].childNodes[0].nodeValue;
			 var value=showdatelst.childNodes[i].childNodes[0].nodeValue;
			 document.NewOfflineOrderForm.showDate.options.add(opt);			  
		}	
         }
	}
   }
}

var obe;
function getTicketCategory(){
	obe = getHTTPObject();	
		if(obe == null){
		   alert("Your browser Does not support Ajax!");		
		}		
		document.NewOfflineOrderForm.ticketCategory.options.length=1;
            document.NewOfflineOrderForm.seatsSelected.value = "";
 		document.NewOfflineOrderForm.seatIdentifier.value="";                       
            

		var url = "getticketcategory.do";
		url = url+"?showId=";
		var id = document.NewOfflineOrderForm.showId.value;
		var showdate = document.NewOfflineOrderForm.showDate.value;
		url += id;
		url = url+"&showDate=";
		url += showdate;
		obe.open("GET", url, true);
		obe.onreadystatechange = TicketCategory;
		obe.send(null);
}

function TicketCategory(){	
	if(obe.readyState == 4){		
		if(obe.status == 200){
			 var categlist = obe.responseXML.getElementsByTagName("TicketCategory")[0];                   

                  if(categlist != null){
			 var categlength=categlist.childNodes.length;		
			 for(i=0;i<categlength;i++){					 
			     var opt=document.createElement("option");
			     opt.value=categlist.childNodes[i].childNodes[1].childNodes[0].nodeValue;
			     opt.text=categlist.childNodes[i].childNodes[0].nodeValue;
			     var value=categlist.childNodes[i].childNodes[0].nodeValue;		 
			     document.NewOfflineOrderForm.ticketCategory.options.add(opt);			  
			}		 
		}
	 }	
   }
}

var obej;
function seatingEnabled(){
	obej = getHTTPObject();	
		if(obej == null){
		   alert("Your browser Does not support Ajax!");		
		}	      
            document.NewOfflineOrderForm.seatsSelected.value = "";
 		document.NewOfflineOrderForm.seatIdentifier.value="";  
           
		var url = "getshowDetails.do";
            var id = document.NewOfflineOrderForm.showId.value;
		url += "?showId=";
           url += id;
		obej.open("GET", url, true);
		obej.onreadystatechange = seatingRequired;
		obej.send(null);
}

function seatingRequired(){	
	if(obej.readyState == 4){		
		if(obej.status == 200){
			 var seatingRequired = obej.responseXML.getElementsByTagName("SeatingRequired")[0];                   
                  if(seatingRequired != null){
			 var seatinglength=seatingRequired.childNodes.length;	
			 for(i=0;i<seatinglength;i++){			     
			     var seat=seatingRequired.childNodes[i].childNodes[0].nodeValue;
	                   if(seat == "true") {
                         document.getElementById("seating").style.visibility = 'visible';
				}	
			}		 
		}
	 }	
   }
}

var obejc;
function StockAvailable(){
	obejc = getHTTPObject();	
		if(obejc == null){
		   alert("Your browser Does not support Ajax!");		
		}	           
		var url = "StockAvailable.do";
            var id = document.NewOfflineOrderForm.showId.value;
            var date = document.NewOfflineOrderForm.showDate.value;
            var category = document.NewOfflineOrderForm.ticketCategory.value;

		url += "?showId="+id;
            url += "&showDate="+date;
            url += "&ticketCategory="+category;

		obejc.open("GET", url, true);
		obejc.onreadystatechange = Productstock;
		obejc.send(null);
}

function Productstock(){	
	if(obejc.readyState == 4){		
		if(obejc.status == 200){
                   var ticketAvail = obejc.responseXML.getElementsByTagName("quantity")[0]; 
                 if(ticketAvail != null){
			 var length=ticketAvail.childNodes.length;	
			 for(i=0;i<length;i++){			     
			     var availQty=ticketAvail.childNodes[i].childNodes[0].nodeValue;
	                   if(availQty > 0) {
                        document.NewOfflineOrderForm.ticketAvail.value = availQty;
                        document.getElementById("Qty").innerHTML = availQty;
                        document.getElementById("ticketAvail").style.visibility = 'visible';
			}	
		}		 
	 }			
          	                   
    }		 
  }
}	


function seatingVisibility() {
  document.getElementById("seating").style.visibility = 'hidden';
  document.getElementById("ticketAvail").style.visibility = 'hidden';
  document.NewOfflineOrderForm.seatsSelected.value="NA";
  document.NewOfflineOrderForm.seatIdentifier.value="";
  document.NewOfflineOrderForm.customerName.value="";
  document.NewOfflineOrderForm.customerTelephoneNumber.value="";
  document.NewOfflineOrderForm.customerEmail.value="";
  document.NewOfflineOrderForm.paymentMethod.value="0";
  document.NewOfflineOrderForm.cityName.value="0";
  document.NewOfflineOrderForm.ticketQuantity.value="0";
  document.NewOfflineOrderForm.ticketAvail.value = "";
  document.NewOfflineOrderForm.customerAddress.value = "";
  document.NewOfflineOrderForm.customerCity.value = "";
  document.NewOfflineOrderForm.customerState.value = "";  
  document.NewOfflineOrderForm.notes.value="";
}

function chooseSeatsOffline() { 
   var myArgum = new Array();  
   var showId = document.NewOfflineOrderForm.showId.value;
   var selectedShow = document.NewOfflineOrderForm.showDate.value;
   var selectedTicketClass = document.NewOfflineOrderForm.ticketCategory.value;

   myArgum [0] = document.NewOfflineOrderForm.ticketQuantity.value;
   myArgum [1] = selectedTicketClass;
   
 if(document.NewOfflineOrderForm.seatIdentifier.value == null || document.NewOfflineOrderForm.seatIdentifier.value =="" || document.NewOfflineOrderForm.seatIdentifier.value == "undefined" || document.NewOfflineOrderForm.seatIdentifier.value == 'cancel')
   	myArgum[2] = "N";
   else
   	myArgum[2] = document.NewOfflineOrderForm.seatIdentifier.value;
    returnvalue = window.showModalDialog("OfflineSeatLayout.do?showId="+showId+"&showDate="+selectedShow+"&selectedTicketClass="+selectedTicketClass, myArgum, 'dialogHeight:410px;dialogWidth:850px;edge:sunken;status:no');
  if(returnvalue != 'cancel'){
        document.NewOfflineOrderForm.seatIdentifier.value = returnvalue;  
  
        var seatlst= returnvalue.split(",");   
   
  var selectedSeats="";  
  	 for(i=0;i<seatlst.length;i++){
   	 	var seatno = seatlst[i];
   	 	var seats = seatno.split("_");
   	 	selectedSeats += ", " +seats[0]+ "-" + seats[2];
   	 	document.NewOfflineOrderForm.seatsSelected.value = selectedSeats;
      }   
  } 
}

var cus;
function verifyCustomer(){
        cus = getHTTPObject();	
		if(cus == null){
		   alert("Your browser Does not support Ajax!");		
		}
          document.NewOfflineOrderForm.customerIdentifier.value="";
          document.NewOfflineOrderForm.customerName.value="";
          document.NewOfflineOrderForm.customerTelephoneNumber.value="";	     
          document.NewOfflineOrderForm.customerAddress.value = "";
          document.NewOfflineOrderForm.customerCity.value = "";
          document.NewOfflineOrderForm.customerState.value = ""; 
          document.getElementById("CusNotAvail").innerHTML = "";
           
          if(document.NewOfflineOrderForm.customerEmail.value == ""){
            alert("Please input the Email Id to Verify Customer!");
            document.NewOfflineOrderForm.customerEmail.focus();
            return;
           }
             
		var url = "CustomerSearch.do";
            var email = document.NewOfflineOrderForm.customerEmail.value;            
		url += "?customerEmail="+email;
       //alert(url);    
		cus.open("GET", url, true);
		cus.onreadystatechange = CustomerSearchResult;
		cus.send(null);

}

function CustomerSearchResult() {
      if(cus.readyState == 4){		
		if(cus.status == 200){   
                var customerDetail =  cus.responseXML.getElementsByTagName("CustomerDetail")[0]; 
               //alert(customerDetail); 
             var len = customerDetail.childNodes.length;
                     if(len > 0){          
                        var Customer = cus.responseXML.getElementsByTagName("customer")[0]; 
                        document.NewOfflineOrderForm.customerIdentifier.value = Customer.childNodes[0].childNodes[0].nodeValue;
                        document.NewOfflineOrderForm.customerName.value = Customer.childNodes[1].childNodes[0].nodeValue;
				document.NewOfflineOrderForm.customerTelephoneNumber.value = Customer.childNodes[2].childNodes[0].nodeValue;
				document.NewOfflineOrderForm.customerAddress.value = Customer.childNodes[3].childNodes[0].nodeValue;
				document.NewOfflineOrderForm.customerCity.value = Customer.childNodes[4].childNodes[0].nodeValue;
				document.NewOfflineOrderForm.customerState.value = Customer.childNodes[5].childNodes[0].nodeValue;
 			      } else if(len == 0) {
                        var noCustomer = "Customer Does not Exists! Please Provide the mandatory details Below"; 
                        document.getElementById("CusNotAvail").innerHTML = noCustomer;
                        document.NewOfflineOrderForm.customerEmail.value = ""; 
                 }              
              }		 
        }

   }



function hideTextBox()
{
  	if(document.RegisterCustomerForm.customerCustom2.value != -1)
     { 
       if(document.RegisterCustomerForm.customerCustom2.value == 'Others')
         {
           document.RegisterCustomerForm.othersTxtBox.style.visibility='visible';
         }
       else
         {
           document.RegisterCustomerForm.othersTxtBox.style.visibility='hidden';
         }
     }  
 }

