﻿var WWW = 'http://www.aquarelle.md/';

function CommentListingPage (Page)
{
	var CommentCount = GetIt ('CommentsCount').value;
	var CommentsPageLimit = GetIt ('CommentsPageLimit').value;
	var PagesCount = Math.ceil(CommentCount/CommentsPageLimit);
	
	for (i=1; i<=CommentCount; i++)
	{
		ShowState = (i>(Page-1)*CommentsPageLimit && i<=Page*CommentsPageLimit) ? 'block' : 'none';
		GetIt('CommentMessage'+i).style.display = ShowState;
	}
	for (i=1; i<=PagesCount; i++)
	{
		ShowState = (i==Page) ? 'ListingBoxLinkSelected' : 'ListingBoxLink';
		GetIt('CommentPage'+i).className = ShowState;
	}
}
var RatingState = true;
function Rating (State, Value, Target)
{
	var step = 26;
	
	if (RatingState)
	{
		if (State=='over') GetIt ('Rating').style.backgroundPosition = (-(10-Value)*step)+'px 0px';
		else if (State=='out') GetIt ('Rating').style.backgroundPosition = (-(10-Value)*step)+'px 0px';
		else if (State=='click') 
		{
			DoRequest ('RatingVote', Value, Target);
			RatingState = false;
		}
	}
}
function PopupLive (link)
{
	var width = 600;
	var height = 235;

	var live = window.open(link,"_blank", "toolbar=no,width="+width+",height="+height+",directories=no,menubar=no,scrollbars=no,resizable=no");
	live.focus();
}

function PopupFlashMagazine (link)
{
	var width = 706;
	var height = 556;

	var win = window.open(link,"_blank", "toolbar=no,width="+width+",height="+height+",directories=no,menubar=no,scrollbars=no,resizable=no");
	win.focus();
}

function SetStyle(Type, TypeRecord, ElementId, StartId, EndId, ThisId, ThisValue, Value)
{
	for (var i=StartId; i<=EndId; i++) 
	{
		if (Type=='style') GetIt (ElementId+i).style[TypeRecord] = (i==ThisId) ? ThisValue : Value;
		if (Type=='class') GetIt (ElementId+i).className = (i==ThisId) ? ThisValue : Value;
	}
}
function EscapeString (str)
{
	str = str.replace(/\n/g, escape("\n"));
	str = str.replace(/&/g, escape("&"));
	str = str.replace(/=/g, escape("="));
	
	return str;
}
function AddComment (id)
{
	var author = GetIt ('user').value;
	var message = GetIt ('message').value;
	var code = GetIt ('code').value;
	var option1 = 'id='+id+'&author='+EscapeString(author)+'&message='+EscapeString(message)+'&code='+EscapeString(code);
	DoRequest ('AddComment', option1);
}
function DoRequest (type, option1, option2, option3, option4)
{
	var URL = new GetURL(type, option1);
	var RequestURL = WWW+'AjaxRequest.php?';
	
	if (type=='Zodiac')
	{
		link = RequestURL+'&type='+type+'&id='+option1;
		URL.Get(link);
	}
	if (type=='PollVote')
	{
		var PollOption = -1;
		for (i=0; i<document.forms['BlockPoll'+option1]['PollVote'+option1].length; i++)
		{
            if (document.forms['BlockPoll'+option1]['PollVote'+option1][i].checked==true) PollOption = i;
        }
		if (PollOption<0) return;
		else link = RequestURL+'&type='+type+'&id='+option1+'&answer='+PollOption;
		//alert (link);
		URL.Get(link);
	}
	else if (type=='PollVoteMultiplePublic')
	{
		var Ids = option1.split('|');
		var link = RequestURL+'&type='+type;
		
		for (j=0; j<Ids.length; j++)
		{
			var PollOption = -1;
			if (document.forms['BlockPoll'+Ids[j]]['PollVote'+Ids[j]])
			{
				for (i=0; i<document.forms['BlockPoll'+Ids[j]]['PollVote'+Ids[j]].length; i++)
				{
					if (document.forms['BlockPoll'+Ids[j]]['PollVote'+Ids[j]][i].checked==true) PollOption = i;
				}
			}
			if (PollOption > -1)link += '&poll'+j+'='+Ids[j]+'&answer'+j+'='+PollOption;
		}
		URL.Get(link);
	}
	else if (type=='PollVote2')
	{
		var GetOptions = '';
		for (i=1; i<=option2; i++)
		{
			if (GetIt('item'+i).checked) GetOptions += '&option_id'+i+'='+GetIt('itemVal'+i).value;
		}
		link = RequestURL+'&type='+type+'&id='+option1+GetOptions;
		//alert (link);
		//return;
		URL.Get(link);
	}
	else if (type=='PollVoteMultiple')
	{
		var GetOptions = '';
		var GetOptionsCount = 0;
		for (i=0; i<option2; i++)
		{
			for (j=0; j<document.forms.MultipleContest['item'+i].length; j++)
			{
				if (document.forms.MultipleContest['item'+i][j].checked)
				{
					GetOptions += '&option_id'+i+'='+document.forms.MultipleContest['item'+i][j].value;
					GetOptionsCount++;
				}
			}
		}
		if (GetOptionsCount < option2) 
		{
			alert (option3);
			return false;
		}
		else 
		{
			option4.className = 'nds';
			link = RequestURL+'&type='+type+'&id='+option1+GetOptions;
		}
		//alert (link); return;
		URL.Get(link);
	}
	else if (type=='AddComment')
	{
		link = RequestURL+'&type='+type+'&'+option1;
		//alert (link);
		URL.Get(link);
	}
	else if (type=='HomeSectionDataSet')
	{
		link = RequestURL+'&type='+type+'&section='+option1+'&page='+option2;
		//alert (link);
		URL.Get(link);
	}
	else if (type=='RatingVote')
	{
		link = RequestURL+'&type='+type+'&value='+option1+'&id='+option2;
		//alert (link);
		URL.Get(link);
	}
	else if (type=='AdsVote')
	{
		link = RequestURL+'&type='+type+'&value='+GetIt('AdsPoll'+option1).value+'&id='+option1+'&section='+option2;
		//alert (link);
		URL.Get(link);
	}
}
function SetRequestData (type, data, option)
{
	//alert (data);
	if (type=='Zodiac') GetIt ('SideBoxInfoZodiacContent').innerHTML = data;
	if (type=='PollVote') {
		GetIt ('HomePollContent'+option).innerHTML = data;
	}
	if (type=='PollVoteMultiplePublic') {
		if (data=='Ok') location.reload();
	}
	if (type=='PollVote2') 
	{
		if (eval(data)) location.reload();
		//alert (data);
		//GetIt ('HomePollContent').innerHTML = data;
	}
	if (type=='PollVoteMultiple') 
	{
		if (eval(data)) location.reload();
		//alert (data);
		//GetIt ('HomePollContent').innerHTML = data;
	}
	if (type=='HomeSectionDataSet') GetIt ('HomeSectionBox'+option).innerHTML = data;
	if (type=='AddComment')
	{
		if (data == 'error1') alert ('Не все * обязательные поля заполнены');
		else if (data == 'error2') 
		{
			GetIt ('CommentsImageCode').src = GetIt ('CommentsImageCodePath').value+'?'+ Math.random();
			alert ('Неверный код');			
		}
		else 
		{
			alert (data);
			//GetIt ('CommentsList').innerHTML += data;
			//GetIt('CommentsMessage').style.display = 'block';
			//GetIt('CommentsMessage').style.width = GetIt('Comments').offsetWidth+'px';
			//GetIt('CommentsMessage').style.height = GetIt('Comments').offsetHeight+'px';
			
			//GetIt('CommentsMessage').innerHTML = '<div class="CloseButton" onClick="GetIt(\'CommentsMessage\').style.display=\'none\';">x</div><div class="PostMessage">'+data+'</div>';
			//GetIt('MessagesBox').style.display = 'block';
			//GetIt('CommentsListAdContent').innerHTML += data;
			GetIt ('message').value = '';
			GetIt ('code').value = '';
			GetIt ('CommentsImageCode').src = GetIt ('CommentsImageCodePath').value+'?'+ Math.random();
		}
	}
	if (type=='RatingVote' && data != '') GetIt ('RatingBox').innerHTML = data;
	if (type=='AdsVote' && data != '') GetIt ('AdsRatingBox'+option).innerHTML = data;
}
function GetURL (type, option)
{
	var xmlhttp, alerted;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
		try {
			xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
		} catch (e) {
			try {
				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
			} catch (E) {
				alert("You must have Microsofts XML parsers available")
			}
		 }
	@else
		alert("You must have JScript version 5 or above.")
		xmlhttp=false
		alerted=true
	@end @*/

	if (!xmlhttp && !alerted) {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			alert("You need a browser which supports an XMLHttpRequest Object.\nMozilla build 0.9.5 has this Object and IE5 and above, others may do, I don't know, any info jim@jibbering.com")
		}
	}
	function RSchange() {
		if (xmlhttp.readyState==4) {
			//alert(xmlhttp.responseText);
			SetRequestData (type, xmlhttp.responseText, option);
		}
	}
	this.Get = function (url){
		if (xmlhttp) { 
			xmlhttp.open("GET", url+'&rand='+ Math.random(), true);
			xmlhttp.onreadystatechange = RSchange;
			xmlhttp.send(null);
		}
	}
	this.Request = function (url){
		if (xmlhttp) { 
			xmlhttp.open("GET", url+'&rand='+ Math.random(), true);
			xmlhttp.send(null);
		}
	}
}
function CreateBookmarkLink()
{
	var title = document.title; 
	var url = window.location.href;
	
	if (window.sidebar)
	{	// Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	}
	else if( window.external)
	{	// IE Favorite
		window.external.AddFavorite( url, title); 
	}
	else if (window.opera && window.print) 
	{	// Opera Hotlist
		return true;
	}
}

function GetIt (id) { return document.getElementById (id); }

function MultiSelector( list_target, inp){
	this.inp = inp;
	this.list_target = list_target;
	this.count = 0;
	this.id = 0;
	this.addElement = function( element ){
		element.name = this.inp+'_' + this.id++;
		element.multi_selector = this;	
		element.onchange = function(){
			var new_element = document.createElement( 'input' );
			new_element.type = 'file';
			new_element.className = 'tb_af';
			this.parentNode.insertBefore( new_element, this );
			this.multi_selector.addElement( new_element );
			this.multi_selector.addListRow( this );
			this.style.position = 'absolute';				
			this.style.left = '-1000px';			
		};
		this.count++;
		this.current_element = element;
	};
	this.addListRow = function( element ){
		var new_row = document.createElement( 'div' );
		var new_row_button = document.createElement( 'a' );
		new_row_button.href = '#';
		new_row_button.appendChild(document.createTextNode('удалить'));
		new_row_button.className = 'TextBold';
		new_row.element = element;
		new_row_button.onclick= function(){
			if (this.parentNode.element.multi_selector.count==2) document.getElementById('space').style.position="absolute";

			this.parentNode.element.parentNode.removeChild( this.parentNode.element );
			this.parentNode.parentNode.removeChild( this.parentNode );
			this.parentNode.element.multi_selector.count--;
			this.parentNode.element.multi_selector.current_element.disabled = false;
			return false;
		};
		if (this.count>=2) document.getElementById('space').style.position="relative";

		new_row.innerHTML = element.value+"&nbsp; &nbsp;";
		new_row.appendChild( new_row_button);
		this.list_target.appendChild( new_row );
	};
};

