var contactOwner = function(callback)
{
	__fields 		= ["name", "details"];
	__values 		= ["nume", "detalii"];
	__errors 		= "";
	__first_err_fld = "";

	for (i=0;i<__fields.length;i++)
	{
		if (document.getElementById(__fields[i]).value=="")
		{
			__errors += "\t"+__values[i]+"\n";
			if (__first_err_fld=="")
				__first_err_fld = __fields[i];
		}
	}
	
	if (__errors!="")
	{
		alert("Unul sau mai multe din urmatoarele campuri sunt incomplete:\n\n"+__errors+"\nVa rugam verificati.")
		document.getElementById(__first_err_fld).focus();
	}
	else
	{
		if ( (document.getElementById("email").value!="") && (!isEmail(document.getElementById("email").value)) )
		{
			alert("Adresa de email este incorecta.")
			document.getElementById("email").focus();
		}
		else
		{
			if (document.getElementById("phone1").value=="" && document.getElementById("phone2").value=="" && document.getElementById("email").value=="")
			{
				alert("Pentru a va putea contacta inapoi trebuie sa ne lasati un numar de telefon sau o adresa de email.")
				document.getElementById("phone1").focus();
			}
			else
			{
				$.post("/ajax/contact_owner.php", $("#contact_owner").serialize(),
					function(data){
						if (data=="ok")
						{
                            if (!!callback)
                            {
                                callback();
                            }
                            else
                                $('#contact_owner_rs').fadeIn(function() {
                                    $('#contact_owner_rs').html('<div style="padding:5px 0px 10px 10px;">Informatiile au fost trimise cu succes.<br />Va multumim pentru timpul acordat.</div>');
                                });
						}
						else
							alert(data);
				});
			}
		}
	}
}

var contactMaster = function()
{
	__fields 		= ["name", "details"];
	__values 		= ["nume", "detalii"];
	__errors 		= "";
	__first_err_fld = "";

	for (i=0;i<__fields.length;i++)
	{
		if (document.getElementById(__fields[i]).value=="")
		{
			__errors += "\t"+__values[i]+"\n";
			if (__first_err_fld=="")
				__first_err_fld = __fields[i];
		}
	}
	
	if (__errors!="")
	{
		alert("Unul sau mai multe din urmatoarele campuri sunt incomplete:\n\n"+__errors+"\nVa rugam verificati.")
		document.getElementById(__first_err_fld).focus();
	}
	else
	{
		if ( (document.getElementById("email").value!="") && (!isEmail(document.getElementById("email").value)) )
		{
			alert("Adresa de email este incorecta.")
			document.getElementById("email").focus();
		}
		else
		{
			if (document.getElementById("phone1").value=="" && document.getElementById("phone2").value=="" && document.getElementById("email").value=="")
			{
				alert("Pentru a va putea contacta inapoi trebuie sa ne lasati un numar de telefon sau o adresa de email.")
				document.getElementById("phone1").focus();
			}
			else
			{
				$.post("ajax/contact_master.php", $("#contact_master").serialize(),
					function(data){
						if (data=="ok")
						{
							$('#contact_master_rs').fadeIn(function() {
								$('#contact_master_rs').html("Informatiile au fost trimise cu succes.<br />Va multumim pentru timpul acordat.");
							})
						}
						else
							alert(data);
				});
			}
		}
	}
}

var showHideDetails = function(myID, rID)
{
	if (document.getElementById("details_"+myID).style.display=='none')
		document.getElementById("details_"+myID).style.display='';
	else
		document.getElementById("details_"+myID).style.display='none';

	if (document.getElementById("controller_"+myID).style.fontWeight==900)
	{		
		$.get('../ajax/mark_contactare_as_read.php?rID='+rID, function(data)
		{
			if (data=="ok")
				document.getElementById("controller_"+myID).style.fontWeight = 100;
		});
	}
}

var removeContactare = function(myID, rID)
{
	if (confirm("Sunteti sigur/a ca vreti sa stergeti aceasta inregistrare?"))
	{
		$.get('../ajax/delete_contactare.php?rID='+rID, function(data)
		{
			if (data=="ok")
				document.getElementById("controller_"+myID).style.display = 'none';
		});
	}
}


$(document).ready(function()
{
	$('a[name=modal]').click(function(e)
	{
		e.preventDefault();
		var id = $(this).attr('href');
	
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		$('#maskYM').css({'width':maskWidth,'height':maskHeight});
		$('#maskYM').fadeIn(1000);	
		$('#maskYM').fadeTo("slow",0.8);	
	
		var winH = $(window).height();
		var winW = $(window).width();
        
		var scrollTop = document.documentElement.scrollTop;
		$(id).css('top',  winH/2-$(id).height()/2+scrollTop);
		$(id).css('left', winW/2-$(id).width()/2);

		$(id).fadeIn(2000); 
	});
	
	$('.window .close').click(function (e)
	{
		e.preventDefault();
		$('#maskYM').hide();
		$('.window').hide();
	});		
	
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});		



    $('.tooltip[title]').bt({
        trigger: 'hover',
        padding: 10,
        width: 200,
        spikeLength: 5,
        spikeGirth: 5,
        cornerRadius: 5,
        //fill: 'rgba(97, 158, 225, .9)',
        strokeWidth: 1,
        //strokeStyle: '#CCC',
        //cssStyles: {color: '#FFF', fontWeight: 'bold'}
    });
    $('.v-tooltip[title]').bt({
        trigger: 'hover',
        positions: ['top'],
        padding: 10,
        width: 200,
        spikeLength: 5,
        spikeGirth: 5,
        cornerRadius: 5,
        fill: 'rgba(97, 158, 225, .9)',
        strokeWidth: 1,
        strokeStyle: '#CCC',
        cssStyles: {color: '#FFF', fontWeight: 'bold'}
    });
    $('.lg-tooltip[title]').bt({
        trigger: 'hover',
        padding: 10,
        width: 450,
        spikeLength: 5,
        spikeGirth: 5,
        cornerRadius: 5,
        fill: 'rgba(255, 255, 255, 1)',
        strokeWidth: 1,
        strokeStyle: '#CCC',
        cssStyles: {color: '#000', fontWeight: 'bold'}
    });
	
	$('a.ajaxylink, a[href^=##]').attr('href', function(index, old_value) {
        return old_value.replace(/^##/, '')
    }).removeClass('ajaxylink');
});

var sendToIM = function()
{
	var id_mess = document.getElementById("id_mess").value
	var message = document.getElementById("message").value
	if (id_mess=="")
	{
		alert("Introdu ID-ul de messenger mai intai.");
		document.getElementById("id_mess").focus();
	}
	else
	{
		document.location='ymsgr:sendim?'+id_mess+"&m="+message;
	}
}

var sendToMail = function(articleID)
{
	var emailSend = document.getElementById("emailSend").value
	var emailSubject = document.getElementById("emailSubject").value
	var yourName = document.getElementById("yourName").value

	if (emailSend=="" || emailSubject=="" || yourName=="")
	{
		alert("Toate campurile sunt obligatorii");
		document.getElementById("emailSend").focus();
	}
	else
	{
		$.get('ajax/sendToMail.php?emailSend='+emailSend+'&emailSubject='+emailSubject+'&yourName='+yourName+"&id="+articleID, function(data)
		{
			alert(data);
			document.getElementById("emailSend").value = '';
		});
	}
}

var checkIfSendToIM = function(e)
{
	if (e.keyCode==13)
		sendToIM();
}




// creare cont
var showHidePresentation = function()
{
	if (document.getElementById("user_type"))
	{
		if (document.getElementById("user_type").value==2)
		{
			$('#presentation_container').show(1000);
			$('#logo_container').show(1000);
			$('#phone_label').html("Telefon mobil *:");
		}
        else if (document.getElementById("user_type").value == 3)
        {
			$('#presentation_container').hide(1000);
			$('#logo_container').show(1000);
        }
		else
		{
			$('#presentation_container').hide(1000);
			$('#logo_container').hide(1000);
			$('#phone_label').html("Telefon *:");
		}
	}
}	

var attachUploadButton = function(fileToLoad)
{
	if (document.getElementById(fileToLoad))
	{
		jQuery(function(){                    
			new AjaxUpload(fileToLoad, {
				action: 'ajax/upload_logo.php?file2load='+fileToLoad,
				onComplete: function(file, response){
					document.getElementById(fileToLoad+"_container").innerHTML = "Upload complet.";
					document.getElementById("logo_creare_cont").value = response;
				}
			});
		});                    
	}
}

var isEmail = function(email)
{
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(email))
		return true;
	else
		return false;
}

var saveAccount = function()
{
	__fields 		= ["user_type", "username", "password", "password2", "name", "email", "judet", "phone"];
	__fieldsAgentie	= ["user_type", "username", "password", "password2", "name", "email", "judet", "phone", "prezentare", "denumire_agentie", "descriere_agentie", "comision_inchiriere", "comision_luat_vanzatorului", "comision_luat_cumparatorului", "localitate", "adresa"];
	__values 		= ["tip utilizator", "nume utilizator", "parola", "repetare parola", "nume", "email", "judet", "telefon", "prezentare", "prezentare", "denumire agentie", "descriere agentie", "comision inchiriere", "comision luat vanzatorului", "comision luat cumparatorului", "localitate", "adresa"];
	__compares 		= [0, "", "", "", "", "", 0, "", "", "", "", "", "", "", "", ""];
	__errors 		= "";
	__first_err_fld = "";
	
	if (document.getElementById("user_type").value==2)
	{
		for (i=0;i<__fieldsAgentie.length;i++)
		{
			if (document.getElementById(__fieldsAgentie[i]).value==__compares[i])
			{
				__errors += "\t"+__values[i]+"\n";
				if (__first_err_fld=="")
					__first_err_fld = __fieldsAgentie[i];
			}
		}
	}
	else
	{
		for (i=0;i<__fields.length;i++)
		{
			if (document.getElementById(__fields[i]).value==__compares[i])
			{
				__errors += "\t"+__values[i]+"\n";
				if (__first_err_fld=="")
					__first_err_fld = __fields[i];
			}
		}
	}
	
	if (__errors!="")
	{
		alert("Unul sau mai multe din urmatoarele campuri sunt incomplete:\n\n"+__errors+"\nVa rugam verificati.")
		document.getElementById(__first_err_fld).focus();
	}
	else
	{
		if (document.getElementById("password").value!=document.getElementById("password2").value)
		{
			alert("Cele doua parole nu coincid. Va rugam verificati.")
			document.getElementById("password2").focus();
		}
        else if (!document.getElementById('username').value.match(/^[a-zA-Z0-9_]*$/))
        {
            alert("Numele de utilizator poate contine doar cifre, litere si caracterul underscore (_).")
            document.getElementById("username").focus();
        }
		else
		{
			if (!isEmail(document.getElementById("email").value))
			{
				alert("Adresa de email este incorecta.")
				document.getElementById("email").focus();
			}
			else
			{
                if (!document.getElementById('accept_t_c').checked)
                {
                    alert('Trebuie sa acceptati termenii si conditiile');
                    return false;
                }
				$.post("ajax/save_account.php", $("#accountForm").serialize(),
					function(data){
						if (data=="ok")
						{
                            document.location.href = "/login.php";
                            return true;
							$('#accountContainer').fadeIn(function() {
								$('#accountContainer').html('<div style="padding:5px;">Contul a fost creat cu succes.<br />Pentru activare, un email de confirmare a fost transmis pe adresa specificata.<br />Verificati atat in mesajele din inbox, cat si in Spam, mailul transmis de noi pentru a va activa acest cont.</div>');
							})
						}
						else
						{
                            if (Recaptcha)
                                Recaptcha.reload();
							alert(data);
						}
				});
			}
		}
	}
}
//creare cont



window.onload = function()
{
	showHidePresentation();
	attachUploadButton('file_logo');
}

