// Emarket.com.do
var interval_value = true;

function open_url(url)
{
	document.location.href = url;
}

function link_jump(el, url)
{
	if(window.confirm(el))
	{
		document.location.href = url;
	}
}

function url_anuncios(url)
{
	window.parent.location.href = url;
}

function change_url(id, campo, contenido)
{
	cont = document.getElementById(id);
	cont.innerHTML = contenido + campo.value;
}

function display_cont(id_view, style_view)
{
	var elemento = document.getElementById(id_view);
	
	elemento.style.display = style_view;
}

function display_if(id_view, cont, valor)
{
	var elemento = document.getElementById(id_view);
	var elemento_cont = document.getElementById(cont);
	
	if(elemento_cont.value == valor)
	{
		elemento.style.display = '';
	}
	else
	{
		elemento.style.display = 'NONE';
	}
}

function country_city(campo, valor, id_view, view_campo, id_select, select_campo, name_campo, name_temp)
{
	if(campo.value == valor)
	{
		display_cont(id_view, 'NONE');
		display_cont(id_select, '');
		document.getElementById(select_campo).name = name_campo;
		document.getElementById(view_campo).name = name_temp;
		document.getElementById(select_campo).value = '';
	}
	else
	{
		display_cont(id_view, '');
		display_cont(id_select, 'NONE');
		document.getElementById(view_campo).name = name_campo;
		document.getElementById(select_campo).name = name_temp;
		document.getElementById(view_campo).value = '';
	}
}

function display_view(id_cont, id_item, id_text, text, text_old, id_search, id_temp_search)
{
	var elemento 			= document.getElementById(id_item);
	var elemento_text 		= document.getElementById(id_text);
	var elemento_cont 		= document.getElementById(id_cont);
	var elemento_type 		= document.getElementById(id_search);
	var elemento_type_temp 	= document.getElementById(id_temp_search);
	
	
	if(elemento.style.display == 'NONE' || elemento.style.display == 'none')
	{
		elemento.innerHTML = elemento_cont.innerHTML;
		elemento_cont.innerHTML = '';
		elemento.style.display = '';
		elemento_text.innerText = text;
		if(elemento_type)
		{
			if(elemento_type_temp.value != '' || elemento_type_temp.value != null)
			{
				elemento_type.value = elemento_type_temp.value;
			}
		}
	}
	else
	{
		if(elemento_type)
		{
			elemento_type_temp.value = elemento_type.value;
			elemento_type.value = '';
		}
		elemento_cont.innerHTML = elemento.innerHTML;
		elemento.innerHTML = '';
		elemento.style.display = 'none';
		elemento_text.innerText = text_old;
	}
}

function price_view(campo, ch_one, ch_two)
{
	if(campo.checked)
	{
		document.getElementById(ch_one).disabled = true;
		document.getElementById(ch_two).disabled = true;
	}
	else
	{
		document.getElementById(ch_one).disabled = false;
		document.getElementById(ch_two).disabled = false;
	}
}

function price_view_ofert(campo, ch_one, ch_two, ch_tree)
{
	if(campo.checked)
	{
		document.getElementById(ch_one).disabled = true;
		document.getElementById(ch_two).disabled = true;
		document.getElementById(ch_tree).disabled = true;
	}
	else
	{
		document.getElementById(ch_one).disabled = false;
		document.getElementById(ch_two).disabled = false;
		document.getElementById(ch_tree).disabled = false;
	}
}

function public_anuncio(url, campos)
{
	var len_array = campos.length;
	var url_temp = '';
	
	if(len_array > 0)
	{
		var x = 0;
		
		for(x=0; x<=len_array; x++)
		{
			if(document.getElementById(campos[x]))
			{
				if(document.getElementById(campos[x]).checked == true)
				{
					url_temp += '&' + campos[x] + '=1';
				}
			}
		}
		
		open_url(url + url_temp);
	}
}

function ismaxlength(obj)
{	
	var mlength = obj.getAttribute ? parseInt(obj.getAttribute("maxlength")) : ""
	
	if (obj.getAttribute && obj.value.length>mlength)
	{
		obj.value = obj.value.substring(0, mlength)
		return obj.value;
	}
}

function write_id(id, valor_id, campo, valor, url_img, extra_campo)
{
	var data_temp;
	document.getElementById(id).value = valor_id;
	data_temp = '<table width="200" border="0" cellspacing="0" cellpadding="5" class="send_user_mail">';
	data_temp += '<tr>';
	data_temp += '<td width="182" align="left">' + valor + '</td>';
	data_temp += '<td width="18" align="right"><a href="javascript:delete_id(\'' + id + '\', \'' + campo + '\', \'' + extra_campo + '\')"><img src="' + url_img + 'icon/delete_user.png" width="18" height="18" border="0" /></a></td>';
	data_temp += '</tr>';
	data_temp += '</table>';
	document.getElementById(campo).innerHTML = data_temp;
}

function delete_id(id, campo, extra_campo)
{
	document.getElementById(id).value = '0';
	document.getElementById(campo).innerHTML = document.getElementById(extra_campo).innerHTML;
}

function star_view(num, num_star, url_img)
{
	if(num >= 0)
	{
		var x;
		var contenido = '';
		
		for(x=1; x<=num_star; x++)
		{
			if(x > num)
			{
				document.getElementById('star_num' + x).src = url_img + 'icon/extrella_off.gif';
			}
			else
			{
				document.getElementById('star_num' + x).src = url_img + 'icon/extrella.gif';
			}
		}
	}
}

function winOpen(URL, windowName, width, height, resizable, location, menubar, scrollbars, status, toolbar){
	var windowFeatures;
	windowFeatures = '';
	if (width != '' && width != null){
		windowFeatures = windowFeatures+'width='+width+',';
	}
	if (height != '' && height != null){
		windowFeatures = windowFeatures+'height='+height+',';
	}
	if (resizable){
		windowFeatures = windowFeatures+'resizable=' + resizable + ',';
	}
	if (location){
		windowFeatures = windowFeatures+'location,';
	}
	if (menubar){
		windowFeatures = windowFeatures+'menubar,';
	}
	if (scrollbars){
		windowFeatures = windowFeatures+'scrollbars,';
	}
	if (status){
		windowFeatures = windowFeatures+'status,';
	}
	if (toolbar){
		windowFeatures = windowFeatures+'toolbar,';
	}
	window.open(URL, windowName, windowFeatures);
}

function clear_campo(campo, valor)
{
	if(campo.value == valor)
	{
		campo.value = '';
		campo.focus();
	}
}

function URLEncode(clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

function Ajax_view()
{
	var xmlhttp=false;
 	try {
 		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 	} catch (e) {
 		try {
 			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 		} catch (E) {
 			xmlhttp = false;
 		}
  	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
 		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

function info_view(valor, url, cont_id, elim)
{
	var contenedor, text_temp;
	 
	contenedor = document.getElementById(cont_id);
	
	if(elim != '')
	{
		document.getElementById(elim).innerHTML = '';
	}
	
	ajax = Ajax_view();
	ajax.open("GET", url + valor, true);
	ajax.onreadystatechange = function()
	{
		if(ajax.readyState==1)
		{
			contenedor.innerHTML = '<strong>Espere por favor...</strong>';
		}
		else if(ajax.readyState==4)
		{
			contenedor.innerHTML = ajax.responseText
		}
	}
	ajax.send(null)
}

function search_mail()
{
	var valor_temporal = URLEncode(document.getElementById('search_people').value);
	info_view(valor_temporal, 'PLUGIN/VIEW_AJAX/user_list=', 'result_user', 'result_user');
}

function search_mail_campo(e)
{	
	if (e == null) { // ie
		keycode = window.event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}
	if(keycode == 13) {
		search_mail();
	}	
}

function chat_load_user()
{
	var contenedor;
	 
	contenedor = document.getElementById('chat_view_online');
	
	ajax = Ajax_view();
	ajax.open("GET", 'PLUGIN/VIEW_AJAX/chat=online', true);
	ajax.onreadystatechange = function()
	{
		if(ajax.readyState==1)
		{
			contenedor.innerHTML = '<strong>Espere por favor...</strong>';
		}
		else if(ajax.readyState==4)
		{
			contenedor.innerHTML = ajax.responseText
			
			if(contenedor.innerHTML != '')
			{
				clear_chat();
			}
		}
	}
	ajax.send(null)
}

function chat_interval(id_anuncio, text_campo, codec)
{
	id_interval = setInterval("chat_view(1, '" + id_anuncio + "', '" + text_campo + "', '" + codec + "')", 5000);
}

function chat_interval_clear()
{
	clearInterval(id_interval);
}

function chat_print_mens(mens)
{
	var contenedor = document.getElementById('context_chat');
	contenedor.innerHTML += mens;
}

function chat_print_close(mens)
{
	var contenedor = document.getElementById('context_chat');
	contenedor.innerHTML += mens;
	interval_value = false;
}

function chat_view(type, id_anuncio, text_campo, codec)
{
	chat_interval_clear();
	
	var contenedor = document.getElementById('context_chat');
	var text = document.getElementById(text_campo).value;
	var temporal = '';
	
	if(type == 1)
	{
		var url = 'PLUGIN/ANUNCIO/anuncio=' + id_anuncio + '&view=chat&action=last_mens&codec=' + codec;
	}
	else if(type == 2)
	{
		var url = 'PLUGIN/ANUNCIO/anuncio=' + id_anuncio + '&view=chat&action=new&codec=' + codec + '&text=' + URLEncode(text);
		document.getElementById(text_campo).value = '';
	}
	else
	{
		var url = 'PLUGIN/ANUNCIO/anuncio=' + id_anuncio + '&view=chat&action=last_mens&codec=' + codec;
		document.getElementById(text_campo).value = '';
	}
	
	ajax = Ajax_view();
	ajax.open("GET", url, true);
	ajax.onreadystatechange = function()
	{
		if(ajax.readyState==4)
		{
			temporal = ajax.responseText;
			eval(temporal);
			contenedor.scrollTop = contenedor.scrollHeight;
		}
	}
	ajax.send(null)
	
	if(interval_value == true)
	{
		chat_interval(id_anuncio, text_campo, codec);
	}
}