function DoConfirm(message, url) {
	if(confirm(message)) location.href = url;
}

function DoConfirmForm(text) {
	var agree;
	if (text != null)
		agree = confirm(text);
	else
		agree = confirm('Уверены?');
	if (agree) 
		return true;
	else 
		return false;
}

function show(el) {
	var eel = document.getElementById(el);
	if (eel.style.display) { eel.style.display='' }
	else { eel.style.display='none' }
}

function highlight(el, color) {
	var eel = document.getElementById(el);
	eel.style.background = color;
}

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

function insertAtCursor(myField, myValue) {
	textAreaScrollPosition = myField.scrollTop;
	//IE support
	if (document.selection) {
		myField.focus();

		//in effect we are creating a text range with zero
		//length at the cursor location and replacing it
		//with myValue
		sel = document.selection.createRange();
		sel.text = myValue;

	//Mozilla/Firefox/Netscape 7+ support
	} else if (myField.selectionStart || myField.selectionStart == '0') {

		myField.focus();
		//Here we get the start and end points of the
		//selection. Then we create substrings up to the
		//start of the selection and from the end point
		//of the selection to the end of the field value.
		//Then we concatenate the first substring, myValue,
		//and the second substring to get the new value.
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
		myField.setSelectionRange(endPos+myValue.length, endPos+myValue.length);
	} else {
		myField.value += myValue;
	}
	myField.scrollTop = textAreaScrollPosition;

}

function mozWrap(txtarea, open, close)
{
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd == 1 || selEnd == 2) 
		selEnd = selLength;

	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + open + s2 + close + s3;
	return;
}

function bbcode (icon)
{
	var txtarea = document.getElementById('comment');
	var theSelection = false;
	
	var clientPC = navigator.userAgent.toLowerCase(); // Get client info
	var clientVer = parseInt(navigator.appVersion); // Get browser version

	var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
	var is_win = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
	txtarea.focus();
	
	if ((clientVer >= 4) && is_ie && is_win)
	{
		theSelection = document.selection.createRange().text; // Get text selection
		if (theSelection) {
			document.selection.createRange().text = '[' + icon + ']' + theSelection + '[/' + icon + ']';
			txtarea.focus();
			theSelection = '';
			return;
		}
	}
	else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
	{
		mozWrap(txtarea, '[' + icon + ']', '[/' + icon + ']');
		return;
	}

	txtarea.value = txtarea.value + '[' + icon + ']' + '[/' + icon + ']';
	txtarea.focus();
}

function smile (icon)
{
	var txtarea = document.getElementById('comment');
	insertAtCursor(txtarea, ' ' + icon + ' ');
	txtarea.focus();
}

function stripHTMLTags(html)
{
   var tmp = document.createElement("DIV");
   tmp.innerHTML = html;
   return tmp.textContent||tmp.innerText;
}

function quote_user (field, user)
{
	var txtarea = document.getElementById('comment');
	var txt = document.getElementById(field).innerHTML;
	insertAtCursor(txtarea, '[quote name="' + user + '"]' + stripHTMLTags(txt) + '[/quote]');
	txtarea.focus();
}

function vote(id, v) {
	var url = '/vote.php?id=' + id + '&v=' + v
	window.location.href = url;
}

var requestPB;
var destPB;

function processStateChange()
{
	if (requestPB.readyState == 4)
	{
		contentDiv = document.getElementById(destPB);
		if (requestPB.status == 200)
		{
			response = requestPB.responseText;
			contentDiv.innerHTML = response;	
		}
		else
		{
			contentDiv.innerHTML = "Error: Status "+requestPB.status;
		}
	}
}

function getNewHTTPObject()
{
        var xmlhttp;

        /** Special IE only code ... */
        /*@cc_on
          @if (@_jscript_version >= 5)
              try
              {
                  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
              }
              catch (e)
              {
                  try
                  {
                      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                  }
                  catch (E)
                  {
                      xmlhttp = false;
                  }
             }
          @else
             xmlhttp = false;
        @end @*/

        /** Every other browser on the planet */
        if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
        {
            try
            {
                xmlhttp = new XMLHttpRequest();
            }
            catch (e)
            {
                xmlhttp = false;
            }
        }

        return xmlhttp;
}

function loadHTML(URL, destination)
{
	destPB = destination;
	requestPB = getNewHTTPObject();
	if (requestPB)
	{
		requestPB.onreadystatechange = processStateChange;
		requestPB.open("GET", URL, true);
		requestPB.send(null);
	}
}


function vote_pesnya(id, spanID, val)
{
	var callURL = '/vote.php?id=' + id + '&v=' + val;
	loadHTML(callURL, spanID);
}

function report_video_error(id, spanID)
{
	var callURL = '/vote.php?viderror=' + id;
	loadHTML(callURL, spanID);
}
