
//-------- FORM validator
function formValidator()
{
	// your validation code !!!

	return true;
}



//-------- trim function
function trim(str) 
{
	str = str.replace(/^\s+/, '');

	for (var i = str.length - 1; i >= 0; i--) 
	{
		if (/\S/.test(str.charAt(i))) 
		{
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}



//-------- Setting ERROR function
var setInnerHTML = function(elementId, text)
{
	document.getElementById(elementId).style.color = 'red';
	document.getElementById(elementId).style.fontWeight = 'bold';
    document.getElementById(elementId).innerHTML = text;
} 



//-------- reload Token
function reloadToken()
{
	var img_token = document.getElementById('img_token');
	var path = img_token.src;
	var hash = path.substr(path.length - 1, 8);
	
	if (hash == '#')
	{
		img_token.src = 'token.php';
	}
	else
	{
		img_token.src = 'token.php#';
	}
}




//--------- checkToken - MINT Ajax
function checkToken() 
{
	// validate token
	var token = document.getElementById('token_text');	
	token.value = trim(token.value);	
	if( token.value.length < 1 ) 
	{
		setInnerHTML("error", "Nie wpisałeś(aś) Tokena");
		token.style.backgroundColor = '#FFFFCC';
		token.focus();
		return false;
	}

	

	var req = mint.Request();
    req.AddParam("token_text", document.getElementById('token_text').value);
   
    req.OnSuccess = function() 
    {	

    	if(this.responseText == 'true')
		{	
			if( formValidator() )
			{	
				document.token_form.submit();
			}
		}
		else
		{
			setInnerHTML("error", "Podałeś(aś) błędny Token");
			token.style.backgroundColor = '#FFFFCC';
			token.focus();
		}
	}
    
    req.OnAbort =  function() 
    {
        alert("Serwer ma problemy z odebraniem zapytania. Spróbuj ponownie późnej.");
    }
  
    req.OnError =  function() 
    {
        alert("Błąd podczas zapytania do serwera.");
    }
   
    req.Send("ajaxresponse/check_token.php");
} 
	

