var http='';
var origSubmit='';
function FormCheck(ajax,action,next,formid){
	var querystr='';
	var theForm = document.getElementById(formid);
	if (document.getElementById('fullName') && document.getElementById('fullName').value==''){
			alert('Please enter your name');
			document.getElementById('fullName').focus();
			document.getElementById("reqName").style.fontWeight="bold";
			document.getElementById("reqName").style.color="#C0122E";
			return false;
	}
	if (document.getElementById('childName') && document.getElementById('childName').value==''){
			alert('Please enter an age for the child (enter a period (.) to leave blank)');
			document.getElementById('childName').focus();
			document.getElementById("reqChild").style.fontWeight="bold";
			document.getElementById("reqChild").style.color="#C0122E";
			return false;
	}
	if (document.getElementById('childAge') && document.getElementById('childAge').value==''){
			alert('Please enter an age for the child (enter a zero (0) to leave blank)');
			document.getElementById('childAge').focus();
			document.getElementById("reqAge").style.fontWeight="bold";
			document.getElementById("reqAge").style.color="#C0122E";
			return false;
	}
	if (document.getElementById('childFunny') && document.getElementById('childFunny').value==''){
			if (formid=="sendComment"){
				alert('Hey - you forgot your comment!');
			}
			else{
				alert('Hey - you forgot to tell us the funny things your child said!');
			}
			document.getElementById('childFunny').focus();
			document.getElementById("reqFunny").style.fontWeight="bold";
			document.getElementById("reqFunny").style.color="#C0122E";
			return false;
	}
	if(ajax){
		for (i=0;i<theForm.elements.length;i++){
			querystr=querystr+theForm.elements[i].name+'='+ theForm.elements[i].value+'&';
		}
		querystr=querystr+'submit=1';
		origSubmit='<input type="submit" name="submit" value="Submit" id="send"  />';
		document.getElementById('processing').innerHTML="<div align=\'center\'><img src='/images/ajax-loader.gif' style='margin-top:-3px;'></div>";
		ajaxForm(action,querystr,next);
		return false;
	}
	return true;
}
function flagFunny(id){
	if(confirm("Are you sure you wish to flag this funny as inappropriate?")){
			ajaxForm('flagFunny','funnyID='+id,'confirmFlag');
	}
}
function flagComment(id,funID){
	if(confirm("Are you sure you wish to flag this comment as inappropriate?")){
			ajaxForm('flagComment','commentID='+id+'&funnyID='+funID,'confirmFlag');
	}
}
function voteFunny(id){
	ajaxForm('voteFunny','funnyID='+id,'confirmFlag');
}
function getHTTPObject(){
	var xhr=false;
	if(window.XMLHttpRequest){
		xhr=new XMLHttpRequest();
	}
	else if(window.ActiveXObject){
		try{
			xhr=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e){
			try{
				xhr=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e){}
		}
	}
	return xhr;
}

function ajaxForm(action,request_vars,next){
	//window.onload=init;
	//alert(request_vars);
	http='';
	http=getHTTPObject();
	next_function = next;
	http.open('get',  action + '.php?' + request_vars);
	http.onreadystatechange = ajres;
	http.send(null);
}
function ajres() 
{
    if(http.readyState==4)
    {    	
		eval(next_function + "(http.responseText)"); 
	}
}
function confirmFlag(result,param){
	alert(result);
}
function formResult(result,param){
	if(result==0){
		document.getElementById('processing').innerHTML=origSubmit+'<h3>Sorry, there was an error. Try again.</h3>';
	}
	else{
		alert(result);
		document.getElementById('processing').innerHTML='<h3 style=\'margin-top:-3px;\'>'+result+'&nbsp;<a href="http://www.funnythingsmykidssay.com" style="color: #C0122E">View</a></h3>';
	}
}
function commentformResult(result,param){
	if(result==0){
		document.getElementById('processing').innerHTML=origSubmit+'<h3>Sorry, there was an error. Try again.</h3>';
	}
	else{
		alert('Thank you, your comment was added!');
		curWin=document.location.href;
		newWin=curWin.replace('#sendComment','');
		window.location=newWin + '/#comment';
		//document.getElementById('processing').innerHTML='<h3 style=\'margin-top:-3px;\'>'+result+'</h3>';
	}
}


	
	
