/*------------------------------------------------------
Title:		Movies API Configurator
Updated:	September 19 2008
------------------------------------------------------ */





// generate URL
function generate() {
	if (!document.getElementById) return;
	
	var urlField = get('api_url');
	var urlLaunch = get('launch');
	var iframe = get('api_preview');

	// get spid value
	for (i = 0; i < get('configurator').content_type.length; i++) {
		if(get('configurator').content_type[i].checked == true) {
			var val = get('configurator').content_type[i].value;
			
			if(val == 1) {var type = 'MoviesInLocalTheatres'; var val1 = true;}
			if(val == 2) {var type = 'MovieSummary'; var val2 = true;}
			if(val == 3) {var type = 'TheatresAndShowtimesByMovie'; var val3 = true;}
			if(val == 4) {var type = 'LocalTheatresAndShowtimes'; var val4 = true;}
			if(val == 5) {var type = 'TheatreList'; var val5 = true;}
			if(val == 6) {var type = 'TheatreShowtimes'; var val6 = true;}
			if(val == 7) {var type = 'FutureReleases'; var val7 = true;}
		}		
	}	
	
	var urlString = 'http://' + value('data_environment') + '/movies/' + type + '?rType=' + value('rType');

	// check for JSONA
	if(value('rType')=='jsona') {
		urlString = urlString + '&rVar=' + value('rVar');
	}
	
	urlString = urlString + '&srvcVersion=' + value('srvcVersion');


	// Call Back Function
	if(value('cbf') != '') {	
		urlString = urlString + '&cbf=' + value('cbf');
	}


	// AID Logic
	if(value('aid') != '') {	
		urlString = urlString + '&aid=' + value('aid');
	}
	else {
		alert('You must provide an Affiliate ID');
		get('aid').focus();
		return;
	}


	// Key Logic
	if(value('key') != '') {	
		urlString = urlString + '&key=' + value('key');
	}
	else {
		alert('You must provide a Key');
		get('key').focus();
		return;
	}

	
	// MoviesInLocalTheatres
	if(val1) {
		var geo = true;
		
		if(value('lat_01').length==0 && value('lon_01').length==0) {
			if(value('postal_code_01').length==0) {
				alert('You must provide a Postal Code OR both a Latitude AND Longitude');
				get('postal_code_01').focus();
				return;
			} else {
				var geo = false;
				var postal = true;
			}
		}
		else {
			if(!postal) {
				if(value('lat_01').length==0 || value('lon_01').length==0) {
					alert('You must provide both a Latitude AND Longitude');
					if(value('lat_01').length==0) {get('lat_01').focus();}
					if(value('lon_01').length==0) {get('lon_01').focus();}
					return;
				}
			}
		}
		
		if(postal) {
			urlString = urlString + '&postalCode=' + value('postal_code_01');
		}
		else if(geo) {
			urlString = urlString + '&lat=' + value('lat_01') + '&lon=' + value('lon_01');
		}
		
		
		urlString = urlString + '&country=' + value('country_01');
		
		// if date is set, add to query
		if(value('date_01') != '') {
			urlString = urlString + '&date=' + value('date_01'); 
		}
		
		urlString = urlString + '&numDays=' + value('days_01');

		// if radius is set, add to query
		if(value('radius_01') != '') {
			
			// if the value isn't an integer OR not between 0 and 100
			if(   (isNaN(value('radius_01')))   ||   ( (value('radius_01') < 0)  ||  (value('radius_01') > 100) )   ) {
				alert('Please enter a value between 0 and 100 for the radius.');
				get('radius_01').value = '';
				get('radius_01').focus();
				return;
			}
			
			urlString = urlString + '&radius=' + value('radius_01') + '&radiusUnit=' + value('radius_unit_01'); 
		}
		
		// if Release Horizon is set, add to query
		if(value('horizon_01') != '') {
			
			// if the value isn't an integer OR less than 0
			if(   (isNaN(value('horizon_01')))   ||   (value('horizon_01') < 0)   ) {
				alert('Please enter a number greater than 0 for the Release Horizon.');
				get('horizon_01').value = '';
				get('horizon_01').focus();
				return;
			}
			
			urlString = urlString + '&rhDays=' + value('horizon_01'); 
		}
	}
	
	
	// MovieSummary
	if(val2) {
		
		if(value('movie_id_01').length==0 && value('root_id_01').length==0) {
			alert('You must provide either a Movie ID or a Root ID');
			get('movie_id_01').focus();
			return;
		}
		else {
			if(value('movie_id_01') != '') {	
				urlString = urlString + '&movieId=' + value('movie_id_01');
			}
			else if(value('root_id_01') != '') {	
				urlString = urlString + '&rootId=' + value('root_id_01');
			}
		}
		
		urlString = urlString + '&country=' + value('country_02');
	}
	
	
	// TheatresAndShowtimesByMovie
	if(val3) {
		
		if(value('movie_id_02').length==0 && value('root_id_02').length==0) {
			alert('You must provide either a Movie ID or a Root ID');
			get('movie_id_02').focus();
			return;
		}
		else {
			if(value('movie_id_02') != '') {	
				urlString = urlString + '&movieId=' + value('movie_id_02');
			}
			else if(value('root_id_02') != '') {	
				urlString = urlString + '&rootId=' + value('root_id_02');
			}
		}
		
		var geo = true;
		
		if(value('lat_02').length==0 && value('lon_02').length==0) {
			if(value('postal_code_02').length==0) {
				alert('You must provide a Postal Code OR both a Latitude AND Longitude');
				get('postal_code_02').focus();
				return;
			} else {
				var geo = false;
				var postal = true;
			}
		}
		else {
			if(!postal) {
				if(value('lat_02').length==0 || value('lon_02').length==0) {
					alert('You must provide both a Latitude AND Longitude');
					if(value('lat_02').length==0) {get('lat_02').focus();}
					if(value('lon_02').length==0) {get('lon_02').focus();}
					return;
				}
			}
		}
		
		if(postal) {
			urlString = urlString + '&postalCode=' + value('postal_code_02');
		}
		else if(geo) {
			urlString = urlString + '&lat=' + value('lat_02') + '&lon=' + value('lon_02');
		}
		
		
		urlString = urlString + '&country=' + value('country_03');


		// if date is set, add to query
		if(value('date_02') != '') {
			urlString = urlString + '&date=' + value('date_02'); 
		}
		
		
		urlString = urlString + '&numDays=' + value('days_02');


		// if # of theatres is set, add to query
		if(value('numTheatres_01') != '') {
			urlString = urlString + '&numTheatres=' + value('numTheatres_01'); 
		}


		// if radius is set, add to query
		if(value('radius_02') != '') {
			
			// if the value isn't an integer OR not between 0 and 100
			if(   (isNaN(value('radius_02')))   ||   ( (value('radius_02') < 0)  ||  (value('radius_02') > 100) )   ) {
				alert('Please enter a value between 0 and 100 for the radius.');
				get('radius_02').value = '';
				get('radius_02').focus();
				return;
			}
			
			urlString = urlString + '&radius=' + value('radius_02') + '&radiusUnit=' + value('radius_unit_02'); 
		}

	}
	
	
	
	
	// LocalTheatresAndShowtimes
	if(val4) {
		var geo = true;
		
		if(value('lat_03').length==0 && value('lon_03').length==0) {
			if(value('postal_code_03').length==0) {
				alert('You must provide a Postal Code OR both a Latitude AND Longitude');
				get('postal_code_03').focus();
				return;
			} else {
				var geo = false;
				var postal = true;
			}
		}
		else {
			if(!postal) {
				if(value('lat_03').length==0 || value('lon_03').length==0) {
					alert('You must provide both a Latitude AND Longitude');
					if(value('lat_03').length==0) {get('lat_03').focus();}
					if(value('lon_03').length==0) {get('lon_03').focus();}
					return;
				}
			}
		}
		
		if(postal) {
			urlString = urlString + '&postalCode=' + value('postal_code_03');
		}
		else if(geo) {
			urlString = urlString + '&lat=' + value('lat_03') + '&lon=' + value('lon_03');
		}
		
		
		urlString = urlString + '&country=' + value('country_04');


		// if date is set, add to query
		if(value('date_03') != '') {
			urlString = urlString + '&date=' + value('date_03'); 
		}
		
		
		urlString = urlString + '&numDays=' + value('days_03');


		// if # of theatres is set, add to query
		if(value('numTheatres_02') != '') {
			urlString = urlString + '&numTheatres=' + value('numTheatres_02'); 
		}
		
		// if radius is set, add to query
		if(value('radius_03') != '') {
			
			// if the value isn't an integer OR not between 0 and 100
			if(   (isNaN(value('radius_03')))   ||   ( (value('radius_03') < 0)  ||  (value('radius_03') > 100) )   ) {
				alert('Please enter a value between 0 and 100 for the radius.');
				get('radius_03').value = '';
				get('radius_03').focus();
				return;
			}
			
			urlString = urlString + '&radius=' + value('radius_03') + '&radiusUnit=' + value('radius_unit_03'); 
		}
	}
	
	
	
	// TheatreList
	if(val5) {
		var geo = true;
		
		if(value('lat_04').length==0 && value('lon_04').length==0) {
			if(value('postal_code_04').length==0) {
				alert('You must provide a Postal Code OR both a Latitude AND Longitude');
				get('postal_code_04').focus();
				return;
			} else {
				var geo = false;
				var postal = true;
			}
		}
		else {
			if(!postal) {
				if(value('lat_04').length==0 || value('lon_04').length==0) {
					alert('You must provide both a Latitude AND Longitude');
					if(value('lat_04').length==0) {get('lat_04').focus();}
					if(value('lon_04').length==0) {get('lon_04').focus();}
					return;
				}
			}
		}
		
		if(postal) {
			urlString = urlString + '&postalCode=' + value('postal_code_04');
		}
		else if(geo) {
			urlString = urlString + '&lat=' + value('lat_04') + '&lon=' + value('lon_04');
		}
		
		
		urlString = urlString + '&country=' + value('country_05');


		// if # of theatres is set, add to query
		if(value('numTheatres_03') != '') {
			urlString = urlString + '&numTheatres=' + value('numTheatres_03'); 
		}
		

		// if radius is set, add to query
		if(value('radius_04') != '') {
			
			// if the value isn't an integer OR not between 0 and 100
			if(   (isNaN(value('radius_04')))   ||   ( (value('radius_04') < 0)  ||  (value('radius_04') > 100) )   ) {
				alert('Please enter a value between 0 and 100 for the radius.');
				get('radius_04').value = '';
				get('radius_04').focus();
				return;
			}
			
			urlString = urlString + '&radius=' + value('radius_04') + '&radiusUnit=' + value('radius_unit_04'); 
		}
	}
	
	
	
	// TheatreShowtimes
	if(val6) {
		if(value('theatreId_01') != '') {	
			urlString = urlString + '&theatreId=' + value('theatreId_01');
		}
		else {
			alert('You must provide a Theatre ID');
			get('theatreId_01').focus();
			return;
		}
		
		// urlString = urlString + '&country=' + value('country_06');

		// if date is set, add to query
		if(value('date_04') != '') {
			urlString = urlString + '&date=' + value('date_04'); 
		}
		
		urlString = urlString + '&numDays=' + value('days_04');
	}
	
	
	
	// FutureReleases
	if(val7) {
		// urlString = urlString + '&country=' + value('country_07');


		// if release date is set, add to query
		if(value('date_05') != '') {
			urlString = urlString + '&releaseDate=' + value('date_05'); 
		}


		// if number of release days is set, add to query
		if(value('days_05') != '') {
			
			// if the value isn't an integer OR not between 0 and 180
			if(   (isNaN(value('days_05')))   ||   ( (value('days_05') < 0)  ||  (value('days_05') > 180) )   ) {
				alert('Please enter a value between 0 and 180 for the number of release days.');
				get('days_05').value = '';
				get('days_05').focus();
				return;
			}
			
			urlString = urlString + '&numReleaseDays=' + value('days_05'); 
		}
	}
	
	
	
	
	urlField.value = iframe.src = urlLaunch.href = urlString;
	urlLaunch.style.display = 'block';
}
