// JavaScript Document
var http;
var __interval;

function Ajax_call()
{
	var http=null;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  http=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
		{
		http=new ActiveXObject("Msxml2.XMLHTTP");
		}
	  catch (e)
		{
		http=new ActiveXObject("Microsoft.XMLHTTP");
		}
	  }
	return http;
}


var sid;
var fid;
var rand;
function initCheck(s, f, r)
{
  // alert("You must use your VALID information while filling this offer out. If your information is fake our system is automatically alerted to permanently ban you and not unlock the download.");
   sid = s;
   fid = f;
   rand = r;
   checkDownload();
   
}


function checkDownload()
{

				window.clearInterval(__interval);	
				
				__interval="";		
				http = new Ajax_call();
				/*if(sid == null || fid == null || sid == 'undefined' || fid == 'undefined')
                {
				    return false;	
				}*/
				
				if (http) {
				
				var url = "validate_regular.php";
				var params = "sid="+sid+'&f='+fid+"&seid="+Math.random()+"&r="+rand;
				http.onreadystatechange =  function(){ updateDownload(); };
				http.open('POST', url, true);
				http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				http.send(params);
                
				}else
				{
				alert("http undefined");
				}	    	
}




function updateDownload(fid)
{
	
  var output = document.getElementById('status');
 

  if(http.readyState == 2 || http.readyState == 3)
  {
			output.innerHTML = "<div style='color:#ff0000; font-size:13px; text-align:center; margin-top:10px; margin-bottom:10px; width:96%;'><img src='templates/default/images/loading.gif' alt='' style='vertical-align:middle'  /> - checking for completition</div>";	  
  }
 
  if(http.readyState==4)
  {
	   
	    var response = JSON.parse(http.responseText);	
		
        if(response.error != 0)
		{
			output.innerHTML = "<div style='color:#ff0000; font-size:13px; text-align:center; margin-top:10px; margin-bottom:10px; width:96%;'><img src='templates/default/images/loading.gif' alt='' style='vertical-align:middle' /> - Offer not completed...</div>";
			__interval = window.setInterval(checkDownload, 1500);
			
			
		}
	
		if(response.token != 0 && response.error == 0)
	    {
			
			    output.innerHTML = "<div style='color:#0000ff; font-size:13px; text-align:center; margin-top:10px; margin-bottom:10px; width:96%;'>Your download is now available click below to begin</div>";
			    window.clearInterval(__interval);
				$(window).unbind('beforeunload');
				$('#surveysLayer').css('display', 'none');
				$('#dform').css('display', 'block');
				$('#tokenId').val(response.token);
				//window.location = http.responseText; 
				
		}
   
   
  }
	
}
	


