var xmlHttp;
var is_ie = (navigator.userAgent.indexOf('MSIE') >= 0) ? 1 : 0; 
var is_ie5 = (navigator.appVersion.indexOf("MSIE 5.5")!=-1) ? 1 : 0; 
var is_opera = ((navigator.userAgent.indexOf("Opera6")!=-1)||(navigator.userAgent.indexOf("Opera/6")!=-1)) ? 1 : 0; 
var is_netscape = (navigator.userAgent.indexOf('Netscape') >= 0) ? 1 : 0; 
var is_safari = (navigator.userAgent.indexOf('Safari') >= 0) ? 1 : 0; 
var url;
var status; 
var selectedMovieID;

function getCinemas(movieID) {
	status = '0';
	var requestURL = '/includes/ajaxSessionsMovie.asp';
	url = requestURL+'?status=0&movieID='+movieID;
	xmlHttp = GetXmlHttpObject(stateChangeHandler);
	xmlHttp_Get(xmlHttp, url);
}

function getSessionDays(movieID){
	status = '1';
	var requestURL = '/includes/ajaxSessionsMovie.asp';
	url = requestURL+'?movieID='+movieID+'&cinema='+document.searchTimes.cinemaCode.value;
	xmlHttp = GetXmlHttpObject(stateChangeHandler);
	xmlHttp_Get(xmlHttp, url);
}

function getSessionTimes(day){
	status = '2';
	var requestURL = '/includes/ajaxSessionsMovie.asp';		
	url = requestURL+ '?movieID='+document.searchTimes.movieID.value+'&cinema='+document.searchTimes.cinemaCode.value+'&day='+day;
	xmlHttp = GetXmlHttpObject(stateChangeHandler);
	xmlHttp_Get(xmlHttp, url);
}

function sendComment(movieID)
{ 
	status = '3';
	if (document.getElementById('name').value == '' || document.getElementById('comment').value == '') {
		errs = "Please enter your name and a comment"
		alert(errs);
		return false;
	}
	document.getElementById('commentForm').style.display='none';
	document.getElementById('loadingImage').style.display='block';
	var name = document.getElementById('name').value;
	var comment = document.getElementById('comment').value;
	var url="/includes/ajaxComments.asp";
	url=url+"?call=1&movieID="+movieID+"&name="+name+"&comment="+comment;
	xmlHttp = GetXmlHttpObject(stateChangeHandler);
	xmlHttp_Get(xmlHttp, url);
}

function updateRating(movieID, vote)
{ 
	status = '4';
	var url="/includes/ajaxComments.asp";
	url=url+"?call=2&movieID="+movieID+"&vote="+vote;
	xmlHttp = GetXmlHttpObject(stateChangeHandler);
	xmlHttp_Get(xmlHttp, url);
}

function getPrizes(confirmationNumber) {
	status = '5';
	var url="/includes/getPrizes.asp";
	url=url+"?confirmationNumber="+confirmationNumber;
	xmlHttp = GetXmlHttpObject(stateChangeHandler);
	xmlHttp_Get(xmlHttp, url);
}

function stateChangeHandler()
{
	var oDiv = null;	
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete'){
		var str = xmlHttp.responseText;	
		if (status == '0') {
		    var cinemaArray  =str.split(",");
		    var cinema;
		    addOption(document.searchTimes.cinemaCode, 0, '', 'Select Cinema', '');
			for(var i=0;i<cinemaArray.length-1;i++) {				
				cinema = cinemaArray[i].split("|")
				addOption(document.searchTimes.cinemaCode, i+1, cinema[0], cinema[1], '');
			}
		}
		if (status == '1') {
			var dayArray = str.split(",");
			var day;
			addOption(document.searchTimes.sessionDate, 0, '', 'Select Day', '');
			for(var i=0;i<dayArray.length-1;i++) {				
				day = dayArray[i].split("|")
				addOption(document.searchTimes.sessionDate, i+1, day[0], day[1], '');
			}			
		} else if (status == '2') {
			var dayArray = str.split(",");
			var day;
			var goldClassText = '';
			document.searchTimes.sessionTime.options.length = 0;
			addOption(document.searchTimes.sessionTime, 0, '', 'Select Time', '');
			//alert(dayArray.length);
			for(var i=0;i<dayArray.length-1;i++) {				
				goldClassText = '';
				day = dayArray[i].split("|")
				if (day[1].match('59') > 0)
					goldClassText = ' (Gold Class)';
				if (day[1].match('70') > 0)
					goldClassText = ' (Silverscreen)';
				if (day[1].match('72') > 0)
					goldClassText = ' (Charity Screening)';
				addOption(document.searchTimes.sessionTime, i+1, day[0], day[0]+goldClassText, '');
				document.getElementById('propertyId').value=day[1].replace(/-/g,',');
			}			
		} else if (status == '3') {
			document.getElementById('loadingImage').style.display='none';
			document.getElementById('commentSent').style.display='block';
		} else if (status == '4') {
			var ratingsArray = str.split("|");
			var thumbsUp = ratingsArray[0];
			var thumbsDown = ratingsArray[1];
			document.getElementById('thumbsDown').innerHTML = thumbsDown;
			document.getElementById('thumbsUp').innerHTML = thumbsUp;
			document.getElementById('enabled').style.display='none';
			document.getElementById('disabled').style.display='inline';
		} else if (status == '5') {
		    document.getElementById('winnersArea').innerHTML = str;
		}					
	}
}

// for adding an item in drop down list
function addOption(dropDownObj, pos, newValue, newText, value) {
	dropDownObj.options[pos] = new Option();
	dropDownObj.options[pos].value = newValue;
	dropDownObj.options[pos].text = newText;
	if (newValue == value && value != '')
	{
		document.searchTimes.movieCode.selectedIndex = pos;
	}	
}

function xmlHttp_Get(xmlhttp, url) {
	xmlhttp.open('GET', url, true);
	xmlhttp.send(null);
} 

function GetXmlHttpObject(handler) { 
	var objXmlHttp = null;   
	if (is_ie){
		var strObjName = (is_ie5) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP';
		try{ 
			objXmlHttp = new ActiveXObject(strObjName);
			objXmlHttp.onreadystatechange = handler;
		}
		catch(e){
			alert('IE detected, but object could not be created. Verify that active scripting and activeX controls are enabled');
			return;
		}
	}
	else if (is_opera){ 
		alert('Opera detected. The page may not behave as expected.'); 
		return;
	}
	else{
		objXmlHttp = new XMLHttpRequest();
		objXmlHttp.onload = handler;
		objXmlHttp.onerror = handler;
	}
	return objXmlHttp;
}		
	
function dynamicSelect()
{
	this.selects = new Array();	
	this.addSelect = function(name)
	{
		this.selects[name] = new selectObj();
	}

	this.updateOptions = function(source, target)
	{
		var form = source.form;
		var target = form.elements[target];
		var value = source.options[source.selectedIndex].value;
		
		while(target.options.length) target.remove(0);		
		if(!this.selects[source.name].options[value])
		{
			//alert('Invalid selection.'); //For debugging while you set it up
			return;
		}
		
		var data = this.selects[source.name].options[value].options;		
		for(var x=0; x<data.length; x++)
		{
			try
			{
				target.add(data[x]);
			}
			catch(e)
			{
				target.add(data[x], null);
			}
		}
		
		target.selectedIndex = 0;
	}

}



function selectObj()
{
	this.options = new Array();	
	this.addOption = function(value)
	{
		this.options[value] = new optionObj();
	}
}



function optionObj()
{
	this.options = new Array();	
	this.createOption = function(name, value)
	{
		this.options[this.options.length] = new Option(name, value);
	}

}	