/***********************************************************************************************************************
hs_Transport.js
ver 1.00
5.10.07


***********************************************************************************************************************/

var server_today_str;

function logofff()
{
	if (confirm("are you sure you wish to log off?"))
	{
	    var tr = new hs_Transport();
		tr.Logoff();
	}
}

var str = new String(window.location);


if (str.indexOf("www") == -1)
{
	if ((str.indexOf("localhost") == -1)&&(str.indexOf("testing.") == -1))
	{
		index = str.indexOf("//");
		str = str.substr(0, index + 2) + "www." + str.substr(index+2);
		window.location.href = str;
	}
}



function GetServerRoot()
{
	var str = new String(window.location);
	var index = str.lastIndexOf("/");
	str = str.substr(0, index);
	index = str.lastIndexOf("/");
	str = str.substr(0, index+1);

	return str;
}


var CSS_DISPLAY_MAX = 10; /*no of items displayable in a css display without scrolling*/
/*
to tell a line in a css display to hide itself 'cos there is no data for it to show (this is used as display_index - NO_CSS_DISPLAY_DATA
so the actual value handled will always be -ve*/
var NO_CSS_DISPLAY_DATA = 1000; 


function send_request(obj)
{
	var newMessage = new MessageHandler(obj);
	pendingTX.push(newMessage);		
	newMessage.TX();
}



hs_Transport.prototype.constructor = new hs_Transport();
hs_Transport.prototype.TX = hs_Transport_TX;
hs_Transport.prototype.Logon = hs_Transport_Logon;
hs_Transport.prototype.Logoff = hs_Transport_Logoff;
hs_Transport.prototype.RequestFromArray = hs_Transport_RequestFromArray;
hs_Transport.prototype.SendRequest = hs_Transport_SendRequest;
hs_Transport.prototype.DebugRequest = hs_Transport_DebugRequest;


hs_Transport.prototype.get_request = hs_Transport_GetRequest;
hs_Transport.prototype.ParseResponse = hs_Transport_ParseResponse;


function hs_Transport()
{
    this.object_name = '';

	this.phpStr = '';
	this.phpObjArray = new Array();
	this.phpObj = null;

	this.displayItemWrapper = new Array();

	this.debug_me = false;
	this.scroll_selected_colour = '#00ff00';
	this.scroll_not_selected_colour = '#ffffff';
}

function hs_Transport_TX()
{
//alert(this.request);
	send_request(this);
}

function hs_Transport_Logon(name, pwd)
{
	this.request = 'action_request=LOGON&user_name='+name+'&pwd='+pwd;
//alert(this.request);
	send_request(this);
}

function hs_Transport_Logoff()
{
	this.request = 'COMMAND=LOGOFF';
//alert(this.request);
	send_request(this);
}

function hs_Transport_SendRequest(request)
{
	this.request = 'action_request='+request;
	send_request(this);	
}


function hs_Transport_RequestFromArray(ar)
{
	var i = 0;
	this.request = "";

	for (i=0; i < ar.length; i+=2)
	{
		if (i > 0 )
			this.request += '&';
		this.request += ar[i] + "=" + ar[i+1];
	}
}


/*..............................................................................................................*/




function hs_Transport_ParseResponse(db_str)
{
	this.phpObjArray.length = 0;
	this.phpStr = db_str;

	var phpStrArray = new Array();
	phpStrArray = this.phpStr.split("}");

	if (phpStrArray.length < 1)   /*php error*/
	{	
		alert('parse error ' + this.phpStr);
		return;		
	}


	for (var i=0; i < phpStrArray.length-1; i++)
	{
		phpStrArray[i] += '}';
		this.phpObjArray[i] = phpStrArray[i].parseJSON();

	}
	if (this.phpObjArray[0].action == "error")
		alert("ERROR RECEIVED FROM SERVER \n TABLE " + this.phpObjArray[0].table + " \n  " + this.phpObjArray[0].error_data);
	if (this.phpObjArray[0].action == undefined)
		alert("no action defined error \n" + db_str);
	if (this.phpObjArray[0].action == 'report_duplicate')
	    alert("error - that item already exists in the database");
	if (this.phpObjArray[0].action == 'closed_for_maintenance')
		window.location.href = GetServerRoot() + "/maintain.html";
	if (this.phpObjArray[0].action == 'show_mail')
		alert("email sent " + this.phpObjArray[0].data);
	if ((this.phpObjArray[0].action == 'redirect')||(this.phpObjArray[0].action == 'redirect_to_logon'))
		window.location = this.phpObjArray[0].location;
	if (this.phpObjArray[0].action == 'debug')
		alert(this.phpObjArray[0].data);
	if (this.phpObjArray[0].action == 'session_ended')
	{
		alert('you have succesfully logged off');
		window.location = "../index.html";
	}
	

		
}

function hs_Transport_GetRequest()
{
	return this.request;
}

/*...............................................................................................................*/

/* ...........................................................................................................................*/


function hs_Transport_DebugRequest()
{
	rq_ar = this.request.split('&');
	var dbg_str = "";
	for (var i=0; i < rq_ar.length; i++)	
		dbg_str +=  rq_ar[i] + '\r\n';
	alert(dbg_str);
}



/*used to replace the | char with new line - only applicable if data was entered from a text area rather than "input = text"
to use this you must 
1)  in the html...
<div class = 'enquiry_customer_DB_comments_item' id = 'enquiry_customer_DB_comments_0'><PRE>.</PRE></div>
2) in Show_CSSDisplay(phpObj, index) (db_specific.js)
document.getElementById('enquiry_customer_DB_comments_'+index).firstChild.firstChild.data = this.LB(phpObj.comments);
*/




