
function opcRunDivWriter(cId, divIn)
	{
	createForm(cId, divIn);
	}

function createForm(cId, divIn)
	{
	var form = document.createElement("form");
	form.setAttribute('id', 'opcja_nlt_form');

	var emailLab = document.createElement("label");
	emailLab.setAttribute("for","opcja_nlt_form_email");
	emailLab.innerHTML = 'Twój e-mail';

	var emailInp = document.createElement("input");
	emailInp.setAttribute("name","nlt_email");
	emailInp.setAttribute("class","text");
	emailInp.setAttribute("type","text");
	emailInp.setAttribute("value","");
	emailInp.setAttribute("id","opcja_nlt_form_email");

	var emailSub = document.createElement("input");
	emailSub.setAttribute("name","go");
	emailSub.setAttribute("value","Dodaj");
	emailSub.setAttribute("type","button");
	emailSub.setAttribute("class","button");
	emailSub.setAttribute("onclick","opcSubmitData();");
	emailSub.setAttribute("id","opcja_nlt_form_submit");

	var cliIdInp = document.createElement("input");
	cliIdInp.setAttribute("name","nlt_c_id");
	cliIdInp.setAttribute("value", cId);
	cliIdInp.setAttribute("type", "hidden");


	form.appendChild(emailLab);
	form.appendChild(emailInp);
	form.appendChild(emailSub);
	form.appendChild(cliIdInp);
	divIn.appendChild(form);
	}

function opcSubmitData()
	{
	var form = document.getElementById('opcja_nlt_form');
	if (!opcValidateEmail(form.nlt_email.value))
		alert("Podany adres email nie jest prawidłowy!");
	else
		opcSendData(form.nlt_c_id.value, form.nlt_email.value);
	return false;
	}

function opcSendData(cId, email)
	{
	if (window.XMLHttpRequest)
			http = new XMLHttpRequest();
	else
			http = new ActiveXObject("Microsoft.XMLHTTP");

	http.open("GET","http://news.test.opcja.pl/index.php?m=fre&p=register_json&c_id="+cId+"&nlt_eml="+email, true);
	http.onreadystatechange = function()
		{
		if(http.readyState == 4 && http.statusText == 'OK')
			alert('Dziękujemy! Twój e-mail został zarejestrowany');
		}

	http.send();
	}

function opcValidateEmail(emailStr)
	{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(emailStr.search(reg) == -1)
		return false;
	else
		return true;
	}


