
window.onresize = function(){ resize_main_pic(); }

function resize_main_pic(){
	document.getElementById("main_picture").style.display = "none";
	document.getElementById("body_content").style.display = "none";
	var height = parseInt(document.getElementById("main_table").offsetHeight) - parseInt(document.getElementById("inner_cell").style.paddingTop) - parseInt(document.getElementById("inner_cell").style.paddingBottom) - parseInt(document.getElementById("inner_table").offsetHeight);
	document.getElementById("main_picture").style.display = "";
	document.getElementById("body_content").style.display = "";
	show_picture(height);
}

function fixPNGImage(element) {
  element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + element.src + "')";
  element.src = "images/blank.gif";
}

function textCounter(area,maxlength){
	if(area.value.length >= maxlength){
		alert("You've reached the " + maxlength + " character limit.");
		area.value = area.value.substring(0, maxlength);
		event.returnValue = false;
	}
}


// AJAX
var http = false;
var http2 = false;
var http3 = false;
var http4 = false;
var http5 = false;
var http6 = false;
if(navigator.appName == "Microsoft Internet Explorer"){
	http = new ActiveXObject("Microsoft.XMLHTTP");
	http2 = new ActiveXObject("Microsoft.XMLHTTP");
	http3 = new ActiveXObject("Microsoft.XMLHTTP");
	http4 = new ActiveXObject("Microsoft.XMLHTTP");
	http5 = new ActiveXObject("Microsoft.XMLHTTP");
	http6 = new ActiveXObject("Microsoft.XMLHTTP");
} else {
	http = new XMLHttpRequest();
	http2 = new XMLHttpRequest();
	http3 = new XMLHttpRequest();
	http4 = new XMLHttpRequest();
	http5 = new XMLHttpRequest();
	http6 = new XMLHttpRequest();
}

function show_picture(height){
	if(height == null){ height = "400"; }
	http.open("GET", "lookup/photo.php?height="+height, true);
	http.onreadystatechange=function() {
		if(http.readyState == 4){
			document.getElementById("main_picture").innerHTML = http.responseText;
		}
	}
	http.send(null);
}

function show_posts(pagenum, preload){
	http2.open("GET", "lookup/posts.php?pagenum="+pagenum, true);
	http2.onreadystatechange=function() {
		if(http2.readyState == 4){
			document.getElementById("guestbook_posts").innerHTML = http2.responseText;
		}
	}
	http2.send(null);
	
	if(preload == null){ document.location.href = "#embarrass_megan"; }
}

function show_blog(pagenum){
	if(pagenum == null){ pagenum = ""; }
	http4.open("GET", "lookup/blog.php?pagenum="+pagenum, true);
	http4.onreadystatechange=function() {
		if(http4.readyState == 4){
			document.getElementById("blog_posts").innerHTML = http4.responseText;
		}
	}
	http4.send(null);
}

function show_rules_posts(pagenum, preload){
	http5.open("GET", "lookup/rules_posts.php?pagenum="+pagenum, true);
	http5.onreadystatechange=function() {
		if(http5.readyState == 4){
			document.getElementById("rules_posts").innerHTML = http5.responseText;
		}
	}
	http5.send(null);
	
	if(preload == null){ document.location.href = "#recessionistas"; }
}


//Guestbook Page
function validate_guestbook_form(){
	reset_guestbook_form();
	
	var required = "";
	if (document.theform.name.value.length == 0) { required += "\n     - Name"; document.theform.name.className='textboxRequired'; }
	if (document.theform.email.value.length == 0) { required += "\n     - Email"; document.theform.email.className='textboxRequired'; }
	else if (document.theform.email.value.search(/(\w+[\w|\.|-]*\w+)(@\w+[\w|\.|-]*\w+\.\w{2,4})/) == -1) { required += "\n     - Please specify a valid email address"; document.theform.email.className='textboxRequired'; }
	if (document.theform.comment.value.length == 0) { required += "\n     - Comment"; document.theform.comment.className='textboxRequired'; }
	if (document.theform.confirm1.value != document.theform.confirm2.value) { required += "\n     - Re-type Word"; document.theform.confirm2.className='textboxRequired'; }
	
	if (required != "") {
		alert("Please fill in the required fields:" + required);
		return false;
	
	} else {
		submit_guestbook_form();
		return false;
	}
}

function clear_guestbook_form(){
	var x = confirm("Are you sure you want to erase all of your data?");
	if(x){
		reset_guestbook_form();
		document.theform.reset();
	}
}

function reset_guestbook_form(){
	document.theform.name.focus();
	document.theform.name.className='textbox';
	document.theform.email.className='textbox';
	document.theform.comment.className='textbox';
	document.theform.confirm2.className='textbox';
}

function submit_guestbook_form(){
	document.theform.submit_button.disabled = "true";
	
	http3.open("POST", "lookup/guestbook.php", true);
	http3.onreadystatechange=function() {
		if(http3.readyState == 4){
			 if (http3.status == 200){
			 	document.getElementById("guestbook_form").innerHTML = http3.responseText;
			}
		}
	}
	http3.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	http3.send("name="+escape(document.theform.name.value)+"&email="+escape(document.theform.email.value)+"&comment="+escape(document.theform.comment.value)+"&confirm1="+escape(document.theform.confirm1.value)+"&confirm2="+escape(document.theform.confirm2.value));
}


//Rules Page
function validate_rules_form(){
	reset_rules_form();
	
	var required = "";
	if (document.rulesform.name.value.length == 0) { required += "\n     - Name"; document.rulesform.name.className='textboxRequired'; }
	if (document.rulesform.email.value.length == 0) { required += "\n     - Email"; document.rulesform.email.className='textboxRequired'; }
	else if (document.rulesform.email.value.search(/(\w+[\w|\.|-]*\w+)(@\w+[\w|\.|-]*\w+\.\w{2,4})/) == -1) { required += "\n     - Please specify a valid email address"; document.rulesform.email.className='textboxRequired'; }
	if (document.rulesform.comment.value.length == 0) { required += "\n     - Comment"; document.rulesform.comment.className='textboxRequired'; }
	if (document.rulesform.confirm1.value != document.rulesform.confirm2.value) { required += "\n     - Re-type Word"; document.rulesform.confirm2.className='textboxRequired'; }
	
	if (required != "") {
		alert("Please fill in the required fields:" + required);
		return false;
	
	} else {
		submit_rules_form();
		return false;
	}
}

function clear_rules_form(){
	var x = confirm("Are you sure you want to erase all of your data?");
	if(x){
		reset_rules_form();
		document.rulesform.reset();
	}
}

function reset_rules_form(){
	document.rulesform.name.focus();
	document.rulesform.name.className='textbox';
	document.rulesform.email.className='textbox';
	document.rulesform.comment.className='textbox';
	document.rulesform.confirm2.className='textbox';
}

function submit_rules_form(){
	document.rulesform.submit_button.disabled = "true";
	
	http6.open("POST", "lookup/rules.php", true);
	http6.onreadystatechange=function() {
		if(http6.readyState == 4){
			 if (http6.status == 200){
			 	document.getElementById("rules_form").innerHTML = http6.responseText;
			}
		}
	}
	http6.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	http6.send("name="+escape(document.rulesform.name.value)+"&email="+escape(document.rulesform.email.value)+"&comment="+escape(document.rulesform.comment.value)+"&confirm1="+escape(document.rulesform.confirm1.value)+"&confirm2="+escape(document.rulesform.confirm2.value));
}


// SLIDER **************************************************************************
var sliding = false;
var sliderIntervalId = 0;
var currentContainer = 3;
var newSpeed = 0;
var oldSpeed = 0;
var newHeight = 0;
var oldHeight = 0;
var maxHeight = 0;
var minHeight = 39;
var slideSpeed = 7;
if(navigator.appVersion.indexOf("MSIE 7.") != -1){ slideSpeed = 2; }

function slide(elem){
	var new_slider = document.getElementById("container_"+elem);
	var old_slider = document.getElementById("container_"+currentContainer);
	
	if(new_slider.offsetHeight == minHeight && !sliding){
		window.onresize = "";
		
		// SET THE CURRENT AND MAX HEIGHTS
		oldHeight = parseInt(document.getElementById("container_inner_"+currentContainer).offsetHeight);
		maxHeight = parseInt(document.getElementById("container_inner_"+elem).offsetHeight);
		newHeight = minHeight;
		/*oldHeight = parseInt(old_slider.offsetHeight);
		newHeight = minHeight;
		new_slider.style.height = "auto";
		maxHeight = new_slider.offsetHeight;
		new_slider.style.height = minHeight;*/
		
		// SET THE SLIDE SPEED
		newSpeed = parseFloat(Math.abs(maxHeight - minHeight) / slideSpeed);
		oldSpeed = parseFloat(Math.abs(oldHeight - minHeight) / slideSpeed);
		
		// CHANGE THE OPEN/CLOSED INDICATORS
		document.getElementById("header_"+currentContainer+"_on").style.display = "none";
		document.getElementById("header_"+currentContainer).style.display = "";
		document.getElementById("header_"+elem+"_on").style.display = "";
		document.getElementById("header_"+elem).style.display = "none";
		document.getElementById("arrow_"+currentContainer+"_on").style.display = "none";
		document.getElementById("arrow_"+currentContainer).style.display = "";
		document.getElementById("arrow_"+currentContainer+"_up").style.display = "none";
		document.getElementById("arrow_"+elem+"_on").style.display = "none";
		document.getElementById("arrow_"+elem).style.display = "none";
		document.getElementById("arrow_"+elem+"_up").style.display = "";
		
		// BEGIN SLIDING
		sliding = true;
		sliderIntervalId = setInterval("slide_action('"+elem+"')", 30);
	}
}

function slide_action(elem){
	var new_slider = document.getElementById("container_"+elem);
	var old_slider = document.getElementById("container_"+currentContainer);
	
	// EXPANDING
	if(newHeight < maxHeight || oldHeight > minHeight){
		newHeight += newSpeed;
		oldHeight -= oldSpeed;
		if(newHeight > maxHeight){ newHeight = maxHeight; }
		if(oldHeight < minHeight){ oldHeight = minHeight; }
		new_slider.style.height = newHeight + "px";
		old_slider.style.height = oldHeight + "px";
		
	// DONE
	} else if(newHeight == maxHeight && oldHeight == minHeight){
		new_slider.style.cursor = "default";
		old_slider.style.cursor = "pointer"; old_slider.style.cursor = "hand";
		currentContainer = elem;
		window.onresize = function(){ resize_main_pic(); }
		sliding = false;
		clearInterval(sliderIntervalId);
		new_slider.style.height = "auto";
	}
}

function toggle_header(elem){
	if(document.getElementById("container_"+elem).offsetHeight == minHeight){
		if(document.getElementById("header_"+elem).style.display == "none"){
			document.getElementById("header_"+elem+"_on").style.display = "none";
			document.getElementById("header_"+elem).style.display = "";
			document.getElementById("arrow_"+elem+"_on").style.display = "none";
			document.getElementById("arrow_"+elem).style.display = "";
		} else {
			document.getElementById("header_"+elem+"_on").style.display = "";
			document.getElementById("header_"+elem).style.display = "none";
			document.getElementById("arrow_"+elem+"_on").style.display = "";
			document.getElementById("arrow_"+elem).style.display = "none";
		}
	}
}
