//gregbass.js  copyright Greg Bass

/*FUNCTIOM finddomain is used to transparently get the scripts to work wether local or uploaded */
	function finddomain(){
	if (document.location.host=='localhost'){server = 'http://localhost/websites/gregbass/';}
	else (server = "http://gregbass.com/")
	return server;
}

/*FUNCTION handlezipform handles the zip form */
	function handlezipform(form){
	var zip = form.zip.value;
	var div = "wxdiv";
	var script = "get_wx.php";
	var p1 = '1';
	ajax_div(div, script, p1, zip)	
	}

/*FUNCTION handleapform handles the airport form */
	function handleapform(form){
	var ap = form.ap.value;
	var div = "apwxdiv";
	var script = "get_ap_wx.php";
	var p1 = '2';
//	document.write(ap);
	ajax_div(div, script, p1, ap)	
	}

/*FUNCTIOM printdiv prints just the contents of the div */
	function printSelection(node){

  	var content=node.innerHTML
  	var pwin=window.open('','print_content','width=100,height=100');

  	pwin.document.open();
  	pwin.document.write('<html><body onload="window.print()">'+content+'</body></html>');
  	pwin.document.close();
 
  	setTimeout(function(){pwin.close();},1000);

	}



/* 	FUNCTION ajax-div
	This function changes the displayed contents of any div using an XMLHTTPRequest to a script on the server that produces the desired content.
	The function is called from the page by the "on click" portion of the link tag and is invoked vice the normal link any time javascript is enabled
	in the browser.  This function will thus send data to the server, get the new data back and then display the data into the given div. 
*/
	function ajax_div(div_name, script_name, p1, p2, p3, p4, p5){
//	div_name : the div to be acted on.
//	script_name : the server script to call
//	p* : optional parameters to be passed (must not be skipped, i.e; p4 must be defined before p5)
	div_name = div_name;
	script_name = script_name;
	p1 = p1;
	p2 = p2;
	p3 = p3;
	p4 = p4;
	p5 = p5;
	url = script_name+"?p1="+p1+"&p2="+p2+"&p3="+p3+"&p4="+p4+"&p5="+p5;	
	if (p5==null){url = script_name+"?p1="+p1+"&p2="+p2+"&p3="+p3+"&p4="+p4}
	if (p4==null){url = script_name+"?p1="+p1+"&p2="+p2+"&p3="+p3}
	if (p3==null){url = script_name+"?p1="+p1+"&p2="+p2}
	if (p2==null){url = script_name+"?p1="+p1}
	if (p1==null){url = script_name}
	
// adjust for server location:	
	finddomain()
	url = server + url;
//	need to add time to avoid browser cache:
	url = url+"&t="+new Date().getTime();
//	fade the div 
	new Effect.Fade(div_name, { queue: { position: 'front', scope: 'ajaxdiv'}, from:1.0, to: 0.01, duration: 0.5, delay: 0, afterFinish: function(){bassRequest(url, div_name)} }); //script.aculo.us effects.js vv1.8.1
	
}

//This function performs the XMLHTTP request, writes the new content and fades the div back in
function bassRequest(url, div_name){
	var url = url;
	var div_name = div_name;
	var XMLHttpRequestObject = false;
	if (window.XMLHttpRequest){
		XMLHttpRequestObject = new XMLHttpRequest();
	}
	else {
		if (window.ActiveXObject){
			try{XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {}	
			}
		}

		if(XMLHttpRequestObject){
		XMLHttpRequestObject.open("GET", url);
		XMLHttpRequestObject.onreadystatechange = function(){
			if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
// if the data has been received, replace the content:
			var new_text = XMLHttpRequestObject.responseText;
			document.getElementById(div_name).innerHTML = new_text;
			//fade in the div:	
  	new Effect.Appear(div_name, { queue: {position:'end', scope: 'ajaxdiv'}, from:0.1, to: 1.0, duration: 0.5, delay: 0 }); //script.aculo.us effects.js vv1.8.1
			
//clear memory
				delete XMLHttpRequestObject;
				XMLHttpRequestObject = null;
			}
		}
		XMLHttpRequestObject.send(null);
	}		
	else {
		document.getElementById(div_name).innerHTML = "Sorry, but I couldn't create an XMLHttpRequest";
		Effect.Appear(div_name);
	}
	//fade in the div:	
//  	new Effect.Appear(div_name, { queue: {position:'end', scope: 'ajaxdiv'}, from:0.1, to: 1.0, duration: 0.5, delay: 0 }); //script.aculo.us effects.js vv1.8.1
}




// www.sean.co.uk

function pausecomp(millis)
{
var date = new Date();
var curDate = null;

do { curDate = new Date(); }
while(curDate-date < millis);
} 

//OLD stuff below:
//This function fades the center_content div, gets the new content based on the menu button selected using an httprequest and then fades in the div.
	function ajax_button(page){
	page = page;
	//conditionals to assign php script on server:
	if (page==0){server_script = "ajaxed_home.php"}
	if (page==1){server_script = "ajaxed_weather.php"}
	if (page==2){server_script = "ajaxed_astronomy.php"}
	if (page==3){server_script = "ajaxed_tbd.php"}
	if (page==4){server_script = "ajaxed_tbd.php"}
	if (page==5){server_script = "ajaxed_tbd.php"}
	if (page==6){server_script = "ajaxed_tbd.php"}
	if (page==7){server_script = "ajaxed_tbd.php"}
	if (page==8){server_script = "ajaxed_about.php"}
	
	// adjust for server location:	
	finddomain()
	url = server
	url = url+server_script;
//	document.write(url);
	
	//begin to fade current contents of center content div:
//	Element.setOpacity('center_content', 1.0)  //script.aculo.us effects.js v1.7.0
//	Effect.Fade('center_content', {from:1.0, to: 0.01});  //script.aculo.us effects.js v1.7.0
	new Effect.Fade('center_content', { queue: 'front', scope: 'centerdiv', from:1.0, to: 0.01, duration: 0.5, delay: 0, afterFinish: function(){getDataReturnText(url, display_results)}  }); //script.aculo.us effects.js vv1.8.1	
//  	getDataReturnText(url, display_results)
  
//  	new Effect.Appear('center_content', { queue: 'end', scope: 'centerdiv', from:0.1, to: 1.0, duration: 0.5, delay: 0 }); //script.aculo.us effects.js vv1.8.1
	//Use HTTP request (ajax gold, etc) to get data from php script on server (ajaxed_weather, etc)
//	setTimeout("getDataReturnText(url, display_results)",1000); //use setTimeout to delay for the above fade, need to fade WHILE is sends but it is called from the ajax gold
//	getDataReturnText(url, display_results)
	
	}
	
	//then call another javascript function to write the results into the page (I think) Ajax gold has space for the callback function so it does that step.
	function display_results(text){
	
	document.getElementById("center_content").innerHTML = text;	
	//fade the center_div back in:
	new Effect.Appear('center_content', { queue: 'end', scope: 'centerdiv', from:0.1, to: 1.0, duration: 0.5, delay: 0 }); //script.aculo.us effects.js vv1.8.1
	
	}
	
/*	
//This function does the same as the above does for the center_content div but is more general and does it for any given div (even passing a few peters)//Global variables:
divname = "wx_test_div"; // I need to get rid of this so divname is passed between functions somehow

	
	function ajax_div-old(divname, scriptname, p1, p2, p3, p4){
	divname = divname;
	scriptname = scriptname;
	p1 = p1;
	p2 = p2;
	p3 = p3;
	p4 = p4;
	url = scriptname+"?p1="+p1+"&p2="+p2+"&p3="+p3+"&p4="+p4;	
	if (p4==null){url = scriptname+"?p1="+p1+"&p2="+p2+"&p3="+p3}
	if (p3==null){url = scriptname+"?p1="+p1+"&p2="+p2}
	if (p2==null){url = scriptname+"?p1="+p1}
	if (p1==null){url = scriptname}
	
	
//document.write(divname);		
	//begin to fade current contents of center content div:
	Element.setOpacity(divname, 1.0)  //script.aculo.us effects.js v1.7.0
	Effect.Fade(divname, {from:1.0, to: 0.01});  //script.aculo.us effects.js v1.7.0
//	url = "ajaxed_weather.php?p1=80505&p2=1";
	//Use HTTP request (ajax gold, etc) to get data from php script on server (ajaxed_weather, etc)
	setTimeout("getDataReturnText(url, display_div_results)",1000); //use setTimeout to delay for the above fade, need to fade WHILE is sends but it is called from the ajax gold
//	TO DO: I think I am going to have to get rid of this and do my custom httprequest because I have to pass the divname as well as the text.
	}
	
//This is the same as the second function above but for the generic div
	//then call another javascript function to write the results into the page (I think) Ajax gold has space for the callback function so it does that step.
	function display_div_results-old(text){
//document.write(divname);
//	divname = divname;


// TO DO:

// NEXT FIGURE OUT HOW TO PASS THE DIV NAME TO THIS FUNCITON


	document.getElementById(divname).innerHTML = text;	
	//fade the center_div back in:
	Effect.Appear(divname);  //script.aculo.us effects.js v1.7.0
	
	}

*/