function setupInnerPass(companyId, personId){
	showModal('cargando');
	$.ajax({
		  type: 'POST',
		  url: '/user/innerpass_setup.php',
		  data: 'companyId=' + companyId + '&personId=' + personId,
		  dataType: 'json',
		  success: function(data) {
			if(data.resourceId !=null){
		    	$('#show_message #title').html('Congratulations');
		    	$('#show_message #message').html('Your acount was configured properly.');
		    	$('#setup_innerpass').hide();
		    }	
		    else{ 
		    	$('#show_message #title').html('Error');
		    	$('#show_message #message').html(data.error);
		    };
		    $('#cargando').hide();
		    showModal('#show_message');
		  }
		});
}

function innerPassRequestMeeting(supplierCompanyId, buyerId){
	showModal('cargando');
	$.ajax({
		  type: 'POST',
		  url: '/user/innerpass_meeting.php',
		  data: 'supplierCompanyId=' + supplierCompanyId + '&buyerId=' + buyerId,
		  dataType: 'json',
		  success: function(data) {
			showModal('#cargando');
			if(data.meetingID !=null){
		    	$('#show_message #title').html('Congratulations');
		    	$('#show_message #message').html('Your meeting is scheduled.');
		    	$('#setup_innerpass').hide();
		    }	
		    else{ 
		    	$('#show_message #title').html('Error');
		    	$('#show_message #message').html(data.error);
		    };
		    $('#cargando').hide();
		    showModal('#show_message');
		  }
		});
}

function noInnerPassInfo(){
	$('#show_messa	ge #title').html('Error');
    $('#show_message #message').html('You don\'t complete your skype name or phone number. Please click <a href="/user/companyinformation#Phone">here</a> to complete.');
    showModal('#show_message');
}


function loadState( selected ){
	$("#Address_CountryID").change(function () {
		$("#Address_StateID").html("<option>Loading...</option>");
		$("#Address_CountryID option:selected").each(function () {
   			elegido=$(this).val();
    	    $.post("/functions/ajax/selects.php", { CountryID: elegido, element: "state", StateID: selected  }, function(data){
    			$("#Address_StateID").html(data);
    		});        
		});
	})
	if( selected != 0 ){ 
		$("#Address_StateID").html("<option>Loading...</option>");
		$("#Address_CountryID option:selected").each(function () {
			elegido=$(this).val();
		    $.post("/functions/ajax/selects.php", { CountryID: elegido, element: "state", StateID: selected  }, function(data){
				$("#Address_StateID").html(data);
			});        
		});
	}
}

function addFriend( friendid){ 
	showModal("#cargando");
	$.post("/user/add_friend.php", { friendid: friendid}, function(data){
		$('#cargando').hide();
		if(data.indexOf('ok') > 0){
			showModal("#popup_thanks");
			$('#add_friend' + friendid).html('<p>Pending</p>');
			$('#add_friend' + friendid).attr('class', 'search_users_list_pendant');
		}
		else{
			$('#mask .popup').hide();
		}
		
	});
}

function updateCompanyLogo( companyid){ 
	$.post("/user/update_company_logo.php", { companyid: companyid}, function(data){
	});
}

function joinGroup( groupid){ 
	showModal("#cargando");
	$.post("/user/join_group.php", { groupid: groupid}, function(data){
		if(data.indexOf('ok') > 0){
			$('#joinGroup' + groupid).html("<p class=\"search_users_list_joined\">Joined</p>");
			$('[name=join]').remove();
			var members = parseInt($('#groupMembers' + groupid).html());
			$('#groupMembers' + groupid).html(members + 1);
		}
		$('#cargando').hide();
		$('#mask').hide();
	});
}

function sendPrivateMessage(to, subject, message){
	$('#write_message').hide();
	showModal("#cargando");
	$.post("/user/private_message.php", { to: to, subject: subject, message: message}, function(data){
		$('#cargando').hide();
		showModal("#message_sended");
	});
}

function showResume( id ){ 
	showModal("#cargando");
	$.post("/user/user_resume.php", { id: id}, function(data){
		$('#cargando').hide();
		$('#user_resume').html(data);
		$('[name=close]').click(function (e) {
			//Cancelar el comportamiento del enlace
			e.preventDefault();
			$('#mask, .popup').hide();
		});
		showModal('#user_resume');
	});
}


function deleteFriendConfirm(friendid){
	$('#friendToDelete').attr('value', friendid);
	showModal('#delete_colleague');
}

function deleteFriend(){
	var friendid = $('#friendToDelete').attr('value');
	$('#delete_colleague').hide();
	showModal("#cargando");
	$.post("/user/delete_friend.php", { id: friendid}, function(data){
		$('#cargando').hide();
		if(data.indexOf('ok') > 0){
			$('#' + friendid ).remove();
		}
		$('#mask, .popup').hide();
		
	});	
}

function showEmbed(id, type){
	showModal("#cargando");
	$.post("/user/get_content_embed.php", { id: id, type: type}, function(data){
		$('#cargando').hide();
		$("#embed_code").html(data);
		showModal("#embed");
	});
}

function showVirtualTour(companyid){
	showModal("#cargando");
	$.post("/user/get_presentation_video.php", { companyid: companyid}, function(data){
		$('#cargando').hide();
		$("#embed_code").html(data);
		showModal("#embed");
	});
}

function updatePhotosCount( ){
	$.post("/user/get_photos_count.php", { }, function(data){
		$('#uploaded_photos').html(data);
	});
}

function updateMiniGallery( ){
	$.post("/user/get_photos_minigallery.php", { }, function(data){
		$('#mini_gallery').html(data);
	});
}


function openGallery( companyid ){
	showModal("#cargando");
	$.post("/user/gallery.php", {companyid : companyid }, function(data){
		$('#gallery_content').html(data);
		$('#cargando').hide();
		showModal("#gallery");
	});
}


function showPresentationSlide(companyid){
	showModal("#cargando");
	$.post("/user/get_presentation_slide.php", { companyid: companyid}, function(data){
		$('#cargando').hide();
		$("#embed_code").html(data);
		showModal("#embed");
	});
}

function unreadMessages(){
	$(":checkbox:checked").each(function(){
		
		$.post("/user/unread_message.php", { id: this.value}, function(data){
		});
	});
	
	$(":checkbox:checked").each(function(){
		$('#row_' + this.value).attr('class', 'inbox_message_unread');		
	});
	
	$("input[name=message]").each(function(){
			this.checked = 0;
	});
}

function loadSelect( select, selected ){
	$("#" + select + "_id option:selected").each(function () {
		elegido=$(this).val();
	    $.post("functions/ajax/ws_team.php", { element: select, company_type_id: selected  }, function(data){
			$("#" + select + "_id").html(data);
		});        
	});
}

function validateUserEmail(element_id, error_element_id ){
	
	if($('#' + element_id).val() != '')
		$('#' + error_element_id).load('/functions/ajax/validators.php', {type: "user_email",email : $('#' + element_id).val()});
}


function ImgError(source, img){
    source.src = img;
    source.onerror = "";
    return true;
}

function toFriendly( val ){
	if(val)
		return val.replace(/\s/g, "-");
}

//Funcion para borrar contendio de formularios
function clearField(obj) {
	if (obj.defaultValue==obj.value) obj.value = '';
}

function trim(s)
{
    var l=0; var r=s.length -1;
    while(l < s.length && s[l] == ' ')
    {     l++; }
    while(r > l && s[r] == ' ')
    {     r-=1;     }
    return s.substring(l, r+1);
} 

function makeMeetingRequest(){
	$('#paner').scrollTo(0, 1);
	showModal('#popup_meetingrequest');
}
/************************************************************************************************************/
/*											Pop up functions												*/
/************************************************************************************************************/
$(document).ready(function() {
//	selecciona todo el tag A con el nombre igual al modal
	$('a[name=modal]').click(function(e) {
		$('#mask, .popup').hide();
		var id = $(this).attr('href');
		//Cancela el comportamiento del enlace
		$('html, body').animate({scrollTop:0});
		e.preventDefault();
		showModal(id);
	});
	//Si se presiona el bot�n Close
	$('[name=close]').click(function (e) {
		//Cancelar el comportamiento del enlace
		e.preventDefault();
		$('#mask, .popup').hide();
	});
	
	$('[name=form]').submit(function(e) {
		showModal("#cargando");
	});

});

function showModal(id){
	//Obt�n el alto y ancho de la pantalla

	var maskHeight = $(document).height();
	var maskWidth = $(window).width();
	//Configura el alto y ancho de la m�scara para rellenar la pantalla
	$('#mask').css({'width':maskWidth,'height':maskHeight});
	
	//transition effect
	
	//Obt�n el alto y ancho de la ventana
	var winH = $(window).height();
	var winW = $(window).width();
	//Configura la ventana popup para que est� centrada
	$(id).css('top',  winH/2-$(id).height()/2);
	$(id).css('left', winW/2-$(id).width()/2);
	
	$('#mask').fadeIn(500);
	$('#mask').fadeTo("slow",0.8);
	$('html, body').animate({scrollTop:0});
	$(id).fadeIn(1000);
	
	
	
}


//###########################################################
//Menu desplegable
//###########################################################

var timeout	= 4000;
var closetimer	= 800;
var ddmenuitem	= 0;

//open hidden layer
function mopen(id)
{	
	// cancel close timer
	mcancelclosetime();

	// close old layer
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

	// get new layer and show it
	ddmenuitem = document.getElementById(id);
	ddmenuitem.style.visibility = 'visible';

	if(document.getElementById('mJob')) {
		document.getElementById('mJob').style.color = "#fff";
		document.getElementById('mJob').style.background = "#b9a791";
	}
}
//close showed layer
function mclose()
{
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
	if(document.getElementById('mJob')) {
		document.getElementById('mJob').style.color = "#a09a95";
		document.getElementById('mJob').style.background = "";
	}
}

//go close timer
function mclosetime()
{
	closetimer = window.setTimeout(mclose, timeout);
}

//cancel close timer
function mcancelclosetime()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

//close layer when click-out
document.onclick = mclose;
