/*
This file contains ECMAScript (JavaScript) code to check
required fields are filled.
*/

// *********************************************************
onerror=handleErr;

// *********************************************************
function handleErr(msg,url,l)
{
	/*
	Handles script error on page.
	*/
	var txt = "";
	txt+="There was an error on this page.\n\n";
	txt+="Error: " + msg + "\n";
	txt+="URL: " + url + "\n";
	txt+="Line: " + l + "\n\n";
	txt+="Click OK to continue.\n\n";
	alert(txt);
	return true;
}

// *********************************************************
function check_field(field)
{
	/*
	If field is filled, normal return expected.
	Exception is throwed otherwise.
	*/
	var n = field.name;
	var v = field.value;

	if ( v == "" )
	{
		throw("Field " + n + " is empty!");
	}

	if ( n == "email" )
	{
		var p = v.search("@");
		var s = "You have typed wrong e-mail address!";
		if ( (p == -1) || (p == 0) || (p == (v.length-1)) )
		{
			throw(s);
		}
		var t = v.slice(p+1);
		var i = t.search(/\./);
		if ( (i == -1) || (i == 0) || (i == (t.length-1)) )
		{
			throw(s);
		}
	}

	if ( n == "cvfile" )
	{
		var s = "You have select wrong CV file!";
		var p = v.length;
		p -= 1; // Pointer to the last character.
		p -= 2; // Pointer to the third right character.
		if ( (p < 0) )
		{
			throw(s);
		}
		var t = v.slice(p);
		t = t.toLowerCase();
		if ( t != "doc" )
		{
			throw(s);
		}
	}

	return;
}

// *********************************************************
function try_to_submit()
{
	/*
	Tries to submit upload form, if all required fields are filled.
	*/
	// Initialise variables
	var first_name = document.getElementsByName("first_name")[0];
	var last_name = document.getElementsByName("last_name")[0];
	var email = document.getElementsByName("email")[0];
	var id_occ_area = document.getElementsByName("id_occ_area")[0];
	var cvfile = document.getElementsByName("cvfile")[0];
	var formupload = document.getElementsByName("formupload")[0];

	try
	{
		check_field(first_name);
		check_field(last_name);
		check_field(email);
		check_field(id_occ_area);
		check_field(cvfile);
		formupload.submit();
	}
	catch(except)
	{
		alert(except + '\n' + "Please, fill it by reasonable value.");
	}
}

// *********************************************************
// End of file.
// *********************************************************
