/* -------------------- general -------------------- */	

//ajax request
function ajax_request(method, parameters, callback)
	{
		/*
			examples: 
				1.
					ajax_request('get', 'script=example&id=' + id +'&section=' + section, function(data)
									{ 
										document.getElementById(element_id).innerHTML = data; 
									}
								);			
				2.
					ajax_request('get', 'script=example&id=' + id +'&section=' + section, function(data)
									{ 
										document.form_id.field_name.value = data; 
									}
								);
				3. 
					ajax_request('get', 'script=example&id=' + id +'&section=' + section, function(data)
									{ 
										alert(data);
									}
								);
				4.
					var parameters  = '';					
						parameters += 'field_one=' + encodeURIComponent( document.getElementById('field_one').value ) + '&'
						parameters += 'field_two=' + encodeURIComponent( document.getElementById('field_two').value )
									
					ajax_request('post', parameters, function(data)
									{ 
										document.getElementById(element_id).innerHTML = data; 
									}
								);														
		*/
		
		var xmlHttp;
			
		try
			{
				// Firefox, Opera 8.0+, Safari
				xmlHttp = new XMLHttpRequest();
			}
		catch (e)
			{
				// Internet Explorer
				try
					{
						xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
					}
				catch (e)
					{
						try
							{
								xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
							}
						catch (e)
							{
								alert('Your browser does not support AJAX!');
								return false;
							}
					}
			}
					
		if (method == 'post')
			{			
				xmlHttp.open('POST', ajax_script, true);
				xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
				xmlHttp.setRequestHeader('Content-length', parameters.length);
				xmlHttp.setRequestHeader('Connection', "close");
				xmlHttp.onreadystatechange = function() {
					if (xmlHttp.readyState == 4 && xmlHttp.status == 200) 
						{
							callback(xmlHttp.responseText);
						}
				};						
				xmlHttp.send(parameters);				
			}
		else
			{			
				xmlHttp.open('GET', ajax_script + '?' + parameters, true);
				xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');				
				xmlHttp.onreadystatechange = function() {
					if (xmlHttp.readyState == 4 && xmlHttp.status == 200) 
						{
							callback(xmlHttp.responseText);
						}
				};				
				xmlHttp.send(null);			
			}				
	}

//bookmark
function add_to_bookmark(url, title)
	{
		if (document.all)
			{
				window.external.AddFavorite(url, title);
			}
		else
			{
				alert('Press CTRL+D to bookmark page');
			}
	}

//show element
function show_element(id)
	{
		$('#' + id).show();
	}

//hide element
function hide_element(id)
	{
		$('#' + id).hide();
	}
		
// -------------------- common functions -------------------- //

//select smile
function select_smile(emoticon)
	{
		//prepare variables
		var message = $('#c_content').val();
		
		//do
		$('#c_content').val(message + ' ' + emoticon);
		$('#c_content').scrollTop = $('#c_content').scrollHeight; 
		$('#c_content').focus();
	}
	
//character count
function charCount(field, show, limit) 
	{
		//prepare variables
		var field_value  = $('#' + field).val();
		var field_length = field_value.length;
		
		//do
		if (field_length > limit) {
			$(field_value).val($(field_value).val().substring(0, limit));
		} else {
			alert
			var char_left = limit - field_length;
			$('#' + show).text(char_left + ' characters left');
		}
	}
	
//show loading
function show_loading(element, message)
	{
		//show 'loading'
		$('#' + element).html('<div class="loading"><img src="' + img_link + '/loading.gif" /> ' + message + '</div>');	
	}

//open window
function ventana(theURL, winName, features) { 
	window.open(theURL, winName, features);
}

// -------------------- specific -------------------- //
		
//show lef comment box
function show_left_comment(type)
	{
		//call php script
		ajax_request('get', 'script=show_left_comment&type=' + type, function(data) {
			$('#box_left_comment').html(data); 
		});
	}
	
/* -------------------- comment system -------------------- */

//show comments
function show_comments(section, id, page)
	{
		//show 'loading'
		show_loading('box_comment_list', 'Please wait, comments list is loading ...');
		
		//call php script
		ajax_request('get', 'script=show_comments&section=' + section + '&id=' + id + '&page=' + page, function(data) { 
			$('#box_comment_list').html(data); 
		});
	}

//submit comment	
function submit_comment(section, id) 
	{
		//prepare variables
		var element_id  = 'box_comment_form'
		var parameters  = 'script=submit_comment&section=' + section + '&id=' + id + '&'
			parameters += 'c_position=' + encodeURIComponent( $('#c_position').val() ) + '&'		
			parameters += 'c_name=' + encodeURIComponent( $('#c_name').val() ) + '&'
			parameters += 'c_email=' + encodeURIComponent( $('#c_email').val() ) + '&'
			parameters += 'c_webpage=' + encodeURIComponent( $('#c_webpage').val() ) + '&'
			parameters += 'c_content=' + encodeURIComponent( $('#c_content').val() ) + '&'
			parameters += 'verify_string=' + encodeURIComponent( $('#verify_string').val() )

		//call php script - post comment
		ajax_request('post', parameters, function(data) {
			if (data.search('ERROR: ') == 0)
				{
					//show 'box_comment_form' + error
					$('#box_comment_form').html(data.replace('ERROR:', ''));
				}
			else
				{
					//reset fields
					$('#c_error').text('');
					$('#c_count').text('');
					$('#c_content').val('');
					$('#verify_string').val('');
										
					//show comment list
					$('#box_comment_list').html(data);

					//scroll to comment list
					var top_offset = $('#box_comment_list').offset().top;
			
					$(window).scrollTop(top_offset - 200);	
				}							
		});
	}

//rate comment	
function rate_comment(section, id, action) 
	{
		//call php script
		ajax_request('get', 'script=rate_comment&id=' + id + '&section=' + section + '&action=' + action, function(data) {
			if (data) $('#comment_rating_' + id).html(data);
		});
	}
	
//reply comment	
function reply_comment(id) 
	{
		//set position
		$('#c_position').val(id);
		
		//set focus
		$('#c_content').focus();
	}	

//view hidden comment
function view_hidden_comment(section, id)
	{
		//call php script
		ajax_request('get', 'script=view_hidden_comment&section=' + section + '&id=' + id, function(data) {
			if (data)
				{
					$('#comment_content_' + id).html(data);
					$('#comment_rating_' + id).removeClass('hidden');
				}
		});
	}
