// JavaScript Document
var http = false;
var asd = ''; 

if(navigator.appName == "Microsoft Internet Explorer") {
  http = new ActiveXObject("Microsoft.XMLHTTP");
  
} else {
  http = new XMLHttpRequest();
}
function checkUsers(value)
{
	http.abort();
	document.getElementById('user_status').innerHTML = "<img src='"+ siteName+"images/loading.gif'>";
	http.open("GET", siteName+"functions/ajax.php?check=user_status&username="+value, true);
	http.onreadystatechange=function() 
	{
		if(http.readyState == 4) 
		{
			document.getElementById('user_status').innerHTML = http.responseText;
		}
	}
  http.send(null);
}


/*function to check if the offer name alreay exists */

function checkOffer(value)
{
	http.abort();
	document.getElementById('user_status').innerHTML = "<img src='"+ siteName+"images/loading.gif'>";
	http.open("GET", siteName+"functions/ajax.php?check=check_offer&offer_name="+value, true);
	http.onreadystatechange=function() 
	{
		if(http.readyState == 4) 
		{
			document.getElementById('user_status').innerHTML = http.responseText;
		}
	}
  http.send(null);
}
/*end*/


/**/

function checkEditOfferName(value,id)
{
	http.abort();
	document.getElementById('user_status').innerHTML = "<img src='"+ siteName+"images/loading.gif'>";
	
	http.open("GET", siteName+"functions/ajax.php?check=check_offer_edit&offer_name="+value+"&offer_id="+id, true);
	http.onreadystatechange=function() 
	{
		if(http.readyState == 4) 
		{
			document.getElementById('user_status').innerHTML = http.responseText;
		}
	}
  http.send(null);
}


/**/


function checkEmails(value)
{
	http.abort();
	document.getElementById('email_status').innerHTML = "<img src='"+ siteName+"images/loading.gif'>";
	http.open("GET", siteName+"functions/ajax.php?check=email_status&email="+value, true);
	http.onreadystatechange=function() 
	{
		if(http.readyState == 4) 
		{
			document.getElementById('email_status').innerHTML = http.responseText;
		}
	}
  http.send(null);

}

function checkCategory(value)
{
	http.abort();
	document.getElementById('user_status').innerHTML = "<img src='"+ siteName+"images/loading.gif'>";
	http.open("GET", siteName+"functions/ajax.php?check=check_category&category="+value, true);
	http.onreadystatechange=function() 
	{
		if(http.readyState == 4) 
		{
			document.getElementById('user_status').innerHTML = http.responseText;
		}
	}
  http.send(null);
}

function checkEditCategoryName(value,id)
{
	http.abort();
	document.getElementById('user_status').innerHTML = "<img src='"+ siteName+"images/loading.gif'>";
	http.open("GET", siteName+"functions/ajax.php?check=check_category_edit&cat_name="+value+"&cat_id="+id, true);
	http.onreadystatechange=function() 
	{
		if(http.readyState == 4) 
		{
			document.getElementById('user_status').innerHTML = http.responseText;
		}
	}
  http.send(null);
}

function checkNetwork(value)
{
	http.abort();
	document.getElementById('user_status').innerHTML = "<img src='"+ siteName+"images/loading.gif'>";
	http.open("GET", siteName+"functions/ajax.php?check=check_network&network="+value, true);
	http.onreadystatechange=function() 
	{
		if(http.readyState == 4) 
		{
			document.getElementById('user_status').innerHTML = http.responseText;
		}
	}
  http.send(null);
}

function checkEditNetworkName(value,id)
{
	http.abort();
	document.getElementById('user_status').innerHTML = "<img src='"+ siteName+"images/loading.gif'>";
	http.open("GET", siteName+"functions/ajax.php?check=check_network_edit&network="+value+"&net_id="+id, true);
	http.onreadystatechange=function() 
	{
		if(http.readyState == 4) 
		{
			document.getElementById('user_status').innerHTML = http.responseText;
		}
	}
  http.send(null);
}
//var counter = 0;
function checkIfPostBacked(rand){	
	http.abort();
	//document.getElementById('user_status').innerHTML = "<img src='"+ siteName+"images/loading.gif'>";
	
	http.open("GET", siteName+"functions/ajax.php?check=check_if_post_backed&rand="+rand, true);
	http.onreadystatechange=function() 
	{
		if(http.readyState == 4) 
		{
				
				var p = trim(http.responseText);
				
			if(p.length <30){	
				clearTimeout(asd);
				//document.getElementById('waiting_area').innerHTML = http.responseText;
				//document.getElementById('waiting_area').style.display="block"; 
				invokeDownload(rand);
				
			}
			else{
				
				//document.getElementById('showhide').innerHTML = http.responseText;
				//document.getElementById('showhide').style.display="block"; 					
				var asd = setTimeout("checkIfPostBacked('"+rand+"')",15000);
				
				
			}
		}
	}
  http.send(null);  
  
}



function invoke_waiting_area(rand){	
	document.getElementById('offer_listing_area').style.display="none"; 
	document.getElementById('showhide').style.display = 'block';
	checkIfPostBacked(rand);
}

function showHide(){
	clearTimeout(asd);
	document.getElementById('offer_listing_area').style.display = 'block';
	document.getElementById('showhide').style.display = 'none';
}

function invokeDownload(rand){
	
	http.abort();
	//document.getElementById('user_status').innerHTML = "<img src='"+ siteName+"images/loading.gif'>";
	http.open("GET", siteName+"functions/ajax.php?check=invoke_download&rand="+rand, true);
	http.onreadystatechange=function() 
	{		
		if(http.readyState == 4) 
		{			
			window.location.href = http.responseText+'&extra=extra';
			//alert(http)
			tb_remove(); // ca		
		
		}
	}
  http.send(null);
}

function dummy(){	
	}
	
	
function trim(inputString) {
   
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { 
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { 
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { 
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); 
   }
   return retValue; 
}
