var xmlHttp
var inErrors=0;
var emptyErrors=0;
var afterErrors=0;
//function showButton()
//{
// document.getElementById("submit").disabled=false;
//}
//function hideButton()
//{
// document.getElementById("submit").disabled=true;	
//}


function password(el)
{
	usertbl = document.getElementById(el);	
	info = document.getElementById("infoTr").rowIndex;
	pass = document.getElementById('password');
	pass2 = document.getElementById('password2');
	
	if(!pass && !pass2)
	{
		srow = usertbl.insertRow(info);
		cell0 = srow.insertCell(0);	
		cell1 = srow.insertCell(1);
		cell2 = srow.insertCell(2);
	
		cell0.innerHTML = "Hasło: ";
        
  		element1 = document.createElement('input');
	    element1.setAttribute('type','password');
 		element1.setAttribute('class','txt-in');
 		element1.className = 'txt-in';
	    element1.setAttribute('name','password');
	    element1.setAttribute('id','password');
	    cell1.appendChild(element1);
    
	
		srow = usertbl.insertRow(info+1);
		cell0 = srow.insertCell(0);	
		cell1 = srow.insertCell(1);
		cell2 = srow.insertCell(2);
	
		cell0.innerHTML = "Powtórz hasło:";
        
	    element1 = document.createElement('input');
	    element1.setAttribute('type','password');
	    element1.setAttribute('class','txt-in');
	    element1.className = 'txt-in';
	    element1.setAttribute('name','password2');
	    element1.setAttribute('id','password2');
	    element1.setAttribute('onblur','validatePassword()');
	    element1.onblur = function()
	    {
	    	validatePassword();
	    }
	    cell1.appendChild(element1);
    
	    cell2.setAttribute('class','error');
	    cell2.className = 'error';
	    cell2.setAttribute('id','passwordField'); 

		link = document.getElementById('passwordLink');
		link.setAttribute('onclick','removePassword("profileTable")');

		link.onclick = function()
		{
			removePassword("profileTable");	
		}

    }
   
}

function removePassword(el)
{
	usertbl = document.getElementById(el);	
	info = document.getElementById("infoTr").rowIndex;
	pass = document.getElementById('password');
	pass2 = document.getElementById('password2');
	
	if(pass && pass2)
	{
		usertbl.deleteRow(info-2);
		usertbl.deleteRow(info-2);
		link = document.getElementById('passwordLink');
		link.setAttribute('onclick','password("profileTable")');
		link.onclick = function()
		{
			password("profileTable");	
		}
	}
}

function addrow(el)
{
	usertbl = document.getElementById(el);
	nip = document.getElementById('nip');
	firm = document.getElementById('firm');
	if(!nip && !firm)
	{
	srow = usertbl.insertRow(0);
	cell0 = srow.insertCell(0);	
	cell1 = srow.insertCell(1);
	cell2 = srow.insertCell(2);
	
	cell0.innerHTML = "Nazwa firmy: ";
        
    element1 = document.createElement('input');
    element1.setAttribute('type','text');
    element1.setAttribute('class','txt-in');
    element1.className = 'txt-in';
    element1.setAttribute('name','firm');
    element1.setAttribute('id','firm');
    element1.setAttribute('onblur','validateString(this.value,this.id)');
    element1.onblur = function()
	    {
	    	validateString(this.value,this.id);
	    }
    cell1.appendChild(element1);
    
    cell2.setAttribute('class','error');
    cell2.className = 'error';
    cell2.setAttribute('id','firmField'); 
	
	srow = usertbl.insertRow(4);
	cell0 = srow.insertCell(0);	
	cell1 = srow.insertCell(1);
	cell2 = srow.insertCell(2);
	
	cell0.innerHTML = "NIP:";
        
    element1 = document.createElement('input');
    element1.setAttribute('type','text');
    element1.setAttribute('class','txt-in');
    element1.className = 'txt-in';
    element1.setAttribute('name','nip');
    element1.setAttribute('id','nip');
    element1.setAttribute('onblur','validateString(this.value,this.id)');
    element1.onblur = function()
	    {
	    	validateString(this.value,this.id);
	    }
    cell1.appendChild(element1);
    
    cell2.setAttribute('class','error');
    cell2.className = 'error';
    cell2.setAttribute('id','nipField'); 
    

    }
}
function substractrow(el)
{
	usertbl = document.getElementById(el);
	nip = document.getElementById('nip');
	firm = document.getElementById('firm');

	if(nip)
	{
		srow = usertbl.deleteRow(4);
	}
	if(firm)
	{
		srow = usertbl.deleteRow(0);
	}

}

function validatePassword()
{ 
	var pass1 = document.getElementById("password").value;
	var pass2 = document.getElementById("password2").value;
	var length = (document.getElementById("password2").value).length;
	if ( pass1 != pass2)
		document.getElementById("passwordField").innerHTML = "Hasła muszą być takie same"
	else
	{ 	if ( pass1.length > 30 || pass1.length < 5)
			document.getElementById("passwordField").innerHTML = "Hasło może mieć 5 do 30 znaków"	
		else	
			{
			document.getElementById("passwordField").innerHTML = ""	
			return false;
			}
	}	
	return true;
}

function validateString( str, destination)
{
 destinationStr = destination + "Field";
if (str.length==0)
  { 
  document.getElementById(destinationStr).innerHTML="Pole nie może być puste"
  return
  }
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
  {
  alert ("Browser does not support HTTP Request")
  return
  } 
var url="scripts/validate.html"
url=url+"?type="+destination+"&test="+str
//url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
} 

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById(destinationStr).innerHTML=xmlHttp.responseText 
 //	if ( xmlHttp.responseText != "" )
 //	{
 		
 //	}
 } 
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 // Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

	



function checkForm()
{
var tables;	

tables = document.getElementsByTagName('td')

	for (i=0; i<tables.length; i++) 
	{
		if (tables[i].className == "error")
		{
			if (tables[i].innerHTML == '' )
			{
				tables[i].style.color = '#000000';
			}
			else
			{
				inErrors = inErrors + 1; 
				tables[i].style.color = '#ff0000';
			}
		}		
	}
		
	if (inErrors > 0)
	{
		alert ("Pola zostały wypełnione nieprawidłowo lub są puste");
		inErrors = 0;
		return false;
	}

	tables = document.getElementsByTagName('input')
	for (i=0; i<tables.length; i++)
	{
		if ( (tables[i].type == "text" && tables[i].name != "username") ||
		     (tables[i].type == "password" && tables[i].name != "password"))
		{
			if (tables[i].value == '' )
			{
				emptyErrors++;
			}	
		}
	}

    if (emptyErrors > 0)
	{
		alert ("Żadne pole nie może być puste");
		emptyErrors = 0;
		return false;
	}

	if (document.referrer != 'http://inkdividual.com/contact.html' && document.referrer != 'http://inkdividual.com/profile.html')
	{
		if (!document.getElementById('agreement').checked)
		{
			alert ("Należy zaakceptować regulamin");
			return false;
		}
	}	
	
	else return true;
}	