/*
_|      _|  _|            _|                            _|  _|              _|      _|            
_|_|  _|_|      _|_|_|        _|_|_|  _|_|      _|_|_|  _|        _|_|_|  _|_|_|_|      _|    _|  
_|  _|  _|  _|  _|    _|  _|  _|    _|    _|  _|    _|  _|  _|  _|_|        _|      _|    _|_|    
_|      _|  _|  _|    _|  _|  _|    _|    _|  _|    _|  _|  _|      _|_|    _|      _|  _|    _|  
_|      _|  _|  _|    _|  _|  _|    _|    _|    _|_|_|  _|  _|  _|_|_|        _|_|  _|  _|    _|

  Website: Minimalistix.be
  Version: 1.0
  Programming: Hulpia Frederik - info at powerplant dot be - http://www.powerplant.be
  
  Filename: class.inputcheck.js
  Info: This class check's the user input
*/ 

/*========================================================================
  Event Handlers - Input checks
========================================================================*/
function flexInputCheck ()
{
	// Get objects
	this.navbit		= flexGlobal.getObject('navbit');
	this.firstName	= flexGlobal.getObject('inpFirstName');
	this.lastName	= flexGlobal.getObject('inpLastName');
	this.email		= flexGlobal.getObject('inpEmail');
	this.phone		= flexGlobal.getObject('inpPhone');
	this.mobile		= flexGlobal.getObject('inpMobile');
	this.msgSend	= flexGlobal.getObject('send');
}

/*------------------------------------------------------------------------
 Method(initAjaxCheck): Initialize ajax events
 -------------------------------------------------------------------------
 
 @ input	none		none
 
 @ return	none		none
-------------------------------------------------------------*/
flexInputCheck.prototype.initAjaxCheck = function()
{
	// Set events
	if(this.navbit)		{ this.navbit.onclick		= function (){ loadContent(0); }}
	if(this.firstName)	{ this.firstName.onkeyup	= flexInputChecks.doAjaxCheck; }
	if(this.lastName)	{ this.lastName.onkeyup		= flexInputChecks.doAjaxCheck; }
	if(this.email)		{ this.email.onkeyup		= flexInputChecks.doAjaxCheck; }
	if(this.phone)		{ this.phone.onkeyup		= flexInputChecks.doAjaxCheck; }
	if(this.mobile)		{ this.mobile.onkeyup		= flexInputChecks.doAjaxCheck; }
	if(this.msgSend)	{ this.msgSend.onclick		= flexInputChecks.checkBeforeSend; }
}

/*------------------------------------------------------------------------
 Method(doAjaxCheck): Do the ajax input check
 -------------------------------------------------------------------------
 
 @ input	event		current event
 
 @ return	none		none
-------------------------------------------------------------*/
flexInputCheck.prototype.doAjaxCheck = function(e)
{
	e = doEvent(e);

	// Check if empty
	if(this.value == '')
	{
		// Get the current id
		var name = this.id.substring(3);

		// Get image object of the current id
		var img	= flexGlobal.getObject('img' + name);

		// Set no image to the current object
		img.innerHTML = '';
		return true;
	}
	
	// Do ajax input check of the current object
	flexInputChecks.check(this);	
}

/*------------------------------------------------------------------------
 Method(check): Check input
 -------------------------------------------------------------------------
 
 @ input	object		current object
 
 @ return	none		none
-------------------------------------------------------------*/
flexInputCheck.prototype.check = function (object)
{
	// Set ajax handler
	flexInputCheck.checkit = new flexAjax();
	flexInputCheck.checkit.init();
			
	// Set onreadystatechange
	flexInputCheck.checkit.onreadystatechange(flexInputChecks.get_ajax_responseInputCheck);
			
	// Send AJAX request
	flexInputCheck.checkit.send("../includes/ajaxInputCheck.php", "id=" + object.id + "&value=" + object.value);
}

/*------------------------------------------------------------------------
 Method(response): get_ajax_responseInputCheck
 -------------------------------------------------------------------------
 
 @ input	none		none
 
 @ return	none		none
-------------------------------------------------------------*/
flexInputCheck.prototype.get_ajax_responseInputCheck = function ()
{
	// Get Ajax Handler
	var ajax = flexInputCheck.checkit.AjaxHandler;

	// Check if ready and get response
	if (ajax.readyState == 4 && ajax.status == 200)
	{
		if(ajax.responseText.substring(0, 5) == '##1#;')
		{
			// Change the image to error
			var img = flexGlobal.getObject('img' + ajax.responseText.substring(8));
			img.innerHTML = '<img src="img/inputNOk.gif" width="16" height="16" border="0" alt="Controleer uw ingave" title="Controleer uw ingave" />';
		}
		else if(ajax.responseText.substring(0, 5) == '##0#;')
		{
			// Change the image to ok
			var img = flexGlobal.getObject('img' + ajax.responseText.substring(8));
			img.innerHTML = '<img src="img/inputOk.gif" width="16" height="16" border="0" alt="" />';
		}
		else
		{
			return false;
		}
	}
}

/*------------------------------------------------------------------------
 Method(checkBeforeSend): Check all the values before sending  
 -------------------------------------------------------------------------
 
 @ input	none		none
 
 @ return	none		none
-------------------------------------------------------------*/
flexInputCheck.prototype.checkBeforeSend = function(e)
{
	// Get objects
	var sndFirstName	= flexGlobal.getObject('inpFirstName');
	var sndLastName		= flexGlobal.getObject('inpLastName');
	var sndEmail		= flexGlobal.getObject('inpEmail');
	var sndPhone		= flexGlobal.getObject('inpPhone');
	var sndMobile		= flexGlobal.getObject('inpMobile');
	var rteval			= flexGlobal.getObject('rte');
	
	// Set ajax handler
	flexInputCheck.checkit = new flexAjax();
	flexInputCheck.checkit.init();
			
	// Set onreadystatechange
	flexInputCheck.checkit.onreadystatechange(flexInputChecks.sendMessage);
			
	// Build uri
	var uri = 
		"id=All" +
		"&inpFirstName=" 		+ sndFirstName.value	+
		"&inpLastName=" 		+ sndLastName.value 	+
		"&inpEmail="			+ sndEmail.value 		+
		"&inpPhone="			+ sndPhone.value	 	+
		"&inpMobile="			+ sndMobile.value 		+
		"&inpRte="				+ rteval.value;
	
	// Send AJAX request
	flexInputCheck.checkit.send("../includes/ajaxInputCheck.php", uri);
}

/*------------------------------------------------------------------------
 Method(response): sendMessage
 -------------------------------------------------------------------------
 
 @ input	none		none
 
 @ return	none		none
-------------------------------------------------------------*/
flexInputCheck.prototype.sendMessage = function ()
{
	// Get Ajax Handler
	var ajax = flexInputCheck.checkit.AjaxHandler;

	// Check if ready and get response
	if (ajax.readyState == 4 && ajax.status == 200)
	{
		// Get all the results
		var result 		= ajax.responseXML.documentElement;
		var firstName	= result.getElementsByTagName('firstName')[0].firstChild.data;
		var lastName	= result.getElementsByTagName('lastName')[0].firstChild.data;
		var email		= result.getElementsByTagName('email')[0].firstChild.data;
		var phone		= result.getElementsByTagName('phone')[0].firstChild.data;
		var mobile		= result.getElementsByTagName('mobile')[0].firstChild.data;
		var RTE			= result.getElementsByTagName('rte')[0].firstChild.data;
		var RTEData		= result.getElementsByTagName('rtedata')[0].firstChild.data;

		// Get objects
		var imgFirstName	= flexGlobal.getObject('imgFirstName');
		var imgLastName		= flexGlobal.getObject('imgLastName');
		var imgEmail		= flexGlobal.getObject('imgEmail');
		var imgPhone		= flexGlobal.getObject('imgPhone');
		var imgMobile		= flexGlobal.getObject('imgMobile');
		
		// Check for errors
		if (firstName	== '0' &&
			lastName	== '0' &&
			email		== '0' &&
			phone		== '0' &&
			mobile		== '0' &&
			RTE			==	'0')
		{
			// All gone well, submit
			flexInputChecks.sendFinal();
		}
		else
		{
			// Set images
			if(firstName == '1')	{ imgFirstName.innerHTML = '<img src="img/inputNOk.gif" width="16" height="16" border="0" alt="Please check your input" title="Please check your input" />'; }
			if(lastName == '1')		{ imgLastName.innerHTML = '<img src="img/inputNOk.gif" width="16" height="16" border="0" alt="Please check your input" title="Please check your input" />'; }
			if(email == '1')		{ imgEmail.innerHTML = '<img src="img/inputNOk.gif" width="16" height="16" border="0" alt="Please check your input" title="Please check your input" />'; }
			if(phone == '1')		{ imgPhone.innerHTML = '<img src="img/inputNOk.gif" width="16" height="16" border="0" alt="Please check your input" title="Please check your input" />'; }
			if(mobile == '1')		{ imgMobile.innerHTML = '<img src="img/inputNOk.gif" width="16" height="16" border="0" alt="Please check your input" title="Please check your input" />'; }
		}		
	}
}

/*------------------------------------------------------------------------
 Method(response): sendFinal
 -------------------------------------------------------------------------
 
 @ input	none		none
 
 @ return	none		none
-------------------------------------------------------------*/
flexInputCheck.prototype.sendFinal = function ()
{
	// Get objects
	var sndFirstName	= flexGlobal.getObject('inpFirstName');
	var sndLastName		= flexGlobal.getObject('inpLastName');
	var sndEmail		= flexGlobal.getObject('inpEmail');
	var sndPhone		= flexGlobal.getObject('inpPhone');
	var sndMobile		= flexGlobal.getObject('inpMobile');
	var sndRte			= flexGlobal.getObject('rte');
	var selPriority		= flexGlobal.getObject('selPriority');
	
	// Set ajax handler
	flexInputCheck.sendit = new flexAjax();
	flexInputCheck.sendit.init();
			
	// Set onreadystatechange
	flexInputCheck.sendit.onreadystatechange(flexInputChecks.sendStatus);
			
	// Build uri
	var uri = 
		"inpFirstName=" 		+ sndFirstName.value	+
		"&inpLastName=" 		+ sndLastName.value 	+
		"&inpEmail="			+ sndEmail.value 		+
		"&inpPhone="			+ sndPhone.value	 	+
		"&inpMobile="			+ sndMobile.value 		+
		"&inpRte="				+ sndRte.value			+
		"&selPriority="			+ selPriority.value;
	
	// Send AJAX request
	flexInputCheck.sendit.send("../includes/ajaxSendMessage.php", uri);
	
	// Disable the send button
	this.msgSend.disabled = true;
}

/*------------------------------------------------------------------------
 Method(response): sendStatus
 -------------------------------------------------------------------------
 
 @ input	none		none
 
 @ return	none		none
-------------------------------------------------------------*/
flexInputCheck.prototype.sendStatus = function ()
{
	// Get Ajax Handler
	var ajax = flexInputCheck.sendit.AjaxHandler;

	// Check if ready and get response
	if (ajax.readyState == 4 && ajax.status == 200)
	{
		if(ajax.responseText.substring(0, 5) == '##1#;')
		{
			// Give error message
			alert('Error while sending the message, please try again');
			
			// Enable the send button
			flexGlobal.getObject('send').disabled = false;
		}
		else if(ajax.responseText.substring(0, 5) == '##0#;')
		{
			// Success, return to homepage
			alert('The message was successfully send.\n\nPress Ok button to continue');
			
			// Go back to the homepage
			loadContent(0);
		}
		else
		{
			return false;
		}
	}
}