//
// 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(",");
    }
   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);
 }
 }
 }
 }
 }


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';
         }
     }  
 }

