/**
 * @author 
 */
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';
		});
	}
}

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;
}


		function IsNumeric(sText)
		{
		   var ValidChars = "0123456789.";
		   var IsNumber=true;
		   var Char;
		
		 
		   for (i = 0; i < sText.length && IsNumber == true; i++) 
		      { 
		      Char = sText.charAt(i); 
		      if (ValidChars.indexOf(Char) == -1) 
		         {
		         IsNumber = false;
		         }
		      }
		   return IsNumber;   
	   }
		
	jQuery(document).ready(function($){
		/*
		//Lightbox
			jQuery('a[rel*=lightbox]').lightBox({
				keyToClose:"c",
				imageLoading: '/js/libraries/lightbox/images/lightbox-ico-loading.gif',
				imageBtnClose: '/js/libraries/lightbox/images/lightbox-btn-close.gif',
				imageBtnPrev: '/js/libraries/lightbox/images/lightbox-btn-prev.gif',
				imageBtnNext: '/js/libraries/lightbox/images/lightbox-btn-next.gif'			
			}); 
		//############
		*/
		
		// Form alege judet
		var alegeOras = jQuery('#alege_oras');
		alegeOras.nextAll(':submit').hide();
		alegeOras.change(function(){
			jQuery(this).parents('form').submit();
		});



/*
        $('.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');
	});
	//jQuery('a[rel*=lightbox]').lightbox();


    function removeMessage(row, message_id) {
        var $ = jQuery;
        $.getJSON('/administrare/sterge_mesaj.php', { id: message_id }, function(r) {
            if ("console" in window)
                console.log(r);
            if (r)
                if (r.success)
                {
                    $('#controller_' + row).hide().remove();
                }
                else
                    alert(data.message);
        });
    }
