var bbspaystarted = false;
var g_bbspath = "hardcoded/bbs/";
function bbsPayStart( transactionId )
{
	if( bbspaystarted ) return false;
	bbspaystarted = true;
	
	// Do the setup stuff
	ajax_fetchCallback( g_bbspath + "BBSSale.php?random="+Math.random()+"&TransactionId=" + transactionId, bbsPayContinue );
	
	// Show the pay page
	bbsPayFrameShow();
}
function bbsAuthStart( transactionId )
{
	if( bbspaystarted ) return false;
	bbspaystarted = true;
	
	// Do the setup stuff
	ajax_fetchCallback( g_bbspath + "BBSAuth.php?random="+Math.random()+"&TransactionId=" + transactionId, bbsPayContinue );
	
	// Show the pay page
	bbsPayFrameShow();
}
function bbsPayContinue( data )
{
	// Data returned is the input field of the form which should be submitted...
	// var iframe = getIFrame("bbsFrame");
	var iframe = document.getElementById("bbsContent");
	var iform = document.getElementById("bbsForm");
	if( iform == null )
		alert( "iForm is null" );
		
	// Change the input to the values from data
	/*
	var text = data.split(";");
	formInput.name = text[0];
	formInput.value= text[1];
	*/
	// data returned is the input field
	document.getElementById("bbsFormInnerDiv").innerHTML = data;

	var iformStatus = document.getElementById("bbsFormStatus");
	
	if( iformStatus == null )
	{
		// Some big error just happened...
		//idiv = iframe.getElementById("bbsFormDiv");
		//idiv.innerHTML = data;
		//idiv.style.display = "block";
		// alert( "The transaction you requested cannot be completed\n at this time.\nWe apologize for any inconvenience this may cause.\n\n("+data+")" );
		iframe.innerHTML = "<h2>Connection down</h2>The transaction you requested cannot be completed at this time.<br><img src='bbs_down.jpg'><br>We apologize for any inconvenience this may cause.<br><br>Server code 1 ("+data+")<br><input type='button' value='Close' onclick='bbsPayFrameHide()'>";
	}
	else if( iformStatus.value == "ok" )
	{							
		// Inside the iframe there is a form which should be sent
		// var iform = iframe.getElementById("bbsForm");
		iform.submit();
	}
	else
	{
		// A "controlled" error just happened...
		//idiv = document.getElementById("bbsFormDiv");
		//idiv.innerHTML = data;
		//idiv.style.display = "block";
		
		// alert( "This happened:\n" + iformStatus.value );
		iframe.innerHTML = "<h2>Connection down</h2>The transaction you requested cannot be completed at this time.<br><img src='bbs_down.jpg'><br>We apologize for any inconvenience this may cause.<br><br>Server code 2 ("+iformStatus.value+")<br><input type='button' value='Close' onclick='bbsPayFrameHide()'>";
	}
	
	
}

/***********
	When payment returns, this function is called		DEPRICATED, REPLIES NOW REDIRECT USER TO DIFFERENT PAGE
***********/
function bbsPayHasReturned( data )
{
	// Transactional data is processed by php,
	// this script only gets to know if the
	// transaction:
	// 		was payed (all ok)
	//		failed (not allowed)
	//		cancelled (by user)
	
	if( data == "ok" )
	{
		if( confirm( "Transaction OK. Close BBS window?" ) )
			bbsPayFrameHide();
	}
	else
	{
		alert( data );
	}

	// Enable the button (the button should not be enabled if the pay was successfull....)
	bbspaystarted = false;
}

// Animation of the pay frame functions
function bbsPayFrameShow()
{
	document.getElementById("bbsContent").style.display = "block";
	bbsPayFrameShowIteration(0,1);
}
function bbsPayFrameHide()
{
	bbsPayFrameShowIteration(49,-1);
	
	// Reset the form page
	//var iframe = getIFrame("bbsFrame");
	//window.frames['bbsFrame'].location.href = "bbsPayForm.html";
	
	bbspaystarted = false;
}
function bbsPayFrameShowIteration( completion, add)
{
	var divtag = document.getElementById("bbsContent");
	
	if( completion < 0 )
	{
		divtag.style.display = "none";
		return true;
	}
	
	if( completion > 25 )
	{
		divtag.style.top  = 50- (completion-25) * 1.75/2 + "%";
		divtag.style.height=((completion-25) * 4.5)/2 + "%";
	}
	else
	{
		divtag.style.left = 50 - completion * 1.75/2 + "%";
		divtag.style.width= (completion * 4.5)/4 + "%";
	}
	
	if( completion < 50 )
		document.timer = setTimeout( "bbsPayFrameShowIteration("+(completion+add)+","+add+")",10 );
	
}

// IFrame helper object
function getIFrame(id) {
	var oIframe = document.getElementById(id);
	var oDoc = (oIframe.contentWindow || oIframe.contentDocument);
	if (oDoc.document) oDoc = oDoc.document; 
	return oDoc;
} 











//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////AJAX STUFF

//Preforms an ajax fetch
/*Parameters: url to call, function to call when the data is ready (prototype: myfunction(data))*/
function ajax_fetchCallback(url, callback)
{
	var xmlHttp;
	try
  	{
  		// Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
  	}
	catch (e)
  	{
  		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
 	}
  	xmlHttp.onreadystatechange=function()
    {
    	if(xmlHttp.readyState==4)
      	{
           
            callback(xmlHttp.responseText);
      	}
    }
  	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	return true;
}//*/

//Single ajax request. 
/*If the url returns something, it's displayed in an alert box*/
function ajax_request(url)
{
	var xmlHttp;
	try
  	{
  		// Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
  	}
	catch (e)
  	{
  		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
 	}
  	xmlHttp.onreadystatechange=function()
    {
    	if(xmlHttp.readyState==4)
      	{
			if (xmlHttp.responseText != "")
				alert("A page said:\n" + xmlHttp.responseText);
            //callback(xmlHttp.responseText);
      	}
    }
  	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	return true;
}//*/

// returns a string of name=value for all child elements being of types: input textarea select

function dom_walkform(elem)
{
	var separator = "";
	var text = "";
	for( var i=0; i<elem.childNodes.length; i++ )
	{
		var node = elem.childNodes[i];
		var name = node.nodeName.toLowerCase();
		if( (name=="input" && node.type!="checkbox") || name=="select" || name=="textarea" )
		{
			text += separator + node.name + "=" + node.value;
		}
		else if( name=="input" && node.type=="checkbox" && node.checked )
		{
			text += separator + node.name + "=1";
		}
		else
			text += separator + dom_walkform(node);
		
		separator = "&";
	}
	return text;
}

function ajax_postform(url,formid,callback)
{
	showLoadingBar();
	var form = document.getElementById(formid);
	if( form == null )
	{
		alert( "The form you are trying to post does not exist" );
		return false;
	}
	
	var poststring = dom_walkform(form);
	
	// Create http object
	var xmlHttp;
	try
  	{
  		// Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
  	}
	catch (e)
  	{
  		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
 	}
	
	// post it
	xmlHttp.open("POST", url, true);

	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", poststring.length);
	xmlHttp.setRequestHeader("Connection", "close");

	xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
		if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			callback( xmlHttp.responseText );
			hideLoadingBar();
		}
		else if( xmlHttp.readyState == 4 )
		{
			if( xmlHttp.status == 404 )
				alert( "AJAX failed to POST data: 404 - Page Not Found ("+url+")" );
			else
				alert( "AJAX failed to POST data: Returned status (" + xmlHttp.status + ")" );
			hideLoadingBar();
		}
		
	}
	xmlHttp.send(poststring);
}

function showLoadingBar() { }
function hideLoadingBar() { }

