function createWindow(){
	var w = 366;
	var h = 200;
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	  
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',menubar=0,resizable=0,scrollbars=0, status=0,titlebar=1,toolbar=0,left=0,top=0';
	window.open('','newWindow',winprops);
	return true;
}

function sendMessage(){
   	var msg = '';
   	if($('#telefono').val()){ 
   		if(!/^\d{10}$/.test($('#telefono').val())) 
   			msg = '* El número telefónico debe ser de 10 dígitos.\n';
   	}else{
   		msg += '* El campo del teléfono es obligatorio.\n';
   	}
   	if($('#mensaje').val()){
   		if($('#mensaje').val().lenght > 150) 
   			msg += '* El mensaje debe ser menor a 150 caracteres.\n';
   	}else{
   		msg += '* El campo del mensaje es obligatorio.\n';
   	}
   	
   	if(msg!=''){
	   	alert(msg);
	   	return false;
   	}else{
   		createWindow();
   	}
}

function limitChars(textid, limit){
	var text = $(textid).val();
	var textlength = text?text.length:0;
	if(textlength > limit){
		$(textid).val(text.substr(0,limit));
		return false;
	}else{
		return true;
	}
}

/* Open the player */		
function openPlayer(boxId){
	$('#'+boxId).css('display','block');
	document.getElementById('filter').style.display='block';
}

/**
 * onDomReady
 *
 */
$(document).ready(function(){
	truncateStr();

	$('#mensaje').keydown(function(){
		var text = $(this).val();
		var textlength = text?text.length:0;
		if(textlength > 150){
			return false;
		}else{
			return true;
		}
	});
	
	$('#mensaje').keypress(function(){
		var text = $(this).val();
		var textlength = text?text.length:0;
		if(textlength > 150){
			return false;
		}else{
			return true;
		}
	});
	
	$('#mensaje').keyup(function(){
		limitChars('#mensaje', 150);
	})
	
	if($('').autocomplete){
		$("#search").autocomplete({ajax_get : get_look_suggs,minchars:1,height:130});
	}

	if($('#terminalSeleccionada').length>0){
		var idTer = $.cookie(COOKIE_NAME);
		if(idTer!=null){
			$.getJSON(
				context+'/ajax/terminal/sessionTerminal.do',
				{'idTerminal':idTer},
				function(data){
					var a = $('#terminalSeleccionada');
					a.text(data.brand+' '+data.terminalName);
				}
			);
		}else{
			$('#apDiv1').css('display','');
		}
	}
});

function get_look_suggs(key,cont){ 
   var script_name = context+'/ajax/search/autocomplete.do';
   var params = { 'q':encodeURIComponent(key)}
   $.get(script_name,params,
         function(obj){ 
           // obj is just array of strings
           var res = [];
           for(var i=0;i<obj.length;i++){
             res.push({ id:i , value:obj[i]});
           }
           // will build suggestions list
           cont(res); 
         },
         'json');
}

$(document).ready(function(){   
	jQuery("body").append(getMsgBoxContent());
});

function showMessage(tipo,msg){
	if(tipo=='loading'){
		document.getElementById('_container').innerHTML=getLoadingContent();
	}else{
		$('#_container').html(getMsgContent());
		$('#_msg').html(msg);
		$('#_msg_icon').html(getTypeIcon(tipo));
	}
	$('#_x').css('display','block');
	$('#_inx').css('display','block');
}

function hideMessage(){
	$('#_x').css('display','none');
	$('#_inx').css('display','none');
}

function showLoading(){
	showMessage('loading');
}

function hideLoading(){
	hideMessage();
}

function getMsgBoxContent(){
	var _msg='';
	_msg+='<div id="_x">';
	_msg+='</div>';
	_msg+='<div id="_inx">';
	_msg+='	<table>';
	_msg+='		<tr>';
	_msg+='			<td align="center" valign="middle">';
	_msg+='				<div id="_container">';
	_msg+='				</div>';
	_msg+='			</td>';
	_msg+='		</tr>';
	_msg+='	</table>';
	_msg+='</div>';
	return _msg;
}

function getMsgContent(){
	var _msg='';
	_msg+='					<div id="_containermsg" class="msgbox">';
	_msg+='						<table border="0" cellpadding="0" cellspacing="0" width="100%" height="100%">';
	_msg+='							<tr>';
	_msg+='								<td rowspan="2" width="33" id="_msg_icon" style="padding-right: 15px;"></td>';
	_msg+='								<td align="center" id="_msg">&nbsp;</td>';
	_msg+='							</tr> ';
	_msg+='							<tr><td height="25" valign="bottom"><input type="button" value="   OK   " style="font-size: 8px;" onclick="hideMessage()"></td></tr>';
	_msg+='						</table>';
	_msg+='					</div>';
	return _msg;
}

function getLoadingContent(){
	return '<img src="'+context+'/images/lightbox/loadingAnimation.gif"/>';
}

function getTypeIcon(tipo){
	return '<img src="'+context+'/images/msgbox/icon-'+tipo+'.gif"/>';
}
