/**
 * @author Rok
 */

var pnv;
if (!pnv) pnv = {};
if (!pnv.tv) pnv.tv = {};

pnv.tv.contact = function () {
}

pnv.tv.contact.query = "";
pnv.tv.contact.successful = false;

pnv.tv.contact.send = function ()
{
	if (pnv.tv.contact.checkFields() && !(pnv.tv.contact.successful))
	{
		pnv.tv.contact.query = pnv.tv.contact.makeGet();


		ret = pnv.tv.contact.commit();
				
				if (ret == 'true')
				{
					pnv.tv.contact.successful = true;
					document.getElementById("kontaktni_obrazec_cont").innerHTML = "<p><b>Sporočilo je bilo poslano!</b></p>";
				}

		
		return false;
	}
	else
	{
		return false;
	}
}

pnv.tv.contact.checkFields = function()
{
	if (document.getElementById("kontakt_ime").value == "")
	{
		alert("Vpisati morate vaše ime!");
		return false;
	}
	
	if (document.getElementById("kontakt_email").value == "")
	{
		alert("Vpisati morate vaš e-poštni naslov!");
		return false;
	}
	else
	{
		email = document.getElementById("kontakt_email").value;
		AtPos = email.indexOf("@");
		StopPos = email.lastIndexOf(".");
		
		if (AtPos == -1 || StopPos == -1) {
			alert("Vaš e-poštni naslov ni veljaven!");
			return false;
		}
	}
	
	if (document.getElementById("kontakt_besedilo").value == "")
	{
		alert("Vpišite vaše sporočilo!");
		return false;
	}
	
	if (document.getElementById("kontakt_kontrolna").value == "")
	{
		alert("Vpišite kontrolno številko!");
		return false;
	}
	
	if (document.getElementById("kontakt_kontrolna").value != document.getElementById("kontakt_nakljucna").value)
	{
		alert("Številka, ki ste jo vpisali se ne ujema s kontrolno številko!");
		return false;
	}
	
	return true;
}

pnv.tv.contact.makeGet = function(){
	var getstr = "?";
	
	getstr += "ime=" + replace_sumniki2(document.getElementById('kontakt_ime').value) + "&";
	getstr += "email=" + document.getElementById('kontakt_email').value + "&";
	getstr += "besedilo=" + replace_sumniki2(document.getElementById('kontakt_besedilo').value) + "&";
	getstr += "kontrolna=" + document.getElementById('kontakt_kontrolna').value;														
	
	return getstr;
	
}

pnv.tv.contact.commit = function (){
	xmlHttp = ajax_GetXmlHttpObject();
	
	if (xmlHttp == null) {
		alert("Vaš brskalnik ne podpira tehnologije AJAX");
		return;
	}
	
	var url = 'ajax/contact_commit.php';
	url = url + pnv.tv.contact.query;
	url = url + "&sid=" + Math.random();
	xmlHttp.open("GET", url, false);
	xmlHttp.send(null);
	
	return xmlHttp.responseText;
}

replace_sumniki2 = function (chr)
{
	chr = chr.replace(/č/g, "c");
	chr = chr.replace(/š/g, "s");
	chr = chr.replace(/ž/g, "z");
	chr = chr.replace(/Č/g, "C");
	chr = chr.replace(/Š/g, "S");
	chr = chr.replace(/Ž/g, "Z");
	
	return chr;
}
