﻿var ajax_cursor_html = '<img src="' + SitePath + '/upload/images/hour_glass.gif" width="25" height="24" alt="Пожалуйста, подождите, пока загрузятся данные с сервера.">';
var ajax_wait_message = 'Пожалуйста, подождите, пока загрузятся данные с сервера.';
var ajax_cursor_class_name = 'hour_glass';
var cursor_x_shift = -2;
var cursor_y_shift = -2;

function trim(s) {
	return rtrim(ltrim(s));
} // end function

function ltrim(s) {
	return s.replace(/^\s+/, ''); 
} // end function

function rtrim(s) {
	return s.replace(/\s+$/, ''); 
} // end function
function findPos(obj)
{
    var curleft = curtop = 0;
    if (obj.offsetParent)
	{
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent)
		{
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
    	}
    }
    return [curleft,curtop];
}
function AddPageId(uri) {
	if (uri.indexOf('?') == -1) uri += '?';
	else if ((uri.charAt(uri.length - 1) != '?') && (uri.charAt(uri.length - 1) != '&')) uri += '&';
	uri += 'page.id=' + PageId;
	return uri;
}
function LoadPage(uri, divId, callback) {
	var div = document.getElementById(divId);
	if (div) {
		var event = 
			function (result) {
				div.style.visibility = "hidden";
				
				div.innerHTML = result;
				
                if (window.startAccordion) startAccordion();
				if (callback) eval(callback);
				div.style.visibility = "visible";
			} // end event
		HttpRequest(SitePath + AddPageId(uri), null, divId, event);		
	} // end if	
	return false;
} // end function

function ShowPhoto(link, image) {
	var div = document.getElementById("body");
	var popup = document.getElementById("fotka");
	if (div && popup) {
		var event = 
			function (result) {				
				popup.style.top = findPos(link)[1]-100+"px";
				popup.style.left = (document.body.offsetWidth/2)-200+"px";
				popup.innerHTML = result;
				showNextBanner();
				div.className="bgnone op";
				popup.style.display="block";
                //if (window.startAccordion) startAccordion();
				//if (callback) eval(callback);
			} // end event
		if (image) {
			HttpRequest(SitePath + AddPageId("/AJAX/getmenuimage.aspx?image="+image), null, popup, event);
		} else {
			HttpRequest(SitePath + AddPageId("/AJAX/getmenuimage.aspx?image=no-image.gif"), null, popup, event);
		}

	} // end if
}

function HidePhoto() {	
	var div = document.getElementById("body");
	var popup = document.getElementById("fotka");
	div.className="bgnone";
	popup.style.display="none";
}
// begin of Comment functions
function LeaveComment(link, dish_id) {
	var popup = document.getElementById("comment");
	if (popup) {
		var event = 
			function (result) {				
				popup.innerHTML = result;
				popup.style.display="block";
			} // end event
		HttpRequest(SitePath + AddPageId("/AJAX/leave_comment.aspx?dish_id="+dish_id), null, popup, event);
	} // end if
}
function HideComment() {	
	var popup = document.getElementById("comment");
	popup.style.display="none";
	popup.className="";
}
function ClearField(id,val)
{
	var obj = document.getElementById(id);
	if(obj.value==val)
		obj.value="";
	return false;
}
function CheckCommentForm(){
	var i, curvalue, defvalue;
	var ReqFields = Array("comment","name","control_key");
	var ReqFieldsNames = Array("Комментарий","Имя","Число антиспама");
	for (i=0; i < ReqFields.length; i++){
		curvalue = trim(eval("document.LeaveCommentForm." + ReqFields[i] + ".value"));
		defvalue = trim(eval("document.LeaveCommentForm." + ReqFields[i] + ".defaultValue"));
		if (curvalue == "" || curvalue == defvalue) {
			alert("Запоните поле " + ReqFieldsNames[i]);
			eval("document.LeaveCommentForm." + ReqFields[i] + ".focus()");
			return false;
		} 
	}
	return true;
}
function CheckCommentForm2(){
	var i, curvalue, defvalue;
	var ReqFields = "control_key";
	var ReqFieldsNames = "Число антиспама";
		curvalue = trim(eval("document.LeaveCommentForm." + ReqFields + ".value"));
		defvalue = trim(eval("document.LeaveCommentForm." + ReqFields + ".defaultValue"));
		if (curvalue == "" || curvalue == defvalue) {
			alert("Запоните поле " + ReqFieldsNames);
			eval("document.LeaveCommentForm." + ReqFields + ".focus()");
			return false;
		} 
	return true;
}
function AddComment(DishId,flag) {
	var i;
	if(flag==0)
		i=CheckCommentForm();
	else
		i=CheckCommentForm2();
	if(i){
		var popup = document.getElementById("comment");
		if (popup) {
			name = document.getElementById('name_field').value;
			comment = document.getElementById('comment_field').value;
			control_key	= document.getElementById('control_key').value;	
			var event =
			function (result) {
				popup.innerHTML = result;
				popup.style.display="block";
				return false;
			} // end event
			HttpRequest(SitePath + AddPageId("/AJAX/add_comment.aspx?dish_id=" +DishId + "&name=" + name + "&comment=" + comment + "&control_key=" + control_key), null, popup, event);		
		} // end if
	}
	return false;
}
// end of Comment functions

function DynamicBanner(divId, bannerId, positionId)
{
	this.div = document.getElementById(divId);
	this.bannerId = bannerId;
	this.positionId = positionId;
	this.ajaxUrl = 'AJAX/GetNextBanner.aspx?';
	var self = this;
	var reload = function()
	{
		var event = 
		function (result) {
		    var root = XmlRootByText(result);
   	        var currentBannerId = GetXmlNodeValue(GetXmlNodeByTagName(root, 'CurrentBannerId'));
			if (self.bannerId != currentBannerId) {
			
				self.div.innerHTML = GetXmlNodeValue(GetXmlNodeByTagName(root, 'HtmlContent'));
				self.bannerId = currentBannerId;
			}
			
		} // end event
		HttpRequest(SitePath + AddPageId(self.ajaxUrl + 'currentBannerId=' + self.bannerId + '&positionId=' + self.positionId), null, null, event);	
		window.setTimeout(reload, 5000);
	}
	window.setTimeout(reload, 5000);
	//window.setInterval(reload, 5000);
}

//Vote functions
function VotePopup(link, DishId) {
	var popup = document.getElementById("comment");
	if (popup) {
		var event = 
			function (result) {				
				popup.innerHTML = result;
				popup.style.display="block";
			} // end event
		HttpRequest(SitePath + AddPageId("/AJAX/dish_vote_popup.aspx?DishId="+DishId), null, popup, event);
	} // end if
}
function DoDishVote(ContainerId, DishId, Mark){
	var div = document.getElementById(ContainerId);
	//var Mark = document.getElementById('HiddenMark').value;
	if (div) {
		var event =
		function (result) {
			div.innerHTML = result;
		} // end event
		var stringParams = "";
		stringParams += (Mark == "" || Mark == undefined) ? "?Mark=0" : "?Mark=" + Mark + "&DishId=" +DishId;
		HttpRequest(SitePath + AddPageId('/AJAX/ajax_dish_vote.aspx' + stringParams), null, 'DoDishVote', event);
		VotePopup(this, DishId);
	} // end if
return false;
}
//end of Vote functions
