// JavaScript Document
//First step in the authorization process
//ping netflix the first time with attached php proxy
//and get back new url to redirect the user
//alert("first js is attached - pinging netflix for the first time");
var request=null;
var secondRequest=null;
//alert("proxy.php attached to send remote request");
var DEBUG =false;
var newTotal = null;
//create the request
function createRequest () {
try {
		if(DEBUG) alert("trying Mozilla XMLHTTP request object");
		request = new XMLHttpRequest(); //firefox/mozilla method
		}
		catch(trymicrosoft) {
			try {
				if(DEBUG) alert("trying new Msft XMLHTTP request object");
				request = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(othermicrosoft) {
					try {
						if(DEBUG) alert("trying old Msft XMLHTTP request object");
						request = new ActiveXObject("Microsoft.XMLHTTP");
						}
						catch(giveup) {
							if(DEBUG) alert("can't send request");
							request = null;
							}
					
						}
				}
		}
		
		
		
		function createNewRequest () {
try {
		if(DEBUG) alert("trying Mozilla XMLHTTP request object");
		newRequest = new XMLHttpRequest(); //firefox/mozilla method
		}
		catch(trymicrosoft) {
			try {
				if(DEBUG) alert("trying new Msft XMLHTTP request object");
				newRequest = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(othermicrosoft) {
					try {
						if(DEBUG) alert("trying old Msft XMLHTTP request object");
						newRequest = new ActiveXObject("Microsoft.XMLHTTP");
						}
						catch(giveup) {
							if(DEBUG) alert("can't send request");
							newRequest = null;
							}
					
						}
				}
		}
		

					
//where the maaaaagic happens				
function getTheNewURL () {
	//alert("in get thhe new url function");
	createRequest();
	//separate php file has my url and dynamic info in it
	var url = "proxy.php";
	request.open("GET", 'proxy.php',true);
	//call the update page function every time the ready state changes
	request.onreadystatechange = updatePage;
	request.send(null);
}

function updatePage() {
	if(request.readyState == 4) {
		//alert("readyState 4");
		newTotal = request.responseText;
		//alert("The response text is " + newTotal);
		
		/*
		createNewRequest();

		//var url = "proxy.php";
		newRequest.open("GET", 'webservice.php',true);
		//call the update page function every time the ready state changes
		newRequest.onreadystatechange = updatePage;
		newRequest.send(newTotal);*/
		
		
		
		
		
		
		
		window.location = newTotal;
			
	}
}


//inspect the browser object model to get current url
//split off what i need to split off
//

/*

function createTheSecondRequest () {
try {
		if(DEBUG) alert("trying Mozilla XMLHTTP request object");
		secondRequest = new XMLHttpRequest(); //firefox/mozilla method
		}
		catch(trymicrosoft) {
			try {
				if(DEBUG) alert("trying new Msft XMLHTTP request object");
				secondRequest = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(othermicrosoft) {
					try {
						if(DEBUG) alert("trying old Msft XMLHTTP request object");
						secondRequest = new ActiveXObject("Microsoft.XMLHTTP");
						}
						catch(giveup) {
							if(DEBUG) alert("can't send request");
							secondRequest = null;
							}
					
						}
				}
		}



//where the maaaaagic happens				
function assembleTheURL () {
	alert("in get the new url function");
	createTheSecondRequest();
	//separate php file has my url and dynamic info in it
	//can i send the response text to the second php doc for dissasembling?
	var url = "proxy.php";
	request.open("GET", 'proxy.php',true);
	//call the update page function every time the ready state changes
	request.onreadystatechange = updatePageAgain;
	request.send(null);
}

function updatePageAgain() {
	if(request.readyState == 4) {
		alert("readyState 4");
		var newTotal = request.responseText;
		//alert("The response text is " + newTotal);
	}
}

*/




