// image pre-loading
var ajaxObj = null;
var global_lang = null;
var sendQueue = null;
var loading_image = new Image();
loading_image.src = "images/saving.gif";


function CreateAjaxObject()
{
	try
	{
		ajaxObj = new XMLHttpRequest();
	}
	catch(e)
	{
		var arr = new Array("MSXML2.XMLHTTP.6.0",
							"MSXML2.XMLHTTP.5.0",
							"MSXML2.XMLHTTP.4.0",
							"MSXML2.XMLHTTP.3.0",
							"MSXML2.XMLHTTP",
							"Microsoft.XMLHTTP");

		for (var i = 0; i < arr.length && !ajaxObj; i++)
		{
			try
			{
				ajaxObj = new ActiveXObject(arr[i]);
			}
			catch(e)
			{}
		}
	}
}

function SetLanguage (str)
{
	global_lang = str;
}

function SendAjaxRequest (str)
{
	if (ajaxObj == null)
	{
		CreateAjaxObject();
		sendQueue = new Array();
	}

	if (ajaxObj)
	{
		sendQueue.push(str);
		SendNextRequest();
	}
}

function SendNextRequest()
{
	if ((ajaxObj.readyState == 4 || ajaxObj.readyState == 0) && sendQueue.length > 0)
	{
		var str = sendQueue.shift();
		ajaxObj.open("POST", str, true);
		ajaxObj.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		ajaxObj.setRequestHeader('Cache-Control', 'no-cache');
		if (global_lang != null)
			ajaxObj.setRequestHeader('Accept-Language', global_lang);

		ajaxObj.onreadystatechange = onServerResponse;
		ajaxObj.send("");
	}
}

function ShowTab (li_id, div_id)
{
	document.getElementById(li_id).className = 'active';
	document.getElementById(div_id).style.display = 'block';
}

function HideTab (li_id, div_id)
{
	document.getElementById(li_id).className = 'not_active';
	document.getElementById(div_id).style.display = 'none';
}

function ShowItem (item_id, show)
{
	var item = document.getElementById(item_id);
	if (show)
		item.style.visibility = "visible";
	else
		item.style.visibility = "hidden";
}

function HideItem (item_id)
{
    ShowItem(item_id, false);
}

function DisplayItem (id, how)
{
	document.getElementById(id).style.display = how;
}

function IsItemVisibile (id)
{
	return document.getElementById(id).style.display != 'none';
}

function ToggleItem (id)
{
    if (IsItemVisibile(id))
        DisplayItem(id, 'none');
    else
        DisplayItem(id, 'block');
}

function SetValue (control, value)
{
	control.value = value;
}

function mouseOver (control)
{
	control.style.filter = "alpha(opacity=50)"; // IE
	control.style.opacity = 0.5; // CSS standard
}

function mouseOut (control)
{
	control.style.filter = "alpha(opacity=100)";
	control.style.opacity = 1;
}

function focusOnSearch (control)
{
	if (control.className.indexOf(" search_field_focus") != -1)
		control.className = control.className.replace(" search_field_focus", "");
}

function focusOutSearch (control)
{
	if (control.value == '' && control.className.indexOf("search_field_focus") == -1)
		control.className += " search_field_focus";
}

function mouseOverSearchList (control)
{
	if (control.className.indexOf(" search_extra_hilight") == -1)
		control.className += " search_extra_hilight";
}

function mouseOutSearchList (control)
{
	if (control.className.indexOf("search_extra_hilight") != -1)
		control.className = control.className.replace(" search_extra_hilight", "");
}

function closeSearchListList()
{
    var item = document.getElementById('search_list');
    item.style.visibility = "hidden";
}

function clickSearchList()
{
	var item = document.getElementById('search_list');
	if (item.style.visibility == "hidden")
		item.style.visibility = "visible";
	else
		item.style.visibility = "hidden";
}

function selectSearchType (type, search)
{
    document.getElementById('search_where').innerHTML = type;
    document.getElementById('search_list').style.visibility = "hidden";
    document.getElementById('search_form').type.value = search;
}

function comment_on_keyup (id_text_area, id_tr)
{
	var ctl_text = document.getElementById(id_text_area);
	var ctl_tr = document.getElementById(id_tr);
	if (ctl_text.value == "")
		DisplayItem(id_tr, 'none');
	else
		DisplayItem(id_tr, 'block');
}

function comment_on_focus (id_text_area)
{
	var control = document.getElementById(id_text_area);
	if (control.className.indexOf("disabled_text") != -1)
	{
		control.value = "";
		control.className = control.className.replace("disabled_text", "enabled_text");
	}
}

function comment_on_blur (id_text_area, text)
{
	var control = document.getElementById(id_text_area);
	if (control.value == "")
	{
		control.value = text + "..."; //"Scrivi un commento...";
		control.className = control.className.replace("enabled_text", "disabled_text");
	}
}

function mouseClickStar (row, col)
{
	var item = document.getElementById("span_" + row);
	item.name = col;
	setStars(row, col);
	
	var votes = checkAllVoted();
	if (votes)
	{
		var link = document.getElementById("vote_button");
		link.style.visibility = "visible";
		
		var index = link.href.indexOf("&value=");
		if (index == -1)
			index = link.href.length;
	
		var new_link = link.href.substr(0, index) + "&value=" + votes;
		link.href = new_link;
	}
}

function checkAllVoted()
{
	var votes = "";
	for (var i = 1; i < 5; i++)
	{
		var item = document.getElementById("span_" + i);
		if (!item.name)
			return "";
		
		votes += item.name;
	}
	return votes;
}

function setStars (row, col)
{
	for (var i = 1; i <= col; i++) // set
	{
		var id = "row" + row + "col" + i;
		var image = "star_yellow";
		if (col == 5)
			image = "star_red";
			
		document.getElementById(id).src = "images/" + image + ".png";
	}
	
	for (; i <= 5; i++) //unset
	{
		var id = "row" + row + "col" + i;
		document.getElementById(id).src = "images/star_gray.png";
	}
}

function mouseOverStar (row, col)
{
	if (document.getElementById("span_" + row).name)
		return;
		
	setStars(row, col);
}

function mouseOutStar (row, col)
{
	if (document.getElementById("span_" + row).name)
		return;
	
	setStars(row, 0);
}

function GetProfileLink (user_id)
{
  return '<a href="index.php?page=profile&amp;id=' + user_id + '">';
}

function GetBorderClass (s)
{
    var border = 'gray_border';
    //s = strtolower(sex);
    if (s == 'm')
        border = 'cyan_border';
    else if (s == 'f')
        border = 'pink_border';
    else if (s == 'v') // valid photo - used for album check
        border = 'green_border';
    else if (s == 'y') // waiting for approval
        border = 'yellow_border';
    else if (s == 's') // image selected
        border = 'select_border';

    return border;
}

function GetUserPreview (name, filename, sex)
{
	var border = GetBorderClass(sex);
	var link;
	if (filename == '')
		link = 'images/no_photo_mini_48.png';
	else
		link = 'users48x48/' + filename;
		
	var width = 48;
	var height = 48;
	var html = '<img class="photo ' + border + '" src="' + link +
			'" width="' + width + '" height="' + height + '" border="0" alt="' + /*htmlspecialchars*/(name) + '" />';
	return html;
}

// private method for UTF-8 decoding
function utf8_decode (utftext)
{
	var string = "";
	var i = 0;
	var c = c1 = c2 = 0;

	while ( i < utftext.length ) {

		c = utftext.charCodeAt(i);

		if (c < 128) {
			string += String.fromCharCode(c);
			i++;
		}
		else if((c > 191) && (c < 224)) {
			c2 = utftext.charCodeAt(i+1);
			string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
			i += 2;
		}
		else {
			c2 = utftext.charCodeAt(i+1);
			c3 = utftext.charCodeAt(i+2);
			string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
			i += 3;
		}

	}
	return string;
}

function base64_decode(data)
{
	var b64array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var input = data;
    var output = "";
    var hex = "";
    var chr1, chr2, chr3 = "";
    var enc1, enc2, enc3, enc4 = "";
    var i = 0;

    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

    do {
        enc1 = b64array.indexOf(input.charAt(i++));
        enc2 = b64array.indexOf(input.charAt(i++));
        enc3 = b64array.indexOf(input.charAt(i++));
        enc4 = b64array.indexOf(input.charAt(i++));
        
        chr1 = (enc1 << 2) | (enc2 >> 4);
        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
        chr3 = ((enc3 & 3) << 6) | enc4;
        
        output = output + String.fromCharCode(chr1);
        
        if (enc3 != 64) {
            output = output + String.fromCharCode(chr2);
        }
        if (enc4 != 64) {
            output = output + String.fromCharCode(chr3);
        }
    
        chr1 = chr2 = chr3 = "";
        enc1 = enc2 = enc3 = enc4 = "";
    
    } while (i < input.length);
	return output;
}
