function in_array(needle,haystack,strict){
	var key = '';
 
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }
 
    return false;
}

function ajax_object(){
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
		return ajaxRequest;
	} 
	catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			return ajaxRequest;
		} 
		catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
				return ajaxRequest;
			} 
			catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
}

function ajaxDesignVote(design,voteValue,userId){
	ao = ajax_object();
	ao.onreadystatechange = function(){
		if(ao.readyState == 4){
			document.getElementById("designVote"+design).innerHTML = ao.responseText;
		}
	}
	ao.open("GET", "/ajax/design_voting.php?design_id="+design+"&vote_value="+voteValue+"&user_id="+userId, true);
	ao.send(null);
}

function ajaxMainDesignVote(design,voteValue,userId,nextDesignLink){
	ao = ajax_object();
	ao.onreadystatechange = function(){
		if(ao.readyState == 4){
			document.getElementById("votingdetail").innerHTML = ao.responseText;
		}
	}
	ao.open("GET", "/ajax/design_main_voting.php?design_id="+design+"&vote_value="+voteValue+"&user_id="+userId+"&next_design_link="+nextDesignLink, true);
	ao.send(null);
}

function ajaxDesignVote2(design,voteValue){
	ao = ajax_object();
	ao.onreadystatechange = function(){
		if(ao.readyState == 4){
			document.getElementById("designVote"+design).innerHTML = ao.responseText;
		}
	}
	ao.open("GET", "ajax/design_voting2.php?design_id="+design+"&vote_value="+voteValue, true);
	ao.send(null);
}

function ajaxRequestReferral(form){
	form.disabled=true;
	p = $("#"+form.id+" p.message");
	p.hide('fast',function(){p.html("Please wait...");}).show('fast');
	// tell the user to wait
	ao = ajax_object();
	ao.onreadystatechange = function(){
		if(ao.readyState == 4){
			p.hide('fast',function(){p.html(ao.responseText);}).show('fast');
			if (ao.responseText != "We are now sending invites to all your friends!"){
				form.disabled = false;				
			} else {
				$("#openinviter").hide('fast',function(){$("#openinviter").html(ao.responseText);}).show('fast');
			}
		}
	}
	with (form){
		// to do: get this AJAX working properly by passing stuff through AJAX rather than the query string
		qstring = "/ajax/referral.php?";
		allowed = new Array();
		aCount = 0;
		for(var i = 0; i < elements.length; i++){
			if (elements[i].name.match('_')){
				elsp = elements[i].name.split('_');
				// if it's got an underscore, it's part of the list of people
				if (elements[i].type == 'checkbox' && elements[i].checked == 1){
					// add to array of allowed elements
					aCount++;
					if (aCount < 50){
						allowed.push(elsp[elsp.length - 1]);
					}
				}
				// if name is in allowed elements, add it to the query string
				if (in_array(elsp[elsp.length-1],allowed,false)){
					qstring = qstring + elements[i].name + "=" + elements[i].value + "&";
				}
			}
			else {
				qstring = qstring + elements[i].name + "=" + elements[i].value + "&";
			}
		}
		qstring = qstring.substring(0,qstring.length-1);
		ao.open("GET", qstring, true);
		ao.send(null);
	}
}

function ajaxSlogan(form){
	form.disabled = true;
	p = $("p.slogan_message");
	p.hide('fast',function(){p.html("Please wait...");}).show('fast');
	// tell the user to wait
	ao = ajax_object();
	ao.onreadystatechange = function(){
		if(ao.readyState == 4){
			p.hide('fast',function(){p.html(ao.responseText);}).show('fast');
			if (ao.responseText != "We are checking your slogan, it should appear on the website soon!"){
				form.disabled = false;
			}
		}
	}
	with (form){
		ao.open("GET", "/ajax/slogan_input.php?slogan="+description.value, true);
		ao.send(null);
	}
}

function ajaxDesignComment(form){
	form.disabled = true;
	ao = ajax_object();
	with (form){
		ao.onreadystatechange = function(){
			if(ao.readyState == 4){
				$("p.noComments").hide('slow');
				$("ul.comments").append(ao.responseText);
				$("ul.comments li:last").show('slow');
				form.disabled = false;
			}
		}	
		ao.open("GET", "/ajax/comment_design_input.php?comment="+comment.value+"&design_id="+design_id.value, true);
		ao.send(null);
	}
}

function ajaxHomepageComment(form){
	form.disabled = true;
	ao = ajax_object();
	with (form){
		ao.onreadystatechange = function(){
			if(ao.readyState == 4){
				$("div.feedbackcomment:first").before(ao.responseText);
				$("div.feedbackcomment:first").show('slow');
				form.disabled = false;
			}
		}	
		ao.open("GET", "/ajax/comment_design_input.php?comment="+comment.value+"&design_id="+design_id.value, true);
		ao.send(null);
	}
}

function ajaxUserComment(form){
	form.disabled = true;
	ao = ajax_object();
	ao.onreadystatechange = function(){
		if(ao.readyState == 4){
			$("p.noComments").hide('slow');
			$("ul.comments").append(ao.responseText);
			$("ul.comments li:last").show('slow');
			form.disabled = false;
		}
	}
	with (form){
		ao.open("GET", "/ajax/comment_user_input.php?comment="+comment.value+"&user_id="+user_id.value, true);
		ao.send(null);
	}
}

function ajaxFollow(follower,followee,username){
	ao = ajax_object();
	ao.onreadystatechange = function(){
		if(ao.readyState == 4){
			$("#followersbox").html('<p>You are now following '+username+'!</p>');
		}
	}
	ao.open("GET", "/ajax/create_following.php?follower="+follower+"&followee="+followee, true);
	ao.send(null);
}
function ajaxFollowBlog(user_id,blog_id){
	ao = ajax_object();
	ao.onreadystatechange = function(){
		if(ao.readyState == 4){
			$("#blogFollow"+blog_id).html(ao.responseText);
		}
	}
	ao.open("GET", "/ajax/blog_follow.php?user_id="+user_id+"&blog_id="+blog_id, true);
	ao.send(null);
}

function ajaxUnfollowBlog(user_id,blog_id){
	ao = ajax_object();
	ao.onreadystatechange = function(){
		if(ao.readyState == 4){
			$("#blogFollow"+blog_id).html(ao.responseText);
		}
	}
	ao.open("GET", "/ajax/blog_unfollow.php?user_id="+user_id+"&blog_id="+blog_id, true);
	ao.send(null);
}

function ajaxPostBlogComment(form){
	ao = ajax_object();
	with (form){
		if (comment.value.length == 0) {
			return false;
		}
		// turn off the form
		$('#postacomment'+reply_to_comment.value).toggle();
		ao.onreadystatechange = function(){	
			if(ao.readyState == 4){
				// alert(ao.responseText);
				if (reply_to.value != 0){
					$("#blogcomment"+reply_to.value).after(ao.responseText);
				} else {
					$(".blogdetailwrap").append(ao.responseText);
				}
			}
		}
		ao.open("GET", "/ajax/post_blog_comment.php?comment="+comment.value+"&blog_id="+blog_id.value+"&user_id="+user_id.value+"&reply_to="+reply_to.value+"&reply_to_comment="+reply_to_comment.value+"&depth="+depth.value, true);
		ao.send(null);
	}
}

function ajaxSubmitSlogan(form){
	ao = ajax_object();
	with (form){
		$("#sloganSubmitInfo").hide('fast');
		ao.onreadystatechange = function(){
			if(ao.readyState == 4){
				$(".message").html(ao.responseText);
				$(".message").show('fast');
			}
		}
		ao.open("GET", "/ajax/slogan_input.php?slogan="+slogan.value, true);
		ao.send(null);
	}
}

function ajaxSloganVoteTosh(slogan_id,user_id){
	if ($("#thatstosh"+slogan_id).className == 'thatstoshActive'){
		return false;
	}
	ao = ajax_object();
	ao.onreadystatechange = function(){
		if(ao.readyState == 4){
			// alert(ao.responseText);
			if (ao.responseText == 'success'){
				$("#thatstosh"+slogan_id).addClass('thatstoshActive');
				$("#thatstosh"+slogan_id).removeClass('thatstosh');
				$("#thatsswishswosh"+slogan_id).addClass('thatsswishswosh');	
				$("#thatsswishswosh"+slogan_id).removeClass('thatsswishswoshActive');
			}
		}
	}
	ao.open("GET", "/ajax/slogan_vote_tosh.php?slogan_id="+slogan_id+"&user_id="+user_id, true);
	ao.send(null);
}

function ajaxSloganVoteSwishswosh(slogan_id,user_id){
	if ($("#thatsswishswosh"+slogan_id).className == 'thatsswishswoshActive'){
		return false;
	}
	ao = ajax_object();
	ao.onreadystatechange = function(){
		if(ao.readyState == 4){
			if (ao.responseText == 'success'){
				$("#thatsswishswosh"+slogan_id).addClass('thatsswishswoshActive');
				$("#thatsswishswosh"+slogan_id).removeClass('thatsswishswosh');
				$("#thatstosh"+slogan_id).addClass('thatstosh');	
				$("#thatstosh"+slogan_id).removeClass('thatstoshActive');	
			}
		}
	}
	ao.open("GET", "/ajax/slogan_vote_swishswosh.php?slogan_id="+slogan_id+"&user_id="+user_id, true);
	ao.send(null);
}

function ajaxDeleteBlog(blog_id){
	function impDeleteBlog(v,m,f){
		if(v=='true'){
			$("#box").fadeOut(300);
			ao = ajax_object();
			ao.onreadystatechange = function(){
				if(ao.readyState == 4){
					$('#blog'+blog_id).fadeOut(300);
				}
			}
			ao.open("GET", "/ajax/delete_blog.php?blog_id="+blog_id, true);
			ao.send(null);
		}
	};
	$.prompt("Are you sure you want to delete this blog?",{
		callback: impDeleteBlog,
		buttons: { OK: 'true', Cancel: 'false'}
	});
}

function ajaxDeleteProfileComment(comment_id){
	function impDeleteComment(v,m,f){
		if(v=='true'){
			$("#box").fadeOut(300);
			ao = ajax_object();
			ao.onreadystatechange = function(){
				if(ao.readyState == 4){
					$('#profile_comment'+comment_id).fadeOut(300);
				}
			}
			ao.open("GET", "/ajax/delete_profile_comment.php?comment_id="+comment_id, true);
			ao.send(null);
		}
	};
	$.prompt("Are you sure you want to delete this comment?",{
		callback: impDeleteComment,
		buttons: { OK: 'true', Cancel: 'false'}
	});
}
function ajaxDeleteDesignComment(comment_id){
	function impDeleteComment(v,m,f){
		if(v=='true'){
			ao = ajax_object();
			ao.onreadystatechange = function(){
				if(ao.readyState == 4){
					$('#design_comment_'+comment_id).fadeOut(300);
				}
			}
			ao.open("GET", "/ajax/delete_design_comment.php?comment_id="+comment_id, true);
			ao.send(null);
		}
	};
	$.prompt("Are you sure you want to delete this comment?",{
		callback: impDeleteComment,
		buttons: { OK: 'true', Cancel: 'false'}
	});
}

function ajaxDeleteBlogComment(comment_id){
	function impDeleteComment(v,m,f){
		if(v=='true'){
			ao = ajax_object();
			ao.onreadystatechange = function(){
				if(ao.readyState == 4){
					$('#blogcomment'+comment_id).fadeOut(300);
				}
			}
			ao.open("GET", "/ajax/delete_blog_comment.php?comment_id="+comment_id, true);
			ao.send(null);
		}
	};
	$.prompt("Are you sure you want to delete this comment?",{
		callback: impDeleteComment,
		buttons: { OK: 'true', Cancel: 'false'}
	});
}

function ajaxNewsletter(form){
	with (form){
		ao = ajax_object();
		ao.onreadystatechange = function(){
			if (ao.readyState == 4){
				$("#newsletterMessage").html(ao.responseText);
			}
		}
		ao.open("GET", "/ajax/newsletter_subscribe.php?email="+email.value, true);
		ao.send(null);
	}
}

function ajaxShareDesign(form){
	form.disabled = true;
	p = $("#sharemessage");
	p.hide("slow",function(){;
	with(form){
		ao = ajax_object();
		ao.onreadystatechange = function(){
			if (ao.readyState == 4){
				p.html(ao.responseText);
				p.show("slow");
			}
		}
		ao.open("GET","/ajax/share_with_friend.php?email="+email.value+"&design_id="+design_id.value,true);
		ao.send(null);
	}});
}
/*
 * Adds a key/value pair to the stated session array
 * 
 */
function ajaxAddSessionValue(arr,key,value){
	ao = ajax_object();
	ao.onreadystatechange = function(){
		if (ao.readyState == 4){
			if (ao.responseText == 'success'){
				return ("success");
			} else {
				return ("failure");
			}
		}
	};
	ao.open("GET","/ajax/session_add_value.php?arr="+arr+"&key="+key+"&value="+value, true);
	ao.send(null);
}

/*
 * Clears a session array
 */

function ajaxClearSession(arr){
	ao = ajax_object();
	ao.onreadystatechange = function(){
		if (ao.readyState == 4){
			return true;
		}
		else {
			return false;
		}
	};
	ao.open("GET","/ajax/session_clear.php?arr="+arr, true);
	ao.send(null);
}
/*
 * Gets the specified session array as an object (php returns JSON)
 */
function ajaxGetSession(arr){
	ao = ajax_object();
	ao.onreadystatechange = function(){
		if (ao.readyState == 4){
			return JSON.parse(ao.responseText);
		}
		else {
			return false;
		}
	};
	ao.open("GET","/ajax/session_get.php?arr="+arr, true);
	ao.send(null);
}