function popup(url) {
	newwindow=window.open(url,'name','height=200,width=240');
	if (window.focus) {
		newwindow.focus()
	}
	return false;
}

function popupXY(url, x, y) {
	newwindow=window.open(url,'name','height='+x+',width='+y);
	if (window.focus) {newwindow.focus()}
	return false;
}

function getXmlHttp() {
	
	try {
		// Opera 8.0+, Firefox, Safari
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer Browsers
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				// Something went wrong
				alert("AJAX not supported");
				return false;
			}
		}
	}		
	
	return xmlhttp;
}

function getPage(url)
{
	xmlhttp = getXmlHttp();
	
	xmlhttp.onreadystatechange=function()
		{	
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				$(".pagination").html(xmlhttp.responseText);
			}
		}
		
	// handle different types
	xmlhttp.open("GET",url+"?action=page",true);
	xmlhttp.send();
}	

function submitLogin(form) {
	url = 'http://' + window.location.hostname + '/getLogin.php';
	email = form.email.value;
	//alert(email);
	//alert(url);
	$.ajax({
		async: false,
		type: "post",
		url: url,
		data: {'email': email},
		dataType: 'json',
		success: function(data) {
			data = data.data;
			//alert(data);
			data = data.replace(new RegExp( "\\n", "g" ),"");  //strip \n
			data = data.replace(new RegExp( "\\r", "g" ),"");   //strip \r
			data = data.replace(new RegExp( "\\s\\s+", "g" ),"");  //strip double space
			data = data.replace(new RegExp( "^\\s", "g" ),"");  //strip single space at beginning
			data = data.replace(new RegExp( "\\s$", "g" ),"");  //strip single space at end	
			if (data.indexOf('Error') == -1) {				
				data = data.split("~");
				salt = data[0];
				challengeStr = data[1];
				pw = form.password.value;
				pw =  hex_sha1(salt+pw);
				hashed_pw = hex_sha1(challengeStr + pw);
				
				// adjust the values
				form.password.value = '';
				form.en_password.value = hashed_pw;
				
			}
		}
	});
}

function updateRecent2()
{
	try {
	xmlhttp = getXmlHttp();
	}
	catch (e) {	
		alert("An exception occurred in the script. Error name: " + e.name + ". Error message: " + e.message);
	}

	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			//if the feed is empty, populate it with entries and assign the latest time stamp as latestEntry
			if ($('.live-feed').is(':empty')) {   
				$('.live-feed').html(xmlhttp.responseText);
				$('.live-feed-renderer').html(xmlhttp.responseText);
			}
			else {
				var latestEntry = parseInt($('.live-feed-renderer div:first').attr('class'));
				$(".live-feed-renderer").html(xmlhttp.responseText);
				var newlyUpdatedEntry = parseInt($('.live-feed-renderer div:first').attr('class'));
				var currentEntry = parseInt($('.live-feed div:first').attr('class'));
				
				if (newlyUpdatedEntry > latestEntry && newlyUpdatedEntry != currentEntry) {
					$('.' + newlyUpdatedEntry).css('display', 'none');
					$('.live-feed div:last-child').remove();
					$('.live-feed').prepend($('.live-feed-renderer div:first'));
					slideNewFeedEntry(newlyUpdatedEntry);
				}
			}
		}
	}
	xmlhttp.open("GET","getUpdates2.php",true);
	xmlhttp.send();

}

function checkName(){

	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp2=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	if(xmlhttp2) {
		// handle different types
		xmlhttp2.open("GET","verifyUsername.php?name="+$('#username').val(),false);
		xmlhttp2.send();		
		$("#username-check").html(xmlhttp2.responseText);
	}
	else {
		return;
	}
}


function updateRecentOffer()
{
	xmlhttp = getXmlHttp();

	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			//if the feed is empty, populate it with entries and assign the latest time stamp as latestEntry
			if ($('.offer-feed').is(':empty')) {   
				$('.offer-feed').html(xmlhttp.responseText);
				$('.offer-feed-renderer').html(xmlhttp.responseText);
				ajaxRefreshed();
			}
			else {
				var latestEntry = parseInt($('.offer-feed-renderer div:first').attr('class'));
				$(".offer-feed-renderer").html(xmlhttp.responseText);
				var newlyUpdatedEntry = parseInt($('.offer-feed-renderer div:first').attr('class'));
				var currentEntry = parseInt($('.offer-feed div:first').attr('class'));
				
				if (newlyUpdatedEntry > latestEntry && newlyUpdatedEntry != currentEntry) {
					$('.' + newlyUpdatedEntry).css('display', 'none');
					$('.offer-feed div:last').remove();
					$('.offer-feed div:last').remove();
					$('.offer-feed div + div + div + div + div + div + div').remove();
					$('.offer-feed').prepend($('.offer-feed-renderer div:first'));
					slideNewFeedEntry(newlyUpdatedEntry);
				}
			}
		}
	}
	xmlhttp.open("GET","getRecentCredit.php",true);
	xmlhttp.send();
}		

function verifyComplete(offer_id, page) {

	$.ajax({
		async: false,
		type: "POST",
		url: 'verifyComplete.php',
		data: {'offer_id': offer_id},
		success: function(data) {
			if(data != 'no'){
				alert('Thank you for completing the offer.');
				offer_id = data;
				$.ajax({
					async: false,
					type: "GET",
					url: 'getOffers.php',
					data: {'complete': offer_id, 'page': page},
					success: function(data) {
						result = data;
					}
				});	
			}
			else {
				alert('Please attempt the offer before clicking complete.');
			}
		}
	});
	//alert(result);
	$(".offers-table").html(result);
	getPage("getOffers.php");
}

function updateOffer(type, str)
{
	xmlhttp = getXmlHttp();
	
	xmlhttp.onreadystatechange=function()
		{	
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				if(xmlhttp.responseText == '')
					location.reload();
								
				$(".offers-table").html(xmlhttp.responseText);
				getPage("getOffers.php");
				ajaxRefreshed();
			}
		}
		
	// reset the variables
	if(type == 'search') {
		$(".offers-checkbox-free").attr('checked', true);
		//$(".offers-checkbox-cc-free").attr('checked', true);
		//$(".offers-checkbox-cc-pay").attr('checked', true);
		//$(".offers-checkbox-cellphone").attr('checked', true);
	}
		
	// handle different types
	xmlhttp.open("GET","getOffers.php?"+type+"="+str,true);
	xmlhttp.send();
}			

function updateOffer2(type, str)
{
	xmlhttp = getXmlHttp();
	
	xmlhttp.onreadystatechange=function()
		{	
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				if(xmlhttp.responseText == '')
					location.reload();
								
				$(".offers-table").html(xmlhttp.responseText);
				getPage("getOffers2.php");
				ajaxRefreshed();
			}
		}
		
	// reset the variables
	if(type == 'search') {
		$(".offers-checkbox-free").attr('checked', true);
		//$(".offers-checkbox-cc-free").attr('checked', true);
		//$(".offers-checkbox-cc-pay").attr('checked', true);
		//$(".offers-checkbox-cellphone").attr('checked', true);
	}
		
	// handle different types
	xmlhttp.open("GET","getOffers2.php?"+type+"="+str,true);
	xmlhttp.send();
}	

function updateRef(type, str)
{
	xmlhttp = getXmlHttp();
	
	xmlhttp.onreadystatechange=function()
		{	
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				if(xmlhttp.responseText == '')
					location.reload();
				
				$(".referral-history-table").html(xmlhttp.responseText);
				getPage("getRefHistory.php");
				ajaxRefreshed();
			}
		}
		
	// handle different types
	xmlhttp.open("GET","getRefHistory.php?"+type+"="+str,true);
	xmlhttp.send();
}	
	
function updateOff(type, str)
{
	xmlhttp = getXmlHttp();
	
	xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				if(xmlhttp.responseText == '')
					location.reload();
							
				$(".offer-history-table").html(xmlhttp.responseText);
				getPage("getOfferHistory.php");
				ajaxRefreshed();
			}
		}
		
	// handle different types
	xmlhttp.open("GET","getOfferHistory.php?"+type+"="+str,true);
	xmlhttp.send();
}		

function getHidden(type, str)
{
	xmlhttp = getXmlHttp();
	
	xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				if(xmlhttp.responseText == '')
					location.reload();
							
				$(".offers-table").html(xmlhttp.responseText);
				getPage("getHiddenOffers.php");
				ajaxRefreshed();
			}
		}
		
	// handle different types
	xmlhttp.open("GET","getHiddenOffers.php?"+type+"="+str,true);
	xmlhttp.send();
}		

function rSummary(type, str)
{
	xmlhttp = getXmlHttp();
	
	xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				if(xmlhttp.responseText == '')
					location.reload();
							
				$(".reward-summary-table").html(xmlhttp.responseText);
				getPage("getRewardSummary.php");
				ajaxRefreshed();
			}
		}
		
	// handle different types
	xmlhttp.open("GET","getRewardSummary.php?"+type+"="+str,true);
	xmlhttp.send();
}		

function pSummary(type, str)
{
	xmlhttp = getXmlHttp();
	
	xmlhttp.onreadystatechange=function()
		{	
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				if(xmlhttp.responseText == '')
					location.reload();
							
				$(".payout-table").html(xmlhttp.responseText);
				getPage("getPayoutSummary.php");
				ajaxRefreshed();
			}
		}
		
	// handle different types
	xmlhttp.open("GET","getPayoutSummary.php?"+type+"="+str,true);
	xmlhttp.send();
	
}		

function refTracking(type, str) 
{
	xmlhttp = getXmlHttp();
	
	xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				if(xmlhttp.responseText == '')
					location.reload();
										
				$(".referral-stats-table").html(xmlhttp.responseText);
				getPage("getRefTrack.php");
				ajaxRefreshed();
			}
		}
		
	// handle different types
	xmlhttp.open("GET","getRefTrack.php?"+type+"="+str,true);
	xmlhttp.send();
}


