function createRequestObject() {
	var tmpXmlHttpObject;
	if (window.XMLHttpRequest) {
		tmpXmlHttpObject = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		try {
			tmpXmlHttpObject = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				tmpXmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!tmpXmlHttpObject) {
		alert('Problem creating the XMLHttpRequest object');
	} else {
		return tmpXmlHttpObject;
	}
}

// ajax request objects
// (ie7 needs these separately defined :P)
var db = createRequestObject();
var auth = createRequestObject();
var stsset = createRequestObject();
var linkpo = createRequestObject();
var photos = createRequestObject();
var groups = createRequestObject();
var friend = createRequestObject();

function processAuthResponse() {
    if (auth.readyState == 4) {
        var response = auth.responseText;
       	if (response<1) {
			FB.Connect.showPermissionDialog(perm,authnext);
		} else {
			authnext();
		}
    }
}

function processActionResponse() {
	var object = ajaxObject;
    if (object.readyState == 4) {
        var response = object.responseText;
       	if (response<1) {
			alert('Action not executed.');
		} else {
			commit();
		}
    }
}

function processFriendCheck() {
    if (friend.readyState == 4) {
        var response = friend.responseText;
       	if (response<1) {
       		var warning_text = "Please wait while  Chelios confirms your friend request.<br />If you haven't requested him yet, please do so <a href=\"http://www.facebook.com/profile.php?id="+fid+"\" target=\"_blank\">HERE</a>.<br />(Confirmations can take up to 24 hours. So be patient dude.)";
			top.confirm(warning_text);
		} else {
       		clearInterval(top.activeCheck);
			commit();
		}
    }
}

function processGroupCheck() {
    if (groups.readyState == 4) {
        var response = groups.responseText;
       	if (response<1) {
       		var warning_text = "You haven't joined the group yet. Let me know when you've grown a pair. <a href=\"http://www.facebook.com/group.php?gid="+gid+"\" target=\"_blank\">HERE'S THE LINK</a>.";
			top.confirm(warning_text);
		} else {
       		clearInterval(top.activeCheck);
			commit();
		}
    }
}

function generateTimestamp() {
	var date = new Date();
	var now = date.getTime();
	return now;
}

function get_auth_via_js(perm,next) {
	var warning_text = "What's the matter, chicken? You have to authorize us to complete the challenge to move forward in the contest.";
	var re_auth = function() { get_auth_via_js(perm,next); };
	this.perm = perm;
	this.authnext = function(bool) { if (bool==false) { top.confirm(warning_text,re_auth); } else { next(); }};
	aStamp = generateTimestamp();
	auth.open('get', 'ajax_auth.php?q='+aStamp+'&perm='+perm);
	auth.onreadystatechange = processAuthResponse;
	auth.send(null);
}

function check_friends_via_ajax() {
	this.friendnext = function() { next(); };
	fStamp = generateTimestamp();	
	friend.open('get', 'ajax_fcheck.php?q='+fStamp+'&uid='+uid+'&fid='+fid);
	friend.onreadystatechange = processFriendCheck;
	friend.send(null);
}

function check_group_via_ajax() {
	gStamp = generateTimestamp();	
	groups.open('get', 'ajax_gcheck.php?q='+gStamp+'&uid='+uid+'&gid='+gid);
	groups.onreadystatechange = processGroupCheck;
	groups.send(null);
}

// deprecated!!
//	function challenge_news_feed(bundle,vars,uid,chal,val,next) {
//		this.uid = uid;
//		this.chal = chal;
//		this.val = val;
//		this.followup = next;
//		FB.Connect.showFeedDialog(bundle,'','','','','',update_db,'','');
//		update_db(1,next);
//	}

function challenge_status() {
	this.ajaxObject = stsset;
	this.commit = function() { update_db(1,next); }
	if (text=='') {
		var params = 'method='+'facebook.users.setStatus'+'&clear=true';
	} else {
		var params = 'method='+'facebook.users.setStatus'+'&status='+text+'&clear=false';
	}
	stsset.open('post', 'ajax_action.php', true);
	stsset.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	stsset.setRequestHeader("Content-length", params.length);
	stsset.setRequestHeader("Connection", "close");
	stsset.onreadystatechange = processActionResponse;	
	stsset.send(params);	
}

function challenge_link() {
	this.ajaxObject = linkpo;
	this.commit = function() { update_db(1,next); }
	var params = 'method='+'facebook.links.post'+'&url='+url+'&comment='+comment;
	linkpo.open('post', 'ajax_action.php', true);
	linkpo.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	linkpo.setRequestHeader("Content-length", params.length);
	linkpo.setRequestHeader("Connection", "close");
	linkpo.onreadystatechange = processActionResponse;	
	linkpo.send(params);	
}

function challenge_photo() {
	this.ajaxObject = linkpo;
	this.commit = update_db(1,next);
	var params = 'method='+'facebook.photos.upload'+'&url='+url+'&title='+title;
	photos.open('post', 'ajax_action.php', true);
	photos.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	photos.setRequestHeader("Content-length", params.length);
	photos.setRequestHeader("Connection", "close");
	photos.onreadystatechange = processActionResponse;	
	photos.send(params);	
}

function challenge_friend(fid,uid,chal,val,next) {
	this.fid = fid;
	this.uid = uid;
	this.chal = chal;
	this.val = val;
	this.commit = function() { update_db(1,next); };
	var fb_friend_url = 'http://www.facebook.com/profile.php?id='+fid;
	MM_openBrWindow(fb_friend_url,'_blank','toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=800,height=600');
	top.activeCheck = setInterval(function() { check_friends_via_ajax(); },2000);	
}

function challenge_group(gid,uid,chal,val,next) {
	this.gid = gid;
	this.uid = uid;
	this.chal = chal;
	this.val = val;
	this.commit = function() { update_db(1,next); };
	var fb_group_url = 'http://www.facebook.com/group.php?gid='+gid;
	MM_openBrWindow(fb_group_url,'_blank','toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=800,height=600');
	top.activeCheck = setInterval(function() { check_group_via_ajax(); },2000);
}

function try_challenge_status(text,uid,chal,val,next) {
	this.text = text;
	this.uid = uid;
	this.chal = chal;
	this.val = val;
	this.next = next;
	this.perm = 'status_update'
	get_auth_via_js('status_update',challenge_status);
}

function try_challenge_link(url,comment,uid,chal,val,next) {
	this.url = url;
	this.comment = comment;
	this.uid = uid;
	this.chal = chal;
	this.val = val;
	this.next = next;
	this.perm = 'share_item'
	get_auth_via_js('share_item',challenge_link);
}

function try_photo_upload(url,title,uid,chal,val,next) {
	this.url = url;
	this.title = title;
	this.uid = uid;
	this.chal = chal;
	this.val = val;
	this.next = next;
	this.perm = 'photo_upload'
	get_auth_via_js('photo_upload',challenge_photo);
}

function update_db(bool,followup) {
	if (bool>=1) {
		var params = 'uid='+uid+'&chal='+chal+'&val='+val;
		db.open('post', 'db/update.php', true);
		db.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		db.setRequestHeader("Content-length", params.length);
		db.setRequestHeader("Connection", "close");
		db.send(params);
		if (followup!=''&&followup!=undefined) {
			followup();
		} else {
			setTimeout(function() { top.document.getElementById("challengeFrame").src = "challenge00Next.php"; },2000);
		}
	}
}

function wuss(chalText,chalAction) {
	var moveForward = function() {
		setTimeout(function() { top.confirm(chalText,function() { chalAction(); }); },500);
	}
	top.confirm("If you don't complete this challenge you won't be entered to win the $1,000!",function() { moveForward(); });
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
	window.open(theURL,winName,features);
}

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
function MM_setTextOfLayer(objId,x,newText) { //v9.0
  with (document) if (getElementById && ((obj=getElementById(objId))!=null))
    with (obj) innerHTML = unescape(newText);
}
function MM_showHideLayers() { //v9.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) 
  with (document) if (getElementById && ((obj=getElementById(args[i]))!=null)) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function showDivFunc() {
	MM_showHideLayers('fbContent','','show');
}
function hideDivFunc() {
	MM_showHideLayers('fbContent','','hide');
}