var showing_submit = false;
var showing_more = false;
var browser_type = 0;

function browser_sniffer() {
//	browser_type = <?php echo "// HOho: "  . $_SESSION['agent'] . "<br>"; ?>;
}

function hoho() {
	/*
	document.getElementById('what_am_i_div').innerHTML=''; 
	document.getElementById('td1').innerHTML='School: '; 
	document.getElementById('td2').innerHTML='<input type=text name=school id=school>';
	//show_more(); 
	
		document.getElementById('rest_of_stuff').style.visibility='visible';
		document.getElementById('rest_of_stuff').style.display='block';
		document.getElementById('rest_of_stuff').style.visibility='block';
		//show('rest_of_stuff');
		document.getElementById('rest_of_stuff').style.position='relative';
		show("sign_up_buttons");
		
	document.getElementById('school').focus();
	*/
}

function am_student() {
	document.getElementById('what_am_i_div').innerHTML=''; 
	document.getElementById('td1').innerHTML='School: '; 
	document.getElementById('td2').innerHTML='<input type=text name=school id=school>';
	show_more(1); 
	/*	
		document.getElementById('rest_of_stuff').style.visibility='visible';
		document.getElementById('rest_of_stuff').style.display='block';
		document.getElementById('rest_of_stuff').style.visibility='block';
		//show('rest_of_stuff');
		document.getElementById('rest_of_stuff').style.position='relative';
		show("sign_up_buttons");
		*/
	document.getElementById('school').focus();
}


function am_instructor() {
	document.getElementById('what_am_i_div').innerHTML=''; 
	document.getElementById('td1').innerHTML='Institution:'; 
	document.getElementById('td2').innerHTML='<input type=text name=institution id=institution>'; 
	document.getElementById('td3').innerHTML='Department:'; 
	document.getElementById('td4').innerHTML='<input type=text name=department id=department>'; 
	document.getElementById('td5').innerHTML='Dept Telephone:'; 
	document.getElementById('td6').innerHTML='<input type=text name=dept_telephone id=dept_telephone>';
	show_more(2); 
	document.getElementById('institution').focus();
}


function am_neither() {
	document.getElementById('what_am_i_div').innerHTML='';
	show_more(0); 
	//document.getElementById('rest_of_stuff').style.visibility='visible';
	//document.getElementById('rest_of_stuff').style.position='relative';
}  // am_neither()


	//*********************************************
	// Display related utilities
	//*********************************************

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			//alert("1. curtop is now " + curtop);
			obj = obj.offsetParent;
		}
	} else if (obj.y) {
			//alert("2. curtop is now " + curtop);
		curtop += obj.y;
	}
	//alert("findPosY returning " + curtop);
	return curtop;
}


function findPosYY(obj)
{
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			//alert("1. curtop is now " + curtop);
			obj = obj.offsetParent;
		}
	} else if (obj.y) {
			//alert("2. curtop is now " + curtop);
		curtop += obj.y;
	}
	//alert("findPosY returning " + curtop);
	return curtop;
}


	//*********************************************
	// Hide / Show
	//*********************************************

//=====================================================================
/*
if (document.all) { // note, this code would also run in IE5+, which has partial support
  IE4+ code         // for the W3C DOM. You could also add check for !DOM 
                    // here (&& !(document.getElementById)) and equate dissimilar IE/W3C
                    // DOMs with prototype function, and run all IE5+/NS6+ browsers with
                    // only DOM-standards-compliant code. IE5+ browsers would then drop 
                    // down to third elseif below. 
} else if (document.layers) {
  NS4+ code 
} else if (document.getElementById) {
  NS6+ code         // and IE5+ code you flatten differences between MS DOM and W3C DOM
} 
*/
//=====================================================================
function hide(nr) {
	if (document.layers) {
		document.layers[nr].display = 'none';
		document.layers[nr].visibility = 'hide';
	} else if (document.all) {
		document.all[nr].style.display = 'none';
		document.all[nr].style.visibility = 'hidden';
	} else if (document.getElementById) {
		document.getElementById(nr).style.display = 'none';
		document.getElementById(nr).style.visibility = 'hidden';
	}
}  // hide()

function show(nr) {
	if (document.layers) {
		document.layers[nr].display = 'block';
		document.layers[nr].visibility = 'show';
	} else if (document.all) {
		document.all[nr].style.display = 'block';
		document.all[nr].style.visibility = 'visible';
	} else if (document.getElementById) {
		document.getElementById(nr).style.display = 'block';
		document.getElementById(nr).style.visibility = 'visible';
	}
}  // show()




	//*********************************************
	// Email checking
	//*********************************************


function check_email() {
	var email = document.getElementById('email_up').value;
	var cmd = "../utils/email.php?email=" + email;
	http.open("GET", cmd, true); 
	http.onreadystatechange = check_email_response; 
	http.send(null);
}
function check_email_response() {
	if (http.readyState == 4) {
		status = http.responseText;
		if (status == '0') {	// Okay!
			show_sign_up_buttons();
		} else if (status == '1') {		// bad e-mail formatting
			show_sign_up_error("Invalid e-mail address");
		} else if (status == '2') {		// e-mail address already in use
			show_sign_up_error("E-mail address already used!");			
		}
	}
}

function show_sign_up_buttons() {
	var sign_up_error = document.getElementById("sign_up_error");
	sign_up_error.innerHTML = "";
	hide("sign_up_error");
	//alert("show_sign_up_buttons");
	show_submit();
}

function show_submit(n) {
	showing_submit = true;
	//alert("hoho: more is " + showing_more + " and submit is " + showing_submit);
	if (showing_more) {
		document.getElementById('rest_of_stuff').style.visibility='visible';
		if (document.layers) { // netscape
			//alert("d.layers!"); 
		} else if (document.all) {
			//alert("d.all!");
			// IE 6
			if (n==1) {  // student
				document.getElementById('sign_up_div').style.height = '300px';
				document.getElementById('rest_of_stuff').style.left = '100px';
				document.getElementById('rest_of_stuff').style.top = '270px';
			} else if (n==2) {  // instructor
				document.getElementById('sign_up_div').style.height = '350px';
				document.getElementById('rest_of_stuff').style.left = '100px';
				document.getElementById('rest_of_stuff').style.top = '320px';
			} else if (n==0) { // neither
				document.getElementById('sign_up_div').style.height = '260px';
				document.getElementById('rest_of_stuff').style.left = '100px';
				document.getElementById('rest_of_stuff').style.top = '230px';
			}
			document.getElementById('rest_of_stuff').style.dsiplay='block';
		} else if (document.getElementById) {
			//alert("d.getElementById!");
			// FF 1.5
			document.getElementById('rest_of_stuff').style.position='relative';
		} else {
			//alert("d.???");
		}
			
		/*
		document.getElementById('rest_of_stuff').style.position='relative';
		document.getElementById('rest_of_stuff').style.position='absolute';
		*/
		//show('rest_of_stuff');
		show("sign_up_buttons");
	}
}

function show_more(n) {
//alert("a");
	showing_more = true;
//alert("b: show_more() showing_submit is " + showing_submit + ", showing_more is " + showing_more);
	if (true || showing_submit) {
	//alert("c");

		show_submit(n);
//alert("d");
	}
}

function show_sign_up_error(err) {
	hide("sign_up_buttons");
	var sign_up_error = document.getElementById("sign_up_error");
	sign_up_error.innerHTML = "<font color=#ff0000>" + err + "</font>";
	var sign_up_email_input = document.getElementById("email_up");
	sign_up_email_input.style.backgroundColor = "red";
	show("sign_up_error");
}

function clear_sign_up() {
	var sign_up_email_input = document.getElementById("email_up");
	sign_up_email_input.style.backgroundColor = "white";	
}


	//*********************************************
	// Ajax Stuff
	//*********************************************
	
	//=============================================
	// test_ajax
	//=============================================
	function test_ajax() {
		http.open("GET", "../sign-in/test.php");
		http.onreadystatechange = test_ajax_response; 
		http.send(null);
	}  // test_ajax()
	
	function test_ajax_response() {
		if (http.readyState == 4) {
			var response = http.responseText;
			alert(response);
		}
	} // test_ajax_response()


	//=============================================
	// Login check
	//=============================================
	var da_caller;
	//function xxxx() { alert("xxxx"); }
	/*
	function is_logged_in(p, el_name) {
		da_caller = el_name;		
		var addr = '../sign-in/sign-in-or-sign-up.php?p=' + p;
		http.open("GET", addr);
		http.onreadystatechange = is_logged_in_response; // is_logged_in_response; 
		http.send(null);
	}  // is_logged_in()

	function is_logged_in_response() {
		if (http.readyState == 4) {
			var response = http.responseText;
			//alert("Got response [" + response + "]");
			if (response == '1') return;
			var caller = document.getElementById(da_caller);
			var cx = findPosX(caller);
			var cy = findPosY(caller);
			var siosu = document.getElementById('sign-in-or-sign-up');
			siosu.innerHTML = response;
			siosu.style.left = cx;
			siosu.style.top = cy;
			siosu.style.width = 450;
			siosu.style.zIndex = 7;
		}
	} // is_logged_in_response()
*/

/*******************/
function is_logged_in(p, el_name) { // xmlhttpPost(strURL) {
	da_caller = el_name;		
	var strURL = '../sign-in/sign-in-or-sign-up.php?p=' + p;
	//http.open("GET", addr);
	//http.onreadystatechange = is_logged_in_response; // is_logged_in_response; 
	//http.send(null);

	//var strURL = '../tmp/ha.php';
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
			var response = self.xmlHttpReq.responseText;
        //    alert(response);
			//alert("Got response [" + response + "]");
			if (response == '1') return;
			var caller = document.getElementById(da_caller);
			var cx = findPosX(caller);
			var cy = findPosY(caller);
			var siosu = document.getElementById('sign-in-or-sign-up');
			siosu.innerHTML = response;
			siosu.style.left = cx;
			siosu.style.top = cy;
			siosu.style.width = 450;
			siosu.style.zIndex = 7;
        }
    }
    self.xmlHttpReq.send(strURL); //getquerystring());
}

function getquerystring() {
    var form     = document.forms['f1'];
    var word = form.word.value;
    qstr = 'w=' + escape(word);  // NOTE: no '?' before querystring
    return qstr;
}

function updatepage(str){
    document.getElementById("result").innerHTML = str;
}

/*******************/


	//=============================================
	// Check 'n' Change
	//=============================================
	function check_n_change(p) {
		http.open("GET", "../sign-in/sign-in-or-sign-up.php?p=" + p);
		http.onreadystatechange = check_n_change_response; 
		http.send(null);		
	}  // check_n_change()
	
	function check_n_change_response(p) {
		if (http.readyState == 4) {
			var response = http.responseText;
			//alert("Got response [" + response + "]");
			if (response == '1') {
				document.getElementById('check-n-change').innerHTML = 'Get Okay';
			} else {
				document.getElementById('check-n-change').innerHTML = response;
			}
		}
	}  // check_n_change()
	

	//=============================================
	// Sign In
	//=============================================
	function sign_in(p) {
		http.open("GET", "../sign-in/sign-in.php?p=" + p);
		http.onreadystatechange = sign_in_response; 
		http.send(null);
	}
	function sign_in_response() {
		if (http.readyState == 4) {
			var response = http.responseText;
			//alert(response);
		}
	}


	//=============================================
	// Ajax Get Mobile Prep
	//=============================================
	function get_mobile_prep() {
	}


	//=============================================
	// Ajax support stuff
	//=============================================
	function getHTTPObject() { 
		var xmlhttp; 
		/*@cc_on 
		@if (@_jscript_version >= 5) 
			try { 
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
			} catch (e) { 
				try { 
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
				} catch (E) { 
					xmlhttp = false; 
				} 
			} 
		@else xmlhttp = false; 
		@end @*/  
		if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { 
			try { 
				xmlhttp = new XMLHttpRequest(); 
			} catch (e) { 
				xmlhttp = false; 
			} 
		} 
		return xmlhttp; 
	} 
	var http = getHTTPObject(); // We create the HTTP Object 
