// TPI Form Validator, Version 2 (Beta)
// Startup Function

// Disable JSCafe's Bootup Function
autoLoadOff = 1;

eventHandler.add(window, "load", function()
{
	// Set correct AJAX Directory
	serverURL = "/ajax";
	// And the TPI Webserver doesn't like POST via ajax
	method = "GET";
	// And the hold-place text
	var strDivInsert = "<div class=\"forminfo\"><!-- Form Validation by Mati Lurey, TPI v2 --></div>";
	
	// Find all forms with classname of validate
	var forms = getElements("form.validate");
	for (var i = 0; i < forms.length; i++)
	{
		// Override the submit function
		forms[i].onsubmit = function(e)
		{
			if (this.className.indexOf("validokay") == -1){Submit_Form(this); return false;}
		}
		
		// Put a place to add a message
		forms[i].innerHTML = strDivInsert + forms[i].innerHTML;
	}
});

function Submit_Form(objForm)
{
	// It's crazy ajax time
	
	// We need to get all INPUT and SELECT's inside of the form and send via AJAX to directory "ajax"
	// Oh wait, just a few lines of code =]
	
	// Find any form already with submitid
	var form = document.getElementById("submitid");
	if (form){form.id = "";}
	objForm.id = "submitid";
	
	// Clear all Invalid Entries
	ex(":!class! [obj='#submitid td *'] [class='invalid'] [type='rep']");
	// Execute Statement to Send Data
	ex(":!getdata! [obj='#submitid td *>=valthis'] [sendto='form_validate'] [withid='1']");
	// On focus remove class of invalid
	ex(":!event! [obj='#submitid td *'] [type='focus'] [code=';!edit! [obj=`#submitid div.forminfo`] ;!class! [obj=`=`] [class=`invalid`] [type=`rep`] ;!event! [obj=`=`] [type=`focus`] [delete=`1`]']");
	// On focus, also select current text
	ex(":!onfocus_select! [obj='#submitid td *']");
}
