/*
 * AjaxObject is a hypothetical object that encapsulates the transaction
 *     request and callback logic.
 *
 * handleSuccess( ) provides success case logic
 * handleFailure( ) provides failure case logic
 * processResult( ) displays the results of the response from both the
 * success and failure handlers
 * call( ) calling this member starts the transaction request.
 */

 var progressState = 0;
 var foundRows = new Array();
 var sortByPrice = new Array();
 var currentPage = 0;
 var rowsPerPage = 20;
 var isSearching = 0;
 var strSid = '';
 var nDwn = 0;

function insertNewRows(newRows)
{
	foundRows = new Array();
 	for(var i=0; i<newRows.length; i++)
	{ 
 	foundRows.push(newRows[i]); // save the row
 	}
}

function showPage(page)
{
currentPage = page;
showRows();
}

function sortByPrice(a, b)
{
var partsA = new Array(), partsB = new Array(), valA, valB;
partsA = a.split("|");
partsB = b.split("|");

valA = parseInt(partsA[1]);
valB = parseInt(partsB[1]);
if(isNaN(valA)) valA = 0;
if(isNaN(valB)) valB = 0;

return (valA-valB);
}

function sortTrash(a, b)
{
	return a-b;
}

function showPriceList()
{
	var prices = new Array();
	for(i=0;i<foundRows.length;i++)
	{
		var parts = new Array();
		parts = foundRows[i].split("|");
		prices[i] = parseInt(parts[1]);
	}
	alert(prices);
}

function showRows()
{	
var tableHtml = "";
var resultRowList = new Array();
var minElement = currentPage*rowsPerPage;
var maxElement = Math.min(foundRows.length, minElement+rowsPerPage);

//showPriceList();
//foundRows = foundRows.sort(sortByPrice); //sortByPrice
//showPriceList();

	for(i=minElement;i<maxElement;i++)
	{
	//alert("Adding: "+foundRows[currentPage*rowsPerPage + i]);
	tableHtml = tableHtml.concat(foundRows[i].split("|").shift()); // DCE5EA FFFFFF  
	}
	//alert("Result: "+tableHtml);
document.getElementById("rows").innerHTML = tableHtml;

	resultRowList = document.getElementsByName('resultRow');
	//alert("rows: "+resultRowList.length);
	for(i=0;i<resultRowList.length;i++)
		if(i % 2 == 0) resultRowList[i].style.background = '#DCE5EA';
		else resultRowList[i].style.background = '#BED9E8';

document.getElementById("pager").innerHTML = "";
	for(i=0;i<Math.ceil(foundRows.length / rowsPerPage);i++)
		if(i == currentPage) document.getElementById("pager").innerHTML += '<a class="asel" href="javascript:showPage('+i+')">'+(i+1)+'</a>';
		else document.getElementById("pager").innerHTML += '<a class="anorm" href="javascript:showPage('+i+')">'+(i+1)+'</a>';
	
}
 

function ScheduleDownload()
{
	if(isSearching)
		setTimeout("AjaxObject.startRequest()",1000);
}


var AjaxObject = {

	handleSuccess:function(o){
		// This member handles the success response
		// and passes the response object o to AjaxObject's
		// processResult member.
		this.processResult(o);
		ScheduleDownload();
	},

	handleFailure:function(o){
		// Failure handler
		//alert("Dwn failed: "+o.responseText);
		ScheduleDownload();
	},

	processResult:function(o){
		// This member is called by handleSuccess
//		alert('downloaded ');
		if(o.responseText != '') {
			var newRows = new Array();
			//alert(o.responseText);
			newRows = o.responseText.split("/////");
			insertNewRows(newRows);
			showRows();
			//alert("Dwn non-empty result");
			
			if(o.responseText.substr(o.responseText.length-9, 9) == '===END===')
			{
				isSearching = 0;
				SetProgress(100);
				ToggleProgress(false);
				//alert("Found ===END===");
			}
			else ; //alert(o.responseText.substr(o.responseText.length-9, 9));
		}
		//else alert('Dwn returned empty result');
	},

	startRequest:function() {
		var args = 'strSid=' + encodeURIComponent(strSid);
		//alert(args);
	   YAHOO.util.Connect.asyncRequest('POST', 'getdata.php', callback, args);
	  // alert("started");
	  nDwn ++;
	  SetProgress(progressState++);
	  
	  document.getElementById("dwn").value = nDwn;
	}

};



/*
 * Define the callback object for success and failure
 * handlers as well as object scope.
 */
var callback =
{
	success:AjaxObject.handleSuccess,
	failure:AjaxObject.handleFailure,
	scope: AjaxObject,
	cache: false,
	timeout: 5000
};


var SearchObject = {

	handleSuccess:function(o){
		
		this.processResult(o);
	},

	handleFailure:function(o){ 
		//isSearching = 0;
		//alert("searching failed "+ o.responseText);
	},

	processResult:function(o){
		//alert("searching finished "+ o.responseText);
	},

	startRequest:function() {
		var today = new Date();
  		strSid = today.getTime() + Math.round(Math.random()*1000) + Math.round(Math.random()*1000); 
		progressState = 0;
		SetProgress(progressState);
		ToggleProgress(true);
		
		
	   var data = 'strFrom=' + encodeURIComponent(document.getElementById("strFrom").value) + '&' + 
	   'strTo=' + encodeURIComponent(document.getElementById("strTo").value) + '&' +
	   'nAdults=' + encodeURIComponent(document.getElementById("nAdults").value) + '&' +
	   'strChildList=' + encodeURIComponent(document.getElementById("strChildList").value) + '&' +
	   'bReturn=' + encodeURIComponent(document.getElementById("bReturn").value) + '&' +
	   'selMY1=' + encodeURIComponent(document.getElementById("selMY1").value) + '&' +
	   'selMY2=' + encodeURIComponent(document.getElementById("selMY2").value) + '&' +
	   'selDay1=' + encodeURIComponent(document.getElementById("selDay1").value) + '&' +
	   'selDay2=' + encodeURIComponent(document.getElementById("selDay2").value) + '&' +
	   'strSid=' + encodeURIComponent(strSid);

//	   YAHOO.util.Connect.asyncRequest('POST', 'run.php', callback2, data);
	   YAHOO.util.Connect.asyncRequest('GET', '/run.php?'+data, callback2);
	   
	   document.getElementById("info").value = strSid + "   " + 'run.php?'+data;
	   
	   isSearching = 1;
	}

};

var callback2 =
{
	success:SearchObject.handleSuccess,
	failure:SearchObject.handleFailure,
	scope: SearchObject,
	cache: false,
	timeout: 5000
};


function toggleKids(val)
{
	if(val != 0) document.getElementById('kidsAge').style.visibility='visible';
	else document.getElementById('kidsAge').style.visibility='hidden';
}

